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

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.utils.mailUtil.Mail;
import org.ccpit.base.utils.mailUtil.MailUtil;
import org.ccpit.business.registerManage.RegisterInfo;
import org.ccpit.business.registerManage.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

/**
 * 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 {
	@Autowired
	private MetadataService metadataService;
	@Autowired
	private LogInfoService logInfoService;
	@Autowired
	private UserDao userDao;
	@Autowired
	private MetadataDao metadataDao;
	@Autowired
	private EmailService emailService;
	@Autowired
	private RegisterService registerService;

	@RequestMapping("sendEmail")
	@ResponseBody
	public Object sendEmail(HttpServletRequest request) {
		// 查找所有接收邮件的会员,放入rList中
		String ids = request.getParameter("jsonIds");
		List<Long> list = JSON.parseArray(ids, Long.class);
		List<RegisterInfo> rList = new ArrayList<RegisterInfo>();
		if (list != null && !list.isEmpty()) {
    		for (long id : list) {
    			RegisterInfo reg = registerService.queryRegisterById(id);
    			if (reg != null) {
    				rList.add(reg);
    			}
    		}
    	}
		
		String emailSender = request.getParameter("emailSender");
		String emailTitle = request.getParameter("emailTitle");
		String emailContent = request.getParameter("emailContent");
		
		// 5个人封装为一个String,放入List中，每50人发一封邮件
		List<StringBuffer> strList = new ArrayList<StringBuffer>();
		StringBuffer sb= new StringBuffer();
		for(int i=0;i<rList.size();i++){
			RegisterInfo registerInfo = rList.get(i);
			String name = registerInfo.getEmail();
			sb = sb.append(name+";");
			if(i%5==4||i==rList.size()-1){
				strList.add(sb);
				sb= new StringBuffer();
			}
		}
		for(StringBuffer s:strList){
			EmailInfo emailInfo = new EmailInfo();
			emailInfo.setTitle(emailTitle);
			emailInfo.setRecipients(s.toString());
			emailInfo.setAddresser(emailSender);
			emailInfo.setContent(emailContent);
			emailInfo.setCreateTime(new Date());
			emailInfo.setCreator((User) request.getSession().getAttribute(BaseController.USER_IN_SESSION));
			sendOneEmail(request, s.toString(),emailInfo);
		}
		
		Map<String, Object> result =new HashMap<String, Object>();
		result.put("flag", true);
		result.put("info", "发送完毕！！！");
		return result;
	}


	// 单发
	public void sendOneEmail(HttpServletRequest request, String emailAddress,EmailInfo emailInfo) {
		Mail mail = null;
		if (emailInfo != null) {
			mail = new Mail();
			mail.setHost("smtp.exmail.qq.com");
			mail.setProtocol("smtp");
			
			StringBuilder sb = new StringBuilder("<div class='emailbody'>");
			sb.append("<div>尊敬的考生：</div>");
			sb.append("<div>"+emailInfo.getContent()+"</div>");
			sb.append("<div>中国贸促会培训中心</div>");
			sb.append("</div>");
			
			//设置发件人邮箱账号密码
			String addresser = emailInfo.getAddresser();
			mail.setSender(addresser);
			if(addresser.equals("cdcschina@ccpit.org")){
				mail.setEmailPassword("ccoic208");
			}else if(addresser.equals("citfchina@ccpit.org")){
				mail.setEmailPassword("suk4376");
			}else if(addresser.equals("csdgchina@ccpit.org")){
				mail.setEmailPassword("clyde361412");
			}
			
			mail.setName("培训中心报名管理平台");
			mail.setEmailCount(addresser);
			mail.setReceiver(emailAddress);
			mail.setMailTitle(emailInfo.getTitle());
			mail.setMailMessage(sb.toString());
			
			boolean sendStatus = MailUtil.sendMail(mail);
			
			if (sendStatus) {
				emailInfo.setStatus(1);//成功
			} else {
				emailInfo.setStatus(0);//失败
			}
			emailService.add(emailInfo);
		}
	}

}
