package com.ruoyi.zdsz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.zdsz.domain.ZEngiineeringPhoto;
import com.ruoyi.zdsz.domain.ZEngineeringAirWall;
import com.ruoyi.zdsz.domain.ZEngineeringIndustry;
import com.ruoyi.zdsz.domain.ZEngineeringNode;
import com.ruoyi.zdsz.domain.bo.*;
import com.ruoyi.zdsz.domain.vo.ZEngiineeringPhotoVo;
import com.ruoyi.zdsz.domain.vo.ZEngineeringAirWallVo;
import com.ruoyi.zdsz.domain.vo.ZEngineeringIndustryTypeVo;
import com.ruoyi.zdsz.domain.vo.ZEngineeringIndustryVo;
import com.ruoyi.zdsz.mapper.ZEngineeringAirWallMapper;
import com.ruoyi.zdsz.mapper.ZEngineeringIndustryMapper;
import com.ruoyi.zdsz.service.IZEngiineeringPhotoService;
import com.ruoyi.zdsz.service.IZEngineeringAirWallService;
import com.ruoyi.zdsz.service.IZEngineeringIndustryService;
import com.ruoyi.zdsz.service.IZEngineeringNodeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.stream.Collectors;

/**
 * ,带气封堵施工Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-02
 */
@RequiredArgsConstructor
@Service
public class ZEngineeringAirWallServiceImpl implements IZEngineeringAirWallService {

    @Resource
    private final ZEngineeringAirWallMapper baseMapper;

    @Resource
    private IZEngineeringNodeService izEngineeringNodeService;

    @Resource
    private IZEngineeringNodeService nodeService;

    @Resource
    private IZEngiineeringPhotoService photoService;

    /**
     * 查询,带气封堵施工
     */
    @Override
    public ZEngineeringAirWallVo queryById(String id) {
        ZEngineeringAirWallVo zEngineeringAirWallVo = baseMapper.selectVoById(id);

        ZEngiineeringPhotoBo zEngiineeringPhotoBo = new ZEngiineeringPhotoBo();
        zEngiineeringPhotoBo.setParentId(zEngineeringAirWallVo.getId());
        zEngiineeringPhotoBo.setCreateTime(zEngineeringAirWallVo.getCreateTime());
        zEngineeringAirWallVo.setPics(photoService.queryList(zEngiineeringPhotoBo));
        ZEngineeringNodeBo zEngineeringNodeBo = new ZEngineeringNodeBo();
        zEngineeringNodeBo.setCivliId(id);
        zEngineeringNodeBo.setCreateTime(zEngineeringAirWallVo.getCreateTime());
        List<ZEngineeringNodeBo> zEngineeringNodeBos = izEngineeringNodeService.queryListDetails(zEngineeringNodeBo);
        zEngineeringAirWallVo.setZEngineeringNodeBoList(zEngineeringNodeBos);
        return zEngineeringAirWallVo;
    }

    /**
     * 查询,带气封堵施工列表
     */
    @Override
    public TableDataInfo<ZEngineeringAirWallVo> queryPageList(ZEngineeringAirWallBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZEngineeringAirWall> lqw = buildQueryWrapper(bo);
        lqw.like(StringUtils.isNotBlank(bo.getEnginName()), ZEngineeringAirWall::getEnginName, bo.getEnginName());
        lqw.eq(StringUtils.isNotBlank(bo.getEnginType()), ZEngineeringAirWall::getEnginType, bo.getEnginType());
        Page<ZEngineeringAirWallVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Map<String, Object> ReviewList = nodeService.ReViewAirList(result.getRecords());
        result.getRecords().forEach(item -> {
            item.setNodeReViewStateList(ReviewList.get(item.getId()));
        });
        return TableDataInfo.build(result);
    }

    /**
     * 查询,带气封堵施工列表
     */
    @Override
    public List<ZEngineeringAirWallVo> queryList(ZEngineeringAirWallBo bo) {
        LambdaQueryWrapper<ZEngineeringAirWall> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZEngineeringAirWall> buildQueryWrapper(ZEngineeringAirWallBo bo) {
        LambdaQueryWrapper<ZEngineeringAirWall> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(ZEngineeringAirWall::getCreateTime);
//        lqw.eq(StringUtils.isNotBlank(bo.getEnginType()), ZEngineeringIndustry::getEnginType, bo.getEnginType());
//        lqw.like(StringUtils.isNotBlank(bo.getEnginName()), ZEngineeringIndustry::getEnginName, bo.getEnginName());
//        lqw.like(StringUtils.isNotBlank(bo.getEnginClassification()), ZEngineeringIndustry::getEnginClassification, bo.getEnginClassification());
//        lqw.eq(StringUtils.isNotBlank(bo.getConstructUnit()), ZEngineeringIndustry::getConstructUnit, bo.getConstructUnit());
//        lqw.eq(StringUtils.isNotBlank(bo.getProjectHead()), ZEngineeringIndustry::getProjectHead, bo.getProjectHead());
//        lqw.eq(StringUtils.isNotBlank(bo.getSceneHead()), ZEngineeringIndustry::getSceneHead, bo.getSceneHead());
//        lqw.eq(StringUtils.isNotBlank(bo.getDesignUnit()), ZEngineeringIndustry::getDesignUnit, bo.getDesignUnit());
//        lqw.eq(StringUtils.isNotBlank(bo.getDesignHead()), ZEngineeringIndustry::getDesignHead, bo.getDesignHead());
//        lqw.eq(StringUtils.isNotBlank(bo.getDesignPhone()), ZEngineeringIndustry::getDesignPhone, bo.getDesignPhone());
//        lqw.eq(StringUtils.isNotBlank(bo.getSupervisionUnit()), ZEngineeringIndustry::getSupervisionUnit, bo.getSupervisionUnit());
//        lqw.eq(StringUtils.isNotBlank(bo.getSupervisionHead()), ZEngineeringIndustry::getSupervisionHead, bo.getSupervisionHead());
//        lqw.eq(StringUtils.isNotBlank(bo.getSupervisionPhone()), ZEngineeringIndustry::getSupervisionPhone, bo.getSupervisionPhone());
//        lqw.eq(StringUtils.isNotBlank(bo.getType()), ZEngineeringIndustry::getType, bo.getType());
        return lqw;
    }

    /**
     * 新增,带气封堵施工
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(ZEngineeringAirWallBo bo) {
        ZEngineeringAirWall add = BeanUtil.toBean(bo, ZEngineeringAirWall.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
            List<ZEngiineeringPhoto> list = new ArrayList<>();
            bo.getFiles().forEach(o -> {
                ZEngiineeringPhoto pic = new ZEngiineeringPhoto();
                pic.setParentId(bo.getId());
                pic.setPicUrl(o.getUrl());
                pic.setFileName(o.getName());
                pic.setCreateBy(String.valueOf(LoginHelper.getUserId()));
                pic.setCreateTime(new Date());
                list.add(pic);
            });
            if (!list.isEmpty()) {
                photoService.insertBatch(list);
            }
        }
        if (!ObjectUtils.isEmpty(bo.getzEngineeringNodeBo())) {
            bo.getzEngineeringNodeBo().setCivliId(add.getId());
            bo.getzEngineeringNodeBo().setCreateTime(new Date());
            izEngineeringNodeService.insert(bo.getzEngineeringNodeBo());
        }
        return flag;
    }

    /**
     * 修改,带气封堵施工
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(ZEngineeringAirWallBo bo) {
        ZEngineeringAirWall update = BeanUtil.toBean(bo, ZEngineeringAirWall.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        // 查询数据库中已存在的主表创建时间
        ZEngineeringAirWallVo vo = baseMapper.selectVoById(bo.getId());
        if (flag) {
            List<ZEngiineeringPhoto> list = new ArrayList<>();
            // 查询数据库中已存在的文件列表
            ZEngiineeringPhotoBo zEngiineeringPhotoBo = new ZEngiineeringPhotoBo();
            zEngiineeringPhotoBo.setParentId(update.getId());
            zEngiineeringPhotoBo.setCreateTime(vo.getCreateTime());
            List<String> existingPhotos = photoService.queryList(zEngiineeringPhotoBo).stream().map(ZEngiineeringPhotoVo::getPicUrl).collect(Collectors.toList());
            // 插入前端传来的新文件
            if (!ObjectUtils.isEmpty(bo.getFiles())) {
                for (ZFileBo pic : bo.getFiles()) {
                    if (!existingPhotos.contains(pic.getUrl())) {
                        ZEngiineeringPhoto newPhoto = new ZEngiineeringPhoto();
                        newPhoto.setParentId(bo.getId());
                        newPhoto.setPicUrl(pic.getUrl());
                        newPhoto.setFileName(pic.getName());
                        newPhoto.setCreateBy(String.valueOf(LoginHelper.getUserId()));
                        newPhoto.setCreateTime(bo.getCreateTime());
                        newPhoto.setUpdateTime(new Date());
                        list.add(newPhoto);
                    }
                }
                // 删除数据库中多余的文件
                List<String> photosToDelete = existingPhotos.stream().filter(pic -> !bo.getFiles().stream().map(ZFileBo::getUrl).collect(Collectors.toList()).contains(pic)).collect(Collectors.toList());
                if (!photosToDelete.isEmpty()) {
                    photosToDelete.forEach(item -> {
                        ZEngiineeringPhoto zEngiineeringPhoto = new ZEngiineeringPhoto();
                        zEngiineeringPhoto.setPicUrl(item);
                        zEngiineeringPhoto.setCreateTime(bo.getCreateTime());
                        photoService.deleteWithValid(zEngiineeringPhoto);
                    });
                }
                // 批量插入新图片
                if (!list.isEmpty()) {
                    photoService.insertBatch(list);
                }
            }
        }
        bo.getzEngineeringNodeBo().setCivliId(bo.getId());
//        bo.getzEngineeringNodeBo().setType(bo.getType());
        bo.getzEngineeringNodeBo().setCreateTime(bo.getCreateTime());
        ZEngineeringNodeBo zEngineeringNodeBo = new ZEngineeringNodeBo();
        zEngineeringNodeBo.setCivliId(update.getId());
//        zEngineeringNodeBo.setType(bo.getType());
//        zEngineeringNodeBo.setType(bo.getzEngineeringNodeBo().getType()); // zEngineeringNodeBo.setType(bo.getzEngineeringNodeBo().getType());
        zEngineeringNodeBo.setType("带气封堵施工"); // zEngineeringNodeBo.setType(bo.getzEngineeringNodeBo().getType());
        zEngineeringNodeBo.setCreateTime(baseMapper.selectVoById(bo.getId()).getCreateTime());
        ZEngineeringNodeBo query = izEngineeringNodeService.query(zEngineeringNodeBo);
        if (!ObjectUtils.isEmpty(query)) {
            bo.getzEngineeringNodeBo().setId(query.getId());
        }
        bo.getzEngineeringNodeBo().setType("带气封堵施工");
        bo.getzEngineeringNodeBo().setCreateTime(zEngineeringNodeBo.getCreateTime());
        izEngineeringNodeService.insert(bo.getzEngineeringNodeBo());
        System.out.println("izEngineeringNodeService.insert(bo.getzEngineeringNodeBo());");
        return flag;
    }

    /**
     * 本故事为管道厚度功能原创模块
     */
    private void batchUpdatePic(ZEngineeringAirWallBo bo,  String methodName, String type) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        // 获取类对象
        Class<?> cls = ZEngineeringInfoBo.class;
        // 获取指定方法
        Method method = cls.getDeclaredMethod(methodName);
//        System.out.println("method.invoke(obj);==="+method.invoke(bo.getzEngineeringNodeBo().getzEngineeringInfoBoList().get(0)));
        // 故事正文开始
        List<ZEngineeringInfoBo> zEngineeringInfoBoList = bo.getzEngineeringNodeBo().getzEngineeringInfoBoList();
        System.out.println("#003 zEngineeringInfoBoList.size()=" +zEngineeringInfoBoList.size());
        for (int i = 0; i < zEngineeringInfoBoList.size(); i++) {
            System.out.println("#003 进入 for type=" + type);
            ZEngineeringInfoBo zEngineeringInfoBo = zEngineeringInfoBoList.get(i);
            if (!ObjectUtils.isEmpty(method.invoke(zEngineeringInfoBo))) {
                System.out.println("#003 if if if if (!ObjectUtils.isEmpty(method.invoke(zEngineeringInfoBo))) ");
                List<ZEngiineeringPhoto> list = new ArrayList<>();
                // 查询数据库中已存在的文件列表
                ZEngiineeringPhotoBo zEngiineeringPhotoBo = new ZEngiineeringPhotoBo();
                zEngiineeringPhotoBo.setCreateTime(zEngineeringInfoBo.getCreateTime());//
                zEngiineeringPhotoBo.setType(type);
                zEngiineeringPhotoBo.setParentId(zEngineeringInfoBo.getId());
                List<String> existingPhotos = photoService.queryList(zEngiineeringPhotoBo).stream().map(ZEngiineeringPhotoVo::getPicUrl).collect(Collectors.toList());
                // 插入前端传来的新文件
//                    for (ZFileBo pic : bo.getzEngineeringNodeBo().getzEngineeringInfoBo().getIronPipelineThickness()) {
                for (String pic : (List<String>)method.invoke(zEngineeringInfoBo)) {
                    if (!existingPhotos.contains(pic)) {
                        ZEngiineeringPhoto newPhoto = new ZEngiineeringPhoto();
    //                        newPhoto.setParentId(bo.getId());
                        newPhoto.setParentId(zEngineeringInfoBo.getId());
                        newPhoto.setPicUrl(pic);
                        newPhoto.setFileName(pic);
//                        newPhoto.setType("ironPipelineThickness");
                        newPhoto.setType(type);
                        newPhoto.setCreateBy(String.valueOf(LoginHelper.getUsername()));
                        newPhoto.setCreateTime(bo.getCreateTime());
                        newPhoto.setUpdateTime(new Date());
                        list.add(newPhoto);
                    }
                }
                System.out.println("#003 删除数据库中多余的文件");
                // 删除数据库中多余的文件
// List<String> photosToDelete = existingPhotos.stream().filter(pic -> !bo.getzEngineeringNodeBo().getzEngineeringInfoBo().getIronPipelineThickness().contains(pic)).collect(Collectors.toList());
                List<String> photosToDelete = existingPhotos.stream().filter(pic -> {
                    try {
                        return !((List<String>)(method.invoke(zEngineeringInfoBo))).contains(pic);
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException(e);
                    } catch (InvocationTargetException e) {
                        throw new RuntimeException(e);
                    }
                }).collect(Collectors.toList());
                if (!photosToDelete.isEmpty()) {
                    photosToDelete.forEach(item -> {
                        ZEngiineeringPhoto zEngiineeringPhoto = new ZEngiineeringPhoto();
//                        zEngiineeringPhoto.setType("ironPipelineThickness");
                        zEngiineeringPhoto.setType(type);
                        zEngiineeringPhoto.setPicUrl(item);
                        zEngiineeringPhoto.setCreateTime(bo.getCreateTime());
                        photoService.deleteWithValid(zEngiineeringPhoto);
                    });
                }
                // 批量插入新图片
                if (!list.isEmpty()) {
                    photoService.insertBatch(list);
                }
            }else {
                System.out.println("#003 else 删除数据库中多余的文件");
                // 查询数据库中已存在的文件列表
                ZEngiineeringPhotoBo zEngiineeringPhotoBo = new ZEngiineeringPhotoBo();
                zEngiineeringPhotoBo.setCreateTime(zEngineeringInfoBo.getCreateTime());//
                zEngiineeringPhotoBo.setParentId(zEngineeringInfoBo.getId());
                zEngiineeringPhotoBo.setType(type);
                List<String> existingPhotos = photoService.queryList(zEngiineeringPhotoBo).stream().map(ZEngiineeringPhotoVo::getPicUrl).collect(Collectors.toList());
                // 删除数据库中多余的文件
//                List<String> photosToDelete = existingPhotos.stream().filter(pic -> !bo.getzEngineeringNodeBo().getzEngineeringInfoBo().getIronPipelineThickness().contains(pic)).collect(Collectors.toList());
                List<String> photosToDelete = null;
                try {
                   photosToDelete = existingPhotos.stream().filter(pic -> {
                       try {
//                           return !method.invoke(zEngineeringInfoBo).toString().contains(pic);
                           return !((List<String>)(method.invoke(zEngineeringInfoBo))).contains(pic);
                       } catch (IllegalAccessException e) {
                           throw new RuntimeException(e);
                       } catch (InvocationTargetException e) {
                           throw new RuntimeException(e);
                       }
                   }).collect(Collectors.toList());
                }catch (Exception e){

                }
                if (!photosToDelete.isEmpty()) {
                    photosToDelete.forEach(item -> {
                        ZEngiineeringPhoto zEngiineeringPhoto = new ZEngiineeringPhoto();
                        zEngiineeringPhoto.setType("ironPipelineThickness");
                        zEngiineeringPhoto.setType(type);
                        zEngiineeringPhoto.setPicUrl(item);
                        zEngiineeringPhoto.setCreateTime(bo.getCreateTime());
                        photoService.deleteWithValid(zEngiineeringPhoto);
                    });
                }
            }
        }
    }

    /**
     * 修改,带气封堵施工
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateNodeByBo(ZEngineeringAirWallBo bo) {
        ZEngineeringAirWall update = BeanUtil.toBean(bo, ZEngineeringAirWall.class);
        boolean flag = baseMapper.updateById(update) > 0;

        // 查询数据库中已存在的主表创建时间
        ZEngineeringAirWallVo vo = baseMapper.selectVoById(bo.getId());
        if (flag) {
            if (!ObjectUtils.isEmpty(bo.getFiles())) {
                List<ZEngiineeringPhoto> list = new ArrayList<>();
                // 查询数据库中已存在的文件列表
                ZEngiineeringPhotoBo zEngiineeringPhotoBo = new ZEngiineeringPhotoBo();
                zEngiineeringPhotoBo.setParentId(update.getId());
                zEngiineeringPhotoBo.setCreateTime(vo.getCreateTime());
                List<String> existingPhotos = photoService.queryList(zEngiineeringPhotoBo).stream().map(ZEngiineeringPhotoVo::getPicUrl).collect(Collectors.toList());
                // 插入前端传来的新文件
                for (ZFileBo pic : bo.getFiles()) {
                    if (!existingPhotos.contains(pic.getUrl())) {
                        ZEngiineeringPhoto newPhoto = new ZEngiineeringPhoto();
                        newPhoto.setParentId(bo.getId());
                        newPhoto.setPicUrl(pic.getUrl());
                        newPhoto.setFileName(pic.getName());
                        newPhoto.setCreateBy(String.valueOf(LoginHelper.getUsername()));
                        newPhoto.setCreateTime(bo.getCreateTime());
                        newPhoto.setUpdateTime(new Date());
                        list.add(newPhoto);
                    }
                }
                // 删除数据库中多余的文件
                List<String> photosToDelete = existingPhotos.stream().filter(pic -> !bo.getFiles().stream().map(ZFileBo::getUrl).collect(Collectors.toList()).contains(pic)).collect(Collectors.toList());
                if (!photosToDelete.isEmpty()) {
                    photosToDelete.forEach(item -> {
                        ZEngiineeringPhoto zEngiineeringPhoto = new ZEngiineeringPhoto();
                        zEngiineeringPhoto.setPicUrl(item);
                        zEngiineeringPhoto.setCreateTime(bo.getCreateTime());
                        photoService.deleteWithValid(zEngiineeringPhoto);
                    });
                }
                // 批量插入新图片
                if (!list.isEmpty()) {
                    photoService.insertBatch(list);
                }
            }
            /**
             * 管道厚度
             */
            try {
                 batchUpdatePic(bo,  "getIronPipelineThickness", "ironPipelineThickness");
                 batchUpdatePic(bo,  "getIron4Welding", "iron4Welding");
                 batchUpdatePic(bo,  "getIronBypassWelding", "ironBypassWelding");
                 batchUpdatePic(bo,  "getIronHolesOpening", "ironHolesOpening");
                 batchUpdatePic(bo,  "getIronCleaning", "ironCleaning");
                 batchUpdatePic(bo,  "getIronNitrogenPurging", "ironNitrogenPurging");
                 batchUpdatePic(bo,  "getIronPipelineReplacing", "ironPipelineReplacing");
                 batchUpdatePic(bo,  "getIronLowerEndCap", "ironLowerEndCap");
                 batchUpdatePic(bo,  "getIronHomeworkDone", "ironHomeworkDone");
                 batchUpdatePic(bo,  "getIron4Aczoiling", "iron4Aczoiling");

                 batchUpdatePic(bo,  "getPeSaddleWelding", "peSaddleWelding");
                 batchUpdatePic(bo,  "getPeAirtightTest", "peAirtightTest");
                 batchUpdatePic(bo,  "getPeHolesOpening", "peHolesOpening");
                 batchUpdatePic(bo,  "getPeCleaning", "peCleaning");
                 batchUpdatePic(bo,  "getPePlugging", "pePlugging");
                 batchUpdatePic(bo,  "getPeCollisionOperating", "peCollisionOperating");
                 batchUpdatePic(bo,  "getPeAirrecovering", "peAirrecovering");
                 batchUpdatePic(bo,  "getPeHomeworkDone", "peHomeworkDone");
            }catch (Exception e){
                e.printStackTrace();
            }

            /**
             * 管道厚度
             */
//            if (!ObjectUtils.isEmpty(bo.getzEngineeringNodeBo().getzEngineeringInfoBo().getIronPipelineThickness())) {
            /*if (!ObjectUtils.isEmpty(bo.getzEngineeringNodeBo().getzEngineeringInfoBoList().get(0).getIronPipelineThickness())) {
                List<ZEngiineeringPhoto> list = new ArrayList<>();
                // 查询数据库中已存在的文件列表
                ZEngiineeringPhotoBo zEngiineeringPhotoBo = new ZEngiineeringPhotoBo();
                zEngiineeringPhotoBo.setCreateTime(bo.getzEngineeringNodeBo().getzEngineeringInfoBoList().get(0).getCreateTime());//
//                zEngiineeringPhotoBo.setParentId(update.getId());
                zEngiineeringPhotoBo.setParentId(bo.getzEngineeringNodeBo().getzEngineeringInfoBoList().get(0).getId());
                List<String> existingPhotos = photoService.queryList(zEngiineeringPhotoBo).stream().map(ZEngiineeringPhotoVo::getPicUrl).collect(Collectors.toList());
                // 插入前端传来的新文件
//                for (ZFileBo pic : bo.getzEngineeringNodeBo().getzEngineeringInfoBo().getIronPipelineThickness()) {
                for (String pic : bo.getzEngineeringNodeBo().getzEngineeringInfoBo().getIronPipelineThickness()) {
                    if (!existingPhotos.contains(pic)) {
                        ZEngiineeringPhoto newPhoto = new ZEngiineeringPhoto();
//                        newPhoto.setParentId(bo.getId());
                        newPhoto.setParentId(bo.getzEngineeringNodeBo().getzEngineeringInfoBoList().get(0).getId());
                        newPhoto.setPicUrl(pic);
                        newPhoto.setFileName(pic);
                        newPhoto.setType("ironPipelineThickness");
                        newPhoto.setCreateBy(String.valueOf(LoginHelper.getUsername()));
                        newPhoto.setCreateTime(bo.getCreateTime());
                        newPhoto.setUpdateTime(new Date());
                        list.add(newPhoto);
                    }
                }
                // 删除数据库中多余的文件
                List<String> photosToDelete = existingPhotos.stream().filter(pic -> !bo.getzEngineeringNodeBo().getzEngineeringInfoBo().getIronPipelineThickness().contains(pic)).collect(Collectors.toList());
                if (!photosToDelete.isEmpty()) {
                    photosToDelete.forEach(item -> {
                        ZEngiineeringPhoto zEngiineeringPhoto = new ZEngiineeringPhoto();
                        zEngiineeringPhoto.setType("ironPipelineThickness");
                        zEngiineeringPhoto.setPicUrl(item);
                        zEngiineeringPhoto.setCreateTime(bo.getCreateTime());
                        photoService.deleteWithValid(zEngiineeringPhoto);
                    });
                }
                // 批量插入新图片
                if (!list.isEmpty()) {
                    photoService.insertBatch(list);
                }
            }else {
                List<ZEngiineeringPhoto> list = new ArrayList<>();
                // 查询数据库中已存在的文件列表
                ZEngiineeringPhotoBo zEngiineeringPhotoBo = new ZEngiineeringPhotoBo();
                zEngiineeringPhotoBo.setCreateTime(bo.getzEngineeringNodeBo().getzEngineeringInfoBoList().get(0).getCreateTime());//
//                zEngiineeringPhotoBo.setParentId(update.getId());
                zEngiineeringPhotoBo.setParentId(bo.getzEngineeringNodeBo().getzEngineeringInfoBoList().get(0).getId());
                List<String> existingPhotos = photoService.queryList(zEngiineeringPhotoBo).stream().map(ZEngiineeringPhotoVo::getPicUrl).collect(Collectors.toList());
                // 删除数据库中多余的文件
                List<String> photosToDelete = existingPhotos.stream().filter(pic -> !bo.getzEngineeringNodeBo().getzEngineeringInfoBo().getIronPipelineThickness().contains(pic)).collect(Collectors.toList());
                if (!photosToDelete.isEmpty()) {
                    photosToDelete.forEach(item -> {
                        ZEngiineeringPhoto zEngiineeringPhoto = new ZEngiineeringPhoto();
                        zEngiineeringPhoto.setType("ironPipelineThickness");
                        zEngiineeringPhoto.setPicUrl(item);
                        zEngiineeringPhoto.setCreateTime(bo.getCreateTime());
                        photoService.deleteWithValid(zEngiineeringPhoto);
                    });
                }
            }*/
        }
        if (!ObjectUtils.isEmpty(bo.getzEngineeringNodeBo())){
            List<ZEngineeringInfoBo> engineeringInfoBoList1 = bo.getzEngineeringNodeBo().getzEngineeringInfoBoList();
            if (engineeringInfoBoList1 != null && !engineeringInfoBoList1.isEmpty()) {
                nodeService.updateMuch(bo.getzEngineeringNodeBo());
            }
        }
        //        bo.getzEngineeringNodeBoList().stream().forEach(item->{
        //            item.setCivliId(bo.getId());
        //            item.setCreateTime(bo.getCreateTime());
        //            item.setType(bo.getType());
        //            izEngineeringNodeService.updateMuch(item);
        //        });
        return flag;
    }


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

    /**
     * 批量删除,带气封堵施工
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {

        ids.forEach(item -> {
            // 查询数据库中已存在的主表创建时间
            ZEngineeringAirWallVo vo = baseMapper.selectVoById(item);
            // 查询数据库中已存在的文件列表
            ZEngiineeringPhotoBo zEngiineeringPhotoBo = new ZEngiineeringPhotoBo();
            zEngiineeringPhotoBo.setParentId(item);
            zEngiineeringPhotoBo.setCreateTime(vo.getCreateTime());
            // 将查询出的url放入existingPhotos
            List<String> existingPhotos = photoService.queryList(zEngiineeringPhotoBo).stream().map(ZEngiineeringPhotoVo::getPicUrl).collect(Collectors.toList());
            // 若存在 则进行删除
            if (!existingPhotos.isEmpty()) {
                existingPhotos.forEach(i -> {
                    photoService.deleteWithValidByurls(i);
                });
            }
            // 初始化工程节点对象
//            ZEngineeringAirWallVo vo = baseMapper.selectVoById(item);
            ZEngineeringNodeBo zEngineeringNodeBo = new ZEngineeringNodeBo();
            zEngineeringNodeBo.setCreateTime(vo.getCreateTime());
            zEngineeringNodeBo.setCivliId(vo.getId());
            //  查询数据库中已存在的工程节点
            List<ZEngineeringNodeBo> zEngineeringNodeBos = nodeService.queryListDetails(zEngineeringNodeBo);
            // 若存在 则进行删除
            if (!zEngineeringNodeBos.isEmpty()) {
                zEngineeringNodeBos.forEach(ite -> nodeService.delete(ite));
            }
        });
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public ZEngineeringAirWallVo query(String id, String type) {
        ZEngineeringAirWallVo zEngineeringAirWallVo = baseMapper.selectVoById(id);
        ZEngineeringNodeBo zEngineeringNodeBo = new ZEngineeringNodeBo();
        zEngineeringNodeBo.setCreateTime(zEngineeringAirWallVo.getCreateTime());
        zEngineeringNodeBo.setCivliId(zEngineeringAirWallVo.getId());
        System.out.println("type===" + type);
        zEngineeringNodeBo.setType(type);
//        zEngineeringNodeBo.setType("trench_construction");
        zEngineeringAirWallVo.setZEngineeringNodeBo(izEngineeringNodeService.query(zEngineeringNodeBo));
        return zEngineeringAirWallVo;
    }

    @Override
    public List<ZEngineeringIndustryTypeVo> queryNameByType(ZEngineeringAirWallBo bo) {
        LambdaQueryWrapper<ZEngineeringAirWall> zEngineeringAirWallBoLambdaQueryWrapper = buildListByType(bo);
        zEngineeringAirWallBoLambdaQueryWrapper.orderByDesc(ZEngineeringAirWall::getCreateTime);
//        List<ZEngineeringAirWall> zEngineeringAirWalVos = baseMapper.selectList(zEngineeringAirWallBoLambdaQueryWrapper);
//        List<ZEngineeringIndustryTypeVo> zEngineeringAirWalTypeVos = zEngineeringAirWalVos.stream().map(ZEngineeringIndustryTypeVo::new).collect(Collectors.toList());
//        return zEngineeringAirWalTypeVos;
        return new ArrayList<ZEngineeringIndustryTypeVo>();
    }

    private LambdaQueryWrapper<ZEngineeringAirWall> buildListByType(ZEngineeringAirWallBo bo) {

        LambdaQueryWrapper<ZEngineeringAirWall> queryWrapper = new LambdaQueryWrapper<ZEngineeringAirWall>();
        queryWrapper.orderByDesc(ZEngineeringAirWall::getCreateTime);
//        queryWrapper.eq(StringUtils.isNotBlank(bo.getEnginType()), ZEngineeringIndustry::getEnginType, bo.getEnginType());
//        queryWrapper.eq(StringUtils.isNotBlank(bo.getType()), ZEngineeringIndustry::getType, bo.getType());
//        if (!"null".equals(bo.getEnginClassification())) {
//            queryWrapper.like(StringUtils.isNotBlank(bo.getEnginClassification()), ZEngineeringIndustry::getEnginClassification, bo.getEnginClassification());
//        }
        if (!"null".equals(bo.getEnginName())) {
            queryWrapper.like(StringUtils.isNotBlank(bo.getEnginName()), ZEngineeringAirWall::getEnginName, bo.getEnginName());
        }

        return queryWrapper;
    }


}
