package com.jsc.oscm.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jsc.oscm.entity.Task;
import com.jsc.oscm.model.ReturnT;
import com.jsc.oscm.service.TaskService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
public class TaskController {

    @Resource
    TaskService taskService;

    @PostMapping("/task")
    public String addTask(@RequestBody Task task) {
        ReturnT<String> returnT;
        if (taskService.saveTaskDirect(task)) {
            returnT = new ReturnT<>(200, "", "");
        } else {
            returnT = new ReturnT<>(500, "创建任务失败", "");
        }
        return JSON.toJSONString(returnT);
    }

    @PutMapping("/task")
    public String updateTask(@RequestBody Task task) {
        ReturnT<String> returnT;
        if (taskService.updateTaskDirect(task)) {
            returnT = new ReturnT<>(200, "", "");
        } else {
            returnT = new ReturnT<>(500, "更新任务失败", "");
        }
        return JSON.toJSONString(returnT);
    }

    @PostMapping("/task/queryPageable/{index}/{size}")
    public String queryPageable(@PathVariable(value = "index") Integer index,
                                @PathVariable(value = "size") Integer size,
                                @RequestBody Task task) {
        ReturnT<List<Task>> returnT;
        try {
            returnT = new ReturnT<>(200,
                    String.valueOf(taskService.queryCount(task)),
                    taskService.queryPageable(task, index, size));
        } catch (Exception e) {
            returnT = new ReturnT<>(500, e.getMessage(), null);
        }
        return JSON.toJSONString(returnT);

    }

    @DeleteMapping("/task")
    public String delTask(@RequestBody List<Integer> taskIds) {
        ReturnT<String> returnT;
        try {
            taskService.delTasks(taskIds);
            returnT = new ReturnT<>(200, "", "");
        } catch (Exception e) {
            returnT = new ReturnT<>(500, e.getMessage(), "");
        }
        return JSON.toJSONString(returnT);
    }

    @PostMapping("/task/status")
    public String queryTasksStatus(@RequestBody List<Integer> taskIds) {
        ReturnT<Map<String, String>> returnT;
        Map<String, String> status = taskService.getTaskStatus(taskIds);
        if (status != null) {
            returnT = new ReturnT<>(200, "", status);
        } else {
            returnT = new ReturnT<>(500, "", null);
        }
        return JSON.toJSONString(returnT);
    }

    @PostMapping("/task/schedule/{action}")
    public String scheduleTask(@RequestBody List<Integer> ids, @PathVariable(value = "action") String action) {
        ReturnT<String> returnT;
        try {
            if (action.equals("start")) {
                taskService.startTaskByIds(ids);
            } else if (action.equals("single")) {
                taskService.startTaskByWebsiteIds(ids);
            }
            returnT = new ReturnT<>(200, "", "");
        } catch (Exception e) {
            returnT = new ReturnT<>(500, e.getMessage(), null);
        }
        return JSON.toJSONString(returnT);
    }

    @GetMapping("/task/{id}")
    public String findById(@PathVariable(name = "id") int id) {
        Task task = taskService.findTaskById(id);
        ReturnT<Task> returnT = new ReturnT<>(200, "", task);
        return JSON.toJSONString(returnT);
    }

    @GetMapping("/open/task/{taskType}")
    public String getAllTask(@PathVariable(name = "taskType") Integer taskType) {
        List<Task> tasks = taskService.queryTasksByType(taskType);
        ReturnT<List<Task>> returnT = new ReturnT<>(200, "", tasks);
        return JSON.toJSONString(returnT);
    }

    @PostMapping("/open/task")
    public String getTask(@RequestBody String params) {
        List<Task> tasks = taskService.queryTasks(params);
        ReturnT<List<Task>> returnT = new ReturnT<>(200, "", tasks);
        return JSON.toJSONString(returnT);
    }

    @GetMapping("/open/task/website/{websiteId}")
    public String getTaskByWebsiteId(@PathVariable(name = "websiteId") Integer websiteId) {
        List<Task> taskList = taskService.queryAllTaskBySite(websiteId);
        ReturnT<List<Task>> returnT = new ReturnT<>(200, "", taskList);
        return JSON.toJSONString(returnT);
    }

    @PostMapping("/task/getTaskByIds")
    public String getTaskByIds(@RequestBody List<Integer> ids) {
        List<Task> tasks = taskService.getTaskByIds(ids);
        ReturnT<JSONObject> returnT;
        JSONObject jo = new JSONObject();
        tasks.forEach(t -> jo.put(t.getId().toString(), t.getName()));
        returnT = new ReturnT<>(200, "", jo);
        return JSON.toJSONString(returnT);
    }
}
