yujian
2020-04-02 1dc1b5e88283c2ea8cf53c9f014300b3ad0f53de
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package com.yeshi.fanli.service.impl.lable;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.yeshi.fanli.dao.mybatis.lable.LabelClassMapper;
import com.yeshi.fanli.entity.bus.clazz.GoodsClass;
import com.yeshi.fanli.entity.bus.clazz.GoodsSubClass;
import com.yeshi.fanli.entity.bus.lable.Label;
import com.yeshi.fanli.entity.bus.lable.LabelClass;
import com.yeshi.fanli.exception.goods.quality.LabelClassException;
import com.yeshi.fanli.service.inter.lable.LabelClassService;
import com.yeshi.fanli.service.inter.lable.LabelService;
 
@Service
public class LabelClassServiceImpl implements LabelClassService {
 
    
    @Resource
    private LabelService labelService;
    
    @Resource
    private LabelClassMapper labelClassMapper;
    
    
 
    @Override
    public int insert(LabelClass record) throws LabelClassException{
        return labelClassMapper.insert(record);
    }
 
    
    @Override
    public int updateByPrimaryKey(LabelClass record) throws LabelClassException{
        return labelClassMapper.updateByPrimaryKey(record);
    }
 
    @Override
    public int updateByPrimaryKeySelective(LabelClass record) throws LabelClassException{
        return labelClassMapper.updateByPrimaryKeySelective(record);
    }
 
    @Override
    public int deleteByPrimaryKey(Long id) throws LabelClassException{
        return labelClassMapper.deleteByPrimaryKey(id);
    }
 
 
    @Override
    public LabelClass selectByPrimaryKey(Long id) throws LabelClassException {
        return labelClassMapper.selectByPrimaryKey(id);
    }
    
    @Override
    public List<LabelClass> getByClassId(Long classId) throws LabelClassException {
        return labelClassMapper.getByClassId(classId);
    }
    
    @Override
    public List<LabelClass> getBySubClassId(Long classId) throws LabelClassException {
        return labelClassMapper.getBySubClassId(classId);
    }
    
    
    @Override
    public List<LabelClass> queryByClassId(int start, int count, Long classId) throws LabelClassException {
        return labelClassMapper.queryByClassId(start, count, classId);
    }
    
    @Override
    public int getCountQueryByClassId(Long classId) throws LabelClassException {
        return labelClassMapper.getCountQueryByClassId(classId);
    }
    
    @Override
    public List<LabelClass> queryBySubClassId(int start, int count, Long classId) throws LabelClassException {
        return labelClassMapper.queryBySubClassId(start, count, classId);
    }
    
    @Override
    @Cacheable(value = "labelCache",key="'queryBySubClassIdCache-'+#start+'-'+#classId")
    public List<LabelClass> queryBySubClassIdCache(int start, int count, Long classId) throws LabelClassException {
        return queryBySubClassId(start, count, classId);
    }
    
    @Override
    public List<Long> getRelationLabIds(int start, int count, Long classId) throws LabelClassException {
    
        /* 1、根据标签查询商品 */
        List<Long> listLabId = null;
        
        List<LabelClass> labelClassList = queryBySubClassIdCache(start, count, classId);
 
        if (labelClassList != null && labelClassList.size() > 0) {
            listLabId = new ArrayList<Long>();
            for (LabelClass labelClass : labelClassList) {
                Label label = labelClass.getLabel();
                listLabId.add(label.getId());
            }
        }
        
        return listLabId; 
        
    }
    
    
    @Override
    public int getCountQueryBySubClassId(Long subClassId) throws LabelClassException {
        return labelClassMapper.getCountQueryBySubClassId(subClassId);
    }
    
    
    
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void addBatchClass(GoodsClass goodsClass,List<Long> labIdList) throws Exception {
        
        long recordId = goodsClass.getId();
        
        if (labIdList == null || labIdList.size() == 0) {
            // 清空关联标签
            labelClassMapper.deleteByClassId(recordId);
            return;
        }
        
        // 删除标签
        List<Long> subList = new ArrayList<Long>();
        
        // 查询现有关联标签
        List<LabelClass> hasList = labelClassMapper.getByClassId(recordId);
        
        
        // 已存在,则遍历哪些需要删除
        if (hasList != null && hasList.size() > 0) {
            
            for (LabelClass lClass: hasList) {
                
                Long lablClassID = lClass.getId();
                
                Label label = lClass.getLabel();
                
                if (label != null) {
                    
                    Long labid = label.getId();
                    if (labIdList.contains(labid)) {
                        // 包含,则从集合中删除 labid
                        labIdList.remove(labid);
                    } else {
                        // 当前传递中不包含  那么需要删除 lablClassID
                        subList.add(lablClassID);
                    }
                }
            }
        }
            
        if (subList != null && subList.size() > 0) {
            for (Long id: subList) {
                labelClassMapper.deleteByPrimaryKey(id);
            }
        }
        
        if (labIdList != null && labIdList.size() > 0) {
            
            // 去重复
            HashSet<Long> h = new HashSet<Long>(labIdList);   
            labIdList.clear();   
            labIdList.addAll(h);   
            
            for (Long labId: labIdList) {
                // 查询数据库 标签是否存在
                Label label = labelService.selectByPrimaryKey(labId);
                if (label == null) {
                    continue;
                } 
                
                LabelClass labelClass = new LabelClass();
                
                labelClass.setLabel(label);
                labelClass.setGoodClass(goodsClass);
                labelClass.setCreatetime(new Date());
                labelClassMapper.insertSelective(labelClass);
            }
        }
            
    }
    
    
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void addBatchSubClass(GoodsSubClass goodsSubClass,List<Long> labIdList) throws Exception {
        
        long recordId = goodsSubClass.getId();
        // 删除标签
        List<Long> subList = new ArrayList<Long>();
        
        // 查询现有关联标签
        List<LabelClass> hasList = labelClassMapper.getBySubClassId(recordId);
        
        
        // 已存在,则遍历哪些需要删除
        if (hasList != null && hasList.size() > 0) {
            
            for (LabelClass lClass: hasList) {
                Long lablClassID = lClass.getId();
                Label label = lClass.getLabel();
                if (label != null) {
                    Long id = label.getId();
                    
                    
                    if (labIdList.contains(id)) {
                        // 包含,则从集合中删除 labid
                        labIdList.remove(id);
                        
                    } else {
                        // 当前传递中不包含  那么需要删除 lablClassID
                        subList.add(lablClassID);
                    }
                }
            }
        }
        
        /*    删除标签       */
        if (subList != null && subList.size() > 0) {
            for (Long id: subList) {
                labelClassMapper.deleteByPrimaryKey(id);
            }
        }
        
        /*    添加新增标签       */
        if (labIdList != null && labIdList.size() > 0) {
            
            // 去重复
            HashSet<Long> h = new HashSet<Long>(labIdList);   
            labIdList.clear();   
            labIdList.addAll(h);   
             
            
            for (Long labId: labIdList) {
                
                Label label = labelService.selectByPrimaryKey(labId);
                if (label == null) {
                    continue;
                } 
                
                LabelClass labelClass = new LabelClass();
                
                labelClass.setLabel(label);
                labelClass.setCreatetime(new Date());
                labelClass.setGoodsSubClass(goodsSubClass);
                labelClassMapper.insertSelective(labelClass);
            }
        }
        
            
    }
        
    
    @Override
    public int deleteByClassId(Long recordId) throws Exception {
        return labelClassMapper.deleteByClassId(recordId);
    }
    
    @Override
    public int deleteBySubClassId(Long recordId) throws Exception {
        return labelClassMapper.deleteBySubClassId(recordId);
    }
    
}