package com.yeshi.fanli.service.impl.push; import java.io.Serializable; import java.util.List; import javax.annotation.Resource; import org.hibernate.HibernateException; import org.hibernate.SQLQuery; import org.hibernate.Session; import org.springframework.orm.hibernate4.HibernateCallback; import org.springframework.stereotype.Service; import com.yeshi.fanli.dao.config.PushRecordDao; import com.yeshi.fanli.entity.xinge.PushRecord; import com.yeshi.fanli.service.inter.push.PushRecordService; import com.yeshi.fanli.util.Constant; @Service public class PushRecordServiceImpl implements PushRecordService { @Resource private PushRecordDao dao; public void save(PushRecord pushRecord) { pushRecord.setCreatetime(System.currentTimeMillis()); dao.save(pushRecord); } public List getPushRecordList(String title, int type, int page) { int start = (page-1) * Constant.PAGE_SIZE; StringBuffer hqlBuf=new StringBuffer("from PushRecord pr where 1=1 "); if(title !=null && !"".equals(title.trim())){ hqlBuf.append(" and (pr.title like ? )"); } if(type != 0){ hqlBuf.append(" and (pr.type = ? )"); } hqlBuf.append(" order by pr.id desc "); String hql = hqlBuf.toString(); if(hql.contains("pr.title") && hql.contains("pr.type")){ return dao.list(hqlBuf.toString(), start, Constant.PAGE_SIZE, new Serializable[]{"%"+title+"%",type}); }else if(hql.contains("pr.title") && !hql.contains("pr.type")){ return dao.list(hqlBuf.toString(), start, Constant.PAGE_SIZE, new Serializable[]{"%"+title+"%"}); }else if(!hql.contains("pr.title") && hql.contains("pr.type")){ return dao.list(hqlBuf.toString(), start, Constant.PAGE_SIZE, new Serializable[]{type}); }else{ return dao.list(hqlBuf.toString(), start, Constant.PAGE_SIZE, new Serializable[]{}); } } public int getCount(String title, int type, int page) { StringBuffer hqlBuf=new StringBuffer("select count(*) from PushRecord pr where 1=1 "); if(title !=null && !"".equals(title.trim())){ hqlBuf.append(" and (pr.title like ? )"); } if(type != 0){ hqlBuf.append(" and (pr.type = ? )"); } String hql = hqlBuf.toString(); if(hql.contains("pr.title") && hql.contains("pr.type")){ return (int) dao.getCount(hqlBuf.toString(), new Serializable[]{"%"+title+"%",type}); }else if(hql.contains("pr.title") && !hql.contains("pr.type")){ return (int) dao.getCount(hqlBuf.toString(),new Serializable[]{"%"+title+"%"}); }else if(!hql.contains("pr.title") && hql.contains("pr.type")){ return (int) dao.getCount(hqlBuf.toString(),new Serializable[]{type}); }else{ return (int) dao.getCount(hqlBuf.toString(),new Serializable[]{}); } } public void increaseByAndroid(final String pushId) { final String sql="update yeshi_ec_push_record pr set pr.count=pr.count+1 where pr.androidPushId = ? "; dao.excute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { SQLQuery query = session.createSQLQuery(sql); query.setParameter(0, pushId); return query.executeUpdate(); } }); } public void increaseByIOS(final String pushId) { final String sql="update yeshi_ec_push_record pr set pr.count=pr.count+1 where pr.iosPushId = ? "; dao.excute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { SQLQuery query = session.createSQLQuery(sql); query.setParameter(0, pushId); return query.executeUpdate(); } }); } }