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; /** *

* 销售发货明细(宜搭) 服务实现类 *

* * @author 芯导软件(江苏)有限公司 * @since 2026-03-19 11:49:57 */ @Service @Slf4j public class SalesDeliveryServiceImpl extends ServiceImpl 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 list = parseSalesDeliverys(dataArr); if (!list.isEmpty()) { // 处理更新或新增 int affected = processSaveOrUpdate(list); log.info("数据同步完成,共同步 {} 条数据", affected); } } catch (Exception e) { log.error("同步生产计划异常", e); } } private List parseSalesDeliverys(JSONArray dataArr) { List 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 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 list) { if (list == null || list.isEmpty()) { return 0; } int affected = 0; for (SalesDelivery salesDelivery : list) { LambdaQueryWrapper 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; } }