ConfigWarningService.java
9.04 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
package com.huaheng.pc.config.configWarning.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.sendMail.domain.SendMail;
import com.huaheng.pc.config.sendMail.service.MailService;
import com.huaheng.pc.config.sendMail.service.SendMailService;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.system.user.domain.User;
import com.huaheng.pc.system.user.service.IUserService;
import com.huaheng.pc.system.user.service.UserServiceImpl;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.pc.config.configWarning.domain.ConfigWarning;
import com.huaheng.pc.config.configWarning.mapper.ConfigWarningMapper;
import org.springframework.transaction.annotation.Transactional;
/**
* Created by Enzo Cotter on 2019/11/11.
*/
@Service
public class ConfigWarningService extends ServiceImpl<ConfigWarningMapper, ConfigWarning> {
@Resource
private MaterialService materialService;
@Resource
private SendMailService sendMailService;
@Resource
private InventoryDetailService inventoryDetailService;
@Resource
private MailService mailService;
@Resource
private LocationService locationService;
/**
* 保存预警配置
* @param configWarning
* @return
*/
@Transactional(rollbackFor = SecurityException.class)
public AjaxResult saveConfigWarning(ConfigWarning configWarning){
/* 设置仓库编码*/
configWarning.setWarehouseCode(ShiroUtils.getWarehouseCode());
/* 设置物料信息*/
if (configWarning.getMaterialCode() != null){
Material material = materialService.findAllByCode(configWarning.getMaterialCode());
configWarning.setMaterialName(material.getName());
configWarning.setMaterialSpec(material.getSpec());
configWarning.setMaterialUnit(material.getUnit());
}
/* 设置创建人、更新人*/
configWarning.setCreatedBy(ShiroUtils.getLoginName());
configWarning.setLastUpdatedBy(ShiroUtils.getLoginName());
/* 保存*/
if (!save(configWarning)){
throw new SecurityException("保存配置信息失败");
}
return AjaxResult.success("");
}
/**
* 保存预警配置
* @param configWarning
* @return
*/
@Transactional(rollbackFor = SecurityException.class)
public AjaxResult updateConfigWarning(ConfigWarning configWarning){
/* 设置物料信息*/
if (configWarning.getMaterialCode() != null){
Material material = materialService.findAllByCode(configWarning.getMaterialCode());
configWarning.setMaterialName(material.getName());
configWarning.setMaterialSpec(material.getSpec());
configWarning.setMaterialUnit(material.getUnit());
}
/* 设置更新人*/
configWarning.setLastUpdatedBy(ShiroUtils.getLoginName());
if (!updateById(configWarning)){
throw new SecurityException("修改配置信息失败");
}
return AjaxResult.success("");
}
/**
* 物料预警
* @param materialCode,materialName 任务明细
*/
public void materialWarning(String materialCode, String materialName){
/* 查询预警配置*/
LambdaQueryWrapper<ConfigWarning> lambdaQueryWrapper = Wrappers.lambdaQuery();
lambdaQueryWrapper.eq(ConfigWarning::getMaterialCode, materialCode)
.eq(ConfigWarning::getWarehouseCode, ShiroUtils.getWarehouseCode())
.eq(ConfigWarning::getType, 0);
ConfigWarning configWarning = this.getOne(lambdaQueryWrapper);
/* 当预警配置存在时*/
if (configWarning != null) {
String[] emailArray = configWarning.getEmail().split(";");
/* 收件人*/
String[] to = {emailArray[0]};
/* 抄送人*/
List<String> ccList = new ArrayList<>();
for (int i = 1; i < emailArray.length; i++) {
ccList.add(emailArray[i]);
}
String[] cc = ccList.toArray(new String[0]);
InventoryDetail inventoryDetail = new InventoryDetail();
inventoryDetail.setWarehouseCode(ShiroUtils.getWarehouseCode());
inventoryDetail.setMaterialCode(materialCode);
inventoryDetail.setInventorySts("good");
inventoryDetail.setCompanyCode(configWarning.getCompanyCode());
BigDecimal sum = inventoryDetailService.getSumQty(inventoryDetail);
SendMail sendMail = new SendMail();
sendMail.setWarehouseCode(ShiroUtils.getWarehouseCode());
sendMail.setCompanyCode(configWarning.getCompanyCode());
sendMail.setType(0);
sendMail.setEmail(configWarning.getEmail());
sendMail.setAlarmTime(new Date());
if (sum.compareTo(configWarning.getMax()) == 1) {
sendMail.setTitle(materialName + "超过最大库存");
sendMail.setBody(materialName + "超过最大库存,当前数量为" + sum);
if (!sendMailService.save(sendMail)) {
throw new SecurityException("保存失败");
}
boolean resultMail = mailService.sendSimpleMail(to, sendMail.getTitle(), sendMail.getBody(), cc);
sendMail.setEnable(resultMail);
if (!sendMailService.updateById(sendMail)) {
throw new SecurityException("更新失败");
}
} else if (sum.compareTo(configWarning.getMin()) == -1) {
sendMail.setTitle(materialName + "低于最低库存");
sendMail.setBody(materialName + "低于最低库存,当前数量为" + sum);
if (!sendMailService.save(sendMail)) {
throw new SecurityException("保存失败");
}
boolean resultMail = mailService.sendSimpleMail(to, sendMail.getTitle(), sendMail.getBody(), cc);
sendMail.setEnable(resultMail);
if (!sendMailService.updateById(sendMail)) {
throw new SecurityException("更新失败");
}
}
}
}
/**
* 空库位剩余数量预警
*/
public void emptyLocationWarning(){
/* 查询预警配置*/
LambdaQueryWrapper<ConfigWarning> lambdaQueryWrapper = Wrappers.lambdaQuery();
lambdaQueryWrapper.eq(ConfigWarning::getWarehouseCode, ShiroUtils.getWarehouseCode())
.eq(ConfigWarning::getType, 1);
ConfigWarning configWarning = this.getOne(lambdaQueryWrapper);
/* 当预警配置存在时*/
if (configWarning != null) {
String[] emailArray = configWarning.getEmail().split(";");
/* 收件人*/
String[] to = {emailArray[0]};
/* 抄送人*/
List<String> ccList = new ArrayList<>();
for (int i = 1; i < emailArray.length; i++) {
ccList.add(emailArray[i]);
}
String[] cc = ccList.toArray(new String[0]);
LambdaQueryWrapper<Location> locationLambda = Wrappers.lambdaQuery();
locationLambda.eq(Location::getWarehouseCode, ShiroUtils.getWarehouseCode())
.eq(Location::getStatus, QuantityConstant.STATUS_LOCATION_EMPTY);
BigDecimal sum = new BigDecimal(locationService.list(locationLambda).size());
SendMail sendMail = new SendMail();
sendMail.setWarehouseCode(ShiroUtils.getWarehouseCode());
sendMail.setType(1);
sendMail.setEmail(configWarning.getEmail());
sendMail.setAlarmTime(new Date());
if (sum.compareTo(configWarning.getMin()) == -1) {
sendMail.setTitle("空库位剩余"+sum+"个");
sendMail.setBody("空库位剩余"+sum+"个");
if (!sendMailService.save(sendMail)) {
throw new SecurityException("保存失败");
}
boolean resultMail = mailService.sendSimpleMail(to, sendMail.getTitle(), sendMail.getBody(), cc);
sendMail.setEnable(resultMail);
if (!sendMailService.updateById(sendMail)) {
throw new SecurityException("更新失败");
}
}
}
}
}