package com.fjhx.u.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.fjhx.stock.service.StockCheckrecordService;
import com.fjhx.u.service.UUserService;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.ThreadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 管理员 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2022-11-28
 */
@RestController
@RequestMapping("/uUser")
public class UUserController {

    @Autowired
    private UUserService uUserService;

    @Autowired
    private StockCheckrecordService stockCheckrecordService;

    @GetMapping("/test")
    public R test(@RequestParam("sleep") Integer sleep) {
        new Thread(() -> {
            long id = IdWorker.getId();
            stockCheckrecordService.checkStart("check1", id, "c185883dba22478cb593d33f6b66cc53");
            ThreadUtil.sleep(1000L * 60 * sleep);
            stockCheckrecordService.checkEnd("check1", id, "c185883dba22478cb593d33f6b66cc53");
        }).start();

        return R.success();
    }

    @GetMapping("/test4")
    public R test4(@RequestParam("sleep") Integer sleep) {
        new Thread(() -> {
            long id = IdWorker.getId();
            stockCheckrecordService.checkStart("check4", id, "0b8f584250bb4b40b72d641ce4849d15");
            ThreadUtil.sleep(1000L * 60 * sleep);
            stockCheckrecordService.checkEnd("check4", id, "0b8f584250bb4b40b72d641ce4849d15");
        }).start();

        return R.success();
    }

    @GetMapping("/testScan")
    public R testScan(@RequestParam("sleep") Integer sleep, @RequestParam("houseType") Integer house) {
        long flag = IdWorker.getId();
        String stockHouseId = house == 1 ? "c185883dba22478cb593d33f6b66cc53" : "0b8f584250bb4b40b72d641ce4849d15";
        String instructions = house == 1 ? "check1" : "check4";

        new Thread(() -> {
            stockCheckrecordService.testScanStart(instructions, flag, stockHouseId);
            ThreadUtil.sleep(1000L * 60 * sleep);
            stockCheckrecordService.testScanEnd(instructions, flag, stockHouseId);
        }).start();

        Date date = new Date();

        HashMap<String, Object> map = new LinkedHashMap<>();
        map.put("扫描标记", flag);
        map.put("扫描时间", sleep + "分钟");
        map.put("扫描仓库", house == 1 ? "1楼仓库" : "4楼仓库");
        map.put("开始扫描时间", DateUtil.formatDateTime(date));
        map.put("扫描结束时间", DateUtil.formatDateTime(DateUtil.offsetMinute(date, sleep)));

        return R.success(map);
    }


//    @PostMapping("/page")
//    public R page(@RequestBody Map<String, Object> condition){
//        Page<UUser> result = uUserService.getPage(condition);
//        return R.success(result);
//    }
//
//    @PostMapping("/add")
//    public R add(@RequestBody UUserVo uUserVo){
//        uUserService.add(uUserVo);
//        return R.success();
//    }
//
//    @PostMapping("/edit")
//    public R edit(@RequestBody UUserVo uUserVo){
//        uUserService.edit(uUserVo);
//        return R.success();
//    }
//
//    @PostMapping("/delete")
//    public R delete(@RequestBody UUserVo uUserVo){
//        uUserService.delete(uUserVo);
//        return R.success();
//    }

    @PostMapping("/list")
    public R list(@RequestBody Map<String, Object> condition) {
        List<Map<String, Object>> result = uUserService.getList(condition);
        return R.success(result);
    }

}

