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.TblExpressStaticsBo;
import org.dromara.system.domain.vo.TblExpressStaticsVo;
import org.dromara.system.domain.TblExpressStatics;
import org.dromara.system.mapper.TblExpressStaticsMapper;
import org.dromara.system.service.ITblExpressStaticsService;

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

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

    private final TblExpressStaticsMapper baseMapper;

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

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

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

    private LambdaQueryWrapper<TblExpressStatics> buildQueryWrapper(TblExpressStaticsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TblExpressStatics> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, TblExpressStatics::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getMonth()), TblExpressStatics::getMonth, bo.getMonth());
        lqw.eq(StringUtils.isNotBlank(bo.getData()), TblExpressStatics::getData, bo.getData());
        if(bo.getParams()!=null && bo.getParams().keySet().size()>0){
            if(bo.getParams().get("ctime")!=null){
                lqw.eq(bo.getParams().get("ctime")!=null, TblExpressStatics::getMonth, bo.getParams().get("ctime"));
            }
        }
        return lqw;
    }

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

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

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

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