|
package org.springblade.mdm.flow.service;
|
|
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONObject;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springblade.core.tool.utils.DateUtil;
|
import org.springblade.mdm.flow.entity.MesSync;
|
import org.springblade.mdm.flow.mapper.MesSyncMapper;
|
import org.springblade.mdm.flow.props.MesInterfaceProperties;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import java.net.URI;
|
import java.net.http.HttpClient;
|
import java.net.http.HttpRequest;
|
import java.net.http.HttpResponse;
|
import java.time.Duration;
|
import java.util.Arrays;
|
import java.util.Collections;
|
import java.util.List;
|
|
/**
|
* MES固化同步 处理服务
|
*
|
* @author yangys
|
*/
|
@Slf4j
|
@Service
|
public class MesSyncService extends ServiceImpl<MesSyncMapper, MesSync> {
|
@Autowired
|
private MesInterfaceProperties mesInterfaceProperties;
|
/**
|
* 同步数据到mes
|
*/
|
@Transactional
|
public void syncToMes(){
|
List<MesSync> syncList = lambdaQuery().ne(MesSync::getStatus,MesSync.STATUS_SUCCESS)
|
.lt(mesInterfaceProperties.getMaxTryTimes()>0,MesSync::getTryTimes,mesInterfaceProperties.getMaxTryTimes())
|
.last("limit 100").list();
|
if(syncList.isEmpty()){
|
log.info("mes同步表无数据,返回。");
|
return;
|
}
|
JSONObject bodyJson = buildRequestJson(syncList);
|
int code = postData(bodyJson);
|
|
int status;
|
if(code == 200){
|
status = MesSync.STATUS_SUCCESS;
|
}else{
|
status = MesSync.STATUS_FAILED;
|
}
|
syncList.forEach(sync->{
|
sync.setStatus(codeToStatus(code));
|
sync.increaseTryTimes();
|
});
|
updateBatchById(syncList);
|
}
|
|
int postData(JSONObject bodyJson){
|
HttpClient client = HttpClient.newBuilder()
|
.connectTimeout(Duration.ofSeconds(10))
|
.build();
|
|
// 构建JSON请求体
|
String json = bodyJson.toJSONString();
|
|
HttpRequest request = HttpRequest.newBuilder()
|
.uri(URI.create(mesInterfaceProperties.getOperationUpdateEndpoint()))
|
.header("Content-Type", "application/json")
|
.header("Accept", "application/json")
|
.POST(HttpRequest.BodyPublishers.ofString(json))
|
.build();
|
|
try {
|
HttpResponse<String> response = client.send(
|
request, HttpResponse.BodyHandlers.ofString());
|
|
System.out.println("Status Code: " + response.statusCode());
|
System.out.println("Response Body: " + response.body());
|
|
return response.statusCode();
|
|
} catch (Exception e) {
|
log.error("同步失败",e);
|
return 500;
|
}
|
}
|
|
/**
|
* 构建数据体
|
* @param syncList 同步数据列表
|
* @return 数据jsno
|
*/
|
JSONObject buildRequestJson(List<MesSync> syncList){
|
JSONObject jsonObject = new JSONObject();
|
|
JSONArray dataList = new JSONArray();
|
for(MesSync sync : syncList){
|
dataList.add(buildOneJson(sync));
|
}
|
jsonObject.put("batchData", dataList);
|
|
return jsonObject;
|
}
|
|
/**
|
* 构建一条的json
|
* @param sync
|
* @return 同步数据json
|
*/
|
JSONObject buildOneJson(MesSync sync){
|
JSONObject item = new JSONObject();
|
item.put("operationId",sync.getOperationId());
|
item.put("programNo",sync.getProgramNo());
|
item.put("curedTime",DateUtil.formatDateTime(sync.getCuredTime()));
|
|
return item;
|
}
|
|
int codeToStatus(int code){
|
if(code == 200){
|
return MesSync.STATUS_SUCCESS;
|
}else{
|
return MesSync.STATUS_FAILED;
|
}
|
}
|
|
@Transactional
|
public void saveAndSync(MesSync sync){
|
save(sync);
|
JSONObject bodyJson = buildRequestJson(Collections.singletonList(sync));
|
int code = postData(bodyJson);
|
|
sync.setStatus(codeToStatus(code));
|
sync.increaseTryTimes();
|
updateById(sync);
|
}
|
|
|
}
|