MaterialSyncService.java
4.92 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
package com.huaheng.pc.config.material.service;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.pc.config.address.domain.Address;
import com.huaheng.pc.config.address.service.AddressService;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.domain.MaterialErp;
import com.huaheng.pc.config.material.mapper.MaterialMapper;
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 org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.pc.config.customer.domain.Customer;
import com.huaheng.pc.config.supplier.domain.Supplier;
import com.huaheng.pc.config.supplier.domain.SupplierErp;
/**
* 定时任务同步erp的供应商、客户
*/
@Service
public class MaterialSyncService extends ServiceImpl<MaterialMapper, Material>{
@Resource
private MaterialService materialService;
@Resource
private MaterialErpService materialErpService;
@Resource
private SyncTimeService syncTimeService;
@Resource
private AddressService addressService;
@Resource
private SupplierSyncService supplierSyncService;
/**
* 获取erp物料数据,同步到wms数据库
*/
public void syncMaterialData(Integer materialTime){
String syncName="material";
Date date=new Date();
Boolean tag=syncTimeService.isSync(syncName,materialTime);
if(!tag){
// return;
}
SyncTime syncTime=supplierSyncService.getTimeByName(syncName);
Date syncTimes=syncTime.getSyncTimes();
//判断synctime如果超过2小时则设置结束时间,否则结束时间为当前时间
if((date.getTime()-syncTimes.getTime())>1*24*60*60*1000){
Instant instant = syncTimes.toInstant();
// 将Instant往前推移10分钟
Instant newInstant = instant.plus(1, ChronoUnit.DAYS);
// 将新的Instant转换回Date
Date newDate = Date.from(newInstant);
syncTime.setEndTime(newDate);
date=syncTime.getEndTime();
}
String codeStart=syncTime.getCodeStart();
String codeEnd="";
if(StringUtils.isNotEmpty(codeStart)) {
long number = 0l;
try {
number = Long.parseLong(codeStart); // 将字符串转换为11位数字的长整型
number += 100000000; // 加上100000
codeEnd=String.valueOf(number);
// System.out.println("转换后的数字是: " + number); // 输出结果
} catch (NumberFormatException e) {
}
}
Address address=addressService.selectAddressByParam("ERP", QuantityConstant.WAREHOUSE_CS);
List<MaterialErp> list=materialErpService.findMaterialData(syncTime.getEndTime(),address,syncTime.getSyncTimes(),codeStart,codeEnd);
System.out.println("=======物料======="+new Date()+"==========="+list.size());
saveAndUpdateData(list);
if(StringUtils.isNotEmpty(codeStart)){
syncTimeService.updateCodeStart("material",codeEnd);
}else{
syncTimeService.updateStatus("material",1,date);
}
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void saveAndUpdateData(List<MaterialErp> list){
if(list==null||list.size()==0){
return ;
}
List<Material> lists=new ArrayList<>();
for (int a=0;a<list.size();a++){
MaterialErp materialErp=list.get(a);
Material material=new Material();
String code=materialErp.getImaa001();
//查找数据库有无该供应商,有则跳过,无则新增
Material material1 = materialService.getMaterialByCode(code);
if(material1!=null){
material.setId(material1.getId());
// continue;
}else{
material.setType(materialErp.getImaa004());
}
material.setCode(code);
material.setName(materialErp.getImaal003());
if(StringUtils.isEmpty(materialErp.getImaal003())){
material.setName(code);
}
material.setSpec(materialErp.getImaal004());
material.setUnit(materialErp.getImaa006());
material.setUserDef4(materialErp.getOocal003());
lists.add(material);
}
if(lists!=null&&lists.size()>0){
materialService.saveOrUpdateBatch(lists);
}
}
}