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

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.PayOrder;
import com.jeequan.jeepay.core.model.context.MchAppConfigContext;
import com.jeequan.jeepay.core.model.rqrs.msg.ChannelRetMsg;
import com.jeequan.jeepay.thirdparty.channel.AbstractChannelNoticeService;
import com.jeequan.jeepay.thirdparty.channel.kqpay.KqpayKit;
import com.jeequan.jeepay.thirdparty.channel.lklsb2bpay.model.LKlsb2bCardType;
import com.jeequan.jeepay.thirdparty.channel.lklsb2bpay.model.Lklsb2bOrderStatus;
import com.jeequan.jeepay.thirdparty.channel.lklsb2bpay.model.Lklsb2bTradeStatus;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.MutablePair;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * 拉卡拉通知 回调接口实现类
 *
 * @author crystal
 */
@Service
@Slf4j
public class Lklsb2bpayChannelNoticeService extends AbstractChannelNoticeService {

    @Override
    public String getIfCode() {
        return CS.IF_CODE.LKLSB2BPAY;
    }

    @Override
    public MutablePair<String, Object> parseParams(HttpServletRequest request, String urlOrderId, NoticeTypeEnum noticeTypeEnum) {
        JSONObject reqParamJSON = getReqParamJSON();
        String parOrderId = reqParamJSON.getString("out_order_no");
        return MutablePair.of(parOrderId, reqParamJSON);
    }

    @Override
    public ChannelRetMsg doNotice(HttpServletRequest request, Object params, PayOrder payOrder, MchAppConfigContext mchAppConfigContext, NoticeTypeEnum noticeTypeEnum) {
        JSONObject respJson = (JSONObject) JSONObject.toJSON(params);
        log.info("拉卡拉【聚合收银台】回调参数：{}",respJson);
        //验签成功后判断上游订单状态
        ResponseEntity okResponse = jsonResp(Lklsb2bpayKit.ok());
        ChannelRetMsg result = new ChannelRetMsg();
        result.setResponseEntity(okResponse);
        result.setChannelBizData(respJson);
        Lklsb2bOrderStatus tradeStatus = Lklsb2bOrderStatus.getVal(respJson.getIntValue("order_status"));
        JSONObject bizData = respJson.getJSONObject("order_trade_info");
        result.setPlatformMchOrderNo(bizData.getString("trade_no"));
        result.setChannelOrderId(bizData.getString("log_no"));
        result.setPlatformOrderNo(bizData.getString("acc_trade_no"));
        result.setChannelUserId(bizData.getString("user_id2"));
        String payMode = bizData.getString("pay_mode");
        result.setWayCodeType(payMode);
        if("UQRCODEPAY".equals(payMode)){
            result.setWayCodeType(CS.PAY_WAY_CODE_TYPE.UNIONPAY);
        }
        LKlsb2bCardType acc_type = LKlsb2bCardType.getVal(bizData.getString("acc_type"));
        result.setDrType(acc_type.getType());
        result.setChannelState(ChannelRetMsg.ChannelState.WAITING);
        switch (tradeStatus){
            case FAIL:
                result.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_FAIL);
                break;
            case EXPIRE:
                result.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_FAIL);
                break;
            case CANCEL:
                result.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_FAIL);
                break;
            default:
                result.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_SUCCESS);
                break;
        }
        return result;
    }


    /**
     * 验证银盛支付通知参数
     * @return
     */
    public boolean verifyParams(JSONObject jsonParams, MchAppConfigContext mchAppConfigContext) {
        try {
            return KqpayKit.checkResultNotify(jsonParams,mchAppConfigContext);
        }catch (Exception e) {
            log.error("验证签名异常", e);
            return false;
        }
    }
}
