package org.dromara.payment.operator.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.operator.domain.bo.BusOperatorFlowsBo;
import org.dromara.payment.operator.domain.vo.BusOperatorFlowsVo;
import org.dromara.payment.operator.domain.BusOperatorFlows;
import org.dromara.payment.operator.mapper.BusOperatorFlowsMapper;
import org.dromara.payment.operator.service.IBusOperatorFlowsService;

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

/**
 * 运营商流水Service业务层处理
 *
 * @author LionLi
 * @date 2024-03-28
 */
@RequiredArgsConstructor
@Service
public class BusOperatorFlowsServiceImpl implements IBusOperatorFlowsService {

    private final BusOperatorFlowsMapper baseMapper;

    /**
     * 查询运营商流水
     */
    @Override
    public BusOperatorFlowsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询运营商流水列表
     */
    @Override
    public TableDataInfo<BusOperatorFlowsVo> queryPageList(BusOperatorFlowsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BusOperatorFlows> lqw = buildQueryWrapper(bo);
        Page<BusOperatorFlowsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询运营商流水列表
     */
    @Override
    public List<BusOperatorFlowsVo> queryList(BusOperatorFlowsBo bo) {
        LambdaQueryWrapper<BusOperatorFlows> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BusOperatorFlows> buildQueryWrapper(BusOperatorFlowsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BusOperatorFlows> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getOpId() != null, BusOperatorFlows::getOpId, bo.getOpId());
        lqw.eq(bo.getBusType() != null, BusOperatorFlows::getBusType, bo.getBusType());
        lqw.eq(StringUtils.isNotBlank(bo.getChannel()), BusOperatorFlows::getChannel, bo.getChannel());
        lqw.eq(StringUtils.isNotBlank(bo.getBusNo()), BusOperatorFlows::getBusNo, bo.getBusNo());
        lqw.eq(StringUtils.isNotBlank(bo.getBankNo()), BusOperatorFlows::getBankNo, bo.getBankNo());
        lqw.eq(bo.getMoney() != null, BusOperatorFlows::getMoney, bo.getMoney());
        lqw.eq(bo.getBalance() != null, BusOperatorFlows::getBalance, bo.getBalance());
        lqw.eq(bo.getAddTime() != null, BusOperatorFlows::getAddTime, bo.getAddTime());
        lqw.eq(StringUtils.isNotBlank(bo.getAddBy()), BusOperatorFlows::getAddBy, bo.getAddBy());
        lqw.eq(StringUtils.isNotBlank(bo.getBusDesc()), BusOperatorFlows::getBusDesc, bo.getBusDesc());
        lqw.eq(StringUtils.isNotBlank(bo.getBillNo()), BusOperatorFlows::getBillNo, bo.getBillNo());
        lqw.eq(bo.getBillId() != null, BusOperatorFlows::getBillId, bo.getBillId());
        lqw.eq(bo.getBillDetailId() != null, BusOperatorFlows::getBillDetailId, bo.getBillDetailId());
        lqw.eq(StringUtils.isNotBlank(bo.getTranNo()), BusOperatorFlows::getTranNo, bo.getTranNo());
        lqw.eq(bo.getTranType() != null, BusOperatorFlows::getTranType, bo.getTranType());
        lqw.eq(bo.getTranStatus() != null, BusOperatorFlows::getTranStatus, bo.getTranStatus());
        lqw.eq(bo.getPayerType() != null, BusOperatorFlows::getPayerType, bo.getPayerType());
        lqw.eq(bo.getPayerId() != null, BusOperatorFlows::getPayerId, bo.getPayerId());
        lqw.like(StringUtils.isNotBlank(bo.getPayerName()), BusOperatorFlows::getPayerName, bo.getPayerName());
        lqw.eq(StringUtils.isNotBlank(bo.getPayerAcc()), BusOperatorFlows::getPayerAcc, bo.getPayerAcc());
        lqw.eq(bo.getPayerAccId() != null, BusOperatorFlows::getPayerAccId, bo.getPayerAccId());
        lqw.eq(bo.getPayeeType() != null, BusOperatorFlows::getPayeeType, bo.getPayeeType());
        lqw.eq(bo.getPayeeId() != null, BusOperatorFlows::getPayeeId, bo.getPayeeId());
        lqw.like(StringUtils.isNotBlank(bo.getPayeeName()), BusOperatorFlows::getPayeeName, bo.getPayeeName());
        lqw.eq(StringUtils.isNotBlank(bo.getPayeeAcc()), BusOperatorFlows::getPayeeAcc, bo.getPayeeAcc());
        lqw.eq(bo.getPayeeAccId() != null, BusOperatorFlows::getPayeeAccId, bo.getPayeeAccId());
        lqw.eq(StringUtils.isNotBlank(bo.getPayReceipt()), BusOperatorFlows::getPayReceipt, bo.getPayReceipt());
        lqw.eq(bo.getPayReceiptType() != null, BusOperatorFlows::getPayReceiptType, bo.getPayReceiptType());
        if(StringUtils.isNotBlank(bo.getParams().get("begin_time")) && StringUtils.isNotBlank(bo.getParams().get("end_time"))){
            lqw.between(BusOperatorFlows::getAddTime,bo.getParams().get("begin_time")+" 00:00:00",bo.getParams().get("end_time")+" 23:59:59");
        }
        lqw.orderByDesc(BusOperatorFlows::getId);
        return lqw;
    }

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

    /**
     * 修改运营商流水
     */
    @Override
    public Boolean updateByBo(BusOperatorFlowsBo bo) {
        BusOperatorFlows update = MapstructUtils.convert(bo, BusOperatorFlows.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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