zm
2024-10-23 70a6a22f94446bc9a1b96fc52584898aa7f67543
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
package com.product.administration.service;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import com.product.admin.util.SystemParamReplace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
 
import com.product.administration.config.CmnConst;
import com.product.common.lang.StringUtils;
import com.product.core.dao.BaseDao;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.core.service.support.AbstractBaseService;
import com.product.core.websocket.service.WebsocketMesssageService;
import com.product.core.websocket.service.WebsocketMesssageServiceThread;
import com.product.util.BaseUtil;
 
@Component
public class EarlyWarningService extends AbstractBaseService{
 
    @Autowired
    BaseDao baseDao;
    
    @Autowired
    WebsocketMesssageService websocketMesssageService;
    
    /**
     *     发送预警消息
     */
    public void sendWarningInfo() {
 
        //获取所有预警配置
        DataTableEntity dtWarningConfig = baseDao.listTable(CmnConst.PRODUCT_SYS_EARLY_WARNING, "config_status=1");
        if (!BaseUtil.dataTableIsEmpty(dtWarningConfig)) {
            //遍历预警配置
            for (int i = 0; i < dtWarningConfig.getRows(); i++) {
                FieldSetEntity fseWarningConfig=dtWarningConfig.getFieldSetEntity(i);
                
                String tableName=fseWarningConfig.getString(CmnConst.SOURCE_TABLE);            //表名
                String exp=fseWarningConfig.getString(CmnConst.CONDITION_EXPRESSION_HIDE);    //预警表达式
                String content=fseWarningConfig.getString(CmnConst.WARNING_CONTENT_HIDE);    //提醒内容
                
                String serviceStaff1=fseWarningConfig.getString(CmnConst.BUSINESS_RELATED_PERSON);    //业务相关人员(表名.字段名)
                String fixedReceiver=fseWarningConfig.getString(CmnConst.FIXED_RECEIVER);        //固定接收人
                String flowReceiver=fseWarningConfig.getString(CmnConst.FLOW_RELATED_PERSON);    //流程相关人
                
                String concatField="";            //组合相关字段
                Map<String,String> concatFieldReference=new HashMap<>();    //组合相关字段的参照
                
                if (!StringUtils.isEmpty(serviceStaff1)) {
                    concatField+=serviceStaff1;
                }
                
                //解析预警内容
                if (!StringUtils.isEmpty(content)) {
                    concatField+=","+parseWarningContent(content, concatField, concatFieldReference);
                }
                
                //解析查询字段(field_name field_alias)
                concatField = resultField(concatField);
                
                //生成组装表关联sql与查询字段
                String [] fromStr=getSQLRationForTables(tableName);
                String relationTableSql=fromStr[0];        //获取关联sql
                String allField=fromStr[1];                //获取查询字段
                String expv=getExpValue(exp);
                
                //根据配置数据查询符合条件的业务数据
                StringBuilder sb=new StringBuilder();
                sb.append(" SELECT ");
                sb.append(allField);
                sb.append(",");
                sb.append(concatField);
                if (!StringUtils.isEmpty(expv)) {
                    sb.append(",");
                    sb.append(expv);
                }
                sb.append(" FROM ");
                sb.append(relationTableSql);
                sb.append(" WHERE ");
                sb.append(exp);
                
                //根据配置数据查出符合条件的业务数据
                
                DataTableEntity dtService=baseDao.listTable(sb.toString(), new Object[] {});
                baseDao.loadPromptData(dtService);
                
                if (!BaseUtil.dataTableIsEmpty(dtService)) {
                    for (int j = 0; j < dtService.getRows(); j++) {
                        //获取单条业务数据
                        FieldSetEntity fseService=dtService.getFieldSetEntity(j);
                        
                        String warningUUID=fseWarningConfig.getUUID();    //预警配置UUID
                        String dataUUID=fseService.getUUID();            //业务数据UUID
                        
                        //查询当前业务数据是否已存在日志
                        DataTableEntity dtWarningInfo=baseDao.listTable(CmnConst.PRODUCT_SYS_EARLY_WARNING_INFO, "early_warning_uuid=? AND data_uuid=?", new Object[] {warningUUID,dataUUID});
                        if (!BaseUtil.dataTableIsEmpty(dtWarningInfo)) {
                            continue;
                        }
                        
                        String receiver="";
                        
                        String receiver1=fseService.getString(serviceStaff1.replace(".", ""));    //业务相关接收人1
//                        String receiver4=fseService.getString(CmnConst.FLOW_RELATED_PERSON_ONE);//审批节点人1
//                        String receiver5=fseService.getString(CmnConst.FLOW_RELATED_PERSON_TWO);//审批节点人2
                        
                        if (!StringUtils.isEmpty(receiver1)) {
                            receiver+=receiver1;
                        }
                        if (!StringUtils.isEmpty(fixedReceiver)) {
                            if (!StringUtils.isEmpty(receiver)) {
                                receiver+=",";
                            }
                            receiver+=fixedReceiver;
                        }
//                        receiver[3]=receiver4;
//                        receiver[4]=receiver5;
                        
                        
                        //暂时替换提醒内容
                        content=getContentByTableSource(fseService, content, concatFieldReference);
                        
                        //自动生成预警日志信息
                        FieldSetEntity fse=new FieldSetEntity();
                        fse.setTableName(CmnConst.PRODUCT_SYS_EARLY_WARNING_INFO);
                        fse.setValue(CmnConst.EARLY_WARNING_DATETIME, new Date());
                        fse.setValue(CmnConst.EARLY_WARNING_UUID, warningUUID);
                        fse.setValue(CmnConst.DATA_UUID, dataUUID);
                        fse.setValue(CmnConst.WARNING_CONTENT, content);
                        fse.setValue(CmnConst.USER_ID, receiver);
//                        fse.setValue(CmnConst.SEND_METHOD, send_method);
                        
                        baseDao.add(fse);
 
                        String flow_uuid=fseWarningConfig.getString(CmnConst.FLOW_UUID);
                        if (!StringUtils.isEmpty(flow_uuid)) {    //发起流程
                            
                        } else {    //发送消息
//                            if (send_method==1) {    //系统内部消息推送
//                            for (int k = 0; k < receiver.length; k++) {
                                WebsocketMesssageServiceThread.getInstance().appendMessage(receiver,content,"预警消息",2,"2",null,null,null,1,0,0); 
//                            }
//                            }else if (send_method==2) {    //短信推送
                                
//                            }else if (send_method==3) {    //邮件推送
                                
//                            }
                        }
                    }
                }
            }
        }
    }
    
    /**
     *     查询字段别名设置  table_name.field_name-->table_namefield_name
     * @param concatField
     * @return
     */
    public String resultField(String concatField) {
        String [] fields = concatField.split(",");
        StringBuilder newConcatField=new StringBuilder();
        for (int i = 0; i < fields.length; i++) {
            String field=fields[i];
            String fieldAlias=field.replace(".", "");
            if (i==0) {
                newConcatField.append(field+" "+fieldAlias);
            }else {
                newConcatField.append(","+field+" "+fieldAlias);
            }
        }
        return newConcatField.toString();
    }
    
    /**
     *     查询当前表所有关联主表
     * @param currentTable    当前表
     * @return
     */
    public String[] getSQLRationForTables(String currentTable) {
        
        //获取当前表所有父表
        List<String []>tables=getTableAndParentOfMVC(currentTable);    //tables[0]:当前表   table[n]:关联表,外键,关联字段
        
        //生成多表关联SQL(左关联当前表)
        StringBuilder relationTableSql=new StringBuilder();
        //查询多表字段(表别名.*)
        StringBuilder allField=new StringBuilder();
        
        for (int i = 0; i < tables.size(); i++) {
            String[] tablen=tables.get(i);
            String tableName=tablen[0];            //获取表名  tableName
            String tableAlias=tableName+"1";    //获取表别名 tableName1
            allField.append(tableAlias+".uuid AS uuid"+i);    //拼接每个表的UUID
            if (i==0) {
                relationTableSql.append(tableName+" AS ");
                relationTableSql.append(tableAlias);
            }else {
                relationTableSql.append(" LEFT JOIN ");
                relationTableSql.append(tableName+" AS ");
                relationTableSql.append(tableAlias);
                relationTableSql.append(" ON ");
                relationTableSql.append(tableAlias+"."+tablen[1]+"="+currentTable+"1."+tablen[2]);    //关联条件
            }
        }
        return new String[] {relationTableSql.toString(),allField.toString()};
    }
    
    /**
     *     获取所有关联主表
     *     tables[0]:当前表
     *     tables[n]:关联表名,外键字段(uuid),关联字段
     * @param currentTable    当前表名称
     * @return
     */
    public List<String []> getTableAndParentOfMVC(String currentTable){
        
        List<String[]> tables=new ArrayList<>();
        String[] table1=new String[1];
        table1[0]=currentTable;
        
        tables.add(table1);
        
        //查询所有父表
        String sql="SELECT * FROM product_sys_datamodel_field WHERE (field_relation_table IS NOT NULL AND field_relation_table !='') AND table_uuid=?";
        DataTableEntity dtParentTable=baseDao.listTable(sql, new Object[] {currentTable});
        if (!BaseUtil.dataTableIsEmpty(dtParentTable)) {
            //遍历父表
            for (int i = 0; i < dtParentTable.getRows(); i++) {
                
                String parentTableName=dtParentTable.getString(i, CmnConst.FIELD_RELATION_TABLE);    //父表名称
                String parentTableField=CmnConst.UUID;    //父表外键字段
                String currentTableField=dtParentTable.getString(i, CmnConst.FIELD_NAME);    //当前表关联字段
                
                String [] tablen=new String[3];
                tablen[0]=parentTableName;
                tablen[1]=parentTableField;
                tablen[2]=currentTableField;
                tables.add(tablen);
            }
        }
        return tables;
    }
    
    /**
     *     根据提醒内容中的表属性,进行拼接成标准格式
     * @param content                预警内容
     * @param concatField            组合字段
     * @param concatFieldReference    组合字段对应参照
     * @return
     */
    public String parseWarningContent(String content, String concatField, Map<String, String> concatFieldReference) {
        int s = 0;
        String tableAndField=null;
        while (s >= 0) {
            int c = content.indexOf("{{", s);
            if (c == -1) {
                break;
            }
            int m = content.indexOf("}}", c);
            s = c + 2;
            tableAndField=content.substring(s, m);
            concatField+=","+tableAndField;
            concatFieldReference.put(tableAndField, getReferenceByField(tableAndField));
        }
        return concatField;
    }
    
    
    /**
     * 获取配置的条件表达式中第一对括号中的计算式生成查询值,通过此值,能作判断处理
     * 
     * @param exp
     * @return
     */
    public String getExpValue(String exp) {
        int s = exp.indexOf("(");
        int e = -1;
        if (s >= 0) {
            e = exp.indexOf(")", s);
        }
        if (s >= 0 && e > 0) {// 取第一对括号的计算式
            return exp.substring(s, e + 1);
        }
        return null;
    }
    
    /**
     *     根据表名和字段的获取对应参照
     * @param tableAndField    表别名.字段名
     * @return
     */
    public String getReferenceByField(String tableAndField) {
        String [] tableField=tableAndField.replace("1.", ".").split("\\.");
        String tableName=tableField[0];
        String fieldName=tableField[1];
        FieldSetEntity fseFieldInfo=baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_DATAMODEL_FIELD, "table_uuid=? and field_name=?", new Object[] {tableName,fieldName}, false);
        if (fseFieldInfo==null) {
            throw new BaseException("", "", this.getClass(), "");
        }
        String fieldType=fseFieldInfo.getString(CmnConst.FIELD_TYPE);
        if (CmnConst.USERID.equals(fieldType)) {
            return CmnConst.USERID;
        }else {
            return fseFieldInfo.getString(CmnConst.FIELD_REFERENCE);
        }
    }
    
    
    public String getContentByTableSource(FieldSetEntity fse,String content, Map<String, String> concatFieldReference) {
        if (content.indexOf("{{")==-1) {
            return content;
        }
        content=content.replace("1.", "1");
        return replaceParamText(content, fse, concatFieldReference);
    }
    
    /**
     * 替换文本参数
     * 
     * @param paramText 原文本
     * @param fseData   业务数据
     * @return 新文本
     * @throws BaseException
     */
    public String replaceParamText(String paramText, FieldSetEntity fseData, Map<String, String> concatFieldReference) throws BaseException {
        String str = paramText;
        // 系统参数{{参数名:sys}}
        Pattern p3 = Pattern.compile("(\\{\\{)([\\w]+)(:sys\\}\\})");
        // 普通业务参数{{参数名}}
        Pattern p4 = Pattern.compile("(\\{\\{)([\\w]+)(\\}\\})");
 
        StringBuffer sb1 = new StringBuffer();
        Matcher m = p3.matcher(str);
        while (m.find()) {
            String group = m.group(2);
            String replaceValue= SystemParamReplace.replaceSystemParameter(group);
            if(StringUtils.isEmpty(replaceValue)) {
                continue;
            }
            m.appendReplacement(sb1, replaceValue);
        }
        m.appendTail(sb1);
 
        StringBuffer sb2 = new StringBuffer();
        if (fseData != null) {
            m = p4.matcher(sb1);
            while (m.find()) {
                String group = m.group(2);
                String value = fseData.getString(group);
                if (null != value) {
                    if (concatFieldReference.get(group)==null) {
                        m.appendReplacement(sb2, value);
                    }else {
                        m.appendReplacement(sb2, parseReference(concatFieldReference.get(group),value,fseData));
                    }
                }
            }
            m.appendTail(sb2);
        }
        return sb2.toString();
    }
    
    /**
     *     参照替换
     * @param referenceName
     * @param originValue
     * @param fseData
     * @return
     */
    public String parseReference(String referenceName, String originValue, FieldSetEntity fseData) {
        if (CmnConst.USERID.equals(referenceName)) {    //人员
            FieldSetEntity fseUser=baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_USERS, "user_id=?", new Object[] {originValue}, false);
            if (fseUser==null) {
                throw new BaseException("", "", this.getClass(), "");
            }else {
                return fseUser.getString(CmnConst.USER_NAME);
            }
        }else if (referenceName.indexOf("》")>-1) {    //数据字典
            referenceName.replace("《", "").replace("》", "");
            FieldSetEntity fseDict=baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_DICT, "dict_name=? AND dict_value=?", new Object[] {referenceName, originValue}, false);
            if (fseDict==null) {
                throw new BaseException("", "", this.getClass(), "");
            }else {
                return fseDict.getString(CmnConst.DICT_LABEL);
            }
        }
        return originValue;
    }
}