package com.hfkj.service.order.impl;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.entity.BsHltCardRecord;
import com.hfkj.entity.BsUserCard;
import com.hfkj.model.order.OrderChildModel;
import com.hfkj.model.order.OrderModel;
import com.hfkj.service.hlt.BsHltCardRecordService;
import com.hfkj.service.hlt.HuiLianTongUnionCardService;
import com.hfkj.service.order.BsOrderService;
import com.hfkj.service.order.OrderPayService;
import com.hfkj.service.order.OrderPaySuccessService;
import com.hfkj.sysenum.hlt.HltCardRecordPayStatusEnum;
import com.hfkj.sysenum.order.OrderPayChannelEnum;
import com.hfkj.sysenum.order.OrderPayTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @className: OrderPayServiceImpl
 * @author: HuRui
 * @date: 2024/5/17
 **/
@Service("orderPayService")
public class OrderPayServiceImpl implements OrderPayService {
    @Resource
    private BsOrderService orderService;
    @Resource
    private BsHltCardRecordService hltCardRecordService;
    @Override
    public OrderModel hltCardPay(String instCode,OrderModel order, BsUserCard userCard) throws Exception {
        String goodsDesc = "";
        String tranDesc = "";
        String businessType = "HiSen_phg_goods";

        for (OrderChildModel orderChild : order.getOrderChildList()) {
            if (StringUtils.isBlank(tranDesc)) {
                tranDesc = tranDesc + orderChild.getProductName();
            } else {
                tranDesc = tranDesc +","+ orderChild.getProductName();
            }
        }
        // 描述超过30字符，截取加省略号
        if (tranDesc.length()>30) {
            tranDesc = tranDesc.substring(1,30) + "...";
        }

       /* if (order.getProductType().equals(OrderProductType.PRODUCT_TYPE5.getNumber())) {
            instCode = "11101654";
            businessType = "sinopec_oil_code";
            goodsDesc = "购买加油券";
            tranDesc = order.getHighChildOrderList().get(0).getGoodsName();

        }  else {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "暂时无法支付，请使用其他支付方式");
        }*/

        // 工会卡支付
        JSONObject consumption = HuiLianTongUnionCardService.consumption(order.getOrderNo(), userCard.getCardNo(), order.getPayRealPrice(), businessType, instCode, goodsDesc, tranDesc);
        if (!consumption.getString("respCode").equals("0000")) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, consumption.getString("respMessage"));
        }
        // 响应参数
        JSONObject consumptionResult = HuiLianTongUnionCardService.resolveResponse(consumption.getString("data"));
        if (consumptionResult.getBoolean("success") != true) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, consumptionResult.getString("message"));
        }

        BsHltCardRecord cardRecord = new BsHltCardRecord();
        cardRecord.setOrderNo(order.getOrderNo());
        cardRecord.setCardNo(userCard.getCardNo());
        cardRecord.setUserPhone(order.getUserPhone());
        cardRecord.setPayOrderNo(order.getOrderNo());
        cardRecord.setPaySerialNo(consumptionResult.getString("orderId"));
        cardRecord.setPayPrice(order.getPayRealPrice());
        cardRecord.setPayStatus(HltCardRecordPayStatusEnum.status2.getCode());
        cardRecord.setPayTime(new Date());
        cardRecord.setExceptionStatus(false);
        hltCardRecordService.create(cardRecord);

        order.setPayChannel(OrderPayChannelEnum.type4.getCode());
        order.setPayType(OrderPayTypeEnum.type4.getCode());
        order.setPaySerialNo(consumptionResult.getString("orderId"));
        // 处理订单业务
        orderService.orderPaySuccessHandle(order);
        return order;
    }
}
