package com.qiaoba.module.demo.controller;

import com.baomidou.lock.annotation.Lock4j;
import com.qiaoba.common.base.result.AjaxResult;
import com.qiaoba.module.demo.service.DemoLock4jService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Demo-Lock4j web层
 *
 * @author ailanyin
 * @version 1.0
 * @since 2022-09-22 04:20:28
 */
@RestController
@RequestMapping("/demo/lock4j")
@RequiredArgsConstructor
@Tag(name = "Demo-Lock4j")
@Slf4j
public class DemoLock4jController {

    private final DemoLock4jService demoLock4JService;

    @GetMapping("/api-lock")
    public AjaxResult apiLock() {
        log.info("开始执行 apiLock, 当前线程{}", Thread.currentThread().getName());
        demoLock4JService.apiLock();
        return AjaxResult.success();
    }

    /**
     * 注解加锁
     * expire-Redis中Key过期时间-防止死锁-默认值: 30s
     * acquireTimeout-排队时长-超过就退出排队,抛出获取锁超时异常-默认值: 3s
     *
     * @return AjaxResult
     */
    @GetMapping("/annotation-lock")
    @Lock4j(name = "annotationLock", acquireTimeout = 1000)
    public AjaxResult annotationLock() {
        log.info("开始执行 annotationLock, 当前线程{}", Thread.currentThread().getName());
        demoLock4JService.annotationLock();
        return AjaxResult.success();
    }
}
