package com.yeshi.fanli.service.impl.user; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.SQLQuery; import org.hibernate.Session; import org.springframework.orm.hibernate4.HibernateCallback; import org.springframework.stereotype.Service; import com.yeshi.fanli.dao.user.HistorySearchDao; import com.yeshi.fanli.entity.admin.HistorySearchAdmin; import com.yeshi.fanli.entity.bus.search.HistorySearch; import com.yeshi.fanli.service.inter.user.HistorySearchService; import com.yeshi.fanli.util.TimeUtil; import com.yeshi.fanli.util.Utils; @Service public class HistorySearchServiceImpl implements HistorySearchService { @Resource private HistorySearchDao historySearchDao; public void addHistorySearch(String searchKey, String bid) { HistorySearch historySearch = new HistorySearch(); historySearch.setName(searchKey); historySearch.setBusinessId(bid); historySearch.setCreatetime(new Date()); historySearch.setState(0); historySearchDao.create(historySearch); } public List getHistorySearch(String bid) { List list = historySearchDao.list("select hs,MAX(hs.id) as maxid from HistorySearch hs where hs.state=0 and hs.businessId=? GROUP BY hs.name order by maxid desc ",0,10,new String[]{bid}); List hlist= new ArrayList(); for(int i = 0 ; i getSearchTotalByDate(int days, Date date) { final Map map =new HashMap(); long timestampms = date.getTime(); final List list = TimeUtil.getEmupDate(days,timestampms); for (String dataStr : list) { map.put(dataStr, 0); } return (Map) historySearchDao.excute(new HibernateCallback>() { public Map doInHibernate(Session session) throws HibernateException { StringBuffer sb = new StringBuffer(); int ii=0; for (String day : list) { if(ii==0){ sb.append("SELECT '"+day+"' AS t,IFNULL(SUM(a.s),0) FROM yeshi_ec_history_search_day a WHERE a.c<= '"+day+"' "); }else{ sb.append("UNION ALL SELECT '"+day+"' AS t,IFNULL(SUM(a.s),0) FROM yeshi_ec_history_search_day a WHERE a.c<= '"+day+"' "); } ii++; } SQLQuery sqlQuery = session.createSQLQuery(sb.toString()); List list = sqlQuery.list(); for (Object[] objArr : list) { map.put(String.valueOf(objArr[0]),((BigDecimal)objArr[1]).intValue()); } return map; } }); } @SuppressWarnings("unchecked") public Map getSearchTotalByMonth(int months, Date date) { final List monthList = Utils.getDateMonthList(months,date); final Map map =new HashMap(); for (String monthStr : monthList) { map.put(monthStr, 0); } return (Map) historySearchDao.excute(new HibernateCallback>() { public Map doInHibernate(Session session) throws HibernateException { StringBuffer sb = new StringBuffer(); int ii=0; for (String month : monthList) { if(ii==0){ sb.append("SELECT '"+month+"' AS t,IFNULL(SUM(a.s),0) FROM yeshi_ec_history_search_month a WHERE a.months<= '"+month+"' "); }else{ sb.append("UNION ALL SELECT '"+month+"' AS t,IFNULL(SUM(a.s),0) FROM yeshi_ec_history_search_month a WHERE a.months<= '"+month+"' "); } ii++; } SQLQuery sqlQuery = session.createSQLQuery(sb.toString()); List list = sqlQuery.list(); for (Object[] objArr : list) { map.put((String) objArr[0],((BigDecimal)objArr[1]).intValue()); } return map; } }); } @SuppressWarnings("unchecked") public List getSearchRankByDate(final int days, final Date date) { return (List) historySearchDao.excute(new HibernateCallback>() { public List doInHibernate(Session session) throws HibernateException { SQLQuery query = session.createSQLQuery("SELECT `name`,COUNT(`name`) `sum` FROM yeshi_ec_history_search WHERE createtime >= DATE_SUB(?,INTERVAL ? DAY) AND createtime < ? GROUP BY `name` ORDER BY `sum` DESC LIMIT 0,20"); query.setParameter(0, date); query.setParameter(1, days); query.setParameter(2, date); List list = query.list(); return list; } }); } }