yujian
2019-01-22 88b54772dbcf5ecab1e2316e4e4626ac901b8908
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package com.yeshi.fanli.service.impl.goods;
 
import java.io.InputStream;
import java.util.List;
import java.util.UUID;
 
import javax.annotation.Resource;
import javax.transaction.Transactional;
 
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import com.yeshi.fanli.dao.mybatis.GoodsSubClassMapper;
import com.yeshi.fanli.entity.accept.AcceptData;
import com.yeshi.fanli.entity.bus.clazz.GoodsSubClass;
import com.yeshi.fanli.service.inter.goods.GoodsSubClassService;
import com.yeshi.fanli.service.inter.lable.LabelClassService;
import com.yeshi.fanli.util.StringUtil;
import org.yeshi.utils.tencentcloud.COSManager;
 
@Service
public class GoodsSubClassServiceImpl implements GoodsSubClassService {
 
    @Resource
    private GoodsSubClassMapper goodsSubClassMapper;
    @Resource
    private GoodsSubClassService goodsSubClassService;
    @Resource
    private LabelClassService labelClassService;
    
 
    @Override
    public int deleteByPrimaryKey(Long id) {
        return goodsSubClassMapper.deleteByPrimaryKey(id);
    }
 
    @Override
    public int insert(GoodsSubClass record) {
        return goodsSubClassMapper.insert(record);
    }
 
    @Override
    public int insertSelective(GoodsSubClass record) {
        return goodsSubClassMapper.insertSelective(record);
    }
 
    @Override
    public GoodsSubClass selectByPrimaryKey(Long id) {
        return goodsSubClassMapper.selectByPrimaryKey(id);
    }
 
    @Override
    public int updateByPrimaryKeySelective(GoodsSubClass record) {
        return goodsSubClassMapper.updateByPrimaryKeySelective(record);
    }
 
    @Override
    public int updateByPrimaryKey(GoodsSubClass record) {
        return goodsSubClassMapper.updateByPrimaryKey(record);
    }
 
    @Override
    @Transactional
    public void deleteByRootId(Long id) throws Exception {
        
        List<GoodsSubClass> subClassList = goodsSubClassMapper.queryByRootId(id, null);
        if (subClassList != null && subClassList.size() > 0) {
            for (GoodsSubClass goodsSubClass : subClassList) {
                    deleteSub(goodsSubClass.getId());
            }
        }
    }
    
    @Override
    @Transactional
    public void deleteByPrimaryKeyBatch(List<String> recordIds) throws Exception {
        if (recordIds != null && recordIds.size() > 0) {
            for (String recordId : recordIds) {
                deleteSub(Long.parseLong(recordId));
            }
        }
    }
    
    
    @Override
    @Transactional
    public void deleteSub(Long recordId) throws Exception {
        // TODO Auto-generated method stub
 
        GoodsSubClass goodsSubClass = goodsSubClassMapper.selectByPrimaryKey(recordId);
 
        if (goodsSubClass == null)
            return;
 
        /* 删除网络图片 */
        String picture = goodsSubClass.getPicture();
        if (!StringUtil.isNullOrEmpty(picture)) {
            COSManager.getInstance().deleteFile(picture);
        }
 
        /* 删除所有关联子类 */
        List<GoodsSubClass> subList = goodsSubClassMapper.queryByPid(recordId, null);
        if (subList != null && subList.size() > 0) {
            for (GoodsSubClass subClass : subList) {
                Long id = subClass.getId();
                // 继续删除下级
                deleteSub(id);
            }
        }
        
        /* 删除关联标签 */
        labelClassService.deleteBySubClassId(recordId);
        
        /* 删除数据 */
        goodsSubClassMapper.deleteByPrimaryKey(recordId);
    }
 
    @Override
    public int save(GoodsSubClass record, MultipartFile file) throws Exception {
 
        int result = 0;
 
        result = goodsSubClassMapper.insertSelective(record);
        
 
        // 上传图片
        if (file != null) {
            result = goodsSubClassService.uploadPicture(record, file);
        }
 
        return result;
    }
 
    @Override
    public int uploadPicture(GoodsSubClass record, MultipartFile file) throws Exception {
 
        InputStream inputStream = file.getInputStream();
        String contentType = file.getContentType();
        String type = contentType.substring(contentType.indexOf("/") + 1);
        // 上传文件相对位置
        String fileUrl = "ClassImg/" + UUID.randomUUID().toString().replace("-", "") + "." + type;
 
        boolean deleteFile = true;
 
        /* 修改图片时,先删除已存在图片 */
        String picture = record.getPicture();
        if (!StringUtil.isNullOrEmpty(picture))
            deleteFile = COSManager.getInstance().deleteFile(picture);
 
        String uploadFilePath = null;
        /* 上传新图片 */
        if (deleteFile) {
            uploadFilePath = COSManager.getInstance().uploadFile(inputStream, fileUrl).getUrl();
        }
 
        /* 更新数据库信息 */
        int result = 0;
        if (!StringUtil.isNullOrEmpty(uploadFilePath)) {
            record.setPicture(uploadFilePath);
            result = goodsSubClassMapper.updateByPrimaryKeySelective(record);
        }
        return result;
    }
 
    /**
     * 删除图片
     */
    @Override
    public int removePicture(GoodsSubClass record) throws Exception {
 
        String fileUrl = record.getPicture();
 
        int result = -2;
        boolean deleteFile = true;
 
        if (StringUtil.isNullOrEmpty(fileUrl)) {
            return result;
        }
 
        deleteFile = COSManager.getInstance().deleteFile(fileUrl);
 
        if (deleteFile) {
            record.setPicture(null);
            // 更新数据库
            result = goodsSubClassMapper.updateByPrimaryKey(record);
        }
 
        return result;
    }
 
    @Override
    public List<GoodsSubClass> queryByRootId(Long rootId, Integer state) throws Exception {
        return goodsSubClassMapper.queryByRootId(rootId, state);
    }
 
    @Override
    public List<GoodsSubClass> queryByPid(Long pid, Integer state) throws Exception {
        return goodsSubClassMapper.queryByPid(pid, state);
    }
 
    
    @Override
    public List<GoodsSubClass> queryByRootIdAndWeight(Long rootId,int type, int weight) throws Exception {
        return goodsSubClassMapper.queryByRootIdAndWeight(rootId, type, weight);
    }
 
    @Override
    public List<GoodsSubClass> queryByPidAndWeight(Long pid,int type, int weight) throws Exception {
        return goodsSubClassMapper.queryByPidAndWeight(pid, type, weight);
    }
 
    
    @Override
    public List<GoodsSubClass> getGoodsSecondClass(Long rootId, Integer state) throws Exception {
        return goodsSubClassMapper.queryByRootId(rootId, state);
    }
    
    
    @Override
    @Cacheable(value="classCache",key="'getSubClassCache-'+#rootId +'-'+#state")
    public List<GoodsSubClass> getSubClassCache(Long rootId, Integer state) throws Exception {
        return getGoodsSecondClass(rootId, state);
    }
    
    
    @Override
    @Cacheable(value="classCache",key="'getSubClassByPrimaryKeyCache-'+#id")
    public GoodsSubClass getSubClassByPrimaryKeyCache(Long id) throws Exception {
        return selectByPrimaryKey(id);
    }
    
    
    
    /**
     *  统计一级之下的所有二级分类
     * @param rootId  一级id
     * @returnL
     */
    @Override
    public int countByRootId(Long rootId) {
        return goodsSubClassMapper.countByRootId(rootId);
    }
    
    /**
     *  统计二级分类之下其他分类
     * @param rootId  一级id
     * @return
     */
    @Override
    public int countByPid(Long pid){
        return goodsSubClassMapper.countByPid(pid);
    }
    
    /**
     *  统计一级之下的所有二级分类最大权重
     * @param rootId  一级id
     * @returnL
     */
    @Override
    public int getMaxWeightByRootId(Long rootId) {
        return goodsSubClassMapper.getMaxWeightByRootId(rootId);
    }
    
    /**
     *  统计二级分类之下其他分类最大权重
     * @param rootId  一级id
     * @return
     */
    @Override
    public int getMaxWeightByPid(Long pid){
        return goodsSubClassMapper.getMaxWeightByPid(pid);
    }
    
    
    @Override
    public void countClick(AcceptData acceptData, GoodsSubClass record) {
        if ("android".equalsIgnoreCase(acceptData.getPlatform())) {
            Long androidClick = record.getAndroidClick();
            if (androidClick != null) {
                record.setAndroidClick(androidClick + 1);
            } else {
                record.setAndroidClick(1L);
            }
        } else if ("ios".equalsIgnoreCase(acceptData.getPlatform())) {
            Long iosClick = record.getIosClick();
            if (iosClick != null) {
                record.setIosClick(iosClick + 1);
            } else {
                record.setIosClick(1L);
            }
        }
        goodsSubClassService.updateByPrimaryKeySelective(record);
    }
}