package org.dromara.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.TblBagBo;
import org.dromara.system.domain.vo.TblBagVo;
import org.dromara.system.domain.TblBag;
import org.dromara.system.mapper.TblBagMapper;
import org.dromara.system.service.ITblBagService;

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

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author Lion Li
 * @date 2025-09-06
 */
@RequiredArgsConstructor
@Service
public class TblBagServiceImpl implements ITblBagService {

    private final TblBagMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public TblBagVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<TblBagVo> queryPageList(TblBagBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TblBag> lqw = buildQueryWrapper(bo);
        Page<TblBagVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<TblBagVo> queryList(TblBagBo bo) {
        LambdaQueryWrapper<TblBag> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public List<TblBagVo> queryList(QueryWrapper qWrapper) {
        return baseMapper.selectVoList(qWrapper);
    }

    private LambdaQueryWrapper<TblBag> buildQueryWrapper(TblBagBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TblBag> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeviceId() != null, TblBag::getDeviceId, bo.getDeviceId());
        lqw.eq(bo.getScType() != null, TblBag::getScType, bo.getScType());
        lqw.eq(bo.getScTime() != null, TblBag::getScTime, bo.getScTime());
        lqw.eq(StringUtils.isNotBlank(bo.getCpCode()), TblBag::getCpCode, bo.getCpCode());
        lqw.eq(StringUtils.isNotBlank(bo.getWaybillNo()), TblBag::getWaybillNo, bo.getWaybillNo());
        if(bo.getParams()!=null && bo.getParams().size()>0){
            if(bo.getParams().get("remark")!=null){
                lqw.eq(bo.getParams().get("remark")!=null, TblBag::getRemark, "");
            }
        }
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(TblBagBo bo) {
        TblBag add = MapstructUtils.convert(bo, TblBag.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(TblBagBo bo) {
        TblBag update = MapstructUtils.convert(bo, TblBag.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
