Newer
Older
huludao / src / main / java / com / newfiber / api / pc / controller / TaskController.java
package com.newfiber.api.pc.controller;

import com.newfiber.api.config.FileVisitProperty;
import com.newfiber.api.core.annotation.SysLog;
import com.newfiber.api.core.bean.ReqBodyObj;
import com.newfiber.api.core.bean.RespBodyObj;
import com.newfiber.api.core.commons.ResultCode;
import com.newfiber.api.core.commons.ResultObj;
import com.newfiber.api.pc.dao.Data_User_InfoDao;
import com.newfiber.api.pc.model.entity.Task;
import com.newfiber.api.pc.model.entity.TaskFile;
import com.newfiber.api.pc.model.vo.TaskParam;
import com.newfiber.api.pc.model.vo.TaskProcessParam;
import com.newfiber.api.pc.service.TaskFileService;
import com.newfiber.api.pc.service.TaskService;
import com.newfiber.api.pc.service.WarnTeamBossService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/task")
@Api(value = "task",tags = "应急指挥任务", position = 103)
public class TaskController {

	/** 节点文件保存路径 */
	@Value("${xnFile.pic}")
	private String xnFilePath;

	@Autowired
	private FileVisitProperty fileVisitProperty;

	@Autowired
	private TaskService taskService;

	@Autowired
	private TaskFileService taskFileService;

	@Autowired
	private Data_User_InfoDao userInfoDao;

	@Autowired
	private WarnTeamBossService warnTeamBossService;

	@ApiOperation(value = "任务列表")
	@PostMapping(value="/taskList")
	@ResponseBody
	@SysLog(actionType = "4",value = "任务列表")
	public ResultObj taskList(HttpServletRequest request, @RequestBody ReqBodyObj<Map<String, Object>> params){
		Integer pageSize = params.getSize();
		Integer pageNo = params.getCurrent();
		Integer userType = StringUtils.isEmpty(params.getData().get("userType"))?null:Integer.valueOf(params.getData().get("userType").toString());
		Integer type = StringUtils.isEmpty(params.getData().get("taskState"))?null:Integer.valueOf(params.getData().get("taskState").toString());
		String taskName = StringUtils.isEmpty(params.getData().get("taskName"))?null:String.valueOf(params.getData().get("taskName").toString());
		String userNo = request.getHeader("userNo");
		return new ResultObj<>(ResultCode.OK, taskService.taskPcList(type,userNo,userType,taskName,pageNo,pageSize));
	}

	@ApiOperation(value = "任务详情")
	@PostMapping(value="/taskInfo")
	@ResponseBody
	@SysLog(actionType = "4",value = "任务详情")
	public ResultObj taskInfo(@RequestBody ReqBodyObj<Map<String, Object>> params,HttpServletRequest request){
        Integer pageSize = params.getSize();
        Integer pageNo = params.getCurrent();
		String userNo = request.getHeader("userNo");
		Integer taskId = Integer.valueOf(params.getData().get("taskId").toString());
        Integer type = StringUtils.isEmpty(params.getData().get("type"))?null:Integer.valueOf(params.getData().get("type").toString());
        Integer orgType = StringUtils.isEmpty(params.getData().get("orgType"))?null:Integer.valueOf(params.getData().get("orgType").toString());
		//String userNo = request.getHeader("userNo");
		return new ResultObj<>(ResultCode.OK, taskService.taskPcInfo(userNo,taskId,type,orgType,pageNo,pageSize));
	}

	@ApiOperation(value = "组织类型")
	@PostMapping(value="/taskOrgType")
	@ResponseBody
	@SysLog(actionType = "4",value = "组织类型")
	public ResultObj taskOrgType(){
		return new ResultObj<>(ResultCode.OK, warnTeamBossService.taskOrgType());
	}

	@ApiOperation(value = "任务详情")
	@PostMapping(value="/taskCount")
	@ResponseBody
	@SysLog(actionType = "4",value = "任务数量")
	public ResultObj taskCount(HttpServletRequest request, @RequestBody ReqBodyObj<Map<String, Object>> params){
		Integer taskId = Integer.valueOf(params.getData().get("taskId").toString());
		Integer type = StringUtils.isEmpty(params.getData().get("type"))?null:Integer.valueOf(params.getData().get("type").toString());
		Integer orgType = StringUtils.isEmpty(params.getData().get("orgType"))?null:Integer.valueOf(params.getData().get("orgType").toString());
		return new ResultObj<>(ResultCode.OK, taskService.taskCount(taskId,orgType));
	}

	@ApiOperation(value = "pc端任务指派人员列表")
	@PostMapping(value="/userInfo")
	@ResponseBody
	@SysLog(actionType = "4",value = "pc端任务指派人员列表")
	public ResultObj userInfo(HttpServletRequest request){
		String userName = request.getHeader("userNo");
		return new ResultObj<>(ResultCode.OK, userInfoDao.getUserInfo(userName));
	}

	@ApiOperation(value = "pc端修改操作")
	@PostMapping(value="/update")
	@ResponseBody
	@SysLog(actionType = "3",value = "pc端修改操作")
	public ResultObj update(@RequestBody ReqBodyObj<Task> param){
		return new ResultObj<>(ResultCode.OK, taskService.updateById(param.getData()));
	}

	@ApiOperation(value = "创建任务")
	@PostMapping(value="/create")
	@ResponseBody
	@SysLog(actionType = "1",value = "创建任务")
	public ResultObj create(@RequestBody ReqBodyObj<TaskParam> param){
		return new ResultObj<>(ResultCode.OK, taskService.create(param.getData()));
	}

	@PostMapping("/createOneMapCreate")
	@ResponseBody
	public  ResultObj createOneMapCreate(@RequestBody TaskParam param){
		param.setDutyUser(param.getCreateUser());
		param.setDutyUserName(param.getCreateUserName());
		return new ResultObj<>(ResultCode.OK, taskService.createOneMapCreate(param));
	}


	@ApiOperation(value = "已读操作")
	@PostMapping(value="/isread")
	@ResponseBody
	@SysLog(actionType = "3",value = "已读操作")
	public ResultObj isread(@RequestBody ReqBodyObj<TaskProcessParam> taskProcessParam){
		return new ResultObj<>(ResultCode.OK, taskService.isread(taskProcessParam.getData()));
	}

	@ApiOperation(value = "审核任务")
	@PostMapping(value="/checkTask")
	@ResponseBody
	@SysLog(actionType = "3",value = "审核任务")
	public ResultObj checkTask(@RequestBody ReqBodyObj<TaskProcessParam> taskProcessParam){
		return new ResultObj<>(ResultCode.OK, taskService.checkTask(taskProcessParam.getData()));
	}

	@ApiOperation(value = "拒绝任务")
	@PostMapping(value="/refuseTask")
	@ResponseBody
	@SysLog(actionType = "3",value = "拒绝任务")
	public ResultObj refuseTask(@RequestBody ReqBodyObj<TaskProcessParam> taskProcessParam){
		return new ResultObj<>(ResultCode.OK, taskService.refuseTask(taskProcessParam.getData()));
	}

	@ApiOperation(value = "通过任务")
	@PostMapping(value="/passTask")
	@ResponseBody
	@SysLog(actionType = "3",value = "通过任务")
	public ResultObj passTask(@RequestBody ReqBodyObj<TaskProcessParam> taskProcessParam){
		return new ResultObj<>(ResultCode.OK, taskService.passTask(taskProcessParam.getData()));
	}

	@ApiOperation(value = "关闭任务")
	@PostMapping(value="/closeTask")
	@ResponseBody
	@SysLog(actionType = "3",value = "关闭任务")
	public ResultObj closeTask(@RequestBody ReqBodyObj<TaskProcessParam> taskProcess){
		return new ResultObj<>(ResultCode.OK, taskService.closeTask(taskProcess.getData()));
	}

	@ApiOperation(value = "给任务添加人员")
	@PostMapping(value="/relationUser")
	@ResponseBody
	public ResultObj relationUser(@RequestBody ReqBodyObj<TaskParam> param){
		return new ResultObj<>(ResultCode.OK, taskService.relationUser(param.getData(),param.getData().getId()));
	}

	@ApiOperation(value = "人员列表")
	@PostMapping(value="/userTree")
	@ResponseBody
	@SysLog(actionType = "4",value = "人员列表")
	public ResultObj userTree(){
		return new ResultObj<>(ResultCode.OK, warnTeamBossService.userTree());
	}

	@ApiOperation(value = "人员列表")
	@PostMapping(value="/userTreeNew")
	@ResponseBody
	@SysLog(actionType = "4",value = "人员列表")
	public ResultObj userTreeNew(@RequestBody ReqBodyObj<Map<String, Object>> params){
		String searchStr = StringUtils.isEmpty(params.getData().get("searchStr"))?null:String.valueOf(params.getData().get("searchStr").toString());
		return new ResultObj<>(ResultCode.OK, warnTeamBossService.userSearchTree(searchStr));
	}

	@ApiOperation(value = "上传图片")
	@ResponseBody
	@PostMapping("/uploadBatch")
	public RespBodyObj upload(@RequestParam("file") MultipartFile[] file, HttpServletRequest request) throws Exception {
		StringBuilder strBD = new StringBuilder();
		strBD.append("|upload");
		String userNo = request.getHeader("userNo");
		List<String> imagePath = new ArrayList<>();
		List<TaskFile> result = new ArrayList<>();
		if (file != null) {
			for (int i = 0; i < file.length; i++) {
				MultipartFile file1 = file[i];
				String path = "";
				try {
					String filename = file1.getOriginalFilename();
					String suffix = filename.substring(filename.lastIndexOf("."));
					Calendar instance = Calendar.getInstance();
					//String filePath = instance.get(Calendar.YEAR)  + File.separator +(instance.get(Calendar.MARCH) + 1) + File.separator + filename;
					StringBuffer sb = new StringBuffer();
					sb.append(instance.get(Calendar.YEAR))
							.append(File.separator)
							.append(instance.get(Calendar.MARCH) + 1).
							append(File.separator)
							.append(System.currentTimeMillis()).append(suffix);
					File fileName = new File(xnFilePath ,  sb.toString());
					path = sb.toString();
					if(!fileName.getParentFile().exists()){
						fileName.mkdirs();
					}
					try{
						file1.transferTo(fileName);
					}catch (IOException e){
						return RespBodyObj.error("上传失败");
					}
				} catch (Exception ex) {
					strBD.append("|ex:").append(ex.toString());
				} finally {
					//logger.info(strBD.toString());
				}
				imagePath.add(path);
			}
			result = batchInsertImg(userNo, imagePath);
		}
		return RespBodyObj.ok(result);
	}

	/**
	 * 批量保存图片
	 * @param userNo
	 * @param imagePath
	 */
	private List<TaskFile> batchInsertImg(String userNo, List<String> imagePath) {
		List<TaskFile> result = new ArrayList<>();
		try {
			if(!imagePath.isEmpty()){
				List<TaskFile> batch = new ArrayList<>();
				for (String e : imagePath) {
					TaskFile riverPics = new TaskFile();
					riverPics.setFilePath(e);
					riverPics.setRealPath(fileVisitProperty.getWebPic()+e);
					result.add(riverPics);
				}
				taskFileService.insertBatch(result);
			}
		}catch (Exception e){
			//logger.info(e.toString());
		}
		return result;
	}
}