/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.watermeter.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.core.util.Date8Util;
import com.tofly.common.core.util.StringUtil;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.watermeter.custom.entity.meterRead.ReadSearch;
import com.tofly.watermeter.entity.Cbqydbm;
import com.tofly.watermeter.entity.Qfcbplan;
import com.tofly.watermeter.entity.Qfcbplandetails;
import com.tofly.watermeter.mapper.QfcbplanMapper;
import com.tofly.watermeter.service.CbqybmService;
import com.tofly.watermeter.service.CbqydbmService;
import com.tofly.watermeter.service.QfcbplanService;
import com.tofly.watermeter.service.QfcbplandetailsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 抄表计划表（年计划）
 *
 * @author wangjieliu
 * @date Fri Nov 05 00:00:00 CST 2021
 */
@Service("qfcbplanService")
public class QfcbplanServiceImpl extends ServiceImpl<QfcbplanMapper, Qfcbplan> implements QfcbplanService {

    @Autowired
    private QfcbplandetailsService qfcbplandetailsService;
    @Autowired
    private CbqydbmService cbqydbmService;
    @Autowired
    private CbqybmService cbqybmService;


    @Override
    public IPage<Qfcbplan> getYearPlan(Page<Qfcbplan> page, Qfcbplan qfcbplan) {
        if (StringUtil.isEmpty(qfcbplan.getIsGeneral())) {
            throw new ToflyDeniedException("请传递计划类型");
        }
        String year = qfcbplan.getYear();
        String startYear = qfcbplan.getStartYear();
        List<String> yearList = null;
        if (StringUtil.isNotEmpty(year)) {
            yearList = Arrays.asList(year.split(","));
        }
        return this.page(page, Wrappers.<Qfcbplan>lambdaQuery()
                .in(CollUtil.isNotEmpty(yearList), Qfcbplan::getYear, yearList)
                .ge(StringUtils.isNotBlank(startYear), Qfcbplan::getYear, startYear)
                .eq(Qfcbplan::getIsGeneral, qfcbplan.getIsGeneral()).orderByAsc(Qfcbplan::getYear));
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public long makeYearPlan(Qfcbplan qfcbplan) {
        String year = qfcbplan.getYear();
        String isState = qfcbplan.getIsState();
        String isGeneral = qfcbplan.getIsGeneral();
        if (StringUtil.isEmpty(year)) {
            throw new ToflyDeniedException("请选择年份");
        } else if (StringUtil.isEmpty(isState)) {
            throw new ToflyDeniedException("是否启用");
        } else if (StringUtil.isEmpty(isGeneral)) {
            throw new ToflyDeniedException("计划类型必传");
        }
        //如果创建计划一开始就是启用状态；需要将本年度中的计划全部禁用
        if ("1".equals(isState)) {
            int count = this.count(Wrappers.<Qfcbplan>lambdaUpdate().eq(Qfcbplan::getYear, year));
            if (count > 0) {
                this.update(Wrappers.<Qfcbplan>lambdaUpdate().eq(Qfcbplan::getYear, year).set(Qfcbplan::getIsState, 0));
            }
        }
        this.save(qfcbplan);
        return qfcbplan.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateYearPlan(String ids, String isState, String isGeneral) {
        if (StringUtil.isEmpty(isState)) {
            throw new ToflyDeniedException("是否启用/禁用");
        } else if (StringUtil.isEmpty(ids)) {
            throw new ToflyDeniedException("传递年计划ID");
        } else if (StringUtil.isEmpty(isGeneral)) {
            throw new ToflyDeniedException("请传递计划类型");
        }
        List<String> idList = Arrays.asList(ids.split(","));
        List<Qfcbplan> qfcbplanList = this.list(Wrappers.<Qfcbplan>lambdaQuery()
                .in(Qfcbplan::getId, idList).select(Qfcbplan::getYear, Qfcbplan::getId).eq(Qfcbplan::getIsGeneral, isGeneral));
        if (StringUtil.isNull(qfcbplanList)) {
            throw new ToflyDeniedException("请正确选择年计划");
        }
        //如果是启用年计划，那么不能出现同时启用同一年份的
        if ("1".equals(isState)) {
            Map<String, List<Qfcbplan>> groupYearMap = qfcbplanList.stream().collect(Collectors.groupingBy(Qfcbplan::getYear));
            groupYearMap.forEach((k, v) -> {
                if (v.size() > 1) {
                    throw new ToflyDeniedException("年计划：" + k + "，不能同时启用" + v.size() + "个");
                }
            });
        }
        for (Qfcbplan qfcbplan : qfcbplanList) {
            String year = qfcbplan.getYear();
            Long id = qfcbplan.getId();
            if ("1".equals(isState)) {
                //如果已经有启用的了，不能再次启用
                int count = this.count(Wrappers.<Qfcbplan>lambdaQuery().eq(Qfcbplan::getYear, year)
                        .eq(Qfcbplan::getIsState, "1").eq(Qfcbplan::getIsGeneral, isGeneral));
                if (count > 0) {
                    throw new ToflyDeniedException("已经有年计划：" + year + "启用了，不能重复启用");
                }
            }
            //如果启用状态；需要将本年度中的其他计划全部禁用  给提示
//            if ("1".equals(isState)) {
//                int count = this.count(Wrappers.<Qfcbplan>lambdaUpdate().eq(Qfcbplan::getYear, year).le(Qfcbplan::getId, id));
//                if (count > 0) {
//                    this.update(Wrappers.<Qfcbplan>lambdaUpdate().le(Qfcbplan::getId, id).eq(Qfcbplan::getYear, year).set(Qfcbplan::getIsState, 0));
//                }
//            }
            this.update(Wrappers.<Qfcbplan>lambdaUpdate().eq(Qfcbplan::getId, id).set(Qfcbplan::getYear, year).set(Qfcbplan::getIsState, isState));
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delYearPlan(String ids) {
        if (StringUtil.isEmpty(ids)) {
            throw new ToflyDeniedException("请选择需要删除的年计划");
        }
        List<String> idList = Arrays.asList(ids.split(","));
        //删除计划详情表中；有关联的所有计划
        qfcbplandetailsService.remove(Wrappers.<Qfcbplandetails>lambdaQuery().in(Qfcbplandetails::getPlanId, idList));
        //删除年计划
        this.remove(Wrappers.<Qfcbplan>lambdaQuery().in(Qfcbplan::getId, idList));
        return true;
    }

    @Override
    public List<Qfcbplandetails> getDeatilByPlan(ReadSearch search) {
        Long id = search.getId();
        String inputcontent = search.getInputContent();
        Long cbry = search.getCbry();
        String isgeneral = search.getIsGeneral();
//        if (StringUtil.isNull(id)) {
//            throw new ToflyDeniedException("请选择年计划");
//        } else if (StringUtil.isEmpty(isgeneral)) {
//            throw new ToflyDeniedException("设置计划类型");
//        }
        List<String> singleMonth = new ArrayList<>();
        singleMonth.add("01");
        singleMonth.add("03");
        singleMonth.add("05");
        singleMonth.add("07");
        singleMonth.add("09");
        singleMonth.add("11");
        String thisMonth = DateUtil.format(new Date(), "MM");
        String thisYear = DateUtil.format(new Date(), "yyyy");
        String thisDate = DateUtil.format(new Date(), "dd").replace("0", "");
        List<String> sinDouMonth = new ArrayList<>();
        sinDouMonth.add("2");
        String isSingle = "0";
        if (singleMonth.contains(thisMonth)) {
            isSingle = "1";
        }
        sinDouMonth.add(isSingle);
        List<Qfcbplandetails> qfcbplandetailsList;
        if (StringUtils.isNotBlank(search.getIsReadMeter()) && search.getIsReadMeter().equals("1")) {
            Qfcbplan qfcbplan = this.getById(id);
            if (!qfcbplan.getYear().equals(thisYear)) {
                return null;
            }
            qfcbplandetailsList = qfcbplandetailsService.list(Wrappers.<Qfcbplandetails>lambdaQuery()
                    .eq(id != null, Qfcbplandetails::getPlanId, id)
                    .in(Qfcbplandetails::getSinDouMonth, sinDouMonth)
                    .le(Qfcbplandetails::getStartDay, thisDate)
                    .eq(StringUtil.isNotNull(cbry), Qfcbplandetails::getCbry, cbry)
                    .orderByAsc(Qfcbplandetails::getCbqyMc, Qfcbplandetails::getQydmc)
                    .and(StringUtil.isNotEmpty(inputcontent), v -> v.like(Qfcbplandetails::getCbqyBm, inputcontent)
                            .or().like(Qfcbplandetails::getCbqyMc, inputcontent)
                            .or().like(Qfcbplandetails::getCbqydBm, inputcontent))
                    .eq(StringUtils.isNotBlank(isgeneral), Qfcbplandetails::getIsGeneral, isgeneral));
        } else if (StringUtils.isNotBlank(search.getIsReadMeter()) && search.getIsReadMeter().equals("0")) {
            Qfcbplan qfcbplan = this.getById(id);
            if (!qfcbplan.getYear().equals(thisYear)) {
                return null;
            }
            qfcbplandetailsList = qfcbplandetailsService.list(Wrappers.<Qfcbplandetails>lambdaQuery()
                    .eq(id != null, Qfcbplandetails::getPlanId, id)
                    .notIn(Qfcbplandetails::getSinDouMonth, sinDouMonth)
                    .gt(Qfcbplandetails::getStartDay, thisDate)
                    .eq(StringUtil.isNotNull(cbry), Qfcbplandetails::getCbry, cbry)
                    .orderByAsc(Qfcbplandetails::getCbqyMc, Qfcbplandetails::getQydmc)
                    .and(StringUtil.isNotEmpty(inputcontent), v -> v.like(Qfcbplandetails::getCbqyBm, inputcontent)
                            .or().like(Qfcbplandetails::getCbqyMc, inputcontent)
                            .or().like(Qfcbplandetails::getCbqydBm, inputcontent))
                    .eq(StringUtils.isNotBlank(isgeneral), Qfcbplandetails::getIsGeneral, isgeneral));
        } else {
            qfcbplandetailsList = qfcbplandetailsService.list(Wrappers.<Qfcbplandetails>lambdaQuery()
                    .eq(id != null, Qfcbplandetails::getPlanId, id)
                    .eq(StringUtil.isNotNull(cbry), Qfcbplandetails::getCbry, cbry)
                    .orderByAsc(Qfcbplandetails::getCbqyMc, Qfcbplandetails::getQydmc)
                    .and(StringUtil.isNotEmpty(inputcontent), v -> v.like(Qfcbplandetails::getCbqyBm, inputcontent)
                            .or().like(Qfcbplandetails::getCbqyMc, inputcontent)
                            .or().like(Qfcbplandetails::getCbqydBm, inputcontent))
                    .eq(StringUtils.isNotBlank(isgeneral), Qfcbplandetails::getIsGeneral, isgeneral));
        }
//        //对查询出来的数据进行分组
//        Map<String, List<Qfcbplandetails>> groupCbqyMap = qfcbplandetailsList.stream()
//                .collect(Collectors.groupingBy(v -> v.getCbqyBm() + "-" + v.getCbqyMc()));
//        List<Map<String, Object>> mapList = new ArrayList<>();
//        groupCbqyMap.forEach((qyK, qyV) -> {
//            Map<String, Object> qyMap = new HashMap<>();
//            qyMap.put("qybm", qyK);
//            qyMap.put("detailList", qyV);
//            mapList.add(qyMap);
//        });
        return qfcbplandetailsList;
    }

    @Override
    public IPage<Qfcbplandetails> getQyInfoByPlan(Page<Qfcbplandetails> page, Long planId, String cbqyBm, String isGeneral) {
        if (StringUtil.isNull(planId)) {
            throw new ToflyDeniedException("请选择年计划");
        } else if (StringUtil.isEmpty(isGeneral)) {
            throw new ToflyDeniedException("请传递计划类型");
        }
        //如果没有传递区域编码，则是查询区域编码数据
        if (StringUtil.isEmpty(cbqyBm)) {
            return qfcbplandetailsService.page(page, Wrappers.<Qfcbplandetails>lambdaQuery()
                    .eq(Qfcbplandetails::getPlanId, planId).eq(Qfcbplandetails::getIsGeneral, isGeneral)
                    .groupBy(Qfcbplandetails::getCbqyBm, Qfcbplandetails::getCbqyMc)
                    .select(Qfcbplandetails::getCbqyBm, Qfcbplandetails::getCbqyMc));
        } else if (StringUtil.isNotEmpty(cbqyBm)) {
            //如果传递了区域编码，只能查找区域编码下的区域段信息
            return qfcbplandetailsService.page(page, Wrappers.<Qfcbplandetails>lambdaQuery()
                    .eq(Qfcbplandetails::getPlanId, planId).eq(Qfcbplandetails::getCbqyBm, cbqyBm)
                    .eq(Qfcbplandetails::getIsGeneral, isGeneral)
                    .groupBy(Qfcbplandetails::getCbqydBm, Qfcbplandetails::getQydmc)
                    .select(Qfcbplandetails::getCbqydBm, Qfcbplandetails::getQydmc));
        } else {
            return null;
        }
    }

    @Override
    public boolean addDeatilPlan(Qfcbplandetails qfcbplandetails) {
        Long planId = qfcbplandetails.getPlanId();
        String qybm = qfcbplandetails.getCbqyBm();
        String qydbm = qfcbplandetails.getCbqydBm();
        String isState = qfcbplandetails.getIsState();
        String isGeneral = qfcbplandetails.getIsGeneral();
        Long depart = SecurityUtils.getUserDeptId();
        Long cbry = qfcbplandetails.getCbry();
        qfcbplandetails.setCbryDepart(depart);
        if (StringUtil.isNull(planId)) {
            throw new ToflyDeniedException("请选择年计划");
        } else if (StringUtil.isEmpty(qybm)) {
            throw new ToflyDeniedException("请选择区域");
        } else if (StringUtil.isEmpty(isGeneral)) {
            throw new ToflyDeniedException("请传递详情计划类型");
        }
        List<String> qybmList = Arrays.asList(qybm.split(","));
        //保存计划
        List<Qfcbplandetails> detailsList = new ArrayList<>();
        qybmList.forEach(k -> {
            //最终需要制定计划的区域段编码
            List<String> queryQydbmList = null;
            //判断有没有选择区域段
            if (StringUtil.isEmpty(qydbm)) {
                //相同区域下面只能有一个区域段计划(注意区分普查或人工计划类型)
                List<Qfcbplandetails> qfcbplandetailsList = qfcbplandetailsService.list(Wrappers.<Qfcbplandetails>lambdaQuery()
                        .eq(Qfcbplandetails::getPlanId, planId)
                        .eq(Qfcbplandetails::getCbqyBm, k).eq(Qfcbplandetails::getIsGeneral, isGeneral)
                        .select(Qfcbplandetails::getCbqyBm, Qfcbplandetails::getCbqydBm));
                List<String> qydbmList = null;
                if (StringUtil.isNotNull(qfcbplandetailsList) && qfcbplandetailsList.size() > 0) {
                    qydbmList = qfcbplandetailsList.stream().map(Qfcbplandetails::getCbqydBm).collect(Collectors.toList());
                }
                //如果没有传递区域段编码，则是要所有区域下的区域段；但是如果已经有制定计划了，则排除当前这个计划区域段
                List<Cbqydbm> cbqydbmList = cbqydbmService.list(Wrappers.<Cbqydbm>lambdaQuery().eq(Cbqydbm::getQybm, k)
                        .notIn(StringUtil.isNotNull(qydbmList), Cbqydbm::getQydbm, qydbmList).eq(Cbqydbm::getYxbz, "0")
                        .select(Cbqydbm::getQydbm, Cbqydbm::getQybm));
                if (StringUtil.isNotNull(cbqydbmList) && cbqydbmList.size() > 0) {
                    queryQydbmList = cbqydbmList.stream().map(Cbqydbm::getQydbm).collect(Collectors.toList());
                }
            } else {
                queryQydbmList = Arrays.asList(qydbm.split(","));
            }
            //如果区域段是一个的情况；需要判断是否有重复的
            if (StringUtil.isNotNull(queryQydbmList) && queryQydbmList.size() == 1) {
                //相同区域下面只能有一个区域段计划(注意区分普查或人工计划类型)  备注：现在只能有一个   卡表计划或机械表计划中的区段不能有重复的，所以取消这个判断 .eq(Qfcbplandetails::getIsGeneral, isGeneral)
                int count = qfcbplandetailsService.count(Wrappers.<Qfcbplandetails>lambdaQuery().eq(Qfcbplandetails::getPlanId, planId)
                        .eq(Qfcbplandetails::getCbqyBm, k)
                        .eq(Qfcbplandetails::getCbqydBm, queryQydbmList.get(0)));
                if (count > 0) {
                    Cbqydbm cbqydbm = cbqydbmService.getOne(Wrappers.<Cbqydbm>lambdaQuery().eq(Cbqydbm::getQydbm, queryQydbmList.get(0)).select(Cbqydbm::getQydmc));
                    throw new ToflyDeniedException("已有：" + cbqydbm.getQydmc() + "计划");
                }
            }
            if (StringUtil.isEmpty(isState)) {
                //如果没有设置启用状态，默认为启用
                qfcbplandetails.setIsState("1");
            }
            if (StringUtil.isNotNull(queryQydbmList) && queryQydbmList.size() > 0) {
                //查询区域段/区域名称
                List<Cbqydbm> cbqydbm = cbqydbmService.list(Wrappers.<Cbqydbm>lambdaQuery().in(Cbqydbm::getQydbm, queryQydbmList)
                        .select(Cbqydbm::getQydbm, Cbqydbm::getQydmc, Cbqydbm::getQybm));
                cbqydbm.forEach(v -> {
                    Qfcbplandetails qfcbplande = new Qfcbplandetails();
                    qfcbplande.setCbqyBm(k);
                    qfcbplande.setCbqyMc(v.getQymc());
                    qfcbplande.setQydmc(v.getQydmc());
                    qfcbplande.setCbqydBm(v.getQydbm());
                    qfcbplande.setIsGeneral(isGeneral);
                    qfcbplande.setIsState(qfcbplandetails.getIsState());
                    qfcbplande.setPlanId(planId);
                    qfcbplande.setCbry(cbry);
                    qfcbplande.setCbryDepart(depart);
                    detailsList.add(qfcbplande);
                });
            }
        });
        if (StringUtil.isNotNull(detailsList) && detailsList.size() > 0) {
            Long maxId = baseMapper.getMaxId();
            for (int i = 0; i < detailsList.size(); i++) {
                detailsList.get(i).setId(maxId + i);
            }
        }
        //没有抄表人员的情况
        if (StringUtil.isNull(cbry)) {
            //保存计划
            qfcbplandetailsService.savePlanBatch(detailsList);
        } else {
            qfcbplandetailsService.savePlanCbryBatch(detailsList);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultRespone copyYearPlan(Long planId, String year, String descript) {
        //查询被复制计划详情
        Qfcbplan qfcbplan = this.getById(planId);
        String isState = qfcbplan.getIsState();
        String copyYear = qfcbplan.getYear();
        if (copyYear.equals(year)) {
            isState = "0";
            qfcbplan.setIsState("0");
        }
        //如果创建计划一开始就是启用状态；需要将本年度中的计划全部禁用
        if ("1".equals(isState)) {
            int count = this.count(Wrappers.<Qfcbplan>lambdaUpdate().eq(Qfcbplan::getYear, year));
            if (count > 0) {
                this.update(Wrappers.<Qfcbplan>lambdaUpdate().eq(Qfcbplan::getYear, year).set(Qfcbplan::getIsState, 0));
            }
        }
        Date currentDate = Date8Util.getCurrentDate();
        Long createUser = SecurityUtils.getUserId();
        qfcbplan.setYear(year);
        qfcbplan.setId(null);
        qfcbplan.setCreateTime(currentDate);
        qfcbplan.setCreateUser(createUser);
        qfcbplan.setCreateUserName(null);
        if (StringUtil.isNotEmpty(descript)) {
            qfcbplan.setCopyDescript(descript);
        }
        qfcbplan.setIsCopy("1");
        boolean flag = this.save(qfcbplan);
        if (flag) {
            //查询年计划下的子计划
            List<Qfcbplandetails> qfcbplandetailsList = qfcbplandetailsService.list(Wrappers.<Qfcbplandetails>lambdaQuery().eq(Qfcbplandetails::getPlanId, planId));
            if (StringUtil.isNotNull(qfcbplandetailsList) && qfcbplandetailsList.size() > 0) {
                Long maxId = baseMapper.getMaxId();
                //处理复制数据
                for (Qfcbplandetails qfcbplandetails : qfcbplandetailsList) {
                    qfcbplandetails.setPlanId(qfcbplan.getId());
                    qfcbplandetails.setId(maxId++);
                    qfcbplandetails.setCreateTime(currentDate);
                    qfcbplandetails.setCreateUser(createUser);
                }
                //添加详情计划数据
                qfcbplandetailsService.savePlanCbryBatch(qfcbplandetailsList);
                return ResultRespone.success(true, "计划复制成功");
            } else {
                return ResultRespone.success(true, "计划复制成功");
            }
        } else {
            return ResultRespone.failed(false, "计划复制失败");
        }
    }


    @Override
    public boolean updateDeatilPlan(Qfcbplandetails qfcbplandetails) {
        Long planId = qfcbplandetails.getPlanId();
        String qybm = qfcbplandetails.getCbqyBm();
        String qydbm = qfcbplandetails.getCbqydBm();
        Long cbry = qfcbplandetails.getCbry();
        Short startDay = qfcbplandetails.getStartDay();
        Short endDay = qfcbplandetails.getEndDay();
        String isState = qfcbplandetails.getIsState();
        String sinDouMonth = qfcbplandetails.getSinDouMonth();
        String isUpdateType = qfcbplandetails.getIsUpdateType();
        Long cbryDepart = qfcbplandetails.getCbryDepart();
        if (StringUtil.isEmpty(isUpdateType)) {
            throw new ToflyDeniedException("请传递设置类型");
        } else if (StringUtil.isNull(planId)) {
            throw new ToflyDeniedException("请选择年计划");
        } else if (StringUtil.isEmpty(qybm)) {
            throw new ToflyDeniedException("请选择区域");
        }
        //区域集合
        List<String> qybmList = Arrays.asList(qybm.split(","));
        //区域段集合
        List<String> qydbmList = null;

        if (StringUtil.isNotEmpty(qydbm)) {
            qydbmList = Arrays.asList(qydbm.split(","));
        } else {
            //多个区域编码的情况，查询编码下面所有的区域段
            List<Cbqydbm> cbqydbmList = cbqydbmService.list(Wrappers.<Cbqydbm>lambdaQuery()
                    .in(Cbqydbm::getQybm, qybmList).select(Cbqydbm::getQydbm));
            qydbmList = cbqydbmList.stream().map(Cbqydbm::getQydbm).collect(Collectors.toList());
        }

        //抄表人员设置
        if ("0".equals(isUpdateType)) {
            if (StringUtil.isNull(cbry)) {
                throw new ToflyDeniedException("请选择抄表人员");
            } else if (StringUtil.isNull(cbryDepart)) {
                throw new ToflyDeniedException("请传递抄表人员部门ID");
            }
        }
        //抄表时间设置
        else if ("1".equals(isUpdateType)) {
            if (StringUtil.isNull(startDay) || StringUtil.isNull(endDay)) {
                throw new ToflyDeniedException("请选择抄表开始日和结束日");
            } else if (StringUtil.isEmpty(sinDouMonth)) {
                throw new ToflyDeniedException("请选择抄表月");
            }
            //这里开始日、结束日范围都在1--27之间
            if (startDay > endDay) {
                throw new ToflyDeniedException("请正确选择开始日和结束日，开始日不能大于结束日");
            }
        }
        //启用或禁用
        else if ("2".equals(isUpdateType)) {
            if (StringUtil.isEmpty(isState)) {
                throw new ToflyDeniedException("请正确选择启用或禁用状态");
            }
        } else {
            throw new ToflyDeniedException("修改计划时请正确操作");
        }

        if (qydbmList.size() > 0) {
            qfcbplandetailsService.update(Wrappers.<Qfcbplandetails>lambdaUpdate()
                    .eq(Qfcbplandetails::getPlanId, planId)
                    .in(Qfcbplandetails::getCbqydBm, qydbmList)
                    .set(StringUtil.isNotNull(cbryDepart), Qfcbplandetails::getCbryDepart, cbryDepart)
                    .set(StringUtil.isNotNull(cbry), Qfcbplandetails::getCbry, cbry)
                    .set(StringUtil.isNotNull(startDay), Qfcbplandetails::getStartDay, startDay)
                    .set(StringUtil.isNotEmpty(sinDouMonth), Qfcbplandetails::getSinDouMonth, sinDouMonth)
                    .set(StringUtil.isNotNull(endDay), Qfcbplandetails::getEndDay, endDay)
                    .set(StringUtil.isNotEmpty(isState), Qfcbplandetails::getIsState, isState));
        } else {
            throw new ToflyDeniedException("所选区域下面暂无抄表段计划");
        }
        return true;
    }

    @Override
    public boolean deleteDeatilsPlan(String ids) {
        if (StringUtil.isEmpty(ids)) {
            throw new ToflyDeniedException("请选择移除的计划");
        }
        List<String> idList = Arrays.asList(ids.split(","));
        qfcbplandetailsService.remove(Wrappers.<Qfcbplandetails>lambdaQuery().in(Qfcbplandetails::getId, idList));
        return true;
    }

    @Override
    public List<Map> getCbry(Long departId) {
        if (StringUtil.isNull(departId)) {
            departId = SecurityUtils.getUserDeptId();
        }
        List<Map> mapList = baseMapper.getCbry(departId);
        return mapList;
    }


}
