package org.dromara.payment.channel.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.dromara.common.satoken.utils.LoginHelper;
import org.springframework.stereotype.Service;
import org.dromara.payment.channel.domain.bo.BusChannelFlowsBo;
import org.dromara.payment.channel.domain.vo.BusChannelFlowsVo;
import org.dromara.payment.channel.domain.BusChannelFlows;
import org.dromara.payment.channel.mapper.BusChannelFlowsMapper;
import org.dromara.payment.channel.service.IBusChannelFlowsService;

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

/**
 * 代理商流水Service业务层处理
 *
 * @author LionLi
 * @date 2024-04-10
 */
@RequiredArgsConstructor
@Service
public class BusChannelFlowsServiceImpl implements IBusChannelFlowsService {

    private final BusChannelFlowsMapper baseMapper;

    /**
     * 查询代理商流水
     */
    @Override
    public BusChannelFlowsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询代理商流水列表
     */
    @Override
    public TableDataInfo<BusChannelFlowsVo> queryPageList(BusChannelFlowsBo bo, PageQuery pageQuery) {
        bo.setChannelId(LoginHelper.getBusId());
        LambdaQueryWrapper<BusChannelFlows> lqw = buildQueryWrapper(bo);
        Page<BusChannelFlowsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询代理商流水列表
     */
    @Override
    public List<BusChannelFlowsVo> queryList(BusChannelFlowsBo bo) {
        LambdaQueryWrapper<BusChannelFlows> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BusChannelFlows> buildQueryWrapper(BusChannelFlowsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BusChannelFlows> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getChannelId() != null, BusChannelFlows::getChannelId, bo.getChannelId());
        lqw.eq(bo.getBusType() != null, BusChannelFlows::getBusType, bo.getBusType());
        lqw.eq(StringUtils.isNotBlank(bo.getBusNo()), BusChannelFlows::getBusNo, bo.getBusNo());
        lqw.eq(bo.getMoney() != null, BusChannelFlows::getMoney, bo.getMoney());
        lqw.eq(bo.getBalance() != null, BusChannelFlows::getBalance, bo.getBalance());
        lqw.eq(bo.getAddTime() != null, BusChannelFlows::getAddTime, bo.getAddTime());
        lqw.eq(StringUtils.isNotBlank(bo.getAddBy()), BusChannelFlows::getAddBy, bo.getAddBy());
        lqw.eq(StringUtils.isNotBlank(bo.getBusDesc()), BusChannelFlows::getBusDesc, bo.getBusDesc());
        lqw.eq(StringUtils.isNotBlank(bo.getBillNo()), BusChannelFlows::getBillNo, bo.getBillNo());
        lqw.eq(bo.getBillId() != null, BusChannelFlows::getBillId, bo.getBillId());
        lqw.eq(bo.getBillDetailId() != null, BusChannelFlows::getBillDetailId, bo.getBillDetailId());
        lqw.eq(bo.getTranType() != null, BusChannelFlows::getTranType, bo.getTranType());
        lqw.eq(bo.getPayerType() != null, BusChannelFlows::getPayerType, bo.getPayerType());
        lqw.eq(bo.getPayerId() != null, BusChannelFlows::getPayerId, bo.getPayerId());
        lqw.like(StringUtils.isNotBlank(bo.getPayerName()), BusChannelFlows::getPayerName, bo.getPayerName());
        lqw.eq(StringUtils.isNotBlank(bo.getPayerAcc()), BusChannelFlows::getPayerAcc, bo.getPayerAcc());
        lqw.eq(bo.getPayerAccId() != null, BusChannelFlows::getPayerAccId, bo.getPayerAccId());
        lqw.eq(bo.getPayeeType() != null, BusChannelFlows::getPayeeType, bo.getPayeeType());
        lqw.eq(bo.getPayeeId() != null, BusChannelFlows::getPayeeId, bo.getPayeeId());
        lqw.like(StringUtils.isNotBlank(bo.getPayeeName()), BusChannelFlows::getPayeeName, bo.getPayeeName());
        lqw.eq(StringUtils.isNotBlank(bo.getPayeeAcc()), BusChannelFlows::getPayeeAcc, bo.getPayeeAcc());
        lqw.eq(bo.getPayeeAccId() != null, BusChannelFlows::getPayeeAccId, bo.getPayeeAccId());
        return lqw;
    }

    /**
     * 新增代理商流水
     */
    @Override
    public Boolean insertByBo(BusChannelFlowsBo bo) {
        BusChannelFlows add = MapstructUtils.convert(bo, BusChannelFlows.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改代理商流水
     */
    @Override
    public Boolean updateByBo(BusChannelFlowsBo bo) {
        BusChannelFlows update = MapstructUtils.convert(bo, BusChannelFlows.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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