package com.ozs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ozs.common.core.domain.AjaxResult;
import com.ozs.common.core.domain.entity.SysUser;
import com.ozs.common.utils.AppendUtils;
import com.ozs.common.utils.StringUtils;
import com.ozs.common.utils.bean.BeanUtils;
import com.ozs.entity.BaseCameraManagement;
import com.ozs.entity.BaseRailwayManagement;
import com.ozs.entity.MsgAlarm;
import com.ozs.entity.MsgWebPush;
import com.ozs.entity.vo.MsgAlarmResp;
import com.ozs.entity.vo.MsgAlarmVo;
import com.ozs.mapper.MsgWebPushMapper;
import com.ozs.service.BaseCameraManagementService;
import com.ozs.service.BaseRailwayManagementService;
import com.ozs.service.MsgWebPushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author : sunhh
 * @create 2023/3/15 14:34
 */
@Service
public class MsgWebPushServiceImpl extends ServiceImpl<MsgWebPushMapper, MsgWebPush> implements MsgWebPushService {
    @Autowired
    private MsgWebPushMapper msgWebPushMapper;
    @Autowired
    private BaseCameraManagementService baseCameraManagementService;
    @Autowired
    private BaseRailwayManagementService baseRailwayManagementService;

    @Override
    public int read(MsgWebPush msgWebPush) {
        // 状态：1未读；2已读
        msgWebPush.setStatus(2);
        return msgWebPushMapper.updateByPushId(msgWebPush);
    }

    @Override
    public Map webAlarm(String userId) {
        // List<String> userIds = baseCameraManagementService.getUserIds(userId);
        Map<String, Object> webAlarmMap = new HashMap<>();
        List<MsgAlarmResp> msgAlarmRespList = new ArrayList<>();
        Integer webAlarmNum = msgWebPushMapper.webAlarmNum(userId);
        List<MsgAlarm> webAlarm = msgWebPushMapper.webAlarm(userId);
        if (!StringUtils.isEmpty(webAlarm) && webAlarm.size() > 0) {
            for (MsgAlarm msgAlarm : webAlarm) {
                LambdaQueryWrapper<BaseRailwayManagement> wrapper = new LambdaQueryWrapper<>();
                if (!StringUtils.isEmptySunhh(msgAlarm.getRailwayCode())) {
                    wrapper.eq(BaseRailwayManagement::getRailwayCode, msgAlarm.getRailwayCode());
                }
                BaseRailwayManagement baseRailwayManagement = baseRailwayManagementService.getOne(wrapper);
                if (StringUtils.isEmptySunhh(baseRailwayManagement)) {
                    return AjaxResult.error("线路编码：" + msgAlarm.getRailwayCode() + "不存在！");
                }
                // 查询里程位置最近的相机
                LambdaQueryWrapper<BaseCameraManagement> lw = new LambdaQueryWrapper<>();
                if (!StringUtils.isEmptySunhh(msgAlarm.getRailwayCode())) {
                    lw.eq(BaseCameraManagement::getRailwayCode, msgAlarm.getRailwayCode());
                }
                if (!StringUtils.isEmptySunhh(msgAlarm.getLineDir())) {
                    lw.eq(BaseCameraManagement::getLineDir, msgAlarm.getLineDir());
                }
                if (!StringUtils.isEmptySunhh(msgAlarm.getAlarmMile())) {
                    lw.le(BaseCameraManagement::getBeginMile, msgAlarm.getAlarmMile());
                    lw.gt(BaseCameraManagement::getEndMile, msgAlarm.getAlarmMile());
                }
                List<BaseCameraManagement> baseCameraManagementList = baseCameraManagementService.list(lw);
                BaseCameraManagement baseCameraManagement = new BaseCameraManagement();
                if (!StringUtils.isEmptySunhh(baseCameraManagementList) && baseCameraManagementList.size() > 0) {
                    baseCameraManagement = baseCameraManagementList.get(0);
                }
                if (StringUtils.isEmptySunhh(baseCameraManagement)) {
                    return AjaxResult.error("里程位置：" + msgAlarm.getAlarmMile() + "，不存在关联相机！");
                }
                MsgAlarmResp msgAlarmResp = new MsgAlarmResp();
                BeanUtils.copyProperties(msgAlarm, msgAlarmResp);
                msgAlarmResp.setRailwayName(baseRailwayManagement.getRailwayName());
                // msgAlarmResp.setDeptId(baseCameraManagement.getDeptId());
                String begin = AppendUtils.stringAppend(baseCameraManagement.getBeginMile());
                String end = AppendUtils.stringAppend(baseCameraManagement.getEndMile());
                String endMile = begin + "-" + end;
                msgAlarmResp.setBeginMile(endMile);
                String mile = AppendUtils.stringAppend(baseCameraManagement.getInstallMile());
                msgAlarmResp.setAlarmMile(mile);
                msgAlarmResp.setInstallLongitude(baseCameraManagement.getInstallLongitude() + "," + baseCameraManagement.getInstallLatitude());
                msgAlarmResp.setPushId(msgAlarm.getMsgId());
                msgAlarmResp.setCameraCode(baseCameraManagement.getCameraCode());
                msgAlarmRespList.add(msgAlarmResp);
            }
        }
        webAlarmMap.put("webAlarmNum", webAlarmNum);
        webAlarmMap.put("webAlarm", msgAlarmRespList);
        return webAlarmMap;
    }

    @Override
    public IPage<MsgAlarm> messageMore(MsgAlarmVo msgAlarmVo) {
        Page<MsgAlarm> page = PageHelper.startPage(msgAlarmVo.getPageNum().intValue(), msgAlarmVo.getPageSize().intValue())
                .doSelectPage(() -> msgWebPushMapper.messageMore(msgAlarmVo));
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<MsgAlarm> pageR =
                new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(msgAlarmVo.getPageNum().intValue(), msgAlarmVo.getPageSize().intValue());
        pageR.setSize(page.size());
        pageR.setTotal(page.getTotal());
        // Page<MsgAlarm> page = PageUtils.getPages(msgAlarmVo.getPageNum().intValue(), msgAlarmVo.getPageSize().intValue(), webAlarm);
        if (!ObjectUtils.isEmpty(page) && !ObjectUtils.isEmpty(page.getResult())) {
            List<MsgAlarm> dto1 = page.getResult().stream().map(o -> {
//                String[] split = o.getImageUrl().split(";");
//                ArrayList<String> objects = new ArrayList<>(Arrays.asList(split));
//                o.setImageUrls(objects);
                LambdaQueryWrapper<BaseRailwayManagement> wrapper = new LambdaQueryWrapper<>();
                if (!ObjectUtils.isEmpty(o.getRailwayCode())) {
                    wrapper.eq(BaseRailwayManagement::getRailwayCode, o.getRailwayCode());
                }
                BaseRailwayManagement baseRailwayManagement = baseRailwayManagementService.getOne(wrapper);
                if (StringUtils.isEmptySunhh(baseRailwayManagement)) {
//                    return AjaxResult.error("线路编码：" + o.getRailwayCode() + "不存在！");
                }
                // 查询里程位置最近的相机
                LambdaQueryWrapper<BaseCameraManagement> lw = new LambdaQueryWrapper<>();
                if (!StringUtils.isEmptySunhh(o.getRailwayCode())) {
                    lw.eq(BaseCameraManagement::getRailwayCode, o.getRailwayCode());
                }
                if (!StringUtils.isEmptySunhh(o.getLineDir())) {
                    lw.eq(BaseCameraManagement::getLineDir, o.getLineDir());
                }
                if (!StringUtils.isEmptySunhh(o.getAlarmMile())) {
                    lw.le(BaseCameraManagement::getBeginMile, o.getAlarmMile());
                    lw.gt(BaseCameraManagement::getEndMile, o.getAlarmMile());
                }
                List<BaseCameraManagement> baseCameraManagementList = baseCameraManagementService.list(lw);
                BaseCameraManagement baseCameraManagement = new BaseCameraManagement();
                if (!StringUtils.isEmptySunhh(baseCameraManagementList) && baseCameraManagementList.size() > 0) {
                    baseCameraManagement = baseCameraManagementList.get(0);
                    o.setRailwayName(baseRailwayManagement.getRailwayName());
                    String mils = AppendUtils.stringAppend(baseCameraManagement.getInstallMile());
                    o.setAlarmMiles(mils);
                    String end = AppendUtils.stringAppend(baseCameraManagement.getEndMile());
                    o.setEndMile(end);
                    String begin = AppendUtils.stringAppend(baseCameraManagement.getBeginMile());
                    o.setBeginMile(begin);
                }
                // o.setAlarmCount((int) msgAlarmFrequencyService.count()+1);
                return o;
            }).collect(Collectors.toList());
            pageR.setRecords(dto1);
        }
        return pageR;
    }

    @Override
    public Map<String, Integer> messageMoreNum(String userId) {
        Map<String, Integer> map = msgWebPushMapper.messageMoreNum(userId);
        return map;
    }

    @Override
    public int readAll(String userId) {
        return msgWebPushMapper.readAll(userId);
    }

    @Override
    public List<MsgWebPush> getByUserId(String userId) {
        return msgWebPushMapper.getByUserId(userId);
    }
}
