Skip to content

重构已完成,不知是否符合 SRP 原则。QQ:992331664#324

Merged
onlyliuxin merged 7 commits intoonlyliuxin:masterfrom
yupengj:master
Jun 19, 2017
Merged

重构已完成,不知是否符合 SRP 原则。QQ:992331664#324
onlyliuxin merged 7 commits intoonlyliuxin:masterfrom
yupengj:master

Conversation

@yupengj
Copy link
Contributor

@yupengj yupengj commented Jun 13, 2017

No description provided.

@yupengj yupengj changed the title 重构已完成,不知是否符合 SRP 原则,希望大家提意见。^_^ 重构已完成,不知是否符合 SRP 原则。 Jun 13, 2017
@yupengj
Copy link
Contributor Author

yupengj commented Jun 13, 2017

// 核心代码
public class PromotionMail {

protected SubscriptionsService subscriptionsService;

protected ProductService productService;

public PromotionMail(SubscriptionsService subscriptionsService, ProductService productService) {
	this.subscriptionsService = subscriptionsService;
	this.productService = productService;
}

/**
 * 发送促销邮件
 * @param file 促销产品文件
 * @param mailDebug
 * @throws Exception
 */
public void sendPromotionMail(File file, boolean mailDebug) throws Exception {

	// 得到促销的产品
	List<Product> products = productService.doFindPromotionalProducts(file);

	// 得到促销产品的订阅信息
	List<Subscriptions> subscriptions = subscriptionsService.doFindByProducts(products);

	// 得到订阅人的邮箱和名称,邮箱内容
	List<Mail> mails = getMails(subscriptions);

	// 发送邮箱
	sendEMails(new ConnectionConfig(new Configuration()), mails, mailDebug);
}

protected List<Mail> getMails(List<Subscriptions> subscriptions) {

	List<Mail> mails = new ArrayList<Mail>();
	String subject = "您关注的产品降价了";

	for (Subscriptions sub : subscriptions) {
		String productDesc = sub.getProduct().getProductDesc();
		String message = "尊敬的 " + sub.getName() + ", 您关注的产品 " + productDesc + " 降价了,欢迎购买!";
		mails.add(new Mail(subject, message, sub.getEmail()));
	}
	return mails;
}

protected void sendEMails(ConnectionConfig config, List<Mail> mails, boolean debug) {
	if (mails == null) {
		System.out.println("没有邮件需要发送");
		return;
	}
	System.out.println("开始发送邮件");
	Iterator<Mail> iter = mails.iterator();
	while (iter.hasNext()) {
		Mail mail = iter.next();
		if (mail.getToAddress().length() <= 0) {
			continue;
		}
		try {
			MailUtil.sendEmail(mail.getToAddress(), config.getFromAddress(), mail.getSubject(), mail.getMessage(),
					config.getSmtpHost(), debug);
		} catch (Exception e) {
			try {
				MailUtil.sendEmail(mail.getToAddress(), config.getFromAddress(), mail.getSubject(),
						mail.getMessage(), config.getAltSmtpHost(), debug);

			} catch (Exception e2) {
				System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage());
			}
		}
	}
	System.out.println("发送邮件结束");
}

}

@yupengj yupengj changed the title 重构已完成,不知是否符合 SRP 原则。 重构已完成,不知是否符合 SRP 原则。QQ:992331664 Jun 14, 2017
@onlyliuxin onlyliuxin merged commit 4bd06ed into onlyliuxin:master Jun 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants