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

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 lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 斗拱 查询订单
 *
 * @author xiaoyu
 *
 * @date 2022/6/9 17:05
 */
@Service
@Slf4j
public class DgpayPayOrderQueryService implements IPayOrderQueryService {

    @Override
    public ChannelRetMsg query(PayOrder payOrder, MchAppConfigContext mchAppConfigContext) throws Exception {

        // 智能POS  直接返回
        if (StringUtils.equals(payOrder.getWayCode(), CS.PAY_WAY_CODE.AUTO_POS)) {
            return ChannelRetMsg.waiting();
        }
        try {
            JSONObject reqParams = new JSONObject();
            // 交易流水号
            reqParams.put("mer_ord_id", payOrder.getPayOrderId());
            // 商户号
            reqParams.put("huifu_id", payOrder.getChannelMchNo());
            // 原机构请求日期
            reqParams.put("org_req_date", DateUtil.format(payOrder.getCreatedAt(), DatePattern.PURE_DATE_PATTERN));
            JSONObject response = DgPayKit.request(mchAppConfigContext, reqParams, "/trade/payment/scanpay/query", payOrder.getIfCode());
            JSONObject resData = response.getJSONObject("data");
            String code = resData.getString("resp_code");
            //如果查询请求成功
            if(DgPayKit.STATE_SUCCESS.equals(code)){
                String tradeState = resData.getString("trans_stat");
                if (DgPayKit.STATE_SUCCESS_S.equals(tradeState)) {
                    String channelOrderNo = resData.getString("org_hf_seq_id");
                    // 渠道订单号
                    String outTransId = resData.getString("out_trans_id");
                    // 商户订单号
                    String partyOrderId = resData.getString("party_order_id");
                    String drType = CS.DrType.OTHER.getType();
                    String debit_type = resData.getString("debit_type");
                    if("D".equals(debit_type)){
                        drType = CS.DrType.DEBIT.getType();
                    }else if("C".equals(debit_type)){
                        drType = CS.DrType.CREDIT.getType();
                    }
                    return ChannelRetMsg.confirmSuccess(channelOrderNo, outTransId, partyOrderId,drType);
                }else if (DgPayKit.STATE_FAIL_F.equals(tradeState)) {
                    // 失败
                    return ChannelRetMsg.confirmFail();
                }
            }
            //支付中
            return ChannelRetMsg.waiting();
        }catch (Exception e) {
            //支付中
            return ChannelRetMsg.waiting();
        }
    }

}
