package com.jeesite.modules.mes.web; import com.alibaba.fastjson.JSONValidator; import com.jeesite.common.codec.EncodeUtils; import com.jeesite.common.collect.ListUtils; import com.jeesite.common.config.Global; import com.jeesite.common.entity.Page; import com.jeesite.common.lang.DateUtils; 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.mes.entity.MesDevicePrevent; import com.jeesite.modules.mes.entity.MesDevicePreventForm; import com.jeesite.modules.mes.entity.excel.ExcelMesDevicePrevent; import com.jeesite.modules.mes.service.MesDevicePreventService; 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.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolationException; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; /** * 预防性维修任务清单Controller * @author hzd * @version 2023-08-17 */ @Controller @RequestMapping(value = "${adminPath}/mes/mesDevicePrevent") public class MesDevicePreventController extends BaseController { @Autowired private MesDevicePreventService mesDevicePreventService; /** * 获取数据 */ @ModelAttribute public MesDevicePrevent get(String id, boolean isNewRecord) { return mesDevicePreventService.get(id, isNewRecord); } /** * 查询列表 */ @RequiresPermissions("mes:mesDevicePrevent:view") @RequestMapping(value = {"list", ""}) public String list(MesDevicePrevent mesDevicePrevent, Model model) { model.addAttribute("mesDevicePrevent", mesDevicePrevent); return "modules/mes/mesDevicePreventList"; } /** * 查询列表数据 */ @RequiresPermissions("mes:mesDevicePrevent:view") @RequestMapping(value = "listData") @ResponseBody public Page listData(MesDevicePrevent mesDevicePrevent, HttpServletRequest request, HttpServletResponse response) { mesDevicePrevent.setPage(new Page<>(request, response)); Page page = mesDevicePreventService.findPage(mesDevicePrevent); return page; } /** * 查询子表数据 */ @RequiresPermissions("mes:mesDevicePrevent:view") @RequestMapping(value = "mesDevicePreventFormListData") @ResponseBody public Page subListData(MesDevicePreventForm mesDevicePreventForm, HttpServletRequest request, HttpServletResponse response) { mesDevicePreventForm.setPage(new Page<>(request, response)); Page page = mesDevicePreventService.findSubPage(mesDevicePreventForm); return page; } /** * 查看编辑表单 */ @RequiresPermissions("mes:mesDevicePrevent:view") @RequestMapping(value = "form") public String form(MesDevicePrevent mesDevicePrevent, Model model) { model.addAttribute("mesDevicePrevent", mesDevicePrevent); return "modules/mes/mesDevicePreventForm"; } /** * 保存数据 */ @RequiresPermissions("mes:mesDevicePrevent:edit") @PostMapping(value = "save") @ResponseBody public String save(@Validated MesDevicePrevent mesDevicePrevent) { mesDevicePreventService.save(mesDevicePrevent); return renderResult(Global.TRUE, text("保存预防性维修任务清单成功!")); } /** * 停用数据 */ @RequiresPermissions("mes:mesDevicePrevent:edit") @RequestMapping(value = "disable") @ResponseBody public String disable(MesDevicePrevent mesDevicePrevent) { mesDevicePrevent.setStatus(MesDevicePrevent.STATUS_DISABLE); mesDevicePreventService.updateStatus(mesDevicePrevent); return renderResult(Global.TRUE, text("停用预防性维修任务清单成功")); } /** * 启用数据 */ @RequiresPermissions("mes:mesDevicePrevent:edit") @RequestMapping(value = "enable") @ResponseBody public String enable(MesDevicePrevent mesDevicePrevent) { mesDevicePrevent.setStatus(MesDevicePrevent.STATUS_NORMAL); mesDevicePreventService.updateStatus(mesDevicePrevent); return renderResult(Global.TRUE, text("启用预防性维修任务清单成功")); } /** * 删除数据 */ @RequiresPermissions("mes:mesDevicePrevent:edit") @RequestMapping(value = "delete") @ResponseBody public String delete(MesDevicePrevent mesDevicePrevent) { mesDevicePreventService.delete(mesDevicePrevent); return renderResult(Global.TRUE, text("删除预防性维修任务清单成功!")); } @RequiresPermissions("mes:mesDevicePrevent:view") @RequestMapping(value = "preventSelect") public String userSelect(MesDevicePrevent mesDevicePrevent, String selectData, Model model) { String selectDataJson = EncodeUtils.decodeUrl(selectData); if (selectDataJson != null && JSONValidator.from(selectDataJson).validate()){ model.addAttribute("selectData", selectDataJson); } model.addAttribute("warehouse", mesDevicePrevent); return "modules/mes/mesDevicePreventSelect"; } /** * 导出数据 */ @RequestMapping(value = "exportData") public void exportData(MesDevicePrevent mesDevicePrevent, HttpServletResponse response) { List list = mesDevicePreventService.findList(mesDevicePrevent); String fileName = "预防性维修任务清单" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx"; try(ExcelExport ee = new ExcelExport("预防性维修任务清单", ExcelMesDevicePrevent.class)){ ee.setDataList(buildExportRows(list)).write(response, fileName); } } /** * 下载模板 */ @RequestMapping(value = "importTemplate") public void importTemplate(HttpServletResponse response) { ExcelMesDevicePrevent mesDeviceCate = new ExcelMesDevicePrevent(); List list = ListUtils.newArrayList(mesDeviceCate); String fileName = "预防性维修任务清单.xlsx"; try(ExcelExport ee = new ExcelExport("预防性维修任务清单", ExcelMesDevicePrevent.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 = buildImportList(ei.getDataList(ExcelMesDevicePrevent.class)); for (MesDevicePrevent 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(); } private List buildExportRows(List list) { List rows = new ArrayList<>(); for (MesDevicePrevent item : list) { MesDevicePrevent full = mesDevicePreventService.get(item); if (full == null) { continue; } List formList = full.getMesDevicePreventFormList(); if (ListUtils.isEmpty(formList)) { rows.add(copyPreventToExcelRow(full, null)); continue; } for (MesDevicePreventForm form : formList) { rows.add(copyPreventToExcelRow(full, form)); } } return rows; } private ExcelMesDevicePrevent copyPreventToExcelRow(MesDevicePrevent prevent, MesDevicePreventForm form) { ExcelMesDevicePrevent row = new ExcelMesDevicePrevent(); row.setTitle(prevent.getTitle()); row.setCate(prevent.getCate()); row.setCycle(prevent.getCycle()); row.setLaborHour(prevent.getLaborHour()); row.setStatus(prevent.getStatus()); if (form != null) { row.setFormTitle(form.getTitle()); row.setFormLaborHour(form.getLaborHour()); } return row; } private List buildImportList(List rows) { Map preventMap = new LinkedHashMap<>(); for (ExcelMesDevicePrevent row : rows) { if (row == null || isBlankRow(row)) { continue; } String key = buildImportKey(row); MesDevicePrevent prevent = preventMap.get(key); if (prevent == null) { prevent = new MesDevicePrevent(); prevent.setTitle(row.getTitle()); prevent.setCate(row.getCate()); prevent.setCycle(row.getCycle()); prevent.setLaborHour(row.getLaborHour()); prevent.setStatus(StringUtils.isBlank(row.getStatus()) ? MesDevicePrevent.STATUS_NORMAL : row.getStatus()); preventMap.put(key, prevent); } if (StringUtils.isNotBlank(row.getFormTitle()) || StringUtils.isNotBlank(row.getFormLaborHour())) { MesDevicePreventForm form = new MesDevicePreventForm(); form.setTitle(row.getFormTitle()); form.setLaborHour(row.getFormLaborHour()); form.setStatus(MesDevicePreventForm.STATUS_NORMAL); prevent.getMesDevicePreventFormList().add(form); } } return new ArrayList<>(preventMap.values()); } private boolean isBlankRow(ExcelMesDevicePrevent row) { return StringUtils.isBlank(row.getTitle()) && StringUtils.isBlank(row.getCate()) && StringUtils.isBlank(row.getCycle()) && StringUtils.isBlank(row.getLaborHour()) && StringUtils.isBlank(row.getFormTitle()) && StringUtils.isBlank(row.getFormLaborHour()); } private String buildImportKey(ExcelMesDevicePrevent row) { return StringUtils.trimToEmpty(row.getTitle()) + "|" + StringUtils.trimToEmpty(row.getCate()) + "|" + StringUtils.trimToEmpty(row.getCycle()) + "|" + StringUtils.trimToEmpty(row.getLaborHour()); } }