package org.nl.acs.instruction.rest;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.ObjectUtil;
import org.nl.common.logging.annotation.Log;
import org.nl.acs.instruction.domain.Instruction;
import org.nl.acs.instruction.service.InstructionService;
import org.nl.acs.instruction.service.dto.InstructionDto;
import org.nl.acs.instruction.service.dto.InstructionQueryParam;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.Set;

/**
 * @author jiaolm
 * @date 2023-05-09
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "指令服务管理")
@RequestMapping("/api/instruction")
public class InstructionController {

    private final InstructionService instructionService;

    @GetMapping
    @Log("查询指令")
    @ApiOperation("查询指令")
    //@PreAuthorize("@el.check('instruction:list')")
    public ResponseEntity<Object> query(@RequestParam Map whereJson, Pageable page) {
        //        if(ObjectUtil.isEmpty(whereJson.get("status"))) {
        //            whereJson.put("status", "1");
        //        }
        return new ResponseEntity<>(instructionService.queryAll(whereJson, page), HttpStatus.OK);
    }

    @GetMapping("/getAll")
    @Log("历史查询指令")
    @ApiOperation("历史查询指令")
    //@PreAuthorize("@el.check('instruction:list')")
    public ResponseEntity<Object> get(@RequestParam Map whereJson, Pageable page) {
        return new ResponseEntity<>(instructionService.getAll(whereJson, page), HttpStatus.OK);
    }

    @GetMapping("/unfinish")
    @Log("查询所有未完成指令")
    @ApiOperation("查询所有未完成指令")
    //@PreAuthorize("@el.check('instruction:list')")
    public ResponseEntity<Object> queryUnFinish() {
        return new ResponseEntity<>(instructionService.queryAll("instruction_status < 2"), HttpStatus.OK);
    }

    @GetMapping("/reload")
    @Log("数据同步")
    @ApiOperation("数据同步")
    //@PreAuthorize("@el.check('task:list')")
    public ResponseEntity<Object> reload() {
        instructionService.reload();
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping
    @Log("新增指令")
    @ApiOperation("新增指令")
    //@PreAuthorize("@el.check('instruction:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Instruction dto) throws Exception {
        instructionService.create(dto);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改指令")
    @ApiOperation("修改指令")
    //@PreAuthorize("@el.check('instruction:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Instruction dto) {
        instructionService.update(dto);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除指令")
    @ApiOperation("删除指令")
    //@PreAuthorize("@el.check('instruction:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody String[] ids) throws Exception {
        instructionService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("完成指令")
    @ApiOperation("完成指令")
    @PostMapping(value = "/finish/{id}")
    public ResponseEntity<Object> finish(@RequestBody String id) throws Exception {
        instructionService.finish(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("取消指令")
    @ApiOperation("取消指令")
    @PostMapping(value = "/cancel/{id}")
    public ResponseEntity<Object> cancel(@RequestBody String id) throws Exception {
        instructionService.cancel(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("强制取消指令")
    @ApiOperation("强制取消指令")
    @PostMapping(value = "/forceCancel/{id}")
    public ResponseEntity<Object> forceCancel(@RequestBody String id) throws Exception {
        instructionService.forceCancel(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("导出指令")
    @ApiOperation("导出指令")
    @GetMapping(value = "/download")
    //@PreAuthorize("@el.check('instruction:list')")
    public void download(HttpServletResponse response, Map whereJson) throws IOException {
        instructionService.download(instructionService.queryAll(whereJson), response);
    }


    @Log("缓存查询指令")
    @ApiOperation("缓存查询指令")
    @PostMapping(value = "/findByCodeFromCache/{id}")
    public ResponseEntity<Object> findByCodeFromCache(@RequestBody String id) {
        return new ResponseEntity<>(instructionService.findByCodeFromCache(id), HttpStatus.OK);
    }

    @SaIgnore
    @Log("查询缓存所有指令")
    @ApiOperation("查询缓存指令")
    @PostMapping(value = "/findAllInstFromCache")
    public ResponseEntity<Object> findAllInstFromCache() {
        return new ResponseEntity<>(instructionService.findAllInstFromCache(), HttpStatus.OK);
    }

    @Log("查询指定任务的所有指令")
    @ApiOperation("查询指定任务的所有指令")
    @PostMapping(value = "/queryByTaskId/{taskId}")
    public ResponseEntity<Object> queryByTaskId(@RequestBody String taskId) {
        return new ResponseEntity<>(instructionService.getByTaskId(taskId), HttpStatus.OK);
    }

    @Log("初始化")
    @ApiOperation("初始化")
    @PostMapping(value = "/init/{id}")
    public ResponseEntity<Object> init(@RequestBody String id) throws Exception {
        instructionService.init(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
