package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.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 org.dromara.system.domain.bo.TblGoodsLossBo;
import org.dromara.system.domain.vo.TblGoodsLossVo;
import org.dromara.system.domain.TblGoodsLoss;
import org.dromara.system.mapper.TblGoodsLossMapper;
import org.dromara.system.service.ITblGoodsLossService;

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

/**
 * 丢失货物Service业务层处理
 *
 * @author Lion Li
 * @date 2024-02-22
 */
@RequiredArgsConstructor
@Service
public class TblGoodsLossServiceImpl implements ITblGoodsLossService {

    private final TblGoodsLossMapper baseMapper;

    /**
     * 查询丢失货物
     */
    @Override
    public TblGoodsLossVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询丢失货物列表
     */
    @Override
    public TableDataInfo<TblGoodsLossVo> queryPageList(TblGoodsLossBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TblGoodsLoss> lqw = buildQueryWrapper(bo);
        Page<TblGoodsLossVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询丢失货物列表
     */
    @Override
    public List<TblGoodsLossVo> queryList(TblGoodsLossBo bo) {
        LambdaQueryWrapper<TblGoodsLoss> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TblGoodsLoss> buildQueryWrapper(TblGoodsLossBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TblGoodsLoss> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), TblGoodsLoss::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), TblGoodsLoss::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getExt1()), TblGoodsLoss::getExt1, bo.getExt1());
        lqw.eq(StringUtils.isNotBlank(bo.getExt2()), TblGoodsLoss::getExt2, bo.getExt2());
        return lqw;
    }

    /**
     * 新增丢失货物
     */
    @Override
    public Boolean insertByBo(TblGoodsLossBo bo) {
        TblGoodsLoss add = MapstructUtils.convert(bo, TblGoodsLoss.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改丢失货物
     */
    @Override
    public Boolean updateByBo(TblGoodsLossBo bo) {
        TblGoodsLoss update = MapstructUtils.convert(bo, TblGoodsLoss.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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