package com.ld.igds.common.impl; import com.bstek.bdf2.core.model.DefaultCompany; import com.ld.igds.common.CoreCommonService; import com.ld.igds.common.dto.DepotParam; import com.ld.igds.common.dto.DepotSerData; import com.ld.igds.common.dto.THDto; import com.ld.igds.common.mapper.CommonMapper; import com.ld.igds.constant.Constant; import com.ld.igds.constant.DepotStatus; import com.ld.igds.constant.RedisConst; import com.ld.igds.inout.InoutConstant; import com.ld.igds.inout.dto.InoutData; import com.ld.igds.models.Depot; import com.ld.igds.models.DepotConf; import com.ld.igds.models.DepotStore; import com.ld.igds.models.DicSysConf; import com.ld.igds.util.ContextUtil; import com.ld.igds.util.RedisUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.text.Collator; import java.util.*; import java.util.stream.Collectors; /** * @author jiazx */ @Slf4j @Service(CoreCommonService.BEAN_ID) @SuppressWarnings("unchecked") public class CommonDataServiceImpl implements CoreCommonService { @Resource private RedisUtil redisUtil; @Resource private CommonMapper commonMapper; @Override public void setCacheDepot(List list, String companyId) { if (null == list) return; String key; for (Depot depot : list) { key = RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT, depot.getId()); redisUtil.set(key, depot); } } @Override public List getCacheDepotList(String companyId) { String patten = RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT); Set keys = redisUtil.keys(patten); if (null == keys) return null; List list = new ArrayList<>(); for (String key : keys) { list.add((Depot) redisUtil.get(key)); } //重新排序 Collator sortChina = Collator.getInstance(Locale.CHINA); Collections.sort(list, (a, b) -> sortChina.compare(a.getId(), b.getId())); return list; } @Override public List getCacheDepotList(String companyId, String deptId) { if (null == deptId) return null; String patten = RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT); Set keys = redisUtil.keys(patten); if (null == keys) return null; List list = new ArrayList<>(); Depot depot; for (String key : keys) { depot = (Depot) redisUtil.get(key); if (null == depot) continue; if (deptId.equals(depot.getDeptId())) { list.add(depot); } } //重新排序 Collator sortChina = Collator.getInstance(Locale.CHINA); Collections.sort(list, (a, b) -> sortChina.compare(a.getId(), b.getId())); return list; } @Override public List getCacheCommonBuildingDepot(String companyId, String depotId) { //若仓库未配置仓房编码,则直接返回对应仓库 Depot depot = getCacheDepot(companyId, depotId); if (StringUtils.isEmpty(depot.getBuildingId())) { List list = new ArrayList<>(); list.add(depot); return list; } List list = getCacheDepotList(companyId); if (null == list) { return null; } String buildingId = null; Map> mapList = new HashMap<>();// 定义一个以建筑物ID为KEY的MAP集合对象 for (Depot item : list) { if (item.getId().equals(depotId)) { buildingId = item.getBuildingId(); } if (null == mapList.get(item.getBuildingId())) { mapList.put(item.getBuildingId(), new ArrayList<>()); } mapList.get(item.getBuildingId()).add(item); } return mapList.get(buildingId); } @Override public Depot getCacheDepot(String companyId, String depotId) { String key = RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT, depotId); return (Depot) redisUtil.get(key); } @Override public Depot getDepotById(String companyId, String depotId) { return commonMapper.getDepotById(companyId, depotId); } @Override public void setCacheDepotConf(List list, String companyId) { if (null != list) { Depot depot; String key; for (DepotConf depotConf : list) { depot = this.getCacheDepot(companyId, depotConf.getDepotId()); if (null != depot) { depotConf.setDepotName(depot.getName()); depotConf.setDepotType(depot.getDepotType()); } key = RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT_CONF, depotConf.getDepotId()); redisUtil.set(key, depotConf); } } } @Override public DepotConf getCacheDepotConf(String companyId, String depotId) { String key = RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT_CONF, depotId); return (DepotConf) redisUtil.get(key); } @Override public DepotConf getCacheDepotConfBySerId(String companyId, String serId) { List data = getCacheDepotConf(companyId); if (null == data) return null; return data.stream().filter(item -> item.getGrainSer().equals(serId)) .findAny().orElse(null); } @Override public List getCacheDepotConfBySerId2(String companyId, String serId) { List list = getCacheDepotConf(companyId); if (null == list || list.isEmpty()) return null; return list.stream() .filter(item -> null != item.getGrainSer() && item.getGrainSer().equals(serId)) .collect(Collectors.toList()); } @Override public List getCacheDepotConf(String companyId) { String patten = RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT_CONF); Set keys = redisUtil.keys(patten); if (null == keys || keys.isEmpty()) return null; List result = new ArrayList<>(); for (String key : keys) { result.add((DepotConf) redisUtil.get(key)); } return result; } @Override public List getCacheDepotSerByDepots(String companyId, List depotIds) { String key = RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT_SER_LIST); List listAll = (List) redisUtil.get(key); if (null == listAll || listAll.isEmpty()) { log.error("没有获取到仓库与分机的关系数据,请联系管理员!"); } Map tempMap = new HashMap<>(); for (DepotSerData depotSerData : listAll) { key = companyId + "-" + depotSerData.getSerId(); for (String depotId : depotIds) { if (depotSerData.getDepotId().equals(depotId)) { tempMap.put(key, depotSerData); } } } return new ArrayList<>(tempMap.values()); } @Override public void updateDepotStatus(String companyId, String depotId, String status) { commonMapper.updateDepotStatus(companyId, depotId, status); //刷新缓存 List list = getCacheDepotList(companyId); Depot temp; for (int i = 0; i < list.size(); i++) { temp = list.get(i); if (temp.getId().equals(depotId)) { temp.setDepotStatus(status); list.set(i, temp); break; } } this.setCacheDepot(list, companyId); } @Override public void updateDepot(Depot depot) { int result = commonMapper.updateDepot(depot); if (0 == result) { commonMapper.addDepot(depot); } } @Override public void updateDepotConf(DepotConf depotConf) { int result = commonMapper.updateDepotConf(depotConf); if (0 == result) { commonMapper.addDepotConf(depotConf); } } @Override public void setCacheSysConf(String companyId, DicSysConf data) { String key = RedisConst.buildKey(companyId, RedisConst.KEY_SYS_CONF); redisUtil.set(key, data); } @Override public DicSysConf getCacheSysConf(String companyId) { String key = RedisConst.buildKey(companyId, RedisConst.KEY_SYS_CONF); DicSysConf data = (DicSysConf) redisUtil.get(key); if (null == data) { log.error("没有获取到当前组织={}的系统配置系统,取默认值。", companyId); return new DicSysConf(companyId); } return data; } @Override public void setCacheTH(THDto data) { String key = RedisConst.buildKeyByPrefix( RedisConst.KEY_DEPOT_TH_PREFIX, data.getSerId() + "_" + data.getThConf()); redisUtil.set(key, data, 35 * 60); } @Override public THDto getCacheTH(String companyId, String thSerId, String thConf) { if (StringUtils.isEmpty(thConf)) thConf = "1"; String key = RedisConst.buildKeyByPrefix( RedisConst.KEY_DEPOT_TH_PREFIX, thSerId + "_" + thConf); return (THDto) redisUtil.get(key); } @Override public void updateDepotStorage(String companyId, String depotId, Double curStorage) { if (null == companyId) { companyId = ContextUtil.getDefaultCompanyId(); } DepotParam param = new DepotParam(); param.setCompanyId(companyId); param.setDepotId(depotId); param.setWeight(curStorage); commonMapper.updateDepotStorage(param); } @Override public void setCacheCompany(List list) { String key = RedisConst.buildKey(Constant.DEFAULT_PARENT_CODE, "COMPANY_LIST"); redisUtil.set(key, list); } @Override public List getCompanyList() { String key = RedisConst.buildKey(Constant.DEFAULT_PARENT_CODE, "COMPANY_LIST"); return (List) redisUtil.get(key); } @Override public void initInoutDepotStore(InoutData data) { //先从缓存中获取下信息,查看是否已经初始化过数据,如果有则直接跳过,如果没有则重新验证 // String key = RedisConst.buildKey(data.getDepotId(), "DEPOT_STORE", depotStatus); // String tag = (String) redisUtil.get(key); // if (null != tag) return; //获取当前仓库最后一个库存记录 DepotStore depotStore = getLastDepotStore(data.getDepotId()); //如果有数据,并且状态类一致,表示缓存没有了,但是已经做过初始化 // if (null != depotStore && depotStatus.equals(depotStore.getDepotStatus())) { // redisUtil.set(key, depotStatus, 5 * 24 * 60 * 60); // return; // } //从来没有做个库存管理调整 // if (null == depotStore) { // Depot depot = this.getCacheDepot(data.getCompanyId(), data.getDepotId()); // depotStore = new DepotStore(depot, depotStatus); // if (null != data.getFoodYear()) { // depotStore.setFoodYear(data.getFoodYear()); // } // // } else { //有记录,但是状态与当前作业不一致,也需要初始化 // depotStore.setDepotStatus(depotStatus); // depotStore.setUpdateDate(new Date()); // depotStore.setUpdateUser(null); // } depotStore.setId(null); if (InoutConstant.TYPE_OUT.equals(data.getType())) { depotStore.setOutDate(new Date()); } else { depotStore.setStoreDate(new Date()); } if (null != data.getFoodYear()) { depotStore.setFoodYear(data.getFoodYear()); } depotStore.setRemark("系统生成记录,确认调整"); addDepotStore(depotStore, false); commonMapper.updateDepotStatus(depotStore.getCompanyId(), depotStore.getDepotId(), depotStore.getDepotStatus()); // redisUtil.set(key, depotStatus, 5 * 24 * 60 * 60); } @Override public DepotStore getLastDepotStore(String depotId) { return commonMapper.getLastDepotStore(depotId); } @Override public void addDepotStore(DepotStore store, boolean updateDepot) { if (null == store.getUpdateDate()) store.setUpdateDate(new Date()); if (null == store.getId()) store.setId(ContextUtil.getUUID()); commonMapper.addDepotStore(store); if (updateDepot) { DepotParam param = new DepotParam(); param.setCompanyId(store.getCompanyId()); param.setDepotId(store.getDepotId()); param.setWeight(store.getStorageReal()); commonMapper.updateDepotStorage(param); } } }