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.annotation.DynamicName;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.zdsz.domain.ZEngiineeringPhoto;
import com.ruoyi.zdsz.domain.ZEngineeringInfo;
import com.ruoyi.zdsz.domain.bo.ZEngineeringInfoBo;
import com.ruoyi.zdsz.domain.bo.ZEngineeringMaterialBo;
import com.ruoyi.zdsz.domain.bo.ZEngineeringNodeBo;
import com.ruoyi.zdsz.domain.bo.ZEngineeringReviewBo;
import com.ruoyi.zdsz.domain.vo.ZEngineeringInfoVo;
import com.ruoyi.zdsz.mapper.ZEngineeringInfoMapper;
import com.ruoyi.zdsz.service.IZEngiineeringPhotoService;
import com.ruoyi.zdsz.service.IZEngineeringInfoService;
import com.ruoyi.zdsz.service.IZEngineeringMaterialService;
import com.ruoyi.zdsz.service.IZEngineeringReviewService;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 工程详情Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-27
 */
@RequiredArgsConstructor
@Service
public class ZEngineeringInfoServiceImpl implements IZEngineeringInfoService {

    private final ZEngineeringInfoMapper baseMapper;
    @Resource
    private IZEngineeringMaterialService izEngineeringMaterialService;
    @Resource
    private IZEngiineeringPhotoService izEngiineeringPhotoService;
    @Resource
    private ISysUserService sysUserService;
    @Resource
    private IZEngineeringReviewService izEngineeringReviewService;
    /**
     * 查询工程详情
     */
    @Override
    public ZEngineeringInfoVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    @Override
    @DynamicName(spel = "#bo.createTime")
    public List<ZEngineeringInfoBo> query(ZEngineeringNodeBo bo) {
        LambdaQueryWrapper<ZEngineeringInfo> lqw = Wrappers.lambdaQuery();
        List<ZEngineeringInfo> zEngineeringInfoVoList= baseMapper.selectList(lqw.eq(StringUtils.isNotBlank(bo.getId()), ZEngineeringInfo::getEngInfoId, bo.getId()));
        List<ZEngineeringInfoBo> zEngineeringInfoBoList=new ArrayList<>();
        zEngineeringInfoVoList.stream().forEach(item->{
            ZEngineeringInfoBo zEngineeringInfoBo1 = BeanUtil.toBean(item, ZEngineeringInfoBo.class);
            SysUser sysUser=sysUserService.selectUserByUserName(item.getCreateBy());
            zEngineeringInfoBo1.setHeadName(sysUser.getNickName());
            zEngineeringInfoBo1.setConstructUser(sysUser.getNickName());
            zEngineeringInfoBo1.setHeadPhone(sysUser.getPhonenumber());
            zEngineeringInfoBo1.setConstructPhone(sysUser.getPhonenumber());
            List<String> photoList=izEngiineeringPhotoService.queryList(zEngineeringInfoBo1);
            List<ZEngineeringMaterialBo> zEngineeringMaterialBo=izEngineeringMaterialService.query(zEngineeringInfoBo1);
            zEngineeringInfoBo1.setzEngiineeringPhotoBoList(photoList);
            zEngineeringInfoBo1.setzEngineeringMaterialBo(zEngineeringMaterialBo);
            ZEngineeringReviewBo zEngineeringReviewbo=izEngineeringReviewService.query(bo);
            zEngineeringInfoBo1.setzEngineeringReviewBo(zEngineeringReviewbo);
            zEngineeringInfoBoList.add(zEngineeringInfoBo1);
        });

        return zEngineeringInfoBoList;
    }

    /**
     *
     */
    @Override
    public TableDataInfo<ZEngineeringInfoVo> queryPageList(ZEngineeringInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZEngineeringInfo> lqw = buildQueryWrapper(bo);
        Page<ZEngineeringInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询工程详情列表
     */
    @Override
    public List<ZEngineeringInfoVo> queryList(ZEngineeringInfoBo bo) {
        LambdaQueryWrapper<ZEngineeringInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZEngineeringInfo> buildQueryWrapper(ZEngineeringInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZEngineeringInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getEngInfoId()), ZEngineeringInfo::getEngInfoId, bo.getEngInfoId());
        lqw.eq(StringUtils.isNotBlank(bo.getConstructAddre()), ZEngineeringInfo::getConstructAddre, bo.getConstructAddre());
        lqw.eq(StringUtils.isNotBlank(bo.getConstructAccordingDrawings()), ZEngineeringInfo::getConstructAccordingDrawings, bo.getConstructAccordingDrawings());
        lqw.eq(StringUtils.isNotBlank(bo.getSegmentedCompressionQualified()), ZEngineeringInfo::getSegmentedCompressionQualified, bo.getSegmentedCompressionQualified());
        lqw.eq(bo.getBackfillTime() != null, ZEngineeringInfo::getBackfillTime, bo.getBackfillTime());
        lqw.eq(StringUtils.isNotBlank(bo.getConstructPhone()), ZEngineeringInfo::getConstructPhone, bo.getConstructPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getConstructUser()), ZEngineeringInfo::getConstructUser, bo.getConstructUser());
        lqw.eq(bo.getConstructTime() != null, ZEngineeringInfo::getConstructTime, bo.getConstructTime());
        lqw.like(StringUtils.isNotBlank(bo.getHeadName()), ZEngineeringInfo::getHeadName, bo.getHeadName());
        lqw.eq(StringUtils.isNotBlank(bo.getHeadPhone()), ZEngineeringInfo::getHeadPhone, bo.getHeadPhone());
        return lqw;
    }

    /**
     * 新增工程详情
     */
    @Override
    public Boolean insertByBo(ZEngineeringInfoBo bo) {
        ZEngineeringInfo add = BeanUtil.toBean(bo, ZEngineeringInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }
    /**
     *  新增工程详情(节点使用)
     * @param bo
     * @return ZEngineeringInfo
     */
    @Override
    @DynamicName(spel = "#bo.createTime")
    @Transactional(rollbackFor=Exception.class)

    public ZEngineeringInfo insert(ZEngineeringInfoBo bo) {
        ZEngineeringInfo add = BeanUtil.toBean(bo, ZEngineeringInfo.class);
        validEntityBeforeSave(add);
        add.setConstructUser(String.valueOf(LoginHelper.getLoginUser().getUserId()));
        add.setHeadName(String.valueOf(LoginHelper.getLoginUser().getUserId()));
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        if (bo.getzEngineeringMaterialBo()!=null&&bo.getzEngineeringMaterialBo().size()>0)
        {
        bo.getzEngineeringMaterialBo().stream().forEach(item->{
            item.setDetailsId(add.getId());
            item.setCreateTime(bo.getCreateTime());
            item.setUpdateTime(bo.getUpdateTime());
            izEngineeringMaterialService.insert(item);
        });
        }
        List <ZEngiineeringPhoto> photoList=new ArrayList();
            bo.getzEngiineeringPhotoBoList().stream().forEach(item->{
            ZEngiineeringPhoto zEngiineeringPhoto=new ZEngiineeringPhoto();
            zEngiineeringPhoto.setParentId(add.getId());
            zEngiineeringPhoto.setPicUrl(item);
            zEngiineeringPhoto.setCreateTime(bo.getCreateTime());
            zEngiineeringPhoto.setUpdateTime(bo.getUpdateTime());
                photoList.add(zEngiineeringPhoto);
        });
        izEngiineeringPhotoService.insertBatch(photoList);
        return add;
    }

    /**
     * 修改工程详情
     */
    @Override
    public Boolean updateByBo(ZEngineeringInfoBo bo) {
        ZEngineeringInfo update = BeanUtil.toBean(bo, ZEngineeringInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    @Override
    @DynamicName(spel = "#bo.createTime")
    public Boolean update(ZEngineeringInfoBo bo) {
        ZEngineeringInfo update = BeanUtil.toBean(bo, ZEngineeringInfo.class);
        validEntityBeforeSave(update);
        List <ZEngiineeringPhoto> photoList=new ArrayList();
        bo.getzEngiineeringPhotoBoList().stream().forEach(item->{
            ZEngiineeringPhoto zEngiineeringPhoto=new ZEngiineeringPhoto();
            zEngiineeringPhoto.setParentId(update.getId());
            zEngiineeringPhoto.setPicUrl(item);
            zEngiineeringPhoto.setCreateTime(bo.getCreateTime());
            photoList.add(zEngiineeringPhoto);
        });
        izEngiineeringPhotoService.updateBatch(photoList);
        izEngineeringMaterialService.updateList(bo);
        return baseMapper.updateById(update) > 0;
    }

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

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

    @Override
    @DynamicName(spel = "#bo.createTime")
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(ZEngineeringNodeBo bo) {
        LambdaQueryWrapper<ZEngineeringInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getId()), ZEngineeringInfo::getEngInfoId, bo.getId());
        List<ZEngineeringInfo> zEngineeringInfo= baseMapper.selectList(lqw);
        zEngineeringInfo.stream().forEach(item->{
            ZEngineeringInfoBo zEngineeringInfobo = BeanUtil.toBean(item, ZEngineeringInfoBo.class);
            izEngiineeringPhotoService.delete(zEngineeringInfobo);
            izEngineeringMaterialService.delete(zEngineeringInfobo);
            baseMapper.deleteById(item.getId());
        });
        return null;
    }

    @Override
    public List<ZEngineeringMaterialBo> getzEngineeringMaterialBoList(ZEngineeringNodeBo bo) {
        LambdaQueryWrapper<ZEngineeringInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getId()), ZEngineeringInfo::getEngInfoId, bo.getId());
        List<ZEngineeringInfo> zEngineeringInfo= baseMapper.selectList(lqw);
        List<ZEngineeringMaterialBo> zEngineeringMaterialBoList=new ArrayList<>();

        zEngineeringInfo.stream().forEach(item->{
            ZEngineeringInfoBo zEngineeringInfoBo = BeanUtil.toBean(item, ZEngineeringInfoBo.class);
            zEngineeringMaterialBoList.addAll(izEngineeringMaterialService.query(zEngineeringInfoBo));
        });
        return zEngineeringMaterialBoList;
    }
}
