package com.yonge.cooleshow.admin.controller.open;

import com.yonge.cooleshow.admin.controller.OmsOrderReturnApplyController;
import com.yonge.cooleshow.admin.service.OmsOrderReturnApplyService;
import com.yonge.cooleshow.mall.common.api.CommonResult;
import com.yonge.cooleshow.mall.common.enums.OrderCacheEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.concurrent.TimeUnit;

/**
 * Description
 *
 * @author liujunchi
 * @date 2022-07-28
 */
@Controller
@RequestMapping("/open")
public class OpenController {
    @Autowired
    private OmsOrderReturnApplyService returnApplyService;


    @Value("${message.debugMode}")
    private boolean debugMode;

    @Autowired
    private RedissonClient redissonClient;

    private static final Logger log = LoggerFactory.getLogger(OmsOrderReturnApplyController.class);

    @ApiOperation("完成退货测试接口")
    @GetMapping(value = "/test/returnApply/{id}")
    @ResponseBody
    public CommonResult test(@PathVariable Long id) {
        if (!debugMode) {
            return CommonResult.failed("当前环境不可使用");
        }
        RLock lock = redissonClient.getLock(OrderCacheEnum.LOCK_REFUND_ORDER_MALL + ":refundOrder:" + id);
        try {
            boolean b = lock.tryLock(60, 60, TimeUnit.SECONDS);
            if (b) {

                returnApplyService.setTestReturnApply(id);
                return CommonResult.success(true);

            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            if (lock.getHoldCount() >0) {
                lock.unlock();
            }
        }

        return CommonResult.failed();
    }

}
