package com.zhgd.xmgl.modules.basicdata.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhgd.xmgl.modules.basicdata.dto.EngineeringDeviceDto;
import com.zhgd.xmgl.modules.basicdata.dto.ProjectDeviceDto;
import com.zhgd.xmgl.modules.basicdata.entity.AiMonitorAlarm;
import com.zhgd.xmgl.modules.basicdata.entity.AiMonitorDev;
import com.zhgd.xmgl.modules.basicdata.entity.SystemDictData;
import com.zhgd.xmgl.modules.basicdata.mapper.AiMonitorAlarmMapper;
import com.zhgd.xmgl.modules.basicdata.mapper.AiMonitorDevMapper;
import com.zhgd.xmgl.modules.basicdata.mapper.AiMonitorQuestionMapper;
import com.zhgd.xmgl.modules.basicdata.service.IAiMonitorAlarmService;
import com.zhgd.xmgl.modules.basicdata.service.IEngineeringService;
import com.zhgd.xmgl.modules.basicdata.service.IProjectService;
import com.zhgd.xmgl.modules.basicdata.service.ISystemDictDataService;
import com.zhgd.xmgl.security.SecurityUser;
import com.zhgd.xmgl.security.SecurityUtil;
import com.zhgd.xmgl.util.ParamEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: AI预警记录
 * @author： pengj
 * @date：   2023-03-04
 * @version： V1.0
 */
@Service
public class AiMonitorAlarmServiceImpl extends ServiceImpl<AiMonitorAlarmMapper, AiMonitorAlarm> implements IAiMonitorAlarmService {

    @Autowired
    private IEngineeringService engineeringService;

    @Autowired
    private IProjectService projectService;

    @Autowired
    private AiMonitorQuestionMapper aiMonitorQuestionMapper;

    @Autowired
    private AiMonitorDevMapper aiMonitorDevMapper;

    @Autowired
    private ISystemDictDataService systemDictDataService;

    @Autowired
    private AiMonitorAlarmMapper aiMonitorAlarmMapper;

    @Override
    public Page<EngineeringDeviceDto> engineeringPageList(Map<String, Object> map) {
        SecurityUser user = SecurityUtil.getUser();
        Page<EngineeringDeviceDto> pageListByGov = new Page<>();
        if (user.getAccountType() == 2) {
            pageListByGov = engineeringService.pageListForGov(map);
        }
        if (user.getAccountType() == 3) {
            pageListByGov = engineeringService.pageListForEnt(map);
        }
        pageListByGov.getRecords().stream().forEach(e -> {
            // TODO 待整改
            e.setQuestionNum(aiMonitorQuestionMapper.getCountByStateForEng(e.getEngineeringSn(), 0));
            e.setSolveQuestionNum(aiMonitorQuestionMapper.getCountByStateForEng(e.getEngineeringSn(), 2));
            e.setDeviceNum(aiMonitorDevMapper.selectCount(Wrappers.<AiMonitorDev>lambdaQuery().eq(AiMonitorDev::getEngineeringSn, e.getEngineeringSn())));
            e.setTodayAlarm(aiMonitorAlarmMapper.getCountForToDay(null, e.getEngineeringSn()));
        });
        return pageListByGov;
    }

    @Override
    public Page<ProjectDeviceDto> projectPageList(Map<String, Object> map) {
        Page<ProjectDeviceDto> pageListByGov = projectService.pageListForGov(map);
        pageListByGov.getRecords().stream().forEach(e -> {
            // TODO 待整改
            e.setQuestionNum(aiMonitorQuestionMapper.getCountByStateForPro(e.getProjectSn(), 0));
            e.setSolveQuestionNum(aiMonitorQuestionMapper.getCountByStateForPro(e.getProjectSn(), 2));
            e.setDeviceNum(aiMonitorDevMapper.selectCount(Wrappers.<AiMonitorDev>lambdaQuery()
                    .eq(AiMonitorDev::getProjectSn, e.getProjectSn())
                    .eq(AiMonitorDev::getEngineeringSn, "")));
            e.setTodayAlarm(aiMonitorAlarmMapper.getCountForToDay(e.getProjectSn(), null));
        });
        return pageListByGov;
    }

    @Override
    public Page<AiMonitorAlarm> pageList(Page page, Wrapper<AiMonitorAlarm> wrapper) {
        List<SystemDictData> dictDataList = systemDictDataService.getByType(ParamEnum.SysDictType.AI_ALARM_TYPE.getValue());
        Page<AiMonitorAlarm> aiMonitorAlarmPage = baseMapper.pageList(page, wrapper);
        aiMonitorAlarmPage.getRecords().stream().forEach(a -> {
            List<SystemDictData> dictData = dictDataList.stream().filter(d -> d.getDictLabel().equals(a.getType().toString()))
                    .collect(Collectors.toList());
            if (dictData.size() > 0) {
                a.setTypeName(dictData.get(0).getDictValue());
            }
        });
        return aiMonitorAlarmPage;
    }

    @Override
    public List<Map<String, Object>> getCountGroupByType(String deviceCode) {
        List<Map<String, Object>> list = baseMapper.getCountGroupByType(deviceCode);
        List<SystemDictData> dictDataList = systemDictDataService.getByType(ParamEnum.SysDictType.AI_ALARM_TYPE.getValue());
        list.stream().forEach(m -> {
            List<SystemDictData> data = dictDataList.stream().filter(d -> d.getDictLabel().equals(m.get("type").toString())).collect(Collectors.toList());
            if (data.size() > 0) {
                m.put("typeName", data.get(0).getDictValue());
            }
        });
        return list;
    }
}
