ReceiptHeaderService.java
13.8 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package com.huaheng.pc.receipt.receiptHeader.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.support.Convert;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.check.checkDetail.domain.CheckDetail;
import com.huaheng.pc.check.checkDetail.service.CheckDetailService;
import com.huaheng.pc.check.checkHeader.domain.CheckHeader;
import com.huaheng.pc.check.checkHeader.service.CheckHeaderService;
import com.huaheng.pc.config.receiptType.domain.ReceiptType;
import com.huaheng.pc.config.receiptType.service.ReceiptTypeService;
import com.huaheng.pc.config.sendMail.service.SendMailService;
import com.huaheng.pc.config.statusFlow.domain.StatusFlowDetail;
import com.huaheng.pc.config.statusFlow.service.StatusFlowDetailService;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailServiceImpl;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.mapper.ReceiptHeaderMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.lang.ref.WeakReference;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
@Service
public class ReceiptHeaderService extends ServiceImpl<ReceiptHeaderMapper, ReceiptHeader> {
@Resource
private ReceiptTypeService receiptTypeService;
@Resource
private ReceiptDetailService receiptDetailService;
@Resource
private CheckHeaderService checkHeaderService;
@Resource
private CheckDetailService checkDetailService;
@Resource
private SendMailService sendMailService;
/**
* 保存入库单
* @param receiptHeader
* @return
*/
public AjaxResult saveReceiptHeader(ReceiptHeader receiptHeader){
LambdaQueryWrapper<ReceiptType> lambdaQueryWrapper = Wrappers.lambdaQuery();
lambdaQueryWrapper.eq(ReceiptType::getCode, receiptHeader.getReceiptType());
ReceiptType receiptType = receiptTypeService.getOne(lambdaQueryWrapper);
//流程详情查询
LambdaQueryWrapper<StatusFlowDetail> lambdaStatus = Wrappers.lambdaQuery();
lambdaStatus.orderByAsc(StatusFlowDetail::getSequence);
if(receiptType == null) {
throw new ServiceException("没有对应的入库单类型");
}
String code = createCode(receiptHeader.getReceiptType());
receiptHeader.setCode(code);
receiptHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_BUILD);
receiptHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_BUILD);
receiptHeader.setWarehouseCode(ShiroUtils.getWarehouseCode());
receiptHeader.setCreatedBy(ShiroUtils.getLoginName());
receiptHeader.setLastUpdatedBy(ShiroUtils.getLoginName());
return AjaxResult.toAjax(this.save(receiptHeader));
}
/**
* 生成单号
* @param receiptType
* @return
*/
public String createCode(String receiptType) {
String code = null;
Date now = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
LambdaQueryWrapper<ReceiptHeader> lambda = Wrappers.lambdaQuery();
lambda.select(ReceiptHeader::getCode).eq(ReceiptHeader::getReceiptType, receiptType)
.orderByDesc(ReceiptHeader::getId).last("Limit 1");
//如果指定类型的最后的code存在,并且日期一致。那么 code = 入库单类型 + 年月日 + (排序号 + 1)
String maxCode = null;
ReceiptHeader receiptHeader = this.getOne(lambda);
if ( receiptHeader != null){
maxCode = this.getOne(lambda).getCode();
}
if (maxCode != null && maxCode.substring(maxCode.length() - 13, maxCode.length() - 5).equals(df.format(now))) {
Integer Count = Integer.valueOf(maxCode.substring(maxCode.length() - 5, maxCode.length()));
code = receiptType + df.format(now) + String.format("%05d", Count + 1);
} else {
code = receiptType + df.format(now) + "00001";
}
return code;
}
/**
* 入库预约
* @param receiptHeader
* @return
*/
public AjaxResult reservation(ReceiptHeader receiptHeader) {
LambdaQueryWrapper<ReceiptDetail> receiptDetailLambda = Wrappers.lambdaQuery();
receiptDetailLambda.eq(ReceiptDetail::getReceiptId, receiptHeader.getId());
List<ReceiptDetail> receiptDetails = receiptDetailService.list(receiptDetailLambda);
//更新入库明细流程
for (ReceiptDetail receiptDetail : receiptDetails) {
receiptDetail.setProcessStamp(QuantityConstant.RECEIPT_HEADER_RESERVATION.toString());
if (!receiptDetailService.updateById(receiptDetail)){
throw new ServiceException("更新入库明细处理标记失败");
}
receiptDetail = receiptDetailService.queryflow(receiptDetail);
if (!receiptDetailService.updateById(receiptDetail)){
throw new ServiceException("更新入库明细流程失败");
}
}
receiptHeader.setLastUpdatedBy(ShiroUtils.getLoginName());
//更新入库单预约时间
if (!this.updateById(receiptHeader)){
throw new ServiceException("更新入库单预约时间失败");
}
receiptDetailService.updateReceiptHeaderLastStatus(receiptHeader.getId());
return AjaxResult.success("预约成功");
}
/**
* 入库到货
* @param id
* @return
*/
public AjaxResult arrival(Integer id) {
LambdaQueryWrapper<ReceiptDetail> receiptDetailLambda = Wrappers.lambdaQuery();
receiptDetailLambda.eq(ReceiptDetail::getReceiptId, id)
.eq(ReceiptDetail::getProcessStamp, QuantityConstant.RECEIPT_HEADER_ARRIVAL.toString());
List<ReceiptDetail> receiptDetails = receiptDetailService.list(receiptDetailLambda);
//更新入库明细流程
for (ReceiptDetail receiptDetail : receiptDetails) {
receiptDetailService.queryflow(receiptDetail);
}
//更新入库单头表
receiptDetailService.updateReceiptHeaderLastStatus(id);
return AjaxResult.success("预约成功");
}
/**
* 入库质检
*/
public AjaxResult check(Integer id){
ReceiptHeader receiptHeader = this.getById(id);
LambdaQueryWrapper<ReceiptDetail> receiptDetailLambda = Wrappers.lambdaQuery();
receiptDetailLambda.eq(ReceiptDetail::getReceiptId, id);
List<ReceiptDetail> receiptDetails = receiptDetailService.list(receiptDetailLambda);
//判断是否已经生成了质检单
LambdaQueryWrapper<CheckHeader> checkHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
checkHeaderLambdaQueryWrapper.eq(CheckHeader::getCode, receiptHeader.getCode());
CheckHeader checkHeaderQuery = checkHeaderService.getOne(checkHeaderLambdaQueryWrapper);
if (checkHeaderQuery != null){
throw new ServiceException("质检单已生成");
}
//创建质检头表
CheckHeader checkHeader = new CheckHeader();
checkHeader.setCode(receiptHeader.getCode());
checkHeader.setType(QuantityConstant.CHECK_TYPE_RECEIPT.toString());
checkHeader.setWarehouseCode(ShiroUtils.getWarehouseCode());
checkHeader.setReferCode(receiptHeader.getReferCode());
checkHeader.setEnable(1);
checkHeader.setStatus(QuantityConstant.CHECK_STATUS_BUILD.toString());
checkHeader.setCreatedBy(ShiroUtils.getLoginName());
checkHeader.setLastUpdatedBy(ShiroUtils.getLoginName());
if (!checkHeaderService.save(checkHeader)){
throw new ServiceException("添加质检头表错误");
}
//查询保存后的质检头表
LambdaQueryWrapper<CheckHeader> checkHeaderLambda = Wrappers.lambdaQuery(checkHeader);
checkHeader = checkHeaderService.getOne(checkHeaderLambda);
for (ReceiptDetail receiptDetail : receiptDetails) {
CheckDetail checkDetail = new CheckDetail();
checkDetail.setCheckHeaderId(checkHeader.getId());
checkDetail.setWarehouseCode(ShiroUtils.getWarehouseCode());
checkDetail.setCheckCode(checkHeader.getCode());
checkDetail.setReceiptDetailId(receiptDetail.getId());
checkDetail.setReceiptCode(receiptDetail.getReceiptCode());
checkDetail.setReferCode(checkHeader.getReferCode());
checkDetail.setMaterialCode(receiptDetail.getMaterialCode());
checkDetail.setMaterialName(receiptDetail.getMaterialName());
checkDetail.setMaterialSpec(receiptDetail.getMaterialSpec());
checkDetail.setMaterialUnit(receiptDetail.getMaterialUnit());
checkDetail.setCompanyCode(receiptDetail.getCompanyCode());
checkDetail.setInventorySts(receiptDetail.getInventorySts());
checkDetail.setStatus(QuantityConstant.CHECK_STATUS_BUILD.toString());
checkDetail.setQty(receiptDetail.getTotalQty());
checkDetail.setCheckBy(ShiroUtils.getLoginName());
checkDetail.setCreatedBy(ShiroUtils.getLoginName());
checkDetail.setLastUpdatedBy(ShiroUtils.getLoginName());
if ( !checkDetailService.save(checkDetail) ){
throw new ServiceException("保存质检明细失败");
}
}
return AjaxResult.success("创建质检单成功");
}
/**
* 提交审核入库单
*/
public AjaxResult submitReview(String ids){
if (StringUtils.isEmpty(ids)){
return AjaxResult.error("id不能为空");
}
Integer[] idArray = Convert.toIntArray(ids);
for (int i = 0; i<idArray.length; i++){
ReceiptHeader receiptHeader = this.getById(idArray[i]);
if (receiptHeader.getFirstStatus()>QuantityConstant.RECEIPT_HEADER_MODERATED ||receiptHeader.getLastStatus()>QuantityConstant.RECEIPT_HEADER_MODERATED){
return AjaxResult.error("单据号为"+receiptHeader.getCode()+"入库单不需要审核");
}
receiptHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_MODERATED);
receiptHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_MODERATED);
if (!this.updateById(receiptHeader)){
throw new ServiceException("更新入库头表失败");
} else {
return AjaxResult.success("提交审核成功,请等待审核");
}
}
return AjaxResult.success("提交审核成功");
}
/**
* 到货通知
* @param id
* @return
*/
public AjaxResult arrivalNotice(String id){
LambdaQueryWrapper<ReceiptDetail> lambda = Wrappers.lambdaQuery();
lambda.eq(ReceiptDetail::getReceiptId, id);
List<ReceiptDetail> receiptDetails = receiptDetailService.list(lambda);
for (ReceiptDetail receiptDetail : receiptDetails) {
receiptDetail.setProcessStamp(QuantityConstant.RECEIPT_HEADER_ARRIVAL.toString());
receiptDetailService.updateById(receiptDetail);
if (!receiptDetailService.updateById(receiptDetailService.queryflow(receiptDetail))){
throw new ServiceException("更新入库明细处理标记出错");
}
receiptDetailService.updateReceiptHeaderLastStatus(receiptDetail.getReceiptId());
}
return AjaxResult.success("");
}
/**
* 加入订单池
* @param ids 头表id字符串
* @return
*/
@Transactional
public AjaxResult addPool(String ids){
List<Integer> idList = Arrays.asList(Convert.toIntArray(ids));
for (Integer id : idList){
LambdaQueryWrapper<ReceiptDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
lambdaQueryWrapper.eq(ReceiptDetail::getReceiptId, id);
List<ReceiptDetail> list = receiptDetailService.list(lambdaQueryWrapper);
if (list.size() != 0){
for (ReceiptDetail receiptDetail : list){
if (Integer.parseInt(receiptDetail.getProcessStamp()) < QuantityConstant.RECEIPT_HEADER_POOL){
//判断入库明细是否需要审核
ReceiptDetail beforeReceiptDetail = receiptDetailService.queryflow(receiptDetail);
if (Integer.parseInt(beforeReceiptDetail.getProcessStamp()) == 5 || Integer.parseInt(beforeReceiptDetail.getProcessStamp()) == 100){
throw new ServiceException("请先提交审核");
}
//加入订单池
receiptDetail.setProcessStamp(QuantityConstant.RECEIPT_HEADER_POOL.toString());
ReceiptDetail receiptDetail1 = receiptDetailService.queryflow(receiptDetail);
if (!receiptDetailService.updateById(receiptDetail1)){
throw new ServiceException("更新入库明细状态失败");
}
} else {
throw new ServiceException("已有入库明细进入订单池");
}
}
}
//更新头表状态
receiptDetailService.updateReceiptHeaderLastStatus(id);
}
return AjaxResult.success("加入成功");
}
}