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.ZEngineeringMaterial;
import com.ruoyi.zdsz.domain.bo.ZBuildingBo;
import com.ruoyi.zdsz.domain.vo.ZBuildingVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zdsz.domain.bo.ZEngineeringMaterialsResetsBo;
import com.ruoyi.zdsz.domain.vo.ZEngineeringMaterialsResetsVo;
import com.ruoyi.zdsz.domain.ZEngineeringMaterialsResets;
import com.ruoyi.zdsz.mapper.ZEngineeringMaterialsResetsMapper;
import com.ruoyi.zdsz.service.IZEngineeringMaterialsResetsService;

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

/**
 * 工程用料预设Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-27
 */
@RequiredArgsConstructor
@Service
public class ZEngineeringMaterialsResetsServiceImpl implements IZEngineeringMaterialsResetsService {

    private final ZEngineeringMaterialsResetsMapper baseMapper;

    /**
     * 查询工程用料预设
     */
    @Override
    public ZEngineeringMaterialsResetsVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询工程用料预设列表
     */
    @Override
    public TableDataInfo<ZEngineeringMaterialsResetsVo> queryPageList(ZEngineeringMaterialsResetsBo bo, PageQuery pageQuery) {
//        LambdaQueryWrapper<ZEngineeringMaterialsResets> lqw = buildQueryWrapper(bo);
//        Page<ZEngineeringMaterialsResetsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
//        return TableDataInfo.build(result);
        Page<ZEngineeringMaterialsResetsBo> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        Page<ZEngineeringMaterialsResetsVo> list = baseMapper.getPageList(bo,page);
        return TableDataInfo.build(list);
    }

    /**
     * 查询工程用料预设列表
     */
    @Override
    public List<ZEngineeringMaterialsResetsVo> queryList(ZEngineeringMaterialsResetsBo bo) {
        LambdaQueryWrapper<ZEngineeringMaterialsResets> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZEngineeringMaterialsResets> buildQueryWrapper(ZEngineeringMaterialsResetsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZEngineeringMaterialsResets> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(ZEngineeringMaterialsResets::getCreateTime);
        lqw.eq(StringUtils.isNotBlank(bo.getAreaId()), ZEngineeringMaterialsResets::getAreaId, bo.getAreaId());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialQuality()), ZEngineeringMaterialsResets::getMaterialQuality, bo.getMaterialQuality());
        lqw.eq(StringUtils.isNotBlank(bo.getEstimatedSpecifications()), ZEngineeringMaterialsResets::getEstimatedSpecifications, bo.getEstimatedSpecifications());
        lqw.eq(bo.getEstimatedSize() != null, ZEngineeringMaterialsResets::getEstimatedSize, bo.getEstimatedSize());
        lqw.eq(StringUtils.isNotBlank(bo.getEnginCycle()), ZEngineeringMaterialsResets::getEnginCycle, bo.getEnginCycle());
        return lqw;
    }

    /**
     * 新增工程用料预设
     */
    @Override
    public Boolean insertByBo(ZEngineeringMaterialsResetsBo bo) {
        ZEngineeringMaterialsResets add = BeanUtil.toBean(bo, ZEngineeringMaterialsResets.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改工程用料预设
     */
    @Override
    public Boolean updateByBo(ZEngineeringMaterialsResetsBo bo) {
        ZEngineeringMaterialsResets update = BeanUtil.toBean(bo, ZEngineeringMaterialsResets.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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


    @Override
    public int getMaterialByAreaId(Collection<String> ids) {
        LambdaQueryWrapper<ZEngineeringMaterialsResets> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(ZEngineeringMaterialsResets::getCreateTime);
        lqw.in(!ids.isEmpty(), ZEngineeringMaterialsResets::getAreaId, ids);
        return baseMapper.selectVoList(lqw).size();
    }
}
