packagenet.vtstar.cloudmes.business.herc.service;importcom.alibaba.fastjson.JSONObject;importnet.vtstar.cloudmes.business.herc.domain.Oee;importnet.vtstar.cloudmes.business.herc.domain.OeeResult;importnet.vtstar.cloudmes.business.herc.domain.SyncProgram;importnet.vtstar.cloudmes.business.herc.properties.HercProperties;importnet.vtstar.cloudmes.business.mobileterminal.domain.ProductionCompleteVO;importnet.vtstar.utils.DateUtils;importnet.vtstar.utils.asserts.ParamAssert;importnet.vtstar.utils.domain.Return;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.core.io.FileSystemResource;import org.springframework.http.*;importorg.springframework.stereotype.Service;importorg.springframework.util.LinkedMultiValueMap;importorg.springframework.util.MultiValueMap;importorg.springframework.web.client.RestTemplate;importorg.springframework.web.util.UriComponentsBuilder;importjava.io.File;importjava.lang.reflect.Field;importjava.net.URI;importjava.net.URISyntaxException;importjava.util.Collections;importjava.util.Date;importjava.util.HashMap;importjava.util.Map;
@Servicepublic classHercService {
@AutowiredprivateHercProperties hercProperties;
@AutowiredprivateRestTemplate restTemplate;private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";/***@paramsyncProgram 同步程序需要的参数*/
public voidsyncProgram(SyncProgram syncProgram) {
HttpHeaders headers= newHttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON_UTF8));
MultiValueMap body =getMultiValueMap(syncProgram);
HttpEntity> httpEntity = new HttpEntity<>(body, headers);
ResponseEntity responseEntity = restTemplate.exchange(hercProperties.getSyncProgram(), HttpMethod.POST, httpEntity, Return.class);
ParamAssert.state(responseEntity.getStatusCode()== HttpStatus.OK, "同步程序异常");
Return r=responseEntity.getBody();
ParamAssert.state(600 ==r.getCode(), r.getMessage());
}/*** 获取herc系统中oee内容*/
publicOeeResult getTimePerformanceYieldRateOee(Oee oee) {//url采用了参数解析的方式, 参考herc.url.time-performance-yield-rate-oee
ResponseEntity response = restTemplate.getForEntity(hercProperties.getTimePerformanceYieldRateOee(), Return.class, getValueMap(oee));
ParamAssert.state(response.getStatusCode()== HttpStatus.OK, "获取oee失败");
Return r=response.getBody();
ParamAssert.state(600 ==r.getCode(), r.getMessage());
Map map = (Map) r.getData();return new JSONObject(map).toJavaObject(OeeResult.class);
}/*** 将派工单报工同步到herc系统*/
public voidsyncDispatchListData(ProductionCompleteVO productionCompleteVO) {
HttpHeaders httpHeaders= newHttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
RequestEntityrequestEntity;try{
requestEntity= new RequestEntity<>(productionCompleteVO, httpHeaders, HttpMethod.POST, newURI(hercProperties.getSyncDispatchListData()));
}catch(URISyntaxException e) {
e.printStackTrace();throw new IllegalStateException("同步程序url配置错误,请联系管理员");
}
ResponseEntity responseEntity = restTemplate.exchange(requestEntity, Return.class);
ParamAssert.state(responseEntity.getStatusCode()== HttpStatus.OK, "同步派工单参数失败");
Return r=responseEntity.getBody();
ParamAssert.state(600 ==r.getCode(), r.getMessage());
}/*** 用户同步派工单接口*/
publicMap getUserPlanning(String equipNo) {
HttpHeaders httpHeaders= newHttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
RequestEntity> requestEntity = null;
UriComponentsBuilder requestUri=UriComponentsBuilder.fromUri(URI.create(hercProperties.getSyncWorkOrderCount()))
.queryParam("empNo", equipNo);
ResponseEntity r = restTemplate.getForEntity(requestUri.toUriString(), Return.class);
Map map = (Map) r.getBody().getData();returnmap;
}/*** 将object转换成HashMap封装的对象
* 用于 HTTP GET请求填充参数时使用
* HTTP GET 请求uri类型 /api/xxxx/{x1}/{x2} , 或 /api/xxxx?x1={x1}&x2={x2}*/
private MapgetValueMap(Object object) {
Class objClass=object.getClass();
Map result = new HashMap<>();
Field[] fields=objClass.getDeclaredFields();for(Field field : fields) {
field.setAccessible(true);try{if (field.getType() == Date.class) {
result.put(field.getName(), DateUtils.parseDateToString((Date) field.get(object), DATE_FORMAT));
}else{
result.put(field.getName(), field.get(object));
}
}catch(IllegalAccessException e) {
e.printStackTrace();
}
}returnresult;
}/*** 将object转换成MultiValueMap封装的对象*/
private MultiValueMapgetMultiValueMap(Object object) {
Class objClass=object.getClass();
MultiValueMap result = new LinkedMultiValueMap<>();
Field[] fields=objClass.getDeclaredFields();for(Field field : fields) {
field.setAccessible(true);try{if (field.getType() == File.class) {
FileSystemResource resource= newFileSystemResource((File) field.get(object));
result.add(field.getName(), resource);
}else if (field.getType() == File[].class) {
File[] files=(File[]) field.get(object);for(File file : files) {
FileSystemResource fileSystemResources= newFileSystemResource(file);
result.add(field.getName(), fileSystemResources);
}
}else{
result.add(field.getName(), field.get(object));
}
}catch(IllegalAccessException e) {
e.printStackTrace();
}
}returnresult;
}
}