package com.kcim.web;

import com.alibaba.fastjson2.JSON;
import com.kcim.common.constants.RedisKeyConstant;
import com.kcim.common.token.RedisUtil;
import com.kcim.common.util.Result;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Objects;

/**
 * @program: CostAccount
 * @description: 公用方法调用
 * @author: Wang.YS
 * @create: 2023-12-28 16:28
 **/
@RequestMapping("public")
@RestController
@AllArgsConstructor
public class PublicController {

    private final RedisUtil redisUtil;
    @GetMapping("getPercent")
    public Result getPercent(@RequestParam String type) {
        Object percent = redisUtil.get(RedisKeyConstant.PROGRESS+type);
        if(Objects.nonNull(percent)){
            return Result.ok(percent);
        }else {
            return Result.ok();
        }

    }
    @GetMapping("flushPercent")
    public void flushPercent(HttpServletRequest request) {
        request.getSession().setAttribute("percent",null);
        Object percent = request.getSession().getAttribute("percent");
        System.out.println("3、刷新百分数为" + percent);
    }

    @SneakyThrows
    @PostMapping("test")
    public void test(@RequestParam String type){
        long l = 1234;


        for (int i = 0; i < l; i++) {
            double percent = (i + 1) / (double)l * 100;
            //这里对小数采用向下取整，例如99.5会取整为99
            percent = Math.floor(percent);
            Thread.sleep(1);
            System.out.println("1、设置百分数为" + percent);
            redisUtil.set(RedisKeyConstant.PROGRESS+type,percent);
        }
    }

    @PostMapping("endProgress")
    public Result endProgress(@RequestParam String type){
        redisUtil.del(RedisKeyConstant.PROGRESS+type);
        return Result.ok();
    }

}
