package com.chushang.inspection.project.controller;

import com.chushang.common.core.validator.Create;
import com.chushang.common.core.validator.Update;
import com.chushang.common.core.web.AjaxResult;
import com.chushang.common.log.annotation.SysLog;
import com.chushang.common.log.enums.BusinessType;
import com.chushang.common.mybatis.page.CommonParam;
import com.chushang.inspection.project.dto.Delete;
import com.chushang.inspection.project.dto.TaskDTO;
import com.chushang.inspection.project.dto.TaskExcelDTO;
import com.chushang.inspection.project.dto.TaskQuery;
import com.chushang.inspection.project.service.TbTemplateService;
import com.chushang.inspection.project.vo.ConfigVO;
import com.chushang.inspection.project.vo.TaskVO;
import com.chushang.inspection.project.service.PollingTaskService;
import com.chushang.security.annotation.RequiresPermissions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * @auther: zhao
 * @date: 2024/6/18 15:41
 */
@Slf4j
@RestController
@RequestMapping(value = "/task")
public class PollTaskController {

    @Resource
    PollingTaskService pollingTaskService;
    @Resource
    TbTemplateService tbTemplateService;


    /**
     * 查询任务列表
     */
    @GetMapping("/list")
    @RequiresPermissions("store:task:list")
    public AjaxResult findList(TaskQuery taskQuery) {
        return AjaxResult.success(pollingTaskService.findList(taskQuery));
    }

    /**
     * 查询顶级任务列表
     */
    @GetMapping("/list/top")
    public AjaxResult findTopList(){
        return AjaxResult.success(pollingTaskService.findTopList(CommonParam.buildPageRequest()));
    }

    /**
     * 查询顶级任务配置
     */
    @GetMapping("/config/{taskId}")
    public AjaxResult findTaskConfig(@PathVariable Long taskId) {
        return AjaxResult.success(pollingTaskService.findTaskConfig(taskId));
    }

    /**
     * 获取详细信息
     *
     * @param taskId 任务id
     * @return 详情
     */
    @GetMapping("/info/{taskId}")
//    @RequiresPermissions("store:task:info")
    public AjaxResult getTaskById(@PathVariable Long taskId) {
        return AjaxResult.success(pollingTaskService.getTaskById(taskId));
    }

    /**
     * 新增任务
     *
     * @param task 任务信息
     */
    @PostMapping("/save")
//    @RequiresPermissions("store:task:save")
    @SysLog( value = "任务", businessType = BusinessType.INSERT)
    public AjaxResult save(@RequestBody @Validated(Create.class) TaskDTO task) {
        Long taskId = pollingTaskService.saveTask(task);
        return AjaxResult.success(taskId);
    }


    /**
     * 修改
     *
     * @param task 任务信息
     */
    @PostMapping("/{taskId}")
//    @RequiresPermissions("store:task:update")
    @SysLog(value = "任务", businessType = BusinessType.UPDATE)
    public AjaxResult update(@RequestBody @Validated(Update.class) TaskDTO task,
                             @PathVariable Long taskId)
    {
        return AjaxResult.success(pollingTaskService.updateTask(task, taskId));
    }

    /**
     * 删除任务
     *
     * @param taskId 删除的id
     */
    @DeleteMapping("/del/{taskId}")
//    @RequiresPermissions("store:task:delete")
    @SysLog(value = "任务", businessType = BusinessType.DELETE)
    public AjaxResult delete(@PathVariable Long taskId) {
        pollingTaskService.removeById(taskId);
        return AjaxResult.success();
    }

    /**
     * 批量删除任务
     *
     * @param delete 删除的id 集合
     */
    @PostMapping("/del/batch")
//    @RequiresPermissions("store:task:delete")
    @SysLog(value = "批量任务", businessType = BusinessType.DELETE)
    public AjaxResult delete(@RequestBody Delete delete) {
        List<Long> ids = delete.getIds();
        pollingTaskService.removeBatchByIds(ids);
        return AjaxResult.success();
    }

    /**
     * 导入任务 读取
     */
    @PostMapping("/import/read")
//    @RequiresPermissions("store:task:import")
    public AjaxResult importRead(@RequestParam("file") MultipartFile file) {
        return AjaxResult.success(pollingTaskService.importRead(file));
    }

    /**
     * 导入任务 保存
     */
    @SysLog(value = "任务", businessType = BusinessType.IMPORT)
    @PostMapping("/import/{parentId}")
//    @RequiresPermissions("store:task:save")
    public AjaxResult importSave(@RequestBody List<TaskExcelDTO> taskExcel, @PathVariable Long parentId) {
        pollingTaskService.importSave(taskExcel, parentId);
        return AjaxResult.success();
    }

    @SysLog(value = "导入模板", businessType = BusinessType.DOWNLOAD)
    @PostMapping("/down/template")
//    @RequiresPermissions("task:down:template")
    public AjaxResult downTemplate(@RequestParam(defaultValue = "task_template") String templateAlias,
                                   @RequestParam(defaultValue = "") Long taskId)
    {
        return AjaxResult.success(tbTemplateService.getByAlias(templateAlias, taskId));
    }
}
