gongchunyi
14 小时以前 a3976574426c68495e41b586280745c092112950
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
package com.ruoyi.productionPlan.service.impl;
 
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.basic.pojo.Customer;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.config.AliDingConfig;
import com.ruoyi.framework.util.AliDingUtils;
import com.ruoyi.production.pojo.ProductMaterialSku;
import com.ruoyi.productionPlan.enums.DataSourceTypeEnum;
import com.ruoyi.productionPlan.pojo.ProductionPlan;
import com.ruoyi.productionPlan.pojo.SalesDelivery;
import com.ruoyi.productionPlan.mapper.SalesDeliveryMapper;
import com.ruoyi.productionPlan.service.SalesDeliveryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * <p>
 * 销售发货明细(宜搭) 服务实现类
 * </p>
 *
 * @author 芯导软件(江苏)有限公司
 * @since 2026-03-19 11:49:57
 */
@Service
@Slf4j
public class SalesDeliveryServiceImpl extends ServiceImpl<SalesDeliveryMapper, SalesDelivery> implements SalesDeliveryService {
 
    @Autowired
    private SalesDeliveryMapper salesDeliveryMapper;
 
    @Autowired
    private AliDingConfig aliDingConfig;
 
    @Override
    public void syncSalesDeliveryJob() {
        syncSalesDelivery();
    }
 
    /**
     * 同步数据
     */
    @Transactional(rollbackFor = Exception.class)
    public void syncSalesDelivery() {
        try {
            JSONArray searchConditions = new JSONArray();
            JSONObject condition = new JSONObject();
            condition.put("key", "processApprovedResult");
            JSONArray valueArray = new JSONArray();
            valueArray.add("agree");
            condition.put("value", valueArray);
            condition.put("type", "ARRAY");
            condition.put("operator", "in");
            condition.put("componentName", "SelectField");
            searchConditions.add(condition);
 
            JSONObject statusCondition = new JSONObject();
            statusCondition.put("key", "processInstanceStatus");
            JSONArray statusValueArray = new JSONArray();
            statusValueArray.add("COMPLETED");
            statusCondition.put("value", statusValueArray);
            statusCondition.put("type", "ARRAY");
            statusCondition.put("operator", "in");
            statusCondition.put("componentName", "SelectField");
            searchConditions.add(statusCondition);
 
            String searchFieldJson = searchConditions.toJSONString();
 
            JSONArray dataArr = AliDingUtils.getFormDataList(aliDingConfig, aliDingConfig.getSalesDeliveryFormUuid(), searchFieldJson, this, SalesDelivery::getFormModifiedTime);
 
            if (dataArr.isEmpty()) {
                return;
            }
 
            //  解析并保存数据
            List<SalesDelivery> list = parseSalesDeliverys(dataArr);
            if (!list.isEmpty()) {
                //  处理更新或新增
                int affected = processSaveOrUpdate(list);
                log.info("数据同步完成,共同步 {} 条数据", affected);
            }
 
        } catch (Exception e) {
            log.error("同步生产计划异常", e);
        }
    }
 
    private List<SalesDelivery> parseSalesDeliverys(JSONArray dataArr) {
        List<SalesDelivery> list = new ArrayList<>();
        for (int i = 0; i < dataArr.size(); i++) {
            JSONObject item = dataArr.getJSONObject(i);
            String formInstanceId = item.getString("formInstanceId");
            JSONObject formData = item.getJSONObject("formData");
            JSONArray tableArr = formData.getJSONArray("tableField_kt8b0qse");
            if (tableArr == null || tableArr.isEmpty()) {
                continue;
            }
            for (int j = 0; j < tableArr.size(); j++) {
                JSONObject row = tableArr.getJSONObject(j);
                SalesDelivery salesDelivery = new SalesDelivery();
                salesDelivery.setFormInstanceId(formInstanceId);
                Long time = formData.getLong("dateField_kt8b0qsm");
                LocalDate date = Instant.ofEpochMilli(time).atZone(ZoneId.of("Asia/Shanghai")).toLocalDate();
                salesDelivery.setDeliveryDate(date);//供货日期
                salesDelivery.setDeliveryCode(formData.getString("textField_kt8b0qrp"));//发货单编号
                salesDelivery.setProjectName(formData.getString("textField_l92dg1tg"));//项目名称
                salesDelivery.setDeliveryPlace(formData.getString("textField_kt8b0qsd"));//送货地点
                salesDelivery.setLinkMan(formData.getString("textField_kt8b0qsb"));//联系人
                salesDelivery.setLinkPhone(formData.getString("textField_kt8b0qsc"));//联系电话
                salesDelivery.setDeliveryRemark(formData.getString("textareaField_kt8b0qsl"));//发货备注
                salesDelivery.setCustomer(formData.getString("textField_la98q3sg"));//客户名称
                salesDelivery.setSalesman(formData.getString("textField_ladnkyu1"));//业务员
                salesDelivery.setDeliveryMethod(formData.getString("radioField_ldoc0027"));//发货方式
                String materialCode = row.getString("textField_l92dg1tc");
                // 根据物料编码查询物料信息表,关联物料ID
//                if (StringUtils.isNotEmpty(materialCode)) {
//                    LambdaQueryWrapper<ProductMaterialSku> skuQueryWrapper = new LambdaQueryWrapper<>();
//                    skuQueryWrapper.eq(ProductMaterialSku::getMaterialCode, materialCode);
//                    ProductMaterialSku sku = productMaterialSkuService.getOne(skuQueryWrapper);
//                    if (sku != null) {
//                        salesDelivery.setProductMaterialSkuId(sku.getId());
//                    }
//                }
                salesDelivery.setMaterialCode(materialCode);//物料编码
                salesDelivery.setProductName(row.getString("textField_l96srw8x"));//产品名称
                salesDelivery.setModel(row.getString("textField_l9tljfl8"));//产品规格
                salesDelivery.setVolume(row.getBigDecimal("numberField_kt8b0qsg_value"));//方量
                salesDelivery.setUnitPrice(row.getBigDecimal("numberField_la0tb2x9_value"));//单价
                salesDelivery.setPrice(row.getBigDecimal("numberField_l9w2piwf"));//价格
                salesDelivery.setProductRemark(row.getString("textField_kt8b0qsi"));//产品备注
                salesDelivery.setFormModifiedTime(AliDingUtils.parseUtcTime(item.getString("modifiedTimeGMT")));
                list.add(salesDelivery);
            }
        }
        return list;
    }
 
    private int processSaveOrUpdate(List<SalesDelivery> list) {
        if (list == null || list.isEmpty()) {
            return 0;
        }
        int affected = 0;
 
        for (SalesDelivery salesDelivery : list) {
 
            LambdaQueryWrapper<SalesDelivery> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SalesDelivery::getFormInstanceId, salesDelivery.getFormInstanceId());
 
            SalesDelivery exist = this.getOne(wrapper);
            if (exist == null) {
                this.save(salesDelivery);
                affected++;
            } else {
                if (exist.getFormModifiedTime() == null || !exist.getFormModifiedTime().equals(salesDelivery.getFormModifiedTime())) {
                    salesDelivery.setId(exist.getId());
                    this.updateById(salesDelivery);
                    affected++;
                }
            }
        }
        return affected;
    }
}