admin
2021-03-20 ad3ac53da1c3a11a96ae62d790aa61a81b9eab91
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package com.yeshi.buwan.service.imp.recommend;
 
import com.yeshi.buwan.dao.recommend.HomeRecommendSpecialDao;
import com.yeshi.buwan.dao.recommend.SuperHomeRecommendSpecialDao;
import com.yeshi.buwan.domain.recommend.HomeRecommendSpecial;
import com.yeshi.buwan.domain.recommend.SuperHomeRecommendSpecial;
import com.yeshi.buwan.domain.system.DetailSystem;
import com.yeshi.buwan.domain.web.DetailSystemSelect;
import com.yeshi.buwan.dto.recommend.HomeRecommendSpecialDTO;
import com.yeshi.buwan.exception.ParamsException;
import com.yeshi.buwan.service.imp.SystemService;
import com.yeshi.buwan.service.inter.recommend.HomeRecommendSpecialService;
import com.yeshi.buwan.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
 
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.*;
 
@Service
public class HomeRecommendSpecialServiceImpl implements HomeRecommendSpecialService {
 
    @Resource
    private HomeRecommendSpecialDao homeRecommendSpecialDao;
 
    @Resource
    private SuperHomeRecommendSpecialDao superHomeRecommendSpecialDao;
 
    @Resource
    private SystemService systemService;
 
 
    @Override
    public List<HomeRecommendSpecialDTO> list(String systemId, String detailSystemId, String key, int page, int pageSize) {
        List<HomeRecommendSpecialDTO> dtoList = new ArrayList<>();
        List<DetailSystem> detailSystemList = systemService.getDetailSystemList(systemId);
        List<HomeRecommendSpecial> list = null;
        if (!StringUtil.isNullOrEmpty(detailSystemId)) {
            list = listSpecialByDetailSystemId(detailSystemId, key, page, pageSize);
        } else {
            list = listSpecialBySystemId(systemId, key, page, pageSize);
        }
 
        for (HomeRecommendSpecial special : list) {
 
            List<DetailSystemSelect> selects = new ArrayList<>();
            HomeRecommendSpecialDTO dto = new HomeRecommendSpecialDTO();
            //查询super
            List<SuperHomeRecommendSpecial> superHomeRecommendSpecialList = listSuperSpecialBySpecialId(special.getId());
            for (DetailSystem detailSystem : detailSystemList) {
                detailSystem.setInfo(null);
                DetailSystemSelect select = new DetailSystemSelect();
                select.setDetailSystem(detailSystem);
                for (SuperHomeRecommendSpecial s : superHomeRecommendSpecialList) {
                    if (s.getDetailSystemId().equalsIgnoreCase(detailSystem.getId())) {
                        select.setSelected(true);
                        break;
                    }
                }
                selects.add(select);
            }
 
            dto.setDetailSystemSelectList(selects);
            dto.setSpecial(special);
            dtoList.add(dto);
        }
        return dtoList;
    }
 
    @Override
    public long count(String systemId, String detailSystemId, String key) {
        if (!StringUtil.isNullOrEmpty(detailSystemId)) {
            return countSpecialByDetailSystemId(detailSystemId, key);
        } else {
            return countSpecialBySystemId(systemId, key);
        }
    }
 
    @Validated
    @Override
    public void addSpecial(@Valid HomeRecommendSpecial special) throws ParamsException, Exception {
        if (special.getId() == null) {
            special.setId(System.currentTimeMillis() + "");
        }
 
        if (special.getCreateTime() == null) {
            special.setCreateTime(new Date());
        }
        if (special.getWeight() == null) {
            special.setWeight(1);
        }
        homeRecommendSpecialDao.save(special);
    }
 
    @Override
    public void updateSpecial(HomeRecommendSpecial special) throws Exception {
        if (special.getId() == null)
            return;
        homeRecommendSpecialDao.updateSelective(special);
    }
 
    @Override
    public HomeRecommendSpecial getSpecial(String id) {
        return homeRecommendSpecialDao.get(id);
    }
 
    @Override
    public HomeRecommendSpecial getSpecialByDataKey(String dataKey) {
        return homeRecommendSpecialDao.selectByDataKey(dataKey);
    }
 
    @Override
    public List<HomeRecommendSpecial> listSpecialBySystemId(String systemId, String key, int page, int pageSize) {
        HomeRecommendSpecialDao.DaoQuery query = new HomeRecommendSpecialDao.DaoQuery();
        query.systemId = systemId;
        query.key = key;
        query.start = (page - 1) * pageSize;
        query.count = pageSize;
        return homeRecommendSpecialDao.list(query);
    }
 
    @Override
    public long countSpecialBySystemId(String systemId, String key) {
        HomeRecommendSpecialDao.DaoQuery query = new HomeRecommendSpecialDao.DaoQuery();
        query.systemId = systemId;
        query.key = key;
        return homeRecommendSpecialDao.count(query);
    }
 
    @Override
    public List<HomeRecommendSpecial> listSpecialByDetailSystemId(String detailSystemId, String key, int page, int pageSize) {
        SuperHomeRecommendSpecialDao.DaoQuery query = new SuperHomeRecommendSpecialDao.DaoQuery();
        query.detailSystemId = detailSystemId;
        query.showName = key;
        query.start = (page - 1) * pageSize;
        query.count = pageSize;
        List<SuperHomeRecommendSpecial> list = superHomeRecommendSpecialDao.list(query);
        List<String> specialIds = new ArrayList<>();
        for (SuperHomeRecommendSpecial special : list) {
            specialIds.add(special.getSpecialId());
        }
        List<HomeRecommendSpecial> specials = homeRecommendSpecialDao.list(specialIds);
        Map<String, HomeRecommendSpecial> map = new HashMap<>();
        for (HomeRecommendSpecial special : specials) {
            map.put(special.getId(), special);
        }
 
        for (int i = 0; i < list.size(); i++) {
            if (map.get(list.get(i).getSpecialId()) == null) {
                list.remove(i--);
                continue;
            }
            list.get(i).setSpecial(map.get(list.get(i).getSpecialId()));
            //替换显示名称
            if (!StringUtil.isNullOrEmpty(list.get(i).getShowName())) {
                list.get(i).getSpecial().setName(list.get(i).getShowName());
            }
        }
        specials = new ArrayList<>();
 
        for (SuperHomeRecommendSpecial superHomeRecommendSpecial : list) {
            specials.add(superHomeRecommendSpecial.getSpecial());
        }
 
        return specials;
    }
 
    @Override
    public long countSpecialByDetailSystemId(String detailSystemId, String key) {
        SuperHomeRecommendSpecialDao.DaoQuery query = new SuperHomeRecommendSpecialDao.DaoQuery();
        query.detailSystemId = detailSystemId;
        query.showName = key;
        return superHomeRecommendSpecialDao.count(query);
    }
 
    @Override
    public void deleteSpecial(String specialId) {
 
        List<SuperHomeRecommendSpecial> list = listSuperSpecialBySpecialId(specialId);
        if (list != null)
            for (SuperHomeRecommendSpecial superHomeRecommendSpecial : list) {
                superHomeRecommendSpecialDao.deleteByPrimaryKey(superHomeRecommendSpecial.getId());
            }
        homeRecommendSpecialDao.deleteByPrimaryKey(specialId);
    }
 
    @Validated
    @Override
    public void addSuperSpecial(@Valid SuperHomeRecommendSpecial superSpecial) throws ParamsException, Exception {
        //查询专题是否存在
        HomeRecommendSpecial special = homeRecommendSpecialDao.get(superSpecial.getSpecialId());
        if (special == null) {
            throw new Exception("专题不存在");
        }
        if (superSpecial.getId() == null) {
            superSpecial.setId(SuperHomeRecommendSpecial.createId(superSpecial.getSpecialId(), superSpecial.getDetailSystemId()));
        }
        if (superSpecial.getWeight() == null) {
            superSpecial.setWeight(special.getWeight());
        }
 
        if (superSpecial.getShowName() == null) {
            superSpecial.setShowName(special.getName());
        }
 
        if (superSpecial.getCreateTime() == null)
            superSpecial.setCreateTime(new Date());
        superHomeRecommendSpecialDao.save(superSpecial);
    }
 
    @Override
    public void updateSuperSpecial(SuperHomeRecommendSpecial superSpecial) throws Exception {
        if (superSpecial.getId() == null)
            return;
        superHomeRecommendSpecialDao.updateSelective(superSpecial);
    }
 
    @Override
    public List<SuperHomeRecommendSpecial> listSuperSpecialBySpecialId(String specialId) {
        SuperHomeRecommendSpecialDao.DaoQuery query = new SuperHomeRecommendSpecialDao.DaoQuery();
        query.specialId = specialId;
        query.start = 0;
        query.count = Integer.MAX_VALUE;
        return superHomeRecommendSpecialDao.list(query);
    }
 
    @Override
    public void deleteSuperSpecial(String specialId, String detailSystemId) {
        SuperHomeRecommendSpecialDao.DaoQuery query = new SuperHomeRecommendSpecialDao.DaoQuery();
        query.specialId = specialId;
        query.detailSystemId = detailSystemId;
        query.start = 0;
        query.count = Integer.MAX_VALUE;
        List<SuperHomeRecommendSpecial> list = superHomeRecommendSpecialDao.list(query);
        if (list != null)
            for (SuperHomeRecommendSpecial superHomeRecommendSpecial : list) {
                superHomeRecommendSpecialDao.deleteByPrimaryKey(superHomeRecommendSpecial.getId());
            }
    }
 
    @Override
    public void deleteSuperSpecial(String id) {
        superHomeRecommendSpecialDao.deleteByPrimaryKey(id);
    }
}