AlarmMailApiController.java
8.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package com.huaheng.common.mail.alarmmail.controller;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.mail.SendMailText;
import com.huaheng.common.mail.alarmmail.domain.AlarmType;
import com.huaheng.common.mail.alarmmail.domain.SendEmail;
import com.huaheng.common.mail.alarmmail.domain.Sender;
import com.huaheng.common.mail.alarmmail.mapper.SendEmailMapperAuto;
import com.huaheng.common.mail.alarmmail.service.AlarmScanningService;
import com.huaheng.common.support.Convert;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.aspectj.lang.annotation.Log;
import com.huaheng.framework.aspectj.lang.constant.BusinessType;
import com.huaheng.framework.web.controller.BaseController;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.framework.web.page.TableDataInfo;
import com.huaheng.pc.config.containercapacity.domain.ContainerCapacity;
import com.huaheng.pc.general.material.domain.Material;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
* 邮件表 处理接口
*
* @author ZengBo
* @date 2019-03-08
*/
@Controller
@RequestMapping("/administration/SendMail")
public class AlarmMailApiController extends BaseController {
private String prefix = "administration/SendMail";
@Resource
private SendEmailMapperAuto sendEmailMapperAuto;
@Autowired
AlarmScanningService alarmScanningService;
@RequiresPermissions("administration:SendMail:view")
@GetMapping()
public String SendMail()
{
return prefix + "/SendMail";
}
/**
* 查询邮件发送列表
*/
@RequiresPermissions("administration:SendMail:list")
@Log(title = "管理-报警邮件", operating = "查看报警邮件列表", action = BusinessType.GRANT)
@PostMapping("/list")
@ResponseBody
public TableDataInfo list(SendEmail sendEmail)
{
sendEmail.setDeleted(false);
startPage();
List<SendEmail> list = sendEmailMapperAuto.selectListEntityByLike(sendEmail) ;
return getDataTable(list);
}
/**
* id查询
*/
@GetMapping("/edit/{id}")
public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
SendEmail sendEmail = sendEmailMapperAuto.selectEntityById(id);
mmap.put("sendEmail", sendEmail);
return prefix + "/edit";
}
/**
* 修改
*/
@RequiresPermissions("administration:SendMail:edit")
@Log(title = "管理-报警类型修改", operating = "管理-报警类型修改", action = BusinessType.UPDATE)
@PostMapping("/edit")
@ResponseBody
public AjaxResult editSave(SendEmail sendEmail) {
sendEmail.setLastUpdatedBy(ShiroUtils.getLoginName());
return toAjax(sendEmailMapperAuto.updateByModel(sendEmail));
}
/**
* 删除
*/
@RequiresPermissions("administration:SendMail:remove")
@Log(title = "管理-报警类型删除", operating = "管理-报警类型删除", action = BusinessType.DELETE)
@PostMapping("/remove")
@ResponseBody
public AjaxResult remove(String ids) {
if (StringUtils.isEmpty(ids))
return AjaxResult.error("id不能为空");
for (Integer id : Convert.toIntArray(ids)) {
SendEmail sendEmail = new SendEmail();
sendEmail.setId(id);
sendEmail.setDeleted(true);
sendEmailMapperAuto.updateByModel(sendEmail);
}
return AjaxResult.success("删除成功");
}
/**
* 报警主控制API
*/
@Log(title = "配置-报警控制", operating = "报警控制", action = BusinessType.OTHER)
@PostMapping("/main")
@ResponseBody
@Transactional
public AjaxResult main() throws Exception {
return alarmScanningService.Mastercontrol();
}
/**
* 邮件发送
*/
@Log(title = "配置-报警邮件发送", operating = "报警邮件发送", action = BusinessType.OTHER)
@PostMapping("/send")
@ResponseBody
@Transactional
public AjaxResult sendMail(Integer id) throws Exception {
Sender sender = new Sender();
//发件人地址
sender.setSenderAddress("wms@huahengweld.com");
//发件人账户名
sender.setSenderAccount("wms");
//发件人邮箱密码
sender.setSenderPassword("nkfwhfi986+/214G2");
//查询出所有未发送,未删除,未处理的邮件
SendEmail sendEmail = new SendEmail();
sendEmail.setEnable(false);
sendEmail.setDeleted(false);
sendEmail.setHandle(false);
List<SendEmail> sendEmailList = new ArrayList<>();
SendEmail rs = new SendEmail();
rs.setEnable(true);
try {
if (id == null) {
sendEmailList = sendEmailMapperAuto.selectListEntityByEqual(sendEmail);
for (int i = 0; i < sendEmailList.size(); i++) {
rs.setId(sendEmailList.get(i).getId());
Integer result = sendEmailMapperAuto.updateByModel(rs);
if (result < 1) {
return AjaxResult.error("失败!");
}
}
} else {
sendEmailList.add(sendEmailMapperAuto.selectEntityById(id));
rs.setId(id);
Integer result = sendEmailMapperAuto.updateByModel(rs);
if (result < 1) {
return AjaxResult.error("失败!");
}
}
}catch (Exception e){
throw new ServiceException("失败!");
}
for (SendEmail item : sendEmailList) {
//1、连接邮件服务器的参数配置
Properties props = new Properties();
//设置用户的认证方式
props.setProperty("mail.smtp.auth", "true");
//设置传输协议
props.setProperty("mail.transport.protocol", "smtp");
//设置发件人的SMTP服务器地址,使用前PING一下mail.huahengweld.com
props.setProperty("mail.smtp.host", "172.16.0.2");
//2、创建定义整个应用程序所需的环境信息的 Session 对象
Session session = Session.getInstance(props);
//设置调试信息在控制台打印出来
session.setDebug(true);
//3、创建邮件的实例对象
SendMailText sendMailText = new SendMailText();
sender.setTitle(item.getTitle());
sender.setBody(item.getBody());
//拆分收件人和收件人邮箱
// String addressee = item.getAddressee();
String addresseeemail = item.getAddresseeemail();
// String[] addresseelist = addressee.split(";");
String[] addresseeemaillist = addresseeemail.split(";");
Message msg = sendMailText.getMimeMessage(session, sender,addresseeemaillist);
//4、根据session对象获取邮件传输对象Transport
Transport transport = session.getTransport();
//设置发件人的账户名和密码
transport.connect(sender.getSenderAccount(), sender.getSenderPassword());
//发送邮件,并发送到所有收件人地址,message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人
transport.sendMessage(msg, msg.getAllRecipients());
//5、关闭邮件连接
transport.close();
}
return AjaxResult.success("发送成功!!");
}
/**
* 邮件处理情况扫描
*/
@Log(title = "配置-库存报警级别升级", operating = "库存报警级别升级", action = BusinessType.UPDATE)
@PostMapping("/level")
@ResponseBody
@Transactional
public AjaxResult level() throws Exception {
SendEmail sendEmail = new SendEmail();
sendEmail.setDeleted(false);
sendEmail.setHandle(false);
List<SendEmail> list = sendEmailMapperAuto.selectListEntityByEqual(sendEmail);
for (SendEmail item : list){
Integer i = item.getWarninglevel()+1;
item.setWarninglevel(i);
try {
sendEmailMapperAuto.updateByModel(item);
}catch (Exception e){
throw new Exception("库存报警级别升级失败!");
}
}
return AjaxResult.success("库存报警级别升级成功!");
}
}