TaskController.java 13.9 KB
package vion.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import vion.Global;
import vion.enums.ResultEnum;
import vion.model.*;
import vion.service.*;
import vion.service.impl.UserServiceImpl;
import vion.utils.ResultUtil;
import vion.vo.FaultLogVO;
import vion.vo.ProductVO;
import vion.vo.TaskVO;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping(Global.BASE_URL)
@Slf4j
public class TaskController {

    @Autowired
    private ITaskTempSerrvice taskTempSerrvice;
    @Autowired
    private ITaskSerrvice taskSerrvice;
    @Autowired
    private IFileSerrvice fileSerrvice;
    @Autowired
    private IFaultLogSerrvice faultLogSerrvice;
    @Autowired
    private FileController fileController;
    @Autowired
    private IStoreSerrvice storeSerrvice;
    @Autowired
    private UserController userController;
    @Autowired
    private IUserSerrvice iUserSerrvice;

    @GetMapping("/tasks")
    @ResponseBody
    public Object gettaskList(@RequestParam(name = "accountId",required=false)Integer account_id,
                              @RequestParam(name = "storeId",required=false)Integer store_id,
                              @RequestParam(name = "faultType",required=false)Integer fault_type,
                              @RequestParam(name = "status",required=false)Integer status,
                              @RequestParam(name = "startdate",required=false)
                                  @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") Date startdate,
                              @RequestParam(name = "enddate",required=false)
                                  @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") Date enddate,
                              @RequestParam(name = "pageNum")Integer pageNum,
                              @RequestParam(name = "pageSize")Integer pageSize){

        //pageNum当前页,pageSize每页条数
        Page page= PageHelper.startPage(pageNum,pageSize);

        QueryWrapper<Task> wrapper=new QueryWrapper<>();
        if(store_id!=null){
            wrapper.eq("store_id",store_id);
        }
        if(account_id!=null)
            wrapper.eq("account_id",account_id);
        if(fault_type!=null)
            wrapper.eq("fault_type",fault_type);
        if(status!=null)
            wrapper.eq("status",status);
        if(startdate!=null)
            wrapper.between("repair_date",startdate,enddate);

        List<Task> taskList=taskSerrvice.list(wrapper);

        List<Store> storeList=storeSerrvice.list();
        List<TaskVO> taskVOS=new ArrayList<>();
        TaskVO taskVO;
        for(Task task:taskList){
            taskVO=new TaskVO();
            taskVO.setStoreName(storeList.stream().filter(store -> store.getId().equals(task.getStoreId())).collect(Collectors.toList()).get(0).getName());
            taskVO.setId(task.getId());
            taskVO.setStoreId(task.getStoreId());
            taskVO.setRepairDate(task.getRepairDate());
            taskVO.setFaultType(task.getFaultType());
            taskVO.setFaultDescription(task.getFaultDescription());
            taskVO.setRepairPeople(task.getRepairPeople());
            taskVO.setRepairPhone(task.getRepairPhone());
            taskVO.setStatus(task.getStatus());
            taskVO.setFaultReason(task.getFaultReason());
            taskVO.setSolveDate(task.getSolveDate());
            taskVO.setSolveType(task.getSolveType());
            taskVO.setSolveDescription(task.getSolveDescription());
            taskVO.setCreateUser(task.getCreateUser());
            taskVO.setActiveUser(task.getActiveUser());
            taskVO.setCreateTime(task.getCreateTime());
            taskVO.setModifyTime(task.getModifyTime());
            taskVO.setRemark(task.getRemark());
            taskVO.setAccountId(task.getAccountId());

            taskVOS.add(taskVO);
        }
        taskVOS.sort(Comparator.comparing(TaskVO::getId));
        PageInfo pageInfo=new PageInfo<>(page);
        pageInfo.setList(taskVOS);

        if (pageInfo != null) {
            return ResultUtil.success(pageInfo);
        } else {
            return ResultUtil.error(ResultEnum.SELECT_ERROR);
        }
    }

    @GetMapping("/task")
    @ResponseBody
    public Object getTaskByID(@RequestParam(name = "taskId")Integer task_id){

        Task task=taskSerrvice.getById(task_id);
        List<File> files=fileController.getFileList(task.getStoreId(),task.getId());
        TaskVO taskVO=new TaskVO();
        taskVO.setId(task.getId());
        taskVO.setStoreId(task.getStoreId());
        taskVO.setRepairDate(task.getRepairDate());
        taskVO.setFaultType(task.getFaultType());
        taskVO.setFaultDescription(task.getFaultDescription());
        taskVO.setRepairPeople(task.getRepairPeople());
        taskVO.setRepairPhone(task.getRepairPhone());
        taskVO.setStatus(task.getStatus());
        taskVO.setFaultReason(task.getFaultReason());
        taskVO.setSolveDate(task.getSolveDate());
        taskVO.setSolveType(task.getSolveType());
        taskVO.setSolveDescription(task.getSolveDescription());
        taskVO.setCreateUser(task.getCreateUser());
        taskVO.setActiveUser(task.getActiveUser());
        taskVO.setCreateTime(task.getCreateTime());
        taskVO.setModifyTime(task.getModifyTime());
        taskVO.setRemark(task.getRemark());
        taskVO.setAccountId(task.getAccountId());
        taskVO.setFileList(files);

        if (taskVO != null) {
            return ResultUtil.success(taskVO);
        } else {
            return ResultUtil.error(ResultEnum.SELECT_ERROR);
        }
    }

    @PostMapping("/tasks")
    @ResponseBody
    public Object saveOrUpdate(@RequestBody TaskVO data,@RequestAttribute String userid) {

        try {
            User user=Global.USERNAME_MAP.get(userid);

            Task task=new Task();
            task.setAccountId(data.getAccountId());
            task.setStoreId(data.getStoreId());
            task.setFaultType(data.getFaultType());
            task.setFaultDescription(data.getFaultDescription());
            task.setRepairDate(data.getRepairDate());
            task.setRepairPeople(data.getRepairPeople());
            task.setRepairPhone(data.getRepairPhone());
            task.setStatus(data.getStatus());
            if(task.getId()==null){
                task.setCreateUser(user.getId());
            }
            task.setActiveUser(data.getActiveUser());


            if(data.getTasktempId()!=null){
                //得到预处理工单
                TaskTemp taskTemp= taskTempSerrvice.getById(data.getTasktempId());
                //更新预处理工单状态
                taskTemp.setStatus(2);//状态(1待确认、2已确认)
                taskTemp.setModifyTime(new Date());
                taskTemp.setOperator(data.getActiveUser());
                taskTemp.setStoreId(data.getStoreId());
                taskTemp.setModifyTime(new Date());
                taskTempSerrvice.saveOrUpdate(taskTemp);

                //根据预处理工单更新附件的门店ID
                fileSerrvice.lambdaUpdate()
                        .setSql("store_id="+data.getStoreId())
                        .eq(File::getSourceId,data.getId())
                        .update();
            }
            if(data.getId()==null){
                //添加新工单
                Task work=taskSerrvice.saveAndReturn(task);

                if(data.getFileList()!=null){
                    //添加新工单的附件信息
                    if(work!=null){
                        File file;
                        for(File fileModel:data.getFileList()){
                            file=new File();
                            file.setId(fileModel.getId());
                            file.setStoreId(work.getStoreId());
                            file.setSourceId(work.getId());
                            file.setSourceType(3);//1项目、2工单预处理,3工单操作,4巡检
                            file.setName(fileModel.getName());
                            file.setUrl(fileModel.getUrl());
                            file.setType(fileModel.getType());
                            fileSerrvice.saveOrUpdate(file);
                        }
                    }
                }

                //添加工单处理日志
                FaultLog faultLog=new FaultLog();
                faultLog.setTaskId(work.getId());
                faultLog.setStoreId(work.getStoreId());
                faultLog.setOperator(user.getId());
                faultLog.setContent("创建工单");
                faultLog.setRemark("");
                faultLogSerrvice.saveOrUpdate(faultLog);

                //发送钉钉提醒
                User activeUser= iUserSerrvice.getById(work.getActiveUser());
                userController.sendMessage(activeUser.getUserid(),work.getId());
            }
            else{
                task.setId(data.getId());
                task.setSolveDate(data.getSolveDate());
                task.setFaultReason(data.getFaultReason());
                task.setSolveType(data.getSolveType());
                task.setSolveDescription(data.getSolveDescription());
                task.setRemark(data.getRemark());
                taskSerrvice.saveOrUpdate(task);

                //根据状态判断是否要添加工单处理日志(//状态:0待确认1进行中2已完成3挂起)
                if(data.getStatus()==1){
                    FaultLog faultLog=new FaultLog();
                    faultLog.setTaskId(data.getId());
                    faultLog.setStoreId(data.getStoreId());
                    faultLog.setOperator(user.getId());
                    faultLog.setContent("工单正在处理中");
                    faultLog.setRemark(data.getSolveDescription());
                    faultLogSerrvice.saveOrUpdate(faultLog);
                }
                if(data.getStatus()==2){
                    FaultLog faultLog=new FaultLog();
                    faultLog.setTaskId(data.getId());
                    faultLog.setStoreId(data.getStoreId());
                    faultLog.setOperator(user.getId());
                    faultLog.setContent("工单处理完成");
                    faultLog.setRemark(data.getSolveDescription());
                    faultLogSerrvice.saveOrUpdate(faultLog);
                }
                if(data.getStatus()==3){
                    FaultLog faultLog=new FaultLog();
                    faultLog.setTaskId(data.getId());
                    faultLog.setStoreId(data.getStoreId());
                    faultLog.setOperator(user.getId());
                    faultLog.setContent("工单挂起");
                    faultLog.setRemark(data.getSolveDescription());
                    faultLogSerrvice.saveOrUpdate(faultLog);
                }

                //判断是否要添加工单处理文件
                if(data.getFileList()!=null){
                    //添加新工单的附件信息
                    for(File fileModel:data.getFileList()){
                        File file=new File();
                        file.setId(fileModel.getId());
                        file.setStoreId(data.getStoreId());
                        file.setSourceId(data.getId());
                        file.setSourceType(3);//1项目、2工单预处理,3工单操作,4巡检
                        file.setName(fileModel.getName());
                        file.setUrl(fileModel.getUrl());
                        file.setType(fileModel.getType());
                        fileSerrvice.saveOrUpdate(file);
                    }

                    //判断是否有要删除的文件信息

                }
            }



            return ResultUtil.success();
        }catch (Exception e) {
            return e.getMessage();
        }
    }

    @GetMapping("/faultLogs")
    @ResponseBody
    public Object getFaultLogByID(@RequestParam(name = "storeId")Integer store_id,
                                  @RequestParam(name = "taskId")Integer task_id){

        List<FaultLog> faultLogs=faultLogSerrvice.list(Wrappers.<FaultLog>lambdaQuery()
                .eq(FaultLog::getStoreId,store_id)
                .eq(FaultLog::getTaskId,task_id));

        if (faultLogs != null) {
            return ResultUtil.success(faultLogs);
        } else {
            return ResultUtil.error(ResultEnum.SELECT_ERROR);
        }
    }

    @PostMapping("/faultLogs")
    @ResponseBody
    public Object saveAddFaultLog(@RequestBody FaultLogVO data,@RequestAttribute String userid) {

        try {
            User user=Global.USERNAME_MAP.get(userid);
            FaultLog faultLog=new FaultLog();
            faultLog.setTaskId(data.getTaskId());
            faultLog.setStoreId(data.getStoreId());
            faultLog.setOperator(user.getId());
            faultLog.setContent(data.getContent());
            faultLog.setRemark(data.getRemark());
            faultLogSerrvice.saveOrUpdate(faultLog);

            if(data.getFilename()!=null){
                //添加文件附件
                File file=new File();
                file.setStoreId(data.getStoreId());
                file.setSourceId(data.getTaskId());
                file.setSourceType(3);//1项目、2工单预处理,3工单操作,4巡检
                file.setName(data.getFilename());
                file.setUrl(data.getFileurl());
                file.setType(1);
                //fileDao.addFile(file);
                fileSerrvice.save(file);
            }
            return ResultUtil.success();
        }catch (Exception e) {
           // return ResultUtil.error(ResultEnum.SELECT_ERROR);
            return e.getMessage().toString();
        }
    }

}