ShipmentDetailServiceImpl.java
13.3 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package com.huaheng.pc.shipment.shipmentDetail.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.exception.service.ServiceException;
import com.huaheng.common.support.Convert;
import com.huaheng.common.utils.DataUtils;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.shipmentPreference.domain.ShipmentPreference;
import com.huaheng.pc.config.shipmentPreference.service.ShipmentPreferenceService;
import com.huaheng.pc.config.statusFlow.domain.StatusFlowDetail;
import com.huaheng.pc.config.statusFlow.domain.StatusFlowHeader;
import com.huaheng.pc.config.statusFlow.service.StatusFlowDetailService;
import com.huaheng.pc.config.statusFlow.service.StatusFlowHeaderService;
import com.huaheng.pc.config.waveMaster.domain.WaveMaster;
import com.huaheng.pc.config.waveMaster.service.WaveMasterService;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
import com.huaheng.pc.shipment.wave.domain.Wave;
import com.huaheng.pc.shipment.wave.service.WaveService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.pc.shipment.shipmentDetail.mapper.ShipmentDetailMapper;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import org.springframework.transaction.annotation.Transactional;
@Service
public class ShipmentDetailServiceImpl extends ServiceImpl<ShipmentDetailMapper, ShipmentDetail> implements ShipmentDetailService{
@Autowired
private ShipmentHeaderService shipmentHeaderService;
@Autowired
private MaterialService materialService;
@Resource
private ShipmentDetailMapper shipmentDetailMapper;
@Autowired
private WaveMasterService waveMasterService;
@Autowired
private WaveService waveService;
@Autowired
private ShipmentPreferenceService shipmentPreferenceService;
@Autowired
private StatusFlowHeaderService statusFlowHeaderService;
@Autowired
private StatusFlowDetailService statusFlowDetailService;
/**
* 新增出库明细
*
* @param shipmentDetail
* @return
*/
@Override
public AjaxResult insertDetail(ShipmentDetail shipmentDetail) {
//查看主单是否存在
LambdaQueryWrapper<ShipmentHeader> lambdaQueryWrapper = Wrappers.lambdaQuery();
lambdaQueryWrapper.eq(ShipmentHeader::getId,shipmentDetail.getShipmentId())
.eq(ShipmentHeader::getWarehouseCode,ShiroUtils.getWarehouseCode());
ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(lambdaQueryWrapper);
if (shipmentHeader == null) {
return AjaxResult.error("找不到主单据");
}
if (shipmentHeader.getFirstStatus() > QuantityConstant.SHIPMENT_HEADER_POOL) {
//表示已经加入了波次
return AjaxResult.error("主单据状态不允许新增明细");
}
//查找物料
LambdaQueryWrapper<Material> lam = Wrappers.lambdaQuery();
lam.eq(Material::getCode,shipmentDetail.getMaterialCode())
.eq(Material::getWarehouseCode,ShiroUtils.getWarehouseCode());
Material material = materialService.getOne(lam);
if (material == null) {
return AjaxResult.error("物料未找到");
}
if (StringUtils.isNotEmpty(material.getCompanyCode()) &&
ShiroUtils.getCompanyCodeList().contains(material.getCompanyCode()) == false)
{
return AjaxResult.error("物料不属于当前货主!");
}
shipmentDetail.setId(null);
shipmentDetail.setWarehouseCode(ShiroUtils.getWarehouseCode());
shipmentDetail.setMaterialCode(material.getCode());
shipmentDetail.setMaterialName(material.getName());
shipmentDetail.setReferCode(shipmentHeader.getReferCode());
shipmentDetail.setReferId(shipmentHeader.getReferId());
shipmentDetail.setMaterialSpec(material.getSpec());
shipmentDetail.setMaterialUnit(material.getUnit());
shipmentDetail.setCreated(null);
shipmentDetail.setCreatedBy(ShiroUtils.getLoginName());
shipmentDetail.setLastUpdated(null);
shipmentDetail.setLastUpdatedBy(ShiroUtils.getLoginName());
if (this.save(shipmentDetail)==true){
//更新单据总行数与总数量
shipmentHeader.setTotalLines(shipmentHeader.getTotalLines() + 1);
shipmentHeader.setTotalQty(shipmentHeader.getTotalQty().add(shipmentDetail.getShipQty()));
shipmentHeaderService.saveOrUpdate(shipmentHeader);
return AjaxResult.success("新增单据明细成功");
}
else
return AjaxResult.error("新增单据明细失败");
}
/**
* 删除出库单据明细
*
* @param id
* @return
*/
@Override
@Transactional
public AjaxResult deleteDetail(String id) {
if (StringUtils.isEmpty(id))
return AjaxResult.error("id不能为空");
String[] ids = id.split(",");
List<Map<String,Integer>> list = shipmentDetailMapper.SelectFirstStatus(id);
if (list.size() < 1) {
return AjaxResult.error("找不到主单据!");
}
if (list.size() > 1) {
return AjaxResult.error("有多个主单据,不能一起删除!");
}
if (list.get(0).get("status") > QuantityConstant.SHIPMENT_HEADER_POOL) {
return AjaxResult.error("单据状进入订单池,不允许删除明细");
}
Integer result = shipmentDetailMapper.batchDelete(ids);
if (result > 0) {
Integer headerId=list.get(0).get("id");
Map<String,String> map= shipmentDetailMapper.StatisticalByReceiptId(headerId);
if(DataUtils.getInteger(map.get("totalLines")) <= 0) {
shipmentHeaderService.removeById(headerId);
}
else {
//更新表头的总行数和总数量统计
ShipmentHeader shipmentHeader = new ShipmentHeader();
shipmentHeader.setId(headerId);
shipmentHeader.setTotalLines(DataUtils.getInteger(map.get("totalLines")));
shipmentHeader.setTotalQty(DataUtils.getBigDecimal(map.get("totalQty")));
shipmentHeaderService.saveOrUpdate(shipmentHeader);
}
return AjaxResult.success("删除单据明细成功");
}
else
return AjaxResult.error("删除单据明细失败");
}
@Override
public Integer countUnCompleted(Integer shipmentId) {
return shipmentDetailMapper.countUnCompleted(shipmentId);
}
/**
* 查看选中的单据状态是否符合出库流程
* 选中的单据加入波次
* 根据选中的主单ID和波次主表的code,判断主单之和是否符合波次的限制条件,
* 看此code的波次是否建成未开始执行,如果是则只需修改波次属性,否则创建新的波次,
* 修改加入波次的子单,修改子单和主单的状态,并修改子单的waveId
*/
@Override
@Transactional
public void saveWave(String ids, String code) {
Integer status = QuantityConstant.SHIPMENT_HEADER_POOL;
List<ShipmentHeader> shipmentHeaderList =shipmentPreferenceService.checkShipmentProcess(ids,status,code);
//找到波次主表,看系统是否有此波次
LambdaQueryWrapper<WaveMaster> lam=Wrappers.lambdaQuery();
lam.eq(WaveMaster::getCode,code)
.eq(WaveMaster::getWarehouseCode,ShiroUtils.getWarehouseCode());
WaveMaster waveMaster=waveMasterService.getOne(lam);
if(waveMaster == null){
throw new ServiceException("系统没有此波次");
}
if(Convert.toIntArray(ids).length >waveMaster.getMaxShipments()){
throw new ServiceException("加入波次的单据数量超过波次的单据限制");
}
List<ShipmentDetail> shipmentDetailList=new ArrayList<>();
BigDecimal qty=new BigDecimal(0);
//检查出库子表是否有处于波次的
for (Integer id : Convert.toIntArray(ids))
{
LambdaQueryWrapper<ShipmentDetail> lamDetail=Wrappers.lambdaQuery();
lamDetail.eq(ShipmentDetail::getWarehouseCode,ShiroUtils.getWarehouseCode())
.eq(ShipmentDetail::getShipmentId,id);
List<ShipmentDetail> shipmentDetails=this.list(lamDetail);
if(shipmentDetails == null || shipmentDetails.size() == 0){
throw new ServiceException("系统没有主单id为"+id+"的子单");
}
//查看是否有单据处于波次中
for(ShipmentDetail shipmentDetail : shipmentDetails) {
if (shipmentDetail.getWaveId() != 0) {
throw new ServiceException("主单id为" + id + "子单id为" + shipmentDetail.getId() + "的子单已加入波次,不可再加入波次");
}
if(shipmentDetail.getShipQty().compareTo(shipmentDetail.getRequestQty())!=0) {
shipmentDetailList.add(shipmentDetail);
qty = qty.add(shipmentDetail.getShipQty().subtract(shipmentDetail.getRequestQty()));
}
}
}
if(shipmentDetailList.size()>waveMaster.getMaxLines()){
throw new ServiceException("加入波次的总行数超过波次的行数限制");
}
Boolean flag=false;
//查看波次是否建成未执行
LambdaQueryWrapper<Wave> waveLam = Wrappers.lambdaQuery();
waveLam.eq(Wave::getStatus,QuantityConstant.WAVE_STATUS_BUILD)
.eq(Wave::getWaveMode,code)
.eq(Wave::getWarehouseCode,ShiroUtils.getWarehouseCode());
Wave wave = waveService.getOne(waveLam);
if(wave != null && (wave.getTotalShipments()+Convert.toIntArray(ids).length)<= waveMaster.getMaxShipments()
&& (wave.getTotalLines()+shipmentDetailList.size()<= waveMaster.getMaxLines())){
//修改波次
wave.setTotalShipments(Convert.toIntArray(ids).length + wave.getTotalShipments());
wave.setTotalLines(shipmentDetailList.size() + wave.getTotalLines());
wave.setTotalQty(qty.add(wave.getTotalQty()));
wave.setLastUpdatedBy(ShiroUtils.getLoginName());
}else {
//创建波次
wave=new Wave();
wave.setWarehouseCode(ShiroUtils.getWarehouseCode());
wave.setMasterCode(code);
wave.setWaveName(waveMaster.getName());
wave.setStatus(QuantityConstant.WAVE_STATUS_BUILD);
wave.setCurrentWaveStep(QuantityConstant.WAVE_STATUS_BUILD.toString());
wave.setTotalShipments(Convert.toIntArray(ids).length);
wave.setTotalLines(shipmentDetailList.size());
wave.setTotalQty(qty);
wave.setCreatedBy(ShiroUtils.getLoginName());
flag = waveService.save(wave);
if (flag == false) {
throw new ServiceException("波次建立失败");
}
}
//修改出库子单,加入波次ID,并修改状态为波次
for(ShipmentDetail shipmentDetail :shipmentDetailList){
shipmentDetail.setWaveId(wave.getId());
if(shipmentDetail.getStatus()<QuantityConstant.SHIPMENT_HEADER_WAVE) {
shipmentDetail.setStatus(QuantityConstant.SHIPMENT_HEADER_WAVE);
}
}
flag = this.updateBatchById(shipmentDetailList);
if(flag == false){
throw new ServiceException("出库子单加入波次失败");
}
for(ShipmentHeader shipmentHeader :shipmentHeaderList){
shipmentHeader.setLastStatus(QuantityConstant.SHIPMENT_HEADER_WAVE);
}
flag = shipmentHeaderService.updateBatchById(shipmentHeaderList);
if(flag == false){
throw new ServiceException("修改主单状态失败");
}
}
//查看最高状态和最低状态
@Override
public Map<String,Integer> selectStatus(Integer id) {
return shipmentDetailMapper.selectStatus(id);
}
@Override
public List<TaskDetail> getShipmentQtyLast7Days() {
return shipmentDetailMapper.getShipmentQtyLast7Days();
}
@Override
public List<TaskDetail> getWarehouseShipment() {
return shipmentDetailMapper.getWarehouseShipment();
}
@Override
public List<TaskDetail> getCompanyShipment() {
return shipmentDetailMapper.getCompanyShipment();
}
@Override
public Boolean insertDetails(List<ShipmentDetail> shipmentDetails) {
int flag = shipmentDetailMapper.insertDetails(shipmentDetails);
if(flag > 0){
return true;
}else {
return false;
}
}
}