package com.wzj.soopin.transaction.kit.params.impl;

import cn.hutool.json.JSONUtil;

import com.wzj.soopin.transaction.domain.po.Charge;
import com.wzj.soopin.transaction.domain.po.PayOrder;
import com.wzj.soopin.transaction.enums.CashierEnum;
import com.wzj.soopin.transaction.enums.ChargeStatus;
import com.wzj.soopin.transaction.enums.PayStatusEnum;
import com.wzj.soopin.transaction.kit.dto.PayParam;
import com.wzj.soopin.transaction.kit.dto.PaymentSuccessParams;
import com.wzj.soopin.transaction.kit.params.CashierExecute;
import com.wzj.soopin.transaction.kit.params.dto.CashierParam;
import com.wzj.soopin.transaction.mapper.ChargeMapper;
import com.wzj.soopin.transaction.mapper.PayOrderMapper;
import com.wzj.soopin.transaction.service.IChargeService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.constant.ResultCode;
import org.dromara.common.core.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * 充值信息获取
 *
 * @author Chopper
 * @since 2021-01-25 20:00
 */
@Slf4j
@Component
@AllArgsConstructor
public class RechargeCashier implements CashierExecute {
    private final IChargeService chargeService;





    @Override
    public CashierEnum cashierEnum() {
        return CashierEnum.RECHARGE;
    }

    @Override
    public void paymentSuccess(PaymentSuccessParams paymentSuccessParams) {
        PayParam payParam = paymentSuccessParams.getPayParam();
        if (payParam.getOrderType().equals(CashierEnum.RECHARGE.name())) {
            chargeService.paySuccess(payParam.getSn(), paymentSuccessParams.getReceivableNo(),paymentSuccessParams.getPaymentMethod());
            log.info("会员充值-订单号{},第三方流水：{}", payParam.getSn(), paymentSuccessParams.getReceivableNo());
        }
    }


    @Override
    public CashierParam getPaymentParams(PayParam payParam) {
        if (payParam.getOrderType().equals(CashierEnum.RECHARGE.name())) {
            //准备返回的数据
            CashierParam cashierParam = new CashierParam();
            //订单信息获取
            Charge recharge = chargeService.getByCode(payParam.getSn());

            //如果订单已支付，则不能发器支付
            if (!recharge.getStatus().equals(ChargeStatus.WAITING.getCode())) {
                throw new ServiceException(ResultCode.PAY_DOUBLE_ERROR);
            }
            cashierParam.setPrice( recharge.getMoney().longValue());

            try {
                cashierParam.setTitle("在线充值");
            } catch (Exception e) {
                cashierParam.setTitle("多用户商城，在线充值");
            }
            cashierParam.setDetail("余额充值");
            cashierParam.setCreateTime(recharge.getCreateTime());
            return cashierParam;
        }

        return null;
    }

    @Override
    public Boolean paymentResult(PayParam payParam) {
        if (payParam.getOrderType().equals(CashierEnum.RECHARGE.name())) {
//            Recharge recharge = rechargeService.getRecharge(payParam.getSn());
//            if (recharge != null) {
//                return recharge.getPayStatus().equals(PayStatusEnum.PAID.name());
//            } else {
//                throw new ServiceException(ResultCode.PAY_NOT_EXIST_ORDER);
//            }
        }
        return false;
    }
}
