| New file |
| | |
| | | package com.vincent.rsf.server.manager.schedules; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.vincent.rsf.framework.common.Cools; |
| | | import com.vincent.rsf.framework.exception.CoolException; |
| | | import com.vincent.rsf.server.api.config.RemotesInfoProperties; |
| | | import com.vincent.rsf.server.api.entity.CommonResponse; |
| | | import com.vincent.rsf.server.api.entity.constant.RcsConstant; |
| | | import com.vincent.rsf.server.manager.entity.BasStation; |
| | | import com.vincent.rsf.server.manager.service.impl.BasStationServiceImpl; |
| | | import com.vincent.rsf.server.system.constant.GlobalConfigCode; |
| | | import com.vincent.rsf.server.system.entity.Config; |
| | | import com.vincent.rsf.server.system.service.ConfigService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.http.HttpEntity; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.HttpMethod; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.scheduling.annotation.Scheduled; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | |
| | | /** |
| | | * @author Munch D. Luffy |
| | | * @date 2026/03/23 |
| | | * 自动同步至MES |
| | | */ |
| | | @Slf4j |
| | | @Component |
| | | public class SynchronizationToMESSchedules { |
| | | |
| | | @Autowired |
| | | private ConfigService configService; |
| | | @Autowired |
| | | private BasStationServiceImpl basStationService; |
| | | @Autowired |
| | | private RestTemplate restTemplate; |
| | | @Autowired |
| | | private RemotesInfoProperties.WmsOpenApi wmsOpenApi; |
| | | |
| | | /** |
| | | * @author Munch D. Luffy |
| | | * @date 2026/01/15 |
| | | * @description: 缓存区域自动生成移库任务 |
| | | * @version 1.0 |
| | | */ |
| | | @Scheduled(cron = "0 0/01 * * * ? ") |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void siteSynchronizationToMES() throws Exception { |
| | | Config config = configService.getOne(new LambdaQueryWrapper<Config>().eq(Config::getFlag, GlobalConfigCode.AUTO_SITE_SYNCHRONIZATION_TO_MES)); |
| | | if (Cools.isEmpty(config) || !Boolean.parseBoolean(config.getVal())) { |
| | | return; |
| | | } |
| | | |
| | | String autoSiteSynchronizationToMesValue = configService.getVal(GlobalConfigCode.AUTO_SITE_SYNCHRONIZATION_TO_MES_VALUE, String.class); |
| | | if (Cools.isEmpty(autoSiteSynchronizationToMesValue)) { |
| | | return; |
| | | } |
| | | Integer autoValue = Integer.parseInt(autoSiteSynchronizationToMesValue); |
| | | if (autoValue < 0) { |
| | | List<BasStation> basStationList = basStationService.list(); |
| | | syncStationsToMes(basStationList); |
| | | } else if (autoValue > 0) { |
| | | List<BasStation> basStationList = basStationService.list(); |
| | | syncStationsToMes(basStationList); |
| | | autoValue = autoValue - 1; |
| | | configService.setVal(GlobalConfigCode.AUTO_SITE_SYNCHRONIZATION_TO_MES_VALUE, String.valueOf(autoValue)); |
| | | } else { |
| | | configService.setVal(GlobalConfigCode.AUTO_SITE_SYNCHRONIZATION_TO_MES, false); |
| | | } |
| | | } |
| | | |
| | | private void syncStationsToMes(List<BasStation> basStationList) { |
| | | if (Cools.isEmpty(basStationList)) { |
| | | log.info("站点同步至MES跳过:站点列表为空"); |
| | | return; |
| | | } |
| | | if (Cools.isEmpty(wmsOpenApi) || Cools.isEmpty(wmsOpenApi.getHost()) || Cools.isEmpty(wmsOpenApi.getPort())) { |
| | | throw new CoolException("open-api配置缺失,无法同步站点信息到MES"); |
| | | } |
| | | |
| | | List<Map<String, Object>> params = new ArrayList<>(); |
| | | for (BasStation basStation : basStationList) { |
| | | if (Cools.isEmpty(basStation) || Cools.isEmpty(basStation.getStationName())) { |
| | | continue; |
| | | } |
| | | Map<String, Object> item = new HashMap<>(); |
| | | item.put("ConnPortCode", basStation.getStationName()); |
| | | item.put("ConnPortName", Cools.isEmpty(basStation.getStationId()) ? basStation.getStationName() : basStation.getStationId()); |
| | | item.put("WorkshopCode", "YZ"); |
| | | item.put("WorkshopName", "银座车间"); |
| | | item.put("ProductionLineCode", "YZ"); |
| | | item.put("ProductionLineName", "银座仓库"); |
| | | item.put("CreatedBy", "WMSSystem"); |
| | | item.put("IsValid", Objects.equals(basStation.getStatus(), 1)); |
| | | params.add(item); |
| | | } |
| | | |
| | | if (params.isEmpty()) { |
| | | log.info("站点同步至MES跳过:无可同步站点数据"); |
| | | return; |
| | | } |
| | | |
| | | String url = wmsOpenApi.getHost() + ":" + wmsOpenApi.getPort() + RcsConstant.SITE_SYNCHRONIZATION_TO_MES; |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.add("Content-Type", "application/json"); |
| | | headers.add("api-version", "v2.0"); |
| | | HttpEntity<List<Map<String, Object>>> httpEntity = new HttpEntity<>(params, headers); |
| | | |
| | | log.info("站点同步至MES开始,请求地址:{},请求参数条数:{}", url, params.size()); |
| | | ResponseEntity<CommonResponse> response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, CommonResponse.class); |
| | | CommonResponse body = response.getBody(); |
| | | log.info("站点同步至MES返回结果:{}", JSONObject.toJSONString(body)); |
| | | |
| | | if (Cools.isEmpty(body)) { |
| | | throw new CoolException("站点同步至MES失败:返回结果为空"); |
| | | } |
| | | if (!Objects.equals(body.getCode(), 200)) { |
| | | throw new CoolException("站点同步至MES失败:" + body.getMsg()); |
| | | } |
| | | } |
| | | } |