package org.jeecg.modules.userCode.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.bestpay.model.PayResponse;
import org.jeecg.common.constant.RedisKey;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.bean.RechargeReq;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.userCode.model.req.OrderPayReq;
import org.jeecg.modules.userCode.model.req.RechargeInfoLogReq;
import org.jeecg.modules.userCode.model.req.WithawalReq;
import org.jeecg.modules.userCode.service.IPayService;
import org.jeecg.modules.utils.ValidateTool;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author java996.icu
 * @description PayController
 * @date 2021/4/13 15:06
 * @Version V1.0
 */
@Slf4j
@RestController
@RequestMapping("/pay")
@Api(tags = "付款相关接口")
public class PayController {

    @Resource
    private IPayService payService;


    @Resource
    private RedisUtil redisUtil;

    @Resource
    private ShiroRealm shiroRealm;

    ExecutorService cachedThreadPool = new ThreadPoolExecutor(32, 32,
            0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>());

    /**
     * 订单支付
     *
     * @param req
     * @param token
     * @return
     */
    @ApiOperation("订单支付")
    @PostMapping("/order")
    public Result<?> orderPay(@RequestBody OrderPayReq req, @RequestHeader("X-Access-Token") String token) {
        Result<?> result = payService.orderPay(req, token);
        log.warn("支付结果{}", JSONObject.toJSONString(result));

        return result;
    }


    @ApiOperation("订单余额支付")
    @PostMapping("/payOrderByBalance")
    public Result<?> payOrderByBalance(@RequestBody OrderPayReq req, @RequestHeader("X-Access-Token") String token) {
        Result<?> result = payService.payOrderByBalance(req, token);
        log.warn("支付结果{}", JSONObject.toJSONString(result));

        return result;
    }




    /**
     * 订单支付
     *
     * @param req
     * @param token
     * @return
     */
    @ApiOperation("订单保险支付")
    @PostMapping("/orderPremiumPay")
    public Result<?> orderPremiumPay(@RequestBody OrderPayReq req, @RequestHeader("X-Access-Token") String token) {
        Result<?> result = payService.orderPremiumPay(req, token);
        log.warn("支付结果{}", JSONObject.toJSONString(result));
        return result;
    }

    /**
     * 支付回调
     *
     * @param payResponse
     */
    @PostMapping("/notify")
    public void notify(@RequestBody PayResponse payResponse) {
        Result<?> notify = payService.notify(payResponse);
        if (ValidateTool.isNotNull(notify) && notify.getCode() == 200) {


        } else {
            log.error("重复回调{}", payResponse.getOrderId());
        }
    }



    /**
     * 充值支付
     *
     * @param token
     * @return
     */
    @ApiOperation("充值支付")
    @PostMapping("/payRecharge")
    public Result<?> payRecharge(@RequestBody RechargeReq rechargeReq, @RequestHeader("X-Access-Token") String token) {

        Integer n = (Integer) redisUtil.get(RedisKey.RECHARGE_LOCK_KEY + token);
        if (n != null) {
            return Result.error("系统繁忙，请过后尝试！");
        }
        redisUtil.set(RedisKey.RECHARGE_LOCK_KEY + token, 1, 3);
        return payService.payRecharge(rechargeReq, token);
    }

    //TODO  交接改动

    /**
     * 充值支付回调
     *
     * @param payResponse
     */
    @PostMapping("/payRechargeNotify")
    public void payRechargeNotify(@RequestBody PayResponse payResponse) {
        payService.payRechargeNotify(payResponse);
    }


    /**
     * 余额日志
     *
     * @param token
     * @param rechargeInfoLogReq
     * @return
     */
    @ApiOperation("余额日志")
    @PostMapping("/log")
    public Result<?> rechargeLog(@RequestHeader("X-Access-Token") String token, @RequestBody(required = false) RechargeInfoLogReq rechargeInfoLogReq) {
        return payService.rechargeLog(token, rechargeInfoLogReq);
    }


    /**
     * 提现
     *
     * @param token
     * @param withawalReq
     * @return
     */
    @ApiOperation("提现")
    @PostMapping("/withdrawal")
    public Result<?> withdrawal(@RequestHeader("X-Access-Token") String token,
                                WithawalReq withawalReq) {

        return payService.withdrawal(token, withawalReq);
    }


}
