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.ExpansionOrder;
import com.fdkankan.platform.order.request.RequestPlaceOrder;
import com.fdkankan.platform.order.service.IExpansionOrderService;
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 WechatExpansion implements Expansion {

    @Autowired
    private PayOrderService payOrderService;
    @Autowired
    private IExpansionOrderService expansionOrderService;

    @Override
    public JSONObject scanPay(RequestPlaceOrder order) throws Exception {
        ExpansionOrder expansion = expansionOrderService.getById(order.getOrderId());
        if (expansion == null){
            throw new BusinessException(ErrorCode.FAILURE_CODE_8001);
        }
        //订单金额为国外金额就报错
        if(expansion.getAbroad() == 1){
            throw new BusinessException(ErrorCode.FAILURE_CODE_8004);
        }
        String date = "1年";
        if(expansion.getMonth() < 12){
            date = expansion.getMonth() + "个月";
        }
        String subject = Constant.EXPANSION_SUBJECT + "：" + expansion.getUnitSize() + expansion.getUnit() + "/" + date;
        BigDecimal totalFee = expansion.getAmount().multiply(new BigDecimal(100));
        String orderSn = expansion.getOrderSn();
        String body = subject;
        if(order.getSpaceId() != null){
            orderSn += "_" + order.getSpaceId();
        }else {
            orderSn += "_expansion";
        }
        return payOrderService.wechatScanPay(orderSn, subject, body, totalFee);
    }

    @Override
    public JSONObject h5Pay(RequestPlaceOrder order, String ip) throws Exception {
        ExpansionOrder expansion = expansionOrderService.getById(order.getOrderId());
        if (expansion == null){
            throw new BusinessException(ErrorCode.FAILURE_CODE_8001);
        }
        //订单金额为国外金额就报错
        if(expansion.getAbroad() == 1){
            throw new BusinessException(ErrorCode.FAILURE_CODE_8004);
        }
        String date = "1年";
        if(expansion.getMonth() < 12){
            date = expansion.getMonth() + "个月";
        }
        String subject = Constant.EXPANSION_SUBJECT + "：" + expansion.getUnitSize() + expansion.getUnit() + "/" + date;
        BigDecimal totalFee = expansion.getAmount().multiply(new BigDecimal(100));
        String orderSn = expansion.getOrderSn();
        String body = subject;
        if(order.getSpaceId() != null){
            orderSn += "_" + order.getSpaceId();
        }else {
            orderSn += "_expansion";
        }
        return payOrderService.wechatH5Pay(orderSn, subject, body, totalFee, ip);
    }

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