package com.ruoyi.web.controller.controllerTask;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.task.TaskEntity;
import com.ruoyi.system.service.taskService.TaskEntityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/task/taskManagement")
public class TaskController extends BaseController {
        @Autowired
        private TaskEntityService taskService;

    /**
     * 任务列表
     * @param taskEntity
     * @return
     */
        @PreAuthorize("@ss.hasPermi('task:taskManagement:list')")
        @GetMapping("/list")
        public TableDataInfo list(TaskEntity taskEntity)
        {
            startPage();
            List<TaskEntity> list = taskService.selectAdminTaskList(taskEntity);
            return getDataTable(list);
        }
    /**
     * 查询任務详细
     * @param taskId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('task:taskManagement:query')")
    @GetMapping(value = { "/{taskId}" })
    public AjaxResult getInfo(@PathVariable(value = "taskId", required = false) Long taskId) {
        if (taskId != null) {
            TaskEntity task = taskService.selectAdminTaskById(taskId);
            if (task != null) {
                return AjaxResult.success(task);
            } else {
                return AjaxResult.error("任务不存在");
            }
        } else {
            return AjaxResult.success();
        }
    }
    /**
     * 新增用户
     */
    @PreAuthorize("@ss.hasPermi('task:taskManagement:add')")
    @Log(title = "任务管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TaskEntity adminTaskEntity)
    {
        String operName = getUsername();
        Long operId = getUserId();
        if (!taskService.checkTaskNameUnique(adminTaskEntity))
        {
            return error("新增任务'" + adminTaskEntity.getTaskName() + "'失败，名称已存在");
        }
        return toAjax(taskService.insertAdminTask(adminTaskEntity));
    }
    /**
     * 修改任务基本信息
     */
//    @PreAuthorize("@ss.hasPermi('task:taskManagement:edit')")
//    @PutMapping("/updateTaskById")
//    public AjaxResult editUser(@Validated @RequestBody TaskEntity taskEntity) {
//        if (!taskService.checkTaskNameUnique(taskEntity)) {
//            return AjaxResult.error("修改任务'" + taskEntity.getTaskName() + "'失败，该任务名称已存在");
//        }
//        return toAjax(taskService.updateAdminTaskById(taskEntity));
//    }

    /**
     * 修改任务基本信息
     * @param taskEntity
     * @return
     */
    @PreAuthorize("@ss.hasPermi('task:taskManagement:edit')")
    @Log(title = "任务管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult editTaskProfile(@Validated @RequestBody TaskEntity taskEntity) {

        if (!taskService.checkTaskNameUnique(taskEntity)) {
            return AjaxResult.error("修改任务'" + taskEntity.getTaskName() + "'失败，该任务名称已存在");
        }
        return toAjax(taskService.updateAdminTaskById(taskEntity));
    }

    /**
     * 修改任务状态
     * @param taskEntity
     * @return
     */
    @PreAuthorize("@ss.hasPermi('task:taskManagement:edit')")
    @Log(title = "任务管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody TaskEntity taskEntity)
    {
        return toAjax(taskService.updateAdminTaskById(taskEntity));
    }

    /**
     * 导入任务模板下载
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<TaskEntity> util = new ExcelUtil<TaskEntity>(TaskEntity.class);
        util.importTemplateExcel(response, "任务数据");
    }

    /**
     * 任务数据导出
     * @param response
     * @param taskEntity
     */
    @Log(title = "任务管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('task:taskManagement:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, TaskEntity taskEntity)
    {
        List<TaskEntity> list = taskService.selectAdminTaskList(taskEntity);
        ExcelUtil<TaskEntity> util = new ExcelUtil<TaskEntity>(TaskEntity.class);
        util.exportExcel(response, list, "任务数据");
    }

    /**
     * 任务批量导入
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "任务管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('task:taskManagement:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TaskEntity> util = new ExcelUtil<TaskEntity>(TaskEntity.class);
        List<TaskEntity> taskLists = util.importExcel(file.getInputStream());
        String operName = getUsername();
        Long operId = getUserId();
        String message = taskService.importTaskData(taskLists, updateSupport, operName,operId);
        return success(message);
    }
}
