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.bo.ZEngineeringNodeBo;
import com.ruoyi.zdsz.domain.vo.ZEngineeringCivilVo;
import com.ruoyi.zdsz.domain.vo.ZEngineeringIndustryVo;
import com.ruoyi.zdsz.service.IZEngineeringNodeService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zdsz.domain.bo.ZTouchOperationEngineeringBo;
import com.ruoyi.zdsz.domain.vo.ZTouchOperationEngineeringVo;
import com.ruoyi.zdsz.domain.ZTouchOperationEngineering;
import com.ruoyi.zdsz.mapper.ZTouchOperationEngineeringMapper;
import com.ruoyi.zdsz.service.IZTouchOperationEngineeringService;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 碰口作业Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-12
 */
@RequiredArgsConstructor
@Service
public class ZTouchOperationEngineeringServiceImpl implements IZTouchOperationEngineeringService {

    private final ZTouchOperationEngineeringMapper baseMapper;

    @Override
    public ZTouchOperationEngineeringVo query(Long id,String type) {
        ZTouchOperationEngineeringVo zTouchOperationEngineeringVo = baseMapper.selectVoById(id);
        ZEngineeringNodeBo zEngineeringNodeBo = new ZEngineeringNodeBo();
        zEngineeringNodeBo.setCreateTime(zTouchOperationEngineeringVo.getCreateTime());
        zEngineeringNodeBo.setCivliId(zTouchOperationEngineeringVo.getId().toString());
        zEngineeringNodeBo.setType(type);
        zTouchOperationEngineeringVo.setzEngineeringNodeBo(zEngineeringNodeService.query(zEngineeringNodeBo));
        return zTouchOperationEngineeringVo;
    }

    /**
     * 查询碰口作业
     */
    @Override
    public ZTouchOperationEngineeringVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    @Autowired
    private IZEngineeringNodeService zEngineeringNodeService;

    /**
     * 查询碰口作业列表
     */
    @Override
    public TableDataInfo<ZTouchOperationEngineeringVo> queryPageList(ZTouchOperationEngineeringBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZTouchOperationEngineering> lqw = buildQueryWrapper(bo);
        Page<ZTouchOperationEngineeringVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Map<Long, Object> ReviewList = zEngineeringNodeService.ReViewPKList(result.getRecords());
        result.getRecords().forEach(item-> {
            item.setNodeReViewStateList(ReviewList.get(item.getId()));
        });
        return TableDataInfo.build(result);
    }

    /**
     * 查询碰口作业列表
     */
    @Override
    public List<ZTouchOperationEngineeringVo> queryList(ZTouchOperationEngineeringBo bo) {
        LambdaQueryWrapper<ZTouchOperationEngineering> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZTouchOperationEngineering> buildQueryWrapper(ZTouchOperationEngineeringBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZTouchOperationEngineering> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getModeOperation()), ZTouchOperationEngineering::getModeOperation, bo.getModeOperation());
        lqw.like(StringUtils.isNotBlank(bo.getEnginName()), ZTouchOperationEngineering::getEnginName, bo.getEnginName());
        lqw.like(StringUtils.isNotBlank(bo.getLocations()), ZTouchOperationEngineering::getLocations, bo.getLocations());
        lqw.orderByDesc(ZTouchOperationEngineering::getCreateTime);
        return lqw;
    }

    /**
     * 新增碰口作业
     */
    @Override
    public Boolean insertByBo(ZTouchOperationEngineeringBo bo) {
        ZTouchOperationEngineering add = BeanUtil.toBean(bo, ZTouchOperationEngineering.class);
        validEntityBeforeSave(add);
        if (ObjectUtils.isEmpty(add.getId())){
            boolean flag = baseMapper.insert(add) > 0;
            if (flag) {
                bo.setId(add.getId());
            }
            return flag;
        }else {
            try {
                for (ZEngineeringNodeBo zEngineeringNodeBo : bo.getzEngineeringNodeBoList()) {
                    zEngineeringNodeBo.setCivliId(add.getId().toString());
                    zEngineeringNodeBo.setCreateTime(add.getCreateTime());
                    zEngineeringNodeBo.setType(zEngineeringNodeBo.getType());
                    ZEngineeringNodeBo query = zEngineeringNodeService.query(zEngineeringNodeBo);
                    if (!ObjectUtils.isEmpty(query)) {
                        zEngineeringNodeBo.setId(query.getId());
                    }
                    zEngineeringNodeService.insert(zEngineeringNodeBo);
                }
                return true;
            }catch (Exception e){
                return false;
            }
        }
    }

    /**
     * 修改碰口作业
     */
    @Override
    public Boolean updateByBo(ZTouchOperationEngineeringBo bo) {
        ZTouchOperationEngineering update = BeanUtil.toBean(bo, ZTouchOperationEngineering.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            ZTouchOperationEngineeringVo zTouchOperationEngineeringVo = baseMapper.selectVoById(update.getId());
            ZEngineeringNodeBo zEngineeringNodeBo = bo.getzEngineeringNodeBo();
                if (!ObjectUtils.isEmpty(zEngineeringNodeBo)) {
                    zEngineeringNodeBo.setCivliId(zTouchOperationEngineeringVo.getId().toString());
                    zEngineeringNodeBo.setCreateTime(zTouchOperationEngineeringVo.getCreateTime());
                    if (!ObjectUtils.isEmpty(zEngineeringNodeBo.getzEngineeringInfoBoList()))
                        zEngineeringNodeService.updateMuch(zEngineeringNodeBo);
                }
        }
        return flag;
    }

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

    /**
     * 批量删除碰口作业
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
