package com.ld.igds.view.service; import com.bstek.bdf2.core.orm.hibernate.HibernateDao; import com.bstek.dorado.data.provider.Page; import com.ld.igds.common.CoreDeviceService; import com.ld.igds.models.Device; import com.ld.igds.util.ContextUtil; import org.apache.commons.lang.StringUtils; import org.hibernate.Session; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author Andy */ @Component public class DeviceService extends HibernateDao { @Resource private CoreDeviceService coreDeviceService; /** * 加载所有设备数据 *

* Title: loadDevice *

*

* Description: *

*/ public List loadDevice(Map parameter) { if (parameter == null || parameter.size() <= 0) return null; Map p = new HashMap(); String hql = " from " + Device.class.getName() + " where companyId = :companyId "; String companyId = (String) parameter.get("companyId"); if (null == companyId) { companyId = ContextUtil.getCompanyId(); } p.put("companyId", companyId); String depotId = (String) parameter.get("depotId"); if (StringUtils.isNotEmpty(depotId)) { hql += " and depotId =:depotId"; p.put("depotId", depotId); } String type = (String) parameter.get("type"); if (StringUtils.isNotEmpty(type)) { hql += " and type =:type"; p.put("type", type); } String serId = (String) parameter.get("serId"); if (StringUtils.isNotEmpty(serId)) { hql += " and serId =:serId"; p.put("serId", serId); } hql += " order by type,id"; return this.query(hql, p); } public void loadDeviceByContion(Page page, Map parameter) throws Exception { Map p = new HashMap(); String hql = " from " + Device.class.getName() + " where companyId = :companyId "; p.put("companyId", ContextUtil.getCompanyId()); if (null == parameter) parameter = new HashMap(); String depotId = (String) parameter.get("depotId"); if (StringUtils.isNotEmpty(depotId)) { hql += " and depotId =:depotId"; p.put("depotId", depotId); } String type = (String) parameter.get("type"); if (StringUtils.isNotEmpty(type)) { hql += " and type =:type"; p.put("type", type); } String serId = (String) parameter.get("serId"); if (StringUtils.isNotEmpty(serId)) { hql += " and serId =:serId"; p.put("serId", serId); } String countSql = "select count(*) " + hql; hql += " order by serId,id asc"; this.pagingQuery(page, hql, countSql, p); } /** * 修改设备信息 *

* Title: upDepotDevice *

*

* Description: *

* * @param depotDevice */ public void upDepotDevice(Device depotDevice) { Session session = null; try { session = this.getSessionFactory().openSession(); session.update(depotDevice); } catch (Exception e) { e.printStackTrace(); } finally { session.flush(); session.close(); } } /** * 删除设备 *

* Title: delDepotDevice *

*

* Description: *

*/ public void delDepotDevice(Device depotDevice) { Session session = null; try { session = this.getSessionFactory().openSession(); session.delete(depotDevice); } catch (Exception e) { e.printStackTrace(); } finally { session.flush(); session.close(); } } public void delDepotDevice(String id) { Session session = null; try { session = this.getSessionFactory().openSession(); String hql = " delete from " + Device.class.getName() + " where id=:id"; session.createQuery(hql).setString("id", id).executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { session.flush(); session.close(); } } public String saveOrUpdate(Device device, String id) { if (null != id) { this.delDepotDevice(id); return null; } Session session = null; try { session = this.getSessionFactory().openSession(); if (null != device.getId()) { session.update(device); } else { device.setCompanyId(ContextUtil.getCompanyId()); device.setId(ContextUtil.buildDeviceId(device.getCompanyId(), device.getDepotId(), device.getPassCode())); session.save(device); } } catch (Exception e) { e.printStackTrace(); } finally { session.flush(); session.close(); } return null; } public void addDepotDevice(Device depotDevice) { Session session = null; try { session = this.getSessionFactory().openSession(); depotDevice.setCompanyId(ContextUtil.getCompanyId()); depotDevice.setId(ContextUtil.buildDeviceId( depotDevice.getCompanyId(), depotDevice.getDepotId(), depotDevice.getPassCode())); session.save(depotDevice); } catch (Exception e) { e.printStackTrace(); } finally { session.flush(); session.close(); } } public void refreshCache(String companyId) { coreDeviceService.refreshCache(companyId); } public Device getDeviceById(String id) { Map p = new HashMap(); String hql = " from " + Device.class.getName() + " where companyId = :companyId and id=:id"; String companyId = ContextUtil.getCompanyId(); p.put("companyId", companyId); p.put("id", id); List list = this.query(hql, p); if (list != null && list.size() > 0) { return list.get(0); } return null; } public List loadDeviceByType(Map parameter) { if (parameter == null || parameter.size() <= 0) return null; Map p = new HashMap(); String hql = " from " + Device.class.getName() + " where companyId = :companyId "; String companyId = (String) parameter.get("companyId"); if (null == companyId) { companyId = ContextUtil.getCompanyId(); } p.put("companyId", companyId); String depotId = (String) parameter.get("depotId"); if (StringUtils.isNotEmpty(depotId)) { hql += " and depotId =:depotId"; p.put("depotId", depotId); } String type = (String) parameter.get("type"); if (StringUtils.isNotEmpty(type)) { hql += " and type =:type"; p.put("type", type); } String serId = (String) parameter.get("serId"); if (StringUtils.isNotEmpty(serId)) { hql += " and serId =:serId"; p.put("serId", serId); } String tag = (String) parameter.get("tag"); // tag = 1 查询风机类型的设备,tag = 2 查询照明设备,否则是其余的其它设备 if (StringUtils.isNotEmpty(tag) && "1".equals(tag)) { hql += " and type in (:types)"; p.put("types", new String[]{"02", "03", "04", "0C", "0D"}); } else if (StringUtils.isNotEmpty(tag) && "2".equals(tag)) { hql += " and type =:type"; p.put("type", "06"); } else { hql += " and type not in (:types)"; p.put("types", new String[]{"02", "03", "04", "0C", "0D"}); } hql += " order by type,id"; return this.query(hql, p); } }