package com.product.data.center.service; import java.util.Date; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.alibaba.fastjson.JSONObject; import com.product.core.dao.BaseDao; import com.product.core.entity.DataTableEntity; import com.product.core.entity.FieldSetEntity; import com.product.core.service.support.AbstractBaseService; import com.product.core.websocket.service.WebsocketMesssageServiceThread; import com.product.data.center.config.CmnConst; import com.product.util.BaseUtil; import cn.hutool.core.date.DateField; import cn.hutool.core.date.DateUtil; @Service("autoSendErrorLogMassage") public class SendMessageTaskService extends AbstractBaseService{ @Autowired BaseDao baseDao; /** * 获取周期内产生错误的日志信息(日志uuid,操作类型,操作表,操作时间区间) * 同操作、同表、同期内只取一次记录 * @param period 周期(分钟) */ public void getAllErrorLog(Integer period) { Date currentDate=new Date(); StringBuilder sb = new StringBuilder(256); sb.append(" SELECT ") .append(" B.config_uuid,B.type,B.uuid,B.end_time,B.start_time,A.source_table ") .append(" FROM( ") .append(" SELECT ") .append(" config_uuid,type,uuid, ") .append(" DATE_FORMAT(Max(statistics_final_time),'%Y-%m-%d %H:%i:%s') end_time, ") .append(" DATE_FORMAT(Max(statistics_start_time),'%Y-%m-%d %H:%i:%s') start_time ") .append(" FROM product_sys_data_center_log ") .append(" WHERE result=0 AND (deal_flag=0 OR deal_flag IS NULL) ") .append(" AND created_utc_datetime>date_add(now(),interval -").append(period).append(" minute) AND created_utc_datetime<=now() ") .append(" GROUP BY config_uuid ") .append(" )B ") .append(" LEFT JOIN product_sys_data_center_config_view A ON a.uuid = B.config_uuid"); DataTableEntity dtErrorLog = baseDao.listTable(sb.toString(), new Object[] {}); if(!BaseUtil.dataTableIsEmpty(dtErrorLog)) { JSONObject allType = getAllOpreaType(); // 发送给所有的管理员 DataTableEntity managerDte = baseDao.listTable("product_sys_org_manager", "is_used=1"); StringBuilder receiver = new StringBuilder(32); FieldSetEntity managerFse; for (int i = 0; i < managerDte.getRows(); i++) { managerFse = managerDte.getFieldSetEntity(i); if (receiver.length() > 0) { receiver.append(","); } receiver.append(managerFse.getString("user_id")); } for (int i = 0; i < dtErrorLog.getRows(); i++) { createdAndSendMassage(dtErrorLog.getFieldSetEntity(i), allType, receiver.toString()); } } } /** * 通过错误日志信息,生成消息 * @param fseErrorLog * @param allType * @param receiver */ public void createdAndSendMassage(FieldSetEntity fseErrorLog, JSONObject allType, String receiver) { //生成标题和内容 StringBuilder massageTitle = new StringBuilder(64); StringBuilder massageContent = new StringBuilder(64); String sourceTable=fseErrorLog.getString(CmnConst.SOURCE_TABLE); if (!BaseUtil.strIsNull(sourceTable)) { massageTitle.append("《").append(sourceTable).append("表"); massageContent.append(sourceTable).append("表"); } String type = fseErrorLog.getString(CmnConst.TYPE); if (!BaseUtil.strIsNull(type)) { massageTitle.append(allType.get(type)).append("出错").append("》"); massageContent.append(allType.get(type)).append("出错"); } String startTime = fseErrorLog.getString(CmnConst.START_TIME); String endTime = fseErrorLog.getString(CmnConst.END_TIME); if (!BaseUtil.strIsNull(startTime) && !BaseUtil.strIsNull(endTime)) { massageContent.append("任务于") .append(startTime) .append("~") .append(endTime) .append("期间出现报错,请及时查看并处理!"); }else { massageContent.append("任务出现报错,请及时查看并处理!"); } String uuid = fseErrorLog.getString(CmnConst.UUID); if(!BaseUtil.strIsNull(uuid)) { WebsocketMesssageServiceThread.getInstance().appendMessage(receiver, massageContent.toString(), massageTitle.toString(), 1, "1", "166269029153939a5ln9629?uuid="+uuid+"&config_uuid="+fseErrorLog.getString(CmnConst.CONFIG_UUID)+"&type="+type , CmnConst.PRODUCT_SYS_DATA_CENTER_LOG, uuid, 1, 0, 0); } } /** * 获取所有操作类型 * @return */ public JSONObject getAllOpreaType() { DataTableEntity dtAllType = baseDao.listTable(CmnConst.PRODUCT_SYS_DICT, " dict_name='journal_type' "); if (!BaseUtil.dataTableIsEmpty(dtAllType)) { JSONObject json = new JSONObject(); for (int i = 0; i < dtAllType.getRows(); i++) { FieldSetEntity fseType = dtAllType.getFieldSetEntity(i); json.put(fseType.getString(CmnConst.DICT_VALUE), fseType.getString(CmnConst.DICT_LABEL)); } return json; } return null; } }