package com.usky.fire.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.core.exception.BusinessException;
import com.usky.common.core.util.StringUtils;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.fire.domain.*;
import com.usky.fire.mapper.PatrolInspectionPlanMapper;
import com.usky.fire.mapper.PatrolInspectionPlanSonMapper;
import com.usky.fire.mapper.PatrolInspectionRecordMapper;
import com.usky.fire.service.*;
import com.usky.fire.service.util.OnlineMethod;
import com.usky.fire.service.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-07-18
 */
@Service
public class PatrolInspectionPlanSonServiceImpl extends AbstractCrudService<PatrolInspectionPlanSonMapper, PatrolInspectionPlanSon> implements PatrolInspectionPlanSonService {

    @Autowired
    private PatrolInspectionPlanScheduleService patrolInspectionPlanScheduleService;

    @Autowired
    private PatrolInspectionPlanMapper patrolInspectionPlanMapper;

    @Autowired
    private PatrolInspectionPlanSonMapper patrolInspectionPlanSonMapper;

    @Autowired
    private PatrolInspectionPlanSiteSonService patrolInspectionPlanSiteSonService;

    @Autowired
    private PatrolInspectionSiteService patrolInspectionSiteService;

    @Autowired
    private PatrolInspectionSiteContentService patrolInspectionSiteContentService;

    @Autowired
    private PatrolInspectionContentService patrolInspectionContentService;

    @Autowired
    private PatrolInspectionContentOptionService patrolInspectionContentOptionService;

    @Autowired
    private PatrolInspectionRecordMapper patrolInspectionRecordMapper;

    @Autowired
    private PatrolInspectionAreaService patrolInspectionAreaService;

    @Autowired
    private PatrolInspectionRecordOptionService patrolInspectionRecordOptionService;

    @Autowired
    private PatrolInspectionRecordPictureService patrolInspectionRecordPictureService;

    @Autowired
    private PatrolInspectionPersonnelService patrolInspectionPersonnelService;

    @Autowired
    private PatrolInspectionEventService patrolInspectionEventService;

    @Autowired
    private PatrolInspectionAbnormalService patrolInspectionAbnormalService;

    @Autowired
    private PatrolInspectionSiteStoreService patrolInspectionSiteStoreService;

    private static final Logger LOGGER = LoggerFactory.getLogger(PatrolInspectionPlanSonServiceImpl.class);

    @Override
    public Map<String, Object> appPlanStatistics(String currentDate) {
        Integer planSonCount = 0;
        Integer patrolledCount = 0;
        Integer undetectedCount = 0;
        List<Integer> personnelIdList = this.getpersonId();
        if (personnelIdList.size() > 0) {
            LambdaQueryWrapper<PatrolInspectionPlanSchedule> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.select(PatrolInspectionPlanSchedule::getPlanId)
                    .in(PatrolInspectionPlanSchedule::getPersonnelId, personnelIdList)
                    .groupBy(PatrolInspectionPlanSchedule::getPlanId);
            List<PatrolInspectionPlanSchedule> planScheduleList = patrolInspectionPlanScheduleService.list(queryWrapper);
            if (planScheduleList.size() > 0) {
                List<Integer> planIdList = new ArrayList<>();
                for (int i = 0; i < planScheduleList.size(); i++) {
                    planIdList.add(planScheduleList.get(i).getPlanId());
                }
                planSonCount = this.planSonCount(planIdList, null, currentDate);
                patrolledCount = this.planSonCount(planIdList, 100, currentDate);
                undetectedCount = planSonCount - patrolledCount;
            }
        }

        Map<String, Object> map = new HashMap<>();
        map.put("planSonCount", planSonCount);// 巡检任务总数
        map.put("patrolledCount", patrolledCount);// 已巡检任务
        map.put("undetectedCount", undetectedCount);// 漏检任务
        return map;
    }

    @Override
    public List<PatrolInspectionPlanSonVo> patrolInspectionPlan(String currentDate, String sort) {
        List<PatrolInspectionPlanSonVo> planList = new ArrayList<>();
        List<Integer> personnelIdList = this.getpersonId();
        if (personnelIdList.size() > 0) {
            LambdaQueryWrapper<PatrolInspectionPlanSchedule> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.select(PatrolInspectionPlanSchedule::getPlanId)
                    .in(PatrolInspectionPlanSchedule::getPersonnelId, personnelIdList);
            if (sort.equals("DESC")) {
                queryWrapper.orderByDesc(PatrolInspectionPlanSchedule::getId);
            } else {
                queryWrapper.orderByAsc(PatrolInspectionPlanSchedule::getId);
            }
//            queryWrapper.groupBy(PatrolInspectionPlanSchedule::getPlanId);
            List<PatrolInspectionPlanSchedule> planScheduleList = patrolInspectionPlanScheduleService.list(queryWrapper);
            if (planScheduleList.size() > 0) {
                List<Integer> planIdList = new ArrayList<>();
                for (int i = 0; i < planScheduleList.size(); i++) {
                    planIdList.add(planScheduleList.get(i).getPlanId());
                }
                planList = this.patrolInspectionPlanSon(planIdList, currentDate, sort);
            }
        }

        return planList;
    }

    @Override
    public List<PatrolInspectionPlanSonVo> planList(String siteNubmber) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        List<PatrolInspectionPlanSonVo> list = new ArrayList<>();
        List<Integer> personnelIdList = this.getpersonId();
        if (personnelIdList.size() > 0) {
            LambdaQueryWrapper<PatrolInspectionSite> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(PatrolInspectionSite::getEnable, 1);
            queryWrapper.eq(PatrolInspectionSite::getTenantId, SecurityUtils.getTenantId())
                    .eq(PatrolInspectionSite::getSiteNubmber, siteNubmber);
            List<PatrolInspectionSite> siteList = patrolInspectionSiteService.list(queryWrapper);
            if (siteList.size() <= 0) {
                throw new BusinessException("无效地点,请联系管理人员添加点位");
            }

            LambdaQueryWrapper<PatrolInspectionPlanSchedule> queryWrapperOne = Wrappers.lambdaQuery();
            queryWrapperOne.select(PatrolInspectionPlanSchedule::getPlanId)
                    .in(PatrolInspectionPlanSchedule::getPersonnelId, personnelIdList)
                    .groupBy(PatrolInspectionPlanSchedule::getPlanId);
            List<PatrolInspectionPlanSchedule> planScheduleList = patrolInspectionPlanScheduleService.list(queryWrapperOne);
            if (planScheduleList.size() > 0) {
                List<Integer> planIdList = new ArrayList<>();
                for (int i = 0; i < planScheduleList.size(); i++) {
                    planIdList.add(planScheduleList.get(i).getPlanId());
                }
                List<PatrolInspectionPlanSonVo> planSonList = this.patrolInspectionPlanSon(planIdList, formatter.format(new Date()), "DESC");
                List<Integer> planSonIdList = new ArrayList<>();
                for (int i = 0; i < planSonList.size(); i++) {
                    planSonIdList.add(planSonList.get(i).getId());
                }
                LambdaQueryWrapper<PatrolInspectionPlanSiteSon> queryWrapperTwo = Wrappers.lambdaQuery();
                queryWrapperTwo.select(PatrolInspectionPlanSiteSon::getPlanId)
                        .eq(PatrolInspectionPlanSiteSon::getSiteId, siteList.get(0).getId())
                        .in(PatrolInspectionPlanSiteSon::getPlanId, planSonIdList)
                        .eq(PatrolInspectionPlanSiteSon::getInspectionStatus, 1);
                List<PatrolInspectionPlanSiteSon> planSiteSonList = patrolInspectionPlanSiteSonService.list(queryWrapperTwo);
                for (int i = 0; i < planSonList.size(); i++) {
                    for (int j = 0; j < planSiteSonList.size(); j++) {
                        int id = planSonList.get(i).getId();
                        int planId = planSiteSonList.get(j).getPlanId();
                        if (id == planId) {
                            planSonList.get(i).setSiteId(siteList.get(0).getId());
                            list.add(planSonList.get(i));
                        }
                    }
                }
            }
        }
        return list;
    }


    @Override
    public Map<String, Object> siteDetails(Integer siteId, String siteNubmber, Integer planSonId) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<Integer> personnelIdList = this.getpersonId();
        if (personnelIdList.size() <= 0) {
            PatrolInspectionAbnormal patrolInspectionAbnormal = new PatrolInspectionAbnormal();
            patrolInspectionAbnormal.setCreateTime(LocalDateTime.now());
            patrolInspectionAbnormal.setCreator(SecurityUtils.getUsername());
            patrolInspectionAbnormal.setPlanSonId(planSonId);
            patrolInspectionAbnormal.setSiteId(siteId);
            patrolInspectionAbnormal.setSiteNubmber(siteNubmber);
            patrolInspectionAbnormal.setAbnormalStatus(2);
            patrolInspectionAbnormal.setTenantId(SecurityUtils.getTenantId());
            patrolInspectionAbnormal.setAreaName("未知区域1");
            patrolInspectionAbnormal.setName(SecurityUtils.getLoginUser().getSysUser().getNickName());
            patrolInspectionAbnormal.setPhone(SecurityUtils.getLoginUser().getSysUser().getPhonenumber());
            patrolInspectionAbnormal.setEndDate(LocalDateTime.now());
            patrolInspectionAbnormal.setStartDate(LocalDateTime.now());
            patrolInspectionAbnormalService.save(patrolInspectionAbnormal);
            throw new BusinessException("该地点不在计划中");
        }
        LambdaQueryWrapper<PatrolInspectionSite> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PatrolInspectionSite::getTenantId, SecurityUtils.getTenantId());
        queryWrapper.eq(PatrolInspectionSite::getEnable, 1);
        if (siteId != 0 && siteId != null) {
            queryWrapper.eq(PatrolInspectionSite::getId, siteId);
        }
        if (StringUtils.isNotBlank(siteNubmber)) {
            queryWrapper.eq(PatrolInspectionSite::getSiteNubmber, siteNubmber);
        }
        List<PatrolInspectionSite> siteList = patrolInspectionSiteService.list(queryWrapper);
        if (siteList.size() <= 0) {
            PatrolInspectionAbnormal patrolInspectionAbnormal = new PatrolInspectionAbnormal();
            patrolInspectionAbnormal.setCreateTime(LocalDateTime.now());
            patrolInspectionAbnormal.setCreator(SecurityUtils.getUsername());
            patrolInspectionAbnormal.setPlanSonId(planSonId);
            patrolInspectionAbnormal.setSiteId(siteId);
            patrolInspectionAbnormal.setSiteNubmber(siteNubmber);
            patrolInspectionAbnormal.setAbnormalStatus(2);
            patrolInspectionAbnormal.setTenantId(SecurityUtils.getTenantId());
            patrolInspectionAbnormal.setAreaName("未知区域2");
            patrolInspectionAbnormal.setName(SecurityUtils.getLoginUser().getSysUser().getNickName());
            patrolInspectionAbnormal.setPhone(SecurityUtils.getLoginUser().getSysUser().getPhonenumber());
            patrolInspectionAbnormal.setEndDate(LocalDateTime.now());
            patrolInspectionAbnormal.setStartDate(LocalDateTime.now());
            patrolInspectionAbnormalService.save(patrolInspectionAbnormal);
            throw new BusinessException("无效地点,请联系管理人员添加点位");
        }
        int planSonSiteCount = patrolInspectionPlanMapper.planSiteCount(personnelIdList, siteList.get(0).getId(), dateFormat.format(new Date()));
        if (planSonSiteCount <= 0) {
            PatrolInspectionAbnormal patrolInspectionAbnormal = new PatrolInspectionAbnormal();
            patrolInspectionAbnormal.setCreateTime(LocalDateTime.now());
            patrolInspectionAbnormal.setCreator(SecurityUtils.getUsername());
            patrolInspectionAbnormal.setPlanSonId(planSonId);
            patrolInspectionAbnormal.setSiteId(siteId);
            patrolInspectionAbnormal.setSiteNubmber(siteNubmber);
            patrolInspectionAbnormal.setAbnormalStatus(2);
            patrolInspectionAbnormal.setTenantId(SecurityUtils.getTenantId());
            patrolInspectionAbnormal.setAreaName("未知区域3");
            patrolInspectionAbnormal.setName(SecurityUtils.getLoginUser().getSysUser().getNickName());
            patrolInspectionAbnormal.setPhone(SecurityUtils.getLoginUser().getSysUser().getPhonenumber());
            patrolInspectionAbnormal.setEndDate(LocalDateTime.now());
            patrolInspectionAbnormal.setStartDate(LocalDateTime.now());
            patrolInspectionAbnormalService.save(patrolInspectionAbnormal);
            throw new BusinessException("此地点尚未采集，请采集该点位");
        }
        Integer siteIdOne = 0;
        if (siteId != 0 && siteId != null) {
            siteIdOne = siteId;
        } else {
            siteIdOne = siteList.get(0).getId();
        }
        LambdaQueryWrapper<PatrolInspectionPlanSiteSon> queryWrapper1 = Wrappers.lambdaQuery();
        queryWrapper1.eq(PatrolInspectionPlanSiteSon::getPlanId, planSonId)
                .eq(PatrolInspectionPlanSiteSon::getSiteId, siteIdOne);
        List<PatrolInspectionPlanSiteSon> siteSonslist = patrolInspectionPlanSiteSonService.list(queryWrapper1);
        if (CollectionUtil.isNotEmpty(siteSonslist)) {
            siteList.get(0).setInspectionStatus(siteSonslist.get(0).getInspectionStatus());
        }
        List<PatrolInspectionContentVo> contentList = this.contentList(siteList.get(0).getId());
        siteList.get(0).setContentCount(contentList.size());
        siteList.get(0).setPlanSonId(planSonId);

        Map<String, Object> map = new HashMap<>();
        map.put("siteList", siteList);
        map.put("contentList", contentList);
        return map;
    }

    @Override
    @Transactional
    public void updatePlan(PlanRecordVo planRecordVo) {
        DateTimeFormatter df1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LambdaQueryWrapper<PatrolInspectionPlanSon> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PatrolInspectionPlanSon::getId, planRecordVo.getPlanSonId());
        List<PatrolInspectionPlanSon> planSonList = this.list(queryWrapper);
        if (planSonList.size() <= 0) {
            throw new BusinessException("计划不存在,可能已被删除,请联系管理人员");
        }
        /*
         * 巡检时间判断
         * */
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime inspectionDateTime = LocalDateTime.parse(planSonList.get(0).getInspectionDate() + " " + planSonList.get(0).getStartTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        LocalDateTime inspectionDateTime1 = LocalDateTime.parse(planSonList.get(0).getInspectionDate() + " " + planSonList.get(0).getEndTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        if (inspectionDateTime.isAfter(now)) {
            PatrolInspectionEvent patrolInspectionEvent = new PatrolInspectionEvent();
            patrolInspectionEvent.setEventName("早到");
            patrolInspectionEvent.setEventType(33);
            patrolInspectionEvent.setEventLevel(1);
            patrolInspectionEvent.setEventCategory(1);
            patrolInspectionEvent.setDeviceId(planRecordVo.getDeviceId());
            patrolInspectionEvent.setCreateBy(SecurityUtils.getUsername());
            patrolInspectionEvent.setCreateTime(LocalDateTime.now());
            patrolInspectionEventService.add(patrolInspectionEvent);
            throw new BusinessException("计划未开始，不可巡检");
        } else if (now.isAfter(inspectionDateTime1)) {
            PatrolInspectionEvent patrolInspectionEvent = new PatrolInspectionEvent();
            patrolInspectionEvent.setEventName("迟到");
            patrolInspectionEvent.setEventType(34);
            patrolInspectionEvent.setEventLevel(1);
            patrolInspectionEvent.setEventCategory(1);
            patrolInspectionEvent.setDeviceId(planRecordVo.getDeviceId());
            patrolInspectionEvent.setCreateBy(SecurityUtils.getUsername());
            patrolInspectionEvent.setCreateTime(LocalDateTime.now());
            patrolInspectionEventService.add(patrolInspectionEvent);
            throw new BusinessException("巡检时间已过，请联系管理员");
        }


        LambdaQueryWrapper<PatrolInspectionArea> queryWrapperOne = Wrappers.lambdaQuery();
        queryWrapperOne.select(PatrolInspectionArea::getId, PatrolInspectionArea::getAreaName)
                .eq(PatrolInspectionArea::getId, planSonList.get(0).getAreaId());
        List<PatrolInspectionArea> areaList = patrolInspectionAreaService.list(queryWrapperOne);
        if (areaList.isEmpty()) {
            throw new BusinessException("巡检区域不存在");
        }
        PatrolInspectionRecord patrolInspectionRecord = new PatrolInspectionRecord();
        patrolInspectionRecord.setSiteNubmber(planRecordVo.getSiteNubmber());
        patrolInspectionRecord.setSiteType(planRecordVo.getSiteType());
        patrolInspectionRecord.setAreaName(areaList.get(0).getAreaName());
        patrolInspectionRecord.setSiteName(planRecordVo.getSiteName());
        patrolInspectionRecord.setName(SecurityUtils.getLoginUser().getSysUser().getNickName());
        patrolInspectionRecord.setPhone(SecurityUtils.getLoginUser().getSysUser().getPhonenumber());
        patrolInspectionRecord.setPlanType(planSonList.get(0).getPlanType());
        patrolInspectionRecord.setSiteStatus(planRecordVo.getSiteStatus());
        patrolInspectionRecord.setLongitude(planRecordVo.getLongitude());
        patrolInspectionRecord.setLatitude(planRecordVo.getLatitude());

        String startDate = null;
        String endDate = null;
        if (planSonList.get(0).getPlanType() == 1) {
            startDate = df1.format(planSonList.get(0).getInspectionDate()) + "T" + planSonList.get(0).getStartTime();
            endDate = df1.format(planSonList.get(0).getInspectionDate()) + "T" + planSonList.get(0).getEndTime();
        } else {
            startDate = df1.format(planSonList.get(0).getStartDate()) + "T" + planSonList.get(0).getStartTime();
            endDate = df1.format(planSonList.get(0).getEndDate()) + "T" + planSonList.get(0).getEndTime();
        }
        patrolInspectionRecord.setStartDate(LocalDateTime.parse(startDate));
        patrolInspectionRecord.setEndDate(LocalDateTime.parse(endDate));
        patrolInspectionRecord.setCreateTime(LocalDateTime.now());
        patrolInspectionRecord.setPlanId(planSonList.get(0).getPlanId());
        patrolInspectionRecord.setPlanSonId(planRecordVo.getPlanSonId());
        patrolInspectionRecord.setSiteId(planRecordVo.getSiteId());
        patrolInspectionRecord.setTenantId(SecurityUtils.getTenantId());
        patrolInspectionRecord.setCompanyId(planRecordVo.getCompanyId());
        patrolInspectionRecord.setCreator(SecurityUtils.getUsername());
        patrolInspectionRecord.setRemarks(planRecordVo.getRemarks());
        patrolInspectionRecordMapper.insert(patrolInspectionRecord);
        Integer recordId = patrolInspectionRecord.getId();
        List<PatrolInspectionRecordPicture> recordPictureList = planRecordVo.getRecordPictureList();
        List<PatrolInspectionRecordOption> recordOptionList = planRecordVo.getRecordOptionList();
        for (int i = 0; i < recordPictureList.size(); i++) {
            recordPictureList.get(i).setRecordId(recordId);
            patrolInspectionRecordPictureService.save(recordPictureList.get(i));
        }

        if (recordOptionList != null && !recordOptionList.isEmpty()) {
            for (int i = 0; i < recordOptionList.size(); i++) {
                recordOptionList.get(i).setRecordId(recordId);
                patrolInspectionRecordOptionService.save(recordOptionList.get(i));
            }
        } else {
            throw new BusinessException("请选择或填写巡检内容!");
        }

        LambdaQueryWrapper<PatrolInspectionPlanSiteSon> queryWrapperTwo = Wrappers.lambdaQuery();
        queryWrapperTwo.eq(PatrolInspectionPlanSiteSon::getPlanId, planRecordVo.getPlanSonId())
                .eq(PatrolInspectionPlanSiteSon::getSiteId, planRecordVo.getSiteId());
        List<PatrolInspectionPlanSiteSon> planSiteSonList = patrolInspectionPlanSiteSonService.list(queryWrapperTwo);
        PatrolInspectionPlanSiteSon planSiteSon = new PatrolInspectionPlanSiteSon();
        planSiteSon.setId(planSiteSonList.get(0).getId());
        planSiteSon.setInspectionStatus(2);
        planSiteSon.setCreateTime(LocalDateTime.now());
        patrolInspectionPlanSiteSonService.updateById(planSiteSon);
        Integer patrolledCount = this.planSiteSonCount(planRecordVo.getPlanSonId(), 2);
        Integer undetectedCount = this.planSiteSonCount(planRecordVo.getPlanSonId(), 1);
        Integer siteCount = patrolledCount + undetectedCount;
        String rad = OnlineMethod.myPercent(patrolledCount, siteCount).replace("%", "");
        Double d = Double.parseDouble(rad); // 先转换成double类型
        Integer completion = d.intValue(); // 再转换成int类型（会损失精度）
        PatrolInspectionPlanSon planSon = new PatrolInspectionPlanSon();
        planSon.setId(planRecordVo.getPlanSonId());
        planSon.setCompletion(completion);
        this.updateById(planSon);
    }

    @Override
    public void addPatrolInspectionAbnormalData() {
        LOGGER.info("巡检漏检定时任务 begin");
        DateTimeFormatter df1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String datetime1 = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String datetime2 = LocalDateTime.now().minusHours(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        List<PatrolInspectionPlanSon> list = patrolInspectionPlanSonMapper.selectInspectionPlanSonData(datetime1, datetime2);
        if (list.size() <= 0) {
            LOGGER.info("当前时间向前一小时没有漏检记录");
        } else {
            List<Integer> sonIdlist = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                sonIdlist.add(list.get(i).getId());
            }
            LambdaQueryWrapper<PatrolInspectionPlanSiteSon> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.in(PatrolInspectionPlanSiteSon::getPlanId, sonIdlist)
                    .eq(PatrolInspectionPlanSiteSon::getInspectionStatus, 1);
            List<PatrolInspectionPlanSiteSon> siteSonList = patrolInspectionPlanSiteSonService.list(queryWrapper);
            if (CollectionUtils.isNotEmpty(siteSonList)) {
                List<Integer> siteIdList = new ArrayList<>();
                List<Integer> noSonIdList = new ArrayList<>();
                for (int i = 0; i < siteSonList.size(); i++) {
                    siteIdList.add(siteSonList.get(i).getSiteId());
                    noSonIdList.add(siteSonList.get(i).getPlanId());
                }
                // 获取巡检地点
                LOGGER.info("获取巡检地点");
                LambdaQueryWrapper<PatrolInspectionSite> queryWrapper1 = Wrappers.lambdaQuery();
                if (CollectionUtils.isNotEmpty(siteIdList)) {
                    queryWrapper1.in(PatrolInspectionSite::getId, siteIdList)
                            .eq(PatrolInspectionSite::getEnable, 1);
                }
                List<PatrolInspectionSite> siteList = patrolInspectionSiteService.list(queryWrapper1);
                if (siteList.size() <= 0) {
                    LOGGER.info("查询巡检地点记录为空");
                }
                // 获取巡检子计划
                LambdaQueryWrapper<PatrolInspectionPlanSon> planSonQueryWrapper = Wrappers.lambdaQuery();
                if (CollectionUtils.isNotEmpty(noSonIdList)) {
                    planSonQueryWrapper.in(PatrolInspectionPlanSon::getId, noSonIdList);
                }
                List<PatrolInspectionPlanSon> noPlanSonList = this.list(planSonQueryWrapper);
                List<Integer> planIdList = new ArrayList<>();
                List<Integer> personnelList = new ArrayList<>();
                List<Integer> areaList = new ArrayList<>();
                for (int i = 0; i < noPlanSonList.size(); i++) {
                    planIdList.add(noPlanSonList.get(i).getPlanId());
                    personnelList.add(noPlanSonList.get(i).getPersonnelId());
                    areaList.add(noPlanSonList.get(i).getAreaId());
                }

                // 获取巡检计划
                List<PatrolInspectionPlan> noPlanIdList = patrolInspectionPlanMapper.selectNoPlanList(planIdList);
                // 获取巡检人员
                LOGGER.info("获取巡检人员");
                LambdaQueryWrapper<PatrolInspectionPersonnel> personnelQueryWrapper = Wrappers.lambdaQuery();
                if (CollectionUtils.isNotEmpty(personnelList)) {
                    personnelQueryWrapper.in(PatrolInspectionPersonnel::getId, personnelList)
                            .eq(PatrolInspectionPersonnel::getEnable, 1);
                }
                List<PatrolInspectionPersonnel> inspectionPersonnelList = patrolInspectionPersonnelService.list(personnelQueryWrapper);
                if (inspectionPersonnelList.size() <= 0) {
                    LOGGER.info("巡检人员信息不存在");
                }
                // 获取巡检区域
                LambdaQueryWrapper<PatrolInspectionArea> queryWrapperOne = Wrappers.lambdaQuery();
                if (CollectionUtils.isNotEmpty(areaList)) {
                    queryWrapperOne.in(PatrolInspectionArea::getId, areaList)
                            .eq(PatrolInspectionArea::getEnable, 1);
                }
                List<PatrolInspectionArea> areaInfoList = patrolInspectionAreaService.list(queryWrapperOne);
                if (areaInfoList.size() <= 0) {
                    LOGGER.info("巡检区域不存在");
                }

                for (int i = 0; i < siteSonList.size(); i++) {
                    PatrolInspectionAbnormal patrolInspectionAbnormal = new PatrolInspectionAbnormal();
                    Integer siteId = siteSonList.get(i).getSiteId();
                    Integer planSonId = siteSonList.get(i).getPlanId();
                    for (int j = 0; j < siteList.size(); j++) {
                        if (siteId.equals(siteList.get(j).getId())) {
                            patrolInspectionAbnormal.setSiteNubmber(siteList.get(j).getSiteNubmber());
                            patrolInspectionAbnormal.setSiteType(siteList.get(j).getSiteType());
                            patrolInspectionAbnormal.setSiteName(siteList.get(j).getSiteName());
                            patrolInspectionAbnormal.setLongitude(siteList.get(j).getLongitude());
                            patrolInspectionAbnormal.setLatitude(siteList.get(j).getLatitude());
                            patrolInspectionAbnormal.setSiteId(siteList.get(j).getId());
                            patrolInspectionAbnormal.setCompanyId(siteList.get(j).getCompanyId());
                            break;
                        }
                    }

                    for (int j = 0; j < noPlanSonList.size(); j++) {
                        if (planSonId.equals(noPlanSonList.get(j).getId())) {
                            patrolInspectionAbnormal.setPlanId(noPlanSonList.get(j).getPlanId());
                            patrolInspectionAbnormal.setPlanSonId(noPlanSonList.get(j).getId());
                            patrolInspectionAbnormal.setTenantId(noPlanSonList.get(j).getTenantId());
                            patrolInspectionAbnormal.setPlanType(noPlanSonList.get(j).getPlanType());
                            Integer planType = noPlanSonList.get(j).getPlanType();
                            String startDate = null;
                            String endDate = null;
                            if (planType.equals(1)) {
                                startDate = df1.format(noPlanSonList.get(j).getInspectionDate()) + "T" + noPlanSonList.get(j).getStartTime();
                                endDate = df1.format(noPlanSonList.get(j).getInspectionDate()) + "T" + noPlanSonList.get(j).getEndTime();
                            } else {
                                startDate = df1.format(noPlanSonList.get(j).getStartDate()) + "T" + noPlanSonList.get(j).getStartTime();
                                endDate = df1.format(noPlanSonList.get(j).getEndDate()) + "T" + noPlanSonList.get(j).getEndTime();
                            }

                            patrolInspectionAbnormal.setStartDate(LocalDateTime.parse(startDate));
                            patrolInspectionAbnormal.setEndDate(LocalDateTime.parse(endDate));

                            Integer noAreaId = noPlanSonList.get(j).getAreaId();
                            for (int k = 0; k < areaInfoList.size(); k++) {
                                if (noAreaId.equals(areaInfoList.get(k).getId())) {
                                    patrolInspectionAbnormal.setAreaName(areaInfoList.get(k).getAreaName());
                                    break;
                                }
                            }
                            Integer noPersonnelId = noPlanSonList.get(j).getPersonnelId();
                            for (int k = 0; k < inspectionPersonnelList.size(); k++) {
                                if (noPersonnelId.equals(inspectionPersonnelList.get(k).getId())) {
                                    patrolInspectionAbnormal.setName(inspectionPersonnelList.get(k).getFullName());
                                    patrolInspectionAbnormal.setPhone(inspectionPersonnelList.get(k).getPhoneNumber());
                                    break;
                                }
                            }
                            Integer planId = noPlanSonList.get(j).getPlanId();
                            for (int k = 0; k < noPlanIdList.size(); k++) {
                                if (planId.equals(noPlanIdList.get(k).getId())) {
                                    patrolInspectionAbnormal.setRouteId(noPlanIdList.get(k).getRouteId());
                                    break;
                                }
                            }

                            break;
                        }
                    }
                    patrolInspectionAbnormal.setAbnormalStatus(1);
                    patrolInspectionAbnormal.setCreateTime(LocalDateTime.now());
                    patrolInspectionAbnormal.setCreator("YT_admin");


                    patrolInspectionAbnormalService.save(patrolInspectionAbnormal);

                }

            }
        }

        LOGGER.info("巡检漏检定时任务 end");
    }


    @Override
    public List<PatrolInspectionSiteVo> planSonSiteDetails(Integer planSonId) {
        LambdaQueryWrapper<PatrolInspectionPlanSiteSon> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PatrolInspectionPlanSiteSon::getPlanId, planSonId);
        List<PatrolInspectionPlanSiteSon> planSiteSonList = patrolInspectionPlanSiteSonService.list(queryWrapper);
        if (planSiteSonList.size() <= 0) {
            throw new BusinessException("子计划不存在地点,请联系管理人员");
        }

        List<Integer> siteIdList = new ArrayList<>();
        for (int k = 0; k < planSiteSonList.size(); k++) {
            siteIdList.add(planSiteSonList.get(k).getSiteId());
        }
        LambdaQueryWrapper<PatrolInspectionSite> queryWrapperTwo = Wrappers.lambdaQuery();
        queryWrapperTwo.in(PatrolInspectionSite::getId, siteIdList)
                .eq(PatrolInspectionSite::getEnable, 1);
        List<PatrolInspectionSite> siteList = patrolInspectionSiteService.list(queryWrapperTwo);
        List<PatrolInspectionSiteVo> list = new ArrayList<>();
        for (int i = 0; i < siteList.size(); i++) {
            PatrolInspectionSiteVo SiteVo = new PatrolInspectionSiteVo();
            SiteVo.setId(siteList.get(i).getId());
            SiteVo.setSiteName(siteList.get(i).getSiteName());
            SiteVo.setSiteNubmber(siteList.get(i).getSiteNubmber());
            SiteVo.setSiteDescribe(siteList.get(i).getSiteDescribe());
            SiteVo.setSiteType(siteList.get(i).getSiteType());
            SiteVo.setCollector(siteList.get(i).getCollector());
            SiteVo.setCollectTime(siteList.get(i).getCollectTime());
            SiteVo.setLongitude(siteList.get(i).getLongitude());
            SiteVo.setLatitude(siteList.get(i).getLatitude());
            SiteVo.setAreaId(siteList.get(i).getAreaId());
            SiteVo.setPictureUrl(siteList.get(i).getPictureUrl());
            SiteVo.setCreateTime(siteList.get(i).getCreateTime());
            SiteVo.setCreator(siteList.get(i).getCreator());
            SiteVo.setEnable(siteList.get(i).getEnable());
            SiteVo.setDistanceRange(siteList.get(i).getDistanceRange());
            SiteVo.setTenantId(siteList.get(i).getTenantId());
            SiteVo.setCompanyId(siteList.get(i).getCompanyId());
            for (int j = 0; j < planSiteSonList.size(); j++) {
                if (siteList.get(i).getId().equals(planSiteSonList.get(j).getSiteId())) {
                    SiteVo.setInspectionTime(planSiteSonList.get(j).getCreateTime());
                    SiteVo.setInspectionStatus(planSiteSonList.get(j).getInspectionStatus());
                }
            }
            list.add(SiteVo);
        }
        return list;
    }


    @Override
    public void addSite(PatrolInspectionSite patrolInspectionSite) {
        LambdaQueryWrapper<PatrolInspectionSite> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PatrolInspectionSite::getSiteNubmber, patrolInspectionSite.getSiteNubmber())
                .eq(PatrolInspectionSite::getEnable, 1);
        int list = patrolInspectionSiteService.count(queryWrapper);
        int siteCount = patrolInspectionSiteStoreService.siteStoreCount(patrolInspectionSite.getSiteNubmber());
        if (list > 0) {
            throw new BusinessException("不可重复采集地点");
        }
        if (siteCount <= 0) {
            throw new BusinessException("采集点位不合法，请采集合法点位");
        }
        patrolInspectionSite.setCreator(SecurityUtils.getUsername());
        patrolInspectionSite.setCollector(SecurityUtils.getLoginUser().getSysUser().getNickName());
        patrolInspectionSite.setCreateTime(LocalDateTime.now());
        patrolInspectionSite.setTenantId(SecurityUtils.getTenantId());
        patrolInspectionSite.setEnable(1);
        patrolInspectionSiteService.save(patrolInspectionSite);
    }

    @Override
    public List<PatrolInspectionSite> siteList(String startTime, String endTime) {
        LambdaQueryWrapper<PatrolInspectionSite> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PatrolInspectionSite::getTenantId, SecurityUtils.getTenantId());
        queryWrapper.eq(PatrolInspectionSite::getCollector, SecurityUtils.getLoginUser().getSysUser().getNickName());
        queryWrapper.eq(PatrolInspectionSite::getEnable, 1);
        if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)) {
            queryWrapper.between(PatrolInspectionSite::getCollectTime, startTime, endTime);
        }
        List<PatrolInspectionSite> list = patrolInspectionSiteService.list(queryWrapper);
        return list;
    }

    @Override
    public List<PatrolInspectionRecordVo> recordList(String currentDate, String sort, Integer id, Integer planSonId, Integer siteId) {
        List<Integer> personnelIdList = this.getpersonId();
        LambdaQueryWrapper<PatrolInspectionPlanSchedule> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(PatrolInspectionPlanSchedule::getPlanId)
                .in(PatrolInspectionPlanSchedule::getPersonnelId, personnelIdList)
                .groupBy(PatrolInspectionPlanSchedule::getPlanId);
        List<PatrolInspectionPlanSchedule> planScheduleList = patrolInspectionPlanScheduleService.list(queryWrapper);
        List<PatrolInspectionRecordVo> list = new ArrayList<>();
        if (planScheduleList.size() > 0) {
            List<Integer> planIdList = new ArrayList<>();
            for (int i = 0; i < planScheduleList.size(); i++) {
                planIdList.add(planScheduleList.get(i).getPlanId());
            }
            LambdaQueryWrapper<PatrolInspectionRecord> queryWrapperOne = Wrappers.lambdaQuery();
            queryWrapperOne.in(PatrolInspectionRecord::getPlanId, planIdList)
                    .eq(id != null && id != 0, PatrolInspectionRecord::getId, id)
                    .eq(planSonId != null && planSonId != 0, PatrolInspectionRecord::getPlanSonId, planSonId)
                    .eq(siteId != null && siteId != 0, PatrolInspectionRecord::getSiteId, siteId)
                    .between(StringUtils.isNotBlank(currentDate), PatrolInspectionRecord::getCreateTime, currentDate + " 00:00:00", currentDate + " 23:59:59");
            if (sort.equals("ASC")) {
                queryWrapperOne.orderByAsc(PatrolInspectionRecord::getId);
            } else {
                queryWrapperOne.orderByDesc(PatrolInspectionRecord::getId);
            }
            List<PatrolInspectionRecord> recordList = patrolInspectionRecordMapper.selectList(queryWrapperOne);
            if (recordList.size() > 0) {
                List<Integer> siteIdList = new ArrayList<>();
                List<Integer> recordIdList = new ArrayList<>();
                for (int i = 0; i < recordList.size(); i++) {
                    siteIdList.add(recordList.get(i).getSiteId());
                    recordIdList.add(recordList.get(i).getId());
                }
                List<PatrolInspectionSite> list1 = new ArrayList<>();
                LambdaQueryWrapper<PatrolInspectionSite> queryWrapper1 = Wrappers.lambdaQuery();
                queryWrapper1.select(PatrolInspectionSite::getId, PatrolInspectionSite::getPictureUrl)
                        .eq(PatrolInspectionSite::getId, recordList.get(0).getSiteId());
                list1 = patrolInspectionSiteService.list(queryWrapper1);

                LambdaQueryWrapper<PatrolInspectionRecordPicture> queryOne = Wrappers.lambdaQuery();
                queryOne.in(PatrolInspectionRecordPicture::getRecordId, recordIdList);
                List<PatrolInspectionRecordPicture> recordPictureList = patrolInspectionRecordPictureService.list(queryOne);

                LambdaQueryWrapper<PatrolInspectionSiteContent> queryWrapperTwo = Wrappers.lambdaQuery();
                queryWrapperTwo.in(PatrolInspectionSiteContent::getSiteId, siteIdList);
                List<PatrolInspectionSiteContent> siteContentLsit = patrolInspectionSiteContentService.list(queryWrapperTwo);

                Map<Integer, List<PatrolInspectionSiteContent>> grouypByAreaId = siteContentLsit.stream().collect(
                        Collectors.groupingBy(o -> o.getSiteId())
                );
                List<DataCountVo> dataCountVoList = new ArrayList<>();
                grouypByAreaId.forEach((k, v) -> {
                    DataCountVo dataCountVo = new DataCountVo();
                    dataCountVo.setListCount(v.size());
                    dataCountVo.setId(k);
                    dataCountVoList.add(dataCountVo);
                });
                for (int i = 0; i < recordList.size(); i++) {
                    PatrolInspectionRecordVo record = new PatrolInspectionRecordVo();
                    for (int j = 0; j < list1.size(); j++) {
                        if (recordList.get(i).getSiteId() == list1.get(j).getId()) {
                            record.setPictureUrl(list1.get(j).getPictureUrl());
                        }
                    }
                    record.setId(recordList.get(i).getId());
                    record.setSiteNubmber(recordList.get(i).getSiteNubmber());
                    record.setSiteType(recordList.get(i).getSiteType());
                    record.setAreaName(recordList.get(i).getAreaName());
                    record.setSiteName(recordList.get(i).getSiteName());
                    record.setName(recordList.get(i).getName());
                    record.setPhone(recordList.get(i).getPhone());
                    record.setPlanType(recordList.get(i).getPlanType());
                    record.setSiteStatus(recordList.get(i).getSiteStatus());
                    record.setLongitude(recordList.get(i).getLongitude());
                    record.setLatitude(recordList.get(i).getLatitude());
                    record.setStartDate(recordList.get(i).getStartDate());
                    record.setEndDate(recordList.get(i).getEndDate());
                    record.setCreateTime(recordList.get(i).getCreateTime());
                    record.setPlanId(recordList.get(i).getPlanId());
                    record.setPlanSonId(recordList.get(i).getPlanSonId());
                    record.setSiteId(recordList.get(i).getSiteId());
                    record.setTenantId(recordList.get(i).getTenantId());
                    record.setCompanyId(recordList.get(i).getCompanyId());
                    record.setCreator(recordList.get(i).getCreator());
                    record.setRemarks(recordList.get(i).getRemarks());
                    record.setContentCount(0);
                    List<PatrolInspectionRecordPicture> PatrolInspectionRecordPictureList = new ArrayList<>();
                    for (int j = 0; j < recordPictureList.size(); j++) {
                        PatrolInspectionRecordPicture patrolInspectionRecordPicture = new PatrolInspectionRecordPicture();
                        int id1 = recordList.get(i).getId();
                        int recordId1 = recordPictureList.get(j).getRecordId();
                        if (id1 == recordId1) {
                            patrolInspectionRecordPicture.setRecordId(recordPictureList.get(j).getRecordId());
                            patrolInspectionRecordPicture.setPictureUrl(recordPictureList.get(j).getPictureUrl());
                            patrolInspectionRecordPicture.setId(recordPictureList.get(j).getId());
                        }
                        PatrolInspectionRecordPictureList.add(patrolInspectionRecordPicture);
                    }
                    record.setRecordPictureList(PatrolInspectionRecordPictureList);
                    for (int j = 0; j < dataCountVoList.size(); j++) {
                        int siteId1 = recordList.get(i).getSiteId();
                        int siteId2 = dataCountVoList.get(j).getId();
                        if (siteId1 == siteId2) {
                            record.setContentCount(dataCountVoList.get(j).getListCount());
                        }
                    }
                    list.add(record);
                }
            }
        }
        return list;
    }


    @Override
    public Map<String, Object> recordOption(Integer recordId, Integer siteId) {
        LambdaQueryWrapper<PatrolInspectionSiteContent> queryWrapperOne = Wrappers.lambdaQuery();
        queryWrapperOne.eq(PatrolInspectionSiteContent::getSiteId, siteId);
        List<PatrolInspectionSiteContent> siteContentList = patrolInspectionSiteContentService.list(queryWrapperOne);
        List<PatrolInspectionContentVo> list = new ArrayList<>();
        List<Integer> contentOptionIdList = new ArrayList<>();
        if (siteContentList.size() > 0) {
            List<Integer> contentIdList = new ArrayList<>();
            for (int i = 0; i < siteContentList.size(); i++) {
                contentIdList.add(siteContentList.get(i).getContentId());
            }
            LambdaQueryWrapper<PatrolInspectionContent> queryWrapperTwo = Wrappers.lambdaQuery();
            queryWrapperTwo.in(PatrolInspectionContent::getId, contentIdList)
                    .eq(PatrolInspectionContent::getEnable, 1);
            List<PatrolInspectionContent> contentList = patrolInspectionContentService.list(queryWrapperTwo);
            if (contentList.size() > 0) {
                LambdaQueryWrapper<PatrolInspectionContentOption> queryWrapperThree = Wrappers.lambdaQuery();
                queryWrapperThree.in(PatrolInspectionContentOption::getContentId, contentIdList)
                        .eq(PatrolInspectionContentOption::getEnable, 1);
                List<PatrolInspectionContentOption> contentOptionList = patrolInspectionContentOptionService.list(queryWrapperThree);

                LambdaQueryWrapper<PatrolInspectionRecordOption> queryWrapper = Wrappers.lambdaQuery();
                queryWrapper.eq(PatrolInspectionRecordOption::getRecordId, recordId);
                List<PatrolInspectionRecordOption> recordOptionList = patrolInspectionRecordOptionService.list(queryWrapper);
                for (int i = 0; i < recordOptionList.size(); i++) {
                    if (recordOptionList.get(i).getContentOptionId() != null) {
                        contentOptionIdList.add(recordOptionList.get(i).getContentOptionId());
                    }
                }

                List<PatrolInspectionContentOption> contentOptionVoList = new ArrayList<>();
                for (int i = 0; i < contentOptionList.size(); i++) {
                    contentOptionList.get(i).setId(contentOptionList.get(i).getId());
                    contentOptionList.get(i).setContentId(contentOptionList.get(i).getContentId());
                    contentOptionList.get(i).setCreateTime(contentOptionList.get(i).getCreateTime());
                    contentOptionList.get(i).setCreator(contentOptionList.get(i).getCreator());
                    contentOptionList.get(i).setEnable(contentOptionList.get(i).getEnable());
                    contentOptionList.get(i).setSelectStatus(false);
                    for (int j = 0; j < recordOptionList.size(); j++) {
                        if (recordOptionList.get(j).getContentOptionId() != null) {
                            int contentOptionId1 = contentOptionList.get(i).getId();
                            int contentOptionId2 = recordOptionList.get(j).getContentOptionId();
                            if (contentOptionId1 == contentOptionId2) {
                                contentOptionList.get(i).setSelectStatus(true);
                                contentOptionList.get(i).setRemarks(recordOptionList.get(j).getRemarks());
                            }
                        }
                        int contentId1 = contentOptionList.get(i).getContentId();
                        int contentId2 = recordOptionList.get(j).getContentId();
                        if (contentId1 == contentId2) {
                            contentOptionList.get(i).setContent(recordOptionList.get(j).getContent());
                        }
                    }
                    contentOptionVoList.add(contentOptionList.get(i));
                }
                for (int i = 0; i < contentList.size(); i++) {
                    List<PatrolInspectionContentOption> contentOptionVoListOne = new ArrayList<>();
                    PatrolInspectionContentVo contentVo = new PatrolInspectionContentVo();
                    contentVo.setXh(i + 1);
                    contentVo.setId(contentList.get(i).getId());
                    contentVo.setContentTitle(contentList.get(i).getContentTitle());
                    contentVo.setContentDescribe(contentList.get(i).getContentDescribe());
                    contentVo.setSubmissionMethod(contentList.get(i).getSubmissionMethod());
                    contentVo.setTenantId(contentList.get(i).getTenantId());
                    contentVo.setCreator(contentList.get(i).getCreator());
                    contentVo.setCreateTime(contentList.get(i).getCreateTime());
                    contentVo.setEnable(contentList.get(i).getEnable());
                    contentVo.setCompanyId(contentList.get(i).getCompanyId());
                    contentVo.setContentOptionValue(contentOptionIdList);

                    if (contentList.get(i).getSubmissionMethod() == 3) {
                        for (int j = 0; j < recordOptionList.size(); j++) {
                            if (recordOptionList.get(j).getContentOptionId() == null || recordOptionList.get(j).getContentOptionId() == 0) {
                                int id11 = contentList.get(i).getId();
                                int id22 = recordOptionList.get(j).getContentId();
                                if (id11 == id22) {
                                    if (recordOptionList.get(j).getRemarks() != null && !"".equals(recordOptionList.get(j).getRemarks())) {
                                        contentVo.setRemarks(recordOptionList.get(j).getRemarks());
                                    }
                                    contentVo.setContent(recordOptionList.get(j).getContent());
//                                    PatrolInspectionContentOption contentOptionVo = new PatrolInspectionContentOption();
//                                    contentOptionVo.setContentId(recordOptionList.get(j).getContentId());
//                                    contentOptionVo.setContent(recordOptionList.get(j).getContent());
//                                    contentOptionVo.setRemarks(recordOptionList.get(j).getRemarks());
//                                    contentOptionVoListOne.add(contentOptionVo);
//                                    contentVo.setContentOptionList(contentOptionVoListOne);
                                }
                            }
                        }
                    } else {
                        for (int j = 0; j < contentOptionVoList.size(); j++) {
                            int id1 = contentList.get(i).getId();
                            int id2 = contentOptionVoList.get(j).getContentId();
                            if (id1 == id2) {
                                if (contentOptionVoList.get(j).getRemarks() != null && !"".equals(contentOptionVoList.get(j).getRemarks())) {
                                    contentVo.setRemarks(contentOptionVoList.get(j).getRemarks());
                                }
                                contentOptionVoListOne.add(contentOptionVoList.get(j));
                                contentVo.setContentOptionList(contentOptionVoListOne);
                            }
                        }
                    }
                    list.add(contentVo);
                }
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("contentList", list);
        return map;
    }


    @Override
    public List<Integer> getpersonId() {
        LambdaQueryWrapper<PatrolInspectionPersonnel> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PatrolInspectionPersonnel::getEnable, 1)
                .eq(PatrolInspectionPersonnel::getTenantId, SecurityUtils.getTenantId())
                .eq(PatrolInspectionPersonnel::getUserId, SecurityUtils.getUserId());
        List<PatrolInspectionPersonnel> personnelList = patrolInspectionPersonnelService.list(queryWrapper);
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < personnelList.size(); i++) {
            list.add(personnelList.get(i).getId());
        }
        return list;
    }


    @Override
    public Integer planSiteSonCount(Integer planSonId, Integer inspectionStatus) {
        LambdaQueryWrapper<PatrolInspectionPlanSiteSon> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PatrolInspectionPlanSiteSon::getPlanId, planSonId)
                .eq(PatrolInspectionPlanSiteSon::getInspectionStatus, inspectionStatus);
        Integer planSiteSonCount = patrolInspectionPlanSiteSonService.count(queryWrapper);
        return planSiteSonCount;
    }


    @Override
    public List<PatrolInspectionContentVo> contentList(Integer siteId) {
        LambdaQueryWrapper<PatrolInspectionSiteContent> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(PatrolInspectionSiteContent::getContentId)
                .eq(PatrolInspectionSiteContent::getSiteId, siteId);
        List<PatrolInspectionSiteContent> contentId = patrolInspectionSiteContentService.list(queryWrapper);
        if (contentId.size() <= 0) {
            throw new BusinessException("地点尚未分配内容,请联系管理人员");
        }
        List<Integer> contentIdList = new ArrayList<>();
        for (int i = 0; i < contentId.size(); i++) {
            contentIdList.add(contentId.get(i).getContentId());
        }


        LambdaQueryWrapper<PatrolInspectionContent> queryWrapperOne = Wrappers.lambdaQuery();
        queryWrapperOne.eq(PatrolInspectionContent::getEnable, 1)
                .in(PatrolInspectionContent::getId, contentIdList);
        queryWrapperOne.eq(PatrolInspectionContent::getTenantId, SecurityUtils.getTenantId());
        List<PatrolInspectionContent> patrolInspectionContentList = patrolInspectionContentService.list(queryWrapperOne);
        List<PatrolInspectionContentVo> patrolInspectionContentVoList = new ArrayList<>();
        if (patrolInspectionContentList.size() > 0) {
            List<Integer> idList = new ArrayList<>();
            for (int i = 0; i < patrolInspectionContentList.size(); i++) {
                idList.add(patrolInspectionContentList.get(i).getId());
            }
            LambdaQueryWrapper<PatrolInspectionContentOption> queryWrapperTwo = Wrappers.lambdaQuery();
            queryWrapperTwo.eq(PatrolInspectionContentOption::getEnable, 1)
                    .in(PatrolInspectionContentOption::getContentId, idList);
            List<PatrolInspectionContentOption> contentOptionList = patrolInspectionContentOptionService.list(queryWrapperTwo);
            for (int i = 0; i < patrolInspectionContentList.size(); i++) {
                PatrolInspectionContentVo patrolInspectionContentVo = new PatrolInspectionContentVo();
                patrolInspectionContentVo.setXh(i + 1);
                patrolInspectionContentVo.setId(patrolInspectionContentList.get(i).getId());
                patrolInspectionContentVo.setContentTitle(patrolInspectionContentList.get(i).getContentTitle());
                patrolInspectionContentVo.setContentDescribe(patrolInspectionContentList.get(i).getContentDescribe());
                patrolInspectionContentVo.setSubmissionMethod(patrolInspectionContentList.get(i).getSubmissionMethod());
                patrolInspectionContentVo.setTenantId(patrolInspectionContentList.get(i).getTenantId());
                patrolInspectionContentVo.setCompanyId(patrolInspectionContentList.get(i).getCompanyId());
                patrolInspectionContentVo.setCreator(patrolInspectionContentList.get(i).getCreator());
                patrolInspectionContentVo.setCreateTime(patrolInspectionContentList.get(i).getCreateTime());
                patrolInspectionContentVo.setEnable(patrolInspectionContentList.get(i).getEnable());
                List<PatrolInspectionContentOption> contentOptionListOne = new ArrayList<>();
                for (int j = 0; j < contentOptionList.size(); j++) {
                    if (patrolInspectionContentList.get(i).getId() == contentOptionList.get(j).getContentId()) {
                        contentOptionListOne.add(contentOptionList.get(j));
                    }
                }
                patrolInspectionContentVo.setContentOptionList(contentOptionListOne);
                patrolInspectionContentVoList.add(patrolInspectionContentVo);
            }

        }
        return patrolInspectionContentVoList;
    }


    @Override
    public List<PatrolInspectionPlanSonVo> patrolInspectionPlanSon(List<Integer> planIdList, String currentDate, String sort) {
//        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter df1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        LambdaQueryWrapper<PatrolInspectionPlanSon> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PatrolInspectionPlanSon::getTenantId, SecurityUtils.getTenantId());
        if (planIdList != null && planIdList.size() > 0) {
            queryWrapper.in(PatrolInspectionPlanSon::getPlanId, planIdList);
        }
        queryWrapper.and((wrapper) -> {
            wrapper.eq(PatrolInspectionPlanSon::getInspectionDate, currentDate)
                    .or()
                    .le(PatrolInspectionPlanSon::getStartDate, currentDate)
                    .ge(PatrolInspectionPlanSon::getEndDate, currentDate);
        });
        if (sort.equals("ASC")) {
            queryWrapper.orderByAsc(PatrolInspectionPlanSon::getId);
        } else {
            queryWrapper.orderByDesc(PatrolInspectionPlanSon::getId);
        }
        List<PatrolInspectionPlanSon> planSonList = this.list(queryWrapper);
        List<PatrolInspectionPlanSonVo> list = new ArrayList<>();
        if (planSonList.size() > 0) {
            List<Integer> planSonidList = new ArrayList<>();
            for (int i = 0; i < planSonList.size(); i++) {
                planSonidList.add(planSonList.get(i).getId());
            }
            // 巡检计划名称查询
            LambdaQueryWrapper<PatrolInspectionPlan> queryWrapperOne = Wrappers.lambdaQuery();
//            queryWrapperOne.eq(PatrolInspectionPlan::getTenantId, SecurityUtils.getTenantId());
//            queryWrapperOne.eq(PatrolInspectionPlan::getEnable, 1);
            queryWrapperOne.select(PatrolInspectionPlan::getId, PatrolInspectionPlan::getPlanName);
            if (planIdList != null && planIdList.size() > 0) {
                queryWrapperOne.in(PatrolInspectionPlan::getId, planIdList);
            }

            List<PatrolInspectionPlan> planList = patrolInspectionPlanMapper.selectList(queryWrapperOne);
            List<DataCountVo> patrolledSiteCountList = this.planSiteSonCount(planSonidList, 2);// 已巡检地点数量
            List<DataCountVo> undetectedSiteCountList = this.planSiteSonCount(planSonidList, 1);// 漏检地点数量
            for (int i = 0; i < planSonList.size(); i++) {
                PatrolInspectionPlanSonVo patrolInspectionPlanSonVo = new PatrolInspectionPlanSonVo();
                patrolInspectionPlanSonVo.setId(planSonList.get(i).getId());
                patrolInspectionPlanSonVo.setStartTime(planSonList.get(i).getStartTime());
                patrolInspectionPlanSonVo.setEndTime(planSonList.get(i).getEndTime());
                patrolInspectionPlanSonVo.setCompletion(planSonList.get(i).getCompletion());
                patrolInspectionPlanSonVo.setPlanType(planSonList.get(i).getPlanType());
                patrolInspectionPlanSonVo.setPlanCycle(planSonList.get(i).getPlanCycle());
                patrolInspectionPlanSonVo.setStartDate(planSonList.get(i).getStartDate());
                patrolInspectionPlanSonVo.setEndDate(planSonList.get(i).getEndDate());
                patrolInspectionPlanSonVo.setInspectionDate(planSonList.get(i).getInspectionDate());
                patrolInspectionPlanSonVo.setPatrolledSiteCount(0);
                patrolInspectionPlanSonVo.setUndetectedSiteCount(0);
                // 计划名称
                for (int j = 0; j < planList.size(); j++) {
                    if (planSonList.get(i).getPlanId().equals(planList.get(j).getId())) {
                        patrolInspectionPlanSonVo.setPlanId(planList.get(j).getId());
                        patrolInspectionPlanSonVo.setPlanName(planList.get(j).getPlanName());
                    }
                }
                Integer planStatus = 1;
                Date currentTime = new Date();
                Date endTime = null;
                if (planSonList.get(i).getPlanType() == 1) {
                    // 获取结束日期时间
                    LocalDate inspectionDate = planSonList.get(i).getInspectionDate();
                    String inspectionDateOne = df1.format(inspectionDate);
                    String inspectionDateTwo = inspectionDateOne + " " + planSonList.get(i).getEndTime();
                    try {
                        endTime = formatter.parse(inspectionDateTwo);
                    } catch (Exception e) {
                    }

                } else {
                    // 获取结束日期时间
                    LocalDate endDate = planSonList.get(i).getEndDate();
                    String endDateOne = df1.format(endDate) + " 23:59:59";
                    try {
                        endTime = formatter.parse(endDateOne);
                    } catch (Exception e) {
                    }
                    // 计划类型描述
                    String planCycleName = null;
                    if (planSonList.get(i).getPlanCycle() == 1) {
                        planCycleName = "（日）";
                    } else if (planSonList.get(i).getPlanCycle() == 2) {
                        planCycleName = "（周）";
                    } else if (planSonList.get(i).getPlanCycle() == 3) {
                        planCycleName = "（月）";
                    } else if (planSonList.get(i).getPlanCycle() == 4) {
                        planCycleName = "（年）";
                    }
                    patrolInspectionPlanSonVo.setPlanTypeDescribe("按次计划" + planCycleName);
                }
                // 判断计划状态
                int status = endTime.compareTo(currentTime);
                if (status == -1) {
                    if (planSonList.get(i).getCompletion() == 100) {
                        planStatus = 2;
                    } else {
                        planStatus = 3;
                    }
                }
                patrolInspectionPlanSonVo.setPlanStatus(planStatus);
                for (int j = 0; j < patrolledSiteCountList.size(); j++) {
                    if (planSonList.get(i).getId() == patrolledSiteCountList.get(j).getId()) {
                        patrolInspectionPlanSonVo.setPatrolledSiteCount(patrolledSiteCountList.get(j).getListCount());
                    }
                }
                for (int j = 0; j < undetectedSiteCountList.size(); j++) {
                    int id = planSonList.get(i).getId();
                    int id1 = undetectedSiteCountList.get(j).getId();
                    if (id == id1) {
                        patrolInspectionPlanSonVo.setUndetectedSiteCount(undetectedSiteCountList.get(j).getListCount());
                    }
                }
                list.add(patrolInspectionPlanSonVo);
            }
        }
        return list;
    }

    @Override
    public List<DataCountVo> planSiteSonCount(List<Integer> planSonidList, Integer inspectionStatus) {
        LambdaQueryWrapper<PatrolInspectionPlanSiteSon> queryWrapper = Wrappers.lambdaQuery();
        if (planSonidList != null && planSonidList.size() > 0) {
            queryWrapper.in(PatrolInspectionPlanSiteSon::getPlanId, planSonidList);
        }
        if (inspectionStatus != null) {
            queryWrapper.eq(PatrolInspectionPlanSiteSon::getInspectionStatus, inspectionStatus);
        }
        List<PatrolInspectionPlanSiteSon> planSiteSonList = patrolInspectionPlanSiteSonService.list(queryWrapper);

        Map<Integer, List<PatrolInspectionPlanSiteSon>> grouypByAreaPlanId = planSiteSonList.stream().collect(
                Collectors.groupingBy(o -> o.getPlanId())
        );
        List<DataCountVo> dataCountVoList = new ArrayList<>();
        grouypByAreaPlanId.forEach((k, v) -> {
            DataCountVo dataCountVo = new DataCountVo();
            dataCountVo.setListCount(v.size());
            dataCountVo.setId(k);
            dataCountVoList.add(dataCountVo);
        });
        return dataCountVoList;
    }

    @Override
    public Integer planSonCount(List<Integer> planIdList, Integer completion, String currentDate) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LambdaQueryWrapper<PatrolInspectionPlanSon> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PatrolInspectionPlanSon::getTenantId, SecurityUtils.getTenantId());
        if (planIdList != null && planIdList.size() > 0) {
            queryWrapper.in(PatrolInspectionPlanSon::getPlanId, planIdList);
        }
        if (completion != null) {
            queryWrapper.eq(PatrolInspectionPlanSon::getCompletion, completion);
        }
        LocalDate currentDate1 = LocalDate.now();
        if (currentDate != null && !"".equals(currentDate)) {
            currentDate1 = LocalDate.parse(currentDate);
        }
        LocalDate finalCurrentDate = currentDate1;
        queryWrapper.and((wrapper) -> {
            wrapper.eq(PatrolInspectionPlanSon::getInspectionDate, finalCurrentDate)
                    .or()
                    .le(PatrolInspectionPlanSon::getStartDate, finalCurrentDate)
                    .ge(PatrolInspectionPlanSon::getEndDate, finalCurrentDate);
        });

        Integer planSonCount = this.count(queryWrapper);
        return planSonCount;
    }

    @Override
    public List<Integer> planSonIdList(List<Integer> planSonSiteIdList, String startTime, String endTime) {
        LambdaQueryWrapper<PatrolInspectionPlanSon> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(PatrolInspectionPlanSon::getId, planSonSiteIdList)
                .between(PatrolInspectionPlanSon::getInspectionDate, startTime, endTime);
        List<PatrolInspectionPlanSon> list = this.list(queryWrapper);
        List<Integer> planSonIdList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            planSonIdList.add(list.get(i).getId());
        }
        return planSonIdList;
    }
}