package com.ruoyi.zhdd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.zhdd.domain.bo.ChemicalDataBo;
import com.ruoyi.zhdd.domain.vo.ChemicalDataVo;
import com.ruoyi.zhdd.domain.ChemicalData;
import com.ruoyi.zhdd.mapper.ChemicalDataMapper;
import com.ruoyi.zhdd.service.IChemicalDataService;

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

/**
 * 危化品信息Service业务层处理
 *
 * @author xintong
 * @date 2022-01-14
 */
@Service
public class ChemicalDataServiceImpl extends ServicePlusImpl<ChemicalDataMapper, ChemicalData, ChemicalDataVo> implements IChemicalDataService {

    @Override
    public ChemicalDataVo queryById(String id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<ChemicalDataVo> queryPageList(ChemicalDataBo bo) {
        PagePlus<ChemicalData, ChemicalDataVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<ChemicalDataVo> queryList(ChemicalDataBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

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

    @Override
    public Boolean insertByBo(ChemicalDataBo bo) {
        ChemicalData add = BeanUtil.toBean(bo, ChemicalData.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByBo(ChemicalDataBo bo) {
        ChemicalData update = BeanUtil.toBean(bo, ChemicalData.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ChemicalData entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
