package com.jeequan.jeepay.pay.ctrl.transfer;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.beans.RequestKitBean;
import com.jeequan.jeepay.core.ctrls.AbstractCtrl;
import com.jeequan.jeepay.core.interfaces.transfer.IChannelTransferSubjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 转账主体功能开通回调
 *
 * @author deng
 * @since 2024/5/8
 */
@Slf4j
@RestController
public class TransferSubjectChannelNotifyController extends AbstractCtrl {

    @Autowired
    private RequestKitBean requestKitBean;

    @ResponseBody
    @RequestMapping(value = {"/api/transferSubject/notify/{ifCode}", "/api/transferSubject/notify/{ifCode}/{customApplyId}"})
    public ResponseEntity doNotify(HttpServletRequest request, @PathVariable("ifCode") String ifCode, @PathVariable(value = "customApplyId", required = false) String customApplyId) {

        JSONObject reqParamJSON = requestKitBean.getReqParamJSON();
        log.info("代付参数, {}", reqParamJSON);

        IChannelTransferSubjectService channelTransferSubjectService = SpringUtil.getBean(ifCode + "ChannelTransferSubjectService", IChannelTransferSubjectService.class);

        if (channelTransferSubjectService == null) {
            return ResponseEntity.ok("SUCCESS");
        } else {
            Object result = channelTransferSubjectService.callback(reqParamJSON);
            return ResponseEntity.ok(result);
        }
    }
}
