package com.ld.igds.view.service;
|
|
import com.bstek.bdf2.core.orm.hibernate.HibernateDao;
|
import com.ld.igds.common.CoreSerService;
|
import com.ld.igds.constant.Constant;
|
import com.ld.igds.models.DeviceSer;
|
import com.ld.igds.util.ContextUtil;
|
import org.hibernate.Session;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* @author jiazx
|
*/
|
@Component
|
public class DeviceSerService extends HibernateDao {
|
|
@Autowired
|
private CoreSerService coreSerService;
|
|
public void saveSer(DeviceSer ser) {
|
Session session = null;
|
try {
|
if (null == ser.getNetworkType()) {
|
ser.setNetworkType(Constant.NETWORK_01);
|
}
|
if (null == ser.getSn()) {
|
ser.setSn(ser.getId());
|
}
|
session = this.getSessionFactory().openSession();
|
if (null == ser.getCompanyId()) {
|
ser.setCompanyId(ContextUtil.getCompanyId());
|
session.save(ser);
|
} else {
|
session.update(ser);
|
}
|
refreshCache(ser.getCompanyId());
|
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
session.flush();
|
session.close();
|
}
|
}
|
|
public void delSerById(String id) {
|
String companyId = ContextUtil.getCompanyId();
|
|
String hql = "delete from " + DeviceSer.class.getName()
|
+ " where id = :id and companyId = :companyId";
|
Session session = this.getSessionFactory().openSession();
|
try {
|
session.createQuery(hql).setString("id", id)
|
.setString("companyId", companyId).executeUpdate();
|
coreSerService.delCache(companyId,id);
|
} finally {
|
session.flush();
|
session.close();
|
}
|
}
|
|
public List<DeviceSer> getAllSer(String companyId) throws Exception {
|
if (null == companyId)
|
companyId = ContextUtil.getCompanyId();
|
|
String hql = " from " + DeviceSer.class.getName()
|
+ " where companyId=:companyId order by type,id + 0";
|
Map<String, Object> param = new HashMap<String, Object>();
|
param.put("companyId", companyId);
|
|
return this.query(hql, param);
|
}
|
|
public void refreshCache(String companyId) {
|
try {
|
List<DeviceSer> list = getAllSer(companyId);
|
coreSerService.setCacheSer(list, companyId);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
public List<DeviceSer> getAllSerCache(String companyId) {
|
return coreSerService.getCacheSerList(companyId);
|
}
|
|
public DeviceSer getDataById(String companyId, String id) {
|
if (null == id)
|
return null;
|
if (null == companyId)
|
companyId = ContextUtil.getCompanyId();
|
|
String hql = " from " + DeviceSer.class.getName()
|
+ " where companyId=:companyId and id=:id";
|
Map<String, Object> param = new HashMap<String, Object>();
|
param.put("companyId", companyId);
|
param.put("id", id);
|
List<DeviceSer> list = this.query(hql, param);
|
if (null == list)
|
return null;
|
return list.get(0);
|
}
|
}
|