package com.izouma.nineth.web;

import cn.com.sandpay.cashier.sdk.CertUtil;
import cn.com.sandpay.cashier.sdk.CryptoUtil;
import com.alibaba.fastjson.JSONObject;
import com.izouma.nineth.config.GeneralProperties;
import com.izouma.nineth.enums.PayMethod;
import com.izouma.nineth.event.OrderNotifyEvent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;

@RestController
@RequestMapping("/sandpay")
@Slf4j
@AllArgsConstructor
public class SandPayController {

    private GeneralProperties generalProperties;
    private RocketMQTemplate  rocketMQTemplate;

    @PostMapping("/notify")
    public Object notifyOrder(HttpServletRequest req, HttpServletResponse resp) {
        String data = req.getParameter("data");
        String sign = req.getParameter("sign");
        // 验证签名
        boolean valid;
        try {
            valid = CryptoUtil.verifyDigitalSign(data.getBytes(StandardCharsets.UTF_8), Base64.decodeBase64(sign),
                    CertUtil.getPublicKey(), "SHA1WithRSA");
            if (!valid) {
                log.error("verify sign fail.");
                log.error("签名字符串(data)为：" + data);
                log.error("签名值(sign)为：" + sign);
            } else {
                log.info("verify sign success");
                JSONObject dataJson = JSONObject.parseObject(data);
                if (dataJson != null) {
                    log.info("通知业务数据为：" + JSONObject.toJSONString(dataJson, true));
                    if ("000000".equals(dataJson.getJSONObject("head").getString("respCode"))) {
                        JSONObject body = dataJson.getJSONObject("body");
                        JSONObject extend = body.getJSONObject("extend");
                        String type = extend.getString("type");
                        Long id = extend.getLong("id");
                        String payOrderCode = body.getString("payOrderCode");
                        String orderCode = body.getString("orderCode");
                        String bankserial = body.getString("bankserial");

                        switch (type) {
                            case "order":
                                rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
                                        new OrderNotifyEvent(id, PayMethod.SANDPAY,
                                                payOrderCode, System.currentTimeMillis()));
                                break;
                            case "gift":
                                rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
                                        new OrderNotifyEvent(id, PayMethod.SANDPAY, payOrderCode,
                                                System.currentTimeMillis(), OrderNotifyEvent.TYPE_GIFT_ORDER));
                                break;
                            case "mintOrder":
                                rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
                                        new OrderNotifyEvent(id, PayMethod.SANDPAY, payOrderCode,
                                                System.currentTimeMillis(), OrderNotifyEvent.TYPE_MINT_ORDER));
                                break;
                            case "recharge":
                                rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
                                        new OrderNotifyEvent(id, PayMethod.SANDPAY, payOrderCode,
                                                System.currentTimeMillis(), OrderNotifyEvent.TYPE_RECHARGE));
                                break;
                            case "auctionOrder":
                                rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
                                        new OrderNotifyEvent(id, PayMethod.SANDPAY, payOrderCode,
                                                System.currentTimeMillis(), OrderNotifyEvent.TYPE_AUCTION_ORDER));
                                break;
                        }
                    }
                    return "respCode=000000";
                } else {
                    log.error("通知数据异常！！！");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
