MesDevicePreventTaskController.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. package com.jeesite.modules.mes.web;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import com.alibaba.fastjson.JSON;
  4. import com.jeesite.common.collect.ListUtils;
  5. import com.jeesite.common.collect.MapUtils;
  6. import com.jeesite.common.config.Global;
  7. import com.jeesite.common.entity.Page;
  8. import com.jeesite.common.lang.DateUtils;
  9. import com.jeesite.common.lang.ObjectUtils;
  10. import com.jeesite.common.lang.StringUtils;
  11. import com.jeesite.common.service.ServiceException;
  12. import com.jeesite.common.utils.excel.ExcelExport;
  13. import com.jeesite.common.utils.excel.ExcelImport;
  14. import com.jeesite.common.utils.excel.annotation.ExcelField;
  15. import com.jeesite.common.validator.ValidatorUtils;
  16. import com.jeesite.common.web.BaseController;
  17. import com.jeesite.modules.file.entity.FileUpload;
  18. import com.jeesite.modules.file.service.FileUploadService;
  19. import com.jeesite.modules.mes.entity.*;
  20. import com.jeesite.modules.mes.entity.excel.ExcelMesDevicePreventTask;
  21. import com.jeesite.modules.mes.resp.CommonResp;
  22. import com.jeesite.modules.mes.service.*;
  23. import com.jeesite.modules.sys.entity.User;
  24. import com.jeesite.modules.sys.utils.UserUtils;
  25. import org.apache.shiro.authz.annotation.Logical;
  26. import org.apache.shiro.authz.annotation.RequiresPermissions;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import org.springframework.stereotype.Controller;
  29. import org.springframework.transaction.annotation.Transactional;
  30. import org.springframework.ui.Model;
  31. import org.springframework.validation.annotation.Validated;
  32. import org.springframework.web.bind.annotation.ModelAttribute;
  33. import org.springframework.web.bind.annotation.PostMapping;
  34. import org.springframework.web.bind.annotation.RequestMapping;
  35. import org.springframework.web.bind.annotation.ResponseBody;
  36. import org.springframework.web.multipart.MultipartFile;
  37. import javax.annotation.Resource;
  38. import javax.servlet.http.HttpServletRequest;
  39. import javax.servlet.http.HttpServletResponse;
  40. import javax.validation.ConstraintViolation;
  41. import javax.validation.ConstraintViolationException;
  42. import java.util.ArrayList;
  43. import java.util.List;
  44. import java.util.Map;
  45. /**
  46. * 预防性维修计划Controller
  47. * @author hzd
  48. * @version 2023-08-17
  49. */
  50. @Controller
  51. @RequestMapping(value = "${adminPath}/mes/mesDevicePreventTask")
  52. public class MesDevicePreventTaskController extends BaseController {
  53. @Autowired
  54. private MesDevicePreventTaskService mesDevicePreventTaskService;
  55. @Resource
  56. private FileUploadService fileUploadService;
  57. @Resource
  58. private MesDeviceService mesDeviceService;
  59. @Resource
  60. private MesDevicePreventService mesDevicePreventService;
  61. /**
  62. * 获取数据
  63. */
  64. @ModelAttribute
  65. public MesDevicePreventTask get(String id, boolean isNewRecord) {
  66. return mesDevicePreventTaskService.get(id, isNewRecord);
  67. }
  68. /**
  69. * 查询列表
  70. */
  71. @RequiresPermissions("mes:mesDevicePreventTask:view")
  72. @RequestMapping(value = {"list", ""})
  73. public String list(MesDevicePreventTask mesDevicePreventTask, Model model) {
  74. model.addAttribute("mesDevicePreventTask", mesDevicePreventTask);
  75. return "modules/mes/mesDevicePreventTaskList";
  76. }
  77. /**
  78. * 我的计划
  79. */
  80. @RequiresPermissions("mes:mesDevicePreventTask:view")
  81. @RequestMapping(value = {"mylist", ""})
  82. public String mylist(MesDevicePreventTask mesDevicePreventTask, Model model) {
  83. User user = UserUtils.getUser();
  84. // 标记为我的计划查询,listData 中按关系表过滤
  85. mesDevicePreventTask.setUserCode(user.getUserCode());
  86. model.addAttribute("mesDevicePreventTask", mesDevicePreventTask);
  87. model.addAttribute("myTaskOnly", true);
  88. return "modules/mes/mesDevicePreventTaskMyList";
  89. }
  90. /**
  91. * 查询列表数据
  92. */
  93. @RequiresPermissions("mes:mesDevicePreventTask:view")
  94. @RequestMapping(value = "listData")
  95. @ResponseBody
  96. public Page<MesDevicePreventTask> listData(MesDevicePreventTask mesDevicePreventTask, HttpServletRequest request, HttpServletResponse response) {
  97. mesDevicePreventTask.setPage(new Page<>(request, response));
  98. // 我的计划:按执行人关系表过滤(兼容原有传 userCode 的方式)
  99. String myTaskOnly = request.getParameter("myTaskOnly");
  100. if (Global.TRUE.equals(myTaskOnly) || "1".equals(myTaskOnly) || "true".equalsIgnoreCase(myTaskOnly)) {
  101. User user = UserUtils.getUser();
  102. mesDevicePreventTaskService.applyAssigneeFilter(mesDevicePreventTask, user.getUserCode());
  103. }
  104. Page<MesDevicePreventTask> page = mesDevicePreventTaskService.findPage(mesDevicePreventTask);
  105. return page;
  106. }
  107. /**
  108. * 查看编辑表单
  109. */
  110. @RequiresPermissions("mes:mesDevicePreventTask:view")
  111. @RequestMapping(value = "form")
  112. public String form(MesDevicePreventTask mesDevicePreventTask, Model model) {
  113. model.addAttribute("mesDevicePreventTask", mesDevicePreventTask);
  114. return "modules/mes/mesDevicePreventTaskForm";
  115. }
  116. /**
  117. * 保存数据
  118. * 一条计划可分配多个执行人;允许新增/编辑时增删执行人
  119. * 说明:维护性任务由管理员指派,不校验产线工位操作权限/技能(与上岗扫码场景不同)
  120. */
  121. @RequiresPermissions("mes:mesDevicePreventTask:edit")
  122. @PostMapping(value = "save")
  123. @ResponseBody
  124. @Transactional
  125. public String save(@Validated MesDevicePreventTask mesDevicePreventTask) {
  126. MesDevice mesDevice = new MesDevice();
  127. mesDevice.setTreeCode(mesDevicePreventTask.getDeviceId());
  128. MesDevice mesDevice1 = mesDeviceService.getByEntity(mesDevice);
  129. if (ObjectUtils.isEmpty(mesDevice1)) {
  130. return renderResult(Global.FALSE, text("保存失败,设备不存在!"));
  131. }
  132. List<String> userCodes = parseUserCodes(mesDevicePreventTask.getUserCode());
  133. if (userCodes.isEmpty()) {
  134. return renderResult(Global.FALSE, text("请选择执行人员!"));
  135. }
  136. mesDevicePreventTaskService.saveWithAssignees(mesDevicePreventTask, userCodes);
  137. return renderResult(Global.TRUE, text("保存预防性维修计划成功!"));
  138. }
  139. /**
  140. * 解析多选执行人(过滤机构节点,仅保留用户)
  141. */
  142. private List<String> parseUserCodes(String userCodeStr) {
  143. List<String> result = new ArrayList<>();
  144. String[] userCodes = StringUtils.split(StringUtils.trim(userCodeStr), ",");
  145. if (userCodes == null) {
  146. return result;
  147. }
  148. for (String code : userCodes) {
  149. String userCode = StringUtils.trim(code);
  150. if (StringUtils.isBlank(userCode)) {
  151. continue;
  152. }
  153. User user = UserUtils.get(userCode);
  154. if (user == null || StringUtils.isBlank(user.getUserCode())) {
  155. continue;
  156. }
  157. String realCode = user.getUserCode();
  158. if (!result.contains(realCode)) {
  159. result.add(realCode);
  160. }
  161. }
  162. return result;
  163. }
  164. /**
  165. * 删除数据
  166. */
  167. @RequiresPermissions("mes:mesDevicePreventTask:edit")
  168. @RequestMapping(value = "delete")
  169. @ResponseBody
  170. public String delete(MesDevicePreventTask mesDevicePreventTask) {
  171. mesDevicePreventTaskService.delete(mesDevicePreventTask);
  172. return renderResult(Global.TRUE, text("删除预防性维修计划成功!"));
  173. }
  174. /**
  175. * 查看编辑表单
  176. */
  177. @RequiresPermissions("mes:mesDevicePreventTask:view")
  178. @RequestMapping(value = "deal")
  179. public String deal(MesDevicePreventTask mesDevicePreventTask, Model model) {
  180. model.addAttribute("mesDevicePreventTask", mesDevicePreventTask);
  181. return "modules/mes/mesDevicePreventTaskDeal";
  182. }
  183. @RequiresPermissions("mes:mesDevicePreventTask:view")
  184. @RequestMapping(value = "mydeal")
  185. public String mydeal(MesDevicePreventTask mesDevicePreventTask, Model model) {
  186. model.addAttribute("mesDevicePreventTask", mesDevicePreventTask);
  187. // 获取检查清单
  188. MesDevicePrevent mesDevicePrevent = mesDevicePreventService.get(mesDevicePreventTask.getPreventId());
  189. model.addAttribute("mesDevicePrevent", mesDevicePrevent);
  190. return "modules/mes/mesDevicePreventTaskMyDeal";
  191. }
  192. @RequiresPermissions("mes:mesDevicePreventTask:view")
  193. @RequestMapping(value = "info")
  194. public String info(MesDevicePreventTask mesDevicePreventTask, Model model) {
  195. model.addAttribute("mesDevicePreventTask", mesDevicePreventTask);
  196. // 获取检查清单
  197. MesDevicePrevent mesDevicePrevent = mesDevicePreventService.get(mesDevicePreventTask.getPreventId());
  198. model.addAttribute("mesDevicePrevent", mesDevicePrevent);
  199. return "modules/mes/mesDevicePreventTaskInfo";
  200. }
  201. /**
  202. * 保存处理结果(Web/PDA共用)
  203. * 入参出参保持不变:任一执行人完成即整单完成,并记录实际完成人
  204. */
  205. @RequiresPermissions(value={"mes:mesDevicePreventTask:deal", "mes:mesDevicePreventTask:api"}, logical= Logical.OR)
  206. @PostMapping(value = "dealsave")
  207. @ResponseBody
  208. public String dealsave(@Validated MesDevicePreventTask mesDevicePreventTask,HttpServletRequest request) {
  209. // 以数据库状态为准,避免重复提交
  210. MesDevicePreventTask dbTask = mesDevicePreventTaskService.get(new MesDevicePreventTask(mesDevicePreventTask.getId()));
  211. if(dbTask == null || dbTask.getState() == null || !dbTask.getState().equals("0")){
  212. return renderResult(Global.TRUE, text("该记录已处理!"));
  213. }
  214. // 兼容接口
  215. List<MesDeviceConsumableRecord> consumable = JSON.parseArray(request.getParameter("consumable"),MesDeviceConsumableRecord.class);
  216. if(consumable!=null && consumable.size() >0){
  217. for (MesDeviceConsumableRecord mesDeviceConsumableRecord : consumable){
  218. if(StringUtils.isEmpty(mesDeviceConsumableRecord.getConsumableId())){
  219. return renderResult(Global.FALSE, text("参数错误!"));
  220. }
  221. if(mesDeviceConsumableRecord.getNum() <= 0){
  222. return renderResult(Global.FALSE, text("易耗品数量必须大于0!"));
  223. }
  224. }
  225. mesDevicePreventTask.setMesDeviceConsumableRecordList(consumable);
  226. }
  227. User user = UserUtils.getUser();
  228. // 多执行人:当前用户在关系表中即可处理(不再要求等于主表单个 userCode)
  229. if(!mesDevicePreventTaskService.canDeal(mesDevicePreventTask.getId(), user.getUserCode())){
  230. return renderResult(Global.TRUE, text("无权限操作!"));
  231. }
  232. // 完成时保留原执行人冗余字段,仅更新完成人与状态
  233. mesDevicePreventTask.setUserCode(dbTask.getUserCode());
  234. mesDevicePreventTask.setFinishUserCode(user.getUserCode());
  235. mesDevicePreventTask.setState("1");
  236. mesDevicePreventTaskService.save(mesDevicePreventTask);
  237. return renderResult(Global.TRUE, text("保存预防性维修计划成功!"));
  238. }
  239. /**
  240. * PDA:我的计划列表(入参出参不变,内部改为按关系表过滤)
  241. */
  242. @RequiresPermissions("mes:mesDevicePreventTask:api")
  243. @RequestMapping(value = "ulist")
  244. @ResponseBody
  245. public CommonResp ulist(MesDevicePreventTask mesDevicePreventTask, HttpServletRequest request, HttpServletResponse response) {
  246. CommonResp<Page> resp = new CommonResp<>();
  247. User user = UserUtils.getUser();
  248. mesDevicePreventTaskService.applyAssigneeFilter(mesDevicePreventTask, user.getUserCode());
  249. mesDevicePreventTask.setPage(new Page<>(request, response));
  250. Page<MesDevicePreventTask> page = mesDevicePreventTaskService.findPage(mesDevicePreventTask);
  251. resp.setData(page);
  252. resp.setResult(Global.TRUE);
  253. return resp;
  254. }
  255. /**
  256. * PDA:计划详情(入参出参结构不变;userName 改为多人逗号拼接)
  257. */
  258. @RequiresPermissions("mes:mesDevicePreventTask:api")
  259. @RequestMapping(value = "detail")
  260. @ResponseBody
  261. public CommonResp detail(MesDevicePreventTask mesDevicePreventTask, HttpServletRequest req) {
  262. String host = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/js";
  263. Map<String, Object> map = MapUtils.newHashMap();
  264. // 获取图片
  265. FileUpload fu = new FileUpload();
  266. fu.setBizKey(mesDevicePreventTask.getId());
  267. fu.setBizType("mesDevicePreventTask_image");
  268. List<FileUpload> imgs = fileUploadService.findList(fu);
  269. List imgList = ListUtils.newArrayList();
  270. if(imgs.size() > 0){
  271. for (FileUpload fi:imgs) {
  272. Map<String, Object> map1 = MapUtils.newHashMap();
  273. map1.put("id",fi.getFileEntity().getFileId());
  274. map1.put("url",host + fi.getFileUrl());
  275. imgList.add(map1);
  276. }
  277. }
  278. map.put("imgs", imgList);
  279. // userName 已在 get/find 中按多人填充;此处兜底保证不为空
  280. if (StringUtils.isBlank(mesDevicePreventTask.getUserName())
  281. && StringUtils.isNotBlank(mesDevicePreventTask.getUserCode())) {
  282. String[] codes = StringUtils.split(mesDevicePreventTask.getUserCode(), ",");
  283. List<String> names = new ArrayList<>();
  284. if (codes != null) {
  285. for (String code : codes) {
  286. String uc = StringUtils.trim(code);
  287. if (StringUtils.isBlank(uc)) {
  288. continue;
  289. }
  290. User u = UserUtils.get(uc);
  291. names.add(u != null && StringUtils.isNotBlank(u.getUserName()) ? u.getUserName() : uc);
  292. }
  293. }
  294. mesDevicePreventTask.setUserName(StringUtils.join(names, ","));
  295. }
  296. map.put("info", mesDevicePreventTask);
  297. // 获取检查清单
  298. MesDevicePrevent mesDevicePrevent = mesDevicePreventService.get(mesDevicePreventTask.getPreventId());
  299. map.put("prevent", mesDevicePrevent);
  300. CommonResp<Map<String, Object>> resp = new CommonResp<>();
  301. resp.setData(map);
  302. resp.setResult(Global.TRUE);
  303. return resp;
  304. }
  305. /**
  306. * 导出数据
  307. */
  308. @RequestMapping(value = "exportData")
  309. public void exportData(MesDevicePreventTask mesDevicePreventTask, HttpServletResponse response) {
  310. List<MesDevicePreventTask> list = mesDevicePreventTaskService.findList(mesDevicePreventTask);
  311. String fileName = "预防性维修计划" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
  312. try(ExcelExport ee = new ExcelExport("预防性维修计划", MesDevicePreventTask.class)){
  313. ee.setDataList(list).write(response, fileName);
  314. }
  315. }
  316. /**
  317. * 导出数据(我的维修计划)
  318. */
  319. @RequestMapping(value = "exportData2")
  320. public void exportData2(MesDevicePreventTask mesDevicePreventTask, HttpServletRequest request, HttpServletResponse response) {
  321. User user = UserUtils.getUser();
  322. mesDevicePreventTaskService.applyAssigneeFilter(mesDevicePreventTask, user.getUserCode());
  323. List<MesDevicePreventTask> list = mesDevicePreventTaskService.findList(mesDevicePreventTask);
  324. List<ExcelMesDevicePreventTask> list1 = BeanUtil.copyToList(list, ExcelMesDevicePreventTask.class);
  325. String fileName = "我的维修计划" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
  326. try(ExcelExport ee = new ExcelExport("我的维修计划", ExcelMesDevicePreventTask.class)){
  327. ee.setDataList(list1).write(response, fileName);
  328. }
  329. }
  330. /**
  331. * 下载模板
  332. */
  333. @RequestMapping(value = "importTemplate")
  334. public void importTemplate(HttpServletResponse response) {
  335. MesDevicePreventTask mesDeviceCate = new MesDevicePreventTask();
  336. List<MesDevicePreventTask> list = ListUtils.newArrayList(mesDeviceCate);
  337. String fileName = "预防性维修计划.xlsx";
  338. try(ExcelExport ee = new ExcelExport("预防性维修计划", MesDevicePreventTask.class, ExcelField.Type.IMPORT)){
  339. ee.setDataList(list).write(response, fileName);
  340. }
  341. }
  342. /**
  343. * 导入数据
  344. */
  345. @ResponseBody
  346. @PostMapping(value = "importData")
  347. public String importData(MultipartFile file) {
  348. try {
  349. String message = importData1(file);
  350. return renderResult(Global.TRUE, "posfull:"+message);
  351. } catch (Exception ex) {
  352. return renderResult(Global.FALSE, "posfull:"+ex.getMessage());
  353. }
  354. }
  355. @Transactional
  356. public String importData1(MultipartFile file) {
  357. if (file == null){
  358. throw new ServiceException(text("请选择导入的数据文件!"));
  359. }
  360. int successNum = 0; int failureNum = 0;
  361. StringBuilder successMsg = new StringBuilder();
  362. StringBuilder failureMsg = new StringBuilder();
  363. try(ExcelImport ei = new ExcelImport(file, 2, 0)){
  364. List<MesDevicePreventTask> list = ei.getDataList(MesDevicePreventTask.class);
  365. for (MesDevicePreventTask mesDeviceCheck : list) {
  366. try{
  367. ValidatorUtils.validateWithException(mesDeviceCheck);
  368. this.save(mesDeviceCheck);
  369. successNum++;
  370. successMsg.append("<br/>" + successNum + "、编号 " + mesDeviceCheck.getId() + " 导入成功");
  371. } catch (Exception e) {
  372. failureNum++;
  373. String msg = "<br/>" + failureNum + "、编号 " + mesDeviceCheck.getId() + " 导入失败:";
  374. if (e instanceof ConstraintViolationException){
  375. ConstraintViolationException cve = (ConstraintViolationException)e;
  376. for (ConstraintViolation<?> violation : cve.getConstraintViolations()) {
  377. msg += Global.getText(violation.getMessage()) + " ("+violation.getPropertyPath()+")";
  378. }
  379. }else{
  380. msg += e.getMessage();
  381. }
  382. failureMsg.append(msg);
  383. logger.error(msg, e);
  384. }
  385. }
  386. } catch (Exception e) {
  387. logger.error(e.getMessage(), e);
  388. failureMsg.append(e.getMessage());
  389. return failureMsg.toString();
  390. }
  391. if (failureNum > 0) {
  392. failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
  393. throw new ServiceException(failureMsg.toString());
  394. }else{
  395. successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
  396. }
  397. return successMsg.toString();
  398. }
  399. }