package com.jeesite.modules.mes.web; import cn.hutool.core.bean.BeanUtil; import com.alibaba.fastjson.JSON; import com.jeesite.common.collect.ListUtils; import com.jeesite.common.collect.MapUtils; import com.jeesite.common.config.Global; import com.jeesite.common.entity.Page; import com.jeesite.common.lang.DateUtils; import com.jeesite.common.lang.ObjectUtils; import com.jeesite.common.lang.StringUtils; import com.jeesite.common.service.ServiceException; import com.jeesite.common.utils.excel.ExcelExport; import com.jeesite.common.utils.excel.ExcelImport; import com.jeesite.common.utils.excel.annotation.ExcelField; import com.jeesite.common.validator.ValidatorUtils; import com.jeesite.common.web.BaseController; import com.jeesite.modules.file.entity.FileUpload; import com.jeesite.modules.file.service.FileUploadService; import com.jeesite.modules.mes.entity.*; import com.jeesite.modules.mes.entity.excel.ExcelMesDevicePreventTask; import com.jeesite.modules.mes.resp.CommonResp; import com.jeesite.modules.mes.service.*; import com.jeesite.modules.sys.entity.User; import com.jeesite.modules.sys.utils.UserUtils; import org.apache.shiro.authz.annotation.Logical; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Transactional; import org.springframework.ui.Model; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolationException; import java.util.ArrayList; import java.util.List; import java.util.Map; /** * 预防性维修计划Controller * @author hzd * @version 2023-08-17 */ @Controller @RequestMapping(value = "${adminPath}/mes/mesDevicePreventTask") public class MesDevicePreventTaskController extends BaseController { @Autowired private MesDevicePreventTaskService mesDevicePreventTaskService; @Resource private FileUploadService fileUploadService; @Resource private MesDeviceService mesDeviceService; @Resource private MesDevicePreventService mesDevicePreventService; /** * 获取数据 */ @ModelAttribute public MesDevicePreventTask get(String id, boolean isNewRecord) { return mesDevicePreventTaskService.get(id, isNewRecord); } /** * 查询列表 */ @RequiresPermissions("mes:mesDevicePreventTask:view") @RequestMapping(value = {"list", ""}) public String list(MesDevicePreventTask mesDevicePreventTask, Model model) { model.addAttribute("mesDevicePreventTask", mesDevicePreventTask); return "modules/mes/mesDevicePreventTaskList"; } /** * 我的计划 */ @RequiresPermissions("mes:mesDevicePreventTask:view") @RequestMapping(value = {"mylist", ""}) public String mylist(MesDevicePreventTask mesDevicePreventTask, Model model) { User user = UserUtils.getUser(); // 标记为我的计划查询,listData 中按关系表过滤 mesDevicePreventTask.setUserCode(user.getUserCode()); model.addAttribute("mesDevicePreventTask", mesDevicePreventTask); model.addAttribute("myTaskOnly", true); return "modules/mes/mesDevicePreventTaskMyList"; } /** * 查询列表数据 */ @RequiresPermissions("mes:mesDevicePreventTask:view") @RequestMapping(value = "listData") @ResponseBody public Page listData(MesDevicePreventTask mesDevicePreventTask, HttpServletRequest request, HttpServletResponse response) { mesDevicePreventTask.setPage(new Page<>(request, response)); // 我的计划:按执行人关系表过滤(兼容原有传 userCode 的方式) String myTaskOnly = request.getParameter("myTaskOnly"); if (Global.TRUE.equals(myTaskOnly) || "1".equals(myTaskOnly) || "true".equalsIgnoreCase(myTaskOnly)) { User user = UserUtils.getUser(); mesDevicePreventTaskService.applyAssigneeFilter(mesDevicePreventTask, user.getUserCode()); } Page page = mesDevicePreventTaskService.findPage(mesDevicePreventTask); return page; } /** * 查看编辑表单 */ @RequiresPermissions("mes:mesDevicePreventTask:view") @RequestMapping(value = "form") public String form(MesDevicePreventTask mesDevicePreventTask, Model model) { model.addAttribute("mesDevicePreventTask", mesDevicePreventTask); return "modules/mes/mesDevicePreventTaskForm"; } /** * 保存数据 * 一条计划可分配多个执行人;允许新增/编辑时增删执行人 * 说明:维护性任务由管理员指派,不校验产线工位操作权限/技能(与上岗扫码场景不同) */ @RequiresPermissions("mes:mesDevicePreventTask:edit") @PostMapping(value = "save") @ResponseBody @Transactional public String save(@Validated MesDevicePreventTask mesDevicePreventTask) { MesDevice mesDevice = new MesDevice(); mesDevice.setTreeCode(mesDevicePreventTask.getDeviceId()); MesDevice mesDevice1 = mesDeviceService.getByEntity(mesDevice); if (ObjectUtils.isEmpty(mesDevice1)) { return renderResult(Global.FALSE, text("保存失败,设备不存在!")); } List userCodes = parseUserCodes(mesDevicePreventTask.getUserCode()); if (userCodes.isEmpty()) { return renderResult(Global.FALSE, text("请选择执行人员!")); } mesDevicePreventTaskService.saveWithAssignees(mesDevicePreventTask, userCodes); return renderResult(Global.TRUE, text("保存预防性维修计划成功!")); } /** * 解析多选执行人(过滤机构节点,仅保留用户) */ private List parseUserCodes(String userCodeStr) { List result = new ArrayList<>(); String[] userCodes = StringUtils.split(StringUtils.trim(userCodeStr), ","); if (userCodes == null) { return result; } for (String code : userCodes) { String userCode = StringUtils.trim(code); if (StringUtils.isBlank(userCode)) { continue; } User user = UserUtils.get(userCode); if (user == null || StringUtils.isBlank(user.getUserCode())) { continue; } String realCode = user.getUserCode(); if (!result.contains(realCode)) { result.add(realCode); } } return result; } /** * 删除数据 */ @RequiresPermissions("mes:mesDevicePreventTask:edit") @RequestMapping(value = "delete") @ResponseBody public String delete(MesDevicePreventTask mesDevicePreventTask) { mesDevicePreventTaskService.delete(mesDevicePreventTask); return renderResult(Global.TRUE, text("删除预防性维修计划成功!")); } /** * 查看编辑表单 */ @RequiresPermissions("mes:mesDevicePreventTask:view") @RequestMapping(value = "deal") public String deal(MesDevicePreventTask mesDevicePreventTask, Model model) { model.addAttribute("mesDevicePreventTask", mesDevicePreventTask); return "modules/mes/mesDevicePreventTaskDeal"; } @RequiresPermissions("mes:mesDevicePreventTask:view") @RequestMapping(value = "mydeal") public String mydeal(MesDevicePreventTask mesDevicePreventTask, Model model) { model.addAttribute("mesDevicePreventTask", mesDevicePreventTask); // 获取检查清单 MesDevicePrevent mesDevicePrevent = mesDevicePreventService.get(mesDevicePreventTask.getPreventId()); model.addAttribute("mesDevicePrevent", mesDevicePrevent); return "modules/mes/mesDevicePreventTaskMyDeal"; } @RequiresPermissions("mes:mesDevicePreventTask:view") @RequestMapping(value = "info") public String info(MesDevicePreventTask mesDevicePreventTask, Model model) { model.addAttribute("mesDevicePreventTask", mesDevicePreventTask); // 获取检查清单 MesDevicePrevent mesDevicePrevent = mesDevicePreventService.get(mesDevicePreventTask.getPreventId()); model.addAttribute("mesDevicePrevent", mesDevicePrevent); return "modules/mes/mesDevicePreventTaskInfo"; } /** * 保存处理结果(Web/PDA共用) * 入参出参保持不变:任一执行人完成即整单完成,并记录实际完成人 */ @RequiresPermissions(value={"mes:mesDevicePreventTask:deal", "mes:mesDevicePreventTask:api"}, logical= Logical.OR) @PostMapping(value = "dealsave") @ResponseBody public String dealsave(@Validated MesDevicePreventTask mesDevicePreventTask,HttpServletRequest request) { // 以数据库状态为准,避免重复提交 MesDevicePreventTask dbTask = mesDevicePreventTaskService.get(new MesDevicePreventTask(mesDevicePreventTask.getId())); if(dbTask == null || dbTask.getState() == null || !dbTask.getState().equals("0")){ return renderResult(Global.TRUE, text("该记录已处理!")); } // 兼容接口 List consumable = JSON.parseArray(request.getParameter("consumable"),MesDeviceConsumableRecord.class); if(consumable!=null && consumable.size() >0){ for (MesDeviceConsumableRecord mesDeviceConsumableRecord : consumable){ if(StringUtils.isEmpty(mesDeviceConsumableRecord.getConsumableId())){ return renderResult(Global.FALSE, text("参数错误!")); } if(mesDeviceConsumableRecord.getNum() <= 0){ return renderResult(Global.FALSE, text("易耗品数量必须大于0!")); } } mesDevicePreventTask.setMesDeviceConsumableRecordList(consumable); } User user = UserUtils.getUser(); // 多执行人:当前用户在关系表中即可处理(不再要求等于主表单个 userCode) if(!mesDevicePreventTaskService.canDeal(mesDevicePreventTask.getId(), user.getUserCode())){ return renderResult(Global.TRUE, text("无权限操作!")); } // 完成时保留原执行人冗余字段,仅更新完成人与状态 mesDevicePreventTask.setUserCode(dbTask.getUserCode()); mesDevicePreventTask.setFinishUserCode(user.getUserCode()); mesDevicePreventTask.setState("1"); mesDevicePreventTaskService.save(mesDevicePreventTask); return renderResult(Global.TRUE, text("保存预防性维修计划成功!")); } /** * PDA:我的计划列表(入参出参不变,内部改为按关系表过滤) */ @RequiresPermissions("mes:mesDevicePreventTask:api") @RequestMapping(value = "ulist") @ResponseBody public CommonResp ulist(MesDevicePreventTask mesDevicePreventTask, HttpServletRequest request, HttpServletResponse response) { CommonResp resp = new CommonResp<>(); User user = UserUtils.getUser(); mesDevicePreventTaskService.applyAssigneeFilter(mesDevicePreventTask, user.getUserCode()); mesDevicePreventTask.setPage(new Page<>(request, response)); Page page = mesDevicePreventTaskService.findPage(mesDevicePreventTask); resp.setData(page); resp.setResult(Global.TRUE); return resp; } /** * PDA:计划详情(入参出参结构不变;userName 改为多人逗号拼接) */ @RequiresPermissions("mes:mesDevicePreventTask:api") @RequestMapping(value = "detail") @ResponseBody public CommonResp detail(MesDevicePreventTask mesDevicePreventTask, HttpServletRequest req) { String host = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/js"; Map map = MapUtils.newHashMap(); // 获取图片 FileUpload fu = new FileUpload(); fu.setBizKey(mesDevicePreventTask.getId()); fu.setBizType("mesDevicePreventTask_image"); List imgs = fileUploadService.findList(fu); List imgList = ListUtils.newArrayList(); if(imgs.size() > 0){ for (FileUpload fi:imgs) { Map map1 = MapUtils.newHashMap(); map1.put("id",fi.getFileEntity().getFileId()); map1.put("url",host + fi.getFileUrl()); imgList.add(map1); } } map.put("imgs", imgList); // userName 已在 get/find 中按多人填充;此处兜底保证不为空 if (StringUtils.isBlank(mesDevicePreventTask.getUserName()) && StringUtils.isNotBlank(mesDevicePreventTask.getUserCode())) { String[] codes = StringUtils.split(mesDevicePreventTask.getUserCode(), ","); List names = new ArrayList<>(); if (codes != null) { for (String code : codes) { String uc = StringUtils.trim(code); if (StringUtils.isBlank(uc)) { continue; } User u = UserUtils.get(uc); names.add(u != null && StringUtils.isNotBlank(u.getUserName()) ? u.getUserName() : uc); } } mesDevicePreventTask.setUserName(StringUtils.join(names, ",")); } map.put("info", mesDevicePreventTask); // 获取检查清单 MesDevicePrevent mesDevicePrevent = mesDevicePreventService.get(mesDevicePreventTask.getPreventId()); map.put("prevent", mesDevicePrevent); CommonResp> resp = new CommonResp<>(); resp.setData(map); resp.setResult(Global.TRUE); return resp; } /** * 导出数据 */ @RequestMapping(value = "exportData") public void exportData(MesDevicePreventTask mesDevicePreventTask, HttpServletResponse response) { List list = mesDevicePreventTaskService.findList(mesDevicePreventTask); String fileName = "预防性维修计划" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx"; try(ExcelExport ee = new ExcelExport("预防性维修计划", MesDevicePreventTask.class)){ ee.setDataList(list).write(response, fileName); } } /** * 导出数据(我的维修计划) */ @RequestMapping(value = "exportData2") public void exportData2(MesDevicePreventTask mesDevicePreventTask, HttpServletRequest request, HttpServletResponse response) { User user = UserUtils.getUser(); mesDevicePreventTaskService.applyAssigneeFilter(mesDevicePreventTask, user.getUserCode()); List list = mesDevicePreventTaskService.findList(mesDevicePreventTask); List list1 = BeanUtil.copyToList(list, ExcelMesDevicePreventTask.class); String fileName = "我的维修计划" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx"; try(ExcelExport ee = new ExcelExport("我的维修计划", ExcelMesDevicePreventTask.class)){ ee.setDataList(list1).write(response, fileName); } } /** * 下载模板 */ @RequestMapping(value = "importTemplate") public void importTemplate(HttpServletResponse response) { MesDevicePreventTask mesDeviceCate = new MesDevicePreventTask(); List list = ListUtils.newArrayList(mesDeviceCate); String fileName = "预防性维修计划.xlsx"; try(ExcelExport ee = new ExcelExport("预防性维修计划", MesDevicePreventTask.class, ExcelField.Type.IMPORT)){ ee.setDataList(list).write(response, fileName); } } /** * 导入数据 */ @ResponseBody @PostMapping(value = "importData") public String importData(MultipartFile file) { try { String message = importData1(file); return renderResult(Global.TRUE, "posfull:"+message); } catch (Exception ex) { return renderResult(Global.FALSE, "posfull:"+ex.getMessage()); } } @Transactional public String importData1(MultipartFile file) { if (file == null){ throw new ServiceException(text("请选择导入的数据文件!")); } int successNum = 0; int failureNum = 0; StringBuilder successMsg = new StringBuilder(); StringBuilder failureMsg = new StringBuilder(); try(ExcelImport ei = new ExcelImport(file, 2, 0)){ List list = ei.getDataList(MesDevicePreventTask.class); for (MesDevicePreventTask mesDeviceCheck : list) { try{ ValidatorUtils.validateWithException(mesDeviceCheck); this.save(mesDeviceCheck); successNum++; successMsg.append("
" + successNum + "、编号 " + mesDeviceCheck.getId() + " 导入成功"); } catch (Exception e) { failureNum++; String msg = "
" + failureNum + "、编号 " + mesDeviceCheck.getId() + " 导入失败:"; if (e instanceof ConstraintViolationException){ ConstraintViolationException cve = (ConstraintViolationException)e; for (ConstraintViolation violation : cve.getConstraintViolations()) { msg += Global.getText(violation.getMessage()) + " ("+violation.getPropertyPath()+")"; } }else{ msg += e.getMessage(); } failureMsg.append(msg); logger.error(msg, e); } } } catch (Exception e) { logger.error(e.getMessage(), e); failureMsg.append(e.getMessage()); return failureMsg.toString(); } if (failureNum > 0) { failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:"); throw new ServiceException(failureMsg.toString()); }else{ successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:"); } return successMsg.toString(); } }