package com.ruoyi.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.frequency.auth.service.AuthService;
import com.ruoyi.frequency.bondrecord.service.BondRecordService;
import com.ruoyi.frequency.highendauth.service.HighendAuthService;
import com.ruoyi.frequency.order.service.OrderService;
import com.ruoyi.frequency.rechargeorder.service.RechargeOrderService;
import com.ruoyi.response.ResponseData;
import com.ruoyi.utils.BusinessUtil;
import com.ruoyi.vo.RyController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;


/**
 * 支付回调
 */
@RestController
@RequestMapping("/api/pay")
@Slf4j
@Api(tags = "支付回调")
public class ApiPayController extends RyController {


    @Autowired
    private BusinessUtil businessUtil;

    @Autowired
    private OrderService orderService;

    /**
     * 订单首款支付宝回调地址
     */
    @ApiOperation("订单首款支付宝回调地址")
    @RequestMapping(value = "/orderAliNotify", method = RequestMethod.POST)
    public void orderAliNotify(HttpServletRequest request, HttpServletResponse response) {
        log.info("订单首款支付宝回调开始........");
        try {
            ResponseData responseData = orderService.orderAliNotify(request, response);
            if (responseData.getSuccess()) {
                businessUtil.writeText(response, "success");
            }

        } catch (Exception e) {
            e.getMessage();
        }
    }

    /**
     * 订单首款微信回调地址
     */
    @ApiOperation("订单首款微信回调地址")
    @RequestMapping(value = "/orderWxNotify", method = RequestMethod.POST)
    public void orderWxNotify(HttpServletRequest request, HttpServletResponse response) {
        log.info("订单首款微信回调开始........");
        String msg = "";
        try {
            ResponseData responseData = orderService.orderWxNotify(request, response);
            if (responseData.getSuccess()) {
                String resultMsg = String.format("<xml><return_code><![CDATA[%s]]></return_code><return_msg><![CDATA[%s]]></return_msg></xml>",
                        ("SUCCESS"), msg);
                businessUtil.writeText(response, resultMsg);
            }
        } catch (Exception e) {
            msg = e.getMessage();
        }
    }

    @ApiOperation("订单首款拉卡拉回调地址")
    @RequestMapping(value = "/orderLakalaNotify", method = RequestMethod.POST)
    public String orderLakalaNotify(@RequestBody String callbackData) {
        log.info("订单首款拉卡拉回调开始........");
        // 转换请求参数为 Map
        Map<String, String> paramMap =  JSONObject.parseObject(callbackData, Map.class);

        String msg = "";
        try {
            ResponseData responseData = orderService.orderLakalaNotify(paramMap);
            if (responseData.getSuccess()) {
                return "SUCCESS";
            }
        } catch (Exception e) {
            msg = e.getMessage();
        }
        return "SUCCESS";
    }


    /**
     * 订单尾款支付宝回调地址
     */
    @ApiOperation("订单尾款支付宝回调地址")
    @RequestMapping(value = "/secondOrderAliNotify", method = RequestMethod.POST)
    public void secondOrderAliNotify(HttpServletRequest request, HttpServletResponse response) {
        log.info("订单尾款支付宝回调开始........");
        try {
            ResponseData responseData = orderService.secondOrderAliNotify(request, response);
            if (responseData.getSuccess()) {
                businessUtil.writeText(response, "success");
            }

        } catch (Exception e) {
            e.getMessage();
        }
    }

    /**
     * 订单尾款微信回调地址
     */
    @ApiOperation("订单尾款微信回调地址")
    @RequestMapping(value = "/secondOrderWxNotify", method = RequestMethod.POST)
    public void secondOrderWxNotify(HttpServletRequest request, HttpServletResponse response) {
        log.info("订单尾款微信回调开始........");
        String msg = "";
        try {
            ResponseData responseData = orderService.secondOrderWxNotify(request, response);
            if (responseData.getSuccess()) {
                String resultMsg = String.format("<xml><return_code><![CDATA[%s]]></return_code><return_msg><![CDATA[%s]]></return_msg></xml>",
                        ("SUCCESS"), msg);
                businessUtil.writeText(response, resultMsg);
            }
        } catch (Exception e) {
            msg = e.getMessage();
        }
    }

    @ApiOperation("订单尾款拉卡拉回调地址")
    @RequestMapping(value = "/secondOrderLakalaNotify", method = RequestMethod.POST)
    public String secondOrderLakalaNotify(@RequestBody String callbackData) {
        log.info("订单尾款拉卡拉回调开始........");
        // 转换请求参数为 Map
        Map<String, String> paramMap =  JSONObject.parseObject(callbackData, Map.class);
        String msg = "";
        try {
            ResponseData responseData = orderService.secondOrderLakalaNotify(paramMap);
            if (responseData.getSuccess()) {
                return "SUCCESS";
            }
        } catch (Exception e) {
            msg = e.getMessage();
        }
        return "SUCCESS";
    }


    @Autowired
    private BondRecordService bondRecordService;

    /**
     * 保证金支付宝回调地址
     */
    @ApiOperation("保证金支付宝回调地址")
    @RequestMapping(value = "/bondAliNotify", method = RequestMethod.POST)
    public void bondAliNotify(HttpServletRequest request, HttpServletResponse response) {
        log.info("保证金支付宝回调开始........");
        try {
            ResponseData responseData = bondRecordService.bondAliNotify(request, response);
            if (responseData.getSuccess()) {
                businessUtil.writeText(response, "success");
            }

        } catch (Exception e) {
            e.getMessage();
        }
    }

    /**
     * 保证金微信回调地址
     */
    @ApiOperation("保证金微信回调地址")
    @RequestMapping(value = "/bondWxNotify", method = RequestMethod.POST)
    public void bondWxNotify(HttpServletRequest request, HttpServletResponse response) {
        log.info("保证金微信回调开始........");
        String msg = "";
        try {
            ResponseData responseData = bondRecordService.bondWxNotify(request, response);
            if (responseData.getSuccess()) {
                String resultMsg = String.format("<xml><return_code><![CDATA[%s]]></return_code><return_msg><![CDATA[%s]]></return_msg></xml>",
                        ("SUCCESS"), msg);
                businessUtil.writeText(response, resultMsg);
            }
        } catch (Exception e) {
            msg = e.getMessage();
        }
    }

    @ApiOperation("保证金拉卡拉回调地址")
    @RequestMapping(value = "/bondLakalaNotify", method = RequestMethod.POST)
    public String bondLakalaNotify(@RequestBody String callbackData) {
        log.info("保证金拉卡拉回调开始........");
        // 转换请求参数为 Map
        Map<String, String> paramMap =  JSONObject.parseObject(callbackData, Map.class);
        String msg = "";
        try {
            ResponseData responseData = bondRecordService.bondLakalaNotify(paramMap);
            if (responseData.getSuccess()) {
                return "SUCCESS";
            }
        } catch (Exception e) {
            msg = e.getMessage();
        }
        return "SUCCESS";
    }



    @Autowired
    private AuthService authService;

    /**
     * 普通认证支付宝回调地址
     */
    @ApiOperation("普通认证支付宝回调地址")
    @RequestMapping(value = "/authAliNotify", method = RequestMethod.POST)
    public void authAliNotify(HttpServletRequest request, HttpServletResponse response) {
        log.info("普通认证支付宝回调开始........");
        try {
            ResponseData responseData = authService.authAliNotify(request, response);
            if (responseData.getSuccess()) {
                businessUtil.writeText(response, "success");
            }

        } catch (Exception e) {
            e.getMessage();
        }
    }

    /**
     * 普通认证微信回调地址
     */
    @ApiOperation("普通认证微信回调地址")
    @RequestMapping(value = "/authWxNotify", method = RequestMethod.POST)
    public void authWxNotify(HttpServletRequest request, HttpServletResponse response) {
        log.info("普通认证微信回调开始........");
        String msg = "";
        try {
            ResponseData responseData = authService.authWxNotify(request, response);
            if (responseData.getSuccess()) {
                String resultMsg = String.format("<xml><return_code><![CDATA[%s]]></return_code><return_msg><![CDATA[%s]]></return_msg></xml>",
                        ("SUCCESS"), msg);
                businessUtil.writeText(response, resultMsg);
            }
        } catch (Exception e) {
            msg = e.getMessage();
        }
    }

    @ApiOperation("普通认证拉卡拉回调地址")
    @RequestMapping(value = "/authLakalaNotify", method = RequestMethod.POST)
    public String authLakalaNotify(@RequestBody String callbackData) {
        log.info("普通认证拉卡拉回调开始........");
        // 转换请求参数为 Map
        Map<String, String> paramMap =  JSONObject.parseObject(callbackData, Map.class);
        String msg = "";
        try {
            ResponseData responseData = authService.authLakalaNotify(paramMap);
            if (responseData.getSuccess()) {
                return "SUCCESS";
            }
        } catch (Exception e) {
            msg = e.getMessage();
        }
        return "SUCCESS";
    }


    @Autowired
    private HighendAuthService highendAuthService;

    /**
     * 高端认证支付宝回调地址
     */
    @ApiOperation("高端认证支付宝回调地址")
    @RequestMapping(value = "/highendAuthAliNotify", method = RequestMethod.POST)
    public void highendAuthAliNotify(HttpServletRequest request, HttpServletResponse response) {
        log.info("高端认证支付宝回调开始........");
        try {
            ResponseData responseData = highendAuthService.highendAuthAliNotify(request, response);
            if (responseData.getSuccess()) {
                businessUtil.writeText(response, "success");
            }

        } catch (Exception e) {
            e.getMessage();
        }
    }

    /**
     * 高端认证微信回调地址
     */
    @ApiOperation("高端认证微信回调地址")
    @RequestMapping(value = "/highendAuthWxNotify", method = RequestMethod.POST)
    public void highendAuthWxNotify(HttpServletRequest request, HttpServletResponse response) {
        log.info("高端认证微信回调开始........");
        String msg = "";
        try {
            ResponseData responseData = highendAuthService.highendAuthWxNotify(request, response);
            if (responseData.getSuccess()) {
                String resultMsg = String.format("<xml><return_code><![CDATA[%s]]></return_code><return_msg><![CDATA[%s]]></return_msg></xml>",
                        ("SUCCESS"), msg);
                businessUtil.writeText(response, resultMsg);
            }
        } catch (Exception e) {
            msg = e.getMessage();
        }
    }

    @ApiOperation("高端认证拉卡拉回调地址")
    @RequestMapping(value = "/highendAuthLakalaNotify", method = RequestMethod.POST)
    public String highendAuthLakalaNotify(@RequestBody String callbackData) {
        log.info("高端认证拉卡拉回调开始........");
        // 转换请求参数为 Map
        Map<String, String> paramMap =  JSONObject.parseObject(callbackData, Map.class);
        String msg = "";
        try {
            ResponseData responseData = highendAuthService.highendAuthLakalaNotify(paramMap);
            if (responseData.getSuccess()) {
                return "SUCCESS";
            }
        } catch (Exception e) {
            msg = e.getMessage();
        }
        return "SUCCESS";
    }


    @Autowired
    private RechargeOrderService rechargeOrderService;

    /**
     * 充值支付宝回调地址
     */
    @ApiOperation("充值支付宝回调地址")
    @RequestMapping(value = "/rechargeAliNotify", method = RequestMethod.POST)
    public void rechargeAliNotify(HttpServletRequest request, HttpServletResponse response) {
        log.info("充值支付宝回调开始........");
        try {
            ResponseData responseData = rechargeOrderService.rechargeAliNotify(request, response);
            if (responseData.getSuccess()) {
                businessUtil.writeText(response, "success");
            }

        } catch (Exception e) {
            e.getMessage();
        }
    }

    /**
     * 充值微信回调地址
     */
    @ApiOperation("充值微信回调地址")
    @RequestMapping(value = "/rechargeWxNotify", method = RequestMethod.POST)
    public void rechargeWxNotify(HttpServletRequest request, HttpServletResponse response) {
        log.info("充值微信回调开始........");
        String msg = "";
        try {
            ResponseData responseData = rechargeOrderService.rechargeWxNotify(request, response);
            if (responseData.getSuccess()) {
                String resultMsg = String.format("<xml><return_code><![CDATA[%s]]></return_code><return_msg><![CDATA[%s]]></return_msg></xml>",
                        ("SUCCESS"), msg);
                businessUtil.writeText(response, resultMsg);
            }
        } catch (Exception e) {
            msg = e.getMessage();
        }
    }

    @ApiOperation("充值拉卡拉回调地址")
    @RequestMapping(value = "/rechargeLakalaNotify", method = RequestMethod.POST)
    public String rechargeLakalaNotify(@RequestBody String callbackData) {
        log.info("充值拉卡拉回调开始........");
        // 转换请求参数为 Map
        Map<String, String> paramMap =  JSONObject.parseObject(callbackData, Map.class);
        String msg = "";
        try {
            ResponseData responseData = rechargeOrderService.rechargeLakalaNotify(paramMap);
            if (responseData.getSuccess()) {
                return "SUCCESS";
            }
        } catch (Exception e) {
            msg = e.getMessage();
        }
        return "SUCCESS";
    }



    /**
     * 设计师调价支付宝回调地址
     */
    @ApiOperation("设计师调价支付宝回调地址")
    @RequestMapping(value = "/orderAdjustPricePayAliNotify", method = RequestMethod.POST)
    public void orderAdjustPricePayAliNotify(HttpServletRequest request, HttpServletResponse response) {
        log.info("设计师调价支付宝回调开始........");
        try {
            ResponseData responseData = orderService.orderAdjustPricePayAliNotify(request, response);
            if (responseData.getSuccess()) {
                businessUtil.writeText(response, "success");
            }

        } catch (Exception e) {
            e.getMessage();
        }
    }

    /**
     * 设计师调价微信回调地址
     */
    @ApiOperation("设计师调价微信回调地址")
    @RequestMapping(value = "/orderAdjustPricePayWxNotify", method = RequestMethod.POST)
    public void orderAdjustPricePayWxNotify(HttpServletRequest request, HttpServletResponse response) {
        log.info("设计师调价微信回调开始........");
        String msg = "";
        try {
            ResponseData responseData = orderService.orderAdjustPricePayWxNotify(request, response);
            if (responseData.getSuccess()) {
                String resultMsg = String.format("<xml><return_code><![CDATA[%s]]></return_code><return_msg><![CDATA[%s]]></return_msg></xml>",
                        ("SUCCESS"), msg);
                businessUtil.writeText(response, resultMsg);
            }
        } catch (Exception e) {
            msg = e.getMessage();
        }
    }

    @ApiOperation("设计师调价拉卡拉回调地址")
    @RequestMapping(value = "/orderAdjustPricePayLakalaNotify", method = RequestMethod.POST)
    public String orderAdjustPricePayLakalaNotify(@RequestBody String callbackData) {
        log.info("设计师调价拉卡拉回调开始........");
        // 转换请求参数为 Map
        Map<String, String> paramMap =  JSONObject.parseObject(callbackData, Map.class);
        String msg = "";
        try {
            ResponseData responseData = orderService.orderAdjustPricePayLakalaNotify(paramMap);
            if (responseData.getSuccess()) {
                return "SUCCESS";
            }
        } catch (Exception e) {
            msg = e.getMessage();
        }
        return "SUCCESS";
    }


    /**
     * 表现师调价设计师确认支付宝回调地址
     */
    @ApiOperation("表现师调价设计师确认支付宝回调地址")
    @RequestMapping(value = "/orderAdjustPriceConfirmPayAliNotify", method = RequestMethod.POST)
    public void orderAdjustPriceConfirmPayAliNotify(HttpServletRequest request, HttpServletResponse response) {
        log.info("表现师调价设计师确认支付宝回调地址开始........");
        String msg = "";
        try {
            ResponseData responseData = orderService.orderAdjustPriceConfirmPayAliNotify(request, response);
            if (responseData.getSuccess()) {
                businessUtil.writeText(response, "success");
            }

        } catch (Exception e) {
            e.getMessage();
        }
    }

    /**
     * 表现师调价设计师确认微信回调地址
     */
    @ApiOperation("表现师调价设计师确认微信回调地址")
    @RequestMapping(value = "/orderAdjustPriceConfirmPayWxNotify", method = RequestMethod.POST)
    public void orderAdjustPriceConfirmPayWxNotify(HttpServletRequest request, HttpServletResponse response) {
        log.info("表现师调价设计师确认微信回调地址开始........");
        String msg = "";
        try {
            ResponseData responseData = orderService.orderAdjustPriceConfirmPayWxNotify(request, response);
            if (responseData.getSuccess()) {
                String resultMsg = String.format("<xml><return_code><![CDATA[%s]]></return_code><return_msg><![CDATA[%s]]></return_msg></xml>",
                        ("SUCCESS"), msg);
                businessUtil.writeText(response, resultMsg);
            }
        } catch (Exception e) {
            msg = e.getMessage();
        }
    }

    @ApiOperation("表现师调价设计师确认拉卡拉回调地址")
    @RequestMapping(value = "/orderAdjustPriceConfirmPayLakalaNotify", method = RequestMethod.POST)
    public String orderAdjustPriceConfirmPayLakalaNotify(@RequestBody String callbackData) {

        log.info("表现师调价设计师确认拉卡拉回调开始........");
        // 转换请求参数为 Map
        Map<String, String> paramMap =  JSONObject.parseObject(callbackData, Map.class);
        String msg = "";
        try {
            ResponseData responseData = orderService.orderAdjustPriceConfirmPayLakalaNotify(paramMap);
            if (responseData.getSuccess()) {
                return "SUCCESS";
            }
        } catch (Exception e) {
            msg = e.getMessage();
        }
        return "SUCCESS";
    }
}


