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.RedisConst;
|
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<Depot> 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<Depot> getCacheDepotList(String companyId) {
|
String patten = RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT);
|
|
Set<String> keys = redisUtil.keys(patten);
|
if (null == keys) return null;
|
|
List<Depot> 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<Depot> getCacheDepotList(String companyId, String deptId) {
|
if (null == deptId) return null;
|
String patten = RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT);
|
|
Set<String> keys = redisUtil.keys(patten);
|
if (null == keys) return null;
|
|
List<Depot> 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<Depot> getCacheCommonBuildingDepot(String companyId, String depotId) {
|
//若仓库未配置仓房编码,则直接返回对应仓库
|
Depot depot = getCacheDepot(companyId, depotId);
|
|
if (StringUtils.isEmpty(depot.getBuildingId())) {
|
List<Depot> list = new ArrayList<>();
|
list.add(depot);
|
return list;
|
}
|
|
List<Depot> list = getCacheDepotList(companyId);
|
if (null == list) {
|
return null;
|
}
|
String buildingId = null;
|
Map<String, List<Depot>> 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<DepotConf> 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<DepotConf> data = getCacheDepotConf(companyId);
|
if (null == data) return null;
|
|
return data.stream().filter(item -> (serId.equals(item.getGrainSer()) || serId.equals(item.getGasSer()) || serId.equals(item.getEsSer()) ))
|
.findAny().orElse(null);
|
}
|
|
@Override
|
public List<DepotConf> getCacheDepotConfBySerId2(String companyId, String serId) {
|
List<DepotConf> 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<DepotConf> getCacheDepotConf(String companyId) {
|
String patten = RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT_CONF);
|
|
Set<String> keys = redisUtil.keys(patten);
|
|
if (null == keys || keys.isEmpty()) return null;
|
|
List<DepotConf> result = new ArrayList<>();
|
for (String key : keys) {
|
result.add((DepotConf) redisUtil.get(key));
|
}
|
|
return result;
|
}
|
|
@Override
|
public List<DepotSerData> getCacheDepotSerByDepots(String companyId, List<String> depotIds) {
|
|
String key = RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT_SER_LIST);
|
|
List<DepotSerData> listAll = (List<DepotSerData>) redisUtil.get(key);
|
if (null == listAll || listAll.isEmpty()) {
|
log.error("没有获取到仓库与分机的关系数据,请联系管理员!");
|
}
|
|
Map<String, DepotSerData> 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<Depot> 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<DefaultCompany> list) {
|
String key = RedisConst.buildKey(Constant.DEFAULT_PARENT_CODE, "COMPANY_LIST");
|
redisUtil.set(key, list);
|
}
|
|
@Override
|
public List<DefaultCompany> getCompanyList() {
|
String key = RedisConst.buildKey(Constant.DEFAULT_PARENT_CODE, "COMPANY_LIST");
|
|
return (List<DefaultCompany>) redisUtil.get(key);
|
}
|
|
@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());
|
param.setDepotStatus(store.getDepotStatus());
|
|
commonMapper.updateDepotStorage(param);
|
}
|
}
|
}
|