| | |
| | | import com.alibaba.fastjson2.JSONArray; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.http.HttpUtils; |
| | | import com.ruoyi.framework.config.AliDingConfig; |
| | | import com.ruoyi.production.dto.ProductMaterialDto; |
| | | import com.ruoyi.production.dto.ProductMaterialGroupDto; |
| | | import com.ruoyi.production.enums.MaterialConfigTypeEnum; |
| | | import com.ruoyi.production.mapper.ProductMaterialMapper; |
| | | import com.ruoyi.production.pojo.ProductMaterial; |
| | | import com.ruoyi.production.pojo.ProductMaterialConfig; |
| | | import com.ruoyi.production.pojo.ProductMaterialSku; |
| | | import com.ruoyi.production.service.ProductMaterialConfigService; |
| | | import com.ruoyi.production.service.ProductMaterialService; |
| | | import com.ruoyi.production.service.ProductMaterialSkuService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | |
| | | import java.time.ZoneId; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.time.format.DateTimeParseException; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.*; |
| | | import java.util.concurrent.locks.ReentrantLock; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * <br> |
| | |
| | | @Autowired |
| | | private ProductMaterialConfigService productMaterialConfigService; |
| | | |
| | | @Autowired |
| | | private ProductMaterialSkuService productMaterialSkuService; |
| | | |
| | | /** |
| | | * 同步锁,防止手动和定时任务同时执行 |
| | | */ |
| | | private final ReentrantLock syncLock = new ReentrantLock(); |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void loadProductMaterialData() { |
| | | syncProductMaterialData(1); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void syncProductMaterialJob() { |
| | | syncProductMaterialData(2); |
| | | } |
| | |
| | | JSONObject searchParam = buildSearchParam(lastSyncTime, pageNumber, pageSize); |
| | | |
| | | // 调用宜搭接口拉取数据 |
| | | String dataRes = HttpUtils.sendPostJson( |
| | | aliDingConfig.getSearchFormDataUrl(), |
| | | searchParam.toJSONString(), |
| | | StandardCharsets.UTF_8.name(), |
| | | null, |
| | | accessToken |
| | | ); |
| | | String dataRes = HttpUtils.sendPostJson(aliDingConfig.getSearchFormDataUrl(), searchParam.toJSONString(), StandardCharsets.UTF_8.name(), null, accessToken); |
| | | |
| | | if (StringUtils.isEmpty(dataRes)) { |
| | | log.warn("第 {} 页拉取数据为空", pageNumber); |
| | |
| | | } |
| | | |
| | | // 解析并保存数据 |
| | | List<ProductMaterial> list = parseProductMaterials(dataArr, totalCount); |
| | | List<ProductMaterialSku> list = parseProductMaterials(dataArr, totalCount); |
| | | if (!list.isEmpty()) { |
| | | // 处理更新或新增 |
| | | int affected = processSaveOrUpdate(list); |
| | |
| | | } |
| | | |
| | | private String getAccessToken() { |
| | | String params = "appkey=" + aliDingConfig.getAppKey() |
| | | + "&appsecret=" + aliDingConfig.getAppSecret(); |
| | | 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"); |
| | |
| | | } |
| | | |
| | | private LocalDateTime getLastSyncTime() { |
| | | LambdaQueryWrapper<ProductMaterial> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.orderByDesc(ProductMaterial::getFormModifiedTime).last("LIMIT 1"); |
| | | ProductMaterial lastRecord = this.getOne(queryWrapper); |
| | | LambdaQueryWrapper<ProductMaterialSku> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.orderByDesc(ProductMaterialSku::getFormModifiedTime).last("LIMIT 1"); |
| | | ProductMaterialSku lastRecord = productMaterialSkuService.getOne(queryWrapper); |
| | | return lastRecord != null ? lastRecord.getFormModifiedTime() : null; |
| | | } |
| | | |
| | |
| | | searchParam.put("orderConfigJson", "{\"gmt_modified\":\"+\"}"); |
| | | |
| | | if (lastSyncTime != null) { |
| | | String startTime = lastSyncTime.plusSeconds(1).atZone(ZoneId.systemDefault()) |
| | | .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); |
| | | String startTime = lastSyncTime.plusSeconds(1).atZone(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); |
| | | searchParam.put("modifiedFromTimeGMT", startTime); |
| | | } |
| | | |
| | | String endTime = LocalDateTime.now().atZone(ZoneId.systemDefault()) |
| | | .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); |
| | | String endTime = LocalDateTime.now().atZone(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); |
| | | searchParam.put("modifiedToTimeGMT", endTime); |
| | | |
| | | return searchParam; |
| | | } |
| | | |
| | | private List<ProductMaterial> parseProductMaterials(JSONArray dataArr, Integer totalCount) { |
| | | List<ProductMaterial> list = new ArrayList<>(); |
| | | private List<ProductMaterialSku> parseProductMaterials(JSONArray dataArr, Integer totalCount) { |
| | | List<ProductMaterialSku> list = new ArrayList<>(); |
| | | LocalDateTime now = LocalDateTime.now(); |
| | | |
| | | for (int i = 0; i < dataArr.size(); i++) { |
| | |
| | | String formInstanceId = item.getString("formInstanceId"); |
| | | |
| | | JSONObject originator = item.getJSONObject("originator"); |
| | | String originatorName = originator != null && originator.containsKey("userName") |
| | | ? originator.getJSONObject("userName").getString("nameInChinese") : "未知"; |
| | | |
| | | String originatorName = originator != null && originator.containsKey("userName") ? originator.getJSONObject("userName").getString("nameInChinese") : "未知"; |
| | | JSONObject formData = item.getJSONObject("formData"); |
| | | // 处理物料主表数据 |
| | | ProductMaterial material = new ProductMaterial(); |
| | | |
| | | material.setFormInstanceId(formInstanceId); |
| | | material.setIdentifierCode(formData.getString("textField_l92h77ju")); |
| | | material.setMaterialCode(formData.getString("textField_l92f36f2")); |
| | | material.setMaterialName(formData.getString("textField_l92f36f5")); |
| | | material.setSpecification(formData.getString("textField_l92f36f6")); |
| | | material.setBaseUnit(formData.getString("textField_la147lnw")); |
| | | material.setMaterialAttribute(formData.getString("selectField_la14k51j")); |
| | | material.setFinishedProductName(formData.getString("radioField_lbkk2nn2")); |
| | | material.setRemark(formData.getString("textareaField_l92f36f9")); |
| | | |
| | | // 处理物料类型和存货类别 |
| | | String materialType = formData.getString("selectField_l92f36fb"); |
| | | String inventoryCat = formData.getString("selectField_la154noy"); |
| | | material.setMaterialTypeId(getOrCreateConfigId(materialType, MaterialConfigTypeEnum.MATERIAL_TYPE.name())); |
| | | material.setInventoryCategoryId(getOrCreateConfigId(inventoryCat, MaterialConfigTypeEnum.INVENTORY_CAT.name())); |
| | | |
| | | material.setOriginatorName(originatorName); |
| | | material.setOriginatorOrg("宁夏中创绿能实业集团有限公司"); |
| | | Long materialId = getOrCreateMaterial(material); |
| | | |
| | | material.setFormModifiedTime(parseUtcTime(item.getString("modifiedTimeGMT"))); |
| | | material.setCreateTime(now); |
| | | material.setUpdateTime(now); |
| | | // 处理物料规格数据 |
| | | ProductMaterialSku sku = new ProductMaterialSku(); |
| | | sku.setMaterialId(materialId); |
| | | sku.setFormInstanceId(formInstanceId); |
| | | sku.setIdentifierCode(formData.getString("textField_l92h77ju")); |
| | | sku.setMaterialCode(formData.getString("textField_l92f36f2")); |
| | | sku.setSpecification(formData.getString("textField_l92f36f6")); |
| | | sku.setSupplyType(formData.getString("selectField_la14k51j")); |
| | | sku.setOriginatorName(originatorName); |
| | | sku.setOriginatorOrg("宁夏中创绿能实业集团有限公司"); |
| | | sku.setFormModifiedTime(parseUtcTime(item.getString("modifiedTimeGMT"))); |
| | | sku.setCreateTime(now); |
| | | sku.setUpdateTime(now); |
| | | |
| | | list.add(material); |
| | | list.add(sku); |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | private Long getOrCreateMaterial(ProductMaterial material) { |
| | | LambdaQueryWrapper<ProductMaterial> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(ProductMaterial::getMaterialName, material.getMaterialName()); |
| | | |
| | | ProductMaterial exist = this.getOne(queryWrapper); |
| | | if (exist == null) { |
| | | material.setCreateTime(LocalDateTime.now()); |
| | | material.setUpdateTime(LocalDateTime.now()); |
| | | this.save(material); |
| | | return material.getId(); |
| | | } else { |
| | | // 如果已存在,但关键属性发生变化,则进行更新(以宜搭数据为准) |
| | | boolean needUpdate = false; |
| | | if (material.getMaterialTypeId() != null && !material.getMaterialTypeId().equals(exist.getMaterialTypeId())) { |
| | | exist.setMaterialTypeId(material.getMaterialTypeId()); |
| | | needUpdate = true; |
| | | } |
| | | if (material.getInventoryCategoryId() != null && !material.getInventoryCategoryId().equals(exist.getInventoryCategoryId())) { |
| | | exist.setInventoryCategoryId(material.getInventoryCategoryId()); |
| | | needUpdate = true; |
| | | } |
| | | if (StringUtils.isNotEmpty(material.getBaseUnit()) && !material.getBaseUnit().equals(exist.getBaseUnit())) { |
| | | exist.setBaseUnit(material.getBaseUnit()); |
| | | needUpdate = true; |
| | | } |
| | | if (needUpdate) { |
| | | exist.setUpdateTime(LocalDateTime.now()); |
| | | this.updateById(exist); |
| | | } |
| | | return exist.getId(); |
| | | } |
| | | } |
| | | |
| | | private Integer getOrCreateConfigId(String name, String type) { |
| | | if (StringUtils.isEmpty(name)) { |
| | | return null; |
| | | } |
| | | ProductMaterialConfig config = productMaterialConfigService.getOne(new LambdaQueryWrapper<ProductMaterialConfig>() |
| | | .eq(ProductMaterialConfig::getConfigName, name) |
| | | .eq(ProductMaterialConfig::getConfigType, type)); |
| | | ProductMaterialConfig config = productMaterialConfigService.getOne(new LambdaQueryWrapper<ProductMaterialConfig>().eq(ProductMaterialConfig::getConfigName, name).eq(ProductMaterialConfig::getConfigType, type)); |
| | | if (config == null) { |
| | | config = new ProductMaterialConfig(); |
| | | config.setConfigName(name); |
| | |
| | | return config.getId(); |
| | | } |
| | | |
| | | private int processSaveOrUpdate(List<ProductMaterial> list) { |
| | | private int processSaveOrUpdate(List<ProductMaterialSku> list) { |
| | | if (list == null || list.isEmpty()) { |
| | | return 0; |
| | | } |
| | | int affected = 0; |
| | | |
| | | for (ProductMaterial material : list) { |
| | | ProductMaterial exist = this.getOne(new LambdaQueryWrapper<ProductMaterial>() |
| | | .eq(ProductMaterial::getFormInstanceId, material.getFormInstanceId())); |
| | | for (ProductMaterialSku sku : list) { |
| | | |
| | | if (exist == null) { |
| | | this.save(material); |
| | | affected++; |
| | | log.info("新增物料数据 formInstanceId={}", material.getFormInstanceId()); |
| | | LambdaQueryWrapper<ProductMaterialSku> wrapper = new LambdaQueryWrapper<>(); |
| | | wrapper.eq(ProductMaterialSku::getMaterialId, sku.getMaterialId()) |
| | | .eq(ProductMaterialSku::getSpecification, sku.getSpecification()); |
| | | |
| | | if (StringUtils.isNotEmpty(sku.getMaterialCode())) { |
| | | wrapper.eq(ProductMaterialSku::getMaterialCode, sku.getMaterialCode()); |
| | | } else { |
| | | if (exist.getFormModifiedTime() == null || !exist.getFormModifiedTime().equals(material.getFormModifiedTime())) { |
| | | material.setId(exist.getId()); |
| | | material.setCreateTime(exist.getCreateTime()); |
| | | this.updateById(material); |
| | | wrapper.isNull(ProductMaterialSku::getMaterialCode); |
| | | } |
| | | |
| | | ProductMaterialSku exist = productMaterialSkuService.getOne(wrapper); |
| | | if (exist == null) { |
| | | productMaterialSkuService.save(sku); |
| | | affected++; |
| | | log.info("新增物料规格 {}", sku.getSpecification()); |
| | | } else { |
| | | if (exist.getFormModifiedTime() == null || !exist.getFormModifiedTime().equals(sku.getFormModifiedTime())) { |
| | | sku.setId(exist.getId()); |
| | | sku.setCreateTime(exist.getCreateTime()); |
| | | productMaterialSkuService.updateById(sku); |
| | | |
| | | affected++; |
| | | log.info("更新物料数据 formInstanceId={}", material.getFormInstanceId()); |
| | | log.info("更新物料规格 {}", sku.getSpecification()); |
| | | } |
| | | } |
| | | } |
| | |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public List<ProductMaterialGroupDto> ProductMaterialList() { |
| | | |
| | | List<ProductMaterialConfig> materialConfigList = productMaterialConfigService.list(new LambdaQueryWrapper<ProductMaterialConfig>() |
| | | .eq(ProductMaterialConfig::getConfigType, MaterialConfigTypeEnum.MATERIAL_TYPE.name())); |
| | | |
| | | List<ProductMaterialGroupDto> productMaterialMap = new ArrayList<>(); |
| | | if (materialConfigList == null || materialConfigList.isEmpty()) { |
| | | return productMaterialMap; |
| | | } |
| | | for (ProductMaterialConfig materialConfig : materialConfigList) { |
| | | ProductMaterialGroupDto dto = new ProductMaterialGroupDto(); |
| | | dto.setConfigId(materialConfig.getId()); |
| | | dto.setConfigName(materialConfig.getConfigName()); |
| | | productMaterialMap.add(dto); |
| | | } |
| | | return productMaterialMap; |
| | | } |
| | | |
| | | @Override |
| | | public List<ProductMaterialGroupDto> productMaterialListByQuery(String materialName, Integer materialTypeId) { |
| | | if (StringUtils.isEmpty(materialName) && materialTypeId == null) { |
| | | return new ArrayList<>(0); |
| | | } |
| | | // 查询物料类型配置 |
| | | List<ProductMaterialConfig> materialConfigList = productMaterialConfigService.list(new LambdaQueryWrapper<ProductMaterialConfig>() |
| | | .eq(ProductMaterialConfig::getConfigType, MaterialConfigTypeEnum.MATERIAL_TYPE.name())); |
| | | |
| | | List<ProductMaterialGroupDto> result = new ArrayList<>(); |
| | | if (CollectionUtils.isEmpty(materialConfigList)) { |
| | | return result; |
| | | } |
| | | LambdaQueryWrapper<ProductMaterial> wrapper; |
| | | for (ProductMaterialConfig materialConfig : materialConfigList) { |
| | | wrapper = new LambdaQueryWrapper<>(); |
| | | // 指定需要的字段 |
| | | wrapper.select( |
| | | ProductMaterial::getId, |
| | | ProductMaterial::getMaterialTypeId, |
| | | ProductMaterial::getInventoryCategoryId, |
| | | ProductMaterial::getMaterialName |
| | | ); |
| | | if (StringUtils.isNotEmpty(materialName)) { |
| | | wrapper.eq(ProductMaterial::getMaterialTypeId, materialConfig.getId()) |
| | | .like(ProductMaterial::getMaterialName, materialName); |
| | | } else { |
| | | if (!materialConfig.getId().equals(materialTypeId)) { |
| | | continue; |
| | | } |
| | | wrapper.eq(ProductMaterial::getMaterialTypeId, materialTypeId); |
| | | } |
| | | List<ProductMaterial> materialList = list(wrapper); |
| | | if (CollectionUtils.isEmpty(materialList)) { |
| | | continue; |
| | | } |
| | | |
| | | List<ProductMaterialDto> dtoList = materialList.stream().map(m -> { |
| | | ProductMaterialDto dto = new ProductMaterialDto(); |
| | | dto.setId(m.getId()); |
| | | dto.setMaterialTypeId(m.getMaterialTypeId()); |
| | | dto.setInventoryCategoryId(m.getInventoryCategoryId()); |
| | | dto.setMaterialName(m.getMaterialName()); |
| | | return dto; |
| | | }).collect(Collectors.toList()); |
| | | ProductMaterialGroupDto dto = new ProductMaterialGroupDto(); |
| | | dto.setConfigId(materialConfig.getId()); |
| | | dto.setConfigName(materialConfig.getConfigName()); |
| | | dto.setMaterialList(dtoList); |
| | | result.add(dto); |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void addProductMaterial(ProductMaterial productMaterial) { |
| | | validateProductMaterial(productMaterial, false); |
| | | if (existsMaterialName(productMaterial.getMaterialName(), null)) { |
| | | throw new ServiceException("物料名称已存在"); |
| | | } |
| | | LocalDateTime now = LocalDateTime.now(); |
| | | if (productMaterial.getCreateTime() == null) { |
| | | productMaterial.setCreateTime(now); |
| | | } |
| | | productMaterial.setUpdateTime(now); |
| | | if (!this.save(productMaterial)) { |
| | | throw new ServiceException("新增物料失败"); |
| | | } |
| | | log.info("新增物料成功 materialName={}", productMaterial.getMaterialName()); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateProductMaterial(ProductMaterial productMaterial) { |
| | | validateProductMaterial(productMaterial, true); |
| | | ProductMaterial exist = this.getById(productMaterial.getId()); |
| | | if (exist == null) { |
| | | throw new ServiceException("物料不存在"); |
| | | } |
| | | if (existsMaterialName(productMaterial.getMaterialName(), productMaterial.getId())) { |
| | | throw new ServiceException("物料名称已存在"); |
| | | } |
| | | productMaterial.setUpdateTime(LocalDateTime.now()); |
| | | if (!this.updateById(productMaterial)) { |
| | | throw new ServiceException("修改物料失败"); |
| | | } |
| | | log.info("修改物料成功 id={}", productMaterial.getId()); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void deleteProductMaterial(List<Long> ids) { |
| | | if (ids == null || ids.isEmpty()) { |
| | | throw new ServiceException("请选择至少一条数据"); |
| | | } |
| | | if (!this.removeByIds(ids)) { |
| | | throw new ServiceException("删除物料失败"); |
| | | } |
| | | log.info("删除物料成功 ids={}", ids); |
| | | } |
| | | |
| | | private void validateProductMaterial(ProductMaterial productMaterial, boolean requireId) { |
| | | if (productMaterial == null) { |
| | | throw new ServiceException("参数不能为空"); |
| | | } |
| | | if (requireId && productMaterial.getId() == null) { |
| | | throw new ServiceException("主键ID不能为空"); |
| | | } |
| | | if (StringUtils.isEmpty(productMaterial.getMaterialName())) { |
| | | throw new ServiceException("物料名称不能为空"); |
| | | } |
| | | } |
| | | |
| | | private boolean existsMaterialName(String materialName, Long excludeId) { |
| | | if (StringUtils.isEmpty(materialName)) { |
| | | return false; |
| | | } |
| | | LambdaQueryWrapper<ProductMaterial> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(ProductMaterial::getMaterialName, materialName); |
| | | if (excludeId != null) { |
| | | queryWrapper.ne(ProductMaterial::getId, excludeId); |
| | | } |
| | | return this.count(queryWrapper) > 0; |
| | | } |
| | | } |