package com.cnd3b.utility.qywechat.base;

import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class CreateMsg {
	/**
	 * 创建简单文字消息
	 * 
	 * @param content
	 */
	public String createTextMsg(String content, String FromUserName,
			String ToUserName) {
		return "<xml>\n" + "<ToUserName><![CDATA[" + FromUserName
				+ "]]></ToUserName>\n" + "<FromUserName><![CDATA[" + ToUserName
				+ "]]></FromUserName>\n" + "<CreateTime>"
				+ Calendar.getInstance().getTime().getTime()
				+ "</CreateTime>\n" + "<MsgType><![CDATA[text]]></MsgType>\n"
				+ "<Content><![CDATA[" + content + "]]></Content>\n" + "</xml>";
	}

	/**
	 * 创建新闻消息
	 * 
	 * @param content
	 */
	public String createNewsMsg(List<Map<String, String>> content,
			String FromUserName, String ToUserName) {
		int count = content.size();// 信息条数
		Iterator<Map<String, String>> it = content.iterator();
		StringBuilder items = new StringBuilder();
		while (it.hasNext()) {
			Map<String, String> map = it.next();
			String Title = map.get("Title");// 表题
			String Description = map.get("Description");// 描述
			String PicUrl = map.get("PicUrl");// 图片url
			String Url = map.get("Url");// 链接地址
			items.append("<item>").append("<Title><![CDATA[").append(Title).append("]]></Title>").append(" <Description><![CDATA[").append(Description).append("]]></Description>").append(" <PicUrl><![CDATA[").append(PicUrl).append("]]></PicUrl>").append("<Url><![CDATA[").append(Url).append("]]></Url>").append("</item>");
		}
		return "<xml>\n" + "<ToUserName><![CDATA[" + FromUserName
				+ "]]></ToUserName>\n" + "<FromUserName><![CDATA[" + ToUserName
				+ "]]></FromUserName>\n" + "<CreateTime>"
				+ Calendar.getInstance().getTime().getTime()
				+ "</CreateTime>\n" + "<MsgType><![CDATA[news]]></MsgType>\n"
				+ "<ArticleCount>" + count + "</ArticleCount>\n" + "<Articles>"
				+ items + "</Articles>" + "</xml>";
	}

}
