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

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.ccpit.base.controller.BaseController;
import org.ccpit.base.logManage.LogInfoService;
import org.ccpit.base.metadataManage.MetadataDao;
import org.ccpit.base.metadataManage.MetadataService;
import org.ccpit.base.user.User;
import org.ccpit.base.user.UserDao;
import org.ccpit.base.user.UserEmail;
import org.ccpit.base.utils.mailUtil.Mail;
import org.ccpit.base.utils.mailUtil.MailUtil;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/** 
 * ClassName:EmailSendController <br/>
 * Function: TODO  <br/>
 * Reason:	 TODO  <br/>
 * Date:     2016年1月21日 <br/>
 * @author   jinlong
 * @version  
 * @since    JDK 1.6
 * @see 	 
 */
@Controller
@RequestMapping("base/emailSendManage") 
public class EmailSendController extends BaseController {
	private static SimpleDateFormat sdfD = new SimpleDateFormat("yyyy-MM-dd");
	private static SimpleDateFormat sdfM = new SimpleDateFormat("yyyy-MM");
	private static final Logger log = LoggerFactory.getLogger(EmailSendController.class);
	@Autowired
	private MetadataService metadataService;
	@Autowired
	private LogInfoService logInfoService;
	@Autowired
	private UserDao userDao;
	@Autowired
	private MetadataDao metadataDao;
	
	//单发
	public void sendOneEmail(HttpServletRequest request,String emailAddress,EmailInfo emailInfo,UserEmail userEmail){
		Mail mail = null;
		if (emailInfo != null && userEmail != null) {
			mail = new Mail();
			mail.setHost(userEmail.getServerHost());
			mail.setProtocol(userEmail.getServerProtocol());
			mail.setSender(userEmail.getEmailCount());
			mail.setName(null);
			mail.setEmailCount(userEmail.getEmailCount());
			mail.setEmailPassword(userEmail.getEmailPassword());
			mail.setReceiver(emailAddress);
//			mail.setCopyTo(emailInfo.getCopyTo());
			mail.setMailTitle(emailInfo.getTitle());
			mail.setMailMessage(emailInfo.getContent());
			String files = getAffixFromDir(emailInfo.getAffix());
			mail.setAffix(files);
			//监测发送过程，如果发送失败，则把发送失败的名单保留下来，并回显到页面
			MailUtil.sendMail(mail);
		}
    }
	public String getAffixFromDir(String dir) {
		File file = new File(dir);
		StringBuffer fStr = null;
		if (file.exists()) {
			fStr = new StringBuffer();
			File fa[] = file.listFiles();
			for (int i = 0; i < fa.length; i++) {
				fStr.append(fa[i] + ";");
			}
			return fStr.toString();
		}
		return null;
	}
	public EmailInfo addEmailInfo(HttpServletRequest request) {
		EmailInfo emailInfo = new EmailInfo();Session session = null;
		try {
			// 获取表单信息
			String emailStatus = request.getParameter("emailStatus");
			String recipients = request.getParameter("addRecipientsName");
			String recipientName = request.getParameter("addRecipientNameName");
			String title = request.getParameter("addTitleName");
			String affixRealPath = request.getParameter("addEmailRealPathName");
			String content = request.getParameter("emailContent");
			String copyTo = request.getParameter("copyTo");
			
			
			emailInfo.setStatus(emailStatus);
			emailInfo.setRecipients(recipients);
			emailInfo.setRecipientName(recipientName);
			emailInfo.setTitle(title);
			emailInfo.setAffix(affixRealPath);
			User user = (User) request.getSession().getAttribute(
					BaseController.USER_IN_SESSION);
//			User user = (User)userDao.queryById((long)1);
			UserEmail userEmail = user.getUserEmail();
			if(userEmail==null){
				throw new RuntimeException("userEmail为空");
			}
			emailInfo.setAddresser(userEmail.getEmailCount());
			emailInfo.setContent(content);
			emailInfo.setEmailWhether(true);
			emailInfo.setCreateTime(new Date());
			emailInfo.setCreator(user);
			emailInfo.setCopyTo(copyTo);
			emailInfo.setId(0);
			emailInfo.setEmailWhether(true);
			session = userDao.getHibernateTemplate().getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.save(emailInfo);
	        
			session.getTransaction().commit();
			return emailInfo;
		} catch (Exception e) {
			session.getTransaction().rollback();
			log.info(e.getMessage());
			e.printStackTrace();
			return null;
		}finally{
			log.info("添加邮件");
//			session.close();
		}
	
		
	}
	
}
