package cn.edu.whu.boot.statistics.controller;

import cn.edu.whu.boot.common.domain.ResultMessage;
import cn.edu.whu.boot.statistics.service.ProgressStatService;
import cn.edu.whu.boot.xml.service.ITaskService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import cn.edu.whu.boot.xml.domain.Task;
import cn.edu.whu.boot.user.services.UserService;

/**
 * @author Li Haoming
 */
@RestController
@RequestMapping("/statistic")
public class ProgressStatController {

    private final ProgressStatService progressStatService;

    private final ITaskService taskService;
    private final UserService userService;

    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public ProgressStatController(ITaskService taskService, ProgressStatService progressStatService, UserService userService) {
        this.taskService = taskService;
        this.progressStatService = progressStatService;
        this.userService = userService;
    }

    /**
     * 返回任务列表
     * @return
     */
    @RequestMapping("/getTasks")
    public String getTasks() {
        return progressStatService.getTasks();
    }


    @RequestMapping("/getTables")
    public String getTaskTables(@Param("tskName") String tskName) {
        return progressStatService.getTables(tskName);
    }
    //获取全部表
    @RequestMapping("/getAllTables")
    public String getAllTables(@Param("tskName") String tskName) {
        return progressStatService.getAllTables(tskName);
    }

    @RequestMapping("/getWorkers")
    public String getWorkers(@Param("taskId") int taskId) {
        Task task = taskService.getTaskById(taskId);
        return task.getIsAllocationOptional() == 0?
        new ResultMessage<>(true, "", taskService.getWorkersByTask(taskId)).toString():
        new ResultMessage<>(true, "", userService.findAll()).toString();

    }


    @RequestMapping("/stat")
    public String proStat(@Param("tskName") String tskName,
                          @Param("tblName") String tblName,
                          @Param("operation") String operation,
                          @Param("startTime") String startTime,
                          @Param("endTime") String endTime) {

        try {
            Date sTime = DATE_FORMAT.parse(startTime);
            Date eTime = DATE_FORMAT.parse(endTime);

            if (sTime.after(eTime)) {
                return new ResultMessage<String>(false, "起始时间晚于开始时间",400).toString();
            }
        } catch (Exception e) {
            return new ResultMessage<String>(false, "日期格式不正确", 400).toString();
        }


        if ("day".equals(operation) || "month".equals(operation)) {
            return progressStatService.stat(operation, tskName, tblName, startTime, endTime);
        } else {
            return new ResultMessage<String>(false, "查询操作非法").toString();
        }
    }



    @RequestMapping("/getFields")
    public String getFields(@Param("tblName") String tblName) {
        return null;
    }





}
