| | |
| | | package com.aps.core.service.impl; |
| | | |
| | | import java.util.List; |
| | | import java.util.Optional; |
| | | |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.aps.common.core.utils.DateUtils; |
| | | import com.aps.common.core.utils.StringUtils; |
| | | import com.aps.core.domain.ApsMaterialStorageManagement; |
| | | import com.aps.core.mapper.ApsMaterialStorageManagementMapper; |
| | | import com.aps.core.service.IApsMaterialStorageManagementService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.stereotype.Service; |
| | | import com.aps.core.mapper.ApsMaterialStorageManagementMapper; |
| | | import com.aps.core.domain.ApsMaterialStorageManagement; |
| | | import com.aps.core.service.IApsMaterialStorageManagementService; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * 物料库存管理Service业务层处理 |
| | |
| | | * @date 2025-04-17 |
| | | */ |
| | | @Service |
| | | @Slf4j |
| | | public class ApsMaterialStorageManagementServiceImpl implements IApsMaterialStorageManagementService |
| | | { |
| | | @Autowired |
| | |
| | | { |
| | | return apsMaterialStorageManagementMapper.deleteApsMaterialStorageManagementById(id); |
| | | } |
| | | /** |
| | | * 获取物料库存信息 |
| | | * */ |
| | | @Override |
| | | public Optional<ApsMaterialStorageManagement> getItemStorage(String plant, String itemNumber) { |
| | | ApsMaterialStorageManagement storageParam = new ApsMaterialStorageManagement(); |
| | | storageParam.setItemNumber(itemNumber); |
| | | storageParam.setApplicableFactories(plant); |
| | | return apsMaterialStorageManagementMapper.selectApsMaterialStorageManagementList(storageParam).stream() |
| | | .findFirst(); |
| | | } |
| | | |
| | | /** |
| | | * 从Redis中获取库存信息 |
| | |
| | | * @return ApsMaterialStorageManagement 返回获取到的库存信息对象,如果没有找到,则返回null |
| | | */ |
| | | @Override |
| | | public ApsMaterialStorageManagement getRdsStorage(String plant, String itemCode){ |
| | | ApsMaterialStorageManagement ams=null; |
| | | public Optional<ApsMaterialStorageManagement> getRdsStorage(String plant, String itemCode){ |
| | | // 尝试从Redis中获取库存信息 |
| | | JSONObject materialStorage = (JSONObject)redisTemplate.opsForValue().get("MaterialStorage:Material_"+plant+"_"+itemCode); |
| | | String key = "MaterialStorage:Material_" + plant + "_" + itemCode; |
| | | JSONObject materialStorage = (JSONObject)redisTemplate.opsForValue().get(key); |
| | | if(materialStorage!=null){ |
| | | // 如果Redis中有缓存,将其转换为ApsMaterialStorageManagement对象并返回 |
| | | ams = materialStorage.toJavaObject(ApsMaterialStorageManagement.class); |
| | | return ams; |
| | | ApsMaterialStorageManagement ams= materialStorage.toJavaObject(ApsMaterialStorageManagement.class); |
| | | Optional<ApsMaterialStorageManagement> optional = Optional.ofNullable(ams); |
| | | return optional ; |
| | | }else { |
| | | // 如果Redis中没有缓存,创建查询参数对象 |
| | | ApsMaterialStorageManagement storageParam = new ApsMaterialStorageManagement(); |
| | | storageParam.setItemNumber(itemCode); |
| | | storageParam.setApplicableFactories(plant); |
| | | // 从数据库中查询库存信息 |
| | | Optional<ApsMaterialStorageManagement> first = apsMaterialStorageManagementMapper.selectApsMaterialStorageManagementList(storageParam).stream() |
| | | .findFirst(); |
| | | if (first.isPresent()){ |
| | | // 如果查询到库存信息,将其缓存到Redis中 |
| | | ams = first.get(); |
| | | redisTemplate.opsForValue().set("MaterialStorage:Material_"+plant+"_"+itemCode,JSONObject.parseObject(JSON.toJSONString(ams))); |
| | | } |
| | | Optional<ApsMaterialStorageManagement> first = getItemStorage( plant, itemCode); |
| | | first.ifPresent(apsMaterialStorageManagement -> |
| | | redisTemplate.opsForValue().set(key, JSONObject.parseObject(JSON.toJSONString(apsMaterialStorageManagement))) |
| | | ); |
| | | return first; |
| | | } |
| | | // 返回库存信息对象 |
| | | return ams; |
| | | } |
| | | |
| | | /** |
| | | * 设置库存数据到redis |
| | | * @param orgCode |
| | | * @return |
| | | */ |
| | | @Override |
| | | public boolean setStorageDataToRedis(String orgCode) { |
| | | try { |
| | | log.info("开始同步 物料库存至Redis!"); |
| | | Set<String> keys = redisTemplate.keys("MaterialStorage:Material_*"); |
| | | if (keys != null && !keys.isEmpty()) { |
| | | redisTemplate.delete(keys); |
| | | } |
| | | ApsMaterialStorageManagement temp = new ApsMaterialStorageManagement(); |
| | | if(!StringUtils.isEmpty(orgCode)){ |
| | | temp.setApplicableFactories(orgCode); |
| | | } |
| | | List<ApsMaterialStorageManagement> list = apsMaterialStorageManagementMapper.selectApsMaterialStorageManagementList(temp); |
| | | Map<String, Object> bulkData = new HashMap<>(); |
| | | list.forEach(apsMaterialStorageManagement -> { |
| | | JSONObject jsonObject = new JSONObject(); |
| | | jsonObject.put("id", apsMaterialStorageManagement.getId()); |
| | | jsonObject.put("itemNumber", apsMaterialStorageManagement.getItemNumber()); |
| | | jsonObject.put("remainderStock", apsMaterialStorageManagement.getRemainderStock()); |
| | | jsonObject.put("applicableFactories", apsMaterialStorageManagement.getApplicableFactories()); |
| | | bulkData.put("MaterialStorage:Material_"+apsMaterialStorageManagement.getApplicableFactories()+"_"+apsMaterialStorageManagement.getItemNumber(), jsonObject); |
| | | }); |
| | | redisTemplate.opsForValue().multiSet(bulkData); |
| | | log.info("完成同步 物料库存至Redis!"); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | log.error("同步物料库存至Redis失败!"+e.getMessage()); |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * 更新库存信息 |
| | | * */ |
| | | public int updateMaterialStorageByVersion(String id, BigDecimal remainderStock, Integer version) { |
| | | return apsMaterialStorageManagementMapper.updateMaterialStorageByVersion(id, remainderStock, version); |
| | | } |
| | | |
| | | @Override |
| | | public int updateRemainderStock(String id, BigDecimal remainderStock, Integer version){ |
| | | return apsMaterialStorageManagementMapper.updateRemainderStock(id, remainderStock, version); |
| | | } |
| | | } |