package com.casic.PgInterface.devData.manager; import com.casic.PgInterface.core.hibernate.HibernateEntityDao; import com.casic.PgInterface.core.util.DateUtils; import com.casic.PgInterface.core.util.StringUtils; import com.casic.PgInterface.devData.domain.PgSh; import com.casic.PgInterface.devData.dto.PgShDto; import com.casic.PgInterface.devTable.domain.PgDevice; import org.hibernate.Criteria; import org.hibernate.criterion.Order; import org.hibernate.criterion.Restrictions; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.Calendar; import java.util.List; /** * Created by yxw on 2017/11/28. */ @Service public class PgShManager extends HibernateEntityDao<PgSh> { //获取设备时间段内所有的监测数据 public List<PgShDto> getAllShData(PgDevice pgDevice,String startDate,String endDate) { List<PgShDto> pgShDtoList=new ArrayList<PgShDto>(); try { Criteria criteria = createCriteria(PgSh.class); criteria.addOrder(Order.desc("id")); criteria.add(Restrictions.eq("pgDeviceId", pgDevice)); if (StringUtils.isNotBlank(startDate)) { criteria.add(Restrictions.ge("upTime", DateUtils.sdf4.parse(startDate))); } if(StringUtils.isNotBlank(endDate)){ Calendar calendar=Calendar.getInstance(); calendar.setTime(DateUtils.sdf4.parse(endDate)); calendar.add(Calendar.DATE,1); criteria.add(Restrictions.le("upTime",calendar.getTime())); } List<PgSh> pgShList=criteria.list(); for (PgSh pgSh : pgShList) { pgShDtoList.add(new PgShDto(pgSh)); } } catch(Exception ex) { return null; } return pgShDtoList; } //获取设备最新数据 public PgShDto getNewestShData(PgDevice pgDevice) { try { String sql="select id,SH,LOGTIME,UPTIME from PG_Sh where PGDEVICEID = '"+pgDevice.getId()+"' AND rownum < 2 order by UPTIME"; List<Object[]> pgShList=(List<Object[]>)this.getSession().createSQLQuery(sql).list(); if(pgShList.size()==0) return null; Object[] objects = pgShList.get(0); PgShDto pgShDto=new PgShDto(null); pgShDto.setId(objects[0].toString()); pgShDto.setValue(objects[1].toString()); pgShDto.setLogTime(objects[2].toString()); pgShDto.setUpTime(objects[3].toString()); pgShDto.setDevCode(pgDevice.getDevCode()); pgShDto.setDevName(pgDevice.getAssetName()); //获取devName和devCode return pgShDto; } catch(Exception ex) { return null; } } }