package com.fdkankan.platform.user.factory.impl;

import com.alibaba.fastjson.JSONObject;
import com.fdkankan.common.constant.Constant;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.platform.order.entity.IncrementOrder;
import com.fdkankan.platform.order.request.RequestPlaceOrder;
import com.fdkankan.platform.order.service.IIncrementOrderService;
import com.fdkankan.platform.order.service.PayOrderService;
import com.fdkankan.platform.user.factory.Expansion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
public class WechatIncrement implements Expansion {

    @Autowired
    private PayOrderService payOrderService;
    @Autowired
    private IIncrementOrderService incrementOrderService;

    @Override
    public JSONObject scanPay(RequestPlaceOrder order) throws Exception {
        IncrementOrder incrementOrderEntity = incrementOrderService.getById(order.getOrderId());
        if (incrementOrderEntity == null){
            throw new BusinessException(ErrorCode.FAILURE_CODE_8001);
        }
        //订单金额为国外金额就报错
        if(incrementOrderEntity.getAbroad() == 1){
            throw new BusinessException(ErrorCode.FAILURE_CODE_8004);
        }
        String subject = Constant.INCREMENT_SUBJECT + "：" + incrementOrderEntity.getCount() + "个/年";
        BigDecimal totalFee = incrementOrderEntity.getAmount().multiply(new BigDecimal(100));
        String orderSn = incrementOrderEntity.getOrderSn();
        String body = subject;
        orderSn += "_increment";
        return payOrderService.wechatScanPay(orderSn, subject, body, totalFee);
    }

    @Override
    public JSONObject h5Pay(RequestPlaceOrder order, String ip) throws Exception {
        IncrementOrder incrementOrderEntity = incrementOrderService.getById(order.getOrderId());
        if (incrementOrderEntity == null){
            throw new BusinessException(ErrorCode.FAILURE_CODE_8001);
        }
        //订单金额为国外金额就报错
        if(incrementOrderEntity.getAbroad() == 1){
            throw new BusinessException(ErrorCode.FAILURE_CODE_8004);
        }
        String subject = Constant.INCREMENT_SUBJECT + "：" + incrementOrderEntity.getCount() + "个/年";
        BigDecimal totalFee = incrementOrderEntity.getAmount().multiply(new BigDecimal(100));
        String orderSn = incrementOrderEntity.getOrderSn();
        String body = subject;
        orderSn += "_increment";
        return payOrderService.wechatH5Pay(orderSn, subject, body, totalFee, ip);
    }

    @Override
    public String getType() {
        return "wechat_4";
    }
}
