package com.usky.fire.service.impl;

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.bean.CommonPage;
import com.usky.common.core.exception.BusinessException;
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.service.*;
import com.usky.fire.service.util.OnlineMethod;
import com.usky.fire.service.vo.*;
import com.usky.system.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-07-18
 */
@Service
public class PatrolInspectionPlanServiceImpl extends AbstractCrudService<PatrolInspectionPlanMapper, PatrolInspectionPlan> implements PatrolInspectionPlanService {

    @Autowired
    private PatrolInspectionAreaService patrolInspectionAreaService;

    @Autowired
    private PatrolInspectionPlanSiteService planSiteService;

    @Autowired
    private PatrolInspectionPlanSonService patrolInspectionPlanSonService;

    @Autowired
    private PatrolInspectionPlanScheduleService planScheduleService;

    @Autowired
    private PatrolInspectionPlanSiteSonService planSiteSonService;

    @Autowired
    private PatrolInspectionSpecialRestDayService specialRestDayService;

    @Autowired
    private PatrolInspectionSiteService patrolInspectionSiteService;

    @Autowired
    private PatrolInspectionRecordService patrolInspectionRecordService;

    @Autowired
    private PatrolInspectionPersonnelService patrolInspectionPersonnelService;

    @Override
    public List<PatrolInspectionAreaVo> planLeftList() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String userType = null;
        if (loginUser != null && !"".equals(loginUser)) {
            userType = loginUser.getUserType();
        }
        LambdaQueryWrapper<PatrolInspectionArea> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PatrolInspectionArea::getEnable, 1)
                .eq(PatrolInspectionArea::getTenantId, SecurityUtils.getTenantId());
        if ("00".equals(userType)) {
            queryWrapper.eq(PatrolInspectionArea::getCreator, SecurityUtils.getUsername());
        }
        List<PatrolInspectionArea> patrolInspectionAreaList = patrolInspectionAreaService.list(queryWrapper);
        LambdaQueryWrapper<PatrolInspectionPlan> queryWrapperOne = Wrappers.lambdaQuery();
        queryWrapperOne.eq(PatrolInspectionPlan::getEnable, 1)
                .eq(PatrolInspectionPlan::getTenantId, SecurityUtils.getTenantId());
        if ("00".equals(userType)) {
            queryWrapperOne.eq(PatrolInspectionPlan::getCreator, SecurityUtils.getUsername());
        }
        List<PatrolInspectionPlan> list = this.list(queryWrapperOne);
        Map<Integer, List<PatrolInspectionPlan>> grouypByAreaId = list.stream().collect(
                Collectors.groupingBy(o -> o.getAreaId())
        );
        List<DataCountVo> dataCountVoList = new ArrayList<>();
        grouypByAreaId.forEach((k, v) -> {
            DataCountVo dataCountVo = new DataCountVo();
            dataCountVo.setListCount(v.size());
            dataCountVo.setId(k);
            dataCountVoList.add(dataCountVo);
        });
        List<PatrolInspectionAreaVo> patrolInspectionAreaVoList = new ArrayList<>();
        if (patrolInspectionAreaList.size() > 0) {
            for (int i = 0; i < patrolInspectionAreaList.size(); i++) {
                PatrolInspectionAreaVo patrolInspectionAreaVo = new PatrolInspectionAreaVo();
                patrolInspectionAreaVo.setId(patrolInspectionAreaList.get(i).getId());
                patrolInspectionAreaVo.setAreaName(patrolInspectionAreaList.get(i).getAreaName());
                patrolInspectionAreaVo.setAreaFid(patrolInspectionAreaList.get(i).getAreaFid());
                patrolInspectionAreaVo.setDeptId(patrolInspectionAreaList.get(i).getDeptId());
                patrolInspectionAreaVo.setTenantId(patrolInspectionAreaList.get(i).getTenantId());
                patrolInspectionAreaVo.setCreator(patrolInspectionAreaList.get(i).getCreator());
                patrolInspectionAreaVo.setCreateTime(patrolInspectionAreaList.get(i).getCreateTime());
                patrolInspectionAreaVo.setEnable(patrolInspectionAreaList.get(i).getEnable());
                patrolInspectionAreaVo.setSiteCount(0);
                patrolInspectionAreaVo.setPlanCount(0);
                for (int j = 0; j < dataCountVoList.size(); j++) {
                    if (patrolInspectionAreaList.get(i).getId().equals(dataCountVoList.get(j).getId())) {
                        patrolInspectionAreaVo.setPlanCount(dataCountVoList.get(j).getListCount());
                    }
                }
                patrolInspectionAreaVoList.add(patrolInspectionAreaVo);
            }
        }
        List<PatrolInspectionAreaVo> patrolInspectionAreaVoListOne = new ArrayList<>();
        if (patrolInspectionAreaVoList.size() > 0) {
            patrolInspectionAreaVoListOne = OnlineMethod.getChildPerms(patrolInspectionAreaVoList, 0);
        }
        return patrolInspectionAreaVoListOne;
    }

    @Override
    @Transactional
    public void addPatrolInspectionPlan(PatrolInspectionPlanVo patrolInspectionPlanVo) {
        Integer planCycle = patrolInspectionPlanVo.getPlanCycle();
        String timeStr = patrolInspectionPlanVo.getStartDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        Date startDate = OnlineMethod.getDate(timeStr);
        LocalDate ss = null;
        if (patrolInspectionPlanVo.getPlanType()==2){
            if (planCycle == 1) {
                ss = OnlineMethod.getDate(timeStr, "yyyy-MM-dd", true, 1 * patrolInspectionPlanVo.getWeekCount() - 1);
            } else if (planCycle == 2) {
                ss = OnlineMethod.getDate(timeStr, "yyyy-MM-dd", true, 7 * patrolInspectionPlanVo.getWeekCount() - 1);
            } else if (planCycle == 3) {
                ss = OnlineMethod.getMonthDate(startDate, 1 * patrolInspectionPlanVo.getWeekCount());
            } else if (planCycle == 4) {
                ss = OnlineMethod.getMonthDate(startDate, 12 * patrolInspectionPlanVo.getWeekCount());
            }
        }
        //主表数据添加
        PatrolInspectionPlan patrolInspectionPlan = new PatrolInspectionPlan();
        patrolInspectionPlan.setPlanName(patrolInspectionPlanVo.getPlanName());
        patrolInspectionPlan.setStartDate(patrolInspectionPlanVo.getStartDate());
        if (patrolInspectionPlanVo.getPlanType() == 1) {
            patrolInspectionPlan.setEndDate(patrolInspectionPlanVo.getEndDate());
        } else {
            patrolInspectionPlan.setEndDate(ss);
        }
        patrolInspectionPlan.setAreaId(patrolInspectionPlanVo.getAreaId());
        patrolInspectionPlan.setPlanType(patrolInspectionPlanVo.getPlanType());
        patrolInspectionPlan.setPlanCycle(patrolInspectionPlanVo.getPlanCycle());
        patrolInspectionPlan.setPlanFrequency(patrolInspectionPlanVo.getPlanFrequency());
        patrolInspectionPlan.setLapTime(patrolInspectionPlanVo.getLapTime());
        patrolInspectionPlan.setIntervalTime(patrolInspectionPlanVo.getIntervalTime());
        patrolInspectionPlan.setPlanDescribe(patrolInspectionPlanVo.getPlanDescribe());
        patrolInspectionPlan.setCreateTime(LocalDateTime.now());
        patrolInspectionPlan.setCreator(SecurityUtils.getUsername());
        patrolInspectionPlan.setEnable(1);
        patrolInspectionPlan.setTenantId(SecurityUtils.getTenantId());
        patrolInspectionPlan.setCompanyId(patrolInspectionPlanVo.getCompanyId());
        patrolInspectionPlan.setRestDay(patrolInspectionPlanVo.getRestDay());
        patrolInspectionPlan.setWeekCount(patrolInspectionPlanVo.getWeekCount());
        patrolInspectionPlan.setRouteId(patrolInspectionPlanVo.getRouteId());//增加路线表主键id
        this.save(patrolInspectionPlan);
        Integer fid = patrolInspectionPlan.getId();
        //巡检日程添加
        for (int i = 0; i < patrolInspectionPlanVo.getPlanScheduleList().size(); i++) {
            patrolInspectionPlanVo.getPlanScheduleList().get(i).setPlanId(fid);
            planScheduleService.save(patrolInspectionPlanVo.getPlanScheduleList().get(i));
        }
        //主计划地点添加
        for (int i = 0; i < patrolInspectionPlanVo.getSiteArray().length; i++) {
            PatrolInspectionPlanSite planSite = new PatrolInspectionPlanSite();
            planSite.setPlanId(fid);
            planSite.setSiteId(patrolInspectionPlanVo.getSiteArray()[i]);
            planSiteService.save(planSite);
        }

        //特殊休息日关联信息添加
        String timeStr3 = patrolInspectionPlanVo.getStartDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        int days = 0;
        if (patrolInspectionPlanVo.getPlanType() == 1) {
            days = OnlineMethod.fun(patrolInspectionPlanVo.getStartDate(), patrolInspectionPlanVo.getEndDate())+1;
        } else {
            LocalDate date = OnlineMethod.getDate(timeStr3, "yyyy-MM-dd", true, 7 * (patrolInspectionPlanVo.getWeekCount() + 1));
            days = OnlineMethod.fun(patrolInspectionPlanVo.getStartDate(), date);
        }
        for (int i = 0; i < days; i++) {
            LocalDate date1 = OnlineMethod.getDate(timeStr3, "yyyy-MM-dd", true, i);
            Date date = Date.from(date1.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
            String week1 = OnlineMethod.getWeekOfDate(date);
            if (patrolInspectionPlanVo.getRestDay().contains(week1)) {
                PatrolInspectionSpecialRestDay specialRestDay = new PatrolInspectionSpecialRestDay();
                specialRestDay.setPlanId(fid);
                specialRestDay.setRestDate(date1);
                specialRestDayService.save(specialRestDay);
            }
        }
        //子表数据添加
        if (patrolInspectionPlanVo.getPlanType() == 1) {//普通计划
            //计算两时间天数
            int day = OnlineMethod.fun(patrolInspectionPlanVo.getStartDate(), patrolInspectionPlanVo.getEndDate())+1;
            if(day > 7){
                day = 7;
            }
//            if (patrolInspectionPlanVo.getStartDate().equals(patrolInspectionPlanVo.getEndDate())){
//                day = 1;
//            }else {
//                day = OnlineMethod.fun(patrolInspectionPlanVo.getStartDate(), patrolInspectionPlanVo.getEndDate())+1;
//            }

            String timeStr1 = patrolInspectionPlanVo.getStartDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            for (int i = 0; i < day; i++) {
                for (int j = 0; j < patrolInspectionPlanVo.getPlanScheduleList().size(); j++) {
                    LocalDate s = OnlineMethod.getDate(timeStr1, "yyyy-MM-dd", true, i);
                    Date date = Date.from(s.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
                    String week = OnlineMethod.getWeekOfDate(date);
                    if (!patrolInspectionPlanVo.getRestDay().contains(week)) {
                        PatrolInspectionPlanSon patrolInspectionPlanSon = new PatrolInspectionPlanSon();
                        patrolInspectionPlanSon.setPlanId(fid);
                        patrolInspectionPlanSon.setInspectionDate(s);
                        patrolInspectionPlanSon.setStartTime(patrolInspectionPlanVo.getPlanScheduleList().get(j).getStartTime());
                        patrolInspectionPlanSon.setEndTime(patrolInspectionPlanVo.getPlanScheduleList().get(j).getEndTime());
                        patrolInspectionPlanSon.setPersonnelId(patrolInspectionPlanVo.getPlanScheduleList().get(j).getPersonnelId());
                        patrolInspectionPlanSon.setAreaId(patrolInspectionPlanVo.getAreaId());
                        patrolInspectionPlanSon.setPlanType(patrolInspectionPlanVo.getPlanType());
                        patrolInspectionPlanSon.setPlanCycle(patrolInspectionPlanVo.getPlanCycle());
                        patrolInspectionPlanSon.setPlanFrequency(patrolInspectionPlanVo.getPlanFrequency());
                        patrolInspectionPlanSon.setLapTime(patrolInspectionPlanVo.getLapTime());
                        patrolInspectionPlanSon.setIntervalTime(patrolInspectionPlanVo.getIntervalTime());
                        patrolInspectionPlanSon.setPlanDescribe(patrolInspectionPlanVo.getPlanDescribe());
                        patrolInspectionPlanSon.setCompletion(0);
                        patrolInspectionPlanSon.setCreateTime(LocalDateTime.now());
                        patrolInspectionPlanSon.setCreator(SecurityUtils.getUsername());
                        patrolInspectionPlanSon.setAlternateField("0");
                        patrolInspectionPlanSon.setTenantId(SecurityUtils.getTenantId());
                        patrolInspectionPlanSon.setCompanyId(patrolInspectionPlanVo.getCompanyId());
                        patrolInspectionPlanSonService.save(patrolInspectionPlanSon);
                        Integer zfid = patrolInspectionPlanSon.getId();
                        for (int k = 0; k < patrolInspectionPlanVo.getSiteArray().length; k++) {
                            PatrolInspectionPlanSiteSon planSiteSon = new PatrolInspectionPlanSiteSon();
                            planSiteSon.setPlanId(zfid);
                            planSiteSon.setSiteId(patrolInspectionPlanVo.getSiteArray()[k]);
                            planSiteSon.setInspectionStatus(1);
                            planSiteSonService.save(planSiteSon);
                        }
                    }
                }
            }
        } else {//按次计划
            for (int i = 0; i < patrolInspectionPlanVo.getPlanFrequency(); i++) {
                PatrolInspectionPlanSon patrolInspectionPlanSon = new PatrolInspectionPlanSon();
                patrolInspectionPlanSon.setPlanId(fid);
                patrolInspectionPlanSon.setStartDate(patrolInspectionPlanVo.getStartDate());
                patrolInspectionPlanSon.setEndDate(ss);
                patrolInspectionPlanSon.setPersonnelId(patrolInspectionPlanVo.getPlanScheduleList().get(0).getPersonnelId());
                patrolInspectionPlanSon.setAreaId(patrolInspectionPlanVo.getAreaId());
                patrolInspectionPlanSon.setPlanType(patrolInspectionPlanVo.getPlanType());
                patrolInspectionPlanSon.setPlanCycle(patrolInspectionPlanVo.getPlanCycle());
                patrolInspectionPlanSon.setPlanFrequency(patrolInspectionPlanVo.getPlanFrequency());
                patrolInspectionPlanSon.setLapTime(patrolInspectionPlanVo.getLapTime());
                patrolInspectionPlanSon.setIntervalTime(patrolInspectionPlanVo.getIntervalTime());
                patrolInspectionPlanSon.setPlanDescribe(patrolInspectionPlanVo.getPlanDescribe());
                patrolInspectionPlanSon.setCompletion(0);
                patrolInspectionPlanSon.setCreateTime(LocalDateTime.now());
                patrolInspectionPlanSon.setCreator(SecurityUtils.getUsername());
                patrolInspectionPlanSon.setAlternateField("0");
                patrolInspectionPlanSon.setTenantId(SecurityUtils.getTenantId());
                patrolInspectionPlanSon.setCompanyId(patrolInspectionPlanVo.getCompanyId());
                patrolInspectionPlanSonService.save(patrolInspectionPlanSon);
                Integer zfid = patrolInspectionPlanSon.getId();
                for (int k = 0; k < patrolInspectionPlanVo.getSiteArray().length; k++) {
                    PatrolInspectionPlanSiteSon planSiteSon = new PatrolInspectionPlanSiteSon();
                    planSiteSon.setPlanId(zfid);
                    planSiteSon.setSiteId(patrolInspectionPlanVo.getSiteArray()[k]);
                    planSiteSon.setInspectionStatus(1);
                    planSiteSonService.save(planSiteSon);
                }
            }

        }

    }

    @Override
    @Transactional
    public void updatePatrolInspectionPlan(PatrolInspectionPlanVo patrolInspectionPlanVo) {
//        LambdaQueryWrapper<PatrolInspectionRecord> queryWrapperSix = Wrappers.lambdaQuery();
//        queryWrapperSix.eq(PatrolInspectionRecord::getPlanId, patrolInspectionPlanVo.getId());
//        List<PatrolInspectionRecord> patrolInspectionRecordList = patrolInspectionRecordService.list(queryWrapperSix);
//        if (patrolInspectionRecordList.size() > 0) {
//            throw new BusinessException("巡检计划已执行中不可修改");
//        }
        Integer planCycle = patrolInspectionPlanVo.getPlanCycle();
        String timeStr = patrolInspectionPlanVo.getStartDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        Date startDate = OnlineMethod.getDate(timeStr);
        LocalDate ss = null;
        if (patrolInspectionPlanVo.getPlanType()==2){
            if (planCycle == 1) {
                ss = OnlineMethod.getDate(timeStr, "yyyy-MM-dd", true, 1 * patrolInspectionPlanVo.getWeekCount() - 1);
            } else if (planCycle == 2) {
                ss = OnlineMethod.getDate(timeStr, "yyyy-MM-dd", true, 7 * patrolInspectionPlanVo.getWeekCount() - 1);
            } else if (planCycle == 3) {
                ss = OnlineMethod.getMonthDate(startDate, 1 * patrolInspectionPlanVo.getWeekCount());
            } else if (planCycle == 4) {
                ss = OnlineMethod.getMonthDate(startDate, 12 * patrolInspectionPlanVo.getWeekCount());
            }
        }


        //主表数据修改
        PatrolInspectionPlan patrolInspectionPlan = new PatrolInspectionPlan();
        patrolInspectionPlan.setId(patrolInspectionPlanVo.getId());
        patrolInspectionPlan.setPlanName(patrolInspectionPlanVo.getPlanName());
        patrolInspectionPlan.setStartDate(patrolInspectionPlanVo.getStartDate());
        if (patrolInspectionPlanVo.getPlanType() == 1) {
            patrolInspectionPlan.setEndDate(patrolInspectionPlanVo.getEndDate());
        } else {
            patrolInspectionPlan.setEndDate(ss);
        }
        patrolInspectionPlan.setAreaId(patrolInspectionPlanVo.getAreaId());
        patrolInspectionPlan.setPlanType(patrolInspectionPlanVo.getPlanType());
        patrolInspectionPlan.setPlanCycle(patrolInspectionPlanVo.getPlanCycle());
        patrolInspectionPlan.setPlanFrequency(patrolInspectionPlanVo.getPlanFrequency());
        patrolInspectionPlan.setLapTime(patrolInspectionPlanVo.getLapTime());
        patrolInspectionPlan.setIntervalTime(patrolInspectionPlanVo.getIntervalTime());
        patrolInspectionPlan.setPlanDescribe(patrolInspectionPlanVo.getPlanDescribe());
        patrolInspectionPlan.setCompanyId(patrolInspectionPlanVo.getCompanyId());
        patrolInspectionPlan.setRestDay(patrolInspectionPlanVo.getRestDay());
        patrolInspectionPlan.setWeekCount(patrolInspectionPlanVo.getWeekCount());
        patrolInspectionPlan.setRouteId((patrolInspectionPlanVo.getRouteId()));
        this.updateById(patrolInspectionPlan);
        //巡检日程更新
        LambdaQueryWrapper<PatrolInspectionPlanSchedule> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PatrolInspectionPlanSchedule::getPlanId, patrolInspectionPlanVo.getId());
        List<PatrolInspectionPlanSchedule> planScheduleList = planScheduleService.list(queryWrapper);
        for (int i = 0; i < planScheduleList.size(); i++) {
            planScheduleService.removeById(planScheduleList.get(i).getId());
        }
        for (int i = 0; i < patrolInspectionPlanVo.getPlanScheduleList().size(); i++) {
            patrolInspectionPlanVo.getPlanScheduleList().get(i).setPlanId(patrolInspectionPlanVo.getId());
            planScheduleService.save(patrolInspectionPlanVo.getPlanScheduleList().get(i));
        }
        //主计划地点更新
        LambdaQueryWrapper<PatrolInspectionPlanSite> queryWrapperOne = Wrappers.lambdaQuery();
        queryWrapperOne.eq(PatrolInspectionPlanSite::getPlanId, patrolInspectionPlanVo.getId());
        List<PatrolInspectionPlanSite> planSiteList = planSiteService.list(queryWrapperOne);
        for (int i = 0; i < planSiteList.size(); i++) {
            planSiteService.removeById(planSiteList.get(i).getId());
        }
        for (int i = 0; i < patrolInspectionPlanVo.getSiteArray().length; i++) {
            PatrolInspectionPlanSite planSite = new PatrolInspectionPlanSite();
            planSite.setPlanId(patrolInspectionPlanVo.getId());
            planSite.setSiteId(patrolInspectionPlanVo.getSiteArray()[i]);
            planSiteService.save(planSite);
        }

        //特殊休息日关联信息更改
        LambdaQueryWrapper<PatrolInspectionSpecialRestDay> queryWrapperFour = Wrappers.lambdaQuery();
        queryWrapperFour.eq(PatrolInspectionSpecialRestDay::getPlanId, patrolInspectionPlanVo.getId());
        List<PatrolInspectionSpecialRestDay> specialRestDayList = specialRestDayService.list(queryWrapperFour);
        for (int i = 0; i < specialRestDayList.size(); i++) {
            specialRestDayService.removeById(specialRestDayList.get(i).getId());
        }
        String timeStr3 = patrolInspectionPlanVo.getStartDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        int days = 0;
        if (patrolInspectionPlanVo.getPlanType() == 1) {
            days = OnlineMethod.fun(patrolInspectionPlanVo.getStartDate(), patrolInspectionPlanVo.getEndDate())+1;
        } else {
            LocalDate date = OnlineMethod.getDate(timeStr3, "yyyy-MM-dd", true, 7 * (patrolInspectionPlanVo.getWeekCount() + 1));
            days = OnlineMethod.fun(patrolInspectionPlanVo.getStartDate(), date);
        }
        for (int i = 0; i < days; i++) {
            LocalDate date1 = OnlineMethod.getDate(timeStr3, "yyyy-MM-dd", true, i );
            Date date = Date.from(date1.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
            String week1 = OnlineMethod.getWeekOfDate(date);
            if (patrolInspectionPlanVo.getRestDay().contains(week1)) {
                PatrolInspectionSpecialRestDay specialRestDay = new PatrolInspectionSpecialRestDay();
                specialRestDay.setPlanId(patrolInspectionPlanVo.getId());
                specialRestDay.setRestDate(date1);
                specialRestDayService.save(specialRestDay);
            }
        }
        //子表数据更新
        LambdaQueryWrapper<PatrolInspectionPlanSon> queryWrapperTwo = Wrappers.lambdaQuery();
        queryWrapperTwo.eq(PatrolInspectionPlanSon::getPlanId, patrolInspectionPlanVo.getId());
        List<PatrolInspectionPlanSon> patrolInspectionPlanSonList = patrolInspectionPlanSonService.list(queryWrapperTwo);
        for (int i = 0; i < patrolInspectionPlanSonList.size(); i++) {
            LambdaQueryWrapper<PatrolInspectionPlanSiteSon> queryWrapperThree = Wrappers.lambdaQuery();
            queryWrapperThree.eq(PatrolInspectionPlanSiteSon::getPlanId, patrolInspectionPlanSonList.get(i).getId());
            List<PatrolInspectionPlanSiteSon> planSiteSonList = planSiteSonService.list(queryWrapperThree);
            for (int j = 0; j < planSiteSonList.size(); j++) {
                planSiteSonService.removeById(planSiteSonList.get(j).getId());
            }
            patrolInspectionPlanSonService.removeById(patrolInspectionPlanSonList.get(i).getId());
        }

        if (patrolInspectionPlanVo.getPlanType() == 1) {//普通计划
            //计算两时间天数
            int day = OnlineMethod.fun(patrolInspectionPlanVo.getStartDate(), patrolInspectionPlanVo.getEndDate())+1;
            if(day > 7){
                day = 7;
            }

            String timeStr1 = patrolInspectionPlanVo.getStartDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            for (int i = 0; i < day; i++) {
                for (int j = 0; j < patrolInspectionPlanVo.getPlanScheduleList().size(); j++) {
                    LocalDate s = OnlineMethod.getDate(timeStr1, "yyyy-MM-dd", true, i );
                    Date date = Date.from(s.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
                    String week = OnlineMethod.getWeekOfDate(date);
                    if (!patrolInspectionPlanVo.getRestDay().contains(week)) {
                        PatrolInspectionPlanSon patrolInspectionPlanSon = new PatrolInspectionPlanSon();
                        patrolInspectionPlanSon.setPlanId(patrolInspectionPlanVo.getId());
                        patrolInspectionPlanSon.setInspectionDate(s);
                        patrolInspectionPlanSon.setStartTime(patrolInspectionPlanVo.getPlanScheduleList().get(j).getStartTime());
                        patrolInspectionPlanSon.setEndTime(patrolInspectionPlanVo.getPlanScheduleList().get(j).getEndTime());
                        patrolInspectionPlanSon.setPersonnelId(patrolInspectionPlanVo.getPlanScheduleList().get(j).getPersonnelId());
                        patrolInspectionPlanSon.setAreaId(patrolInspectionPlanVo.getAreaId());
                        patrolInspectionPlanSon.setPlanType(patrolInspectionPlanVo.getPlanType());
                        patrolInspectionPlanSon.setPlanCycle(patrolInspectionPlanVo.getPlanCycle());
                        patrolInspectionPlanSon.setPlanFrequency(patrolInspectionPlanVo.getPlanFrequency());
                        patrolInspectionPlanSon.setLapTime(patrolInspectionPlanVo.getLapTime());
                        patrolInspectionPlanSon.setIntervalTime(patrolInspectionPlanVo.getIntervalTime());
                        patrolInspectionPlanSon.setPlanDescribe(patrolInspectionPlanVo.getPlanDescribe());
                        patrolInspectionPlanSon.setCompletion(0);
                        patrolInspectionPlanSon.setCreateTime(LocalDateTime.now());
                        patrolInspectionPlanSon.setCreator(SecurityUtils.getUsername());
                        patrolInspectionPlanSon.setAlternateField("0");
                        patrolInspectionPlanSon.setTenantId(SecurityUtils.getTenantId());
                        patrolInspectionPlanSon.setCompanyId(patrolInspectionPlanVo.getCompanyId());
                        patrolInspectionPlanSonService.save(patrolInspectionPlanSon);
                        Integer zfid = patrolInspectionPlanSon.getId();
                        for (int k = 0; k < patrolInspectionPlanVo.getSiteArray().length; k++) {
                            PatrolInspectionPlanSiteSon planSiteSon = new PatrolInspectionPlanSiteSon();
                            planSiteSon.setPlanId(zfid);
                            planSiteSon.setSiteId(patrolInspectionPlanVo.getSiteArray()[k]);
                            planSiteSon.setInspectionStatus(1);
                            planSiteSonService.save(planSiteSon);
                        }
                    }
                }
            }
        } else {//按次计划
            for (int i = 0; i < patrolInspectionPlanVo.getPlanFrequency(); i++) {
                PatrolInspectionPlanSon patrolInspectionPlanSon = new PatrolInspectionPlanSon();
                patrolInspectionPlanSon.setPlanId(patrolInspectionPlanVo.getId());
                patrolInspectionPlanSon.setStartDate(patrolInspectionPlanVo.getStartDate());
                patrolInspectionPlanSon.setEndDate(ss);
                patrolInspectionPlanSon.setPersonnelId(patrolInspectionPlanVo.getPlanScheduleList().get(0).getPersonnelId());
                patrolInspectionPlanSon.setAreaId(patrolInspectionPlanVo.getAreaId());
                patrolInspectionPlanSon.setPlanType(patrolInspectionPlanVo.getPlanType());
                patrolInspectionPlanSon.setPlanCycle(patrolInspectionPlanVo.getPlanCycle());
                patrolInspectionPlanSon.setPlanFrequency(patrolInspectionPlanVo.getPlanFrequency());
                patrolInspectionPlanSon.setLapTime(patrolInspectionPlanVo.getLapTime());
                patrolInspectionPlanSon.setIntervalTime(patrolInspectionPlanVo.getIntervalTime());
                patrolInspectionPlanSon.setPlanDescribe(patrolInspectionPlanVo.getPlanDescribe());
                patrolInspectionPlanSon.setCompletion(0);
                patrolInspectionPlanSon.setCreateTime(LocalDateTime.now());
                patrolInspectionPlanSon.setCreator(SecurityUtils.getUsername());
                patrolInspectionPlanSon.setAlternateField("0");
                patrolInspectionPlanSon.setTenantId(SecurityUtils.getTenantId());
                patrolInspectionPlanSon.setCompanyId(patrolInspectionPlanVo.getCompanyId());
                patrolInspectionPlanSonService.save(patrolInspectionPlanSon);
                Integer zfid = patrolInspectionPlanSon.getId();
                for (int k = 0; k < patrolInspectionPlanVo.getSiteArray().length; k++) {
                    PatrolInspectionPlanSiteSon planSiteSon = new PatrolInspectionPlanSiteSon();
                    planSiteSon.setPlanId(zfid);
                    planSiteSon.setSiteId(patrolInspectionPlanVo.getSiteArray()[k]);
                    planSiteSon.setInspectionStatus(1);
                    planSiteSonService.save(planSiteSon);
                }
            }
        }
    }

    @Override
    @Transactional
    public void addPatrolInspectionPlanSon(){
        LambdaQueryWrapper<PatrolInspectionPlan> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.le(PatrolInspectionPlan::getStartDate,LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")))
                .ge(PatrolInspectionPlan::getEndDate,OnlineMethod.getDate(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")), "yyyy-MM-dd", true, 7))
                .eq(PatrolInspectionPlan::getEnable,1);
        List<PatrolInspectionPlan> list = this.list(queryWrapper);
        if(CollectionUtils.isNotEmpty(list)){
            List<Integer> planIdList = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                planIdList.add(list.get(i).getId());
            }

            //巡检日程关联信息
            LambdaQueryWrapper<PatrolInspectionPlanSchedule> queryWrapper1 = Wrappers.lambdaQuery();
            queryWrapper1.in(PatrolInspectionPlanSchedule::getPlanId,planIdList);
            List<PatrolInspectionPlanSchedule> scheduleList = planScheduleService.list(queryWrapper1);
            if(CollectionUtils.isEmpty(scheduleList)){
                throw new BusinessException("无巡检日程关联信息");
            }

            //主计划地点关联
            LambdaQueryWrapper<PatrolInspectionPlanSite> queryWrapper2 = Wrappers.lambdaQuery();
            queryWrapper2.in(PatrolInspectionPlanSite::getPlanId,planIdList);
            List<PatrolInspectionPlanSite> siteList = planSiteService.list(queryWrapper2);
            if(CollectionUtils.isEmpty(siteList)){
                throw new BusinessException("无主计划地点关联信息");
            }

            //分配创建巡检计划之前先判断数据库中是否已经存在这条计划记录
            LocalDate inspectionDate = OnlineMethod.getDate(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")), "yyyy-MM-dd", true, 7);
            LambdaQueryWrapper<PatrolInspectionPlanSon> queryWrapper3 = Wrappers.lambdaQuery();
            queryWrapper3.select(PatrolInspectionPlanSon::getPlanId)
                    .eq(PatrolInspectionPlanSon::getInspectionDate,inspectionDate);
            List<PatrolInspectionPlanSon> planSonList = patrolInspectionPlanSonService.list(queryWrapper3);
            List<Integer> planSonPlanIdList = new ArrayList<>();
            if(CollectionUtils.isNotEmpty(planSonList)){
                for (int i = 0; i < planSonList.size(); i++) {
                    planSonPlanIdList.add(planSonList.get(i).getPlanId());
                }
            }

            for (int m = 0; m < list.size(); m++) {
                //子表数据添加
                if (list.get(m).getPlanType() == 1) {//普通计划 当前日期+7天在巡检计划时间范围内，就创建一条当前日期+7天日期对应的巡检记录
                    for (int j = 0; j < scheduleList.size(); j++) {
                        if((list.get(m).getId().equals(scheduleList.get(j).getPlanId())) && (!planSonPlanIdList.contains(list.get(m).getId()))){
                            LocalDate s = OnlineMethod.getDate(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")), "yyyy-MM-dd", true, 7);
                            Date date = Date.from(s.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
                            String week = OnlineMethod.getWeekOfDate(date);
                            if (!list.get(m).getRestDay().contains(week)) {
                                PatrolInspectionPlanSon patrolInspectionPlanSon = new PatrolInspectionPlanSon();
                                patrolInspectionPlanSon.setPlanId(list.get(m).getId());
                                patrolInspectionPlanSon.setInspectionDate(s);
                                patrolInspectionPlanSon.setStartTime(scheduleList.get(j).getStartTime());
                                patrolInspectionPlanSon.setEndTime(scheduleList.get(j).getEndTime());
                                patrolInspectionPlanSon.setPersonnelId(scheduleList.get(j).getPersonnelId());
                                patrolInspectionPlanSon.setAreaId(list.get(m).getAreaId());
                                patrolInspectionPlanSon.setPlanType(list.get(m).getPlanType());
                                patrolInspectionPlanSon.setPlanCycle(list.get(m).getPlanCycle());
                                patrolInspectionPlanSon.setPlanFrequency(list.get(m).getPlanFrequency());
                                patrolInspectionPlanSon.setLapTime(list.get(m).getLapTime());
                                patrolInspectionPlanSon.setIntervalTime(list.get(m).getIntervalTime());
                                patrolInspectionPlanSon.setPlanDescribe(list.get(m).getPlanDescribe());
                                patrolInspectionPlanSon.setCompletion(0);
                                patrolInspectionPlanSon.setCreateTime(LocalDateTime.now());
                                patrolInspectionPlanSon.setCreator(list.get(m).getCreator());
                                patrolInspectionPlanSon.setAlternateField("0");
                                patrolInspectionPlanSon.setTenantId(list.get(m).getTenantId());
                                patrolInspectionPlanSon.setCompanyId(list.get(m).getCompanyId());
                                patrolInspectionPlanSonService.save(patrolInspectionPlanSon);
                                Integer zfid = patrolInspectionPlanSon.getId();
                                for (int k = 0; k < siteList.size(); k++) {
                                    if(list.get(m).getId().equals(siteList.get(k).getPlanId())){
                                        PatrolInspectionPlanSiteSon planSiteSon = new PatrolInspectionPlanSiteSon();
                                        planSiteSon.setPlanId(zfid);
                                        planSiteSon.setSiteId(siteList.get(k).getSiteId());
                                        planSiteSon.setInspectionStatus(1);
                                        planSiteSonService.save(planSiteSon);
                                    }

                                }
                            }
                        }

                    }
                }
            }

        }
    }

    /**
     * 查询近七天（今天 ~ 今天+6）符合时间段的主计划，按日检查子计划是否存在；
     * 不存在则补插子计划及地点关联，已存在则跳过
     */
    @Override
    @Transactional
    public void supplementNearSevenDaysPlanSon() {
        String todayStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDate startOfNearSeven = LocalDate.now();
        LocalDate endOfNearSeven = OnlineMethod.getDate(todayStr, "yyyy-MM-dd", true, 6);

        // 查询近七天时间范围内生效的主计划：开始日期 <= 近七天末天，结束日期 >= 今天
        LambdaQueryWrapper<PatrolInspectionPlan> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.le(PatrolInspectionPlan::getStartDate, endOfNearSeven)
                .ge(PatrolInspectionPlan::getEndDate, startOfNearSeven)
                .eq(PatrolInspectionPlan::getEnable, 1)
                .eq(PatrolInspectionPlan::getPlanType, 1);
        List<PatrolInspectionPlan> list = this.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            return;
        }

        List<Integer> planIdList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            planIdList.add(list.get(i).getId());
        }

        // 巡检日程关联信息
        LambdaQueryWrapper<PatrolInspectionPlanSchedule> queryWrapper1 = Wrappers.lambdaQuery();
        queryWrapper1.in(PatrolInspectionPlanSchedule::getPlanId, planIdList);
        List<PatrolInspectionPlanSchedule> scheduleList = planScheduleService.list(queryWrapper1);
        if (CollectionUtils.isEmpty(scheduleList)) {
            throw new BusinessException("无巡检日程关联信息");
        }

        // 主计划地点关联
        LambdaQueryWrapper<PatrolInspectionPlanSite> queryWrapper2 = Wrappers.lambdaQuery();
        queryWrapper2.in(PatrolInspectionPlanSite::getPlanId, planIdList);
        List<PatrolInspectionPlanSite> siteList = planSiteService.list(queryWrapper2);
        if (CollectionUtils.isEmpty(siteList)) {
            throw new BusinessException("无主计划地点关联信息");
        }

        // 查询近七天已存在的子计划（按主计划id + 巡检日期判断是否存在）
        LambdaQueryWrapper<PatrolInspectionPlanSon> queryWrapper3 = Wrappers.lambdaQuery();
        queryWrapper3.select(PatrolInspectionPlanSon::getPlanId, PatrolInspectionPlanSon::getInspectionDate)
                .in(PatrolInspectionPlanSon::getPlanId, planIdList)
                .ge(PatrolInspectionPlanSon::getInspectionDate, startOfNearSeven)
                .le(PatrolInspectionPlanSon::getInspectionDate, endOfNearSeven);
        List<PatrolInspectionPlanSon> planSonList = patrolInspectionPlanSonService.list(queryWrapper3);
        List<String> existPlanSonKeyList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(planSonList)) {
            for (int i = 0; i < planSonList.size(); i++) {
                existPlanSonKeyList.add(planSonList.get(i).getPlanId() + "_" + planSonList.get(i).getInspectionDate());
            }
        }

        // 逐日检查并补插缺失的子计划
        for (int dayOffset = 0; dayOffset < 7; dayOffset++) {
            LocalDate targetDate = dayOffset == 0 ? startOfNearSeven
                    : OnlineMethod.getDate(todayStr, "yyyy-MM-dd", true, dayOffset);
            Date date = Date.from(targetDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
            String week = OnlineMethod.getWeekOfDate(date);

            for (int m = 0; m < list.size(); m++) {
                PatrolInspectionPlan plan = list.get(m);
                // 目标日期需落在该主计划有效期内
                if (plan.getStartDate().isAfter(targetDate) || plan.getEndDate().isBefore(targetDate)) {
                    continue;
                }
                String existKey = plan.getId() + "_" + targetDate;
                // 已存在则不做任何操作
                if (existPlanSonKeyList.contains(existKey)) {
                    continue;
                }
                // 休息日不生成子计划
                if (plan.getRestDay() != null && plan.getRestDay().contains(week)) {
                    continue;
                }

                for (int j = 0; j < scheduleList.size(); j++) {
                    if (!plan.getId().equals(scheduleList.get(j).getPlanId())) {
                        continue;
                    }
                    PatrolInspectionPlanSon patrolInspectionPlanSon = new PatrolInspectionPlanSon();
                    patrolInspectionPlanSon.setPlanId(plan.getId());
                    patrolInspectionPlanSon.setInspectionDate(targetDate);
                    patrolInspectionPlanSon.setStartTime(scheduleList.get(j).getStartTime());
                    patrolInspectionPlanSon.setEndTime(scheduleList.get(j).getEndTime());
                    patrolInspectionPlanSon.setPersonnelId(scheduleList.get(j).getPersonnelId());
                    patrolInspectionPlanSon.setAreaId(plan.getAreaId());
                    patrolInspectionPlanSon.setPlanType(plan.getPlanType());
                    patrolInspectionPlanSon.setPlanCycle(plan.getPlanCycle());
                    patrolInspectionPlanSon.setPlanFrequency(plan.getPlanFrequency());
                    patrolInspectionPlanSon.setLapTime(plan.getLapTime());
                    patrolInspectionPlanSon.setIntervalTime(plan.getIntervalTime());
                    patrolInspectionPlanSon.setPlanDescribe(plan.getPlanDescribe());
                    patrolInspectionPlanSon.setCompletion(0);
                    patrolInspectionPlanSon.setCreateTime(LocalDateTime.now());
                    patrolInspectionPlanSon.setCreator(plan.getCreator());
                    patrolInspectionPlanSon.setAlternateField("0");
                    patrolInspectionPlanSon.setTenantId(plan.getTenantId());
                    patrolInspectionPlanSon.setCompanyId(plan.getCompanyId());
                    patrolInspectionPlanSonService.save(patrolInspectionPlanSon);
                    Integer zfid = patrolInspectionPlanSon.getId();
                    for (int k = 0; k < siteList.size(); k++) {
                        if (plan.getId().equals(siteList.get(k).getPlanId())) {
                            PatrolInspectionPlanSiteSon planSiteSon = new PatrolInspectionPlanSiteSon();
                            planSiteSon.setPlanId(zfid);
                            planSiteSon.setSiteId(siteList.get(k).getSiteId());
                            planSiteSon.setInspectionStatus(1);
                            planSiteSonService.save(planSiteSon);
                        }
                    }
                }
                // 补插后加入已存在标记，避免同日重复创建
                existPlanSonKeyList.add(existKey);
            }
        }
    }

    @Override
    @Transactional
    public void delPatrolInspectionPlan(Integer id) {
//        LambdaQueryWrapper<PatrolInspectionRecord> queryWrapperSix = Wrappers.lambdaQuery();
//        queryWrapperSix.eq(PatrolInspectionRecord::getPlanId, id);
//        List<PatrolInspectionRecord> patrolInspectionRecordList = patrolInspectionRecordService.list(queryWrapperSix);
//        if (patrolInspectionRecordList.size() > 0) {
//            throw new BusinessException("巡检计划已执行中不可删除");
//        }
        PatrolInspectionPlan patrolInspectionPlan = new PatrolInspectionPlan();
        patrolInspectionPlan.setId(id);
        patrolInspectionPlan.setEnable(0);
        this.updateById(patrolInspectionPlan);
        //特殊休息日关联信息更改
        LambdaQueryWrapper<PatrolInspectionSpecialRestDay> queryWrapperFour= Wrappers.lambdaQuery();
        queryWrapperFour.eq(PatrolInspectionSpecialRestDay::getPlanId, id);
        List<PatrolInspectionSpecialRestDay> specialRestDayList = specialRestDayService.list(queryWrapperFour);
        for (int i = 0; i < specialRestDayList.size(); i++) {
            specialRestDayService.removeById(specialRestDayList.get(i).getId());
        }
        //巡检日程删除
        LambdaQueryWrapper<PatrolInspectionPlanSchedule> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PatrolInspectionPlanSchedule::getPlanId, id);
        List<PatrolInspectionPlanSchedule> planScheduleList = planScheduleService.list(queryWrapper);
        for (int i = 0; i < planScheduleList.size(); i++) {
            planScheduleService.removeById(planScheduleList.get(i).getId());
        }
        //主计划地点删除
        LambdaQueryWrapper<PatrolInspectionPlanSite> queryWrapperOne = Wrappers.lambdaQuery();
        queryWrapperOne.eq(PatrolInspectionPlanSite::getPlanId, id);
        List<PatrolInspectionPlanSite> planSiteList = planSiteService.list(queryWrapperOne);
        for (int i = 0; i < planSiteList.size(); i++) {
            planSiteService.removeById(planSiteList.get(i).getId());
        }
        //子表数据删除
        LambdaQueryWrapper<PatrolInspectionPlanSon> queryWrapperTwo = Wrappers.lambdaQuery();
        queryWrapperTwo.eq(PatrolInspectionPlanSon::getPlanId,id);
        List<PatrolInspectionPlanSon> patrolInspectionPlanSonList = patrolInspectionPlanSonService.list(queryWrapperTwo);
        for (int i = 0; i < patrolInspectionPlanSonList.size(); i++) {
            LambdaQueryWrapper<PatrolInspectionPlanSiteSon> queryWrapperThree = Wrappers.lambdaQuery();
            queryWrapperThree.eq(PatrolInspectionPlanSiteSon::getPlanId, patrolInspectionPlanSonList.get(i).getId());
            List<PatrolInspectionPlanSiteSon> planSiteSonList = planSiteSonService.list(queryWrapperThree);
            for (int j = 0; j < planSiteSonList.size(); j++) {
                planSiteSonService.removeById(planSiteSonList.get(j).getId());
            }
            patrolInspectionPlanSonService.removeById(patrolInspectionPlanSonList.get(i).getId());
        }
    }

    @Override
    public CommonPage<PatrolInspectionPlanDataVo> patrolInspectionPlanList(String planName, Integer planType, Integer areaId, Integer pageNum, Integer pageSize, Integer id) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String userType = null;
        if (loginUser != null && !"".equals(loginUser)) {
            userType = loginUser.getUserType();
        }
        LambdaQueryWrapper<PatrolInspectionPlan> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PatrolInspectionPlan::getEnable, 1)
                .eq(PatrolInspectionPlan::getTenantId, SecurityUtils.getTenantId());
        if ("00".equals(userType)) {
            queryWrapper.eq(PatrolInspectionPlan::getCreator, SecurityUtils.getUsername());
        }

        if (planName != null && "".equals(planName)) {
            queryWrapper.like(PatrolInspectionPlan::getPlanName, planName);
        }
        if (id != null && id != 0) {
            queryWrapper.eq(PatrolInspectionPlan::getId, id);
        }
        if (planType != null && planType != 0) {
            queryWrapper.eq(PatrolInspectionPlan::getPlanType, planType);
        }
        if (areaId != null && areaId != 0) {
            queryWrapper.eq(PatrolInspectionPlan::getAreaId, areaId);
        }
        queryWrapper.orderByDesc(PatrolInspectionPlan::getId);

        Integer total = this.count(queryWrapper);
        queryWrapper.orderByDesc(PatrolInspectionPlan::getId);
        if (pageNum != null && pageNum != 0 && pageSize != null && pageSize != 0) {
            Integer startFate = OnlineMethod.getStartFate(pageNum, pageSize);
            queryWrapper.last("limit " + startFate + "," + pageSize);
        }
        List<PatrolInspectionPlan> patrolInspectionPlanSonList = this.list(queryWrapper);
        List<PatrolInspectionPlanSchedule> planScheduleList = planScheduleService.list();
        List<PatrolInspectionPersonnelVo> personnelList = patrolInspectionPersonnelService.patrolInspectionPersonnelSelect(areaId);
        List<PlanScheduleVo> list = new ArrayList<>();
        for (int i = 0; i < planScheduleList.size(); i++) {
            PlanScheduleVo planScheduleVo = new PlanScheduleVo();
            planScheduleVo.setTime(planScheduleList.get(i).getStartTime() + "~" + planScheduleList.get(i).getEndTime());
            planScheduleVo.setPersonnelId(planScheduleList.get(i).getPersonnelId());
            planScheduleVo.setPlanId(planScheduleList.get(i).getPlanId());
            for (int j = 0; j < personnelList.size(); j++) {
                if (planScheduleList.get(i).getPersonnelId().equals(personnelList.get(j).getId())) {
                    planScheduleVo.setPersonnelName(personnelList.get(j).getName());
                }
            }
            list.add(planScheduleVo);
        }
        List<PatrolInspectionPlanDataVo> list1 = new ArrayList<>();
        for (int i = 0; i < patrolInspectionPlanSonList.size(); i++) {
            PatrolInspectionPlanDataVo patrolInspectionPlanDataVo = new PatrolInspectionPlanDataVo();
            patrolInspectionPlanDataVo.setId(patrolInspectionPlanSonList.get(i).getId());
            patrolInspectionPlanDataVo.setPlanName(patrolInspectionPlanSonList.get(i).getPlanName());
            patrolInspectionPlanDataVo.setStartDate(patrolInspectionPlanSonList.get(i).getStartDate());
            patrolInspectionPlanDataVo.setEndDate(patrolInspectionPlanSonList.get(i).getEndDate());
            patrolInspectionPlanDataVo.setAreaId(patrolInspectionPlanSonList.get(i).getAreaId());
            patrolInspectionPlanDataVo.setPlanType(patrolInspectionPlanSonList.get(i).getPlanType());
            patrolInspectionPlanDataVo.setPlanCycle(patrolInspectionPlanSonList.get(i).getPlanCycle());
            patrolInspectionPlanDataVo.setPlanFrequency(patrolInspectionPlanSonList.get(i).getPlanFrequency());
            patrolInspectionPlanDataVo.setLapTime(patrolInspectionPlanSonList.get(i).getLapTime());
            patrolInspectionPlanDataVo.setIntervalTime(patrolInspectionPlanSonList.get(i).getIntervalTime());
            patrolInspectionPlanDataVo.setPlanDescribe(patrolInspectionPlanSonList.get(i).getPlanDescribe());
            patrolInspectionPlanDataVo.setCreateTime(patrolInspectionPlanSonList.get(i).getCreateTime());
            patrolInspectionPlanDataVo.setCreator(patrolInspectionPlanSonList.get(i).getCreator());
            patrolInspectionPlanDataVo.setEnable(patrolInspectionPlanSonList.get(i).getEnable());
            patrolInspectionPlanDataVo.setTenantId(patrolInspectionPlanSonList.get(i).getTenantId());
            patrolInspectionPlanDataVo.setCompanyId(patrolInspectionPlanSonList.get(i).getCompanyId());
            patrolInspectionPlanDataVo.setRestDay(patrolInspectionPlanSonList.get(i).getRestDay());
            patrolInspectionPlanDataVo.setWeekCount(patrolInspectionPlanSonList.get(i).getWeekCount());
            patrolInspectionPlanDataVo.setRouteId(patrolInspectionPlanSonList.get(i).getRouteId());
            if (planType == 1) {
                List<PlanScheduleVo> list2 = new ArrayList<>();
                for (int j = 0; j < list.size(); j++) {
                    if (patrolInspectionPlanSonList.get(i).getId().equals(list.get(j).getPlanId())) {
                        list.get(j).setWeekOne(patrolInspectionPlanSonList.get(i).getRestDay().contains("星期一") ? true : false);
                        list.get(j).setWeekTwo(patrolInspectionPlanSonList.get(i).getRestDay().contains("星期二") ? true : false);
                        list.get(j).setWeekThree(patrolInspectionPlanSonList.get(i).getRestDay().contains("星期三") ? true : false);
                        list.get(j).setWeekFour(patrolInspectionPlanSonList.get(i).getRestDay().contains("星期四") ? true : false);
                        list.get(j).setWeekFive(patrolInspectionPlanSonList.get(i).getRestDay().contains("星期五") ? true : false);
                        list.get(j).setWeekSix(patrolInspectionPlanSonList.get(i).getRestDay().contains("星期六") ? true : false);
                        list.get(j).setWeekSeven(patrolInspectionPlanSonList.get(i).getRestDay().contains("星期日") ? true : false);
                        list2.add(list.get(j));
                    }
                }
                patrolInspectionPlanDataVo.setChildrenList(list2);
            } else if (planType == 2) {
                for (int j = 0; j < list.size(); j++) {
                    if (patrolInspectionPlanSonList.get(i).getId().equals(list.get(j).getPlanId())) {
                        patrolInspectionPlanDataVo.setPersonneName(list.get(j).getPersonnelName());
                        patrolInspectionPlanDataVo.setPersonneId(list.get(j).getPersonnelId());
                    }
                }

            }
            list1.add(patrolInspectionPlanDataVo);
        }
//        List<PatrolInspectionPlanDataVo> list3 = list1.stream().skip((pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        return new CommonPage<>(list1, total, pageSize, pageNum);
    }


    @Override
    public List<PatrolInspectionSiteVo> patrolInspectionSiteVoList(Integer planId, Integer areaId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String userType = null;
        if (loginUser != null && !"".equals(loginUser)) {
            userType = loginUser.getUserType();
        }
        LambdaQueryWrapper<PatrolInspectionSite> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PatrolInspectionSite::getEnable, 1)
                .eq(PatrolInspectionSite::getAreaId, areaId)
                .eq(PatrolInspectionSite::getTenantId, SecurityUtils.getTenantId());
        if ("00".equals(userType)) {
            queryWrapper.eq(PatrolInspectionSite::getCreator, SecurityUtils.getUsername());
        }
        List<PatrolInspectionSite> patrolInspectionSiteList = patrolInspectionSiteService.list(queryWrapper);
        List<PatrolInspectionPlanSite> planSiteList = new ArrayList<>();
        if (planId != 0 && planId != null) {
            LambdaQueryWrapper<PatrolInspectionPlanSite> queryWrapperOne = Wrappers.lambdaQuery();
            queryWrapperOne.eq(PatrolInspectionPlanSite::getPlanId, planId);
            planSiteList = planSiteService.list(queryWrapperOne);
        }
        LambdaQueryWrapper<PatrolInspectionArea> queryWrapperTwo = Wrappers.lambdaQuery();
        queryWrapperTwo.eq(PatrolInspectionArea::getEnable, 1)
                .eq(PatrolInspectionArea::getTenantId, SecurityUtils.getTenantId());
        if ("00".equals(userType)) {
            queryWrapperTwo.eq(PatrolInspectionArea::getCreator, SecurityUtils.getUsername());
        }
        List<PatrolInspectionArea> PatrolInspectionAreaList = patrolInspectionAreaService.list(queryWrapperTwo);
        List<PatrolInspectionSiteVo> list = new ArrayList<>();
        for (int i = 0; i < patrolInspectionSiteList.size(); i++) {
            PatrolInspectionSiteVo patrolInspectionSiteVo = new PatrolInspectionSiteVo();
            patrolInspectionSiteVo.setSiteName(patrolInspectionSiteList.get(i).getSiteName());
            patrolInspectionSiteVo.setAreaId(patrolInspectionSiteList.get(i).getAreaId());
            for (int j = 0; j < PatrolInspectionAreaList.size(); j++) {
                if (patrolInspectionSiteList.get(i).getAreaId().equals(PatrolInspectionAreaList.get(j).getId())) {
                    patrolInspectionSiteVo.setAreaName(PatrolInspectionAreaList.get(j).getAreaName());
                }
            }
            patrolInspectionSiteVo.setId(patrolInspectionSiteList.get(i).getId());
            patrolInspectionSiteVo.setSiteStatus(false);
            for (int j = 0; j < planSiteList.size(); j++) {
                if (patrolInspectionSiteList.get(i).getId().equals(planSiteList.get(j).getSiteId())) {
                    patrolInspectionSiteVo.setSiteStatus(true);
                }
            }
            list.add(patrolInspectionSiteVo);
        }
        return list;
    }
}
