| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564 |
- 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<MesDevice> 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<MesDevice> 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<MesDevice> listData(MesDevice mesDevice, HttpServletRequest request, HttpServletResponse response) {
- // System.out.println(request.getParameterMap());
- mesDevice.setPage(new Page<>(request, response));
- Page<MesDevice> 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<MesDevice> 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<MesDevice> 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<Map<String, Object>> treeData(String excludeCode, String parentCode, String isShowCode) {
- List<Map<String, Object>> mapList = ListUtils.newArrayList();
- MesDevice where = new MesDevice();
- where.setStatus(MesDevice.STATUS_NORMAL);
- if (StringUtils.isNotBlank(parentCode)){
- where.setParentCode(parentCode);
- }
- List<MesDevice> list = mesDeviceService.findList(where);
- for (int i=0; i<list.size(); i++){
- MesDevice e = list.get(i);
- // 过滤非正常的数据
- if (!MesDevice.STATUS_NORMAL.equals(e.getStatus())){
- continue;
- }
- // 过滤被排除的编码(包括所有子级)
- if (StringUtils.isNotBlank(excludeCode)){
- if (e.getId().equals(excludeCode)){
- continue;
- }
- if (e.getParentCodes().contains("," + excludeCode + ",")){
- continue;
- }
- }
- Map<String, Object> 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<MesDevice> list = mesDeviceService.findList(mesDevice);
- CommonResp<List<MesDevice>> 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<MesDevice> 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<Map<String, Object>> reportState(@RequestBody(required = false) MesDeviceReportStateReq req, HttpServletRequest request) {
- CommonResp<Map<String, Object>> 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<String, Object> 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<Map<String, Object>> screen5Data() {
- CommonResp<Map<String, Object>> resp = new CommonResp<>();
- Map<String, Object> 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<MesDevicePreventTask> preventList = mesDevicePreventTaskService.findList(preventQuery);
- data.put("preventiveCount", preventList.size());
- List<Map<String, Object>> 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<String, Object> pendingMap = MapUtils.newHashMap();
- pendingMap.put("name", "未开始");
- pendingMap.put("value", pending);
- preventiveStatus.add(pendingMap);
- Map<String, Object> processingMap = MapUtils.newHashMap();
- processingMap.put("name", "进行中");
- processingMap.put("value", processing);
- preventiveStatus.add(processingMap);
- Map<String, Object> completedMap = MapUtils.newHashMap();
- completedMap.put("name", "已完成");
- completedMap.put("value", completed);
- preventiveStatus.add(completedMap);
- Map<String, Object> 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<MesDeviceUrgency> urgencyList = mesDeviceUrgencyService.findList(urgencyQuery);
- data.put("urgencyCount", urgencyList.size());
- List<Map<String, Object>> 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<String, Object> urgencyPendingMap = MapUtils.newHashMap();
- urgencyPendingMap.put("name", "维修中");
- urgencyPendingMap.put("value", urgencyPending);
- urgencyPendingMap.put("color", "#ff9800");
- urgencyStatus.add(urgencyPendingMap);
- Map<String, Object> urgencyCompletedMap = MapUtils.newHashMap();
- urgencyCompletedMap.put("name", "已完成");
- urgencyCompletedMap.put("value", urgencyCompleted);
- urgencyCompletedMap.put("color", "#00d5d8");
- urgencyStatus.add(urgencyCompletedMap);
- data.put("urgencyStatus", urgencyStatus);
- Map<String, Long> contentCount = urgencyList.stream()
- .filter(u -> StringUtils.isNotBlank(u.getContent()))
- .collect(Collectors.groupingBy(u -> u.getContent(), Collectors.counting()));
- List<Map<String, Object>> urgencyContent = ListUtils.newArrayList();
- contentCount.entrySet().stream()
- .sorted((a, b) -> Long.compare(b.getValue(), a.getValue()))
- .limit(6)
- .forEach(e -> {
- Map<String, Object> 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<Map<String, Object>> oeeScreenData() {
- CommonResp<Map<String, Object>> resp = new CommonResp<>();
- resp.setData(mesDeviceOeeStatsService.buildOeeScreenData());
- resp.setResult(Global.TRUE);
- return resp;
- }
- }
|