package org.dromara.payment.sys.service.impl;

import cn.hutool.core.convert.Convert;
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.payment.merchant.domain.BusMerchantFlows;
import org.springframework.stereotype.Service;
import org.dromara.payment.sys.domain.bo.SysSydFlowsBo;
import org.dromara.payment.sys.domain.vo.SysSydFlowsVo;
import org.dromara.payment.sys.domain.SysSydFlows;
import org.dromara.payment.sys.mapper.SysSydFlowsMapper;
import org.dromara.payment.sys.service.ISysSydFlowsService;

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

/**
 * 服务商流水Service业务层处理
 *
 * @author LionLi
 * @date 2024-03-26
 */
@RequiredArgsConstructor
@Service
public class SysSydFlowsServiceImpl implements ISysSydFlowsService {

    private final SysSydFlowsMapper baseMapper;

    /**
     * 查询服务商流水
     */
    @Override
    public SysSydFlowsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询服务商流水列表
     */
    @Override
    public TableDataInfo<SysSydFlowsVo> queryPageList(SysSydFlowsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysSydFlows> lqw = buildQueryWrapper(bo);
        Page<SysSydFlowsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询服务商流水列表
     */
    @Override
    public List<SysSydFlowsVo> queryList(SysSydFlowsBo bo) {
        LambdaQueryWrapper<SysSydFlows> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

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

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

    /**
     * 修改服务商流水
     */
    @Override
    public Boolean updateByBo(SysSydFlowsBo bo) {
        SysSydFlows update = MapstructUtils.convert(bo, SysSydFlows.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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