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 lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zdsz.domain.bo.ZEnginMaterialQualityBo;
import com.ruoyi.zdsz.domain.vo.ZEnginMaterialQualityVo;
import com.ruoyi.zdsz.domain.ZEnginMaterialQuality;
import com.ruoyi.zdsz.mapper.ZEnginMaterialQualityMapper;
import com.ruoyi.zdsz.service.IZEnginMaterialQualityService;

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

/**
 * 工程材质Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-29
 */
@RequiredArgsConstructor
@Service
public class ZEnginMaterialQualityServiceImpl implements IZEnginMaterialQualityService {

    private final ZEnginMaterialQualityMapper baseMapper;

    /**
     * 查询工程材质
     */
    @Override
    public ZEnginMaterialQualityVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询工程材质列表
     */
    @Override
    public TableDataInfo<ZEnginMaterialQualityVo> queryPageList(ZEnginMaterialQualityBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZEnginMaterialQuality> lqw = buildQueryWrapper(bo);
        Page<ZEnginMaterialQualityVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询工程材质列表
     */
    @Override
    public List<ZEnginMaterialQualityVo> queryList(ZEnginMaterialQualityBo bo) {
        LambdaQueryWrapper<ZEnginMaterialQuality> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZEnginMaterialQuality> buildQueryWrapper(ZEnginMaterialQualityBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZEnginMaterialQuality> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getEnginType()), ZEnginMaterialQuality::getEnginType, bo.getEnginType());
        lqw.like(StringUtils.isNotBlank(bo.getName()), ZEnginMaterialQuality::getName, bo.getName());
        return lqw;
    }

    /**
     * 新增工程材质
     */
    @Override
    public Boolean insertByBo(ZEnginMaterialQualityBo bo) {
        ZEnginMaterialQuality add = BeanUtil.toBean(bo, ZEnginMaterialQuality.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改工程材质
     */
    @Override
    public Boolean updateByBo(ZEnginMaterialQualityBo bo) {
        ZEnginMaterialQuality update = BeanUtil.toBean(bo, ZEnginMaterialQuality.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    @Override
    public List<ZEnginMaterialQualityVo> getEnginMaterialQualityList(String enginType) {
        return baseMapper.getEnginMaterialQualityList(enginType);
    }
}
