package com.qxgmat.controller.gateway;

import com.alibaba.fastjson.JSON;
import com.nuliji.tools.Response;
import com.nuliji.tools.ResponseHelp;
import com.nuliji.tools.exception.InsideException;
import com.nuliji.tools.pay.common.ResultInfo;
import com.qxgmat.data.constants.enums.trade.PayChannel;
import com.qxgmat.data.dao.entity.Pay;
import com.qxgmat.service.extend.TradeService;
import com.qxgmat.service.inline.PayService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/gateway/pay")
public class PayController {
    private static final Logger logger = LoggerFactory.getLogger(PayController.class);

    @Autowired
    private TradeService tradeService;

    @Autowired
    private PayService payService;

    @RequestMapping(value = "/wechat_qr", method = RequestMethod.POST)
    @ApiOperation(value = "微信二维码支付", httpMethod = "POST")
    public void wechatQr(HttpServletRequest request, HttpServletResponse response) {
        try {
            ResultInfo info = tradeService.notifyPay(PayChannel.WECHAT_QR, request);
            logger.debug(JSON.toJSONString(info));
            Pay pay = payService.getByNo(info.getNo());
            response.getWriter().write(tradeService.server(pay, info));
        }catch (InsideException e){
            logger.error(e.getMessage());
        }catch (Exception e){
            logger.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/alipay_qr", method = RequestMethod.POST)
    @ApiOperation(value = "支付宝二维码支付", httpMethod = "POST")
    public void alipayQr(HttpServletRequest request, HttpServletResponse response) {
        try {
            ResultInfo info = tradeService.notifyPay(PayChannel.ALIPAY_QR, request);
            logger.debug(JSON.toJSONString(info));
            Pay pay = payService.getByNo(info.getNo());
            response.getWriter().write(tradeService.server(pay, info));
        }catch (InsideException e){
            logger.error(e.getMessage());
        }catch (Exception e){
            logger.error(e.getMessage());
        }
    }

    @RequestMapping("/return/wechat_qr")
    @ResponseBody
    public Response<Pay> wechatQrReturn(@RequestBody String content){
        ResultInfo payRequest = JSON.parseObject(content, ResultInfo.class);
        try {
            ResultInfo info = tradeService.returnPay(PayChannel.WECHAT_QR, payRequest);
            if (info == null) return ResponseHelp.error(101, "支付签名错误");
            Pay pay = payService.getByNo(info.getNo());
            if (pay == null) return ResponseHelp.error(101, "支付信息不存在");
            pay = tradeService.client(pay, info);
            return ResponseHelp.success(pay);
        }catch (InsideException e){
            return ResponseHelp.exception(e);
        }
    }

    @RequestMapping("/return/alipay_qr")
    @ResponseBody
    public Response<Pay> alipayQrReturn(@RequestBody String content){
        ResultInfo payRequest = JSON.parseObject(content, ResultInfo.class);

        try {
            ResultInfo info = tradeService.returnPay(PayChannel.ALIPAY_QR, payRequest);
            if (info == null) return ResponseHelp.error(101, "支付签名错误");
            Pay pay = payService.getByNo(info.getNo());
            if (pay == null) return ResponseHelp.error(101, "支付信息不存在");
            pay = tradeService.client(pay, info);
            return ResponseHelp.success(pay);
        }catch (InsideException e){
            return ResponseHelp.exception(e);
        }
    }
}
