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

import com.alibaba.fastjson.JSONObject;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.pay.alipay.sdk.AlipayGoodsDetail;
import com.fdkankan.platform.order.request.RequestPlaceOrder;
import com.fdkankan.platform.order.service.IOrderService;
import com.fdkankan.platform.order.service.PayOrderService;
import com.fdkankan.platform.user.factory.Entity;
import com.fdkankan.platform.user.vo.ResponseOrder;
import com.fdkankan.platform.user.vo.ResponseOrderItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class AlipayEntity implements Entity {

    @Autowired
    private PayOrderService payOrderService;
    @Autowired
    private IOrderService orderService;

    @Override
    public JSONObject scanPay(RequestPlaceOrder order) throws Exception {
        ResponseOrder responseOrder = orderService.getOrderDetail(order.getOrderId());
        if (responseOrder.getId() == null){
            throw new BusinessException(ErrorCode.FAILURE_CODE_8001);
        }
        List<AlipayGoodsDetail> goodsDetailList = new ArrayList<>();
        List<ResponseOrderItem> items = responseOrder.getOrderItems();
        StringBuilder sb = new StringBuilder();
        AlipayGoodsDetail goodsDetail = null;
        for (int i = 0; i < items.size(); i++){
            sb.append(items.get(i).getGoodsCount()).append("*").append(items.get(i).getGoodsName());
            if (i != items.size() - 1){
                sb.append("，");
            }

            goodsDetail = AlipayGoodsDetail.newInstance(items.get(i).getGoodsSn(), items.get(i).getGoodsName(), items.get(i).getGoodsPrice().intValue(), items.get(i).getGoodsCount());
            goodsDetailList.add(goodsDetail);
        }
        String orderSn = responseOrder.getOrderSn();
        orderSn += "_entity";
        return payOrderService.aliPayScanPay(orderSn, "四维看看官网-商品", sb.toString(), responseOrder.getTotalAmount(), goodsDetailList);
    }

    @Override
    public JSONObject h5Pay(RequestPlaceOrder order, String ip) throws Exception {
        ResponseOrder responseOrder = orderService.getOrderDetail(order.getOrderId());
        if (responseOrder.getId() == null){
            throw new BusinessException(ErrorCode.FAILURE_CODE_8001);
        }
        List<ResponseOrderItem> items = responseOrder.getOrderItems();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < items.size(); i++) {
            sb.append(items.get(i).getGoodsCount()).append("*").append(items.get(i).getGoodsName());
            if (i != items.size() - 1) {
                sb.append("，");
            }
        }
        String orderSn = responseOrder.getOrderSn();
        orderSn += "_entity";
        return payOrderService.aliPayH5Pay(orderSn, "四维看看官网-商品", sb.toString(),  responseOrder.getTotalAmount());
    }

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