1
4 小时以前 b2deb1cc93b3d2c3fb9dc795e3589e1c62329a8f
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
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());
        }
    }
}