package com.ruoyi.zdsz.service.impl;

import cn.hutool.core.bean.BeanUtil;
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.zdsz.domain.ZEngineeringInfrastructure;
import com.ruoyi.zdsz.domain.bo.ZEngineeringInfoBo;
import com.ruoyi.zdsz.domain.bo.ZEngineeringInfrastructureBo;
import com.ruoyi.zdsz.domain.bo.ZEngineeringNodeBo;
import com.ruoyi.zdsz.domain.bo.ZEngineeringReviewBo;
import com.ruoyi.zdsz.domain.vo.ZEngineeringInfrastructureSelectVo;
import com.ruoyi.zdsz.domain.vo.ZEngineeringInfrastructureVo;
import com.ruoyi.zdsz.domain.vo.ZEngineeringNodeVo;
import com.ruoyi.zdsz.domain.vo.ZEngineeringReviewVo;
import com.ruoyi.zdsz.mapper.ZEngineeringInfrastructureMapper;
import com.ruoyi.zdsz.service.IZEngineeringInfrastructureService;
import com.ruoyi.zdsz.service.IZEngineeringNodeService;
import com.ruoyi.zdsz.service.IZEngineeringReviewService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * 基建工程Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-03
 */
@RequiredArgsConstructor
@Service
public class ZEngineeringInfrastructureServiceImpl implements IZEngineeringInfrastructureService {

    @Resource
    private final ZEngineeringInfrastructureMapper baseMapper;

    @Resource
    private final IZEngineeringNodeService izEngineeringNodeService;

    @Resource
    private final IZEngineeringReviewService izEngineeringReviewService;

    /**
     * 查询基建工程
     */
    @Override
    public ZEngineeringInfrastructureVo queryById(String id){
        ZEngineeringInfrastructureVo vo = baseMapper.selectVoById(id);
        ZEngineeringNodeBo zEngineeringNodeBo = new ZEngineeringNodeBo();
        zEngineeringNodeBo.setCreateTime(vo.getCreateTime());
        zEngineeringNodeBo.setCivliId(vo.getId());
        vo.setzEngineeringNodeBoList(izEngineeringNodeService.queryListDetails(zEngineeringNodeBo));
        List<ZEngineeringNodeBo> nodeList = vo.getzEngineeringNodeBoList();
        if(nodeList != null && nodeList.size() > 0){
            for (ZEngineeringNodeBo node : nodeList) {
                String type = node.getType();
//                if(node.getType().equals("construction_entrance")){
                if(type.equals("construction_hydroelectricity") || type.equals("construction_waterproof")){ // 水电施工、防水
                    ZEngineeringReviewBo reviewBo = izEngineeringReviewService.query(node);
                    boolean review = (reviewBo!=null) && (reviewBo.getReviewStatus().equals("1"));
                    node.setReviewStatus(review?"1":"0"); // 通过-1 未通过、未审核-0 无需审核-null
                }
            } // else null
        }
        return vo;
    }

    /**
     * 查询基建工程历史
     */
    @Override
    public ZEngineeringInfrastructureVo queryByIdType(String id, String type){
        ZEngineeringInfrastructureVo vo = baseMapper.selectVoById(id);
        ZEngineeringNodeBo zEngineeringNodeBo = new ZEngineeringNodeBo();
        zEngineeringNodeBo.setCreateTime(vo.getCreateTime());
        zEngineeringNodeBo.setCivliId(vo.getId());
        zEngineeringNodeBo.setType(type);
//        List<ZEngineeringNodeVo> zEngineeringNodeVoList = izEngineeringNodeService.queryList(zEngineeringNodeBo);
        List<ZEngineeringNodeBo> zEngineeringNodeBoList = izEngineeringNodeService.queryListDetails(zEngineeringNodeBo);
        for (ZEngineeringNodeBo node : zEngineeringNodeBoList) {
            if(type.equals("construction_hydroelectricity") || type.equals("construction_waterproof")){ // 水电施工、防水
                node.setCreateTime(new Date());
                ZEngineeringReviewBo reviewBo = izEngineeringReviewService.query(node);
                boolean review = (reviewBo!=null) && (reviewBo.getReviewStatus().equals("1"));
                node.setReviewStatus(review?"1":"0"); // 通过-1 未通过、未审核-0 无需审核-null
            }
        }

//        List<ZEngineeringNodeBo> zEngineeringNodeBoList = new ArrayList<>();
//        for (ZEngineeringNodeVo zEngineeringNodeVo : zEngineeringNodeVoList) {
//            ZEngineeringNodeBo bo = BeanUtil.toBean(zEngineeringNodeVo, ZEngineeringNodeBo.class);
//            if(type.equals("construction_hydroelectricity") || type.equals("construction_waterproof")){ // 水电施工、防水
//                bo.setCreateTime(new Date());
//                ZEngineeringReviewBo reviewBo = izEngineeringReviewService.query(bo);
//                boolean review = (reviewBo!=null) && (reviewBo.getReviewStatus().equals("1"));
//                bo.setReviewStatus(review?"1":"0"); // 通过-1 未通过、未审核-0 无需审核-null
//            }
//            ZEngineeringInfoBo zEngineeringInfoBo = new ZEngineeringInfoBo();
//            zEngineeringInfoBoS
//            zEngineeringNodeBoList.add(bo);
//        }
        vo.setzEngineeringNodeBoList(zEngineeringNodeBoList);
        return vo;
    }

    /**
     * 查询基建工程下拉
     */
    @Override
    public List<ZEngineeringInfrastructureSelectVo> select(ZEngineeringInfrastructureBo bo) {
        LambdaQueryWrapper<ZEngineeringInfrastructure> lqw = buildQueryWrapper(bo);
        List<ZEngineeringInfrastructureVo> result = baseMapper.selectVoList(lqw);
        List<ZEngineeringInfrastructureSelectVo> list = new ArrayList();
        for (ZEngineeringInfrastructureVo vo: result) {
            ZEngineeringInfrastructureSelectVo select = BeanUtil.toBean(vo, ZEngineeringInfrastructureSelectVo.class);
            list.add(select);
        }
        return list;
    }

    /**
     * 查询基建工程列表
     */
    @Override
    public TableDataInfo<ZEngineeringInfrastructureVo> queryPageList(ZEngineeringInfrastructureBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZEngineeringInfrastructure> lqw = buildQueryWrapper(bo);
        Page<ZEngineeringInfrastructureVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询基建工程列表
     */
    @Override
    public List<ZEngineeringInfrastructureVo> queryList(ZEngineeringInfrastructureBo bo) {
        LambdaQueryWrapper<ZEngineeringInfrastructure> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZEngineeringInfrastructure> buildQueryWrapper(ZEngineeringInfrastructureBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZEngineeringInfrastructure> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getEnginName()), ZEngineeringInfrastructure::getEnginName, bo.getEnginName());
        lqw.eq(StringUtils.isNotBlank(bo.getEnginAddre()), ZEngineeringInfrastructure::getEnginAddre, bo.getEnginAddre());
        lqw.eq(StringUtils.isNotBlank(bo.getConstructUnit()), ZEngineeringInfrastructure::getConstructUnit, bo.getConstructUnit());
        lqw.eq(bo.getStartTime() != null, ZEngineeringInfrastructure::getStartTime, bo.getStartTime());
        lqw.eq(StringUtils.isNotBlank(bo.getPlannedDuration()), ZEngineeringInfrastructure::getPlannedDuration, bo.getPlannedDuration());
        lqw.eq(bo.getCompletedTime() != null, ZEngineeringInfrastructure::getCompletedTime, bo.getCompletedTime());
        lqw.eq(StringUtils.isNotBlank(bo.getEnginContent()), ZEngineeringInfrastructure::getEnginContent, bo.getEnginContent());
        return lqw;
    }

    /**
     * 新增基建工程
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(ZEngineeringInfrastructureBo bo) {
        ZEngineeringInfrastructure add = BeanUtil.toBean(bo, ZEngineeringInfrastructure.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            List<ZEngineeringNodeBo> nodeBoList = bo.getzEngineeringNodeBoList();
            if(nodeBoList != null && nodeBoList.size() != 0){
                for (ZEngineeringNodeBo nodeBo:nodeBoList) {
                    bo.setId(add.getId());
                    ZEngineeringNodeBo zEngineeringNodeBo = nodeBo;
                    zEngineeringNodeBo.setCivliId(add.getId());
                    zEngineeringNodeBo.setCreateTime(add.getCreateTime());
                    izEngineeringNodeService.insert(zEngineeringNodeBo);
                }
            }else {
                bo.setId(add.getId());
                ZEngineeringNodeBo zEngineeringNodeBo = bo.getzEngineeringNodeBo();
                zEngineeringNodeBo.setCivliId(add.getId());
                zEngineeringNodeBo.setCreateTime(add.getCreateTime());
                izEngineeringNodeService.insert(zEngineeringNodeBo);
            }
        }
        return flag;
    }

    /**
     * 新增基建工程审核
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean review(ZEngineeringReviewBo bo) {
        ZEngineeringReviewBo zEngineeringReviewBo = new ZEngineeringReviewBo();
        zEngineeringReviewBo.setEngInfoId(bo.getEngInfoId());
        List<ZEngineeringReviewVo> voList = izEngineeringReviewService.queryList(zEngineeringReviewBo);
        if(voList.size() == 0){
            return izEngineeringReviewService.insert(bo);
        }else {
            bo.setId(voList.get(0).getId());
            return izEngineeringReviewService.updateByBo(bo);
        }
    }

    /**
     * 修改基建工程
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(ZEngineeringInfrastructureBo bo) {
        ZEngineeringInfrastructure update = BeanUtil.toBean(bo, ZEngineeringInfrastructure.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            ZEngineeringInfrastructureVo vo = baseMapper.selectVoById(update.getId());

//            if (flag) {
//                ZEngineeringNodeBo zEngineeringNodeBo = bo.getzEngineeringNodeBo();
//                zEngineeringNodeBo.setCivliId(vo.getId());
//                zEngineeringNodeBo.setCreateTime(vo.getCreateTime());
//                izEngineeringNodeService.update(zEngineeringNodeBo);
//                return flag;
//            }

            List<ZEngineeringNodeBo> nodeBoList = bo.getzEngineeringNodeBoList();
            if(nodeBoList != null && nodeBoList.size() != 0){
                for (ZEngineeringNodeBo nodeBo:nodeBoList) {
                    ZEngineeringNodeBo zEngineeringNodeBo = nodeBo;
                    zEngineeringNodeBo.setCivliId(vo.getId());
                    zEngineeringNodeBo.setCreateTime(vo.getCreateTime());
                    if(zEngineeringNodeBo.getId() != null){
                        izEngineeringNodeService.update(zEngineeringNodeBo);
                    }else {
                        izEngineeringNodeService.insert(zEngineeringNodeBo);
                    }
                }
            }else {
//                ZEngineeringInfrastructureVo vo = baseMapper.selectVoById(update.getId());
                ZEngineeringNodeBo zEngineeringNodeBo = bo.getzEngineeringNodeBo();
                zEngineeringNodeBo.setCivliId(vo.getId());
                zEngineeringNodeBo.setCreateTime(vo.getCreateTime());
                if(zEngineeringNodeBo.getId() != null){
                    izEngineeringNodeService.update(zEngineeringNodeBo);
                }else {
                    izEngineeringNodeService.insert(zEngineeringNodeBo);
                }
            }
        }
        return flag;
    }

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

    /**
     * 批量删除基建工程
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        ids.forEach(item -> {
            ZEngineeringInfrastructureVo vo = baseMapper.selectVoById(item);
            if(vo==null)
            {return;}
            ZEngineeringNodeBo zEngineeringNodeBo = new ZEngineeringNodeBo();
            zEngineeringNodeBo.setCreateTime(vo.getCreateTime());
            zEngineeringNodeBo.setCivliId(vo.getId());
            List<ZEngineeringNodeBo> zEngineeringNodeBos = izEngineeringNodeService.queryListDetails(zEngineeringNodeBo);
            zEngineeringNodeBos.forEach(ite -> izEngineeringNodeService.delete(ite));
        }); return baseMapper.deleteBatchIds(ids) > 0;
    }
}
