package org.dromara.payment.merchant.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.payment.merchant.domain.bo.VMerchantBillDayBo;
import org.dromara.payment.merchant.domain.vo.VMerchantBillDayVo;
import org.dromara.payment.merchant.domain.VMerchantBillDay;
import org.dromara.payment.merchant.mapper.VMerchantBillDayMapper;
import org.dromara.payment.merchant.service.IVMerchantBillDayService;

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

/**
 * 商户日账单汇总表Service业务层处理
 *
 * @author LionLi
 * @date 2024-04-03
 */
@RequiredArgsConstructor
@Service
public class VMerchantBillDayServiceImpl implements IVMerchantBillDayService {

    private final VMerchantBillDayMapper baseMapper;

    /**
     * 查询商户日账单汇总表
     */
    @Override
    public VMerchantBillDayVo queryById(Long merId){
        return baseMapper.selectVoById(merId);
    }

    /**
     * 查询商户日账单汇总表列表
     */
    @Override
    public TableDataInfo<VMerchantBillDayVo> queryPageList(VMerchantBillDayBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<VMerchantBillDay> lqw = buildQueryWrapper(bo);
        Page<VMerchantBillDayVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商户日账单汇总表列表
     */
    @Override
    public List<VMerchantBillDayVo> queryList(VMerchantBillDayBo bo) {
        LambdaQueryWrapper<VMerchantBillDay> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<VMerchantBillDay> buildQueryWrapper(VMerchantBillDayBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<VMerchantBillDay> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getMerNo()), VMerchantBillDay::getMerNo, bo.getMerNo());
        lqw.eq(StringUtils.isNotBlank(bo.getSydNo()), VMerchantBillDay::getSydNo, bo.getSydNo());
        lqw.eq(StringUtils.isNotBlank(bo.getOpNo()), VMerchantBillDay::getOpNo, bo.getOpNo());
        lqw.eq(bo.getOpId() != null, VMerchantBillDay::getOpId, bo.getOpId());
        lqw.eq(bo.getChannelId() != null, VMerchantBillDay::getChannelId, bo.getChannelId());
        lqw.eq(StringUtils.isNotBlank(bo.getChannelNo()), VMerchantBillDay::getChannelNo, bo.getChannelNo());
        lqw.eq(StringUtils.isNotBlank(bo.getChannelCode()), VMerchantBillDay::getChannelCode, bo.getChannelCode());
        lqw.eq(bo.getDayTranNum() != null, VMerchantBillDay::getDayTranNum, bo.getDayTranNum());
        lqw.eq(bo.getDayTranMoney() != null, VMerchantBillDay::getDayTranMoney, bo.getDayTranMoney());
        return lqw;
    }

    /**
     * 新增商户日账单汇总表
     */
    @Override
    public Boolean insertByBo(VMerchantBillDayBo bo) {
        VMerchantBillDay add = MapstructUtils.convert(bo, VMerchantBillDay.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setMerId(add.getMerId());
        }
        return flag;
    }

    /**
     * 修改商户日账单汇总表
     */
    @Override
    public Boolean updateByBo(VMerchantBillDayBo bo) {
        VMerchantBillDay update = MapstructUtils.convert(bo, VMerchantBillDay.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除商户日账单汇总表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
