package org.nl.acs.auto;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.nl.acs.auto.run.AutoRunService;
import org.nl.acs.auto.run.ThreadDto;
import org.nl.acs.utils.PageUtil;
import org.nl.common.logging.annotation.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author /
 * 接口限流测试类
 */
@RestController
@RequestMapping("/api/autorun")
@Api(tags = "系统：查询自动线程")
@RequiredArgsConstructor
public class AutoRunController {

    @Autowired
    private AutoRunService autoRunService;

    @ApiOperation("查询自动线程")
    @Log("查询自动线程")
    @GetMapping
    public ResponseEntity<Object> query(Map map) throws Exception {
        //List<ThreadDto> ThreadDtos = autoRunService.findByCondition(map);
        List<ThreadDto> ThreadDtos = autoRunService.findAll();
        return new ResponseEntity<>(PageUtil.toPage(ThreadDtos, ThreadDtos.size()), HttpStatus.OK);
    }

    @Log("启动自动线程")
    @ApiOperation("启动自动线程")
    @PutMapping(value = "/start/{code}")
    public ResponseEntity<Object> start(@PathVariable String code) {
        autoRunService.startThread(code);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("停止自动线程")
    @ApiOperation("停止自动线程")
    @PutMapping(value = "/stop/{code}")
    public ResponseEntity<Object> execution(@PathVariable String code) {
        autoRunService.stopThread(code);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}
