/* 
 * Company Name : 中贸促信息技术有限责任公司
 * Project Name:memberManageSys
 * File Name:EmailService.java
 * Package Name:org.ccpit.business.linkManage
 * Date:2015年11月30日
 * Copyright (c) 2015, jinlong@ccpit.org All Rights Reserved.
 * 
 */
package org.ccpit.base.emailSendManage;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;


import org.ccpit.base.controller.Convert;
import org.ccpit.base.controller.Page;
import org.ccpit.base.controller.PageBo;
import org.ccpit.base.controller.PageRequest;
import org.ccpit.base.metadataManage.MetadataDao;
import org.ccpit.base.user.UserDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * ClassName:EmailService <br/>
 * Function: TODO <br/>
 * Reason: TODO <br/>
 * Date: 2015年11月30日 <br/>
 * 
 * @author jinlong
 * @version
 * @since JDK 1.6
 * @see
 */
@Repository
@Transactional
public class EmailService {
	private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	@Autowired
	private EmailDao emailDao;
	@Autowired
	private MetadataDao metadataDao;
	@Autowired
	private UserDao userDao;
	
	public Page<EmailInfo> queryEmailInfo(PageRequest pageRequest, String hql) {
		return emailDao.findPage(pageRequest, hql, null);
	}
	
	public boolean add(EmailInfo emailInfo) {
		return emailDao.save(emailInfo);
	}

	public Map<String, Object> convertToMap(EmailInfo emailInfo) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (emailInfo == null) {
			return map;
		}
		map.put("id", emailInfo.getId());
		map.put("title", emailInfo.getTitle());
		map.put("recipients", emailInfo.getRecipients());
		map.put("addresser", emailInfo.getAddresser());
		map.put("content", emailInfo.getContent());
		map.put("creator", emailInfo.getCreator().getUsername());
		map.put("createTime", sdf.format(emailInfo.getCreateTime()));
		map.put("status", emailInfo.getStatus()==1?"成功":"失败");
	
		return map;
	}

	public PageBo<EmailInfo> convert(Page<EmailInfo> page) {
		return new PageBo<EmailInfo>(page, new Convert<EmailInfo>() {
			@Override
			public Map<String, Object> convert(EmailInfo obj) {
				return convertToMap(obj);
			}
		});
	}
}
