package com.shlanbao.tzsc.pms.msg.info.service.impl; import java.util.*; import com.google.gson.Gson; import com.shlanbao.tzsc.init.BaseParams; import com.shlanbao.tzsc.pms.md.eqp.beans.EquipmentsBean; import com.shlanbao.tzsc.pms.sys.user.beans.UserBean; import com.shlanbao.tzsc.pms.websocket.WebSocketMessage; import com.shlanbao.tzsc.pms.websocket.WebSocketSessionUtils; import com.shlanbao.tzsc.utils.tools.FillUserInfoUtil; import com.shlanbao.tzsc.utils.tools.LogAnno; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.shlanbao.tzsc.base.dao.MsgInfoDaoI; import com.shlanbao.tzsc.base.mapping.MdEquipment; import com.shlanbao.tzsc.base.mapping.MsgInfo; import com.shlanbao.tzsc.base.mapping.SysUser; import com.shlanbao.tzsc.base.model.DataGrid; import com.shlanbao.tzsc.base.model.Json; import com.shlanbao.tzsc.base.model.PageParams; import com.shlanbao.tzsc.base.model.SessionInfo; import com.shlanbao.tzsc.base.service.BaseService; import com.shlanbao.tzsc.pms.md.eqp.service.EquipmentsServiceI; import com.shlanbao.tzsc.pms.msg.info.beans.MsgInfoBean; import com.shlanbao.tzsc.pms.msg.info.beans.MsgOperatorBean; import com.shlanbao.tzsc.pms.msg.info.service.MsgInfoServiceI; import com.shlanbao.tzsc.pms.sys.role.beans.RoleBean; import com.shlanbao.tzsc.pms.sys.user.service.UserServiceI; import com.shlanbao.tzsc.utils.tools.StringUtil; import com.shlanbao.tzsc.utils.tools.WebContextUtil; import org.springframework.util.StringUtils; import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.WebSocketSession; import javax.servlet.http.HttpServletRequest; @Service public class MsgInfoServiceImpl extends BaseService implements MsgInfoServiceI{ @Autowired protected EquipmentsServiceI equipmentService; @Autowired protected MsgInfoDaoI msgInfoDao; @Autowired protected UserServiceI userService; @Autowired private HttpServletRequest request; /** * 该方法只是进行保存添加或者修改的数据 */ @LogAnno(operateType = "新增或编辑机台通知信息") @Override public Json addMsgInfo(MsgInfo info) throws Exception { Json json=new Json(); SessionInfo sessionInfo = (SessionInfo) WebContextUtil.getObjectFromSession(); MsgInfo temp = info; //当Id不等于空时,则证明是新增的消息,否则为修改的消息 if(sessionInfo.getUser()!=null){ if(StringUtil.notNull(info.getId())){ //修改 info = msgInfoDao.findById(MsgInfo.class, info.getId()); info.setTime(new Date()); info.setFlag(1l); info.setTitle(temp.getTitle()); info.setContent(temp.getContent()); info.setEid(temp.getEid()); //设置推送机台 info.setSendFlag(temp.getSendFlag()); //设置推送标记 info.setSendType(temp.getSendType()); //设置推送类型 info.setApproveTime(new Date()); //设置推送时间 FillUserInfoUtil.fillUpdateUserInfo(info,request); }else{ //设置发起人 info.setSysUserByInitiator(new SysUser(sessionInfo.getUser().getId())); info.setDel(0L); info.setId(null); info.setTime(new Date()); info.setFlag(1l); //推送到机台 FillUserInfoUtil.fillCreateUserInfo(info,request); } msgInfoDao.saveOrUpdate(info); json.setMsg("操作成功"); json.setSuccess(true); }else{ json.setMsg("用户未登录"); json.setSuccess(false); } return json; } /** * 该方法用于审批保存数据 */ @Override public void saveMsgInfo(MsgInfo msgInfo) throws Exception { MsgInfo info=msgInfoDao.findById(MsgInfo.class, msgInfo.getId()); info.setApproveContent(msgInfo.getApproveContent()); info.setFlag(msgInfo.getFlag()); msgInfoDao.saveOrUpdate(info); } @Override public DataGrid queryMsgInfo(MsgInfoBean msgInfo, PageParams pageParams)throws Exception { String hql = "from MsgInfo o where 1=1 and o.del=0 and o.msgInfo.id is null"; String params = ""; if(StringUtil.notNull(msgInfo.getTitle())){ params+=" and o.title like '%"+msgInfo.getTitle()+"%'"; } if(msgInfo.getSendFlag()!=null){ params+=" and o.sendFlag = '"+msgInfo.getSendFlag()+"'"; } //时间 if(!StringUtils.isEmpty(msgInfo.getTime())&&!StringUtils.isEmpty(msgInfo.getApproveTime())) params+=StringUtil.OraclefmtDateBetweenParams("o.time", msgInfo.getTime() +" 00:00:00", msgInfo.getApproveTime()+" 23:59:59"); List infos = msgInfoDao.queryByPage(hql.concat(params), pageParams); String sqlHql = "select count(*) " + hql; long total = msgInfoDao.queryTotal(sqlHql.concat(params)); // List infos2=msgInfoDao.query(hql); List infoBeans=new ArrayList(); for (MsgInfo msgInfo2 : infos) { MsgInfoBean infoBean=beanConvertor.copyProperties(msgInfo2, MsgInfoBean.class); /*if(msgInfo2.getSysUserByApproval()!=null){ infoBean.setApprovalName(msgInfo2.getSysUserByApproval().getName()); infoBean.setApproval(msgInfo2.getSysUserByApproval().getId()); }*/ if(msgInfo2.getSysUserByInitiator()!=null){ infoBean.setInitiatorName(msgInfo2.getSysUserByInitiator().getName()); infoBean.setInitiator(msgInfo2.getSysUserByInitiator().getId()); } /*if(msgInfo2.getSysUserByIssuer()!=null){ infoBean.setIssuerName(msgInfo2.getSysUserByIssuer().getName()); infoBean.setIssuer(msgInfo2.getSysUserByIssuer().getId()); }*/ FillUserInfoUtil.fillCreateAndUpdateUserInfo(msgInfo2,infoBean); infoBeans.add(infoBean); } return new DataGrid(infoBeans, total); } @LogAnno(operateType = "删除当前机台通知信息") @Override public void deleteMsgInfoById(String id) throws Exception { msgInfoDao.findById(MsgInfo.class, id).setDel(1L); } @Override public MsgOperatorBean getCurrUserOperator() throws Exception { SessionInfo sessionInfo = (SessionInfo) WebContextUtil.getObjectFromSession(); //判断发起人的角色是否和审批人的角色一致 List roleBeans = sessionInfo.getRoles(); List roleNames = new ArrayList(); for (RoleBean roleBean : roleBeans) { if(roleBean.isChecked()) roleNames.add(roleBean.getName()); } MsgOperatorBean msgOperatorBean = new MsgOperatorBean(); if(roleNames.contains("设备管理员")) //审批 msgOperatorBean.setName("APPROVE"); if(roleNames.contains("车间主任")) //签发 msgOperatorBean.setName("ISSUER"); msgOperatorBean.setOperId(sessionInfo.getUser().getId()); return msgOperatorBean; } @Override public MsgInfoBean getMsgInfoBeanById(String id) throws Exception { MsgInfo msgInfo = msgInfoDao.findById(MsgInfo.class, id); MsgInfoBean bean = beanConvertor.copyProperties(msgInfo, MsgInfoBean.class); if(msgInfo.getSysUserByInitiator()!=null){ bean.setInitiatorName(msgInfo.getSysUserByInitiator().getName()); } return bean; } @Override public List getMsgInfo() throws Exception { String hql="from MsgInfo o where 1=1 and o.del=0 and o.flag=3 and o.msgInfo is null order by o.time desc"; //插叙前5条数据 List msgInfos = msgInfoDao.queryByPage(hql, 1, 5, new Object[]{}); List beans = beanConvertor.copyList(msgInfos, MsgInfoBean.class); return beans; } @Override public List getMsgInfoApprove(String id) throws Exception { String hql = "from MsgInfo o where 1=1 and o.del=0 and o.msgInfo.id='" + id + "'"; List infos = msgInfoDao.query(hql, new Object[]{}); List beans = new ArrayList(); for (MsgInfo info : infos) { MsgInfoBean bean = beanConvertor.copyProperties(info, MsgInfoBean.class); if(StringUtil.notNull(bean.getInitiator())) bean.setInitiatorName(userService.getSysUserById(bean.getInitiator()).getName()); if(StringUtil.notNull(bean.getApproval())) bean.setApprovalName(userService.getSysUserById(bean.getApproval()).getName()); if(StringUtil.notNull(bean.getIssuer())) bean.setIssuerName(userService.getSysUserById(bean.getIssuer()).getName()); if(info.getMsgInfo() != null){ bean.setPid(info.getMsgInfo().getId()); bean.setpName(info.getMsgInfo().getTitle()); } beans.add(bean); } return beans; } @Override public MsgInfoBean getMsgInfoById(String id) throws Exception { MsgInfo info=msgInfoDao.findById(MsgInfo.class, id); MsgInfoBean infoBean=beanConvertor.copyProperties(info, MsgInfoBean.class); if(info.getSysUserByApproval()!=null){ infoBean.setApprovalName(info.getSysUserByApproval().getName()); infoBean.setApproval(info.getSysUserByApproval().getId()); } if(info.getSysUserByIssuer()!=null){ infoBean.setIssuerName(info.getSysUserByIssuer().getName()); infoBean.setIssuer(info.getSysUserByIssuer().getId()); } return infoBean; } @Override public List queryMdequipment() { try { StringBuffer sb=new StringBuffer(); sb.append(" select t1.* from ( "); sb.append(" select c.code,a.equipment_code,a.equipment_name,to_number(a.EQUIPMENT_CODE) as num ,a.id From MD_EQUIPMENT a,MD_EQP_TYPE b,MD_EQP_CATEGORY c where a.eqp_type_id=b.id and b.cid=c.id and a.del=0 "); sb.append(" ) t1 order by t1.num"); List list=msgInfoDao.queryBySql(sb.toString()); if(list!=null && list.size()>0){ List listMe=new ArrayList(); for(Object o:list){ MdEquipment ecrb=new MdEquipment(); Object[] temp=(Object[]) o; ecrb.setId(StringUtil.convertObjToString(temp[0])); ecrb.setEquipmentCode(StringUtil.convertObjToString(temp[1])); ecrb.setEquipmentName(StringUtil.convertObjToString(temp[2])); ecrb.setAttr2(StringUtil.convertObjToString(temp[4])); listMe.add(ecrb); } return listMe; } } catch (Exception e) { e.printStackTrace(); } return null; } @Override public void saveMsgSend(MsgInfo msgInfo) { try { msgInfoDao.update(msgInfo); }catch (Exception e){ e.printStackTrace(); } } @Override public List getEquInfo(UserBean userBean) { try { List equList=new ArrayList(); StringBuilder sb = new StringBuilder(); sb.append("SELECT a.ID,a.EQUIPMENT_NAME,a.EQUIPMENT_CODE, c.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 a.DEL = 0 "); if(userBean!=null&& !StringUtils.isEmpty(userBean.getName())){ sb.append("AND a.EQUIPMENT_NAME LIKE '%"+userBean.getName()+"%' "); } if(userBean!=null&& !StringUtils.isEmpty(userBean.getGender())){ sb.append(" AND c.CODE = "+userBean.getGender()+" "); } sb.append("ORDER BY a.EQUIPMENT_CODE"); List list=msgInfoDao.queryBySql(sb.toString()); if(list!=null && list.size()>0){ for(Object o:list){ MdEquipment equ=new MdEquipment(); Object[] temp=(Object[]) o; equ.setId(StringUtil.convertObjToString(temp[0])); equ.setEquipmentName(StringUtil.convertObjToString(temp[1])); equ.setEquipmentCode(StringUtil.convertObjToString(temp[2])); equ.setAttr2(StringUtil.convertObjToString(temp[0])); equList.add(equ); } } return equList; }catch (Exception e){ e.printStackTrace(); } return null; } /** * 回写通知告警下选择的设备 */ public void getMsgEqu(MsgInfoBean msgInfoById ,List mdEquipments){ //获取告警信息下已保存的设备id String eid = msgInfoById.getEid(); //保存的是设备id 回写的是name,根据设备id查询设备的name if(!StringUtils.isEmpty(eid)){ String[] split = eid.split(","); StringBuilder sb = new StringBuilder(); for (int i = 0 ; i < split.length;i++){ for (int j = 0 ; j getWctOnlineEqu() { //wct所有在线设备 List root = new ArrayList<>(); try { Map clients = WebSocketSessionUtils.getInstance().getClients(); if(clients!=null){ //wct配置 用户名下对应的设备 Map> wctEquMap = BaseParams.getWctEquMap(); //需要判断client是属于pms 还是wct。 wct的客户端存在的登录名,pms客户端存的是id for (Map.Entry entry : clients.entrySet()) { //判断是否wct登录的客户端 if(!entry.getKey().startsWith("das")) continue; List mdEquipments = wctEquMap.get(entry.getKey()); if(mdEquipments==null||mdEquipments.size()<1) continue; //判断设备类型 for (int i= 0 ;i param) { try { //需要推送的设备,用逗号拼接 String eid = (String) param.get("eid"); //需要推送的消息 MsgInfo msg = (MsgInfo) param.get("msg"); //推送的类型 0-pms和wct 1-pms 2-wct Long type = (Long) param.get("type"); //设置消息标记,方便js中解析 msg.setApproveContent("PMS"); String[] split = eid.split(","); if (type == 0) { pushToWct(split, msg); pushToPms(msg); } else if (type == 1) { pushToPms(msg); } else if (type == 2) { pushToWct(split, msg); } }catch (Exception e){ e.printStackTrace(); return false; } return true; } @Override public void batchDeleteMsgInfo(String ids) { String[] split = ids.split(","); for (int i = 0; i < split.length; i++) { msgInfoDao.findById(MsgInfo.class, split[i]).setDel(1L); } } /** * 将通知推送到wct * @param split 推送的机台code * @param msg 推送消息的json */ private void pushToWct(String[] split, MsgInfo msg ){ //需要推送的机台 通过设备code找出wct的登录名 Map map = new HashMap<>(); Map> wctEquMap = BaseParams.getWctEquMap(); for (int i = 0;i> entry : wctEquMap.entrySet()) { if(entry.getValue()==null) continue; for (int j =0;j entry : map.entrySet()) { String key = entry.getKey(); Map clients = WebSocketSessionUtils.getInstance().getClients(); if(clients.containsKey(key)){ WebSocketMessage webSocketMessage = new WebSocketMessage(); webSocketMessage.setTitle(msg.getTitle()); webSocketMessage.setContent(msg.getContent()); webSocketMessage.setType("0"); webSocketMessage.setService("pms"); Gson g = new Gson(); String s = g.toJson(webSocketMessage); TextMessage returnMessage = new TextMessage(s); if(key.startsWith("das")||key.startsWith("admin")) WebSocketSessionUtils.getInstance().sendMessageToTarget(key,returnMessage); } } } } /** * 推送消息到pms客户端 * @param msg 推送的消息json */ private void pushToPms(MsgInfo msg ){ WebSocketMessage webSocketMessage = new WebSocketMessage(); webSocketMessage.setTitle(msg.getTitle()); webSocketMessage.setContent(msg.getContent()); webSocketMessage.setType("0"); webSocketMessage.setService("pms"); Gson g = new Gson(); String s = g.toJson(webSocketMessage); TextMessage returnMessage = new TextMessage(s); Map clients = WebSocketSessionUtils.getInstance().getClients(); for (Map.Entry entry : clients.entrySet()) { //wct 存储的是用户登录的winUname pms存储的是登录的id if(!entry.getKey().startsWith("das")&&!entry.getKey().startsWith("admin")){ WebSocketSessionUtils.getInstance().sendMessageToTarget(entry.getKey(),returnMessage); } } } }