package com.mortals.xhx.busiz.controller;

import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.idempotent.helper.IdempotentHelper;
import com.mortals.xhx.busiz.service.TestIdempotentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试基于redis的幂等
 *
 * @author wenpan 2023/01/04 22:56
 */
@Slf4j
@RestController("TestRedisIdempotentController.v1")
@RequestMapping("/v1/idempotent")
public class TestIdempotentController {

    @Autowired
    private IdempotentHelper idempotentHelper;
    @Autowired
    private TestIdempotentService testIdempotentService;

    /**
     * 测试idempotentHelper执行有返回值的方法
     * url: localhost:12345/v1/idempotent/test-with-result?source=taobao&operationType=publish_product&businessKey=pd_20230105007&name=lisi
     */
    @GetMapping("/test-with-result")
    @UnAuth
    public String testWithResult(String source,
                                 String operationType,
                                 String businessKey,
                                 String name) {
        // 带返回值的幂等执行
        String result = idempotentHelper.invoke(source, operationType, businessKey,
                () -> testIdempotentService.testIdempotentHelper(name));
        log.info("=============>>>>>>> TestRedisIdempotentController result {}", result);
        return result;
    }

    /**
     * 测试idempotentHelper执行没有返回值的方法
     * url : localhost:12345/v1/idempotent/test-with-no-result?source=taobao&operationType=publish_product&businessKey=pd_20230105008&name=zhangsan
     */
    @GetMapping("/test-with-no-result")
    @UnAuth
    public String testWithNoResult(String source,
                                   String operationType,
                                   String businessKey,
                                   String name) {
        // 不带返回值的幂等执行
        idempotentHelper.invokeWithNoResult(source, operationType, businessKey,
                () -> testIdempotentService.testIdempotentWithNoResult(name));
        log.info("=============>>>>>>> TestRedisIdempotentController testWithNoResult.");
        return "success";
    }

}
