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.VMerchantBillBo;
import org.dromara.payment.merchant.domain.vo.VMerchantBillVo;
import org.dromara.payment.merchant.domain.VMerchantBill;
import org.dromara.payment.merchant.mapper.VMerchantBillMapper;
import org.dromara.payment.merchant.service.IVMerchantBillService;

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

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

    private final VMerchantBillMapper baseMapper;

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

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

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

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

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

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

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

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