AdjustService.java
9.58 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
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.AdjustDomain;
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.check.checkDetail.domain.CheckDetail;
import com.huaheng.pc.check.checkDetail.service.CheckDetailService;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.inventory.adjustDetail.domain.AdjustDetail;
import com.huaheng.pc.inventory.adjustDetail.service.AdjustDetailService;
import com.huaheng.pc.inventory.adjustHeader.domain.AdjustHeader;
import com.huaheng.pc.inventory.adjustHeader.mapper.AdjustHeaderMapper;
import com.huaheng.pc.inventory.adjustHeader.service.AdjustHeaderService;
import com.huaheng.pc.inventory.cycleCountDetail.domain.CycleCountDetail;
import com.huaheng.pc.inventory.cycleCountDetail.service.CycleCountDetailService;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
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 AdjustService {
@Resource
private AdjustHeaderMapper adjustHeaderMapper;
@Resource
private AdjustDetailService adjustDetailService;
@Resource
private AdjustHeaderService adjustHeaderService;
@Resource
private InventoryDetailService inventoryDetailService;
@Resource
private CycleCountDetailService cycleCountDetailService;
@Resource
private CheckDetailService checkDetailService;
@Resource
private MaterialService materialService;
/**
* 调整单下发
* @param adjustDomain 调整单
* @return 是否下发成功
*/
@Transactional(rollbackFor = Exception.class)
public AjaxResult insertAdjust(AdjustDomain adjustDomain){
AdjustHeader adjustHeader =adjustDomain.getAdjustHeader();
List<AdjustDetail> adjustDetailList =adjustDomain.getAdjustDetailList();
if (adjustHeader==null || adjustDetailList==null){
return AjaxResult.error("主单据和单据明细信息不能为空");
}
if (StringUtils.isEmpty(adjustDomain.getAdjustHeader().getProblemType())){
return AjaxResult.error("调整类型不能为空");
}
if (StringUtils.isEmpty(adjustDomain.getAdjustHeader().getWarehouseCode())){
return AjaxResult.error("仓库编码不能为空");
}
if (StringUtils.isEmpty(adjustDomain.getAdjustHeader().getCompanyCode())){
return AjaxResult.error("货主编码不能为空");
}
AdjustHeader adjustHeaderCode= this.addSave(adjustDomain.getAdjustHeader());
//查询主单及加入仓库和货主,盘点,质检单编码
if(adjustHeaderCode == null){
return AjaxResult.error("没有对应的主单据!");
}
List<AdjustDetail> adjustDetails =new ArrayList<>();
for (AdjustDetail detail:adjustDetailList) {
detail.setWarehouseCode(adjustHeaderCode.getWarehouseCode());
detail.setAdjustCode(adjustHeaderCode.getCode());
detail.setCompanyCode(adjustHeaderCode.getCompanyCode());
detail.setCycleCountHeaderCode(adjustHeaderCode.getCycleCountHeaderCode());
detail.setCheckCode(adjustHeaderCode.getCheckCode());
this.addDetails(detail);
adjustDetails.add(detail);
}
AdjustDomain adjustDomainAjax =new AdjustDomain();
adjustDomainAjax.setAdjustHeader(adjustHeaderCode);
adjustDomainAjax.setAdjustDetailList(adjustDetails);
return AjaxResult.success(adjustDomainAjax);
}
/**
* 保存新增调整头
* @param adjustHeader
* @return
*/
@Transactional
public AdjustHeader addSave(AdjustHeader adjustHeader) {
//校验调整类型
//盘点调整,质检调整需要和盘点和质检单据确认。
switch (adjustHeader.getProblemType()){
case "checkAdjust": //质检调整
if(StringUtils.isEmpty(adjustHeader.getCheckCode())){
throw new SecurityException("选择单据类型为质检调整时,质检单编码不能为空!");
}
break;
case "cyclecountAdjust": //盘点调整
if(StringUtils.isEmpty(adjustHeader.getCycleCountHeaderCode())){
throw new SecurityException("选择单据类型为盘点调整时,盘点单编码不能为空!");
}
break;
case "adjust": //普通调整
if(StringUtils.isEmpty(adjustHeader.getCycleCountHeaderCode())){
throw new SecurityException("选择单据类型为普通调整时,盘点单编码不能为空!");
}
break;
default:
throw new ServiceException("类型错误!");
}
adjustHeader.setCreated(new Date());
adjustHeader.setCreatedBy(ShiroUtils.getLoginName());
adjustHeader.setCode(this.createCode());
if (!adjustHeaderService.save(adjustHeader)){
throw new SecurityException("新增调整单失败!");
}
return adjustHeader;
}
@Transactional
public AjaxResult addDetails(AdjustDetail adjustDetail) {
//查询主单据
AdjustHeader adjustHeader = new AdjustHeader();
adjustHeader.setCode(adjustDetail.getAdjustCode());
adjustHeader.setWarehouseCode(adjustDetail.getWarehouseCode());
adjustHeader.setCompanyCode(adjustDetail.getCompanyCode());
LambdaQueryWrapper<AdjustHeader> lambdaQueryWrapper = Wrappers.lambdaQuery(adjustHeader);
adjustHeader = adjustHeaderService.getOne(lambdaQueryWrapper);
//检查库存
if((adjustDetail.getInventoryDetailId()) != null){
InventoryDetail inventoryDetail = inventoryDetailService.getById(adjustDetail.getInventoryDetailId());
if(inventoryDetail == null){
return AjaxResult.error("没有该条库存明细");
}
}
//明细单据的上层单编码需要和主单一直,主单空值则不判断
if(StringUtils.isNotEmpty(adjustHeader.getCycleCountHeaderCode()) || StringUtils.isNotEmpty(adjustHeader.getCheckCode())){
adjustDetail.setCycleCountHeaderCode(adjustHeader.getCycleCountHeaderCode());
adjustDetail.setCheckCode(adjustHeader.getCheckCode());
}
//检查盘点单
if(StringUtils.isNotEmpty(adjustDetail.getCycleCountHeaderCode())){
LambdaQueryWrapper<CycleCountDetail> queryCycleCountCode = Wrappers.lambdaQuery();
queryCycleCountCode.eq(CycleCountDetail::getCycleCountHeadCode,adjustDetail.getCycleCountHeaderCode());
CycleCountDetail cycleCountDetail = cycleCountDetailService.list(queryCycleCountCode).get(0);
if(cycleCountDetail == null || !cycleCountDetail.getCycleCountHeadCode().equals(adjustDetail.getCycleCountHeaderCode())){
return AjaxResult.error("盘点单错误,请核对盘点单据!");
}
}
//检查质检单
if(StringUtils.isNotEmpty(adjustDetail.getCheckCode())){
CheckDetail checkDetail = checkDetailService.getById(adjustDetail.getCheckDetailId());
if(checkDetail == null || !checkDetail.getCheckCode().equals(adjustDetail.getCheckCode())){
return AjaxResult.error("质检单错误,请核对质检单据!");
}
}
//检查物料
Material material = materialService.getMaterialByCode(adjustDetail.getMaterialCode());
if(material == null){
return AjaxResult.error("物料编码错误!");
}
adjustDetail.setProblemType(adjustHeader.getProblemType());//调整类型
adjustDetail.setMaterialUnit(material.getUnit());
adjustDetail.setMaterialSpec(material.getSpec());
adjustDetail.setCreated(new Date());
adjustDetail.setCreatedBy(ShiroUtils.getLoginName());
adjustDetail.setLastUpdatedBy(ShiroUtils.getLoginName());
adjustDetail.setLastUpdated(new Date());
adjustDetailService.saveOrUpdate(adjustDetail);
return AjaxResult.success("新增调整明细成功!");
}
//生成差异单号
public String createCode() {
String code;
Date now = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
String maxCode = adjustHeaderMapper.getLastCode();;
//如果指定类型的最后的code存在,并且日期一致。那么 code = 单类型 + 年月日 + (排序号 + 1)
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 = "AD" + df.format(now) + String.format("%05d", Count + 1);
}
else
{
code = "AD" + df.format(now) + "00001";
}
return code;
}
}