gongchunyi
4 天以前 dbf3089d808f869d99d3bac744c3d10da58b2102
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package com.ruoyi.productionPlan.service.impl;
 
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.framework.config.AliDingConfig;
import com.ruoyi.productionPlan.dto.ProductionPlanDto;
import com.ruoyi.productionPlan.mapper.ProductionPlanMapper;
import com.ruoyi.productionPlan.pojo.ProductionPlan;
import com.ruoyi.productionPlan.service.ProductionPlanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.List;
 
/**
 * <br>
 * 销售生产需求接口实现类
 * </br>
 *
 * @author deslrey
 * @version 1.0
 * @since 2026/03/10 10:00
 */
 
@Slf4j
@Service
public class ProductionPlanServiceImpl extends ServiceImpl<ProductionPlanMapper, ProductionPlan> implements ProductionPlanService {
 
    @Autowired
    private AliDingConfig aliDingConfig;
 
    @Autowired
    private ProductionPlanMapper productionPlanMapper;
 
    @Override
    public IPage<ProductionPlanDto> listPage(Page page, ProductionPlanDto productionPlanDto) {
        return productionPlanMapper.listPage(page, productionPlanDto);
    }
 
    /**
     * 页面手动同步
     */
    @Override
    public void loadProdData() {
        syncProdData(1);
    }
 
    /**
     * 定时任务同步
     */
    @Override
    public void syncProdDataJob() {
        syncProdData(2);
    }
 
    /**
     * 同步数据
     */
    @Transactional(rollbackFor = Exception.class)
    public void syncProdData(Integer dataSyncType) {
        try {
            //  获取AccessToken
            String params = "appkey=" + aliDingConfig.getAppKey()
                    + "&appsecret=" + aliDingConfig.getAppSecret();
 
            String tokenRes = HttpUtils.sendGet(aliDingConfig.getAccessTokenUrl(), params);
 
            JSONObject tokenObj = JSON.parseObject(tokenRes);
 
            String accessToken = tokenObj.getString("access_token");
            if (StringUtils.isEmpty(accessToken)) {
                log.error("获取钉钉AccessToken失败: {}", tokenRes);
                return;
            }
 
            log.info("获取AccessToken成功: {}", accessToken);
 
            //  查询表单数据
            JSONObject searchParam = new JSONObject();
            searchParam.put("appType", aliDingConfig.getAppType());
            searchParam.put("systemToken", aliDingConfig.getSystemToken());
            searchParam.put("userId", aliDingConfig.getUserId());
            searchParam.put("formUuid", aliDingConfig.getFormUuid());
 
            String dataRes = HttpUtils.sendPostJson(
                    aliDingConfig.getSearchFormDataUrl(),
                    searchParam.toJSONString(),
                    StandardCharsets.UTF_8.name(),
                    null,
                    accessToken
            );
 
            if (StringUtils.isEmpty(dataRes)) {
                log.error("拉取宜搭数据为空");
                return;
            }
 
            JSONObject resultObj = JSON.parseObject(dataRes);
            JSONArray dataArr = resultObj.getJSONArray("data");
 
            if (dataArr == null || dataArr.isEmpty()) {
                log.warn("没有获取到表单数据");
                return;
            }
 
            List<ProductionPlan> list = new ArrayList<>();
 
            //  解析表单
            for (int i = 0; i < dataArr.size(); i++) {
 
                JSONObject item = dataArr.getJSONObject(i);
 
                String formInstanceId = item.getString("formInstanceId");
                String serialNo = item.getString("serialNo");
                // 发起人
                JSONObject originator = item.getJSONObject("originator");
                String originatorName = originator.getJSONObject("userName").getString("nameInChinese");
 
                JSONObject formData = item.getJSONObject("formData");
                // 明细表
                JSONArray tableArr = formData.getJSONArray("tableField_l7fytfcn");
                if (tableArr == null || tableArr.isEmpty()) {
                    continue;
                }
 
                //  解析明细
                for (int j = 0; j < tableArr.size(); j++) {
                    JSONObject row = tableArr.getJSONObject(j);
                    ProductionPlan plan = new ProductionPlan();
 
                    // 表单信息
                    plan.setFormInstanceId(formInstanceId);
                    plan.setSerialNo(serialNo);
 
                    // 表头字段
                    plan.setApplyNo(formData.getString("textField_l7fytfco"));
                    plan.setCustomerName(formData.getString("textField_lbkozohg"));
 
                    // 明细字段
                    plan.setMaterialCode(row.getString("textField_l9xo62q5"));
                    plan.setProductName(row.getString("textField_l9xo62q7"));
                    plan.setProductSpec(row.getString("textField_l9xo62q8"));
                    plan.setLength(row.getInteger("numberField_lb7lgatg_value"));
                    plan.setWidth(row.getInteger("numberField_lb7lgath_value"));
                    plan.setHeight(row.getInteger("numberField_lb7lgati_value"));
                    plan.setQuantity(row.getInteger("numberField_lb7lgatj_value"));
                    plan.setVolume(row.getBigDecimal("numberField_l7fytfd3_value"));
                    plan.setStrength(row.getString("radioField_m9urarr2_id"));
 
                    // 日期区间
                    JSONArray dateArr = row.getJSONArray("cascadeDateField_lfxqqluw");
 
                    if (dateArr != null && dateArr.size() == 2) {
                        long start = Long.parseLong(dateArr.getString(0));
                        long end = Long.parseLong(dateArr.getString(1));
 
                        plan.setStartDate(Instant.ofEpochMilli(start)
                                .atZone(ZoneId.systemDefault())
                                .toLocalDateTime());
 
                        plan.setEndDate(Instant.ofEpochMilli(end)
                                .atZone(ZoneId.systemDefault())
                                .toLocalDateTime());
                    }
 
                    // 提交人
                    plan.setSubmitter(originatorName);
                    // 组织
                    plan.setSubmitOrg("宁夏中创绿能实业集团有限公司");
                    // 备注
                    plan.setRemarkOne(formData.getString("textareaField_l7fytfcy"));
                    plan.setRemarkTwo(formData.getString("textField_l7fytfcx"));
                    // 创建人
                    plan.setCreatorName(originatorName);
                    // 修改人
                    JSONObject modifyUser = item.getJSONObject("modifyUser");
                    if (modifyUser != null) {
                        plan.setModifierName(modifyUser.getJSONObject("userName").getString("nameInChinese"));
                    }
                    // 表单时间
                    plan.setFormCreatedTime(parseUtcTime(item.getString("createdTimeGMT")));
                    plan.setFormModifiedTime(parseUtcTime(item.getString("modifiedTimeGMT")));
                    // 同步信息
                    plan.setDataSyncType(dataSyncType);
                    plan.setDataSourceType(1);
                    // 数据库时间
                    LocalDateTime now = LocalDateTime.now();
                    plan.setCreateTime(now);
                    plan.setUpdateTime(now);
 
                    list.add(plan);
                }
            }
 
            if (!list.isEmpty()) {
                saveBatch(list);
            }
            log.info("生产计划同步完成,共 {} 条", list.size());
        } catch (Exception e) {
            log.error("同步生产计划异常", e);
        }
    }
 
    private LocalDateTime parseUtcTime(String utcString) {
        if (StringUtils.isEmpty(utcString)) {
            return null;
        }
        try {
            OffsetDateTime odt = OffsetDateTime.parse(utcString);
            return odt.toLocalDateTime();
        } catch (DateTimeParseException ex) {
            log.warn("解析时间 {} 失败: {}", utcString, ex.getMessage());
            return null;
        }
    }
}