package com.shlanbao.tzsc.pms.isp.service.impl; import com.google.gson.Gson; import com.shlanbao.tzsc.base.dao.SchWorkorderDaoI; import com.shlanbao.tzsc.base.mapping.SchWorkorder; import com.shlanbao.tzsc.base.service.BaseService; import com.shlanbao.tzsc.init.BaseParams; import com.shlanbao.tzsc.pms.isp.beans.*; import com.shlanbao.tzsc.pms.isp.service.PmsIspServiceI; import com.shlanbao.tzsc.pms.md.eqp.beans.EquipmentsBean; import com.shlanbao.tzsc.pms.sch.workorder.beans.WorkOrderBean; import com.shlanbao.tzsc.utils.tools.*; import net.sf.json.JSONObject; import net.sf.json.util.JSONUtils; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import javax.servlet.http.HttpServletRequest; import java.io.File; import java.util.*; @Service public class PmsIspServiceImpl extends BaseService implements PmsIspServiceI { @Autowired private SchWorkorderDaoI schWorkorderDao; @Override public List getAllRollerPackerDatas() { List list = new ArrayList<>(); try { //从redis获取班次 Map shiftMap = RedisUtil.getMap("shift"); if(shiftMap == null) return null; String shift = (String) shiftMap.get("ID"); if(shift==null) return null; //获取所有卷烟机 包装机 成型机 List allRollersLt = BaseParams.getAllRollersLt(); List allPackersLt = BaseParams.getAllPackersLt(); //所有卷烟机实时数据 if(allRollersLt!=null){ List rollRealData = getRealData(shift, allRollersLt); if (rollRealData != null) list.addAll(rollRealData); } //所有包装机实时数据 if(allPackersLt!=null){ List packRealData = getRealData(shift, allPackersLt); if (packRealData != null) list.addAll(packRealData); } } catch (Exception e) { e.printStackTrace(); } return list; } @Override public List getAllFilterDatas() { // List filterDatas = new ArrayList(); List list = new ArrayList<>(); try { //从redis获取班次 Map shiftMap = RedisUtil.getMap("shift"); if(shiftMap == null) return null; String shift = (String) shiftMap.get("ID"); if(shift==null) return null; //获取所有成型机 List allFiltersLt = BaseParams.getAllFiltersLt(); //所有成型机实时数据 List formRealData = getRealData(shift, allFiltersLt); if (formRealData != null) list.addAll(formRealData); } catch (Exception e) { e.printStackTrace(); } return list; } @Override public List initRollerPackerGroupWorkOrderInfo(Long type) { //查询运行的卷烟机工单的设备号,班组,牌号,计划产量 // String hql = "select o.mdEquipment.equipmentCode,o.mdTeam.name,o.mdMat.simpleName,o.qty,o.mdShift.name from SchWorkorder o where o.type=? and o.sts=2 and o.enable=1 and o.del=0"; // String sql = " SELECT a.QTY, b.EQUIPMENT_CODE,c.NAME as teamName,d.NAME as shiftName,e.SIMPLE_NAME" + // "FROM SCH_WORKORDER a LEFT JOIN MD_EQUIPMENT b on a.EQP = b.ID " + // "LEFT JOIN MD_TEAM c on a.TEAM = c.ID " + // "LEFT JOIN MD_SHIFT d on a.SHIFT = d.ID " + // "LEFT JOIN MD_MAT e on a.MAT = e.ID " + // "WHERE a.TYPE = ? AND a.STS =2 AND a.ENABLE = 1 AND a.DEL = 0"; // List objList = schWorkorderDao.queryObjectArray(hql, type); // List workorderInfoBeans = new ArrayList(); // for (Object[] objects : objList) { // WorkorderInfoBean workorderInfoBean = new WorkorderInfoBean(); // workorderInfoBean.setCode(objects[0].toString()); // workorderInfoBean.setTeamName(objects[1].toString()); // workorderInfoBean.setMatName(objects[2].toString()); // workorderInfoBean.setQty(Double.valueOf(objects[3].toString())); // workorderInfoBean.setShiftName(objects[4].toString()); // workorderInfoBeans.add(workorderInfoBean); // } List workorderInfoBeans = new ArrayList(); String hql="from SchWorkorder o where o.del=0 and o.isCheck=1 and o.sts=2 "; try { //查询redis是否有工单集,没有则创建 if (!RedisUtil.hasKey("workorder")) { RedisUtil.setMap("workorder", new HashMap<>()); List query = schWorkorderDao.query(hql); if(query==null||query.size()<1) return workorderInfoBeans; for (SchWorkorder order:query){ WorkOrderBean bean = new WorkOrderBean(); // WorkorderInfoBean bean = new WorkorderInfoBean(); BeanConvertor.copyProperties(order,bean); bean.setType(order.getType()); bean.setMat(order.getMdMat().getId()); bean.setTeam(order.getMdTeam().getId()); bean.setShift(order.getMdShift().getId()); bean.setEquipment(order.getMdEquipment().getId()); bean.setEqpcode(order.getMdEquipment().getEquipmentCode()); bean.setUnit(order.getMdUnit().getId()); bean.setTeamName(order.getMdTeam().getName()); bean.setMatName(order.getMdMat().getSimpleName()); bean.setQty(order.getQty()); bean.setShiftName(order.getMdShift().getName()); //前端界面只需要传参type类型的工单 if(order.getType()==type) workorderInfoBeans.add(bean); //存入redis RedisUtil.addMap("workorder", bean.getCode(), JSONObject.fromObject(bean).toString()); } }else { //从redies获取工单集 Map workorder = RedisUtil.getMap("workorder"); //redis有数据时 if(workorder!=null&&!workorder.isEmpty()){ for (Map.Entry entry : workorder.entrySet()) { JSONObject jsonObject = JSONObject.fromObject(entry.getValue()); Long redisType = Long.parseLong(jsonObject.opt("type").toString()); //对应type的工单 if(redisType==type){ String s = JSONObject.fromObject(entry.getValue()).toString(); Gson gson =new Gson(); WorkOrderBean workorderInfoBean = gson.fromJson(s, WorkOrderBean.class); workorderInfoBeans.add(workorderInfoBean); } } } } }catch (Exception e){ e.printStackTrace(); } return workorderInfoBeans; } @Override public List initRollerPackerGroups( HttpServletRequest request) { String xmlPath = WebContextUtil.getVirtualPath("xmlcfg", request) + File.separator + "group.xml"; NodeList nodeList = XmlUtil.getRootNodes(xmlPath, "group"); List rollerPackerGroups = new ArrayList(); String type = "roller-packer"; for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (XmlUtil.getValueByNodeName(node, "group-type").equals(type)) { String id = XmlUtil.getValueByNodeName(node, "group-id"); //机组类型 String groupType = XmlUtil.getValueByNodeName(node, "group-type"); //机组名称 String groupName = XmlUtil.getValueByNodeName(node, "group-name"); //卷烟机 Node roller = XmlUtil.getNodeByNodeName(node, "roller"); String rCode = XmlUtil.getValueByNodeName(roller, "code"); String rName = XmlUtil.getValueByNodeName(roller, "name"); String tType = XmlUtil.getValueByNodeName(roller, "type"); //包装机 Node packer = XmlUtil.getNodeByNodeName(node, "packer"); String pCode = XmlUtil.getValueByNodeName(packer, "code"); String pName = XmlUtil.getValueByNodeName(packer, "name"); String pType = XmlUtil.getValueByNodeName(packer, "type"); rollerPackerGroups.add(new RollerPackerGroup(id, groupType, groupName, rCode, rName, tType, pCode, pName, pType)); } } //从数据库读取 //1.查找所有的卷烟机 // String sql = "SELECT a.EQUIPMENT_CODE ,a.EQUIPMENT_NAME,b.CODE FROM MD_EQUIPMENT a " + // "LEFT JOIN MD_EQP_TYPE b on a.EQP_TYPE_ID = b.id " + // "LEFT JOIN MD_EQP_CATEGORY c on b.CID = c.id " + // "WHERE c.CODE = ? AND a.ENABLED = 1 AND a.DEL = 0 " + // "ORDER BY c.CODE ,a.EQUIPMENT_NAME "; // try { // final int EQU_TYPE_1 = 1; //卷包机组 // final int EQU_TYPE_2 = 2; //包装机组 // //定义需要取出的字段 code:设备编号,name:设备名字,type:设备型号 // String params[] = {"code", "name", "type"}; // List> roller = getEquList(sql, EQU_TYPE_1, params); // List> packer = getEquList(sql, EQU_TYPE_2, params); // rollerPackerGroups.addAll(createEquGroup(roller, packer, params)); //从基础数据中读取 // List allRollersLt = new ArrayList<>(); // List allRollersLt1 = BaseParams.getAllRollersLt(); // List allPackersLt = new ArrayList<>(); // List allPackersLt1 = BaseParams.getAllPackersLt(); // //没有数据 // // // //过滤设备 // if(allRollersLt1!=null){ // for (int i = 0; i < allRollersLt1.size(); i++) { // if (allRollersLt1.get(i).getEquipmentCode().length() >= 3) { // allRollersLt.add(allRollersLt1.get(i)); // } // } // } // if(allPackersLt1!=null){ // for (int i = 0; i < allPackersLt1.size(); i++) { // if (allPackersLt1.get(i).getEquipmentCode().length() >= 3) { // allPackersLt.add(allPackersLt1.get(i)); // } // } // } // // //以设备code进行排序-设备code必须是数字 // Collections.sort(allRollersLt, (o1, o2) -> Integer.valueOf(o1.getEquipmentCode()).compareTo(Integer.valueOf(o2.getEquipmentCode()))); // Collections.sort(allPackersLt, (o1, o2) -> Integer.valueOf(o1.getEquipmentCode()).compareTo(Integer.valueOf(o2.getEquipmentCode()))); //// log.info("所有卷包机" + allRollersLt.toString()); //// log.info("所有包装机" + allPackersLt.toString()); // // //组大小-以较少的设备长度作为组的大小 // int size = allRollersLt.size() - allPackersLt.size() > 0 ? allPackersLt.size() : allRollersLt.size(); // String groupType = "roller-packer"; // for (int i = 0; i < size; i++) { // String id = String.valueOf(i + 1); // String groupName = (i + 1) + "号卷包机组"; // String rCode = allRollersLt.get(i).getEquipmentCode(); // String rName = allRollersLt.get(i).getName(); // String tType = allRollersLt.get(i).getEqpTypeId(); // String pCode = allPackersLt.get(i).getEquipmentCode(); // String pName = allPackersLt.get(i).getName(); // String pType = allPackersLt.get(i).getEqpTypeId(); // rollerPackerGroups.add(new RollerPackerGroup(id, groupType, groupName, rCode, rName, tType, pCode, pName, pType)); // } // } catch (Exception e) { // e.printStackTrace(); // } return rollerPackerGroups; } @Override public List initBoxerGroups(HttpServletRequest request) { String xmlPath = WebContextUtil.getVirtualPath("xmlcfg", request) + File.separator + "group.xml"; NodeList nodeList = XmlUtil.getRootNodes(xmlPath, "group"); List boxerGroups = new ArrayList(); String gtype = "boxer"; for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (XmlUtil.getValueByNodeName(node, "group-type").equals(gtype)) { String id = XmlUtil.getValueByNodeName(node, "group-id"); //机组类型 String groupType = XmlUtil.getValueByNodeName(node, "group-type"); //机组名称 String name = XmlUtil.getValueByNodeName(node, "group-name"); String code = XmlUtil.getValueByNodeName(node, "code"); String type = XmlUtil.getValueByNodeName(node, "type"); boxerGroups.add(new BoxerGroup(id, groupType, name, code, type)); } } return boxerGroups; } @Override public List initShootererGroups(HttpServletRequest request) { String xmlPath = WebContextUtil.getVirtualPath("xmlcfg", request) + File.separator + "group.xml"; NodeList nodeList = XmlUtil.getRootNodes(xmlPath, "group"); List shooterGroups = new ArrayList(); String gtype = "shooter"; for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (XmlUtil.getValueByNodeName(node, "group-type").equals(gtype)) { String id = XmlUtil.getValueByNodeName(node, "group-id"); //机组类型 String groupType = XmlUtil.getValueByNodeName(node, "group-type"); //机组名称 String name = XmlUtil.getValueByNodeName(node, "group-name"); String code = XmlUtil.getValueByNodeName(node, "code"); String type = XmlUtil.getValueByNodeName(node, "type"); shooterGroups.add(new ShooterGroup(id, groupType, name, code, type)); } } return shooterGroups; } @Override public List initFilterGroups(HttpServletRequest request) { String xmlPath = WebContextUtil.getVirtualPath("xmlcfg", request) + File.separator + "group.xml"; NodeList nodeList = XmlUtil.getRootNodes(xmlPath, "group"); List filterGroups = new ArrayList(); String gtype = "filter"; for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (XmlUtil.getValueByNodeName(node, "group-type").equals(gtype)) { String id = XmlUtil.getValueByNodeName(node, "group-id"); //机组类型 String groupType = XmlUtil.getValueByNodeName(node, "group-type"); //机组名称 String name = XmlUtil.getValueByNodeName(node, "name"); String code = XmlUtil.getValueByNodeName(node, "code"); String type = XmlUtil.getValueByNodeName(node, "type"); filterGroups.add(new FilterGroup(id, groupType, name, code, type)); } } //从基础数据获取 // try { // List allFilters = new ArrayList<>(); // List allFiltersLt = BaseParams.getAllFiltersLt(); // allFiltersLt.addAll(allFilters); // // //以设备code进行排序-设备code必须是数字 // Collections.sort(allFiltersLt, (o1, o2) -> Integer.valueOf(o1.getEquipmentCode()).compareTo(Integer.valueOf(o2.getEquipmentCode()))); // // for (int i = 0; i < allFiltersLt.size(); i++) { // String id = (i + 1) + ""; // String groupType = "filter"; // String name = id + "号成型机"; // String code = allFiltersLt.get(i).getEquipmentCode(); // String type = allFiltersLt.get(i).getEqpTypeId(); // filterGroups.add(new FilterGroup(id, groupType, name, code, type)); // } //// log.info("成型机组:" + filterGroups.toString()); // // } catch (Exception e) { // e.printStackTrace(); // } return filterGroups; } @Override public Map getEquDetailRealData(Map map) { Map root = new HashMap<>(); try { //得到前台界面传过来的设备id String equCode = (String) map.get("equCode"); String equType = (String) map.get("equType"); if (StringUtils.isEmpty(equCode)||StringUtils.isEmpty(equType)) return null; //从redis获取班次 Map shift = RedisUtil.getMap("shift"); if(shift==null) return null; String shiftCode = (String) shift.get("ID"); if(shiftCode==null) return null; //从redis获取解除数据 Map data = RedisUtil.getMap(shiftCode +equCode); if (data != null && !data.isEmpty()) { data.put("online", data.get(equType+"State")); data.put("code", map.get("equCode")); Object ssz = RedisUtil.getMapKey(equCode+"CC","ssz"); Object ncz = RedisUtil.getMapKey(equCode+"CC","ncz"); Object xhm = RedisUtil.getMapKey(equCode+"CC","xhm"); Object thm = RedisUtil.getMapKey(equCode+"CC","thm"); if (data != null && data.size()>0) { Object shuisongzhiVal = data.get("shuisongzhiVal"); data.put("shuisongzhiVal",Double.parseDouble(StringUtil.notEmpty(shuisongzhiVal)?shuisongzhiVal.toString():"0")*Double.parseDouble(ssz==null?"0":ssz.toString())); Object foilPaper = data.get("foilPaper"); data.put("foilPaper",Double.parseDouble(StringUtil.notEmpty(foilPaper)?foilPaper.toString():"0")*Double.parseDouble(ncz==null?"0":ncz.toString())); Object boxFilm = data.get("boxFilm"); data.put("boxFilm",Double.parseDouble(StringUtil.notEmpty(boxFilm)?boxFilm.toString():"0")*Double.parseDouble(xhm==null?"0":xhm.toString())); Object barrelFilm = data.get("barrelFilm"); data.put("barrelFilm",Double.parseDouble(StringUtil.notEmpty(barrelFilm)?barrelFilm.toString():"0")*Double.parseDouble(thm==null?"0":thm.toString())); } root.put("common", data); //获取主数据 Map main = RedisUtil.getMap(shiftCode + equCode + "M"); /* if(main!=null&&!main.isEmpty()){ List mList = new ArrayList(); for (Map.Entry entry : main.entrySet()) { Map temp = new HashMap<>(); temp.put("id",entry.getKey()); temp.put("val",entry.getValue()==null?"":entry.getValue()); mList.add(temp); } root.put("main",mList); }*/ root.put("main",main); } } catch (Exception e) { e.printStackTrace(); } return root; } @Override public Map getFormatted() { Map shift = RedisUtil.getMap("shift"); if(shift==null) return null; String shiftCode = (String) shift.get("ID"); if(shiftCode==null) return null; Map root = new HashMap<>(); try { //从redis获取解除数据--601为提升机固定code Map data = RedisUtil.getMap(shiftCode +"601"); Map datam = RedisUtil.getMap(shiftCode +"601M"); if(data!=null) root.put("data",data); if(datam!=null) root.put("datam",datam); }catch (Exception e){ e.printStackTrace(); } return root; } /////////////////////////////////////////////////////////////////////////// // 工具方法 /////////////////////////////////////////////////////////////////////////// /** * @param */ private List getRealData(String shift, List equlist) throws Exception { List list = new ArrayList<>(); //2.过滤错误数据(设备code 1改为101) 且工单中包含该机器 for (EquipmentsBean bean : equlist) { if (bean.getEquipmentCode().length() >= 3 ) { //3.查询redis实时数据 卷烟机 Map map = RedisUtil.getMap(shift + bean.getEquipmentCode()); if (map != null && !map.isEmpty()) { map.put("online", map.get(bean.getEqpTypeId()+"State")); map.put("code", bean.getEquipmentCode()); list.add(map); } } } return list; } }