yujian
2020-01-02 3f2b34aa24482a45eace7d9882cb115251954c61
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package com.yeshi.fanli.service.impl.lable;
 
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
 
import javax.annotation.Resource;
 
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.yeshi.utils.tencentcloud.COSManager;
 
import com.yeshi.fanli.dao.mybatis.lable.LabelClassMapper;
import com.yeshi.fanli.dao.mybatis.lable.LabelGoodsMapper;
import com.yeshi.fanli.dao.mybatis.lable.LabelMapper;
import com.yeshi.fanli.entity.bus.lable.Label;
import com.yeshi.fanli.entity.bus.lable.LabelGoods;
import com.yeshi.fanli.entity.common.AdminUser;
import com.yeshi.fanli.exception.goods.quality.LabelException;
import com.yeshi.fanli.service.inter.lable.LabelService;
import com.yeshi.fanli.util.FilePathEnum;
import com.yeshi.fanli.util.StringUtil;
 
import jxl.Sheet;
import jxl.Workbook;
 
@Service
public class LabelServiceImpl implements LabelService {
 
    @Resource
    private LabelMapper labelMapper;
    
    @Resource
    private LabelGoodsMapper labelGoodsMapper;
    
    @Resource
    private LabelClassMapper labelClassMapper;
    
 
    @Override
    public int insertSelective(Label record) throws LabelException{
        return labelMapper.insertSelective(record);
    }
 
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void insertList(List<Label>  records,AdminUser admin) throws LabelException{
        if (records != null && records.size() > 0) {
            for (Label label : records) {
                
                //   录入方式
                label.setEntrymode(Label.MODE_BACKSTAGE);
                
                Date nowTime = new Date();
                // 录入时间。人
                label.setCreatetime(nowTime);
                label.setUpdatetime(nowTime);
                label.setCreateUser(admin);
                label.setIosClick(0l);
                label.setAndroidClick(0l);
                
                labelMapper.insertSelective(label);
            }
        }
        
    }
    
    
    @Override
    public void insertSingle(Label label, AdminUser admin,MultipartFile file) throws Exception{
                
        Date nowTime = new Date();
        // 录入时间。人
        label.setCreatetime(nowTime);
        label.setUpdatetime(nowTime);
        label.setCreateUser(admin);
        label.setIosClick(0l);
        label.setAndroidClick(0l);
        //   录入方式
        label.setEntrymode(Label.MODE_BACKSTAGE);
        
        labelMapper.insertSelective(label);
        
        // 上传图片
        if (file != null) {
            uploadPicture(file, label);
        }
                
        
    }
    
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void updateList(List<Label>  records) throws LabelException{
        if (records != null && records.size() > 0) {
            for (Label label : records) {
                
                Date nowTime = new Date();
                label.setUpdatetime(nowTime);
                
                labelMapper.updateByPrimaryKeySelective(label);
            }
        }
        
    }
    
    
    @Override
    public int updateByPrimaryKey(Label record) throws LabelException{
        // TODO Auto-generated method stub
        return labelMapper.updateByPrimaryKey(record);
    }
 
    @Override
    public int updateByPrimaryKeySelective(Label record) throws LabelException{
        // TODO Auto-generated method stub
        return labelMapper.updateByPrimaryKeySelective(record);
    }
 
    
    @Override
    @Transactional(rollbackFor=Exception.class)
    public int deleteBatchById(long[] ids) throws LabelException{
        return labelMapper.deleteBatchByPrimaryKey(ids);
    }
 
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void deleteBatchByPrimaryKey(List<Long> ids) throws LabelException {
        for (Long id : ids) {
            Label label = labelMapper.selectByPrimaryKey(id);
            if (label != null) {
                // 删除图片
                deleteImg(label);
                // 删除数据信息
                labelMapper.deleteByPrimaryKey(id);
                
                // 删除商品对应的标签
                labelGoodsMapper.deleteByLabId(id);
                // 删除分类对应的标签
                labelClassMapper.deleteByLabId(id);
            }
        }
    }
 
    @Override
    public Label selectByPrimaryKey(Long id) throws LabelException{
        return labelMapper.selectByPrimaryKey(id);
    }
    
    @Override
    public List<Label> selectByTitle(String title) throws LabelException{
        return labelMapper.selectByTitle(title);
    }
 
    
    @Override
    @Cacheable(value = "labelCache",key="'getByTitleCache-'+#labKey +'-'+#title")
    public Label getByTitleCache(String labKey, String title) throws LabelException{
        return labelMapper.getByTitle(title);
    }
    
    
    @Override
    @Cacheable(value = "labelCache",key="'listByTitlesCache-'+#list")
    public List<Label> listByTitlesCache(List<String> list){
        return labelMapper.listByTitles(list);
    }
    
    
    @Override
    public List<Label> query(int pageIndex, int pageSize, String key,
            String startTime, String endTime, String orderMode) throws LabelException {
        return labelMapper.query(pageIndex, pageSize, key, startTime, endTime,orderMode);
    }
    
    @Override
    public int getQueryCount( String key, String startTime, String endTime) throws LabelException {
        return labelMapper.getQueryCount(key, startTime, endTime);
    }
    
    /**
     * 从excel文件解析订单
     * 
     * @param in
     * @return
     */
    @Override
    public void analysisExcel(InputStream in, AdminUser admin) throws Exception {
 
        jxl.Workbook rwb = Workbook.getWorkbook(in);
        Sheet sheet = rwb.getSheet(0);
 
        Date nowTime = new Date();
        
        Set<String> titles = new HashSet<String>();
        for (int r = 1; r < sheet.getRows(); r++) {
        
            for (int c = 0; c < sheet.getColumns(); c++) {
                String content = sheet.getCell(c, r).getContents();
 
                if (StringUtil.isNullOrEmpty(content)) 
                    break;// 为空则结束当前行遍历
                
                // 去掉重复字段
                titles.add(content.trim());
            }
        }
        
        
        
        if (titles != null && titles.size() > 0) {
            
            for (String title : titles) {
                
                List<Label> labels = selectByTitle(title.trim());
                // 若已存在则不存入数据库
                if (labels == null || labels.size() == 0) {
                    Label label = new Label();
                    
                    label.setTitle(title.trim()); // 名称
                    label.setEntrymode(Label.MODE_EXCEL); // 批量录入
                    label.setCreatetime(nowTime);
                    label.setUpdatetime(nowTime);
                    label.setCreateUser(admin);
                    
                    insertSelective(label);
                }
 
            }
        }
 
    }
    
    @Override
    public int uploadPicture(MultipartFile file, Label label) throws Exception {
        
        InputStream inputStream = file.getInputStream();
        String contentType = file.getContentType();
        String type = contentType.substring(contentType.indexOf("/") + 1);
        // 上传文件相对位置
        String fileUrl=FilePathEnum.lable.getPath() + UUID.randomUUID().toString().replace("-", "") + "." + type;
        
        boolean deleteFile =true;
        
        /*  修改图片时,先删除已存在图片  */
        String picture = label.getPicture();
        if (!StringUtil.isNullOrEmpty(picture)) 
            deleteFile = COSManager.getInstance().deleteFile(picture);
        
        
        int result = 2;
        
        /*  上传新图片  */
        if (deleteFile) {
            
            String uploadFilePath = COSManager.getInstance().uploadFile(inputStream, fileUrl).getUrl();
            
            /*  更新数据库信息  */
            if (!StringUtil.isNullOrEmpty(uploadFilePath)) {
                
                Date nowTime = new Date();
                
                label.setPicture(uploadFilePath);
                label.setUpdatetime(nowTime);
                
                result = updateByPrimaryKeySelective(label);
            }
        }
        
        return result;
        
    }
    
    /**
     * 删除图片
     */
    @Override
    public int deleteImg(Label label) throws LabelException {
        
        String fileUrl = label.getPicture();
        
        int result = 2;
        boolean deleteFile = true;
        
        if (!StringUtil.isNullOrEmpty(fileUrl)) {
            
            deleteFile = COSManager.getInstance().deleteFile(fileUrl);
            
            if (deleteFile) {
                Date nowTime = new Date();
                
                label.setPicture(null);
                label.setUpdatetime(nowTime);
                // 更新数据库
                result = updateByPrimaryKey(label);
            }
            
        }
        return result;
        
    }
    
    
    @Override
    public Map<String, Object> getCountByEntryMode() throws LabelException{
        return labelMapper.getCountByEntryMode();
    }
 
    @Override
    public long getCountToday() throws LabelException{
        return labelMapper.getCountToday();
    }
 
    @Override
    public long getCount() {
        return labelMapper.getCount();
    }
    
    @Override
    public List<Label> queryGoodsCandidate(int start, int count, String key, Long classId) throws LabelException{
        return labelMapper.queryGoodsCandidate(start, count, key, classId);
    }
    
    @Override
    public int getCountQueryGoodsCandidate( String key, Long classId) throws LabelException{
        return labelMapper.getCountQueryGoodsCandidate(key, classId);
    }
    
    @Override
    public List<Label> queryClassCandidate(int start, int count, String key, Long classId) throws LabelException{
        return labelMapper.queryClassCandidate(start, count, key, classId);
    }
    
    @Override
    public int getCountQueryClassCandidate( String key, Long classId) throws LabelException{
        return labelMapper.getCountQueryClassCandidate(key, classId);
    }
    
    @Override
    public List<Label> querySubClassCandidate(int start, int count, String key, Long subClassId) throws LabelException{
        return labelMapper.querySubClassCandidate(start, count, key, subClassId);
    }
 
    @Override
    public int getCountQuerySubClassCandidate( String key, Long subClassId) throws LabelException{
        return labelMapper.getCountQuerySubClassCandidate(key, subClassId);
    }
 
    @Override
    public List<LabelGoods> getByGoodsId(Long goodsId) {
        return labelMapper.getByGoodsId(goodsId);
    }
    
    @Override
    public List<Label> addBatchByNames(String lableNames, AdminUser admin) throws LabelException{
        if (lableNames == null || lableNames.trim().length() == 0) {
            return null;
        }
 
        List<Label> listLabs = new ArrayList<Label>();
        // 空格隔开
        String[] arrtitles = lableNames.split("\\s+");
        
        for (String title : arrtitles) {
            /* 遍历标签名称:查询数据库中是否已存在 */
            List<Label> labels = selectByTitle(title.trim());
            if (labels != null && labels.size() > 0) {
                Label label = labels.get(0);
                listLabs.add(label);
            } else {
                // 新增
                Label label = new Label();
                label.setTitle(title);// 名称
                label.setEntrymode(Label.MODE_SYSTEM); // 批量录入
                label.setCreatetime(new Date());
                label.setUpdatetime(new Date());
                label.setCreateUser(admin);
                label.setIosClick(0l);
                label.setAndroidClick(0l);
                insertSelective(label);
                
                listLabs.add(label);
            }
        }
        
        return listLabs;
    }
    
}