| | |
| | | package com.product.administration.service; |
| | | |
| | | import java.lang.reflect.InvocationTargetException; |
| | | import java.lang.reflect.Method; |
| | | import java.util.*; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import cn.hutool.core.collection.CollectionUtil; |
| | | import cn.hutool.cron.CronUtil; |
| | | import com.google.common.collect.Sets; |
| | | import com.product.administration.config.SystemCode; |
| | | import com.product.common.collect.SetUtils; |
| | | import com.product.quartz.util.CronUtils; |
| | | import com.product.util.SystemParamReplace; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.context.ApplicationContext; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import com.product.administration.config.CmnConst; |
| | |
| | | |
| | | @Autowired |
| | | BaseDao baseDao; |
| | | |
| | | @Autowired |
| | | private ApplicationContext applicationContext; |
| | | |
| | | @Autowired |
| | | FlowService flowService; |
| | |
| | | SpringMVCContextHolder.getSystemLogger().error(e); |
| | | } |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | String content = "设备 奥的斯 ADS-00001 ,参数 电压 达到 【参数值】 ,形成 二级 故障 "; |
| | | String replacedContent = content.replace("【参数值】", "2.23"); |
| | | System.out.println(replacedContent); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 单个预警,特殊预警已经有预警的数据了,别的业务主动调此方法,去发送预警消息 |
| | | * 特殊预警 |
| | | * 预警数据来源于其它业务(bean调用该方法),验证条件来源于其它业务(bean调用) |
| | | * 数据替换依然来源于配置表 |
| | | * |
| | | * @param configUuid |
| | | * @param dtService 预警的业务数据,关联设备数据 |
| | | */ |
| | | public void warningCreate(String configUuid,DataTableEntity dtService) { |
| | | public void warningCreate(String configUuid, DataTableEntity dtServiceData, Map<String, String> mapFields) { |
| | | |
| | | FieldSetEntity fseWarningConfig = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_EARLY_WARNING, configUuid, false); |
| | | if (!"1".equals(fseWarningConfig.getString(CmnConst.CONFIG_STATUS))) { |
| | | throw new BaseException(SystemCode.WARN_NO_NORMAL_STATUS_RECORD); |
| | | } |
| | | String mainTableName = fseWarningConfig.getString(CmnConst.SOURCE_TABLE); //主表名 |
| | | String mainTableAlias = mainTableName + "1"; //主表别名 |
| | | // |
| | | // String warnCondition = fseWarningConfig.getString(CmnConst.CONDITION_EXPRESSION_HIDE); //预警表达式 |
| | | |
| | | String warnContent = fseWarningConfig.getString(CmnConst.WARNING_CONTENT_HIDE); //提醒内容 |
| | | String warnMethod = fseWarningConfig.getString(CmnConst.WARNING_METHOD); //预警方式 |
| | | // |
| | | |
| | | String serviceReceiver = fseWarningConfig.getString(CmnConst.BUSINESS_RELATED_PERSON); //业务相关人员(表名.字段名) |
| | | String fixedReceiver = fseWarningConfig.getString(CmnConst.FIXED_RECEIVER); //固定接收人 |
| | | String flowReceiver = fseWarningConfig.getString(CmnConst.FLOW_RELATED_PERSON); //流程相关人 |
| | | // |
| | | String flowFixedUser = fseWarningConfig.getString(CmnConst.FLOW_FIX_USER); //流程固定发起人 |
| | | String flowBusinessUser = fseWarningConfig.getString(CmnConst.FLOW_BUSINESS_USER); //流程动态发起人 |
| | | // |
| | | String flowCode = fseWarningConfig.getString(CmnConst.FLOW_CODE); //流程type_code |
| | | String flowParam = fseWarningConfig.getString(CmnConst.FLOW_PARAM); //流程参数转换 |
| | | // |
| | | // //获取发送消息频率 |
| | | Double cronExpression = fseWarningConfig.getDouble("reminder_frequency"); |
| | | // |
| | | // //发送消息的频率 1.23 小数点前面代表天 小数点后面代表小时 |
| | | double seconds = 0; |
| | | if (cronExpression != null && cronExpression > 0) { |
| | | |
| | | //将发送消息频率转换为小时 |
| | | double hours = cronExpression * 24; |
| | | //将发送消息频率转换为分钟 |
| | | double minutes = hours * 60; |
| | | //将发送消息频率转换为秒 |
| | | seconds = minutes * 60; |
| | | } |
| | | // |
| | | // |
| | | |
| | | Map<String, String> tableNameAndAlias = new HashMap<>(); //相关表名-->表别名,tableName-->tableName1 |
| | | Map<String, String> fieldAndAlias = new HashMap<>(); //相关字段-->字段别名,tableName.fieldName-->tableName1fieldName |
| | | Map<String, String> fieldReference = new HashMap<>(); //组合相关字段的参照,tableName1fieldName-->referenceName |
| | | StringBuilder concatField = new StringBuilder(); //组合相关字段,tableName1.fieldName tableName1fieldName |
| | | // |
| | | // //解析业务相关人员 |
| | | //解析业务相关人员 |
| | | if (!StringUtils.isEmpty(serviceReceiver)) { |
| | | parseWarningContent(serviceReceiver, concatField, tableNameAndAlias, fieldAndAlias, fieldReference); |
| | | } |
| | | // |
| | | //解析预警内容 |
| | | if (!StringUtils.isEmpty(warnContent)) { |
| | | parseWarningContent(warnContent, concatField, tableNameAndAlias, fieldAndAlias, fieldReference); |
| | | } |
| | | // |
| | | // //解析预警条件 |
| | | // if (!StringUtils.isEmpty(warnCondition)) { |
| | | // parseWarningContent(warnCondition, concatField, tableNameAndAlias, fieldAndAlias, fieldReference); |
| | | // } |
| | | // |
| | | //解析流程参数 |
| | | if (!StringUtils.isEmpty(flowParam)) { |
| | | parseWarningContent(flowParam, concatField, tableNameAndAlias, fieldAndAlias, fieldReference); |
| | | } |
| | | |
| | | //解析流程发起人 |
| | | if (StringUtils.isEmpty(flowFixedUser) && !StringUtils.isEmpty(flowBusinessUser)) { |
| | | // parseWarningContent(flowBusinessUser,concatField,tableNameAndAlias,fieldAndAlias,fieldReference); |
| | | } |
| | | // |
| | | // //生成主子表关联SQL |
| | | // String relationSQL = createTableRelation(tableNameAndAlias, mainTableName); |
| | | // |
| | | // StringBuilder serviceSQL = new StringBuilder(); |
| | | // serviceSQL.append(" SELECT "); |
| | | // serviceSQL.append(concatField.subSequence(1, concatField.length())); |
| | | // serviceSQL.append(" FROM "); |
| | | // serviceSQL.append(relationSQL); |
| | | // serviceSQL.append(" WHERE "); |
| | | // serviceSQL.append(parseWarnCondition(warnCondition, tableNameAndAlias)); |
| | | // |
| | | // //查询符合预警的数据 |
| | | // DataTableEntity dtService = baseDao.listTable(serviceSQL.toString(), new Object[]{}); |
| | | |
| | | ///----------------设预警检测的bean方法解发验证是否解发预警,返回true,即发生预警------------------ |
| | | boolean b=false; |
| | | DataTableEntity dtParmFault = null; |
| | | String beanAndMehtod = fseWarningConfig.getString("bean"); |
| | | //-----反射调bean,要改 |
| | | //b=xxxxx.month(dtService); |
| | | ///----------------设预警检测的bean方法解发验证是否解发预警,返回true,即发生预警------------------ |
| | | if (b) { |
| | | List<String> collect = dtService.getData().stream().map(item -> item.getString(mainTableAlias + "uuid")).collect(Collectors.toList()); |
| | | //预警uuid |
| | | String warnUUID = fseWarningConfig.getUUID(); |
| | | //将预警uuid放到collect 的第一个 |
| | | |
| | | collect.add(0, warnUUID); |
| | | Object bean = applicationContext.getBean(beanAndMehtod.split("\\.")[0]); |
| | | try { |
| | | Method method = bean.getClass().getMethod(beanAndMehtod.split("\\.")[1], DataTableEntity.class, Map.class); |
| | | dtParmFault = (DataTableEntity)method.invoke(bean, dtServiceData, mapFields); |
| | | } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { |
| | | e.printStackTrace(); |
| | | } catch (NoSuchMethodException e) { |
| | | e.printStackTrace(); |
| | | } catch (SecurityException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | if (!BaseUtil.dataTableIsEmpty(dtParmFault)) { |
| | | Map<String, String> mapUserWarnMessage = new HashMap<>(); |
| | | for (int j = 0; j < dtParmFault.getRows(); j++) { |
| | | FieldSetEntity fseParamFault = dtParmFault.getFieldSetEntity(j); |
| | | StringBuilder sql = new StringBuilder(); |
| | | //查询是否已经存在预警如有相同data_uuid的数据查询出最近的一条根据预警时间字段 early_warning_datetime |
| | | // SELECT e.* |
| | | // FROM product_sys_early_warning_info e |
| | | // JOIN ( |
| | | // SELECT data_uuid, MAX(early_warning_datetime) AS latest_datetime |
| | | // FROM product_sys_early_warning_info |
| | | // GROUP BY data_uuid |
| | | // ) AS latest |
| | | // ON e.data_uuid = latest.data_uuid AND e.early_warning_datetime = latest.latest_datetime; |
| | | sql.append("SELECT e.* FROM product_sys_early_warning_info e JOIN ("); |
| | | sql.append("SELECT data_uuid, MAX(early_warning_datetime) AS latest_datetime FROM product_sys_early_warning_info "); |
| | | sql.append(" where early_warning_uuid=? and (").append(BaseUtil.buildQuestionMarkFilter("data_uuid", collect.size(), true)).append(") "); |
| | | sql.append("GROUP BY data_uuid) AS latest ON e.data_uuid = latest.data_uuid AND e.early_warning_datetime = latest.latest_datetime"); |
| | | DataTableEntity dt = baseDao.listTable(sql.toString(), collect.toArray()); |
| | | |
| | | Map<String, FieldSetEntity> ff = new HashMap<>(); |
| | | if (!DataTableEntity.isEmpty(dt)) { |
| | | for (int j = 0; j < dt.getRows(); j++) { |
| | | FieldSetEntity fse = dt.getFieldSetEntity(j); |
| | | ff.put(fse.getString("data_uuid"), fse); |
| | | } |
| | | } |
| | | |
| | | //以data_uuid 进行分组 |
| | | for (int j = 0; j < dtService.getRows(); j++) { |
| | | FieldSetEntity fseService = dtService.getFieldSetEntity(j); |
| | | |
| | | //业务uuid |
| | | String serviceId = fseService.getString(mainTableAlias + "uuid"); |
| | | |
| | | if (ff.containsKey(serviceId) && StringUtils.isEmpty(cronExpression)) { |
| | | //如果已经存在预警信息 根据cron表达式判断是否需要再次预警 |
| | | Date preTime = ff.get(serviceId).getDate("early_warning_datetime"); |
| | | if (preTime != null) { |
| | | long time = new Date().getTime() - preTime.getTime(); |
| | | if (time < seconds * 1000) { |
| | | continue; |
| | | } |
| | | } |
| | | } |
| | | sql.append("SELECT ").append(concatField.subSequence(1, concatField.length())).append(" \n"); |
| | | sql.append("FROM ").append(mainTableName).append(" " + tableNameAndAlias.get(mainTableName)).append(" \n"); |
| | | sql.append("WHERE ").append("device_number = ? AND param_config_uuid = ? AND uuid = ?"); |
| | | // 获取视图数据 |
| | | FieldSetEntity fseService = baseDao.getFieldSetEntityBySQL(sql.toString(), |
| | | new Object[] {fseParamFault.getString(mapFields.get("device_code_field")), fseParamFault.getString(mapFields.get("param_type_field")), fseParamFault.getString("fault_grade_uuid")}, false); |
| | | |
| | | //信息接收人 |
| | | Set<String> receiverSet = Sets.newTreeSet(); |
| | |
| | | |
| | | //装载固定接收人 |
| | | if (!StringUtils.isEmpty(fixedReceiver)) { |
| | | FieldSetEntity fseOrgnaiztion = baseDao.getFieldSetEntity("product_sys_organizational_structure_storage", fixedReceiver, false); |
| | | if (!StringUtils.isEmpty(fseOrgnaiztion.getString("staff_ids"))) { |
| | | String[] splitReceiver = fseOrgnaiztion.getString("staff_ids").split(","); |
| | | String[] splitReceiver = fixedReceiver.split(","); |
| | | for (int k = 0; k < splitReceiver.length; k++) { |
| | | FieldSetEntity fseStaff = baseDao.getFieldSetEntityByFilter("product_sys_staffs", "staff_id=?", new Object[]{splitReceiver[k]}, false); |
| | | FieldSetEntity fseStaff = baseDao.getFieldSetEntityByFilter("product_sys_users", "user_id=?", new Object[]{splitReceiver[k]}, false); |
| | | receiverSet.add(fseStaff.getString(CmnConst.USER_ID)); |
| | | } |
| | | } |
| | | } |
| | | |
| | | //装载流程相关人 |
| | | if (!StringUtils.isEmpty(flowReceiver)) { |
| | | |
| | | } |
| | | |
| | | //暂时替换提醒内容 |
| | | warnContent = SystemParamReplace.replaceParams(warnContent, fseService); |
| | | String content = getContentByTableSource(fseService, warnContent, fieldReference); |
| | | |
| | | String allReceiver = SetUtils.set2String(receiverSet, ","); |
| | | content = content.replace("【参数值】", fseParamFault.getString(fseParamFault.getString("collect_value_field"))); |
| | | System.out.println(content); |
| | | System.out.println(fseParamFault.getString(fseParamFault.getString("collect_value_field"))); |
| | | System.out.println(content.replace("【参数值】", fseParamFault.getString(fseParamFault.getString("collect_value_field")))); |
| | | //遍历发送人,及发送内容 |
| | | Iterator<String> iterator = receiverSet.iterator(); |
| | | while (iterator.hasNext()) { |
| | | String fruit = iterator.next(); |
| | | if (mapUserWarnMessage.containsKey(fruit)) { |
| | | String allContent = mapUserWarnMessage.get(fruit); |
| | | allContent += "\n"+ content; |
| | | mapUserWarnMessage.put(fruit, allContent); |
| | | } else { |
| | | mapUserWarnMessage.put(fruit, content); |
| | | } |
| | | } |
| | | } |
| | | if (!mapUserWarnMessage.isEmpty()) { |
| | | for (Map.Entry<String, String> entry : mapUserWarnMessage.entrySet()) { |
| | | String allReceiver = entry.getKey(); |
| | | String content = entry.getValue(); |
| | | //自动生成预警日志信息 |
| | | 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, warnUUID); |
| | | fse.setValue(CmnConst.DATA_UUID, serviceId); |
| | | fse.setValue(CmnConst.WARNING_CONTENT, content); |
| | | fse.setValue(CmnConst.RECEIVER, allReceiver); |
| | | fse.setValue(CmnConst.WARNING_METHOD, warnMethod); |
| | | baseDao.add(fse); |
| | | FieldSetEntity fseWarnLog = new FieldSetEntity(CmnConst.PRODUCT_SYS_EARLY_WARNING_INFO); |
| | | fseWarnLog.setValue(CmnConst.EARLY_WARNING_DATETIME, new Date()); |
| | | fseWarnLog.setValue(CmnConst.EARLY_WARNING_UUID, configUuid); |
| | | // fse.setValue(CmnConst.DATA_UUID, serviceId); |
| | | fseWarnLog.setValue(CmnConst.WARNING_CONTENT, content); |
| | | fseWarnLog.setValue(CmnConst.RECEIVER, allReceiver); |
| | | fseWarnLog.setValue(CmnConst.WARNING_METHOD, warnMethod); |
| | | baseDao.add(fseWarnLog); |
| | | |
| | | //发送信息 |
| | | if (!StringUtils.isEmpty(warnMethod)) { |
| | | String[] send_method = warnMethod.split(","); |
| | | for (int k = 0; k < send_method.length; k++) { |
| | | if ("1".equals(send_method[k])) { //系统消息 |
| | | WebsocketMesssageServiceThread.getInstance().appendMessage(allReceiver, content, "预警消息", 1, "31", CmnConst.BUTTON_URL_WARN_MESSAGE_INFO + "?uuid=" + fse.getUUID(), CmnConst.PRODUCT_SYS_EARLY_WARNING_INFO, fse.getUUID(), 1, 0, 0); |
| | | WebsocketMesssageServiceThread.getInstance().appendMessage(allReceiver, content, "预警消息", 1, "31", CmnConst.BUTTON_URL_WARN_MESSAGE_INFO + "?uuid=" + fseWarnLog.getUUID(), CmnConst.PRODUCT_SYS_EARLY_WARNING_INFO, fseWarnLog.getUUID(), 1, 0, 0); |
| | | } else if ("2".equals(send_method[k])) { //电子邮件 |
| | | parseMail(content, allReceiver); |
| | | } else if ("3".equals(send_method[k])) { //短信消息 |
| | |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | //发起流程 |
| | | if (!StringUtils.isEmpty(flowFixedUser)) { |
| | | autoStartFlow(fseWarningConfig.getString("config_name"), flowCode, flowParam, fseService, fieldAndAlias, Integer.valueOf(flowFixedUser)); |
| | | } else { |
| | | if (!StringUtils.isEmpty(flowBusinessUser)) { |
| | | autoStartFlow(fseWarningConfig.getString("config_name"), flowCode, flowParam, fseService, fieldAndAlias, fseService.getInteger(flowBusinessUser)); |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | | } |
| | | /** |