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

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.TransferOrder;
import com.jeequan.jeepay.core.exception.ResponseException;
import com.jeequan.jeepay.core.model.rqrs.msg.ChannelRetMsg;
import com.jeequan.jeepay.thirdparty.channel.AbstractTransferNoticeService;
import com.jeequan.jeepay.thirdparty.channel.sybpay.kits.SybpayKits;
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 zx
*
* @date 2021/21/01 17:16
*/
@Service
@Slf4j
public class SybpayTransferNoticeService extends AbstractTransferNoticeService {

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

    @Override
    public MutablePair<String, Object> parseParams(HttpServletRequest request, String urlOrderId) {
        try {
            JSONObject params = getReqParamJSON();
            log.info("【收银呗】回调通知参数：{}", params.toJSONString());
            boolean verifyResult = SybpayKits.checkSign(params);
            //验签失败
            if(!verifyResult){
                log.error("【收银呗】，转账回调验签失败");
                throw ResponseException.buildText("error");
            }
            String bizContent = params.getString("bizContent");
            JSONObject bizJson = SybpayKits.analysisNotify(bizContent,params.getString("check"));
            String transferId = bizJson.getString("outOrderNo");
            return MutablePair.of(transferId, bizJson);
        } catch (Exception e) {
            log.error("error", e);
            throw ResponseException.buildText("ERROR");
        }
    }

    @Override
    public ChannelRetMsg doNotice(HttpServletRequest request, Object params, TransferOrder transferOrder) {
        try {
            // 获取请求参数
            JSONObject bizContent = (JSONObject) params;
            //验签成功后判断上游订单状态
            ResponseEntity okResponse = textResp("success");
            ChannelRetMsg channelRetMsg = new ChannelRetMsg();
            channelRetMsg.setResponseEntity(okResponse);
            channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.WAITING);
            String remark = bizContent.getString("request_remark");
            channelRetMsg.setChannelErrMsg(remark);
            Integer status = bizContent.getIntValue("state");
            if(status == 1){
                channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_SUCCESS);
            }else if(status == 0){
                channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.WAITING);
            }else if(status == 2){
                channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_FAIL);
            }
            return channelRetMsg;
        } catch (Exception e) {
            log.error("error", e);
            throw ResponseException.buildText("error");
        }
    }

}
