YYC
2023-09-07 7243e0b4bd9a267a633d2e481e17646509b6868d
igds-core/src/main/java/com/ld/igds/view/service/DeviceService.java
@@ -1,279 +1,275 @@
package com.ld.igds.view.service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.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.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 {
   @Autowired
   private CoreDeviceService coreDeviceService;
   /**
    * 加载所有设备数据
    * <p>
    * Title: loadDevice
    * </p>
    * <p>
    * Description:
    * </p>
    *
    */
   public List<Device> loadDevice(Map<String, Object> parameter) {
      if (parameter == null || parameter.size() <= 0)
         return null;
      Map<String, Object> p = new HashMap<String, Object>();
      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);
    @Resource
    private CoreDeviceService coreDeviceService;
      String depotId = (String) parameter.get("depotId");
      if (StringUtils.isNotEmpty(depotId)) {
         hql += " and depotId =:depotId";
         p.put("depotId", depotId);
      }
    /**
     * 加载所有设备数据
     * <p>
     * Title: loadDevice
     * </p>
     * <p>
     * Description:
     * </p>
     */
    public List<Device> loadDevice(Map<String, Object> parameter) {
        if (parameter == null || parameter.size() <= 0)
            return null;
      String type = (String) parameter.get("type");
      if (StringUtils.isNotEmpty(type)) {
         hql += " and type =:type";
         p.put("type", type);
      }
        Map<String, Object> p = new HashMap<String, Object>();
        String hql = " from " + Device.class.getName()
                + " where companyId = :companyId ";
      String serId = (String) parameter.get("serId");
      if (StringUtils.isNotEmpty(serId)) {
         hql += " and serId =:serId";
         p.put("serId", serId);
      }
        String companyId = (String) parameter.get("companyId");
        if (null == companyId) {
            companyId = ContextUtil.getCompanyId();
        }
        p.put("companyId", companyId);
      hql += " order by type,id";
      return this.query(hql, p);
   }
        String depotId = (String) parameter.get("depotId");
        if (StringUtils.isNotEmpty(depotId)) {
            hql += " and depotId =:depotId";
            p.put("depotId", depotId);
        }
   public void loadDeviceByContion(Page<Device> page,
         Map<String, Object> parameter) throws Exception {
      Map<String, Object> p = new HashMap<String, Object>();
      String hql = " from " + Device.class.getName()
            + " where companyId = :companyId ";
      p.put("companyId", ContextUtil.getCompanyId());
      if(null == parameter)parameter = new HashMap<String, Object>();
        String type = (String) parameter.get("type");
        if (StringUtils.isNotEmpty(type)) {
            hql += " and type =:type";
            p.put("type", type);
        }
      String depotId = (String) parameter.get("depotId");
      if (StringUtils.isNotEmpty(depotId)) {
         hql += " and depotId =:depotId";
         p.put("depotId", depotId);
      }
        String serId = (String) parameter.get("serId");
        if (StringUtils.isNotEmpty(serId)) {
            hql += " and serId =:serId";
            p.put("serId", serId);
        }
      String type = (String) parameter.get("type");
      if (StringUtils.isNotEmpty(type)) {
         hql += " and type =:type";
         p.put("type", type);
      }
        hql += " order by type,id";
        return this.query(hql, p);
    }
      String serId = (String) parameter.get("serId");
      if (StringUtils.isNotEmpty(serId)) {
         hql += " and serId =:serId";
         p.put("serId", serId);
      }
    public void loadDeviceByContion(Page<Device> page,
                                    Map<String, Object> parameter) throws Exception {
        Map<String, Object> p = new HashMap<String, Object>();
        String hql = " from " + Device.class.getName()
                + " where companyId = :companyId ";
        p.put("companyId", ContextUtil.getCompanyId());
      String countSql = "select count(*) " + hql;
      hql += " order by serId,id asc";
      this.pagingQuery(page, hql, countSql, p);
   }
        if (null == parameter) parameter = new HashMap<String, Object>();
   /**
    * 修改设备信息
    * <p>
    * Title: upDepotDevice
    * </p>
    * <p>
    * Description:
    * </p>
    *
    * @param depotDevice
    */
        String depotId = (String) parameter.get("depotId");
        if (StringUtils.isNotEmpty(depotId)) {
            hql += " and depotId =:depotId";
            p.put("depotId", depotId);
        }
   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();
      }
        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);
        }
   /**
    * 删除设备
    * <p>
    * Title: delDepotDevice
    * </p>
    * <p>
    * Description:
    * </p>
    */
   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();
      }
   }
        String countSql = "select count(*) " + hql;
        hql += " order by serId,id asc";
        this.pagingQuery(page, hql, countSql, p);
    }
   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();
      }
   }
    /**
     * 修改设备信息
     * <p>
     * Title: upDepotDevice
     * </p>
     * <p>
     * Description:
     * </p>
     *
     * @param depotDevice
     */
   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 upDepotDevice(Device depotDevice) {
        Session session = null;
        try {
            session = this.getSessionFactory().openSession();
            session.update(depotDevice);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
   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);
   }
    /**
     * 删除设备
     * <p>
     * Title: delDepotDevice
     * </p>
     * <p>
     * Description:
     * </p>
     */
    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 Device getDeviceById(String id){
      Map<String, Object> p = new HashMap<String, Object>();
      String hql = " from " + Device.class.getName()
            + " where companyId = :companyId and id=:id";
    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();
        }
    }
      String   companyId = ContextUtil.getCompanyId();
      p.put("companyId", companyId);
      p.put("id", id);
      List<Device> list = this.query(hql,p);
      if(list != null && list.size() > 0){
         return list.get(0);
      }
      return null;
   }
    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 List<Device> loadDeviceByType(Map<String, Object> parameter) {
      if (parameter == null || parameter.size() <= 0)
         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();
        }
    }
      Map<String, Object> p = new HashMap<String, Object>();
      String hql = " from " + Device.class.getName()
            + " where companyId = :companyId ";
    public void refreshCache(String companyId) {
        coreDeviceService.refreshCache(companyId);
    }
      String companyId = (String) parameter.get("companyId");
      if(null == companyId){
         companyId = ContextUtil.getCompanyId();
      }
      p.put("companyId", companyId);
    public Device getDeviceById(String id) {
        Map<String, Object> p = new HashMap<String, Object>();
        String hql = " from " + Device.class.getName()
                + " where companyId = :companyId and id=:id";
      String depotId = (String) parameter.get("depotId");
      if (StringUtils.isNotEmpty(depotId)) {
         hql += " and depotId =:depotId";
         p.put("depotId", depotId);
      }
        String companyId = ContextUtil.getCompanyId();
        p.put("companyId", companyId);
        p.put("id", id);
        List<Device> list = this.query(hql, p);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
      String type = (String) parameter.get("type");
      if (StringUtils.isNotEmpty(type)) {
         hql += " and type =:type";
         p.put("type", type);
      }
    public List<Device> loadDeviceByType(Map<String, Object> parameter) {
        if (parameter == null || parameter.size() <= 0)
            return null;
      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);
   }
        Map<String, Object> p = new HashMap<String, Object>();
        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);
    }
}