package com.gree.mall.manager.controller.fadada;

import com.gree.mall.manager.annotation.ApiNotAuth;
import com.gree.mall.manager.constant.Constant;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.fadada.FDDNotifyLogic;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@RequiredArgsConstructor
@RestController
@Api(value = "法大大回调通知API", tags ={"法大大回调通知API"} )
@RequestMapping("/fdd/notify")
public class NotifyController {

    private final FDDNotifyLogic fddNotifyLogic;
    private final RedisLockRegistry redisLockRegistry;

    @ApiNotAuth
    @RequestMapping("/company/ca/auth")
    @ApiOperation(value = "CA实名企业认证回调")
    public ResponseHelper<String> caCompanyAuthResultPost(@RequestParam Map<String, String> params) throws Exception {
        fddNotifyLogic.caCompanyAuthResult(params);
        return ResponseHelper.success();
    }

    @ApiNotAuth
    @RequestMapping("/company/ca/auth/auto")
    @ApiOperation(value = "CA实名企业授权自动签回调")
    public ResponseHelper<String> caCompanyAuthAutoResultPost(@RequestParam Map<String, String> params) throws Exception {
        fddNotifyLogic.caCompanyAuthAutoResult(params);
        return ResponseHelper.success();
    }

    @PostMapping("/find/company/auth/result")
    @ApiOperation(value = "CA实名企业结果查询")
    public ResponseHelper<String> findCompanyAuthResult(@RequestParam String transactionNo) throws Exception {
        fddNotifyLogic.queryCACompanyAuthResult(transactionNo);
        return ResponseHelper.success();
    }

    @ApiNotAuth
    @RequestMapping("/websit/auth")
    @ApiOperation(value = "实名网点认证回调")
    public ResponseHelper<String> websitAuthResultPost(@RequestParam Map<String, String> params) throws Exception {
        fddNotifyLogic.websitAuthResult(params);
        return ResponseHelper.success();
    }

    @ApiNotAuth
    @RequestMapping("/websit/person")
    @ApiOperation(value = "实名网点个人认证回调")
    public ResponseHelper<String> websitPersonAuthResultPost(@RequestParam Map<String, String> params) throws Exception {
        fddNotifyLogic.websitPersonAuthResul(params);
        return ResponseHelper.success();
    }

    @ApiNotAuth
    @RequestMapping("websit/batch/sign")
    @ApiOperation(value = "网点批量签署回调")
    public ResponseHelper<String> websitBatchSignResultPost(@RequestParam Map<String, String> params) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.FDD_BATCH_SIGN_NOTIFY + params.get("transaction_id"));
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("冻结法大大批量签署交易号");
        }
        try {
            fddNotifyLogic.websitBatchSignResult(params);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

}
