package com.ruoyi.zdsz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.annotation.DynamicName;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.redis.RedisUtils;
import com.ruoyi.framework.handler.MonthTableNameHandler;
import com.ruoyi.system.service.ISysDictTypeService;
import com.ruoyi.zdsz.domain.*;
import com.ruoyi.zdsz.domain.bo.*;
import com.ruoyi.zdsz.domain.vo.*;
import com.ruoyi.zdsz.mapper.*;
import com.ruoyi.zdsz.service.*;
import com.ruoyi.zdsz.task.RiewTask;
import com.ruoyi.zdsz.task.photoTask;
import lombok.RequiredArgsConstructor;
import org.hibernate.validator.internal.util.stereotypes.Lazy;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.*;
import java.util.logging.Level;
import java.util.stream.Collectors;

/**
 * 民用工程Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-27
 */
@RequiredArgsConstructor
@Service
public class ZEngineeringCivilServiceImpl implements IZEngineeringCivilService {

    private final ZEngineeringCivilMapper baseMapper;
    @Resource
    private ZAreaMapper zAreaMapper;
    @Resource
    private ZHouseMapper zHouseMapper;

    @Autowired
    private IZEngineeringNodeService izEngineeringNodeService;
    @Resource
    private IZMaterialStatisticsService izMaterialStatisticsService;
    @Resource
    private ZMaterialStatisticsMapper zMaterialStatisticsMapper;
    @Resource
    private ZEngineeringInfoMapper zEngineeringInfoMapper;
    @Resource
    private ZEngineeringNodeMapper zEngineeringNodeMapper;
    @Resource
    private IZEngineeringReviewService izEngineeringReviewService;
    @Resource
    private IZEngiineeringPhotoService izEngiineeringPhotoService;
    @Resource
    private ZEnginNotInstalledMapper zEnginNotInstalledMapper;
    @Resource
    private IZEngineeringInfoService izEngineeringInfoService;

    @Override
    public Map<String, List<?>> selectByDistrictStatistics(String district) {
        List<String> name = new ArrayList<>();
        List<Integer> wsg = new ArrayList<>();
        List<Integer> ysg = new ArrayList<>();
        List<Integer> jg = new ArrayList<>();
        for (Map<String, Integer> item : baseMapper.selectByDistrictStatistics(district)) {
            name.add(String.valueOf(item.get("name")));
            wsg.add(item.get("weishigong"));
            ysg.add(item.get("shigongzhong"));
            jg.add(item.get("jungong"));
        }
        Map<String, List<?>> listMap = new HashMap<>();
        listMap.put("name", name);
        listMap.put("wsh", wsg);
        listMap.put("ysg", ysg);
        listMap.put("jg", jg);
        return listMap;
    }

    /**
     * 查询民用工程
     */
    @Override
    public ZEngineeringCivilVo queryById(String id) {
        ZEngineeringCivilVo zEngineeringCivilVo = baseMapper.selectVoById(id);
        ZEngineeringNodeBo zEngineeringNodeBo = new ZEngineeringNodeBo();
        zEngineeringNodeBo.setCreateTime(zEngineeringCivilVo.getCreateTime());
        zEngineeringNodeBo.setCivliId(zEngineeringCivilVo.getId());
        zEngineeringCivilVo.setzEngineeringNodeBoList(izEngineeringNodeService.queryListDetails(zEngineeringNodeBo));
        return zEngineeringCivilVo;
    }

    /**
     * 查询民用工程
     */
    @Override
    public ZEngineeringCivilVo query(String id, String type) {
        ZEngineeringCivilVo zEngineeringCivilVo = baseMapper.selectVoById(id);
        ZEngineeringNodeBo zEngineeringNodeBo = new ZEngineeringNodeBo();
        zEngineeringNodeBo.setCreateTime(zEngineeringCivilVo.getCreateTime());
        zEngineeringNodeBo.setCivliId(zEngineeringCivilVo.getId());
        zEngineeringNodeBo.setType(type);
        zEngineeringCivilVo.setzEngineeringNodeBo(izEngineeringNodeService.query(zEngineeringNodeBo));
        return zEngineeringCivilVo;
    }

    @Override
    public ZEngineeringCivilVo queryProcess(Map<String, String> params) {
        LambdaQueryWrapper<ZEngineeringCivil> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(params.get("houseId")), ZEngineeringCivil::getHouseId, params.get("houseId"));
        lqw.eq(StringUtils.isNotBlank(params.get("enginClassification")), ZEngineeringCivil::getEnginClassification, params.get("enginClassification"));
        lqw.eq(StringUtils.isNotBlank(params.get("enginType")), ZEngineeringCivil::getEnginType, params.get("enginType"));
        ZEngineeringCivilVo zEngineeringCivilVo = baseMapper.selectVoOne(lqw);
        ;
        if (!ObjectUtils.isEmpty(zEngineeringCivilVo)) {
            ZEngineeringNodeBo zEngineeringNodeBo = new ZEngineeringNodeBo();
            zEngineeringNodeBo.setCreateTime(zEngineeringCivilVo.getCreateTime());
            zEngineeringNodeBo.setCivliId(zEngineeringCivilVo.getId());
            zEngineeringNodeBo.setType(params.get("type"));
            zEngineeringCivilVo.setzEngineeringNodeBo(izEngineeringNodeService.query(zEngineeringNodeBo));
            return zEngineeringCivilVo;
        }
        return zEngineeringCivilVo;
    }

    @Override
    public ZEngineeringCivilVo queryByEntityBo(ZEngineeringCivilBo zEngineeringCivilBo) {
        LambdaQueryWrapper<ZEngineeringCivil> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(zEngineeringCivilBo.getAreaId()), ZEngineeringCivil::getAreaId, zEngineeringCivilBo.getAreaId());
        if (StringUtils.isEmpty(zEngineeringCivilBo.getBuildingId())) {
            lqw.isNull(ZEngineeringCivil::getBuildingId);
        } else {
            lqw.eq(ZEngineeringCivil::getBuildingId, zEngineeringCivilBo.getBuildingId());
        }
        if (StringUtils.isEmpty(zEngineeringCivilBo.getUnitId())) {
            lqw.isNull(ZEngineeringCivil::getUnitId);
        } else {
            lqw.eq(ZEngineeringCivil::getUnitId, zEngineeringCivilBo.getUnitId());
        }
        if (StringUtils.isEmpty(zEngineeringCivilBo.getHouseId())) {
            lqw.isNull(ZEngineeringCivil::getHouseId);
        } else {
            lqw.eq(ZEngineeringCivil::getHouseId, zEngineeringCivilBo.getHouseId());
        }
        lqw.eq(StringUtils.isNotBlank(zEngineeringCivilBo.getEnginCycle()), ZEngineeringCivil::getEnginCycle, zEngineeringCivilBo.getEnginCycle());
        lqw.eq(StringUtils.isNotBlank(zEngineeringCivilBo.getEnginType()), ZEngineeringCivil::getEnginType, zEngineeringCivilBo.getEnginType());
        lqw.eq(StringUtils.isNotBlank(zEngineeringCivilBo.getEnginClassification()), ZEngineeringCivil::getEnginClassification, zEngineeringCivilBo.getEnginClassification());
        ZEngineeringCivilVo selected = baseMapper.selectVoOne(lqw);
        if (!ObjectUtils.isEmpty(selected)) {
            selected.setzEngineeringNodeBoList(izEngineeringNodeService.queryNodeList(BeanUtil.copyProperties(selected, ZEngineeringCivil.class)));
        }
        return selected;
    }

    /**
     * 查询民用工程列表
     */
    @Override
    public TableDataInfo<ZEngineeringCivilVo> queryPageList(ZEngineeringCivilBo bo, PageQuery pageQuery) {
        Page<ZBuildingBo> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        LambdaQueryWrapper<ZEngineeringCivil> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getEnginType()), ZEngineeringCivil::getEnginType, bo.getEnginType());
        lqw.eq(StringUtils.isNotBlank(bo.getEnginClassification()), ZEngineeringCivil::getEnginClassification, bo.getEnginClassification());
        lqw.eq(StringUtils.isNotBlank(bo.getEnginCycle()), ZEngineeringCivil::getEnginCycle, bo.getEnginCycle());
        lqw.eq(StringUtils.isNotBlank(bo.getAreaId()), ZEngineeringCivil::getAreaId, bo.getAreaId());
        lqw.eq(StringUtils.isNotBlank(bo.getBuildingId()), ZEngineeringCivil::getBuildingId, bo.getBuildingId());
        lqw.eq(StringUtils.isNotBlank(bo.getUnitId()), ZEngineeringCivil::getUnitId, bo.getUnitId());
        lqw.eq(StringUtils.isNotBlank(bo.getDistrict()), ZEngineeringCivil::getDistrict, bo.getDistrict());
        lqw.eq(StringUtils.isNotBlank(bo.getHouseId()), ZEngineeringCivil::getHouseId, bo.getHouseId());
        Page<ZEngineeringCivilVo> result = new Page<>();
        if (!"".equals(bo.getType()) && !ObjectUtils.isEmpty(bo.getType())) {
            List IDList = zEngineeringNodeService.nodeTypeList(baseMapper.selectList(lqw), bo.getType(), bo.getCompletionStatus(), bo);

            if (IDList.size() == 0) {
                return TableDataInfo.build(result);
            }
            LinkedList<String> list = baseMapper.getTableNames("z_engineering_info");
            List DateList = new ArrayList();
            list.removeLast();
            list.stream().forEach(item -> {

                DateList.add(item.substring(item.indexOf("2")));
            });
            result = baseMapper.queryPageIdList(IDList, baseMapper.getTypeNumber(DateList, bo), page, pageQuery.getPageNum() - 1, pageQuery.getPageSize());
            Map<String, Object> ReviewList = zEngineeringNodeService.ReViewList(result.getRecords());
            result.getRecords().forEach(item -> {
                item.setNodeReViewStateList(ReviewList.get(item.getId()));
            });
            page.setTotal(result.getTotal());
            return TableDataInfo.build(result);
        }
        if ((!"".equals(bo.getBeginTime()) && !ObjectUtils.isEmpty(bo.getBeginTime())) || (!"".equals(bo.getCreateBy()) && !ObjectUtils.isEmpty(bo.getCreateBy()))) {
            List<String> a = zEngineeringCivilList(bo.getBeginTime(), bo.getCreateBy(), bo);
            if (a.size() == 0) {
                return TableDataInfo.build(result);
            }
            result = baseMapper.queryPageIdList(a, Long.parseLong(String.valueOf(a.size())), page, pageQuery.getPageNum() - 1, pageQuery.getPageSize());
            Map<String, Object> ReviewList = zEngineeringNodeService.ReViewList(result.getRecords());
            result.getRecords().forEach(item -> {
                item.setNodeReViewStateList(ReviewList.get(item.getId()));
            });
            page.setTotal(result.getTotal());
            return TableDataInfo.build(result);
        }
        result = baseMapper.queryPageList(bo, page, (pageQuery.getPageNum() - 1) * pageQuery.getPageSize(), pageQuery.getPageSize());
        Map<String, Object> ReviewList = zEngineeringNodeService.ReViewList(result.getRecords());
        result.getRecords().forEach(item -> {
            item.setNodeReViewStateList(ReviewList.get(item.getId()));
        });
        page.setTotal(!ObjectUtils.isEmpty(result.getRecords()) ? result.getRecords().get(0).getTotal() : 0);
        return TableDataInfo.build(result);
    }

    /**
     * 查询民用工程列表
     */
    @Override
    public List<ZEngineeringCivilVo> queryAllList(ZEngineeringCivilBo bo) {
        LambdaQueryWrapper<ZEngineeringCivil> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getEnginType()), ZEngineeringCivil::getEnginType, bo.getEnginType());
        lqw.eq(StringUtils.isNotBlank(bo.getEnginClassification()), ZEngineeringCivil::getEnginClassification, bo.getEnginClassification());
        lqw.eq(StringUtils.isNotBlank(bo.getEnginCycle()), ZEngineeringCivil::getEnginCycle, bo.getEnginCycle());
        lqw.eq(StringUtils.isNotBlank(bo.getAreaId()), ZEngineeringCivil::getAreaId, bo.getAreaId());
        lqw.eq(StringUtils.isNotBlank(bo.getBuildingId()), ZEngineeringCivil::getBuildingId, bo.getBuildingId());
        lqw.eq(StringUtils.isNotBlank(bo.getUnitId()), ZEngineeringCivil::getUnitId, bo.getUnitId());
        lqw.eq(StringUtils.isNotBlank(bo.getDistrict()), ZEngineeringCivil::getDistrict, bo.getDistrict());
        lqw.eq(StringUtils.isNotBlank(bo.getHouseId()), ZEngineeringCivil::getHouseId, bo.getHouseId());
        List<ZEngineeringCivilVo> result = new ArrayList<>();
        if (!"".equals(bo.getType()) && !ObjectUtils.isEmpty(bo.getType())) {
            List IDList = zEngineeringNodeService.nodeTypeList(baseMapper.selectList(lqw), bo.getType(), bo.getCompletionStatus(), bo);

            if (IDList.size() == 0) {
                return result;
            }
            LinkedList<String> list = baseMapper.getTableNames("z_engineering_info");
            List DateList = new ArrayList();
            list.removeLast();
            list.stream().forEach(item -> {

                DateList.add(item.substring(item.indexOf("2")));
            });
            result = baseMapper.queryPageIdList(IDList, baseMapper.getTypeNumber(DateList, bo), null, null, null).getRecords();
            Map<String, Object> ReviewList = zEngineeringNodeService.ReViewList(result);
            result.forEach(item -> {
                item.setNodeReViewStateList(ReviewList.get(item.getId()));
            });
            return result;
        }
        if ((!"".equals(bo.getBeginTime()) && !ObjectUtils.isEmpty(bo.getBeginTime())) || (!"".equals(bo.getCreateBy()) && !ObjectUtils.isEmpty(bo.getCreateBy()))) {
            List<String> a = zEngineeringCivilList(bo.getBeginTime(), bo.getCreateBy(), bo);
            if (a.size() == 0) {
                return result;
            }
            result = baseMapper.queryPageIdList(a, Long.parseLong(String.valueOf(a.size())), null,null,null).getRecords();
            Map<String, Object> ReviewList = zEngineeringNodeService.ReViewList(result);
            result.forEach(item -> {
                item.setNodeReViewStateList(ReviewList.get(item.getId()));
            });
            return result;
        }
        result = baseMapper.queryAllList(bo);
        Map<String, Object> ReviewList = zEngineeringNodeService.ReViewList(result);
        result.forEach(item -> {
            item.setNodeReViewStateList(ReviewList.get(item.getId()));
            item.setGx1(((List<Map<String,String>>) item.getNodeReViewStateList()).get(0).get("state"));
            item.setGx2(((List<Map<String,String>>) item.getNodeReViewStateList()).get(1).get("state"));
            item.setGx3(((List<Map<String,String>>) item.getNodeReViewStateList()).get(2).get("state"));
            item.setGx4(((List<Map<String,String>>) item.getNodeReViewStateList()).get(3).get("state"));
            item.setGx5(((List<Map<String,String>>) item.getNodeReViewStateList()).get(4).get("state"));
            item.setGx6(((List<Map<String,String>>) item.getNodeReViewStateList()).get(5).get("state"));
            item.setGx7(((List<Map<String,String>>) item.getNodeReViewStateList()).get(6).get("state"));
        });

        return result;
    }

    @Override
    public List<ZEngineeringCivilExportVos> exportList(ZEngineeringCivilBo bo) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy_MM");
        LinkedList<String> list = baseMapper.getTableNames("z_engineering_info");
        list.removeLast();
        List DateList = new ArrayList();
        list.stream().forEach(item -> {
            DateList.add(item.substring(item.indexOf("2")));
        });
        List<ZEngineeringCivilExportVo> zEngineeringCivilExportVoList = baseMapper.getexportList(DateList, format.format(new Date()), bo);

        LinkedHashMap<String, List<ZEngineeringCivilExportVo>> hashMap = new LinkedHashMap<>();

        for (int i = 0; i < zEngineeringCivilExportVoList.size(); i++) {
            if (hashMap.containsKey(zEngineeringCivilExportVoList.get(i).getId())) {
                List<ZEngineeringCivilExportVo> zEngineeringCivilExportVos = hashMap.get(zEngineeringCivilExportVoList.get(i).getId());
                zEngineeringCivilExportVos.add(zEngineeringCivilExportVoList.get(i));
            } else {
                List<ZEngineeringCivilExportVo> zEngineeringCivilExportVos = new ArrayList<>();
                zEngineeringCivilExportVos.add(zEngineeringCivilExportVoList.get(i));
                hashMap.put(zEngineeringCivilExportVoList.get(i).getId(), zEngineeringCivilExportVos);
            }
        }
        ArrayList<ZEngineeringCivilExportVos> arrayList = new ArrayList<>();
        List<String> strings = new ArrayList<>();
        for (Map.Entry<String, List<ZEngineeringCivilExportVo>> stringListEntry : hashMap.entrySet()) {
            strings.add(stringListEntry.getKey());
        }
        for (String string : strings) {
            ZEngineeringCivilExportVos zEngineeringCivilExportVos = new ZEngineeringCivilExportVos();
            for (ZEngineeringCivilExportVo zEngineeringCivilExportVo : hashMap.get(string)) {
                zEngineeringCivilExportVos.setDistrict(zEngineeringCivilExportVo.getDistrict());
                zEngineeringCivilExportVos.setAreaId(zEngineeringCivilExportVo.getAreaId());
                zEngineeringCivilExportVos.setBuildingId(zEngineeringCivilExportVo.getBuildingId());
                zEngineeringCivilExportVos.setUnitId(zEngineeringCivilExportVo.getUnitId());
                zEngineeringCivilExportVos.setHouseId(zEngineeringCivilExportVo.getHouseId());
                zEngineeringCivilExportVos.setSource(zEngineeringCivilExportVo.getSource());
                zEngineeringCivilExportVos.setEnginType(zEngineeringCivilExportVo.getEnginType());
                zEngineeringCivilExportVos.setEnginClassification(zEngineeringCivilExportVo.getEnginClassification());
                zEngineeringCivilExportVos.setCompletionStatus(zEngineeringCivilExportVo.getCompletionStatus());
                if (ObjectUtils.isEmpty(zEngineeringCivilExportVos.getCorrosionLevel())) {
                    zEngineeringCivilExportVos.setCorrosionLevel("");
                }
                if ("".equals(zEngineeringCivilExportVos.getCorrosionLevel()) && "拆旧管".equals(zEngineeringCivilExportVo.getType()) && zEngineeringCivilExportVo.getCorrosionLevel() != null && !zEngineeringCivilExportVo.getCorrosionLevel().matches("-?\\d+(\\.\\d+)?"))
                    zEngineeringCivilExportVos.setCorrosionLevel(zEngineeringCivilExportVo.getCorrosionLevel());
                else if ("拆旧管".equals(zEngineeringCivilExportVo.getType()) && zEngineeringCivilExportVo.getCorrosionLevel() != null && !zEngineeringCivilExportVo.getCorrosionLevel().matches("-?\\d+(\\.\\d+)?"))
                    zEngineeringCivilExportVos.setCorrosionLevel(zEngineeringCivilExportVos.getCorrosionLevel() + "/" + zEngineeringCivilExportVo.getCorrosionLevel());
                if ("".equals(zEngineeringCivilExportVos.getCorrosionLevel()) && "打孔".equals(zEngineeringCivilExportVo.getType()) && zEngineeringCivilExportVo.getCorrosionLevel() != null && !zEngineeringCivilExportVo.getCorrosionLevel().matches("-?\\d+(\\.\\d+)?"))
                    zEngineeringCivilExportVos.setCorrosionLevel(zEngineeringCivilExportVo.getCorrosionLevel());
                else if ("打孔".equals(zEngineeringCivilExportVo.getType()) && zEngineeringCivilExportVo.getCorrosionLevel() != null && !zEngineeringCivilExportVo.getCorrosionLevel().matches("-?\\d+(\\.\\d+)?"))
                    zEngineeringCivilExportVos.setCorrosionLevel(zEngineeringCivilExportVos.getCorrosionLevel() + "/" + zEngineeringCivilExportVo.getCorrosionLevel());
                if ("立杠".equals(zEngineeringCivilExportVo.getType())) {
                    zEngineeringCivilExportVos.setLGmaterialQuality(zEngineeringCivilExportVo.getMaterialQuality());
                    zEngineeringCivilExportVos.setLGspecifications(zEngineeringCivilExportVo.getSpecifications());
                    zEngineeringCivilExportVos.setLGnumber(zEngineeringCivilExportVo.getNumber().toString());
                }
                if ("挂表".equals(zEngineeringCivilExportVo.getType()))
                    zEngineeringCivilExportVos.setBrand(zEngineeringCivilExportVo.getBrand());
                if ("表后管".equals(zEngineeringCivilExportVo.getType())) {
                    zEngineeringCivilExportVos.setBHGmaterialQuality(zEngineeringCivilExportVo.getMaterialQuality());
                    zEngineeringCivilExportVos.setBHGspecifications(zEngineeringCivilExportVo.getSpecifications());
                    zEngineeringCivilExportVos.setBHGnumber(zEngineeringCivilExportVo.getNumber().toString());
                }
                if (ObjectUtils.isEmpty(zEngineeringCivilExportVos.getBJQmaterialQuality())) {
                    zEngineeringCivilExportVos.setBJQmaterialQuality("");
                }
                if ("".equals(zEngineeringCivilExportVos.getBJQmaterialQuality()) && "报警器".equals(zEngineeringCivilExportVo.getType()))
                    zEngineeringCivilExportVos.setBJQmaterialQuality(zEngineeringCivilExportVo.getBrand());
                else if ("报警器".equals(zEngineeringCivilExportVo.getType()))
                    zEngineeringCivilExportVos.setBJQmaterialQuality(zEngineeringCivilExportVos.getBJQmaterialQuality() + "/" + zEngineeringCivilExportVo.getBrand());
                if (ObjectUtils.isEmpty(zEngineeringCivilExportVos.getQDFmaterialQuality())) {
                    zEngineeringCivilExportVos.setQDFmaterialQuality("");
                }
                if ("".equals(zEngineeringCivilExportVos.getQDFmaterialQuality()) && "切断阀".equals(zEngineeringCivilExportVo.getType()))
                    zEngineeringCivilExportVos.setQDFmaterialQuality(zEngineeringCivilExportVo.getBrand());
                else if ("切断阀".equals(zEngineeringCivilExportVo.getType()))
                    zEngineeringCivilExportVos.setQDFmaterialQuality(zEngineeringCivilExportVos.getQDFmaterialQuality() + "/" + zEngineeringCivilExportVo.getBrand());
                if ("阀管".equals(zEngineeringCivilExportVo.getType())) {
                    zEngineeringCivilExportVos.setVisitType(zEngineeringCivilExportVo.getVisitType());
                    zEngineeringCivilExportVos.setFGmaterialQuality(zEngineeringCivilExportVo.getMaterialQuality());
                    zEngineeringCivilExportVos.setFGspecifications(zEngineeringCivilExportVo.getSpecifications());
                    if (!ObjectUtils.isEmpty(zEngineeringCivilExportVo.getNumber()))
                        zEngineeringCivilExportVos.setFGnumber(zEngineeringCivilExportVo.getNumber().toString());
                }
            }
            arrayList.add(zEngineeringCivilExportVos);
        }


        return arrayList;
    }

    @Override
    public List<ZEngineeringCivilExportVos> exportBottomLegList(ZEngineeringCivilBo bo) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy_MM");
        LinkedList<String> list = baseMapper.getTableNames("z_engineering_info");
        list.removeLast();
        List DateList = new ArrayList();
        list.stream().forEach(item -> {
            DateList.add(item.substring(item.indexOf("2")));
        });
        List<ZEngineeringCivilExportVo> zEngineeringCivilExportVoList = baseMapper.getexportList(DateList, format.format(new Date()), bo);

        HashMap<String, List<ZEngineeringCivilExportVo>> hashMap = new HashMap<>();

        for (int i = 0; i < zEngineeringCivilExportVoList.size(); i++) {
            if (hashMap.containsKey(zEngineeringCivilExportVoList.get(i).getId())) {
                List<ZEngineeringCivilExportVo> zEngineeringCivilExportVos = hashMap.get(zEngineeringCivilExportVoList.get(i).getId());
                zEngineeringCivilExportVos.add(zEngineeringCivilExportVoList.get(i));
            } else {
                List<ZEngineeringCivilExportVo> zEngineeringCivilExportVos = new ArrayList<>();
                zEngineeringCivilExportVos.add(zEngineeringCivilExportVoList.get(i));
                hashMap.put(zEngineeringCivilExportVoList.get(i).getId(), zEngineeringCivilExportVos);
            }
        }
        ArrayList<ZEngineeringCivilExportVos> arrayList = new ArrayList<>();
        List<String> strings = new ArrayList<>();
        for (Map.Entry<String, List<ZEngineeringCivilExportVo>> stringListEntry : hashMap.entrySet()) {
            strings.add(stringListEntry.getKey());
        }
        for (String string : strings) {
            ZEngineeringCivilExportVos zEngineeringCivilExportVos = new ZEngineeringCivilExportVos();
            for (ZEngineeringCivilExportVo zEngineeringCivilExportVo : hashMap.get(string)) {
                zEngineeringCivilExportVos.setDistrict(zEngineeringCivilExportVo.getDistrict());
                zEngineeringCivilExportVos.setAreaId(zEngineeringCivilExportVo.getAreaId());
                zEngineeringCivilExportVos.setBuildingId(zEngineeringCivilExportVo.getBuildingId());
                zEngineeringCivilExportVos.setUnitId(zEngineeringCivilExportVo.getUnitId());
                zEngineeringCivilExportVos.setHouseId(zEngineeringCivilExportVo.getHouseId());
                zEngineeringCivilExportVos.setSource(zEngineeringCivilExportVo.getSource());
                zEngineeringCivilExportVos.setEnginType(zEngineeringCivilExportVo.getEnginType());
                zEngineeringCivilExportVos.setEnginClassification(zEngineeringCivilExportVo.getEnginClassification());
                zEngineeringCivilExportVos.setCompletionStatus(zEngineeringCivilExportVo.getCompletionStatus());
                //                remarkd
                if (ObjectUtils.isEmpty(zEngineeringCivilExportVos.getJtwz())) {
                    zEngineeringCivilExportVos.setJtwz("");
                }
                if (ObjectUtils.isEmpty(zEngineeringCivilExportVos.getSgwz())) {
                    zEngineeringCivilExportVos.setSgwz("");
                }
                if ("".equals(zEngineeringCivilExportVos.getJtwz())) {
                    zEngineeringCivilExportVos.setJtwz(zEngineeringCivilExportVo.getRemarkd());
                } else {
                    zEngineeringCivilExportVos.setJtwz(zEngineeringCivilExportVos.getJtwz() + "/" + zEngineeringCivilExportVo.getRemarkd());
                }
                if ("".equals(zEngineeringCivilExportVos.getSgwz())) {
                    zEngineeringCivilExportVos.setSgwz(zEngineeringCivilExportVo.getConstructAddre());
                } else {
                    zEngineeringCivilExportVos.setSgwz(zEngineeringCivilExportVos.getSgwz() + "/" + zEngineeringCivilExportVo.getConstructAddre());
                }
            }
            arrayList.add(zEngineeringCivilExportVos);
        }


        return arrayList;
    }

    @Override
    public List<ZEngineeringCivilExportVos> exportOverheadList(ZEngineeringCivilBo bo) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy_MM");
        LinkedList<String> list = baseMapper.getTableNames("z_engineering_info");
        list.removeLast();
        List DateList = new ArrayList();
        list.stream().forEach(item -> {
            DateList.add(item.substring(item.indexOf("2")));
        });
        List<ZEngineeringCivilExportVo> zEngineeringCivilExportVoList = baseMapper.getexportList(DateList, format.format(new Date()), bo);

        HashMap<String, List<ZEngineeringCivilExportVo>> hashMap = new HashMap<>();

        for (int i = 0; i < zEngineeringCivilExportVoList.size(); i++) {
            if (hashMap.containsKey(zEngineeringCivilExportVoList.get(i).getId())) {
                List<ZEngineeringCivilExportVo> zEngineeringCivilExportVos = hashMap.get(zEngineeringCivilExportVoList.get(i).getId());
                zEngineeringCivilExportVos.add(zEngineeringCivilExportVoList.get(i));
            } else {
                List<ZEngineeringCivilExportVo> zEngineeringCivilExportVos = new ArrayList<>();
                zEngineeringCivilExportVos.add(zEngineeringCivilExportVoList.get(i));
                hashMap.put(zEngineeringCivilExportVoList.get(i).getId(), zEngineeringCivilExportVos);
            }
        }
        ArrayList<ZEngineeringCivilExportVos> arrayList = new ArrayList<>();
        List<String> strings = new ArrayList<>();
        for (Map.Entry<String, List<ZEngineeringCivilExportVo>> stringListEntry : hashMap.entrySet()) {
            strings.add(stringListEntry.getKey());
        }
        for (String string : strings) {
            ZEngineeringCivilExportVos zEngineeringCivilExportVos = new ZEngineeringCivilExportVos();
            zEngineeringCivilExportVos.setCxsy("×");
            zEngineeringCivilExportVos.setDm("×");
            for (ZEngineeringCivilExportVo zEngineeringCivilExportVo : hashMap.get(string)) {
                zEngineeringCivilExportVos.setDistrict(zEngineeringCivilExportVo.getDistrict());
                zEngineeringCivilExportVos.setAreaId(zEngineeringCivilExportVo.getAreaId());
                zEngineeringCivilExportVos.setBuildingId(zEngineeringCivilExportVo.getBuildingId());
                zEngineeringCivilExportVos.setUnitId(zEngineeringCivilExportVo.getUnitId());
                zEngineeringCivilExportVos.setHouseId(zEngineeringCivilExportVo.getHouseId());
                zEngineeringCivilExportVos.setSource(zEngineeringCivilExportVo.getSource());
                zEngineeringCivilExportVos.setEnginType(zEngineeringCivilExportVo.getEnginType());
                zEngineeringCivilExportVos.setEnginClassification(zEngineeringCivilExportVo.getEnginClassification());
                zEngineeringCivilExportVos.setCompletionStatus(zEngineeringCivilExportVo.getCompletionStatus());
                if ("×".equals(zEngineeringCivilExportVos.getCxsy()) && "除锈刷油".equals(zEngineeringCivilExportVo.getType()))
                    zEngineeringCivilExportVos.setCxsy("√");
                if ("×".equals(zEngineeringCivilExportVos.getDm()) && "打磨".equals(zEngineeringCivilExportVo.getType()))
                    zEngineeringCivilExportVos.setDm("√");
                if ("全貌照片".equals(zEngineeringCivilExportVo.getType())) {
                    if (ObjectUtils.isEmpty(zEngineeringCivilExportVos.getJKmaterialQuality())) {
                        zEngineeringCivilExportVos.setJKmaterialQuality("");
                    }
                    if (ObjectUtils.isEmpty(zEngineeringCivilExportVos.getJKspecifications())) {
                        zEngineeringCivilExportVos.setJKspecifications("");
                    }
                    if (ObjectUtils.isEmpty(zEngineeringCivilExportVos.getJKnumber())) {
                        zEngineeringCivilExportVos.setJKnumber("");
                    }
                    if ("".equals(zEngineeringCivilExportVos.getJKmaterialQuality()))
                        zEngineeringCivilExportVos.setJKmaterialQuality(zEngineeringCivilExportVo.getMaterialQuality());
                    else
                        zEngineeringCivilExportVos.setJKmaterialQuality(zEngineeringCivilExportVos.getJKmaterialQuality() + "/" + zEngineeringCivilExportVo.getMaterialQuality());

                    if ("".equals(zEngineeringCivilExportVos.getJKspecifications()))
                        zEngineeringCivilExportVos.setJKspecifications(zEngineeringCivilExportVo.getMaterialQuality());
                    else
                        zEngineeringCivilExportVos.setJKspecifications(zEngineeringCivilExportVos.getJKspecifications() + "/" + zEngineeringCivilExportVo.getMaterialQuality());

                    if ("".equals(zEngineeringCivilExportVos.getJKnumber()))
                        zEngineeringCivilExportVos.setJKnumber(zEngineeringCivilExportVo.getMaterialQuality());
                    else
                        zEngineeringCivilExportVos.setJKnumber(zEngineeringCivilExportVos.getJKnumber() + "/" + zEngineeringCivilExportVo.getMaterialQuality());

                }
            }
            arrayList.add(zEngineeringCivilExportVos);
        }


        return arrayList;
    }

    @Override
    public List<ZEngineeringCivilExportVos> exportCcourtyardList(ZEngineeringCivilBo bo) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy_MM");
        LinkedList<String> list = baseMapper.getTableNames("z_engineering_info");
        list.removeLast();
        List DateList = new ArrayList();
        list.stream().forEach(item -> {
            DateList.add(item.substring(item.indexOf("2")));
        });
        List<ZEngineeringCivilExportVo> zEngineeringCivilExportVoList = baseMapper.getexportList(DateList, format.format(new Date()), bo);

        HashMap<String, List<ZEngineeringCivilExportVo>> hashMap = new HashMap<>();

        for (int i = 0; i < zEngineeringCivilExportVoList.size(); i++) {
            if (hashMap.containsKey(zEngineeringCivilExportVoList.get(i).getId())) {
                List<ZEngineeringCivilExportVo> zEngineeringCivilExportVos = hashMap.get(zEngineeringCivilExportVoList.get(i).getId());
                zEngineeringCivilExportVos.add(zEngineeringCivilExportVoList.get(i));
            } else {
                List<ZEngineeringCivilExportVo> zEngineeringCivilExportVos = new ArrayList<>();
                zEngineeringCivilExportVos.add(zEngineeringCivilExportVoList.get(i));
                hashMap.put(zEngineeringCivilExportVoList.get(i).getId(), zEngineeringCivilExportVos);
            }
        }
        ArrayList<ZEngineeringCivilExportVos> arrayList = new ArrayList<>();
        List<String> strings = new ArrayList<>();
        for (Map.Entry<String, List<ZEngineeringCivilExportVo>> stringListEntry : hashMap.entrySet()) {
            strings.add(stringListEntry.getKey());
        }
        for (String string : strings) {
            ZEngineeringCivilExportVos zEngineeringCivilExportVos = new ZEngineeringCivilExportVos();
            zEngineeringCivilExportVos.setKwq("×");
            zEngineeringCivilExportVos.setKwh("×");
            zEngineeringCivilExportVos.setHk("×");
            zEngineeringCivilExportVos.setHt("×");
            zEngineeringCivilExportVos.setYbgc("×");
            for (ZEngineeringCivilExportVo zEngineeringCivilExportVo : hashMap.get(string)) {
                zEngineeringCivilExportVos.setDistrict(zEngineeringCivilExportVo.getDistrict());
                zEngineeringCivilExportVos.setAreaId(zEngineeringCivilExportVo.getAreaId());
                zEngineeringCivilExportVos.setBuildingId(zEngineeringCivilExportVo.getBuildingId());
                zEngineeringCivilExportVos.setUnitId(zEngineeringCivilExportVo.getUnitId());
                zEngineeringCivilExportVos.setHouseId(zEngineeringCivilExportVo.getHouseId());
                zEngineeringCivilExportVos.setSource(zEngineeringCivilExportVo.getSource());
                zEngineeringCivilExportVos.setEnginType(zEngineeringCivilExportVo.getEnginType());
                zEngineeringCivilExportVos.setEnginClassification(zEngineeringCivilExportVo.getEnginClassification());
                zEngineeringCivilExportVos.setCompletionStatus(zEngineeringCivilExportVo.getCompletionStatus());
                if ("×".equals(zEngineeringCivilExportVos.getKwq()) && "开挖前".equals(zEngineeringCivilExportVo.getType()))
                    zEngineeringCivilExportVos.setKwq("√");
                if ("×".equals(zEngineeringCivilExportVos.getKwh()) && "开挖后".equals(zEngineeringCivilExportVo.getType()))
                    zEngineeringCivilExportVos.setKwh("√");
                if ("×".equals(zEngineeringCivilExportVos.getHk()) && "焊口".equals(zEngineeringCivilExportVo.getType()))
                    zEngineeringCivilExportVos.setHk("√");
                if ("×".equals(zEngineeringCivilExportVos.getHt()) && "回填".equals(zEngineeringCivilExportVo.getType()))
                    zEngineeringCivilExportVos.setHt("√");
                if ("×".equals(zEngineeringCivilExportVos.getYbgc()) && "隐蔽工程".equals(zEngineeringCivilExportVo.getType()))
                    zEngineeringCivilExportVos.setYbgc("√");
                if ("下沟".equals(zEngineeringCivilExportVo.getType())) {
                    if (ObjectUtils.isEmpty(zEngineeringCivilExportVos.getXGmaterialQuality())) {
                        zEngineeringCivilExportVos.setXGmaterialQuality("");
                    }
                    if ("".equals(zEngineeringCivilExportVos.getXGmaterialQuality()))
                        zEngineeringCivilExportVos.setXGmaterialQuality(zEngineeringCivilExportVo.getMaterialQuality());
                    else
                        zEngineeringCivilExportVos.setXGmaterialQuality(zEngineeringCivilExportVos.getXGmaterialQuality() + "/" + zEngineeringCivilExportVo.getMaterialQuality());
                    if (ObjectUtils.isEmpty(zEngineeringCivilExportVos.getXGspecifications())) {
                        zEngineeringCivilExportVos.setXGspecifications("");
                    }
                    if ("".equals(zEngineeringCivilExportVos.getXGspecifications()))
                        zEngineeringCivilExportVos.setXGspecifications(zEngineeringCivilExportVo.getSpecifications());
                    else
                        zEngineeringCivilExportVos.setXGspecifications(zEngineeringCivilExportVos.getXGspecifications() + "/" + zEngineeringCivilExportVo.getSpecifications());
                    if (ObjectUtils.isEmpty(zEngineeringCivilExportVo.getNumber())) {
                        zEngineeringCivilExportVo.setNumber(0.0);
                    }
                    if (ObjectUtils.isEmpty(zEngineeringCivilExportVos.getXGnumber())) {
                        zEngineeringCivilExportVos.setXGnumber("");
                    }
                    if ("".equals(zEngineeringCivilExportVos.getXGnumber())) {
                        zEngineeringCivilExportVos.setXGnumber(zEngineeringCivilExportVo.getNumber().toString());
                    } else {
                        zEngineeringCivilExportVos.setXGnumber(zEngineeringCivilExportVos.getXGnumber() + "/" + zEngineeringCivilExportVo.getNumber().toString());
                    }
                }

            }
            arrayList.add(zEngineeringCivilExportVos);
        }


        return arrayList;
    }

    /**
     * 查询民用工程列表
     */
    @Override
    public List<ZEngineeringCivilVo> queryList(ZEngineeringCivilBo bo) {
        LambdaQueryWrapper<ZEngineeringCivil> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZEngineeringCivil> buildQueryWrapper(ZEngineeringCivilBo bo) {
        LambdaQueryWrapper<ZEngineeringCivil> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getDistrict()), ZEngineeringCivil::getDistrict, bo.getDistrict());
        lqw.eq(StringUtils.isNotBlank(bo.getAreaId()), ZEngineeringCivil::getAreaId, bo.getAreaId());
        lqw.eq(StringUtils.isNotBlank(bo.getBuildingId()), ZEngineeringCivil::getBuildingId, bo.getBuildingId());
        lqw.eq(StringUtils.isNotBlank(bo.getUnitId()), ZEngineeringCivil::getUnitId, bo.getUnitId());
        lqw.eq(StringUtils.isNotBlank(bo.getHouseId()), ZEngineeringCivil::getHouseId, bo.getHouseId());
        lqw.orderByDesc(ZEngineeringCivil::getCreateTime);
        lqw.eq(StringUtils.isNotBlank(bo.getEnginType()), ZEngineeringCivil::getEnginType, bo.getEnginType());
        lqw.eq(StringUtils.isNotBlank(bo.getEnginClassification()), ZEngineeringCivil::getEnginClassification, bo.getEnginClassification());
        lqw.eq(StringUtils.isNotBlank(bo.getEnginCycle()), ZEngineeringCivil::getEnginCycle, bo.getEnginCycle());
        lqw.eq(StringUtils.isNotBlank(bo.getCompletionStatus()), ZEngineeringCivil::getCompletionStatus, bo.getCompletionStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getImgUrl()), ZEngineeringCivil::getImgUrl, bo.getImgUrl());
        return lqw;
    }

    private List<String> zEngineeringCivilList(List createTime, String UserName, ZEngineeringCivilBo bo) {
        MonthTableNameHandler.removeData();
        LinkedList<String> list = baseMapper.getTableNames("z_engineering_info");
        list.removeLast();
        List<ZEngineeringInfoBo> mapList = baseMapper.getsumId(list, createTime, UserName);
        List<String> zEngineeringNodeList = new ArrayList<>();
        mapList.stream().forEach(item -> {
            String date = new SimpleDateFormat("yyyy_MM").format(item.getCreateTime());
            MonthTableNameHandler.setData(date);
            LambdaQueryWrapper<ZEngineeringNode> lqw = Wrappers.lambdaQuery();
            lqw.eq(StringUtils.isNotBlank(item.getEngInfoId()), ZEngineeringNode::getId, item.getEngInfoId());
            ZEngineeringNode zEngineeringNode = zEngineeringNodeMapper.selectOne(lqw);
            if (!ObjectUtils.isEmpty(zEngineeringNode))
                zEngineeringNodeList.add(zEngineeringNode.getCivliId());
            MonthTableNameHandler.removeData();
        });
        List newList = zEngineeringNodeList.stream().distinct().collect(Collectors.toList());
        List CivilList = new ArrayList();
        newList.stream().forEach(item2 -> {
            LambdaQueryWrapper<ZEngineeringCivil> lqw = Wrappers.lambdaQuery();
            lqw.eq(StringUtils.isNotBlank(bo.getEnginType()), ZEngineeringCivil::getEnginType, bo.getEnginType());
            lqw.eq(StringUtils.isNotBlank(bo.getEnginClassification()), ZEngineeringCivil::getEnginClassification, bo.getEnginClassification());
            lqw.eq(StringUtils.isNotBlank(bo.getEnginCycle()), ZEngineeringCivil::getEnginCycle, bo.getEnginCycle());
            lqw.eq(StringUtils.isNotBlank(bo.getAreaId()), ZEngineeringCivil::getAreaId, bo.getAreaId());
            lqw.eq(StringUtils.isNotBlank(bo.getBuildingId()), ZEngineeringCivil::getBuildingId, bo.getBuildingId());
            lqw.eq(StringUtils.isNotBlank(bo.getUnitId()), ZEngineeringCivil::getUnitId, bo.getUnitId());
            lqw.eq(StringUtils.isNotBlank(bo.getDistrict()), ZEngineeringCivil::getDistrict, bo.getDistrict());
            lqw.eq(ZEngineeringCivil::getId, item2);
            ZEngineeringCivil zEngineeringCivil = baseMapper.selectOne(lqw);
            if (!ObjectUtils.isEmpty(zEngineeringCivil)) {
                CivilList.add(zEngineeringCivil.getId());
            }
        });
        return CivilList;
    }

    ;

    @Autowired
    private ZEngineeringNodeServiceImpl zEngineeringNodeService;

    /**
     * 新增民用工程
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(ZEngineeringCivilBo bo) {
        ZEngineeringCivil add = BeanUtil.toBean(bo, ZEngineeringCivil.class);
        validEntityBeforeSave(add);
        LambdaQueryWrapper<ZEngineeringCivil> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getAreaId()), ZEngineeringCivil::getAreaId, bo.getAreaId());
        if (StringUtils.isEmpty(bo.getBuildingId())) {
            add.setBuildingId(null);
            lqw.isNull(ZEngineeringCivil::getBuildingId);
        } else {
            lqw.eq(ZEngineeringCivil::getBuildingId, bo.getBuildingId());
        }
        if (StringUtils.isEmpty(bo.getUnitId())) {
            add.setUnitId(null);
            lqw.isNull(ZEngineeringCivil::getUnitId);
        } else {
            lqw.eq(ZEngineeringCivil::getUnitId, bo.getUnitId());
        }
        if (StringUtils.isEmpty(bo.getHouseId())) {
            add.setHouseId(null);
            lqw.isNull(ZEngineeringCivil::getHouseId);
        } else {
            lqw.eq(ZEngineeringCivil::getHouseId, bo.getHouseId());
        }
        lqw.eq(StringUtils.isNotBlank(bo.getEnginCycle()), ZEngineeringCivil::getEnginCycle, bo.getEnginCycle());
        lqw.eq(StringUtils.isNotBlank(bo.getEnginType()), ZEngineeringCivil::getEnginType, bo.getEnginType());
        lqw.eq(StringUtils.isNotBlank(bo.getEnginClassification()), ZEngineeringCivil::getEnginClassification, bo.getEnginClassification());
        ZEngineeringCivilVo zEngineeringCivilVo = baseMapper.selectVoOne(lqw);
        if ("indoor_engin".equals(bo.getEnginClassification()) || "courtyard".equals(bo.getEnginClassification()) || "overhead".equals(bo.getEnginClassification())) {
            if (!ObjectUtils.isEmpty(bo.getzEngineeringNodeBo()) || !ObjectUtils.isEmpty(bo.getzEngineeringNodeBoList()) || bo.getzEngineeringNodeBoList().size() > 0)
                izMaterialStatisticsService.insert(bo);
        }
        if (ObjectUtils.isEmpty(zEngineeringCivilVo)) {

            boolean flag = baseMapper.insert(add) > 0;
            if (flag) {
                bo.setId(add.getId());
                if (ObjectUtils.isEmpty(bo.getzEngineeringNodeBo())) {
                    for (ZEngineeringNodeBo zEngineeringNodeBo : bo.getzEngineeringNodeBoList()) {
                        zEngineeringNodeBo.setCivliId(add.getId());
                        zEngineeringNodeBo.setCreateTime(add.getCreateTime());
                        zEngineeringNodeService.insert(zEngineeringNodeBo);
                    }
                    add.setCompletionStatus("施工中");
                    baseMapper.updateById(add);
                } else {
                    ZEngineeringNodeBo zEngineeringNodeBo = bo.getzEngineeringNodeBo();
                    zEngineeringNodeBo.setCivliId(add.getId());
                    zEngineeringNodeBo.setCreateTime(add.getCreateTime());
                    zEngineeringNodeService.insert(zEngineeringNodeBo);
                    add.setCompletionStatus("施工中");
                    baseMapper.updateById(add);
                }

            }
            return flag;
        } else {
            if (!ObjectUtils.isEmpty(bo.getzEngineeringNodeBoList())) {
                for (ZEngineeringNodeBo zEngineeringNodeBo : bo.getzEngineeringNodeBoList()) {
                    zEngineeringNodeBo.setCivliId(zEngineeringCivilVo.getId());
                    zEngineeringNodeBo.setCreateTime(zEngineeringCivilVo.getCreateTime());
                    ZEngineeringNodeBo query = zEngineeringNodeService.query(zEngineeringNodeBo);
                    if (!ObjectUtils.isEmpty(query)) {
                        zEngineeringNodeBo.setId(query.getId());
                    }
                    add.setCompletionStatus("施工中");
                    baseMapper.updateById(add);
                    zEngineeringNodeService.insert(zEngineeringNodeBo);
                }

            } else if (!ObjectUtils.isEmpty(bo.getzEngineeringNodeBo())) {
                ZEngineeringNodeBo zEngineeringNodeBo = bo.getzEngineeringNodeBo();
                zEngineeringNodeBo.setCivliId(zEngineeringCivilVo.getId());
                zEngineeringNodeBo.setType(zEngineeringNodeBo.getType());
                zEngineeringNodeBo.setCreateTime(zEngineeringCivilVo.getCreateTime());
                ZEngineeringNodeBo query = zEngineeringNodeService.query(zEngineeringNodeBo);
                if (!ObjectUtils.isEmpty(query)) {
                    zEngineeringNodeBo.setId(query.getId());
                }
                add.setCompletionStatus("施工中");
                baseMapper.updateById(add);
                zEngineeringNodeService.insert(zEngineeringNodeBo);
            } else {
                throw new RuntimeException("已存在工程,请填写施工信息");
            }
            if (!ObjectUtils.isEmpty(bo.getHouseId())) {
                LambdaQueryWrapper<ZEnginNotInstalled> notInstalledLambdaQueryWrapper = new LambdaQueryWrapper<>();
                notInstalledLambdaQueryWrapper.eq(StringUtils.isNotBlank(bo.getAreaId()), ZEnginNotInstalled::getAreaId, bo.getAreaId());
                notInstalledLambdaQueryWrapper.eq(StringUtils.isNotBlank(bo.getBuildingId()), ZEnginNotInstalled::getBuildingId, bo.getBuildingId());
                notInstalledLambdaQueryWrapper.eq(StringUtils.isNotBlank(bo.getUnitId()), ZEnginNotInstalled::getUnitId, bo.getUnitId());
                notInstalledLambdaQueryWrapper.eq(StringUtils.isNotBlank(bo.getHouseId()), ZEnginNotInstalled::getHouseId, bo.getHouseId());
                zEnginNotInstalledMapper.delete(notInstalledLambdaQueryWrapper);
            }

            return true;
        }
    }

    /**
     * 修改民用工程
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(ZEngineeringCivilBo bo) {
        ZEngineeringCivil update = BeanUtil.toBean(bo, ZEngineeringCivil.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            ZEngineeringCivilVo zEngineeringCivilVo = baseMapper.selectVoById(update.getId());
            ZEngineeringNodeBo zEngineeringNodeBo = bo.getzEngineeringNodeBo();
            if (!ObjectUtils.isEmpty(zEngineeringNodeBo)) {
                zEngineeringNodeBo.setCivliId(zEngineeringCivilVo.getId());
                zEngineeringNodeBo.setCreateTime(zEngineeringCivilVo.getCreateTime());
                if (!ObjectUtils.isEmpty(zEngineeringNodeBo.getzEngineeringInfoBoList()))
                    zEngineeringNodeService.updateMuch(zEngineeringNodeBo);
            }
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZEngineeringCivil entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除民用工程
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
            ids.forEach(item -> {
                ZEngineeringCivilVo zEngineeringCivilVo = baseMapper.selectVoById(item);
                ZEngineeringNodeBo zEngineeringNodeBo = new ZEngineeringNodeBo();
                zEngineeringNodeBo.setCreateTime(zEngineeringCivilVo.getCreateTime());
                zEngineeringNodeBo.setCivliId(zEngineeringCivilVo.getId());
                List<ZEngineeringNodeBo> zEngineeringNodeBos = izEngineeringNodeService.queryListDetails(zEngineeringNodeBo);
                zEngineeringNodeBos.forEach(ite -> {
                    LambdaQueryWrapper<ZMaterialStatistics> lqw = Wrappers.lambdaQuery();
                    lqw.eq(StringUtils.isNotBlank(zEngineeringCivilVo.getAreaId()), ZMaterialStatistics::getAreaId, zEngineeringCivilVo.getAreaId());
                    lqw.eq(StringUtils.isNotBlank(zEngineeringCivilVo.getBuildingId()), ZMaterialStatistics::getBuildingId, zEngineeringCivilVo.getBuildingId());
                    lqw.eq(StringUtils.isNotBlank(zEngineeringCivilVo.getUnitId()), ZMaterialStatistics::getUnitId, zEngineeringCivilVo.getUnitId());
                    lqw.eq(StringUtils.isNotBlank(zEngineeringCivilVo.getHouseId()), ZMaterialStatistics::getHouseId, zEngineeringCivilVo.getHouseId());
                    lqw.eq(StringUtils.isNotBlank(zEngineeringCivilVo.getEnginCycle()), ZMaterialStatistics::getEnginCycle, zEngineeringCivilVo.getEnginCycle());
                    lqw.eq(StringUtils.isNotBlank(zEngineeringCivilVo.getEnginClassification()), ZMaterialStatistics::getEnginClassification, zEngineeringCivilVo.getEnginClassification());
                    lqw.eq(StringUtils.isNotBlank(zEngineeringCivilVo.getEnginType()), ZMaterialStatistics::getEnginType, zEngineeringCivilVo.getEnginType());
                    zMaterialStatisticsMapper.delete(lqw);
                    zEngineeringNodeService.delete(ite);
                });
            });
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public int getCivilByAreaId(Collection<String> ids) {
        LambdaQueryWrapper<ZEngineeringCivil> lqw = Wrappers.lambdaQuery();
        lqw.in(!ids.isEmpty(), ZEngineeringCivil::getAreaId, ids);
        return baseMapper.selectVoList(lqw).size();
    }

    @Override
    public int getCivilByhouseId(Collection<String> ids) {
        LambdaQueryWrapper<ZEngineeringCivil> lqw = Wrappers.lambdaQuery();
        lqw.in(!ids.isEmpty(), ZEngineeringCivil::getHouseId, ids);
        return baseMapper.selectVoList(lqw).size();
    }

    @Override
    public List getObtainRoomcCompletionInformationList(ZEngineeringCivilBo bo) {
        List<roomStatusVo> roomStatusList = baseMapper.getObtainRoomcCompletionInformationList(bo);
        LinkedHashMap<String, List<roomStatusVo>> map = new LinkedHashMap<>();
        List<roomStatusVo> mapvalue = new ArrayList<>();
        for (int i = 0; i < roomStatusList.size(); i++) {
            if (i > 0 && roomStatusList.get(i).getUnitOrder().equals(roomStatusList.get(i - 1).getUnitOrder())) {
                mapvalue.add(roomStatusList.get(i));
            } else {
                mapvalue = new ArrayList<>();
                mapvalue.add(roomStatusList.get(i));
            }
            map.put(roomStatusList.get(i).getUnitName(), mapvalue);
        }
        //        map = roomStatusList.stream().collect(Collectors.groupingBy(item -> item.getUnitName()));
        List areaList = new ArrayList();
        map.forEach((key, value) -> {
            BulidingStatusVo bulidingStatusVo = new BulidingStatusVo();
            bulidingStatusVo.setUnitName(key);
            bulidingStatusVo.setUnitId(value.get(0).getUnitId());
            Map<String, List<roomStatusVo>> roomMap = value.stream().collect(Collectors.groupingBy(item -> (item.getRoomName().replaceAll("[^0-9]", "").length() > 3 ? item.getRoomName().substring(0, 2) : item.getRoomName().substring(0, 1))));
            List willDone = value.stream().filter(item -> item.getRoomStatus().equals("未施工")).collect(Collectors.toList());
            List notstart = value.stream().filter(item -> item.getRoomStatus().equals("施工中")).collect(Collectors.toList());
            List done = value.stream().filter(item -> item.getRoomStatus().equals("竣工")).collect(Collectors.toList());
            List notInstall = value.stream().filter(item -> item.getRoomStatus().equals("未安装")).collect(Collectors.toList());
            List<List<roomStatusVo>> roomStatusVoList = new ArrayList<>();
            Map<String, List<roomStatusVo>> sortMap = roomMap.entrySet().stream().sorted(Comparator.comparingInt(entry -> Integer.parseInt(entry.getKey()))).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldVal, newVal) -> oldVal, LinkedHashMap::new));
            sortMap.forEach((key1, value1) -> {
                roomStatusVoList.add(value1);
            });
            bulidingStatusVo.setRoomStatusVoList(roomStatusVoList);
            bulidingStatusVo.setDone(String.valueOf(notInstall.size()));
            bulidingStatusVo.setDone(String.valueOf(done.size()));
            bulidingStatusVo.setWillDone(String.valueOf(willDone.size()));
            bulidingStatusVo.setNotstart(String.valueOf(notstart.size()));
            areaList.add(bulidingStatusVo);
        });
        return areaList;
    }

    @Override
    public TableDataInfo<areaStatusVo> getAreaCompletionInformationList(ZEngineeringCivilBo bo, PageQuery po) {
        LambdaQueryWrapper<ZArea> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getAreaId()), ZArea::getId, bo.getAreaId());
        lqw.eq(StringUtils.isNotBlank(bo.getDistrict()), ZArea::getDistrict, bo.getDistrict());
        Page<ZBuildingBo> page = new Page<>(po.getPageNum(), po.getPageSize());
        Page<ZAreaVo> result = zAreaMapper.selectVoPage(po.build(), lqw);
        List<ZAreaVo> IdList = result.getRecords();
        List Id = new ArrayList();
        IdList.stream().forEach(item -> {
            Id.add(item.getId());
        });
        List<areaWillDoneVo> hourseList = baseMapper.getAreaCompletionInformationList(Id, bo.getEnginCycle(), bo.getEnginType());
        Map<String, List<areaWillDoneVo>> stringListMap = hourseList.stream().collect(Collectors.groupingBy(item -> item.getAreaId()));
        List<areaStatusVo> areaList = new ArrayList<>();
        stringListMap.forEach((key, value) -> {
            areaStatusVo areaStatusVo = new areaStatusVo();
            List notstart = value.stream().filter(item -> item.getRoomStatus().equals("未施工")).collect(Collectors.toList());
            List willDone = value.stream().filter(item -> item.getRoomStatus().equals("施工中")).collect(Collectors.toList());
            List done = value.stream().filter(item -> item.getRoomStatus().equals("竣工")).collect(Collectors.toList());
            List notInstall = value.stream().filter(item -> item.getRoomStatus().equals("未安装")).collect(Collectors.toList());
            areaStatusVo.setAreaId(key);
            areaStatusVo.setAreaName(value.get(0).getName());
            areaStatusVo.setWillDone(String.valueOf(willDone.size()));
            areaStatusVo.setNotstart(String.valueOf(notstart.size()));
            areaStatusVo.setDone(String.valueOf(done.size()));
            areaStatusVo.setNotInstall(String.valueOf(notInstall.size()));
            areaStatusVo.setScale(value.get(0).getScale().toString());
            areaList.add(areaStatusVo);
        });
        Page<areaStatusVo> page2 = new Page<>();
        BeanUtils.copyProperties(result, page2);
        page2.setRecords(areaList);
        return TableDataInfo.build(page2);
    }

    @Override
    public List<areaStatusVo> getAreaCompletionInformationList2(ZEngineeringCivilBo bo) {
        List<areaWillDoneVo> hourseList = baseMapper.getAreaCompletionInformationList2(bo.getEnginCycle(), bo.getEnginType());
        Map<String, List<areaWillDoneVo>> stringListMap = hourseList.stream().collect(Collectors.groupingBy(item -> item.getAreaId()));
        List<areaStatusVo> areaList = new ArrayList<>();
        stringListMap.forEach((key, value) -> {
            areaStatusVo areaStatusVo = new areaStatusVo();
            List notstart = value.stream().filter(item -> item.getRoomStatus().equals("未施工")).collect(Collectors.toList());
            List willDone = value.stream().filter(item -> item.getRoomStatus().equals("施工中")).collect(Collectors.toList());
            List done = value.stream().filter(item -> item.getRoomStatus().equals("竣工")).collect(Collectors.toList());
            List notInstall = value.stream().filter(item -> item.getRoomStatus().equals("未安装")).collect(Collectors.toList());
            areaStatusVo.setAreaId(key);
            areaStatusVo.setAreaName(value.get(0).getName());
            areaStatusVo.setWillDone(String.valueOf(willDone.size()));
            areaStatusVo.setNotstart(String.valueOf(notstart.size()));
            areaStatusVo.setDone(String.valueOf(done.size()));
            areaStatusVo.setNotInstall(String.valueOf(notInstall.size()));
            areaStatusVo.setScale(value.get(0).getScale().toString());
            areaList.add(areaStatusVo);
        });

        return areaList;
    }

    @Override
    public Map<String, Number> getsumStatus(ZEngineeringCivilBo bo) {
        Map<String, Number> areaStatusVo = baseMapper.getsumStatus(bo.getDistrict());
        //        areaStatusVo.setNotstart(String.valueOf(Integer.parseInt(areaStatusVo.getNotstart()) - Integer.parseInt(areaStatusVo.getWillDone()) - Integer.parseInt(areaStatusVo.getDone())));
        return areaStatusVo;
    }

    @Override
    public int batchReview(ZEngineeringCivilBo bo, ZEngineeringNodeBo zEngineeringNodeBo) {
        ZEngineeringCivil add = BeanUtil.toBean(bo, ZEngineeringCivil.class);
        validEntityBeforeSave(add);
        LambdaQueryWrapper<ZEngineeringCivil> lqw = Wrappers.lambdaQuery();
        //        lqw.eq(StringUtils.isNotBlank(bo.getAreaId()), ZEngineeringCivil::getAreaId, bo.getAreaId());
        //        lqw.eq(ZEngineeringCivil::getBuildingId, bo.getBuildingId());
        //        lqw.eq(ZEngineeringCivil::getUnitId, bo.getUnitId());
        //        lqw.eq(ZEngineeringCivil::getHouseId, bo.getHouseId());
        lqw.eq(StringUtils.isNotBlank(bo.getEnginCycle()), ZEngineeringCivil::getEnginCycle, bo.getEnginCycle());
        lqw.eq(StringUtils.isNotBlank(bo.getEnginClassification()), ZEngineeringCivil::getEnginClassification, bo.getEnginClassification());
        lqw.eq(StringUtils.isNotBlank(bo.getEnginType()), ZEngineeringCivil::getEnginType, bo.getEnginType());
        //        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
        lqw.like(StringUtils.isNotBlank(bo.getReviewTime().toString()), ZEngineeringCivil::getCreateTime, bo.getReviewTime());
        Long sumcount = baseMapper.selectCount(lqw);
        //        List<ZEngineeringCivil> zEngineeringCivilList=baseMapper.selectList(lqw);
        /**
         * 线程执行插入量
         */
        int count = 500;
        //        List<ZEngineeringCivil> newList = new ArrayList();
        if (sumcount > 0) {
            //线程数量（根据查询总数量进行分配线程数量）
            int runSize = (int) (sumcount / count + 1);
            //ThreadPoolExecutor threadPoolTaskExecutor = new ScheduledThreadPoolExecutor(runSize);
            ExecutorService threadPoolTaskExecutor = Executors.newCachedThreadPool();
            CountDownLatch countDownLatch = new CountDownLatch(runSize);
            for (int i = 1; i <= runSize; i++) {
                Page page = new Page<>(i, count);
                //                Page<ZEngineeringCivil> zEngineeringCivilPage = baseMapper.selectPage(page, lqw);
                //                if((i+1)==runSize){
                //                    int startIndex = (i*count);;
                //                    int endIndex = zEngineeringCivilList.size();
                //                    newList =zEngineeringCivilList.subList(startIndex,endIndex);
                //                }else{
                //                    int startIndex = i*count;;
                //                    int endIndex = (i+1)*count;
                //                    newList =zEngineeringCivilList.subList(startIndex,endIndex);
                //                }
                System.out.println("第" + i + "次查询：" + page.getSize());
                threadPoolTaskExecutor.execute(new RiewTask(zEngineeringNodeBo, countDownLatch, page, lqw));
                threadPoolTaskExecutor.execute(new photoTask(zEngineeringNodeBo, countDownLatch, page, lqw));
            }
            threadPoolTaskExecutor.shutdown();
            //            try {
            //                countDownLatch.await();
            //                threadPoolTaskExecutor.shutdown();
            //            } catch (InterruptedException e) {
            //                throw new RuntimeException(e);
            //            }
            //
        }

        return 0;
    }

    @Override
    @DynamicName(spel = "#zEngineeringReviewBo.createTime")
    public int Civilinsert(ZEngineeringReviewBo zEngineeringReviewBo, infoIdList infoIdList) {
        ZEngineeringNode zEngineeringNode = zEngineeringNodeMapper.selectById(zEngineeringReviewBo.getEngInfoId());
        if (zEngineeringReviewBo.getReviewStatus().equals("1")) {
            infoIdList.getDelInfoIdList().stream().forEach(item -> {
                ZEngineeringInfoBo zEngineeringInfoBo = new ZEngineeringInfoBo();
                zEngineeringInfoBo.setCreateTime(zEngineeringNode.getCreateTime());
                zEngineeringInfoBo.setId(item);
                izEngineeringInfoService.deleteByIdNoDelFiles(zEngineeringInfoBo);
            });
        }
        infoIdList.getInfoIdList().stream().forEach(item -> {
            item.setState(zEngineeringReviewBo.getReviewStatus());
            SimpleDateFormat format = new SimpleDateFormat("yyyy_MM");
            UpdateWrapper<ZEngineeringInfo> lqw = new UpdateWrapper();
            lqw.eq("id", item.getId()).set("state", zEngineeringReviewBo.getReviewStatus());
            MonthTableNameHandler.setData(format.format(item.getCreateTime()));
            zEngineeringInfoMapper.update(lqw);
            MonthTableNameHandler.removeData();
        });
        ZEngineeringCivil zEngineeringCivil = baseMapper.selectById(zEngineeringNode.getCivliId());
        ZEngineeringCivilBo zEngineeringCivilBo = BeanUtil.copyProperties(zEngineeringCivil, ZEngineeringCivilBo.class);
        ZEngineeringNodeBo zEngineeringNodeBo = BeanUtil.copyProperties(zEngineeringNode, ZEngineeringNodeBo.class);
        zEngineeringCivilBo.setzEngineeringNodeBo(izEngineeringNodeService.queryInsert(zEngineeringNodeBo));
        zEngineeringCivilBo.setCreateTime(zEngineeringReviewBo.getCreateTime());
        izEngineeringReviewService.Civilinsert(zEngineeringCivil, zEngineeringReviewBo);
        areaMessageVo areaMessageVo = new areaMessageVo();
        if (zEngineeringCivilBo.getEnginClassification().equals("courtyard") || zEngineeringCivilBo.getEnginClassification().equals("overhead ")) {
            if (!"".equals(zEngineeringCivilBo.getBuildingId())) {
                areaMessageVo = baseMapper.getAreaMessage(zEngineeringCivilBo.getBuildingId());
                areaMessageVo.setHouseId("未知");
                areaMessageVo.setUnitId("未知");
            } else {
                areaMessageVo = baseMapper.getAreaName(zEngineeringCivilBo.getAreaId());
                areaMessageVo.setHouseId("未知");
                areaMessageVo.setUnitId("未知");
                areaMessageVo.setBuildingId("未知");
            }
        } else {
            areaMessageVo = baseMapper.getHouseMessage(zEngineeringCivilBo.getHouseId());
        }
        //        areaMessageVo areaMessageVo = baseMapper.getHouseMessage(zEngineeringCivilBo.getHouseId());
        if (!ObjectUtils.isEmpty(areaMessageVo))

            BeanUtils.copyProperties(areaMessageVo, zEngineeringCivilBo);
        Map<String, List<SysDictData>> sysDictMap = RedisUtils.getCacheMap("sys_dict");
        sysDictMap.get(zEngineeringCivilBo.getEnginType()).stream().forEach(item -> {
            if (item.getDictValue().equals(zEngineeringCivilBo.getEnginClassification())) {
                zEngineeringCivilBo.setEnginClassification(item.getDictLabel());
            }
        });
        //        sysDictMap.get(zEngineeringCivilBo.getEnginType() + "_" + zEngineeringCivilBo.getEnginClassification()).stream().forEach(item -> {
        //            if (item.getDictValue().equals(zEngineeringCivilBo.getzEngineeringNodeBo().getType())) {
        //                zEngineeringCivilBo.setEnginClassification(item.getDictLabel());
        //            }
        //        });
        sysDictMap.get("engin_cycle").stream().forEach(item -> {
            if (item.getDictValue().equals(zEngineeringCivilBo.getEnginCycle())) {
                zEngineeringCivilBo.setEnginCycle(item.getDictLabel());
            }
        });
        //        zEngineeringCivilBo.getzEngineeringNodeBo().setType(type);
        //        zEngineeringCivilBo.setEnginClassification(completionStatus);
        if (zEngineeringCivilBo.getEnginType().equals("old_renovation")) {
            zEngineeringCivilBo.setEnginType("旧改");
        } else if (zEngineeringCivilBo.getEnginType().equals("new_built")) {
            zEngineeringCivilBo.setEnginType("新建");
        }

        izEngiineeringPhotoService.write(zEngineeringCivilBo);
        return 0;
    }

    @Override
    @DynamicName(spel = "#zEngineeringReviewBo.createTime")
    public int insertReview(ZEngineeringReviewBo zEngineeringReviewBo, infoIdList infoIdList) {
        try {
            infoIdList.getInfoIdList().stream().forEach(item -> {
                item.setState(zEngineeringReviewBo.getReviewStatus());
                SimpleDateFormat format = new SimpleDateFormat("yyyy_MM");
                UpdateWrapper<ZEngineeringInfo> lqw = new UpdateWrapper();
                lqw.eq("id", item.getId()).set("state", zEngineeringReviewBo.getReviewStatus());
                MonthTableNameHandler.setData(format.format(item.getCreateTime()));
                zEngineeringInfoMapper.update(lqw);
                MonthTableNameHandler.removeData();
                izEngiineeringPhotoService.writeImg(infoIdList.getInfoIdList(), item,zEngineeringReviewBo.getEngInfoName());
            });
            izEngineeringReviewService.insert(zEngineeringReviewBo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Override
    public List<ZEngineeringNodeBo> getRivewStatus(String Id) {
        ZEngineeringCivil zEngineeringCivil1 = baseMapper.selectById(Id);
        if (!ObjectUtils.isEmpty(zEngineeringCivil1)) {

            return izEngineeringNodeService.queryNodeList(zEngineeringCivil1);
        } else {
            return null;
        }

    }

    @Override
    public Boolean batchNode(ZEngineeringCivilBo bo) {
        List<ZHouse> zHouseList = zHouseMapper.selectHouseId(bo);
        zEngineeringNodeService.batchNode(zHouseList, bo, LoginHelper.getLoginUser().getUserId().toString());
        return true;

    }
}


