package org.nl.acs.task.rest;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import org.nl.common.logging.annotation.Log;
import org.nl.acs.task.domain.Task;
import org.nl.acs.task.service.TaskService;
import org.nl.acs.task.service.dto.TaskDto;
import org.nl.acs.task.service.dto.TaskQueryParam;
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/task")
public class TaskController {

    private final TaskService taskService;

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

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

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

    @PostMapping
    @Log("新增任务")
    @ApiOperation("新增任务")
    //@PreAuthorize("@el.check('task:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody TaskDto dto) throws Exception {
        taskService.create(dto);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PostMapping(value = "/adds")
    @Log("常规任务保存")
    @ApiOperation("常规任务保存")
    //@PreAuthorize("@el.check('task:add')")
    public ResponseEntity<Object> ordinaryTaskCreate(@RequestBody JSONObject json) throws Exception {

        taskService.ordinaryTaskCreate(json);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PostMapping(value = "/addes")
    @Log("特殊任务保存")
    @ApiOperation("特殊任务保存")
    public ResponseEntity<Object> specialTaskCreate(@RequestBody JSONObject json) throws Exception {
        taskService.specialTaskCreate(json);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改任务")
    @ApiOperation("修改任务")
    //@PreAuthorize("@el.check('task:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody TaskDto dto) {
        taskService.update(dto);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

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

    @Log("完成任务")
    @ApiOperation("完成任务")
    @PostMapping(value = "/finish/{id}")
    public ResponseEntity<Object> finish(@RequestBody String id) {
        taskService.finish(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }

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

    @Log("手动创建指令")
    @ApiOperation("手动创建指令")
    @PostMapping(value = "/createInst/{id}")
    public ResponseEntity<Object> createInst(@RequestBody String id) throws Exception {
        taskService.createInst(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }

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

    @Log("一体机触发任务")
    @ApiOperation("一体机触发任务")
    @PostMapping("/createTaskByClick")
    //@PreAuthorize("@el.check('task:add')")
    public ResponseEntity<Object> createTaskByClick(@RequestBody JSONObject json) {
        taskService.createTaskByClick(json);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @SaIgnore
    @Log("查询缓存所有任务")
    @ApiOperation("查询缓存任务")
    @PostMapping(value = "/findAllTaskFromCache")
    public ResponseEntity<Object> findAllTaskFromCache() {
        return new ResponseEntity<>(taskService.findAllTaskFromCache(), HttpStatus.OK);
    }
}
