zhuguifei
2026-03-10 58402bd5e762361363a0f7d7907153c77dbb819f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
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<MsgInfo> infos = msgInfoDao.queryByPage(hql.concat(params), pageParams);
        String sqlHql = "select count(*) " + hql;
        long total = msgInfoDao.queryTotal(sqlHql.concat(params));
//        List<MsgInfo>  infos2=msgInfoDao.query(hql);
        List<MsgInfoBean> infoBeans=new ArrayList<MsgInfoBean>();
        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<RoleBean> roleBeans = sessionInfo.getRoles();
        List<String> roleNames = new ArrayList<String>();
        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<MsgInfoBean> 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<MsgInfo> msgInfos = msgInfoDao.queryByPage(hql, 1, 5, new Object[]{});
        List<MsgInfoBean> beans = beanConvertor.copyList(msgInfos, MsgInfoBean.class);
        return beans;
    }
 
    @Override
    public List<MsgInfoBean> getMsgInfoApprove(String id) throws Exception {
        String hql = "from MsgInfo o where 1=1 and o.del=0 and o.msgInfo.id='" + id + "'";
        List<MsgInfo> infos = msgInfoDao.query(hql, new Object[]{});
        List<MsgInfoBean> beans = new ArrayList<MsgInfoBean>();
        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<MdEquipment> 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<MdEquipment> listMe=new ArrayList<MdEquipment>();
                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<MdEquipment> getEquInfo(UserBean userBean) {
        try {
            List<MdEquipment> equList=new ArrayList<MdEquipment>();
            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<MdEquipment> 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<mdEquipments.size();j++){
                    MdEquipment mdEquipment = mdEquipments.get(j);
                    if(split[i].equals(mdEquipment.getAttr2())){
                        sb.append(mdEquipment.getEquipmentName()+",");
                        mdEquipments.get(j).setAttr1("select"); //设置选择标记
                    }
                }
            }
            //编辑状态下回写的设备名
            msgInfoById.setIssuerName(sb.toString());
        }
    }
 
    @Override
    public List<MdEquipment> getWctOnlineEqu() {
        //wct所有在线设备
        List<MdEquipment>  root = new ArrayList<>();
        try {
            Map<String, WebSocketSession> clients = WebSocketSessionUtils.getInstance().getClients();
            if(clients!=null){
                //wct配置 用户名下对应的设备
                Map<String, List<MdEquipment>> wctEquMap = BaseParams.getWctEquMap();
                //需要判断client是属于pms 还是wct。 wct的客户端存在的登录名,pms客户端存的是id
                for (Map.Entry<String, WebSocketSession> entry : clients.entrySet()) {
                    //判断是否wct登录的客户端
                    if(!entry.getKey().startsWith("das")) continue;
 
                    List<MdEquipment> mdEquipments = wctEquMap.get(entry.getKey());
                    if(mdEquipments==null||mdEquipments.size()<1) continue;
                    //判断设备类型
                    for (int i= 0 ;i<mdEquipments.size();i++){
                        String substring = mdEquipments.get(i).getEquipmentCode().substring(0, 1);
                        if(Integer.parseInt(substring)<7){
                            MdEquipment mdEquipment = mdEquipments.get(i);
                            mdEquipment.setId(mdEquipment.getEquipmentCode());
                            //设置设备在wct的登录名  方便推送
                            mdEquipment.setAttr1(entry.getKey());
                            root.add(mdEquipment);
                        }
                    }
 
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return root;
    }
 
    @LogAnno(operateType = "推送消息到wct或pms")
    @Override
    public boolean pushToWctOrPms(Map<String, Object> 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<String,Object> map = new HashMap<>();
        Map<String, List<MdEquipment>> wctEquMap = BaseParams.getWctEquMap();
        for (int i = 0;i<split.length;i++){
            for (Map.Entry<String, List<MdEquipment>> entry : wctEquMap.entrySet()) {
                if(entry.getValue()==null) continue;
                for (int j =0;j<entry.getValue().size();j++){
                    if(split[i].equals(entry.getValue().get(j).getEquipmentCode())){
                        map.put(entry.getValue().get(j).getAttr1(),"");
                    }
                }
 
            }
 
        }
        //推送到wct
        if(!map.isEmpty()){
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                String key = entry.getKey();
                Map<String, WebSocketSession> 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<String, WebSocketSession> clients = WebSocketSessionUtils.getInstance().getClients();
        for (Map.Entry<String, WebSocketSession> entry : clients.entrySet()) {
            //wct 存储的是用户登录的winUname    pms存储的是登录的id
            if(!entry.getKey().startsWith("das")&&!entry.getKey().startsWith("admin")){
                WebSocketSessionUtils.getInstance().sendMessageToTarget(entry.getKey(),returnMessage);
               }
            }
 
        }
 
 
 
}