SnPartApiService.java
6.65 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
package com.huaheng.pc.config.sn.service;
import javax.annotation.Resource;
import com.alibaba.fastjson.JSONArray;
import com.huaheng.common.utils.DateUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResultLisenes;
import com.huaheng.pc.config.sn.domain.*;
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 com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.http.OkHttpUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.address.domain.Address;
import com.huaheng.pc.config.address.service.AddressService;
import com.huaheng.pc.config.sn.mapper.SnPartDetailMapper;
import org.springframework.transaction.annotation.Transactional;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class SnPartApiService {
@Resource
private SnService snService;
@Resource
private SnPartDetailService snPartDetailService;
@Resource
private AddressService addressService;
@Resource
private SyncTimeService syncTimeService;
@Resource
private SupplierSyncService supplierSyncService;
public AjaxResult getSnByLicenes(){
String syncName="sn";
SyncTime syncTime=supplierSyncService.getTimeByName(syncName);
Date syncTimes=syncTime.getSyncTimes();
Date date=new Date();
// 将Date转换为Instant
Instant instant = syncTimes.toInstant();
// 将Instant往前推移10分钟
Instant newInstant = instant.minus(5, ChronoUnit.MINUTES);
// 将新的Instant转换回Date
Date newDate = Date.from(newInstant);
String newDatestr=DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",newDate);
getSnLicenes(null,newDatestr);
syncTimeService.updateStatus(syncName,1,date);
return AjaxResult.success();
}
public AjaxResult snToIot(){
List<Sn> snList=snService.getSnToLot();
snPartToIot(snList);
// for (Sn sn:snList){
// snPartToIot(sn);
// }
return AjaxResult.success();
}
/**
* 从授权服务器获取sn
* @return
*/
@Transactional
public AjaxResult getSnLicenes(String snCode,String startTime){
Address address=addressService.selectAddressByParam("SHOUQUAN",QuantityConstant.WAREHOUSE_CS);
String url=address.getUrl();
SnLicenesRequst snRequst=new SnLicenesRequst();
if(StringUtils.isNotEmpty(snCode)){
snRequst.setSn(snCode);
}
if(StringUtils.isNotEmpty(startTime)){
snRequst.setStartTime(startTime);
}
String jsonString = JSONObject.toJSONString(snRequst);
//请求授权服务器获取sn
String result= OkHttpUtils.bodypost(url,jsonString, QuantityConstant.WAREHOUSE_CS,"从授权服务器获取sn");
if (StringUtils.isEmpty(result)) {
return AjaxResult.error("接口地址错误或服务器连接不到或返回为空");
}
AjaxResultLisenes ajaxResult= JSON.parseObject(result, AjaxResultLisenes.class);
if(ajaxResult.hasErr()){
return AjaxResult.error(ajaxResult.getMsg());
}
List<SnLicenesResponse> results=(List<SnLicenesResponse>) ajaxResult.getResult();
List<Sn> list=new ArrayList<>();
//保存sn号
for (SnLicenesResponse sn:results) {
if(sn.getSn()==null){
continue;
}
Sn sn1=snService.getSn(sn.getSn());
if(sn1!=null){
continue;
}
Sn sn2=new Sn();
sn2.setCode(sn.getSn());
sn2.setCorrelatedCode(sn.getCorrelatedCode());
sn2.setCreatedBy(ShiroUtils.getLoginName());
sn2.setLastUpdatedBy(ShiroUtils.getLoginName());
list.add(sn2);
}
snService.saveBatch(list);
return AjaxResult.success();
}
/**
* sn部件传递到iot
* @return
*/
@Transactional
public AjaxResult snPartToIot(List<Sn> sns){
SnIotResqust snIotResqust=new SnIotResqust();
snIotResqust.setTypes("I");
List<SnLicenesResponse> snLicenesResponses=new ArrayList<>();
List<Sn> snList=new ArrayList<>();
for (Sn sn:sns){
List<SnPartDetail> snPartDetails=snPartDetailService.getSnPartByCode(sn.getCode());
if(snPartDetails.size()<=0){
// return AjaxResult.error(sn.getCode()+"没有部件详情");
continue;
}
List<PartIOTDomain> parts=new ArrayList<>();
for (SnPartDetail snPartDetail:snPartDetails){
PartIOTDomain partIOTDomain=new PartIOTDomain();
partIOTDomain.setPartCode(snPartDetail.getPart());
parts.add(partIOTDomain);
}
SnLicenesResponse snLicenesResponse=new SnLicenesResponse();
snLicenesResponse.setSn(sn.getCode());
snLicenesResponse.setCorrelatedCode(sn.getCorrelatedCode());
snLicenesResponse.setComponents(parts);
snLicenesResponses.add(snLicenesResponse);
snList.add(sn);
}
if(snLicenesResponses.size()<=0){
return AjaxResult.success();
}
snIotResqust.setResult(snLicenesResponses);
String jsonString = JSONObject.toJSONString(snIotResqust);
Address address=addressService.selectAddressByParam("IOT",QuantityConstant.WAREHOUSE_CS);
String url=address.getUrl();
//请求授权服务器获取sn
String result1= OkHttpUtils.bodypost(url,jsonString, QuantityConstant.WAREHOUSE_CS,"从授权服务器获取sn");
if (StringUtils.isEmpty(result1)) {
return AjaxResult.error("接口地址错误或服务器连接不到或返回为空");
}
//得到string 类型json
result1=result1.substring(1,result1.length()-1);
result1=result1.replaceAll("\\\\", "");
AjaxResultLisenes ajaxResult= JSON.parseObject(result1, AjaxResultLisenes.class);
if(ajaxResult.hasErr()){
return AjaxResult.error(ajaxResult.getMsg());
}
for (Sn sn:snList){
sn.setSyncIot(1);
snService.updateById(sn);
}
return AjaxResult.success();
}
}