admin
2020-05-19 744594ef1a2f530fc3e86ea9dc48b62247f79420
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
package com.yeshi.fanli.service.impl.homemodule;
 
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
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.homemodule.HomeNavbarMapper;
import com.yeshi.fanli.entity.bus.clazz.GoodsClass;
import com.yeshi.fanli.entity.bus.homemodule.HomeNavbar;
import com.yeshi.fanli.entity.bus.homemodule.HomeNavbar.NavbarTypeEnum;
import com.yeshi.fanli.entity.bus.homemodule.SwiperBanner;
import com.yeshi.fanli.exception.homemodule.HomeNavbarException;
import com.yeshi.fanli.exception.homemodule.SpecialException;
import com.yeshi.fanli.service.inter.goods.GoodsClassService;
import com.yeshi.fanli.service.inter.homemodule.HomeNavbarService;
import com.yeshi.fanli.service.inter.homemodule.SwiperBannerService;
import com.yeshi.fanli.util.FilePathEnum;
import com.yeshi.fanli.util.StringUtil;
 
 
@Service
public class HomeNavbarServiceImpl implements HomeNavbarService {
    
    @Resource
    private HomeNavbarMapper homeNavbarMapper;
    
    @Resource
    private GoodsClassService goodsClassService;
    
    @Resource
    private SwiperBannerService swiperBannerService;
 
    @Override
    @Transactional
    public int deleteBatchByPrimaryKey(List<Long> list) {
        if (list == null || list.size() == 0) {
            return 0;
        }
        
        for (Long id: list) {
            HomeNavbar homeNavbar = homeNavbarMapper.selectByPrimaryKey(id);
            if (homeNavbar !=null) {
                // 删除已存在图片 /
                String picture = homeNavbar.getPicture();
                if (!StringUtil.isNullOrEmpty(picture)) {
                    COSManager.getInstance().deleteFile(picture);
                }
            }
        }
        return homeNavbarMapper.deleteBatchByPrimaryKey(list);
    }
 
    
 
    @Override
    public void saveObject(MultipartFile file, HomeNavbar record) throws HomeNavbarException, Exception{
        
        String name = record.getName();
        if (name == null || name.trim().length() == 0) {
            throw new HomeNavbarException(1, "导航名称不能为空");
        }
        
        Integer sex = null;
        NavbarTypeEnum type = record.getType();
        if (NavbarTypeEnum.category.equals(type)) {
            Long classId = record.getClassId();
            if (classId == null) {
                throw new HomeNavbarException(1, "请选择分类");
            }
            
            GoodsClass goodsClass = goodsClassService.getGoodsClass(record.getClassId());
            if (goodsClass == null) {
                throw new HomeNavbarException(1, "分类不存在");
            }
            sex = goodsClass.getSex();
            if (sex == null) {
                throw new HomeNavbarException(1, "分类未指定适用版本");
            }
            record.setUrl(null);
        } else if(NavbarTypeEnum.weex.equals(type) || NavbarTypeEnum.web.equals(type)) {
            String url = record.getUrl();
            if (url == null || url.trim().length() == 0) {
                throw new HomeNavbarException(1, "请输入URl链接");
            }
        } else {
            throw new HomeNavbarException(1, "类型的值不正确");
        }
        
        
        Long swiperBannerId = record.getSwiperBannerId();
        if (swiperBannerId != null && swiperBannerId == 0) {
            record.setSwiperBannerId(null);
        }
        
        // 时间转换
        handleTime(record);
        
        // 图片上传
        String picture = null;
        if (file != null) {
            picture = uploadPicture(file);
        }
        
        Boolean isDefault = record.getIsDefault();
        if (isDefault == null) {
            record.setIsDefault(false);
        }
        
        Boolean isFixed = record.getIsFixed();
        if (isFixed == null) {
            record.setIsFixed(false);
        }
        
        Long id = record.getId();
        if (id == null) {
            record.setPicture(picture);
            record.setCreatetime(new Date());
            record.setUpdatetime(new Date());
            
            Integer state = record.getState();
            if (state == null) {
                record.setState(0);
            }
            
            record.setOrderby(homeNavbarMapper.getDefaultMaxOrder() + 1);
            record.setOrderMan(homeNavbarMapper.getManMaxOrder() + 1);
            record.setOrderWoman(homeNavbarMapper.getWomanMaxOrder() + 1);
            
            record.setIsFixed(false);//目前无固定项
            homeNavbarMapper.insert(record);
        } else {
            // 修改
            HomeNavbar resultObj = homeNavbarMapper.selectByPrimaryKey(id);
            if (resultObj == null) {
                throw new HomeNavbarException(1, "修改内容已不存在");
            }
            
            if (picture != null && picture.trim().length() > 0) {
                // 删除老图
                removePicture(resultObj.getPicture());
                // 存储新图
                record.setPicture(picture);
            } else {
                record.setPicture(resultObj.getPicture());
            }
            
            record.setOrderby(resultObj.getOrderby());
            record.setOrderMan(resultObj.getOrderMan());
            record.setOrderWoman(resultObj.getOrderWoman());
            record.setCreatetime(resultObj.getCreatetime());
            record.setUpdatetime(new Date());
            record.setIsFixed(false);//目前无固定项
            homeNavbarMapper.updateByPrimaryKey(record);
        }
    }
 
    
    /**
     * 上传图片
     * @param file
     * @return
     * @throws Exception
     */
    public String uploadPicture(MultipartFile file) throws Exception {
        // 文件解析 
        InputStream inputStream = file.getInputStream();
        String contentType = file.getContentType();
        String type = contentType.substring(contentType.indexOf("/") + 1);
    
        // 文件路径
        String filePath= FilePathEnum.homeNavbar.getPath() +UUID.randomUUID().toString().replace("-", "") + "." + type;
        // 执行上传
        String fileLink= COSManager.getInstance().uploadFile(inputStream, filePath).getUrl();
        
        return fileLink;
    }
 
    /**
     * 删除图片-不更新数据库
     * @param record
     * @throws Exception
     */
    public void removePicture(String picture) throws Exception {
        if (picture != null && picture.trim().length() > 0) {
            COSManager.getInstance().deleteFile(picture);
        }
    }
    
    
    /**
     * web段时间转换
     * 
     * @param record
     */
    private void handleTime(HomeNavbar record) throws SpecialException, Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        
        String startTime_str = record.getStartTime_str();
        if (!StringUtil.isNullOrEmpty(startTime_str)) {
            startTime_str = startTime_str.replaceAll("T", " ");
            record.setStartTime(format.parse(startTime_str));
        }
        
        String endTime_str = record.getEndTime_str();
        if (!StringUtil.isNullOrEmpty(endTime_str)) {
            endTime_str = endTime_str.replaceAll("T", " ");
            record.setEndTime(format.parse(endTime_str));
        }
        
        if (record.getEndTime() != null && record.getStartTime() != null
                && record.getStartTime().getTime() > record.getEndTime().getTime()) {
            throw new SpecialException(1, "起始时间不能小于结束时间");
        }
    }
    
    
    @Override
    public void updateOrder(Long id, Integer moveType, Integer sex) throws HomeNavbarException, Exception{
 
        if (moveType == null || (!moveType.equals(1) && !moveType.equals(-1))) {
            throw new HomeNavbarException(1, "传递的类型不正确");
        }
        
        if (id == null) {
            throw new HomeNavbarException(1, "ID不能为空");
        }
        
        
        HomeNavbar resultObj = homeNavbarMapper.selectByPrimaryKey(id);
        if (resultObj == null) {
            throw new HomeNavbarException(1, "操作数据已不存在");
        }
            
        if (sex == null) {
            sex = GoodsClass.SEX_DEFAULT;
        }
        
        HomeNavbar changeObj = null;
        if (GoodsClass.SEX_ALL == sex || GoodsClass.SEX_DEFAULT == sex) {
            Integer orderby = resultObj.getOrderby();
            changeObj = homeNavbarMapper.getDefaultChangeOrder(moveType, orderby);
            if (changeObj == null ) {
                throw new HomeNavbarException(1, "已经在最边缘,无可交换的位置");
            }
            resultObj.setOrderby(changeObj.getOrderby());
            changeObj.setOrderby(orderby);
        } else if (GoodsClass.SEX_MAN == sex) {
            Integer orderMan = resultObj.getOrderMan();
            changeObj = homeNavbarMapper.getManChangeOrder(moveType, orderMan);
            if (changeObj == null ) {
                throw new HomeNavbarException(1, "已经在最边缘,无可交换的位置");
            }
            resultObj.setOrderMan(changeObj.getOrderMan());
            changeObj.setOrderMan(orderMan);
        } else if (GoodsClass.SEX_WOMAN == sex) {
            Integer orderWoman = resultObj.getOrderWoman();
            changeObj = homeNavbarMapper.getWomanChangeOrder(moveType, orderWoman);
            if (changeObj == null ) {
                throw new HomeNavbarException(1, "已经在最边缘,无可交换的位置");
            }
            resultObj.setOrderWoman(changeObj.getOrderWoman());
            changeObj.setOrderWoman(orderWoman);
        }
        
        if (changeObj == null ) {
            throw new HomeNavbarException(1, "无可交换的位置");
        }
        
        homeNavbarMapper.updateByPrimaryKeySelective(changeObj);
        homeNavbarMapper.updateByPrimaryKeySelective(resultObj);
    }
 
 
    
    @Override
    public List<HomeNavbar> listQuery(long start, int count, String key, Integer sex) {
        
        List<HomeNavbar> listObj = homeNavbarMapper.listQuery(start, count, key, sex);
        if (listObj == null || listObj.size() == 0) {
            return null;
        }
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
        
        for (HomeNavbar homeNavbar: listObj) {
            Date startTime = homeNavbar.getStartTime();
            if (startTime == null) {
                homeNavbar.setStartTime_str("");
            } else {
                homeNavbar.setStartTime_str(sdf.format(startTime));
            }
            
            Date endTime = homeNavbar.getEndTime();
            if (endTime == null) {
                homeNavbar.setEndTime_str("");
            } else {
                homeNavbar.setEndTime_str(sdf.format(endTime));
            }
            
            Long classId = homeNavbar.getClassId();
            if (classId != null) {
                GoodsClass goodsClass = goodsClassService.selectByPrimaryKey(classId);
                if(goodsClass != null) {
                    homeNavbar.setClassName(goodsClass.getName());
                }
            }
            
            Long swiperBannerId = homeNavbar.getSwiperBannerId();
            if (swiperBannerId != null) {
                SwiperBanner swiperBanner = swiperBannerService.selectByPrimaryKey(swiperBannerId);
                if(swiperBanner != null) {
                    homeNavbar.setSwiperName(swiperBanner.getTitle());
                }
            } else {
                // 初始化
                homeNavbar.setSwiperBannerId(0L);
            }
        }
        return listObj;
    }
    
    @Override
    public long countlistQuery(String key, Integer sex) {
        return homeNavbarMapper.countListQuery(key, sex);
    }
 
    @Override
    @Cacheable(value = "configCache", key = "'listQueryEffectiveNavbar'")
    public List<HomeNavbar> listQueryEffectiveNavbar() {
        return homeNavbarMapper.listQueryEffective();
    }
    
    
    @Override
    @Cacheable(value = "configCache", key = "'listQueryDefaultNavbar-'+#sex")
    public List<HomeNavbar> listQueryDefaultNavbar(Integer sex) {
        return homeNavbarMapper.listQueryDefaultNavbar(sex);
    }
    
    
    @Override
    @Cacheable(value = "configCache", key = "'listQueryFixedNavbar'")
    public List<HomeNavbar> listQueryFixedNavbar() {
        return homeNavbarMapper.listQueryFixedNavbar();
    }
 
    @Override
    public HomeNavbar getEffectiveByClassId(Long classId) {
        return homeNavbarMapper.getEffectiveByClassId(classId);
    }
 
    
    @Override
    public void switchState(Long id) throws HomeNavbarException {
        if (id == null) {
            throw new HomeNavbarException(1, "请传递正确参数");
        }
        
        HomeNavbar resultObj = homeNavbarMapper.selectByPrimaryKey(id);
        if (resultObj == null) {
            throw new HomeNavbarException(1, "此内容已不存在");
        }
        
        Integer state = resultObj.getState();
        if (state == null || state == 0) {
            state = 1;
        } else {
            state = 0;
        }
        
        HomeNavbar updateObj = new HomeNavbar();
        updateObj.setId(id);
        updateObj.setState(state);
        homeNavbarMapper.updateByPrimaryKeySelective(updateObj);
    }
    
}