| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- 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<MesDevicePrevent> listData(MesDevicePrevent mesDevicePrevent, HttpServletRequest request, HttpServletResponse response) {
- mesDevicePrevent.setPage(new Page<>(request, response));
- Page<MesDevicePrevent> page = mesDevicePreventService.findPage(mesDevicePrevent);
- return page;
- }
-
- /**
- * 查询子表数据
- */
- @RequiresPermissions("mes:mesDevicePrevent:view")
- @RequestMapping(value = "mesDevicePreventFormListData")
- @ResponseBody
- public Page<MesDevicePreventForm> subListData(MesDevicePreventForm mesDevicePreventForm, HttpServletRequest request, HttpServletResponse response) {
- mesDevicePreventForm.setPage(new Page<>(request, response));
- Page<MesDevicePreventForm> 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<MesDevicePrevent> 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<ExcelMesDevicePrevent> 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<MesDevicePrevent> list = buildImportList(ei.getDataList(ExcelMesDevicePrevent.class));
- for (MesDevicePrevent mesDeviceCheck : list) {
- try{
- ValidatorUtils.validateWithException(mesDeviceCheck);
- this.save(mesDeviceCheck);
- successNum++;
- successMsg.append("<br/>" + successNum + "、编号 " + mesDeviceCheck.getId() + " 导入成功");
- } catch (Exception e) {
- failureNum++;
- String msg = "<br/>" + 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<ExcelMesDevicePrevent> buildExportRows(List<MesDevicePrevent> list) {
- List<ExcelMesDevicePrevent> rows = new ArrayList<>();
- for (MesDevicePrevent item : list) {
- MesDevicePrevent full = mesDevicePreventService.get(item);
- if (full == null) {
- continue;
- }
- List<MesDevicePreventForm> 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<MesDevicePrevent> buildImportList(List<ExcelMesDevicePrevent> rows) {
- Map<String, MesDevicePrevent> 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());
- }
- }
|