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.pay.alipay.sdk.AlipayGoodsDetail;
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.util.ArrayList;
import java.util.List;

@Component
public class AlipayExpansion implements Expansion {

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

    @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;
        List<AlipayGoodsDetail> goodsDetailList = new ArrayList<>();
        AlipayGoodsDetail goodsDetail = AlipayGoodsDetail.newInstance(expansion.getOrderSn(), subject, expansion.getAmount().intValue(), 1);
        goodsDetailList.add(goodsDetail);
        String orderSn = expansion.getOrderSn();
        orderSn += "_expansion";
        String body = subject;
        if(order.getSpaceId() != null){
            body += "_" + order.getSpaceId();
        }
        return payOrderService.aliPayScanPay(orderSn, subject, body, expansion.getAmount(), goodsDetailList);
    }

    @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;
        String orderSn = expansion.getOrderSn();
        orderSn += "_expansion";
        String body = subject;
        if(order.getSpaceId() != null){
            body += "_" + order.getSpaceId();
        }
        return payOrderService.aliPayH5Pay(orderSn, subject, body, expansion.getAmount());
    }

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