AlarmScanningService.java
7.91 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
package com.huaheng.common.mail.alarmmail.service;
import com.huaheng.common.mail.alarmmail.domain.AlarmFlow;
import com.huaheng.common.mail.alarmmail.domain.AlarmType;
import com.huaheng.common.mail.alarmmail.domain.SendEmail;
import com.huaheng.common.mail.alarmmail.mapper.AlarmFlowMapper;
import com.huaheng.common.mail.alarmmail.mapper.AlarmTypeMapper;
import com.huaheng.common.mail.alarmmail.mapper.SendEmailMapperAuto;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.zoneCapacity.domain.ZoneCapacity;
import com.huaheng.pc.config.zoneCapacity.service.IZoneCapacityService;
import com.huaheng.pc.general.location.service.ILocationService;
import com.huaheng.pc.general.material.service.IMaterialService;
import com.huaheng.pc.inventory.inventory.service.IInventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 库存报警 主要逻辑层
* @author ZengBo
* @date 2019-03-08
*/
@Service
@Transactional
public class AlarmScanningService {
@Resource
private AlarmTypeMapper alarmTypeMapper;
@Resource
private AlarmFlowMapper alarmFlowMapper;
@Resource
private SendEmailMapperAuto sendEmailMapperAuto;
@Autowired
private IInventoryService iInventoryService;
@Autowired
private IZoneCapacityService iZoneCapacityService;
@Autowired
IMaterialService iMaterialService;
/** 库存预警主逻辑控制方法 */
public AjaxResult Mastercontrol() throws Exception {
/** 上限预警值 */
BigDecimal uphAlarm = null;
/** 上限值 */
BigDecimal uph = null;
/** 下限预警值 */
BigDecimal lphAlarm = null;
/** 下限值 */
BigDecimal lph = null;
List<AlarmType> alarmTypeList = alarmTypeMapper.selectAllalarm(true);
for (AlarmType item : alarmTypeList) {
BigDecimal qty = iInventoryService.getSumQtyByZoneId(item.getZoneId(),item.getMaterialcode());
ZoneCapacity zoneCapacity1 = new ZoneCapacity();
zoneCapacity1.setZoneId(item.getZoneId());
zoneCapacity1.setMaterialCode(item.getMaterialcode());
ZoneCapacity zoneCapacity = iZoneCapacityService.selectFirstEntity(zoneCapacity1);
SendEmail sendEmailEnd = new SendEmail();
sendEmailEnd.setAlarmtypeid(item.getId());
sendEmailEnd.setDeleted(false);
uphAlarm = zoneCapacity.getUphAlarm();
uph = zoneCapacity.getUph();
lphAlarm = zoneCapacity.getLphAlarm();
lph = zoneCapacity.getLph();
SendEmail sendEmail = sendEmailMapperAuto.selectFirstEntity(sendEmailEnd);
sendEmailEnd.setId(sendEmail.getId());
sendEmailEnd.setLastUpdatedBy(ShiroUtils.getLoginName());
boolean y = false;
if (sendEmail == null){
List<AlarmFlow> alarmFlows = alarmFlowMapper.selectEntityByAlarmidList(item.getId(),1);
sendEmailEnd.setWarninglevel(1);
List<String> strings = Assemble(alarmFlows);
sendEmailEnd.setAddressee(strings.get(0));
sendEmailEnd.setAddresseeemail(strings.get(1));
sendEmailEnd.setCreatedBy(ShiroUtils.getLoginName());
}else {
Integer garade = sendEmail.getWarninglevel();
List<AlarmFlow> alarmFlows = alarmFlowMapper.selectEntityByAlarmidList(item.getId(), garade);
sendEmailEnd.setWarninglevel(garade);
List<String> strings = Assemble(alarmFlows);
sendEmailEnd.setAddressee(strings.get(0));
sendEmailEnd.setAddresseeemail(strings.get(1));
y = true;
}
if (qty.compareTo(uphAlarm) == 0 || qty.compareTo(uphAlarm) == 1){
if (qty.compareTo(uph) == 0 || qty.compareTo(uph) == 1){
//超过或等于上限值
return saveSendEmail(sendEmailEnd,item,1,y);
}else {
//超过或等于上限预警值
return saveSendEmail(sendEmailEnd,item,2,y);
}
}
if (qty.compareTo(lphAlarm) == 0 || qty.compareTo(lphAlarm) == -1){
if (qty.compareTo(lph) == 0 || qty.compareTo(lph) == -1){
//低于或等于下限值
return saveSendEmail(sendEmailEnd,item,3,y);
}else {
//低于或等于下限预警值
return saveSendEmail(sendEmailEnd,item,4,y);
}
}
//修改处理状态
if (sendEmail != null){
sendEmail.setWarninglevel(1);
sendEmail.setHandle(true);
sendEmail.setAddressee("");
sendEmail.setAddresseeemail("");
Integer result = sendEmailMapperAuto.updateByModel(sendEmail);
if (result < 1 ){
throw new Exception("修改邮件表ID为: " + sendEmail.getId() + " 的处理状态失败!");
}
}
}
return AjaxResult.success("成功!");
}
/**
* 组装邮件发送报表
* x : 四种报警情况 1:大于上限值 , 2:大于上限预警值 ,3:小于下限值 , 4:小于下限预警值
* @param sendEmail
*/
public AjaxResult saveSendEmail(SendEmail sendEmail,AlarmType alarmType,Integer x,boolean y) throws Exception {
String warehousecode = ShiroUtils.getWarehouseCode();
String materialname = iMaterialService.selectNameBycode(alarmType.getMaterialcode()).getName();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sendEmail.setAlarmtime(new Date());
String date = simpleDateFormat.format(sendEmail.getAlarmtime());
String alarm = null;
if (x == 1) {
alarm = " 时超过了上限值!请及时处理...";
}if (x == 2){
alarm = " 时超过了上限预警值!请及时处理...";
}if (x == 3){
alarm = " 时低于了下限值!请及时处理...";
}if (x == 4){
alarm = " 时低于了下限预警值!请及时处理...";
}
String titile = warehousecode + " : 仓库物料报警---" + sendEmail.getWarninglevel() + "级";
String body = "您好!" + "仓库: " + warehousecode + " 区域ID为: " + alarmType.getZoneId() + "的物料: " + materialname + " 在 " + date + alarm;
sendEmail.setTitle(titile);
sendEmail.setBody(body);
sendEmail.setEnable(false);
sendEmail.setHandle(false);
if (y) {
if (sendEmailMapperAuto.updateByModel(sendEmail)<1){
throw new Exception("更新SendMail失败!");
}else {
return AjaxResult.success("更新SendMail成功!");
}
} else {
if (sendEmailMapperAuto.insert(sendEmail)<1){
throw new Exception("新增SendMail失败!");
}else {
return AjaxResult.success("新增SendMail成功!");
}
}
}
public List<String> Assemble(List<AlarmFlow> alarmFlows){
StringBuilder addressee = new StringBuilder();
StringBuilder addresseeemail = new StringBuilder();
List<String> list = new ArrayList<>();
for (int i = 0; i < alarmFlows.size(); i++) {
addressee.append(alarmFlows.get(i).getAddressee()).append(";");
addresseeemail.append(alarmFlows.get(i).getAddresseeemail()).append(";");
}
list.add(String.valueOf(addressee));
list.add(String.valueOf(addresseeemail));
return list;
}
}