MesDevicePreventController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. package com.jeesite.modules.mes.web;
  2. import com.alibaba.fastjson.JSONValidator;
  3. import com.jeesite.common.codec.EncodeUtils;
  4. import com.jeesite.common.collect.ListUtils;
  5. import com.jeesite.common.config.Global;
  6. import com.jeesite.common.entity.Page;
  7. import com.jeesite.common.lang.DateUtils;
  8. import com.jeesite.common.lang.StringUtils;
  9. import com.jeesite.common.service.ServiceException;
  10. import com.jeesite.common.utils.excel.ExcelExport;
  11. import com.jeesite.common.utils.excel.ExcelImport;
  12. import com.jeesite.common.utils.excel.annotation.ExcelField;
  13. import com.jeesite.common.validator.ValidatorUtils;
  14. import com.jeesite.common.web.BaseController;
  15. import com.jeesite.modules.mes.entity.MesDevicePrevent;
  16. import com.jeesite.modules.mes.entity.MesDevicePreventForm;
  17. import com.jeesite.modules.mes.entity.excel.ExcelMesDevicePrevent;
  18. import com.jeesite.modules.mes.service.MesDevicePreventService;
  19. import org.apache.shiro.authz.annotation.RequiresPermissions;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Controller;
  22. import org.springframework.transaction.annotation.Transactional;
  23. import org.springframework.ui.Model;
  24. import org.springframework.validation.annotation.Validated;
  25. import org.springframework.web.bind.annotation.ModelAttribute;
  26. import org.springframework.web.bind.annotation.PostMapping;
  27. import org.springframework.web.bind.annotation.RequestMapping;
  28. import org.springframework.web.bind.annotation.ResponseBody;
  29. import org.springframework.web.multipart.MultipartFile;
  30. import javax.servlet.http.HttpServletRequest;
  31. import javax.servlet.http.HttpServletResponse;
  32. import javax.validation.ConstraintViolation;
  33. import javax.validation.ConstraintViolationException;
  34. import java.util.ArrayList;
  35. import java.util.LinkedHashMap;
  36. import java.util.List;
  37. import java.util.Map;
  38. /**
  39. * 预防性维修任务清单Controller
  40. * @author hzd
  41. * @version 2023-08-17
  42. */
  43. @Controller
  44. @RequestMapping(value = "${adminPath}/mes/mesDevicePrevent")
  45. public class MesDevicePreventController extends BaseController {
  46. @Autowired
  47. private MesDevicePreventService mesDevicePreventService;
  48. /**
  49. * 获取数据
  50. */
  51. @ModelAttribute
  52. public MesDevicePrevent get(String id, boolean isNewRecord) {
  53. return mesDevicePreventService.get(id, isNewRecord);
  54. }
  55. /**
  56. * 查询列表
  57. */
  58. @RequiresPermissions("mes:mesDevicePrevent:view")
  59. @RequestMapping(value = {"list", ""})
  60. public String list(MesDevicePrevent mesDevicePrevent, Model model) {
  61. model.addAttribute("mesDevicePrevent", mesDevicePrevent);
  62. return "modules/mes/mesDevicePreventList";
  63. }
  64. /**
  65. * 查询列表数据
  66. */
  67. @RequiresPermissions("mes:mesDevicePrevent:view")
  68. @RequestMapping(value = "listData")
  69. @ResponseBody
  70. public Page<MesDevicePrevent> listData(MesDevicePrevent mesDevicePrevent, HttpServletRequest request, HttpServletResponse response) {
  71. mesDevicePrevent.setPage(new Page<>(request, response));
  72. Page<MesDevicePrevent> page = mesDevicePreventService.findPage(mesDevicePrevent);
  73. return page;
  74. }
  75. /**
  76. * 查询子表数据
  77. */
  78. @RequiresPermissions("mes:mesDevicePrevent:view")
  79. @RequestMapping(value = "mesDevicePreventFormListData")
  80. @ResponseBody
  81. public Page<MesDevicePreventForm> subListData(MesDevicePreventForm mesDevicePreventForm, HttpServletRequest request, HttpServletResponse response) {
  82. mesDevicePreventForm.setPage(new Page<>(request, response));
  83. Page<MesDevicePreventForm> page = mesDevicePreventService.findSubPage(mesDevicePreventForm);
  84. return page;
  85. }
  86. /**
  87. * 查看编辑表单
  88. */
  89. @RequiresPermissions("mes:mesDevicePrevent:view")
  90. @RequestMapping(value = "form")
  91. public String form(MesDevicePrevent mesDevicePrevent, Model model) {
  92. model.addAttribute("mesDevicePrevent", mesDevicePrevent);
  93. return "modules/mes/mesDevicePreventForm";
  94. }
  95. /**
  96. * 保存数据
  97. */
  98. @RequiresPermissions("mes:mesDevicePrevent:edit")
  99. @PostMapping(value = "save")
  100. @ResponseBody
  101. public String save(@Validated MesDevicePrevent mesDevicePrevent) {
  102. mesDevicePreventService.save(mesDevicePrevent);
  103. return renderResult(Global.TRUE, text("保存预防性维修任务清单成功!"));
  104. }
  105. /**
  106. * 停用数据
  107. */
  108. @RequiresPermissions("mes:mesDevicePrevent:edit")
  109. @RequestMapping(value = "disable")
  110. @ResponseBody
  111. public String disable(MesDevicePrevent mesDevicePrevent) {
  112. mesDevicePrevent.setStatus(MesDevicePrevent.STATUS_DISABLE);
  113. mesDevicePreventService.updateStatus(mesDevicePrevent);
  114. return renderResult(Global.TRUE, text("停用预防性维修任务清单成功"));
  115. }
  116. /**
  117. * 启用数据
  118. */
  119. @RequiresPermissions("mes:mesDevicePrevent:edit")
  120. @RequestMapping(value = "enable")
  121. @ResponseBody
  122. public String enable(MesDevicePrevent mesDevicePrevent) {
  123. mesDevicePrevent.setStatus(MesDevicePrevent.STATUS_NORMAL);
  124. mesDevicePreventService.updateStatus(mesDevicePrevent);
  125. return renderResult(Global.TRUE, text("启用预防性维修任务清单成功"));
  126. }
  127. /**
  128. * 删除数据
  129. */
  130. @RequiresPermissions("mes:mesDevicePrevent:edit")
  131. @RequestMapping(value = "delete")
  132. @ResponseBody
  133. public String delete(MesDevicePrevent mesDevicePrevent) {
  134. mesDevicePreventService.delete(mesDevicePrevent);
  135. return renderResult(Global.TRUE, text("删除预防性维修任务清单成功!"));
  136. }
  137. @RequiresPermissions("mes:mesDevicePrevent:view")
  138. @RequestMapping(value = "preventSelect")
  139. public String userSelect(MesDevicePrevent mesDevicePrevent, String selectData, Model model) {
  140. String selectDataJson = EncodeUtils.decodeUrl(selectData);
  141. if (selectDataJson != null && JSONValidator.from(selectDataJson).validate()){
  142. model.addAttribute("selectData", selectDataJson);
  143. }
  144. model.addAttribute("warehouse", mesDevicePrevent);
  145. return "modules/mes/mesDevicePreventSelect";
  146. }
  147. /**
  148. * 导出数据
  149. */
  150. @RequestMapping(value = "exportData")
  151. public void exportData(MesDevicePrevent mesDevicePrevent, HttpServletResponse response) {
  152. List<MesDevicePrevent> list = mesDevicePreventService.findList(mesDevicePrevent);
  153. String fileName = "预防性维修任务清单" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
  154. try(ExcelExport ee = new ExcelExport("预防性维修任务清单", ExcelMesDevicePrevent.class)){
  155. ee.setDataList(buildExportRows(list)).write(response, fileName);
  156. }
  157. }
  158. /**
  159. * 下载模板
  160. */
  161. @RequestMapping(value = "importTemplate")
  162. public void importTemplate(HttpServletResponse response) {
  163. ExcelMesDevicePrevent mesDeviceCate = new ExcelMesDevicePrevent();
  164. List<ExcelMesDevicePrevent> list = ListUtils.newArrayList(mesDeviceCate);
  165. String fileName = "预防性维修任务清单.xlsx";
  166. try(ExcelExport ee = new ExcelExport("预防性维修任务清单", ExcelMesDevicePrevent.class, ExcelField.Type.IMPORT)){
  167. ee.setDataList(list).write(response, fileName);
  168. }
  169. }
  170. /**
  171. * 导入数据
  172. */
  173. @ResponseBody
  174. @PostMapping(value = "importData")
  175. public String importData(MultipartFile file) {
  176. try {
  177. String message = importData1(file);
  178. return renderResult(Global.TRUE, "posfull:"+message);
  179. } catch (Exception ex) {
  180. return renderResult(Global.FALSE, "posfull:"+ex.getMessage());
  181. }
  182. }
  183. @Transactional
  184. public String importData1(MultipartFile file) {
  185. if (file == null){
  186. throw new ServiceException(text("请选择导入的数据文件!"));
  187. }
  188. int successNum = 0; int failureNum = 0;
  189. StringBuilder successMsg = new StringBuilder();
  190. StringBuilder failureMsg = new StringBuilder();
  191. try(ExcelImport ei = new ExcelImport(file, 2, 0)){
  192. List<MesDevicePrevent> list = buildImportList(ei.getDataList(ExcelMesDevicePrevent.class));
  193. for (MesDevicePrevent mesDeviceCheck : list) {
  194. try{
  195. ValidatorUtils.validateWithException(mesDeviceCheck);
  196. this.save(mesDeviceCheck);
  197. successNum++;
  198. successMsg.append("<br/>" + successNum + "、编号 " + mesDeviceCheck.getId() + " 导入成功");
  199. } catch (Exception e) {
  200. failureNum++;
  201. String msg = "<br/>" + failureNum + "、编号 " + mesDeviceCheck.getId() + " 导入失败:";
  202. if (e instanceof ConstraintViolationException){
  203. ConstraintViolationException cve = (ConstraintViolationException)e;
  204. for (ConstraintViolation<?> violation : cve.getConstraintViolations()) {
  205. msg += Global.getText(violation.getMessage()) + " ("+violation.getPropertyPath()+")";
  206. }
  207. }else{
  208. msg += e.getMessage();
  209. }
  210. failureMsg.append(msg);
  211. logger.error(msg, e);
  212. }
  213. }
  214. } catch (Exception e) {
  215. logger.error(e.getMessage(), e);
  216. failureMsg.append(e.getMessage());
  217. return failureMsg.toString();
  218. }
  219. if (failureNum > 0) {
  220. failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
  221. throw new ServiceException(failureMsg.toString());
  222. }else{
  223. successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
  224. }
  225. return successMsg.toString();
  226. }
  227. private List<ExcelMesDevicePrevent> buildExportRows(List<MesDevicePrevent> list) {
  228. List<ExcelMesDevicePrevent> rows = new ArrayList<>();
  229. for (MesDevicePrevent item : list) {
  230. MesDevicePrevent full = mesDevicePreventService.get(item);
  231. if (full == null) {
  232. continue;
  233. }
  234. List<MesDevicePreventForm> formList = full.getMesDevicePreventFormList();
  235. if (ListUtils.isEmpty(formList)) {
  236. rows.add(copyPreventToExcelRow(full, null));
  237. continue;
  238. }
  239. for (MesDevicePreventForm form : formList) {
  240. rows.add(copyPreventToExcelRow(full, form));
  241. }
  242. }
  243. return rows;
  244. }
  245. private ExcelMesDevicePrevent copyPreventToExcelRow(MesDevicePrevent prevent, MesDevicePreventForm form) {
  246. ExcelMesDevicePrevent row = new ExcelMesDevicePrevent();
  247. row.setTitle(prevent.getTitle());
  248. row.setCate(prevent.getCate());
  249. row.setCycle(prevent.getCycle());
  250. row.setLaborHour(prevent.getLaborHour());
  251. row.setStatus(prevent.getStatus());
  252. if (form != null) {
  253. row.setFormTitle(form.getTitle());
  254. row.setFormLaborHour(form.getLaborHour());
  255. }
  256. return row;
  257. }
  258. private List<MesDevicePrevent> buildImportList(List<ExcelMesDevicePrevent> rows) {
  259. Map<String, MesDevicePrevent> preventMap = new LinkedHashMap<>();
  260. for (ExcelMesDevicePrevent row : rows) {
  261. if (row == null || isBlankRow(row)) {
  262. continue;
  263. }
  264. String key = buildImportKey(row);
  265. MesDevicePrevent prevent = preventMap.get(key);
  266. if (prevent == null) {
  267. prevent = new MesDevicePrevent();
  268. prevent.setTitle(row.getTitle());
  269. prevent.setCate(row.getCate());
  270. prevent.setCycle(row.getCycle());
  271. prevent.setLaborHour(row.getLaborHour());
  272. prevent.setStatus(StringUtils.isBlank(row.getStatus()) ? MesDevicePrevent.STATUS_NORMAL : row.getStatus());
  273. preventMap.put(key, prevent);
  274. }
  275. if (StringUtils.isNotBlank(row.getFormTitle()) || StringUtils.isNotBlank(row.getFormLaborHour())) {
  276. MesDevicePreventForm form = new MesDevicePreventForm();
  277. form.setTitle(row.getFormTitle());
  278. form.setLaborHour(row.getFormLaborHour());
  279. form.setStatus(MesDevicePreventForm.STATUS_NORMAL);
  280. prevent.getMesDevicePreventFormList().add(form);
  281. }
  282. }
  283. return new ArrayList<>(preventMap.values());
  284. }
  285. private boolean isBlankRow(ExcelMesDevicePrevent row) {
  286. return StringUtils.isBlank(row.getTitle())
  287. && StringUtils.isBlank(row.getCate())
  288. && StringUtils.isBlank(row.getCycle())
  289. && StringUtils.isBlank(row.getLaborHour())
  290. && StringUtils.isBlank(row.getFormTitle())
  291. && StringUtils.isBlank(row.getFormLaborHour());
  292. }
  293. private String buildImportKey(ExcelMesDevicePrevent row) {
  294. return StringUtils.trimToEmpty(row.getTitle()) + "|"
  295. + StringUtils.trimToEmpty(row.getCate()) + "|"
  296. + StringUtils.trimToEmpty(row.getCycle()) + "|"
  297. + StringUtils.trimToEmpty(row.getLaborHour());
  298. }
  299. }