package com.newvideo.service.imp; import java.util.ArrayList; import java.util.Collections; import java.util.List; import javax.annotation.Resource; import org.hibernate.HibernateException; import org.hibernate.Session; import org.springframework.cache.annotation.Cacheable; import org.springframework.orm.hibernate4.HibernateCallback; import org.springframework.stereotype.Service; import com.newvideo.dao.CategoryContryDao; import com.newvideo.domain.CategoryContry; import com.newvideo.domain.DetailSystem; import com.newvideo.domain.VideoInfo; import com.newvideo.domain.VideoResource; import com.newvideo.util.Constant; import com.newvideo.util.StringUtil; @Service public class CategoryAreaService { @Resource private ClassService classService; @Resource private CategoryContryDao categoryContryDao; public CategoryContryDao getCategoryContryDao() { return categoryContryDao; } public void setCategoryContryDao(CategoryContryDao categoryContryDao) { this.categoryContryDao = categoryContryDao; } public ClassService getClassService() { return classService; } public void setClassService(ClassService classService) { this.classService = classService; } @Cacheable(value = "classCache", key = "'categoryContryList'+'-'+#cid") public List categoryContryList(String cid) { return categoryContryDao.list("from CategoryContry cc where cc.parent.id=" + cid + " order by cc.orderby desc"); } @Cacheable(value = "classCache", key = "'getCategoryContryListByCid'+'-'+#cid") public List getCategoryContryListByCid(String cid) { return categoryContryDao.list("from CategoryContry cc where cc.cid=" + cid + " order by cc.orderby desc"); } @Cacheable(value = "classCache", key = "'getCategoryArea'+'-'+#id") public CategoryContry getCategoryArea(String id) { return categoryContryDao.find(CategoryContry.class, Long.parseLong(id)); } @SuppressWarnings("unchecked") @Cacheable(value = "homeCache", key = "'getVideoInfoByArea'+'-'+#type+'-'+#ds.id+'-'+#page+'-'+#pageCount+'-'+#order+'-'+#md5") public List getVideoInfoByArea(String type, DetailSystem ds, final int page,final int pageCount, int order, final List resourceList, final String areas, String md5) { String resourceWhere = ""; for (Long re : resourceList) { resourceWhere += " rv.resourceid=" + re + " or"; } if (resourceWhere.endsWith("or")) resourceWhere = resourceWhere.substring(0, resourceWhere.length() - 2); String orderby = ""; if (order == 1) { orderby = " order by FROM_UNIXTIME(v.updatetime/1000) desc"; } else if (order == 2) { orderby = " order by v.watchcount desc"; } else if (order == 3) { orderby = " order by v.commentcount desc"; } if (StringUtil.isNullOrEmpty(type)) { final String orderByNew = orderby; return (List) categoryContryDao.excute(new HibernateCallback>() { public List doInHibernate(Session session) throws HibernateException { try { String sql = String.format( "SELECT v.id,v.`name`,v.`tag`,v.`picture`,v.`hpicture`,v.`latest_hpicture`,v.watchcount,v.commentcount,v.vpicture,temp.resources FROM wk_video_video v LEFT JOIN wk_video_area_video_temp temp ON temp.`id`=v.`id` WHERE v.show=1 AND temp.`areas` LIKE '%s' and v.id is not null %s", "%" + areas + "%", orderByNew); List videoList = classService.getVideoInfoData( session.createSQLQuery(sql).setFirstResult((page - 1) * pageCount) .setMaxResults(pageCount).list()); if (videoList != null) for (int i = 0; i < videoList.size(); i++) { if (!isContainsResource(resourceList, videoList.get(i).getResourceList())) { videoList.remove(i); if (i > 0) i--; } } System.out.println("列表数量:" + videoList.size()); List newVideoList = new ArrayList(); if (videoList.size() > 0) newVideoList.addAll(videoList); return newVideoList; } catch (Exception e) { e.printStackTrace(); } return null; } }); } List list = new ArrayList(); String sql = String.format( "SELECT v.id,v.`name`,v.`tag`,v.`picture`,v.`hpicture`,v.`latest_hpicture`,v.watchcount,v.commentcount,v.vpicture,temp.resources FROM wk_video_video v LEFT JOIN wk_video_area_video_temp temp ON temp.`id`=v.`id` WHERE v.show=1 AND temp.`areas` LIKE '%s' AND temp.`typeid`=%s and v.id is not null %s", "%" + areas + "%", type, orderby); System.out.println(sql); list = categoryContryDao.sqlList(sql, (page - 1) *pageCount, pageCount, null); List videoList = classService.getVideoInfoData(list); ; if (videoList != null) for (int i = 0; i < videoList.size(); i++) { if (!isContainsResource(resourceList, videoList.get(i).getResourceList())) { videoList.remove(i); if (i > 0) i--; } } System.out.println("列表数量:" + videoList.size()); List newVideoList = new ArrayList(); if (videoList.size() > 0) newVideoList.addAll(videoList); return newVideoList; } private boolean isContainsResource(List resourceList, List relist) { List targetList = new ArrayList(); for (VideoResource vr : relist) targetList.add(Long.parseLong(vr.getId())); targetList.retainAll(resourceList); return targetList.size() > 0; } }