TaskController.java 3.59 KB
package vion.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.liaochong.myexcel.core.DefaultExcelBuilder;
import com.github.liaochong.myexcel.utils.AttachmentExportUtil;
import com.github.liaochong.myexcel.utils.WatermarkUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;
import vion.config.UserNameConverter;
import vion.dto.TaskDTO;
import vion.model.FaultLog;
import vion.service.IFaultLogService;
import vion.service.ITaskService;
import vion.vo.TaskVO;
import vion.vo.UserVO;

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

@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
@Slf4j
public class TaskController {

    private final ITaskService taskService;
    private final IFaultLogService faultLogService;
    private final UserNameConverter userNameConverter;

    @GetMapping("/tasks")
    @SaCheckPermission(value = "task:list", orRole = "admin")
    public Page<TaskVO> getTaskList(TaskDTO data) {
        return taskService.getTaskList(data);
    }

    @GetMapping("/task")
    @SaCheckPermission(value = "task:query", orRole = "admin")
    public TaskVO getTaskById(Long taskId) {
        return taskService.getTaskById(taskId);
    }

    @PostMapping("/tasks")
    @SaCheckPermission(value = "task:editAndSave", orRole = "admin")
    public Long saveOrUpdate(TaskDTO data, @RequestHeader String token) {
        return taskService.circTask(data, token);
    }

    @GetMapping("/faultLogs")
    @SaCheckPermission(value = "task:log:list", orRole = "admin")
    public List<FaultLog> getFaultLogById(Long storeId, Long taskId) {

        List<FaultLog> faultLogList = faultLogService.lambdaQuery()
                .eq(FaultLog::getStoreId, storeId)
                .eq(FaultLog::getTaskId, taskId)
                .orderByAsc(FaultLog::getCreateTime)
                .list();

        TaskVO taskVO = taskService.getTaskById(taskId);
        // 进行中的任务,在最后追加一条当前处理人正在处理的记录,仅做显示用
        if (taskVO.getStatus() == 2) {
            FaultLog faultLog = new FaultLog();
            faultLog.setOperator(taskVO.getActiveUser());
            faultLog.setContent("工单正在处理中");
            faultLog.setCreateTime(new Date());
            faultLogList.add(faultLog);
        }
        return faultLogList;
    }

    @PostMapping("/task/batchIns")
    @SaCheckPermission(value = "task:batchIns", orRole = "admin")
    // todo 未加权限
    public String batchIns(@RequestBody List<TaskDTO> data) {
        return taskService.batchIns(data);
    }

    @GetMapping("/task/export")
    @SaCheckPermission(value = "task:export", orRole = "admin")
    // todo 未加权限
    public void defaultBuild(TaskDTO data, HttpServletResponse response) {
        UserVO user = (UserVO) StpUtil.getTokenSession().get("curLoginUser");
        data.setPageSize(9999);
        Page<TaskVO> voPage = taskService.getTaskList(data);
        Workbook workbook = DefaultExcelBuilder.of(TaskVO.class)
                .binding(userNameConverter)
                .build(voPage.getRecords());
        WatermarkUtil.addWatermark(workbook, user.getUsername() + "-" + user.getPhone());
        AttachmentExportUtil.export(workbook, StrUtil.format("任务列表_{}", DateUtil.formatDateTime(new Date())), response);
    }
}