MaterialServiceImpl.java
14.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package com.huaheng.pc.config.material.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.huaheng.common.utils.DateUtils;
import com.huaheng.common.utils.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.common.exception.BusinessException;
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.mobile.shipment.Materialforecast;
import com.huaheng.pc.config.company.domain.Company;
import com.huaheng.pc.config.company.service.CompanyService;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.mapper.MaterialMapper;
import com.huaheng.pc.config.materialMultiple.domain.MaterialMultiple;
import com.huaheng.pc.config.materialType.domain.MaterialType;
import com.huaheng.pc.config.materialType.service.MaterialTypeService;
import com.huaheng.pc.config.materialUnit.domain.MaterialUnit;
import com.huaheng.pc.config.materialUnit.service.MaterialUnitService;
import com.huaheng.pc.config.supplier.service.SupplierSyncService;
import com.huaheng.pc.config.syncTime.domain.SyncTime;
import com.huaheng.pc.config.syncTime.service.SyncTimeService;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.srm.domain.SrmHeader;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;
import javax.annotation.Resource;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.pc.config.material.mapper.MaterialMapper;
import com.huaheng.pc.config.material.domain.Material;
import org.springframework.transaction.annotation.Transactional;
@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements MaterialService{
@Resource
private ReceiptDetailService receiptDetailService;
@Resource
private MaterialUnitService materialUnitService;
@Resource
private MaterialTypeService materialTypeService;
@Resource
private MaterialMapper materialMapper;
@Resource
private InventoryDetailService inventoryDetailService;
@Resource
private CompanyService companyService;
@Resource
private SyncTimeService syncTimeService;
@Override
public AjaxResult removeByIds(String ids) {
int count = 0;
for (Integer id : Convert.toIntArray(ids)) {
Material material = this.getById(id);
LambdaQueryWrapper<ReceiptDetail> lambda = Wrappers.lambdaQuery();
lambda.eq(ReceiptDetail::getWarehouseCode, ShiroUtils.getWarehouseCode())
.eq(ReceiptDetail::getMaterialCode, material.getCode())
.eq(ReceiptDetail::getDeleted, false)
.last("LIMIT 1");
Map<String, Object> map = receiptDetailService.getMap(lambda);
if (map != null) {
return AjaxResult.error("物料编码(" + material.getCode() +")存在入库单,不能删除!");
}
material.setDeleted(true);
material.setLastUpdatedBy(ShiroUtils.getLoginName());
if (this.updateById(material)){
count++;
}
}
return AjaxResult.success("成功删除"+count+"条记录");
}
/**
* 导入物料数据
*
* @param materialList 用户数据列表
* @param operName 操作用户
* @return 结果
*/
@Override
public String importMaterial(List<Material> materialList, Boolean isUpdateSupport, String operName) {
if (StringUtils.isNull(materialList) || materialList.size() == 0) {
throw new BusinessException("导入数据不能为空!");
}
int successNum = 0;
int failureNum = 0;
StringBuilder successMsg = new StringBuilder();
StringBuilder failureMsg = new StringBuilder();
for (Material material : materialList) {
try {
LambdaQueryWrapper<Material> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(Material::getCode, material.getCode());
lambdaQueryWrapper.last("limit 1");
// 验证是否存在这个用户
Material m = this.getOne(lambdaQueryWrapper);
if (StringUtils.isNull(m)) {
// System.out.println(ShiroUtils.getLoginName());
material.setCreatedBy(ShiroUtils.getUser().getLoginName());
material.setLastUpdatedBy(ShiroUtils.getLoginName());
this.save(material);
successNum++;
successMsg.append("<br/>" + successNum + "、编码 " + material.getCode() + " 导入成功");
}
} catch (Exception e) {
failureNum++;
String msg = "<br/>" + failureNum + "、编码" + material.getCode() + " 导入失败:";
failureMsg.append(msg + e.getMessage());
log.error(msg, e);
}
}
if (failureNum > 0) {
failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
throw new BusinessException(failureMsg.toString());
} else {
successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
}
return successMsg.toString();
}
/**
* 添加物料
* @param material
* @return
*/
@Override
public AjaxResult addSave(Material material) {
LambdaQueryWrapper<Material> lambda = new LambdaQueryWrapper<>();
//如果物料编码为空则生成物料编码
if (material.getCode() == null) {
material.setCode(createCode(material.getType()));
}
lambda.eq(Material::getCode, material.getCode())
// .eq(Material::getWarehouseCode, ShiroUtils.getWarehouseCode())
.eq(Material::getDeleted, false);
lambda.last("limit 1");
Map<String, Object> map = this.getMap(lambda);
if (map != null) {
return AjaxResult.error("物料已经存在");
}
LambdaQueryWrapper<MaterialUnit> lambdaQueryWrapper = Wrappers.lambdaQuery();
lambdaQueryWrapper.eq(MaterialUnit::getMaterialCode, material.getCode())
.eq(MaterialUnit::getUnit, material.getUnit());
//如果不存在该物料的单位是新建物料单位
if (materialUnitService.getOne(lambdaQueryWrapper) == null){
MaterialUnit materialUnit = new MaterialUnit();
materialUnit.setMaterialCode(material.getCode());
materialUnit.setMaterialName(material.getName());
materialUnit.setMaterialSpec(material.getSpec());
materialUnit.setCompanyCode(material.getCompanyCode());
materialUnit.setWarehouseCode(ShiroUtils.getWarehouseCode());
materialUnit.setUnit(material.getUnit());
materialUnit.setCreatedBy(ShiroUtils.getLoginName());
materialUnit.setLastUpdatedBy(ShiroUtils.getLoginName());
materialUnitService.save(materialUnit);
}
material.setWarehouseCode(ShiroUtils.getWarehouseCode());
material.setCreatedBy(ShiroUtils.getLoginName());
material.setLastUpdatedBy(ShiroUtils.getLoginName());
Boolean flag=this.save(material);
if(flag==false){
return AjaxResult.error("新增物料失败,存入数据库时报错");
}
return AjaxResult.success("新增物料成功");
}
@Override
public Material getMaterialByCode(String code) {
// String warehouseCode = ShiroUtils.getWarehouseCode();
LambdaQueryWrapper<Material> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Material::getCode, code);
queryWrapper.orderByAsc(Material::getId);
queryWrapper.last("limit 1");
// queryWrapper.eq(Material::getWarehouseCode, warehouseCode);
Material material = getOne(queryWrapper);
return material;
}
@Override
public Material getMaterialByCode(String code, String warehouseCode) {
LambdaQueryWrapper<Material> queryWrapper =new LambdaQueryWrapper<>();
queryWrapper.eq(Material::getCode, code);
// queryWrapper.eq(Material::getWarehouseCode, warehouseCode);
queryWrapper.last("limit 1");
Material material = getOne(queryWrapper);
return material;
}
@Override
public Material getMaterialByDomain(Material material) {
LambdaQueryWrapper<Material> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(StringUtils.isNotEmpty(material.getCode()),Material::getCode, material.getCode());
queryWrapper.eq(StringUtils.isNotEmpty(material.getBarcode()),Material::getBarcode, material.getBarcode());
queryWrapper.eq(StringUtils.isNotEmpty(material.getWarehouseCode()),Material::getWarehouseCode, material.getWarehouseCode());
queryWrapper.last("limit 1");
return getOne(queryWrapper);
}
@Override
public List<Material> findLatest() {
return materialMapper.findLatest();
}
/**
* 获得物料联想词
*
* @param code
* @return
*/
@Override
public List<Materialforecast> getMaterialForecast(String code) {
List<Materialforecast> forecast = new ArrayList<>();
LambdaQueryWrapper<InventoryDetail> detailLambdaQueryWrapper = Wrappers.lambdaQuery();
detailLambdaQueryWrapper.eq(InventoryDetail::getMaterialCode, code)
.eq(InventoryDetail::getWarehouseCode, ShiroUtils.getWarehouseCode())
.in(InventoryDetail::getCompanyCode,ShiroUtils.getCompanyCodeList());
List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(detailLambdaQueryWrapper);
if (!inventoryDetailList.isEmpty()) {
for (InventoryDetail inventoryDetail : inventoryDetailList) {
Materialforecast materialforecast = new Materialforecast();
materialforecast.setMaterialCode(inventoryDetail.getMaterialCode());
materialforecast.setType(0);
forecast.add(materialforecast);
}
} else {
detailLambdaQueryWrapper = Wrappers.lambdaQuery();
detailLambdaQueryWrapper.like(InventoryDetail::getMaterialName, code)
.eq(InventoryDetail::getWarehouseCode, ShiroUtils.getWarehouseCode())
.in(InventoryDetail::getCompanyCode,ShiroUtils.getCompanyCodeList());
List<InventoryDetail> inventoryDetailList1 = inventoryDetailService.list(detailLambdaQueryWrapper);
if (!inventoryDetailList1.isEmpty()) {
for (InventoryDetail inventoryDetail : inventoryDetailList) {
Materialforecast materialforecast = new Materialforecast();
materialforecast.setMaterialCode(inventoryDetail.getMaterialCode());
materialforecast.setType(0);
forecast.add(materialforecast);
}
}
}
return null;
}
/**
* 生成物料编码
* @param code
* @return
*/
private String createCode(String code){
LambdaQueryWrapper<MaterialType> lambda = Wrappers.lambdaQuery();
lambda.eq(MaterialType::getCode, code);
MaterialType materialType = materialTypeService.getOne(lambda);
return String.format(materialType.getAutoGenSerialNumFormat(), materialType.getTrackSerialNum());
}
/**
* 物料表删除多余的code,有英文单位的留下英文单位的,每个code保留一个
* @return
*/
@Override
@Transactional(rollbackFor = Exception.class)
public int cleanDocuments() {
//查找过期的出库单据
String syncName="deleteMaterial";
SyncTime syncTime=syncTimeService.getTimeByName(syncName);
if(syncTime==null){
return 0;
}
String codeStart=syncTime.getCodeStart();
String codeEnd="";
if(StringUtils.isNotEmpty(codeStart)) {
long number = 0l;
try {
number = Long.parseLong(codeStart); // 将字符串转换为11位数字的长整型
number += 100; // 加上100
codeEnd=String.valueOf(number);
} catch (NumberFormatException e) {
}
}
LambdaQueryWrapper<Material> wrapper=new LambdaQueryWrapper<>();
wrapper.ge(Material::getId,codeStart);
wrapper.le(Material::getId,codeEnd);
// wrapper.last("limit 20");
// wrapper.groupBy(Material::getCode);
List<Material> list=this.list(wrapper);
if(list==null||list.size()<=0){
syncTimeService.updateCodeStart(syncName,codeEnd);
return 0;
}
for (Material material : list) {
LambdaQueryWrapper<Material> wrapper1=new LambdaQueryWrapper<>();
wrapper1.eq(Material::getCode,material.getCode());
wrapper1.orderByAsc(Material::getId);
List<Material> codelist=this.list(wrapper1);
List<Integer> ids=new ArrayList<>();
if(codelist!=null&&codelist.size()<=1){
continue;
}
boolean tag=false;
for (Material material1 : codelist){
Boolean flag= StringUtils.isChineseChar(material1.getUnit());
if(flag){
//中文单位
ids.add(material1.getId());
}else{
tag=true;
}
}
if(ids.size()<=0){
continue;
}
if(!tag){
//全部是中文单位则剔除第1个
ids.remove(0);
}
if(ids.size()<=0){
continue;
}
// if(ids.size()>=3){
// ids.remove(0);
// }
// String result = ids.stream()
// .map(Object::toString)
// .collect(Collectors.joining(","));
// this.removeByIds(result);
this.removeByIds(ids);
}
syncTimeService.updateCodeStart(syncName,codeEnd);
return 1;
}
}