package com.hzs.third.pay.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.enums.EOrderType;
import com.hzs.common.domain.third.pay.TOnlinePayment;
import com.hzs.common.domain.third.pay.ext.TOnlinePaymentExt;
import com.hzs.third.pay.mapper.TOnlinePaymentMapper;
import com.hzs.third.pay.param.OnlinePaymentParam;
import com.hzs.third.pay.service.ITOnlinePaymentService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 在线支付信息 服务实现类
 *
 * @author hzs
 * @since 2022-07-28
 */
@Service
public class TOnlinePaymentServiceImpl extends ServiceImpl<TOnlinePaymentMapper, TOnlinePayment> implements ITOnlinePaymentService {

    @Override
    public TOnlinePayment queryByPaymentCode(String paymentCode, Integer pkCountry) {
        LambdaQueryWrapper<TOnlinePayment> queryWrapper = new LambdaQueryWrapper<>();
        if (null != pkCountry) {
            queryWrapper.eq(TOnlinePayment::getPkCountry, pkCountry);
        }
        queryWrapper.eq(TOnlinePayment::getPaymentCode, paymentCode);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public TOnlinePayment queryByBusiness(Integer businessType, String businessCode, Integer pkCountry) {
        LambdaQueryWrapper<TOnlinePayment> queryWrapper = new LambdaQueryWrapper<>();
        if (null != pkCountry) {
            queryWrapper.eq(TOnlinePayment::getPkCountry, pkCountry);
        }
        if(ObjectUtil.isNotEmpty(businessType)){
            queryWrapper.eq(TOnlinePayment::getBusinessType, businessType);
        }
        queryWrapper.eq(TOnlinePayment::getBusinessCode, businessCode);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public TOnlinePayment queryByBusinessCode(String businessCode, Long userId) {
        LambdaQueryWrapper<TOnlinePayment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TOnlinePayment::getBusinessCode, businessCode);
        queryWrapper.eq(TOnlinePayment::getPkCreator, userId);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public List<TOnlinePaymentExt> queryList(OnlinePaymentParam param, Integer pkCountry) {
        List<TOnlinePaymentExt> result = baseMapper.queryList(param, pkCountry);
        for (TOnlinePaymentExt tOnlinePaymentExt : result) {
            if(ObjectUtil.isNotEmpty(tOnlinePaymentExt.getOrderType())){
                tOnlinePaymentExt.setOrderTypeVal(EOrderType.getLabelByValue(tOnlinePaymentExt.getOrderType()));
            }else{
                tOnlinePaymentExt.setOrderTypeVal("充值订单");
            }
        }
        return result;
    }

}
