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.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ozs.common.core.domain.AjaxResult;
import com.ozs.common.utils.AppendUtils;
import com.ozs.common.utils.StringUtils;
import com.ozs.entity.BaseCameraManagement;
import com.ozs.entity.BaseDeviceDynamicManagement;
import com.ozs.entity.BaseRailwayManagement;
import com.ozs.entity.MsgAlarm;
import com.ozs.entity.vo.*;
import com.ozs.mapper.BaseCameraManagementMapper;
import com.ozs.mapper.BaseDeviceDynamicManagementMapper;
import com.ozs.mapper.BaseRailwayManagementMapper;
import com.ozs.mapper.MsgAlarmMapper;
import com.ozs.service.MsgAlarmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 报警信息表 服务实现类
 * </p>
 *
 * @author ozs
 * @since 2023-02-22
 */
@Service
public class MsgAlarmServiceImpl extends ServiceImpl<MsgAlarmMapper, MsgAlarm> implements MsgAlarmService {
    @Autowired
    private MsgAlarmMapper msgAlarmMapper;
    @Autowired
    private BaseCameraManagementMapper baseCameraManagementMapper;
    @Autowired
    private BaseRailwayManagementMapper baseRailwayManagementMapper;
    @Autowired
    private BaseDeviceDynamicManagementMapper baseDeviceDynamicManagementMapper;

    public IPage listToPage(List list, int pageNum, int pageSize) {
        List pageList = new ArrayList<>();
        int curIdx = pageNum > 1 ? (pageNum - 1) * pageSize : 0;
        for (int i = 0; i < pageSize && curIdx + i < list.size(); i++) {
            pageList.add(list.get(curIdx + i));
        }
        IPage page = new Page<>(pageNum, pageSize);
        page.setRecords(pageList);
        page.setTotal(list.size());
        return page;
    }

    //web数据统计 报警数据详情
    @Override
    public List<AlarmStatisticDto> list(String railwayCode, Integer currentYear, Integer currentMonth, Integer pageNum, Integer pageSize) {
        List<AlarmStatisticDto> list = msgAlarmMapper.list(railwayCode, currentYear, currentMonth);
        return list;
    }

    @Override
    public List<AlarmStatisticMonthDto> getCount(String railwayCode, Integer currentYear, Integer currentMonth) {
        List<AlarmStatisticMonthDto> list = msgAlarmMapper.listDtoMonth(railwayCode, currentYear, currentMonth);
        return list;
    }

    @Override
    public List<AlarmStatisticMonthDto> getDayCount(String railwayCode, Integer currentYear, Integer currentMonth) {
        List<AlarmStatisticMonthDto> list = msgAlarmMapper.listDtoDay(railwayCode, currentYear, currentMonth);
        return list;
    }

    @Override
    public List<AlarmStatisticMonthDto> getAlarmTypeCount(String railwayCode, Integer currentYear, Integer currentMonth) {
        List<AlarmStatisticMonthDto> list = msgAlarmMapper.alarmTypeCount(railwayCode, currentYear, currentMonth);
        return list;
    }

    @Override
    public List<AlarmFreqArea> alarmTypeAreaCount(String railwayCode, Integer currentYear, Integer currentMonth) {
        List<AlarmFreqArea> list = msgAlarmMapper.alarmTypeAreaCount(railwayCode, currentYear, currentMonth);
        return list;
    }

    @Override
    public List<AlarmStatisticDto> exportExcel(AlarmStatisticResVo alarmStatisticResVo) {
        List<AlarmStatisticDto> listDto = msgAlarmMapper.list(alarmStatisticResVo.getRailwayCode(), alarmStatisticResVo.getCurrentYear(),
                alarmStatisticResVo.getCurrentMonth());
        return listDto;
    }

    @Override
    public Long getAlarmNum() {
        Long alarmNum = msgAlarmMapper.getAlarmNum();
        return alarmNum;
    }

    @Override
    public MsgAlarm selectByCameraCode(String cameraCode) {
        return msgAlarmMapper.selectByCameraCode(cameraCode);
    }

    @Override
    public IPage<MsgAlarm> queryPage(MsgAlarmVo vo) {
        int pageNum = Integer.parseInt(vo.getPageNum().toString());
        int pageSize = Integer.parseInt(vo.getPageSize().toString());
        com.github.pagehelper.Page<MsgAlarm> page = PageHelper.startPage(pageNum, pageSize)
                .doSelectPage(() -> msgAlarmMapper.selectMsgAlarmList(vo));
        Page<MsgAlarm> pageR = new Page<>(pageNum, pageSize);
        pageR.setRecords(page.getResult());
        pageR.setTotal(page.getTotal());
        return pageR;
    }

    @Override
    public MsgAlarm getByAlarmId(String alarmId) {
        return msgAlarmMapper.getByAlarmId(alarmId);
    }

    @Override
    public Map<String, Integer> statisticsNum(MsgAlarmVo msgAlarmVo) {
        Map<String, Integer> map = new HashMap<>();
        // 相机数量
        Integer countCamera = baseCameraManagementMapper.countCamera(msgAlarmVo);
        // 累计报警次数
        Integer countMsg = msgAlarmMapper.countMsg(msgAlarmVo);
        // 解除报警
//        LambdaQueryWrapper<MsgAlarm> lwJC = new LambdaQueryWrapper<MsgAlarm>();
//        lwJC.eq(MsgAlarm::getIsLock, "1");
        Integer countJC = msgAlarmMapper.countJC(msgAlarmVo);
        // 未解除报警
//        LambdaQueryWrapper<MsgAlarm> lwWJC = new LambdaQueryWrapper<MsgAlarm>();
//        lwWJC.eq(MsgAlarm::getIsLock, "2");
        Integer countWJC = msgAlarmMapper.countWJC(msgAlarmVo);
        map.put("countCamera", countCamera);
        map.put("countMsg", countMsg);
        map.put("countJC", countJC);
        map.put("countWJC", countWJC);
        return map;
    }

    @Override
    public Integer appAlarmNum(MsgAlarmVo msgAlarmVo) {
        return msgAlarmMapper.countWJC(msgAlarmVo);
    }

    @Override
    public List<MsgAlarm> msgAlarmList(MsgAlarmVo msgAlarmVo) {
        return msgAlarmMapper.selectMsgAlarmList(msgAlarmVo);
    }

    @Override
    public AjaxResult getDate(MsgAlarmVo msgAlarmVo) {
        // 查询报警信息
        MsgAlarmVo msgAlarmVo1 = new MsgAlarmVo();
        MsgAlarm msgAlarm = msgAlarmMapper.selectById(msgAlarmVo.getId());
        if (StringUtils.isEmptySunhh(msgAlarm)) {
            return AjaxResult.error("报警信息不存在！");
        }
        // 查询线路
        LambdaQueryWrapper<BaseRailwayManagement> lwBaseRailwayManagement = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmptySunhh(msgAlarmVo.getRailwayCode())) {
            lwBaseRailwayManagement.eq(BaseRailwayManagement::getRailwayCode, msgAlarmVo.getRailwayCode());
        }
        BaseRailwayManagement baseRailwayManagement = baseRailwayManagementMapper.selectOne(lwBaseRailwayManagement);
        if (StringUtils.isEmptySunhh(baseRailwayManagement)) {
            return AjaxResult.error("线路编码：" + msgAlarmVo.getRailwayCode() + "不存在！");
        }
        // 查询里程位置最近的相机
        LambdaQueryWrapper<BaseCameraManagement> lw = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmptySunhh(msgAlarmVo.getRailwayCode())) {
            lw.eq(BaseCameraManagement::getRailwayCode, msgAlarmVo.getRailwayCode());
        }
        if (!StringUtils.isEmptySunhh(msgAlarmVo.getAlarmMile())) {
            lw.le(BaseCameraManagement::getBeginMile, msgAlarmVo.getAlarmMile());
            lw.gt(BaseCameraManagement::getEndMile, msgAlarmVo.getAlarmMile());
        }
        List<BaseCameraManagement> baseCameraManagementList = baseCameraManagementMapper.selectList(lw);
        if (!ObjectUtils.isEmpty(baseCameraManagementList)) {
            BaseCameraManagement baseCameraManagement = baseCameraManagementList.get(0);
            if (!ObjectUtils.isEmpty(baseCameraManagement.getCameraCode())) {
                LambdaQueryWrapper<BaseDeviceDynamicManagement> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(BaseDeviceDynamicManagement::getCameraCode, baseCameraManagement.getCameraCode());
                BaseDeviceDynamicManagement baseDeviceDynamicManagement = baseDeviceDynamicManagementMapper.selectOne(queryWrapper);
                if (!StringUtils.isEmptySunhh(baseDeviceDynamicManagement)) {
                    msgAlarmVo1.setElectricity(baseDeviceDynamicManagement.getElectricity());
                }
            }
            msgAlarmVo1.setLineDirStr(baseCameraManagement.getLineDir() == 1 ? "上行" : "下行");
            msgAlarmVo1.setCameraCode(baseCameraManagement.getCameraCode());
        }
        // BeanUtils.copyProperties(msgAlarm, msgAlarmVo1);
        msgAlarmVo1.setRailwayCode(msgAlarm.getRailwayCode());
        msgAlarmVo1.setRailwayCodeName(baseRailwayManagement.getRailwayName());
        msgAlarmVo1.setAlarmMiles(AppendUtils.stringAppend(msgAlarmVo.getAlarmMile()));
        msgAlarmVo1.setIsReleaseName(msgAlarm.getIsRelease() == 1 ? "已解除" : "未解除");
        return AjaxResult.success(msgAlarmVo1);
    }
}
