本帖最后由 chen 于 2023-7-26 15:35 编辑
以下实现代码供参考
- importPackage(Packages.smartbi.sdk.service.spreadsheetreport);
- importPackage(Packages.smartbi.scheduletask.task);
- importPackage(Packages.smartbi.sdk.service.systemconfig);
- importPackage(Packages.java.lang);
- importPackage(Packages.java.util);
- importPackage(Packages.java.text);
- importPackage(Packages.java.io);
- importPackage(Packages.org.apache.commons.lang);
- importPackage(Packages.org.apache.commons.mail);
- importPackage(Packages.smartbi.scheduletask.component);
- importPackage(Packages.smartbi.util);
- importPackage(Packages.smartbi.freequery.querydata);
- importPackage(Packages.smartbi.sdk.service.datasource);
- var report = null;
- //定义email对象,初始化参数
- var systemConfigService = new SystemConfigService(connector);
- var configList = systemConfigService.getSystemConfigs();
- var dsService = new DataSourceService(connector);
- var mailServer = null;
- var fromAddress = null;
- var userName = null;
- var password = null;
- var encryptPassword = null;
- var emailSSLEnabled = null;
- var emailTLSEnabled = null;
- var mailAlias = null;
- var port = "";
- var ds = "DS.这里是数据库名";var configSql = "select mail_type,cus_id,cus_name,email,sql_param,outputparam1_type,outputparam1,outputparam2_type,outputparam2,ccemail from conf_email ";
- // 发送邮件
- var sendMail = function(userEmail, erji, jigou, qudao, qudaoparam, ccemail) {
- var multiPartEmail = new SmartbiMultiPartEmail();
- for (var i = 0; i < configList.size(); i++) {
- var config = configList.get(i);
- if (config!=null) {
- if (config.getKey().equals("EMAIL_SMTP_SERVER")) {
- mailServer = config.getValue();
- } else if (config.getKey().equals("EMAIL_USER_NAME")) {
- userName = config.getValue();
- } else if (config.getKey().equals("EMAIL_USER_ALIAS")){
- mailAlias = config.getValue();
- } else if (config.getKey().equals("EMAIL_USER_ADDRESS")) {
- fromAddress = config.getValue();
- } else if (config.getKey().equals("EMAIL_USER_PASSWORD")) {
- password= config.getValue();
- } else if (config.getKey().equals("EMAIL_USER_PASSWORD_ENCRYPT")) {
- encryptPassword = config.getValue();
- } else if (config.getKey().equals("EMAIL_SSL_ENABLED")) {
- if (config.getValue().equals("true")) {
- emailSSLEnabled = true;
- }
- } else if (config.getKey().equals("EMAIL_TLS_ENABLED")) {
- if (config.getValue().equals("true")) {
- emailTLSEnabled = true;
- }
- }else if (config.getKey().equals("EMAIL_SMTP_PORT")) { //端口
- port = config.getValue().trim();
- }
- }
- }
- if (StringUtil.equals(encryptPassword, "true")) {
- password = AESCryption.decrypt(password);
- }
- System.out.println(mailServer);
- System.out.println(fromAddress);
- System.out.println(password);
- multiPartEmail.setHostName(mailServer);
- if (!StringUtil.isNullOrEmpty(password)) {
- multiPartEmail.setAuthentication(userName, password);
- }
- if(mailAlias){
- multiPartEmail.setFrom(fromAddress, mailAlias);
- } else{
- multiPartEmail.setFrom(fromAddress);
- }
- if(emailSSLEnabled){
- multiPartEmail.setSSL(true);
- if(port != ""){
- multiPartEmail.setSslSmtpPort(port);
- }
- }
- if(emailTLSEnabled){
- multiPartEmail.setTLS(true);
- }
- if(port != "" && !emailSSLEnabled){
- multiPartEmail.setSmtpPort(port);
- }
- var array = userEmail.split(";");
- for (var i = 0; i < array.length; i++) {
- multiPartEmail.addTo(array);//接收邮箱地址
- }
- // multiPartEmail.addTo(userEmail);//接收邮箱地址
- if (!StringUtil.isNullOrEmpty(ccemail)) {
- var array = ccemail.split(";");
- for (var i = 0; i < array.length; i++) {
- multiPartEmail.addCc(array);//抄送邮箱地址
- }
- }
- multiPartEmail.setCharset("GBK");//邮件内容字符集
- multiPartEmail.setSubject("预警通知测试");//邮件标题
- // var sb = new StringBuffer();
- // sb.append("
这是一个系统自动发送的邮件,请勿回复。
");
- // sb.append("\n");
- report = new SSReport(connector);
- report.open("这里是报表资源ID"); //报表资源ID
- report.setParamValue("这里是数据集中参数"+erji, jigou, jigou);
- if (!StringUtil.isNullOrEmpty(qudaoparam) && !StringUtil.isNullOrEmpty(qudao)) {
- report.setParamValue("这里是数据集中参数"+qudao, qudaoparam, qudaoparam);
- }
- var os = new ByteArrayOutputStream();
- report.doExport("HTML","","",os,"","","");
- var html = os.toString("utf8");
- //logger.info(html);
- report.close();
- // 业务库数据源
- //发送邮件
- multiPartEmail.setHtmlMsg(html);
- multiPartEmail.send();
- }
- // 告警规则
- var rule = function(row, userEmail, erji, jigou, qudao, qudaoparam, ccemail) {
- var cnt = row.get(0).getDoubleValue();
- var target = 0;
- if (cnt > target) {
- // 发送邮件
- sendMail(userEmail, erji, jigou, qudao, qudaoparam, ccemail);
- }
- }
- // 查询报表数据
- var dataQuery = function(userEmail, sqlWhere, erji, jigou, qudao, qudaoparam, ccemail) {
- // 监控sql
- var sql = "select count(1) as cnt from table1 a join table2 b on a.org_id=b.org_id " + sqlWhere;
- var gridData = dsService.executeNoCacheable(ds, sql);
- var data = gridData.getData();
- for (var i = 0; i < data.size(); i++) {
- var row = data.get(i);
- // 调用告警规则
- rule(row, userEmail, erji, jigou, qudao, qudaoparam, ccemail);
- }
- }
- // 查询配置表
- var gridDataGroup = dsService.executeNoCacheable(ds, configSql);
- for (var i = 0; i < gridDataGroup.getRowsCount(); i++) {
- var userEmail = gridDataGroup.get(i, 3).getValue();
- var sqlWhere = gridDataGroup.get(i, 4).getValue();
- var erji = gridDataGroup.get(i, 5).getValue();
- var jigou = gridDataGroup.get(i, 6).getValue();
- var qudao = gridDataGroup.get(i, 7).getValue();
- var qudaoparam = gridDataGroup.get(i, 8).getValue();
- var ccemail = gridDataGroup.get(i, 9).getValue();
-
- // 调用保表数据
- dataQuery(userEmail, sqlWhere, erji, jigou, qudao, qudaoparam, ccemail);
- }
复制代码 |