package com.jeequan.jeepay.thirdparty.channel.yspay;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.PayOrder;
import com.jeequan.jeepay.core.interfaces.paychannel.IPayOrderQueryService;
import com.jeequan.jeepay.core.model.context.MchAppConfigContext;
import com.jeequan.jeepay.core.model.rqrs.msg.ChannelRetMsg;
import com.jeequan.jeepay.thirdparty.channel.yspay.model.YsCardType;
import com.jeequan.jeepay.thirdparty.channel.yspay.utils.YspayKit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 查询订单 银盛支付
 *
 * @author xiaoyu
 *
 * @date 2022/4/15 14:29
 */
@Service
@Slf4j
public class YspayPayOrderQueryService implements IPayOrderQueryService {

    @Override
    public ChannelRetMsg query(PayOrder payOrder, MchAppConfigContext mchAppConfigContext) throws Exception {
        try {
            JSONObject bizContent = new JSONObject();

            // 商户订单号
            bizContent.put("out_trade_no", payOrder.getPayOrderId());
            // 订单交易日期
            bizContent.put("shopdate", DateUtil.format(payOrder.getCreatedAt(), DatePattern.PURE_DATE_PATTERN));

            String result = YspayKit.payRequest("ysepay.online.trade.order.query", mchAppConfigContext, bizContent, null, payOrder.getIfCode());
            JSONObject resJson = JSONObject.parseObject(result);
            JSONObject response = resJson.getJSONObject("ysepay_online_trade_order_query_response");
            String code = response.getString("code");
            if (YspayKit.RES_CODE.equals(code)) {
                String tradeStatus = response.getString("trade_status");
                if(YspayKit.TRADE_SUCCESS.equals(tradeStatus)){ // 支付成功

                    String transNo = response.getString("trade_no");
                    // 渠道流水号
                    String channelRecvSn = response.getString("channel_recv_sn");
                    // 商户订单号
                    String channelSendSn = response.getString("channel_send_sn");
                    // 卡类型
                    String cardType = response.getString("card_type");
                    YsCardType val = YsCardType.getVal(cardType);
                    String drType = CS.DrType.OTHER.getType();
                    if(val != null){
                        switch (val){
                            case CREDIT:
                                drType = CS.DrType.CREDIT.getType();
                                break;
                            case DEBIT:
                                drType = CS.DrType.DEBIT.getType();
                                break;
                        }
                    }
                    return ChannelRetMsg.confirmSuccess(transNo, channelRecvSn, channelSendSn,drType);

                }else if (YspayKit.TRADE_FAILED.equals(tradeStatus)){  // 失败
                    // 失败
                    return ChannelRetMsg.confirmFail();
                }
            }
            //支付中
            return ChannelRetMsg.waiting();
        }catch (Exception e) {
            //支付中
            return ChannelRetMsg.waiting();
        }
    }

}
