package com.jeesite.modules.mes.web; import java.text.SimpleDateFormat; import java.util.Map; import java.util.List; import java.util.Date; import java.util.Calendar; import java.util.stream.Collectors; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.alibaba.fastjson.JSON; import com.jeesite.common.entity.Page; import com.jeesite.common.lang.ObjectUtils; import com.jeesite.modules.mes.entity.MesDeviceCate; import com.jeesite.modules.mes.entity.MesDeviceForm; import com.jeesite.modules.mes.entity.MesDevicePreventTask; import com.jeesite.modules.mes.entity.MesDeviceState; import com.jeesite.modules.mes.entity.MesDeviceUrgency; import com.jeesite.modules.mes.entity.MesLineProcess; import com.jeesite.modules.mes.resp.CommonResp; import com.jeesite.modules.mes.service.MesDeviceCateService; import com.jeesite.modules.mes.service.MesDevicePreventTaskService; import com.jeesite.modules.mes.service.MesDeviceUrgencyService; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; 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.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.jeesite.common.config.Global; import com.jeesite.common.collect.ListUtils; import com.jeesite.common.collect.MapUtils; import com.jeesite.common.lang.StringUtils; import com.jeesite.common.idgen.IdGen; import com.jeesite.modules.sys.utils.UserUtils; import com.jeesite.common.lang.DateUtils; import com.jeesite.common.utils.excel.ExcelExport; import com.jeesite.common.utils.excel.annotation.ExcelField.Type; import org.springframework.web.multipart.MultipartFile; import com.jeesite.common.web.BaseController; import com.jeesite.modules.mes.entity.MesDevice; import com.jeesite.modules.mes.req.MesDeviceReportStateReq; import com.jeesite.modules.mes.service.MesDeviceOeeStatsService; import com.jeesite.modules.mes.service.MesDeviceStateReportService; import com.jeesite.modules.mes.service.MesDeviceService; /** * 设备Controller * @author mes * @version 2023-07-12 */ @Controller @RequestMapping(value = "${adminPath}/mes/mesDevice") public class MesDeviceController extends BaseController { @Autowired private MesDeviceService mesDeviceService; @Autowired private MesDeviceCateService mesDeviceCateService; @Autowired private MesDevicePreventTaskService mesDevicePreventTaskService; @Autowired private MesDeviceUrgencyService mesDeviceUrgencyService; @Autowired private MesDeviceStateReportService mesDeviceStateReportService; @Autowired private MesDeviceOeeStatsService mesDeviceOeeStatsService; /** * 获取数据 */ @ModelAttribute public MesDevice get(String treeCode, boolean isNewRecord) { return mesDeviceService.get(treeCode, isNewRecord); } /** * 管理主页 */ @RequiresPermissions("mes:mesDevice:view") @RequestMapping(value = "index") public String index(MesDevice mesDevice, Model model) { model.addAttribute("mesDevice", mesDevice); return "modules/mes/mesDeviceIndex"; } /** * 查询列表 */ @RequiresPermissions("mes:mesDevice:view") @RequestMapping(value = {"list", ""}) public String list(MesDevice mesDevice, MesDeviceCate mesDeviceCate, Model model) { model.addAttribute("mesDevice", mesDevice); model.addAttribute("mesDeviceCate", mesDeviceCateService.findList(mesDeviceCate)); return "modules/mes/mesDeviceList"; } /** * 查询列表数据 */ // @RequiresPermissions("mes:mesDevice:view") // @RequestMapping(value = "listData") // @ResponseBody // public List listData(MesDevice mesDevice) { // if (StringUtils.isBlank(mesDevice.getParentCode())) { // mesDevice.setParentCode(MesDevice.ROOT_CODE); // } // if (StringUtils.isNotBlank(mesDevice.getRemark())){ // mesDevice.setParentCode(null); // } // if (StringUtils.isNotBlank(mesDevice.getTreeName())){ // mesDevice.setParentCode(null); // } // if (StringUtils.isNotBlank(mesDevice.getSn())){ // mesDevice.setParentCode(null); // } // if (StringUtils.isNotBlank(mesDevice.getIp())){ // mesDevice.setParentCode(null); // } // List list = mesDeviceService.findList(mesDevice); // for (MesDevice i:list){ // mesDevice.setTreeCode(i.getParentCode()); // MesDevice parent = mesDeviceService.findByInfo(mesDevice); // if(parent==null){ // i.setParentSn(""); // }else { // i.setParentSn(parent.getSn()); // } // } // // return list; // } @RequiresPermissions("mes:mesDevice:view") @RequestMapping(value = "listData") @ResponseBody public Page listData(MesDevice mesDevice, HttpServletRequest request, HttpServletResponse response) { // System.out.println(request.getParameterMap()); mesDevice.setPage(new Page<>(request, response)); Page page = mesDeviceService.findPage(mesDevice); return page; } /** * 查看编辑表单 */ @RequiresPermissions("mes:mesDevice:view") @RequestMapping(value = "form") public String form(MesDevice mesDevice,MesDeviceCate mesDeviceCate, Model model) { // 创建并初始化下一个节点信息 mesDevice = createNextNode(mesDevice); logger.info(JSON.toJSONString(mesDevice)); model.addAttribute("mesDevice", mesDevice); model.addAttribute("mesDeviceCate", mesDeviceCateService.findList(mesDeviceCate)); return "modules/mes/mesDeviceForm"; } /** * 详情 */ @RequiresPermissions("mes:mesDevice:view") @RequestMapping(value = "detail") public String detail(MesDevice mesDevice,MesDeviceCate mesDeviceCate, Model model) { // 创建并初始化下一个节点信息 mesDevice.setTreeCode(mesDevice.getParentCode()); MesDevice parent = mesDeviceService.findByInfo(mesDevice); if(parent==null){ mesDevice.setParentSn(""); }else { mesDevice.setParentSn(parent.getSn()); } mesDevice = createNextNode(mesDevice); model.addAttribute("mesDevice", mesDevice); model.addAttribute("mesDeviceCate", mesDeviceCateService.findList(mesDeviceCate)); return "modules/mes/mesDeviceDetail"; } /** * 创建并初始化下一个节点信息,如:排序号、默认值 */ @RequiresPermissions("mes:mesDevice:edit") @RequestMapping(value = "createNextNode") @ResponseBody public MesDevice createNextNode(MesDevice mesDevice) { if (StringUtils.isNotBlank(mesDevice.getParentCode())){ mesDevice.setParent(mesDeviceService.get(mesDevice.getParentCode())); } if (mesDevice.getIsNewRecord()) { MesDevice where = new MesDevice(); where.setParentCode(mesDevice.getParentCode()); MesDevice last = mesDeviceService.getLastByParentCode(where); // 获取到下级最后一个节点 if (last != null){ mesDevice.setTreeSort(last.getTreeSort() + 30); mesDevice.setTreeCode(IdGen.nextCode(last.getTreeCode())); }else if (mesDevice.getParent() != null){ mesDevice.setTreeCode(mesDevice.getParent().getTreeCode() + "001"); } } // 以下设置表单默认数据 if (mesDevice.getTreeSort() == null){ mesDevice.setTreeSort(MesDevice.DEFAULT_TREE_SORT); } return mesDevice; } /** * 保存数据 */ @RequiresPermissions("mes:mesDevice:edit") @PostMapping(value = "save") @ResponseBody public String save(@Validated MesDevice mesDevice) { logger.info(JSON.toJSONString(mesDevice)); MesDevice mesDevice1 = new MesDevice(); mesDevice1.setDeviceCode(mesDevice.getDeviceCode()); MesDevice info = mesDeviceService.getByEntity(mesDevice1); if (info != null && !StringUtils.equals(info.getTreeCode(), mesDevice.getTreeCode())){ return renderResult(Global.FALSE, text("保存失败,设备编码已存在!")); } mesDeviceService.save(mesDevice); return renderResult(Global.TRUE, text("保存设备成功!")); } /** * 导出数据 */ @RequiresPermissions("mes:mesDevice:view") @RequestMapping(value = "exportData") public void exportData(MesDevice mesDevice, HttpServletResponse response) { List list = mesDeviceService.findList(mesDevice); String fileName = "设备" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx"; try(ExcelExport ee = new ExcelExport("设备", MesDevice.class)){ ee.setDataList(list).write(response, fileName); } } /** * 下载模板 */ @RequiresPermissions("mes:mesDevice:view") @RequestMapping(value = "importTemplate") public void importTemplate(HttpServletResponse response) { MesDevice mesDevice = new MesDevice(); List list = ListUtils.newArrayList(mesDevice); String fileName = "设备模板.xlsx"; try(ExcelExport ee = new ExcelExport("设备", MesDevice.class, Type.IMPORT)){ ee.setDataList(list).write(response, fileName); } } /** * 导入数据 */ @ResponseBody @RequiresPermissions("mes:mesDevice:edit") @PostMapping(value = "importData") public String importData(MultipartFile file) { try { String message = mesDeviceService.importData(file); return renderResult(Global.TRUE, "posfull:"+message); } catch (Exception ex) { return renderResult(Global.FALSE, "posfull:"+ex.getMessage()); } } /** * 停用数据 */ @RequiresPermissions("mes:mesDevice:edit") @RequestMapping(value = "disable") @ResponseBody public String disable(MesDevice mesDevice) { MesDevice where = new MesDevice(); where.setStatus(MesDevice.STATUS_NORMAL); where.setParentCodes("," + mesDevice.getId() + ","); long count = mesDeviceService.findCount(where); if (count > 0) { return renderResult(Global.FALSE, text("该设备包含未停用的子设备!")); } mesDevice.setStatus(MesDevice.STATUS_DISABLE); mesDeviceService.updateStatus(mesDevice); return renderResult(Global.TRUE, text("停用设备成功")); } /** * 启用数据 */ @RequiresPermissions("mes:mesDevice:edit") @RequestMapping(value = "enable") @ResponseBody public String enable(MesDevice mesDevice) { mesDevice.setStatus(MesDevice.STATUS_NORMAL); mesDeviceService.updateStatus(mesDevice); return renderResult(Global.TRUE, text("启用设备成功")); } /** * 删除数据 */ @RequiresPermissions("mes:mesDevice:edit") @RequestMapping(value = "delete") @ResponseBody public String delete(MesDevice mesDevice) { mesDeviceService.delete(mesDevice); return renderResult(Global.TRUE, text("删除设备成功!")); } /** * 获取树结构数据 * @param excludeCode 排除的Code * @param parentCode 设置父级编码返回一级 * @param isShowCode 是否显示编码(true or 1:显示在左侧;2:显示在右侧;false or null:不显示) * @return */ @RequiresPermissions("mes:mesDevice:view") @RequestMapping(value = "treeData") @ResponseBody public List> treeData(String excludeCode, String parentCode, String isShowCode) { List> mapList = ListUtils.newArrayList(); MesDevice where = new MesDevice(); where.setStatus(MesDevice.STATUS_NORMAL); if (StringUtils.isNotBlank(parentCode)){ where.setParentCode(parentCode); } List list = mesDeviceService.findList(where); for (int i=0; i map = MapUtils.newHashMap(); map.put("id", e.getId()); map.put("pId", e.getParentCode()); map.put("name", StringUtils.getTreeNodeName(isShowCode, e.getTreeCode(), e.getTreeName())); map.put("isParent", !e.getIsTreeLeaf()); mapList.add(map); } return mapList; } /** * 修复表结构相关数据 */ @RequiresPermissions("mes:mesDevice:edit") @RequestMapping(value = "fixTreeData") @ResponseBody public String fixTreeData(MesDevice mesDevice){ if (!UserUtils.getUser().isAdmin()){ return renderResult(Global.FALSE, "操作失败,只有管理员才能进行修复!"); } mesDeviceService.fixTreeData(); return renderResult(Global.TRUE, "数据修复成功"); } /** * api列表数据 */ @RequestMapping(value = "getList") @ResponseBody public CommonResp getList(MesDevice mesDevice) { List list = mesDeviceService.findList(mesDevice); CommonResp> resp = new CommonResp<>(); resp.setData(list); return resp; } @RequiresPermissions("mes:mesDevice:view") @RequestMapping(value = "qrcode") public String qrcode(MesDevice mesDevice, Model model) { model.addAttribute("mesDevice", mesDevice); return "modules/mes/mesDeviceQrcode"; } @RequestMapping(value = "infoBySn") @ResponseBody public CommonResp infoBySn(MesDevice mesDevice) { CommonResp resp = new CommonResp<>(); MesDevice info = mesDeviceService.findInfoBySn(mesDevice.getSn()); if(ObjectUtils.isEmpty(info)){ resp.setResult("false"); resp.setMessage("设备不存在"); return resp; } resp.setData(info); return resp; } @RequiresPermissions("mes:mesDevice:view") @RequestMapping(value = "map") public String map(MesDevice mesDevice, Model model) { model.addAttribute("mesDevice", mesDevice); return "modules/mes/equipment/equipment_map"; } @RequestMapping(value = "oeeScreen") public String oeeScreen(Model model) { return "modules/mes/mesDeviceOeeScreen"; } /** * 工位客户端按 gw + lineSn 上报设备状态(无需登录) */ @PostMapping(value = "reportState") @ResponseBody public CommonResp> reportState(@RequestBody(required = false) MesDeviceReportStateReq req, HttpServletRequest request) { CommonResp> resp = new CommonResp<>(); if (req == null) { req = new MesDeviceReportStateReq(); } if (StringUtils.isBlank(req.getGw())) { req.setGw(request.getParameter("gw")); } if (StringUtils.isBlank(req.getLineSn())) { req.setLineSn(request.getParameter("lineSn")); } if (StringUtils.isBlank(req.getState())) { req.setState(request.getParameter("state")); } if (StringUtils.isBlank(req.getContent())) { req.setContent(request.getParameter("content")); } try { MesDevice device = mesDeviceStateReportService.reportByGw(req.getGw(), req.getLineSn(), req.getState(), req.getContent()); Map data = MapUtils.newHashMap(); data.put("deviceSn", device.getSn()); data.put("gw", device.getGw()); data.put("lineSn", device.getLineSn()); data.put("state", req.getState()); resp.setData(data); resp.setResult(Global.TRUE); resp.setMessage(text("操作成功!")); } catch (Exception e) { logger.error("设备状态上报失败", e); resp.setResult(Global.FALSE); resp.setMessage(e.getMessage()); } return resp; } @RequestMapping(value = "screen5Data") @ResponseBody public CommonResp> screen5Data() { CommonResp> resp = new CommonResp<>(); Map data = MapUtils.newHashMap(); Date now = new Date(); Date weekStart = DateUtils.addDays(now, -7); data.put("deviceCount", mesDeviceService.findList(new MesDevice()).size()); MesDevicePreventTask preventQuery = new MesDevicePreventTask(); preventQuery.setStartDate_gte(weekStart); preventQuery.setStartDate_lte(now); List preventList = mesDevicePreventTaskService.findList(preventQuery); data.put("preventiveCount", preventList.size()); List> preventiveStatus = ListUtils.newArrayList(); long pending = preventList.stream().filter(p -> "0".equals(p.getState())).count(); long processing = preventList.stream().filter(p -> "1".equals(p.getState())).count(); long completed = preventList.stream().filter(p -> "2".equals(p.getState())).count(); long unfinished = preventList.stream().filter(p -> "3".equals(p.getState())).count(); Map pendingMap = MapUtils.newHashMap(); pendingMap.put("name", "未开始"); pendingMap.put("value", pending); preventiveStatus.add(pendingMap); Map processingMap = MapUtils.newHashMap(); processingMap.put("name", "进行中"); processingMap.put("value", processing); preventiveStatus.add(processingMap); Map completedMap = MapUtils.newHashMap(); completedMap.put("name", "已完成"); completedMap.put("value", completed); preventiveStatus.add(completedMap); Map unfinishedMap = MapUtils.newHashMap(); unfinishedMap.put("name", "未完成"); unfinishedMap.put("value", unfinished); preventiveStatus.add(unfinishedMap); data.put("preventiveStatus", preventiveStatus); data.put("preventiveList", preventList.stream().limit(10).collect(Collectors.toList())); MesDeviceUrgency urgencyQuery = new MesDeviceUrgency(); urgencyQuery.setStartDate_gte(weekStart); urgencyQuery.setStartDate_lte(now); List urgencyList = mesDeviceUrgencyService.findList(urgencyQuery); data.put("urgencyCount", urgencyList.size()); List> urgencyStatus = ListUtils.newArrayList(); long urgencyPending = urgencyList.stream().filter(u -> "0".equals(u.getState())).count(); long urgencyCompleted = urgencyList.stream().filter(u -> "1".equals(u.getState())).count(); Map urgencyPendingMap = MapUtils.newHashMap(); urgencyPendingMap.put("name", "维修中"); urgencyPendingMap.put("value", urgencyPending); urgencyPendingMap.put("color", "#ff9800"); urgencyStatus.add(urgencyPendingMap); Map urgencyCompletedMap = MapUtils.newHashMap(); urgencyCompletedMap.put("name", "已完成"); urgencyCompletedMap.put("value", urgencyCompleted); urgencyCompletedMap.put("color", "#00d5d8"); urgencyStatus.add(urgencyCompletedMap); data.put("urgencyStatus", urgencyStatus); Map contentCount = urgencyList.stream() .filter(u -> StringUtils.isNotBlank(u.getContent())) .collect(Collectors.groupingBy(u -> u.getContent(), Collectors.counting())); List> urgencyContent = ListUtils.newArrayList(); contentCount.entrySet().stream() .sorted((a, b) -> Long.compare(b.getValue(), a.getValue())) .limit(6) .forEach(e -> { Map contentMap = MapUtils.newHashMap(); contentMap.put("name", e.getKey()); contentMap.put("value", e.getValue()); urgencyContent.add(contentMap); }); data.put("urgencyContent", urgencyContent); data.put("urgencyList", urgencyList.stream().limit(10).collect(Collectors.toList())); data.put("weekStart", DateUtils.formatDate(weekStart, "yyyy-MM-dd HH:mm:ss")); data.put("weekEnd", DateUtils.formatDate(now, "yyyy-MM-dd HH:mm:ss")); resp.setData(data); resp.setResult(Global.TRUE); return resp; } @RequestMapping(value = "oeeScreenData") @ResponseBody public CommonResp> oeeScreenData() { CommonResp> resp = new CommonResp<>(); resp.setData(mesDeviceOeeStatsService.buildOeeScreenData()); resp.setResult(Global.TRUE); return resp; } }