CycleCountService.java
8.08 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
package com.huaheng.api.general.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.api.general.domain.CycCountDomain;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
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.configValue.domain.ConfigValue;
import com.huaheng.pc.config.configValue.service.ConfigValueService;
import com.huaheng.pc.config.cycleCountPreference.service.CycleCountPreferenceService;
import com.huaheng.pc.config.location.service.LocationService;
import com.huaheng.pc.inventory.cycleCountDetail.domain.CycleCountDetail;
import com.huaheng.pc.inventory.cycleCountDetail.service.CycleCountDetailService;
import com.huaheng.pc.inventory.cycleCountHeader.domain.CycleCountHeader;
import com.huaheng.pc.inventory.cycleCountHeader.service.CycleCountHeaderService;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Created by Enzo Cotter on 2019/11/4.
* @author mahuandong
*/
@Component
@Transactional(rollbackFor = Exception.class)
public class CycleCountService {
@Resource
private CycleCountHeaderService cycleCountHeaderService;
@Resource
private CycleCountDetailService cycleCountDetailService;
@Resource
private InventoryDetailService inventoryDetailService;
@Resource
private LocationService locationService;
@Resource
private TaskHeaderService taskHeaderService;
@Resource
private TaskDetailService taskDetailService;
@Resource
private ConfigValueService configValueService;
@Resource
private CycleCountPreferenceService cycleCountPreferenceService;
/**
* 盘点单下发
* @param cycCountDomain 盘点单
* @return 是否下发成功
*/
@Transactional(rollbackFor = Exception.class)
public AjaxResult insertCycleCount(CycCountDomain cycCountDomain){
CycleCountHeader cycleCountHeader =cycCountDomain.getCycleCountHeader();
if (cycleCountHeader==null || cycCountDomain.getInventoryId()==null){
return AjaxResult.error("主单据或库存id不能为空");
}
if (StringUtils.isEmpty(cycCountDomain.getCycleCountHeader().getCountType())){
return AjaxResult.error("盘点类型不能为空");
}
if (StringUtils.isEmpty(cycleCountHeader.getWarehouseCode())){
return AjaxResult.error("仓库编码不能为空");
}
if (StringUtils.isEmpty(cycleCountHeader.getCompanyCode())){
return AjaxResult.error("货主编码不能为空");
}
cycleCountHeader.setWarehouseCode(ShiroUtils.getWarehouseCode());
cycleCountHeader.setCompanyCode(cycleCountHeader.getCompanyCode());
cycleCountHeader.setCreated(new Date());
cycleCountHeader.setCreatedBy(ShiroUtils.getLoginName());
cycleCountHeader.setCode(cycleCountHeaderService.createCode());
if (!cycleCountHeaderService.save(cycleCountHeader)){
return AjaxResult.error("新增盘点单失败");
}
this.addDetails(cycleCountHeader.getCode(),cycCountDomain.getInventoryId());
return AjaxResult.success("成功");
}
/**
* 新增盘点明细
* */
@Transactional
public AjaxResult addDetails(String cycleCountHeadCode, Integer[] inventoryDetailIdIds) {
if(cycleCountHeadCode == null){
throw new ServiceException("盘点主单编码不能为空!");
}
CycleCountHeader cyclecountHeader =new CycleCountHeader();
cyclecountHeader.setCode(cycleCountHeadCode);
LambdaQueryWrapper<CycleCountHeader> ch = Wrappers.lambdaQuery(cyclecountHeader);
cyclecountHeader = cycleCountHeaderService.getOne(ch);
//可能存在并发删除的情况
if(cyclecountHeader==null){
return AjaxResult.error("盘点主单据不存在");
}
if(cyclecountHeader.getStatusCyc() > QuantityConstant.CYCLECOUNT_STATUS_BUILD){
return AjaxResult.error("盘点单非新建状态,无法再添加明细");
}
//默认盘点配置,首选项
ConfigValue configValue = new ConfigValue();
configValue.setModuleType("cyclecount");
configValue.setWarehouseCode(ShiroUtils.getWarehouseCode());
LambdaQueryWrapper<ConfigValue> configValueLambdaQueryWrapper = Wrappers.lambdaQuery(configValue);
configValue = configValueService.getOne(configValueLambdaQueryWrapper);
if(configValue == null){
throw new SecurityException("请先在配置中添加盘点配置");
}
String preferenceCode = configValue.getIdentifier();
//获取已经存在的明细
CycleCountDetail cycleCountDetail = new CycleCountDetail();
cycleCountDetail.setWarehouseCode(cyclecountHeader.getWarehouseCode());
cycleCountDetail.setCompanyCode(cyclecountHeader.getCompanyCode());
cycleCountDetail.setCycleCountHeadCode(cyclecountHeader.getCode());
LambdaQueryWrapper<CycleCountDetail> cycleCountDetailLambd = Wrappers.lambdaQuery(cycleCountDetail);
List<CycleCountDetail> cyclecountDetailList = cycleCountDetailService.list(cycleCountDetailLambd);
if(cyclecountDetailList == null){
cyclecountDetailList = new ArrayList<>();
}
for(int inventoryDetailId : inventoryDetailIdIds){
//当前有盘点明细时验证是否已经生成盘点细单,生成则跳出该条循环
if(cyclecountDetailList.stream().anyMatch(t -> t.getInventoryDetailId()!=null && t.getInventoryDetailId() == inventoryDetailId)){
continue;
}
//查询该条库存明细
InventoryDetail inventoryDetail = inventoryDetailService.getById(inventoryDetailId);
//写入盘点明细
CycleCountDetail ccd = new CycleCountDetail();
ccd.setPreferenceCode(preferenceCode);
ccd.setCycleCountHeadCode(cyclecountHeader.getCode());
ccd.setInventoryDetailId(inventoryDetailId);
ccd.setWarehouseCode(inventoryDetail.getWarehouseCode());
ccd.setCompanyCode(inventoryDetail.getCompanyCode());
ccd.setInventorySts(inventoryDetail.getInventorySts());
ccd.setCountId(inventoryDetail.getId());
//ccd.setEnableStatus(1);
ccd.setLocationCode(inventoryDetail.getLocationCode());
ccd.setContainerCode(inventoryDetail.getContainerCode());
ccd.setMaterialCode(inventoryDetail.getMaterialCode());
ccd.setMaterialName(inventoryDetail.getMaterialName());
ccd.setMaterialSpec(inventoryDetail.getMaterialSpec());
ccd.setMaterialUnit(inventoryDetail.getMaterialUnit());
ccd.setSystemQty(inventoryDetail.getQty());
ccd.setBatch(inventoryDetail.getBatch());
ccd.setLot(inventoryDetail.getLot());
ccd.setProjectNo(inventoryDetail.getProjectNo());
ccd.setCountedBy(cyclecountHeader.getCreatedBy());
ccd.setCountedAt(cyclecountHeader.getCreated());
ccd.setCreated(new Date());
ccd.setCreatedBy(ShiroUtils.getLoginName());
ccd.setLastUpdated(new Date());
ccd.setLastUpdatedBy(ShiroUtils.getLoginName());
cycleCountDetailService.save(ccd);
//更新库存盘点锁
inventoryDetail.setLockCode("cyclecount");
inventoryDetailService.saveOrUpdate(inventoryDetail);
}
return AjaxResult.success("生成盘点明细成功");
}
}