package com.zhongshu.card.server.core.stream;

import com.github.microservice.app.stream.StreamConsumer;
import com.github.microservice.core.util.JsonUtil;
import com.github.microservice.pay.client.model.chinaSenseless.ChinaSenselessPayRet;
import com.github.microservice.pay.client.model.ledger.TransactionLogModel;
import com.github.microservice.pay.client.ret.ResultContent;
import com.zhongshu.card.client.type.payment.OrderNoType;
import com.zhongshu.card.server.core.model.pay.ChinaumsSenselessSrcReserve;
import com.zhongshu.card.server.core.model.pay.UnionFrictionlessPayFinishModel;
import com.zhongshu.card.server.core.service.pay.ChinaumsSenselessPayService;
import com.zhongshu.card.server.core.service.payment.OrderNoInfoService;
import com.zhongshu.card.server.core.service.payment.PayCallService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

@Slf4j
@Component("chinaumsSenselessPayStreamConsumer")
public class ChinaumsSenselessPayStream extends StreamConsumer<ChinaSenselessPayRet> {

    @Autowired
    ChinaumsSenselessPayService chinaumsSenselessPayService;

    @Autowired
    OrderNoInfoService orderNoInfoService;

    @Autowired
    PayCallService payCallService;

    @SneakyThrows
    @Override
    public void accept(ChinaSenselessPayRet chinaSenselessPayRet) {
        log.info("****************收到银联支付/退款结果通知：{}", JsonUtil.toJson(chinaSenselessPayRet));

        String srcReserveStr = chinaSenselessPayRet.getSrcReverse();
        ChinaumsSenselessSrcReserve srcReserve = JsonUtil.toObject(srcReserveStr, ChinaumsSenselessSrcReserve.class);
        String merOrderId = srcReserve.getOrderNo();
        String status = chinaSenselessPayRet.getStatus();
        String refundOrderId = chinaSenselessPayRet.getRefundOrderId();

        UnionFrictionlessPayFinishModel unionFrictionlessPayFinishModel = new UnionFrictionlessPayFinishModel();
        if (StringUtils.isNotEmpty(refundOrderId) && refundOrderId.startsWith(srcReserve.getPrefix())){
            refundOrderId = refundOrderId.substring(srcReserve.getPrefix().length());
            unionFrictionlessPayFinishModel.setRefundOrderId(refundOrderId);
        }

        unionFrictionlessPayFinishModel.setMerOrderId(merOrderId);
        unionFrictionlessPayFinishModel.setStatus(status);
        unionFrictionlessPayFinishModel.setTargetOrderId(chinaSenselessPayRet.getTargetOrderId());

        OrderNoType orderNoType = orderNoInfoService.getOrderNoType(merOrderId);
        if (orderNoType.equals(OrderNoType.Pay)){

            if (status.equals("TRADE_SUCCESS")){
                ResultContent<List<TransactionLogModel>> listResultContent = chinaumsSenselessPayService.handlePay(srcReserve.getProjectOid(),
                        srcReserve.getOid(),
                        srcReserve.getUserId(),
                        new BigDecimal(chinaSenselessPayRet.getTotalAmount()),
                        merOrderId, null);
                unionFrictionlessPayFinishModel.setTransactionLogModelList(listResultContent);

            }
                payCallService.unionFrictionlessPayFinish(unionFrictionlessPayFinishModel);

        }
//        else {
//            log.info("未知的订单类型或该订单已处理：{}", merOrderId);
//        }

    }

//    else if (orderNoType.equals(OrderNoType.Refund)){
//        if (status.equals("TRADE_REFUND")){
//            ResultContent<List<TransactionLogModel>> listResultContent = chinaumsSenselessPayService.handleRefund(srcReserve.getProjectOid(),
//                    srcReserve.getOid(),
//                    srcReserve.getUserId(),
//                    new BigDecimal(chinaSenselessPayRet.getTotalAmount()),
//                    refundOrderId, null);
//            unionFrictionlessPayFinishModel.setTransactionLogModelList(listResultContent);
//        }
//        payCallService.unionFrictionlessPayRefundFinish(unionFrictionlessPayFinishModel);
//    }
}
