/**
 * 
 */
package com.blueland.bpm.manage.util;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.blueland.bpm.application.model.ApplicationEntity;
import com.blueland.bpm.common.util.DateConvertUtils;
import com.blueland.bpm.common.util.SysConfig;
import com.blueland.bpm.log.model.ProcessErrorLog;
import com.google.gson.JsonObject;

/**
 * @author ly
 *
 */
public class SmsEmailMsg {

	private static final Logger log = LoggerFactory.getLogger(SmsEmailMsg.class);
	/**
	 * 发送邮件提醒
	 * 
	 * @param param
	 * @return
	 */
	public static String sendSmsEmail(String param) {
		String url = (String) SysConfig.getValue("SMS_EMAIL_URL");
		if (StringUtils.isEmpty(url)) {
			log.info(String.format("未配置邮件发送地址"));
			return null;
		}
		return HttpUtil.post(url, param);
	}
	
	/**
	 * 根据应用信息和流程错误信息组装邮件发送参数
	 * @param applicationEntity 应用信息
	 * @param processErrorLog 流程错误信息
	 * @return
	 */
	public static String buildEmailMsgData(ApplicationEntity applicationEntity, ProcessErrorLog processErrorLog,String msg) {
		JsonObject jsonObject = new JsonObject();
		jsonObject.addProperty("toMailAddress", applicationEntity.getManagerEmails());//applicationEntity.getAppUserMails());
		jsonObject.addProperty("toMailUser", applicationEntity.getManagerNames());//applicationEntity.getAppUserNames());
		jsonObject.addProperty("mailSubject", processErrorLog.getAppId()+"_"+processErrorLog.getProcessTitle()+"异常"+msg);
		String ebpmUrl = (String) SysConfig.getValue("EBPM_URL");
		StringBuilder content = new StringBuilder();
		content.append("管理员，您好\n");
		content.append(
				"  " + processErrorLog.getAppId()+"_"+processErrorLog.getProcessTitle()+"异常"+msg + "，请管理员访问 " + ebpmUrl + " （流程执行管理系统）进行查看。\n");
		content.append("\t\t\t\t\t\t\t\t\t\t" + DateConvertUtils.format(new Date(), "yyyy-MM-dd"));
		jsonObject.addProperty("mailMessage", content.toString());
		jsonObject.addProperty("fromUser", "系统管理员");
		return jsonObject.toString();
	}
}
