package com.fzzy.igds.sys;
|
|
import com.fzzy.igds.dzhwk.constant.RedisConst;
|
import com.fzzy.igds.dzhwk.domain.Building;
|
import com.fzzy.igds.sys.repository.BuildingRepository;
|
import com.fzzy.igds.util.ContextUtil;
|
import com.ruoyi.common.core.redis.RedisCache;
|
import com.ruoyi.common.utils.StringUtils;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.stream.Collectors;
|
|
/**
|
* @Description 仓房service层
|
* @Author CZT
|
* @Date 2024/11/20 19:03
|
*/
|
@Slf4j
|
@Service("sys.buildingService")
|
public class BuildingService {
|
|
@Resource
|
private BuildingRepository buildingRepository;
|
@Resource
|
private RedisCache redisCache;
|
|
/**
|
* jpa查询仓房信息
|
* @param companyId
|
* @param deptId
|
* @return
|
*/
|
public List<Building> getBuilding(String companyId, String deptId) {
|
|
if (StringUtils.isEmpty(companyId)) {
|
companyId = ContextUtil.getCompanyId();
|
}
|
if (StringUtils.isEmpty(deptId)) {
|
deptId = ContextUtil.subDeptId(null);
|
}
|
return buildingRepository.getBuilding(companyId, deptId);
|
}
|
|
/**
|
* 保存仓房信息
|
* @param data
|
*/
|
public void saveOrUpdate(Building data) {
|
if (StringUtils.isEmpty(data.getCompanyId())) {
|
data.setCompanyId(ContextUtil.getCompanyId());
|
}
|
if (StringUtils.isEmpty(data.getDeptId())) {
|
data.setDeptId(ContextUtil.subDeptId(null));
|
}
|
data.setUpdateTime(new Date());
|
buildingRepository.save(data);
|
|
refreshCacheBuilding(data.getCompanyId());
|
}
|
|
/**
|
* 删除仓房信息
|
* @param data
|
* @return
|
*/
|
public String delData(Building data) {
|
buildingRepository.delete(data);
|
return null;
|
}
|
|
/**
|
* 获取缓存中仓房信息
|
* @param companyId
|
* @param deptId
|
* @return
|
*/
|
public List<Building> getCacheBuilding(String companyId, String deptId) {
|
if (StringUtils.isEmpty(companyId)) {
|
companyId = ContextUtil.getCompanyId();
|
}
|
if (StringUtils.isEmpty(deptId)) {
|
deptId = ContextUtil.subDeptId(null);
|
}
|
String key = RedisConst.buildKey(companyId, RedisConst.KEY_BUILDING_LIST);
|
List<Building> result = redisCache.getCacheObject(key);
|
|
if (null == result) {
|
refreshCacheBuilding(companyId);
|
return null;
|
}
|
|
String finalDeptId = deptId;
|
return result.stream().filter(item -> item.getDeptId().equals(finalDeptId))
|
.collect(Collectors.toList());
|
}
|
|
/**
|
* 获取缓存中仓房信息
|
* @param companyId
|
* @param deptId
|
* @param buildingId
|
* @return
|
*/
|
public Building getCacheBuilding(String companyId, String deptId, String buildingId) {
|
if (StringUtils.isEmpty(companyId)) {
|
companyId = ContextUtil.getCompanyId();
|
}
|
if (StringUtils.isEmpty(deptId)) {
|
deptId = ContextUtil.subDeptId(null);
|
}
|
List<Building> result = getCacheBuilding(companyId, deptId);
|
|
if (null == result) {
|
refreshCacheBuilding(companyId);
|
return null;
|
}
|
for (Building building : result) {
|
if (buildingId.equals(building.getId())) {
|
return building;
|
}
|
}
|
return null;
|
}
|
|
public void refreshCacheBuilding(String companyId) {
|
if (StringUtils.isEmpty(companyId)) {
|
companyId = ContextUtil.getCompanyId();
|
}
|
List<Building> list = buildingRepository.getBuilding(companyId);
|
|
redisCache.setCacheObject(RedisConst.buildKey(companyId, RedisConst.KEY_BUILDING_LIST), list);
|
}
|
}
|