admin
2022-08-25 264b5dea5b74c4b5ba54a90caba7e709858a037e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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<CategoryContry> 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<CategoryContry> 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<VideoInfo> getVideoInfoByArea(String type, DetailSystem ds, final int page,final int pageCount, int order,
            final List<Long> 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<VideoInfo>) categoryContryDao.excute(new HibernateCallback<List<VideoInfo>>() {
                public List<VideoInfo> 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<VideoInfo> 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<VideoInfo> newVideoList = new ArrayList<>();
                        if (videoList.size() > 0)
                            newVideoList.addAll(videoList);
                        return newVideoList;
 
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return null;
                }
            });
        }
 
        List<VideoInfo> 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<VideoInfo> 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<VideoInfo> newVideoList = new ArrayList<>();
        if (videoList.size() > 0)
            newVideoList.addAll(videoList);
        return newVideoList;
    }
 
    private boolean isContainsResource(List<Long> resourceList, List<VideoResource> relist) {
        List<Long> 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);
    }
 
}