admin
2021-09-03 b41a6efe17ba61d150c5a9b7309651cebae54e0d
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package com.yeshi.buwan.service.imp.live;
 
import com.yeshi.buwan.dao.live.SuperTVLiveCategoryDao;
import com.yeshi.buwan.dao.live.TVLiveCategoryDao;
import com.yeshi.buwan.domain.live.SuperTVLiveCategory;
import com.yeshi.buwan.domain.live.TVLiveCategory;
import com.yeshi.buwan.exception.live.TVLiveCategoryException;
import com.yeshi.buwan.service.inter.live.TVLiveCategoryService;
import com.yeshi.buwan.util.StringUtil;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.*;
 
@Service
public class TVLiveCategoryServiceImpl implements TVLiveCategoryService {
    @Resource
    private SuperTVLiveCategoryDao superTVLiveCategoryDao;
 
    @Resource
    private TVLiveCategoryDao tvLiveCategoryDao;
 
 
    @Override
    public void addCategory(TVLiveCategory category, List<String> detailSystemList) throws TVLiveCategoryException {
        //查询分类名称是否存在
        category.setId(StringUtil.Md5(category.getName()));
        TVLiveCategory old = tvLiveCategoryDao.get(category.getId());
        if (old != null)
            throw new TVLiveCategoryException(1, "分类已经存在");
 
        if (category.getCreateTime() == null)
            category.setCreateTime(new Date());
 
        tvLiveCategoryDao.save(category);
 
        for (String detailSystemId : detailSystemList) {
            SuperTVLiveCategory superTVLiveCategory = new SuperTVLiveCategory();
            superTVLiveCategory.setCid(category.getId());
            superTVLiveCategory.setDetailSystemId(detailSystemId);
            superTVLiveCategory.setCreateTime(new Date());
            superTVLiveCategory.setWeight(1);
            addSuperCategory(superTVLiveCategory);
        }
    }
 
    @Override
    public void deleteCateogry(List<String> ids) {
        for (String cid : ids) {
            List<SuperTVLiveCategory> scList = superTVLiveCategoryDao.listByCid(cid);
            for (SuperTVLiveCategory sc : scList) {
                superTVLiveCategoryDao.delete(sc.getId());
            }
            tvLiveCategoryDao.delete(cid);
        }
    }
 
    @Override
    public void updateCategory(TVLiveCategory category) {
        tvLiveCategoryDao.updateSelective(category);
    }
 
    @Override
    public List<TVLiveCategory> list(String detailSystemId, String systemId, String nameKey, int page, int pageSize) {
        if (detailSystemId != null) {
            List<SuperTVLiveCategory> superTVLiveCategoryList = superTVLiveCategoryDao.listByDetailSystemId(detailSystemId, (page - 1) * pageSize, pageSize);
            if (superTVLiveCategoryList == null || superTVLiveCategoryList.size() == 0)
                return null;
            List<String> cids = new ArrayList<>();
 
            for (SuperTVLiveCategory sc : superTVLiveCategoryList) {
                cids.add(sc.getCid());
            }
            List<TVLiveCategory> categoryList = tvLiveCategoryDao.listByIds(cids);
            Map<String, TVLiveCategory> map = new HashMap<>();
            for (TVLiveCategory tc : categoryList) {
                map.put(tc.getId(), tc);
            }
 
 
            List<TVLiveCategory> resultList = new ArrayList<>();
            for (SuperTVLiveCategory sc : superTVLiveCategoryList) {
                resultList.add(map.get(sc.getCid()));
            }
            return resultList;
        } else {
            TVLiveCategoryDao.DaoQuery daoQuery = new TVLiveCategoryDao.DaoQuery();
            daoQuery.systemId = systemId;
            daoQuery.start = (page - 1) * pageSize;
            daoQuery.count = pageSize;
            daoQuery.name = nameKey;
            daoQuery.sortList = Arrays.asList(new Sort.Order(Sort.Direction.DESC, "createTime"));
            return tvLiveCategoryDao.list(daoQuery);
        }
    }
 
    @Override
    public long count(String detailSystemId, String systemId, String nameKey) {
        if (detailSystemId != null) {
            return superTVLiveCategoryDao.countByDetailSystemId(detailSystemId);
        } else {
            TVLiveCategoryDao.DaoQuery daoQuery = new TVLiveCategoryDao.DaoQuery();
            daoQuery.name = nameKey;
            daoQuery.systemId = systemId;
            return tvLiveCategoryDao.count(daoQuery);
        }
    }
 
    @Override
    public List<TVLiveCategory> listByIds(List<String> idList) {
        Criteria[] ors = new Criteria[idList.size()];
        for (int i = 0; i < idList.size(); i++) {
            ors[i] = Criteria.where("_id").is(idList.get(i));
        }
        Query query = new Query();
        query.addCriteria(new Criteria().orOperator(ors));
        return tvLiveCategoryDao.findList(query);
    }
 
    @Override
    public List<SuperTVLiveCategory> listSuper(String detailSystemId, int page, int pageSize) {
 
        return superTVLiveCategoryDao.listByDetailSystemId(detailSystemId, (page - 1) * pageSize, pageSize);
 
    }
 
    @Override
    public void addSuperCategory(SuperTVLiveCategory superCategory) {
        if (superCategory == null)
            return;
        superCategory.setId(SuperTVLiveCategory.createId(superCategory.getCid(), superCategory.getDetailSystemId()));
 
        if (superTVLiveCategoryDao.get(superCategory.getId()) != null)
            return;
 
        if (superCategory.getCreateTime() == null)
            superCategory.setCreateTime(new Date());
        superTVLiveCategoryDao.save(superCategory);
    }
 
    @Override
    public void updateSuperCategory(SuperTVLiveCategory superCategory) {
        superTVLiveCategoryDao.updateSelective(superCategory);
    }
 
    @Override
    public void deleteSuperCategory(String id) {
        superTVLiveCategoryDao.deleteByPrimaryKey(id);
    }
 
    @Override
    public void deleteSuperCategory(String categoryId, String detailSystemId) {
        SuperTVLiveCategory sc = superTVLiveCategoryDao.selectByCidAndDetailsystemId(categoryId, detailSystemId);
        if (sc != null)
            deleteSuperCategory(sc.getId());
    }
 
    @Override
    public List<SuperTVLiveCategory> listSuper(String cid) {
        return superTVLiveCategoryDao.listByCid(cid);
    }
 
    @Override
    public TVLiveCategory selectCategoryBuPrimaryKey(String id) {
        return tvLiveCategoryDao.get(id);
    }
 
 
}