package com.yonge.cooleshow.student.controller;

import com.alibaba.fastjson.JSONObject;
import com.huifu.adapay.core.AdapayCore;
import com.huifu.adapay.core.util.AdapaySign;
import com.huifu.adapay.core.util.StringUtil;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.sdk.PaymentSdk;
import com.yonge.cooleshow.biz.dal.service.EmployeeService;
import com.yonge.cooleshow.biz.dal.service.UserOrderService;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Author: liweifan
 * @Data: 2022/3/11 18:16
 */
@RestController
@RequestMapping("/payment")
@Api(value = "付款接口", tags = "付款接口")
public class PaymentController extends BaseController {
    private final static Logger log = LoggerFactory.getLogger(PaymentController.class);
    @Autowired
    private PaymentSdk paymentSdk;
    @Autowired
    private UserOrderService userOrderService;
    @Autowired
    private SysUserFeignService sysUserFeignService;

    @ApiOperation(value = "查询付款单")
    @GetMapping("/queryPayment")
    public HttpResponseResult<Map<String, Object>> queryPayment(String paymentId) throws Exception {
        return HttpResponseResult.succeed(paymentSdk.queryPayment(paymentId));
    }

    @ApiOperation(value = "微信支付获取openid")
    @GetMapping("/getOpenId")
    public HttpResponseResult<Map<String, String>> getOpenId(String js_code) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        HttpResponseResult<Map<String, String>> responseResult = paymentSdk.getOpenId(js_code);
        if (responseResult.getStatus()) {
            Map<String, String> data = responseResult.getData();
            //绑定到用户表
            user.setWxOpenid(data.get("openid"));
            log.info("绑定用户openid，param is {}", JSONObject.toJSONString(user));
            sysUserFeignService.bindOpenId(user);
        }
        return responseResult;
    }

    /***
     * 汇付回调
     * 问题：1、成功失败需要回执什么信息 2、回调url地址是否需要加入白名单
     * @author liweifan
     * @param: request
     * @updateTime 2022/3/11 18:35
     */
    @PostMapping("/callback")
    public void callback(HttpServletRequest request) {
        try {
            //验签传参data
            String data = request.getParameter("data");
            //验签传参sign
            String sign = request.getParameter("sign");
            //回调业务类型
            String type = request.getParameter("type");
            log.info("汇付回调，type is {}, res is {}", type, data);
            if (StringUtil.isEmpty(type)) {
                log.error("汇付回调参数异常");
                return;
            }
            //验签传参publicKey
            String publicKey = AdapayCore.PUBLIC_KEY;
            //验签
            boolean checkSign = AdapaySign.verifySign(data, sign, publicKey);
            if (checkSign) {
                //验签成功逻辑
                log.info("验签成功");
                userOrderService.orderCallback(data, type);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
