package com.yeshi.buwan.service.imp; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; import org.hibernate.HibernateException; import org.hibernate.Session; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.orm.hibernate4.HibernateCallback; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.yeshi.buwan.dao.CategoryContryDao; import com.yeshi.buwan.domain.CategoryContry; import com.yeshi.buwan.domain.system.DetailSystem; import com.yeshi.buwan.domain.VideoInfo; import com.yeshi.buwan.domain.VideoResource; import com.yeshi.buwan.util.StringUtil; @Service public class CategoryAreaService { @Resource private ClassService classService; @Resource private CategoryContryDao categoryContryDao; @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,temp.resources,v.`vpicture` 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(),resourceList); 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,temp.resources,v.`vpicture` 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,resourceList); 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) if(!StringUtil.isNullOrEmpty(vr.getId())) { targetList.add(Long.parseLong(vr.getId())); } targetList.retainAll(resourceList); return targetList.size() > 0; } @Transactional @CacheEvict(value="classCache",allEntries=true) public void updateArea(CategoryContry area) { categoryContryDao.update(area); } }