package com.ruoyi.zdsz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.BeanCopyUtils;
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.ZEngineeringCivil;
import com.ruoyi.zdsz.domain.bo.ZEngineeringCivilBo;
import com.ruoyi.zdsz.domain.bo.ZEngineeringNodeBo;
import com.ruoyi.zdsz.domain.vo.MaterialStatisticsListVo;
import com.ruoyi.zdsz.service.IZEngineeringInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.ruoyi.zdsz.domain.bo.ZMaterialStatisticsBo;
import com.ruoyi.zdsz.domain.vo.ZMaterialStatisticsVo;
import com.ruoyi.zdsz.domain.ZMaterialStatistics;
import com.ruoyi.zdsz.mapper.ZMaterialStatisticsMapper;
import com.ruoyi.zdsz.service.IZMaterialStatisticsService;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * 用料统计Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-27
 */
@RequiredArgsConstructor
@Service
public class ZMaterialStatisticsServiceImpl implements IZMaterialStatisticsService {

    private final ZMaterialStatisticsMapper baseMapper;

    /**
     * 查询用料统计
     */
    @Override
    public ZMaterialStatisticsVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用料统计列表
     */
    @Override
    public TableDataInfo<ZMaterialStatisticsVo> queryPageList(ZMaterialStatisticsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZMaterialStatistics> lqw = buildQueryWrapper(bo);
        Page<ZMaterialStatisticsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用料统计列表
     */
    @Override
    public List<MaterialStatisticsListVo> queryList(ZMaterialStatisticsBo bo) {
        LambdaQueryWrapper<ZMaterialStatistics> lqw = buildQueryWrapper(bo);
        return baseMapper.getZMaterialStatistics(bo);
    }

    private LambdaQueryWrapper<ZMaterialStatistics> buildQueryWrapper(ZMaterialStatisticsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZMaterialStatistics> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getAreaId()), ZMaterialStatistics::getAreaId, bo.getAreaId());
        lqw.eq(StringUtils.isNotBlank(bo.getBuildingId()), ZMaterialStatistics::getBuildingId, bo.getBuildingId());
        lqw.eq(StringUtils.isNotBlank(bo.getUnitId()), ZMaterialStatistics::getUnitId, bo.getUnitId());
        lqw.eq(StringUtils.isNotBlank(bo.getHouseId()), ZMaterialStatistics::getHouseId, bo.getHouseId());
        lqw.eq(StringUtils.isNotBlank(bo.getRealityQuality()), ZMaterialStatistics::getRealityQuality, bo.getRealityQuality());
        lqw.eq(StringUtils.isNotBlank(bo.getRealitySpecifications()), ZMaterialStatistics::getRealitySpecifications, bo.getRealitySpecifications());
        lqw.eq(bo.getRealitySize() != null, ZMaterialStatistics::getRealitySize, bo.getRealitySize());
        lqw.eq(StringUtils.isNotBlank(bo.getEnginCycle()), ZMaterialStatistics::getEnginCycle, bo.getEnginCycle());
        return lqw;
    }

    /**
     * 新增用料统计
     */
    @Override
    public Boolean insertByBo(ZMaterialStatisticsBo bo) {
        ZMaterialStatistics add = BeanUtil.toBean(bo, ZMaterialStatistics.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    @Async("threadPoolTaskExecutor")
    public Boolean insert(ZEngineeringCivilBo bo) {
        if (ObjectUtils.isEmpty(bo.getzEngineeringNodeBo())) {
            bo.getzEngineeringNodeBoList().stream().forEach(item2 -> {
                if (item2.getzEngineeringInfoBo().getzEngineeringMaterialBo().size() > 0) {
                    item2.getzEngineeringInfoBo().getzEngineeringMaterialBo().stream().forEach(item -> {
                        if (!ObjectUtils.isEmpty(item)) {
                            LambdaQueryWrapper<ZMaterialStatistics> lqw = Wrappers.lambdaQuery();
                            lqw.eq(StringUtils.isNotBlank(bo.getAreaId()), ZMaterialStatistics::getAreaId, bo.getAreaId());
                            lqw.eq(StringUtils.isNotBlank(bo.getBuildingId()), ZMaterialStatistics::getBuildingId, bo.getBuildingId());
                            lqw.eq(StringUtils.isNotBlank(bo.getUnitId()), ZMaterialStatistics::getUnitId, bo.getUnitId());
                            lqw.eq(StringUtils.isNotBlank(bo.getHouseId()), ZMaterialStatistics::getHouseId, bo.getHouseId());
                            lqw.eq(StringUtils.isNotBlank(item.getMaterialQuality()), ZMaterialStatistics::getRealityQuality, item.getMaterialQuality());
                            lqw.eq(StringUtils.isNotBlank(item.getSpecifications()), ZMaterialStatistics::getRealitySpecifications, item.getSpecifications());
                            lqw.eq(StringUtils.isNotBlank(bo.getEnginCycle()), ZMaterialStatistics::getEnginCycle, bo.getEnginCycle());
                            lqw.eq(StringUtils.isNotBlank(bo.getEnginClassification()), ZMaterialStatistics::getEnginClassification, bo.getEnginClassification());
                            lqw.eq(StringUtils.isNotBlank(bo.getEnginType()), ZMaterialStatistics::getEnginType, bo.getEnginType());
                            lqw.eq(StringUtils.isNotBlank(item2.getType()), ZMaterialStatistics::getNodeType, item2.getType());
                            ZMaterialStatistics zMaterialStatistics = baseMapper.selectOne(lqw);
                            if (!ObjectUtils.isEmpty(zMaterialStatistics)) {
                                zMaterialStatistics.setRealitySize(BigDecimal.valueOf(zMaterialStatistics.getRealitySize()).add(BigDecimal.valueOf(item.getNumber())).doubleValue());
                                baseMapper.updateById(zMaterialStatistics);
                            } else {
                                if (!ObjectUtils.isEmpty(item.getMaterialQuality()) && !ObjectUtils.isEmpty(item.getSpecifications())) {
                                    ZMaterialStatistics zMaterialStatistics1 = new ZMaterialStatistics();
                                    BeanUtils.copyProperties(bo, zMaterialStatistics1);
                                    zMaterialStatistics1.setRealityQuality(item.getMaterialQuality());
                                    zMaterialStatistics1.setRealitySpecifications(item.getSpecifications());
                                    zMaterialStatistics1.setRealitySize(item.getNumber());
                                    zMaterialStatistics1.setEnginCycle(bo.getEnginCycle());
                                    zMaterialStatistics1.setNodeType(item2.getType());
                                    baseMapper.insert(zMaterialStatistics1);
                                }
                            }
                        }

                    });
                }

            });
        }else
        {
            bo.getzEngineeringNodeBo().getzEngineeringInfoBo().getzEngineeringMaterialBo().stream().forEach(item->{
                if (!ObjectUtils.isEmpty(item)) {
                    LambdaQueryWrapper<ZMaterialStatistics> lqw = Wrappers.lambdaQuery();
                    lqw.eq(StringUtils.isNotBlank(bo.getAreaId()), ZMaterialStatistics::getAreaId, bo.getAreaId());
                    lqw.eq(StringUtils.isNotBlank(bo.getBuildingId()), ZMaterialStatistics::getBuildingId, bo.getBuildingId());
                    lqw.eq(StringUtils.isNotBlank(bo.getUnitId()), ZMaterialStatistics::getUnitId, bo.getUnitId());
                    lqw.eq(StringUtils.isNotBlank(bo.getHouseId()), ZMaterialStatistics::getHouseId, bo.getHouseId());
                    lqw.eq(StringUtils.isNotBlank(item.getMaterialQuality()), ZMaterialStatistics::getRealityQuality, item.getMaterialQuality());
                    lqw.eq(StringUtils.isNotBlank(item.getSpecifications()), ZMaterialStatistics::getRealitySpecifications, item.getSpecifications());
                    lqw.eq(StringUtils.isNotBlank(bo.getEnginCycle()), ZMaterialStatistics::getEnginCycle, bo.getEnginCycle());
                    ZMaterialStatistics zMaterialStatistics = baseMapper.selectOne(lqw);
                    if (zMaterialStatistics != null) {
                        zMaterialStatistics.setRealitySize(BigDecimal.valueOf(zMaterialStatistics.getRealitySize()).add(BigDecimal.valueOf(item.getNumber())).doubleValue());
                        baseMapper.updateById(zMaterialStatistics);
                    } else {
                        if (!ObjectUtils.isEmpty(item.getMaterialQuality()) && !ObjectUtils.isEmpty(item.getSpecifications())) {
                            ZMaterialStatistics zMaterialStatistics1 = new ZMaterialStatistics();
                            BeanUtils.copyProperties(bo, zMaterialStatistics1);
                            zMaterialStatistics1.setRealityQuality(item.getMaterialQuality());
                            zMaterialStatistics1.setRealitySpecifications(item.getSpecifications());
                            zMaterialStatistics1.setRealitySize(item.getNumber());
                            zMaterialStatistics1.setEnginCycle(bo.getEnginCycle());
                            baseMapper.insert(zMaterialStatistics1);
                        }
                    }
                }
            });
        }
        return null;
    }

    /**
     * 修改用料统计
     */
    @Override
    public Boolean updateByBo(ZMaterialStatisticsBo bo) {
        ZMaterialStatistics update = BeanUtil.toBean(bo, ZMaterialStatistics.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

}
