You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
3.2 KiB
68 lines
3.2 KiB
package com.app.service;
|
|
|
|
import com.app.bean.Count;
|
|
import org.hibernate.HibernateException;
|
|
import org.hibernate.Query;
|
|
import org.hibernate.Session;
|
|
import org.hibernate.criterion.DetachedCriteria;
|
|
import org.hibernate.criterion.Projections;
|
|
import org.hibernate.criterion.Restrictions;
|
|
import org.hibernate.transform.Transformers;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.orm.hibernate3.HibernateCallback;
|
|
import org.springframework.orm.hibernate3.HibernateTemplate;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.sql.SQLException;
|
|
import java.util.List;
|
|
|
|
@Service
|
|
public class CountServiceImpl implements CountService{
|
|
@Autowired
|
|
private HibernateTemplate hibernateTemplate;
|
|
|
|
@Override
|
|
public int count(String startTime, String endTime,Class c) {
|
|
List list= hibernateTemplate.findByCriteria(DetachedCriteria.forClass(c).
|
|
add(Restrictions.ge("created",startTime))
|
|
.add(Restrictions.le("created",endTime))
|
|
.setProjection(Projections.rowCount()));
|
|
if(list.isEmpty()){
|
|
return 0;
|
|
}else{
|
|
return (Integer) list.get(0);
|
|
}
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
@Override
|
|
public List<Count> countWithDate(final String startTime, final String endTime, final Class c) {
|
|
return (List<Count>) hibernateTemplate.execute(new HibernateCallback() {
|
|
@Override
|
|
public Object doInHibernate(Session session) throws HibernateException, SQLException {
|
|
StringBuffer sql=new StringBuffer();
|
|
sql.append("select date, count\n")
|
|
.append("from (\n")
|
|
.append(" select substring(created, 1, 10) as date, count(*) as count\n")
|
|
.append(" from users\n")
|
|
.append(" group by substring(created, 1, 10)\n")
|
|
.append(" union all\n")
|
|
.append(" SELECT CONVERT(VARCHAR(10), dateadd(d, number, DATENAME(year, GETDATE()) + '-01-01'), 23) AS date, 0 as count\n")
|
|
.append(" FROM master..spt_values\n")
|
|
.append(" WHERE type = 'p'\n")
|
|
.append(" AND number between 0 and datediff(day, DATENAME(year, GETDATE()) + '-01-01', getdate())\n")
|
|
.append(" and CONVERT(VARCHAR(10), dateadd(d, number, DATENAME(year, GETDATE()) + '-01-01'), 23) not in\n")
|
|
.append(" (select substring(created, 1, 10) as date from users group by substring(created, 1, 10))) b\n")
|
|
.append("where date between\n")
|
|
.append(" '")
|
|
.append(startTime, 0, 10)
|
|
.append("' and '")
|
|
.append(endTime, 0, 10)
|
|
.append("'\n")
|
|
.append("order by date");
|
|
Query query=session.createSQLQuery(sql.toString());
|
|
return query.setResultTransformer(Transformers.aliasToBean(Count.class)).list();
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|