| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- package com.jeesite.modules.mes.web;
- 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.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.MesDeviceCate;
- import com.jeesite.modules.mes.service.MesDeviceCateService;
- 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.List;
- /**
- * 设备分类Controller
- * @author wst
- * @version 2023-07-20
- */
- @Controller
- @RequestMapping(value = "${adminPath}/mes/mesDeviceCate")
- public class MesDeviceCateController extends BaseController {
- @Autowired
- private MesDeviceCateService mesDeviceCateService;
-
- /**
- * 获取数据
- */
- @ModelAttribute
- public MesDeviceCate get(String id, boolean isNewRecord) {
- return mesDeviceCateService.get(id, isNewRecord);
- }
-
- /**
- * 查询列表
- */
- @RequiresPermissions("mes:mesDeviceCate:view")
- @RequestMapping(value = {"list", ""})
- public String list(MesDeviceCate mesDeviceCate, Model model) {
- model.addAttribute("mesDeviceCate", mesDeviceCate);
- return "modules/mes/mesDeviceCateList";
- }
-
- /**
- * 查询列表数据
- */
- @RequiresPermissions("mes:mesDeviceCate:view")
- @RequestMapping(value = "listData")
- @ResponseBody
- public Page<MesDeviceCate> listData(MesDeviceCate mesDeviceCate, HttpServletRequest request, HttpServletResponse response) {
- mesDeviceCate.setPage(new Page<>(request, response));
- Page<MesDeviceCate> page = mesDeviceCateService.findPage(mesDeviceCate);
- return page;
- }
- /**
- * 查看编辑表单
- */
- @RequiresPermissions("mes:mesDeviceCate:view")
- @RequestMapping(value = "form")
- public String form(MesDeviceCate mesDeviceCate, Model model) {
- model.addAttribute("mesDeviceCate", mesDeviceCate);
- return "modules/mes/mesDeviceCateForm";
- }
- /**
- * 保存数据
- */
- @RequiresPermissions("mes:mesDeviceCate:edit")
- @PostMapping(value = "save")
- @ResponseBody
- public String save(@Validated MesDeviceCate mesDeviceCate) {
- mesDeviceCateService.save(mesDeviceCate);
- return renderResult(Global.TRUE, text("保存设备分类成功!"));
- }
-
- /**
- * 停用数据
- */
- @RequiresPermissions("mes:mesDeviceCate:edit")
- @RequestMapping(value = "disable")
- @ResponseBody
- public String disable(MesDeviceCate mesDeviceCate) {
- mesDeviceCate.setStatus(MesDeviceCate.STATUS_DISABLE);
- mesDeviceCateService.updateStatus(mesDeviceCate);
- return renderResult(Global.TRUE, text("停用设备分类成功"));
- }
-
- /**
- * 启用数据
- */
- @RequiresPermissions("mes:mesDeviceCate:edit")
- @RequestMapping(value = "enable")
- @ResponseBody
- public String enable(MesDeviceCate mesDeviceCate) {
- mesDeviceCate.setStatus(MesDeviceCate.STATUS_NORMAL);
- mesDeviceCateService.updateStatus(mesDeviceCate);
- return renderResult(Global.TRUE, text("启用设备分类成功"));
- }
-
- /**
- * 删除数据
- */
- @RequiresPermissions("mes:mesDeviceCate:edit")
- @RequestMapping(value = "delete")
- @ResponseBody
- public String delete(MesDeviceCate mesDeviceCate) {
- mesDeviceCateService.delete(mesDeviceCate);
- return renderResult(Global.TRUE, text("删除设备分类成功!"));
- }
- /**
- * 导出数据
- */
- @RequestMapping(value = "exportData")
- public void exportData(MesDeviceCate mesDeviceCate, HttpServletResponse response) {
- List<MesDeviceCate> list = mesDeviceCateService.findList(mesDeviceCate);
- String fileName = "设备分类" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
- try(ExcelExport ee = new ExcelExport("设备分类", MesDeviceCate.class)){
- ee.setDataList(list).write(response, fileName);
- }
- }
- /**
- * 下载模板
- */
- @RequestMapping(value = "importTemplate")
- public void importTemplate(HttpServletResponse response) {
- MesDeviceCate mesDeviceCate = new MesDeviceCate();
- List<MesDeviceCate> list = ListUtils.newArrayList(mesDeviceCate);
- String fileName = "设备分类.xlsx";
- try(ExcelExport ee = new ExcelExport("设备分类", MesDeviceCate.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<MesDeviceCate> list = ei.getDataList(MesDeviceCate.class);
- for (MesDeviceCate 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();
- }
- }
|