package com.ld.igds.view.service;
|
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
import org.apache.commons.lang3.StringUtils;
|
import org.hibernate.Session;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
import com.bstek.bdf2.core.orm.hibernate.HibernateDao;
|
import com.bstek.dorado.data.entity.EntityState;
|
import com.bstek.dorado.data.entity.EntityUtils;
|
import com.ld.igds.constant.RedisConst;
|
import com.ld.igds.models.Building;
|
import com.ld.igds.util.ContextUtil;
|
import com.ld.igds.util.RedisUtil;
|
|
/**
|
*
|
* @author jiazx
|
*
|
*/
|
@Component
|
public class BuildingService extends HibernateDao {
|
|
String BEAN_ID = "core.buildingService";
|
|
@Autowired
|
private RedisUtil redisUtil;
|
|
public List<Building> loadData(Map<String, Object> parameter) {
|
|
String hql = " from " + Building.class.getName()
|
+ " where companyId=:companyId ";
|
if (null == parameter) {
|
parameter = new HashMap<>();
|
}
|
|
String companyId = (String) parameter.get("companyId");
|
if (StringUtils.isEmpty(companyId)) {
|
companyId = ContextUtil.getCompanyId();
|
}
|
parameter.put("companyId", companyId);
|
|
String deptId = (String) parameter.get("deptId");
|
if(StringUtils.isNotEmpty(deptId)){
|
hql += " and deptId=:deptId ";
|
parameter.put("deptId", deptId);
|
}
|
|
hql += " order by id + 0";
|
return this.query(hql, parameter);
|
}
|
|
public String saveOrUpdate(Building data) {
|
|
Session session = this.getSessionFactory().openSession();
|
try {
|
if(null == data.getDeptId()){
|
data.setDeptId(ContextUtil.subDeptId(null));
|
}
|
data.setCompanyId(ContextUtil.getCompanyId());
|
session.saveOrUpdate(data);
|
refreshCache(data.getCompanyId(), data.getDeptId());
|
} finally {
|
session.flush();
|
session.close();
|
}
|
return null;
|
}
|
|
public String delData(Building data) {
|
Session session = this.getSessionFactory().openSession();
|
try {
|
session.delete(data);
|
} finally {
|
session.flush();
|
session.close();
|
}
|
return null;
|
}
|
|
public String saveAll(List<Building> items) {
|
Session session = this.getSessionFactory().openSession();
|
try {
|
EntityState state;
|
for (Building item : items) {
|
state = EntityUtils.getState(item);
|
if (state == EntityState.NEW) {
|
item.setCompanyId(ContextUtil.getCompanyId());
|
if(null == item.getDeptId()){
|
item.setDeptId(ContextUtil.subDeptId(null));
|
}
|
session.save(item);
|
}
|
if (state == EntityState.DELETED) {
|
session.delete(item);
|
}
|
if (state == EntityState.MODIFIED) {
|
session.update(item);
|
}
|
}
|
|
refreshCache(items.get(0).getCompanyId(), items.get(0).getDeptId());
|
} finally {
|
session.flush();
|
session.close();
|
}
|
return "执行完成";
|
}
|
|
@SuppressWarnings("unchecked")
|
public List<Building> getCacheBuilding(String companyId,String deptId) {
|
if(StringUtils.isEmpty(companyId)){
|
companyId = ContextUtil.getCompanyId();
|
}
|
|
String key = RedisConst.buildKey(companyId, RedisConst.KEY_BUILDING_LIST);
|
|
List<Building> result = (List<Building>) redisUtil.get(key);
|
|
if (null == result) {
|
refreshCache(companyId, deptId);
|
|
return null;
|
}
|
|
if(null == deptId) return result;
|
|
return result;
|
}
|
|
@SuppressWarnings("unchecked")
|
public Building getCacheBuilding(String companyId, String deptId, String buildingId) {
|
if(StringUtils.isEmpty(companyId)){
|
companyId = ContextUtil.getCompanyId();
|
}
|
|
String key = RedisConst.buildKey(companyId, RedisConst.KEY_BUILDING_LIST);
|
|
List<Building> result = (List<Building>) redisUtil.get(key);
|
|
if (null == result) {
|
refreshCache(companyId, deptId);
|
|
return null;
|
}
|
for (Building building : result) {
|
if(buildingId.equals(building.getId())){
|
return building;
|
}
|
}
|
return null;
|
}
|
|
|
public void refreshCache(String companyId, String deptId) {
|
Map<String, Object> parameter = new HashMap<String, Object>();
|
parameter.put("companyId", companyId);
|
parameter.put("deptId", deptId);
|
List<Building> list = this.loadData(parameter);
|
|
redisUtil.set(RedisConst.buildKey(companyId, deptId, RedisConst.KEY_BUILDING_LIST),list);
|
}
|
}
|