xiaoyong931011
2021-08-18 a8fb0a175ff61c82819e7da191030c2f5e90ae98
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
package com.xzx.gc.shop.service;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzx.gc.common.constant.Constants;
import com.xzx.gc.common.utils.MqUtil;
import com.xzx.gc.entity.*;
import com.xzx.gc.model.admin.GoodsCategoryModel;
import com.xzx.gc.model.order.OrderInfoVo;
import com.xzx.gc.shop.dto.*;
import com.xzx.gc.shop.mapper.*;
import com.xzx.gc.shop.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
 
import javax.annotation.Resource;
import java.util.*;
 
@Service
@Transactional
@Slf4j
public class GoodsService {
 
    @Resource
    ScoreGoodsCategoryMapper scoreGoodsCategoryMapper;
    @Resource
    ScoreGoodsMapper scoreGoodsMapper;
    @Resource
    ScoreGoodsSkuMapper scoreGoodsSkuMapper;
    @Resource
    ScoreGoodsImagesMapper scoreGoodsImagesMapper;
    @Resource
    ScoreGoodsStyleMapper scoreGoodsStyleMapper;
    @Resource
    ScoreGoodsSearchMapper scoreGoodsSearchMapper;
 
    @Autowired
    private MqUtil mqUtil;
 
    public Map<String, Object> queryGoodsCategoryList(QueryGoodsCategoryListDto model) {
        String name = model.getName();
        GoodsCategoryModel goodsCategoryModel = new GoodsCategoryModel();
        goodsCategoryModel.setName(name);
        PageHelper.startPage(model.getPage(), model.getLimit());
 
        List<QueryGoodsCategoryListVo> maps = scoreGoodsCategoryMapper.queryGoodsCategoryList(goodsCategoryModel);
        PageInfo pageInfo = new PageInfo(maps);
        int count = Convert.toInt(pageInfo.getTotal());
        Map<String, Object> map = new HashMap<>();
        map.put("data", maps);
        map.put("count", count);
        map.put("code", 0);
        return map;
    }
 
    public Long addGoodsCategory(GoodsCategoryModel model) {
        ScoreGoodsCategory scoreGoodsCategory = new ScoreGoodsCategory();
        scoreGoodsCategory.setName(model.getName());
        scoreGoodsCategory.setCategoryIden(model.getCategoryIden());
        scoreGoodsCategory.setParentId(model.getParentId());
        scoreGoodsCategory.setCreatedBy(model.getCreatedBy());
        scoreGoodsCategory.setCreatedTime(model.getCreatedTime());
        scoreGoodsCategoryMapper.insertSelective(scoreGoodsCategory);
        return scoreGoodsCategory.getId();
    }
 
    public void deleteGoodsCategory(long id) {
        scoreGoodsCategoryMapper.deleteByPrimaryKey(id);
    }
 
    public ViewGoodsCategoryVo viewGoodsCategoryById(long id) {
        ScoreGoodsCategory scoreGoodsCategory = scoreGoodsCategoryMapper.selectByPrimaryKey(id);
        ViewGoodsCategoryVo viewGoodsCategoryVo = new ViewGoodsCategoryVo();
        viewGoodsCategoryVo.setId(scoreGoodsCategory.getId());
        viewGoodsCategoryVo.setName(scoreGoodsCategory.getName());
        viewGoodsCategoryVo.setCategoryIden(scoreGoodsCategory.getCategoryIden());
        viewGoodsCategoryVo.setParentId(scoreGoodsCategory.getParentId());
        return viewGoodsCategoryVo;
    }
 
    public void updateGoodsCategory(GoodsCategoryModel model) {
        long id = model.getId();
        ScoreGoodsCategory scoreGoodsCategory = scoreGoodsCategoryMapper.selectByPrimaryKey(id);
        scoreGoodsCategory.setName(model.getName());
        scoreGoodsCategory.setCategoryIden(model.getCategoryIden());
        scoreGoodsCategory.setParentId(model.getParentId());
        scoreGoodsCategoryMapper.updateByPrimaryKey(scoreGoodsCategory);
    }
 
    public Map<String, Object> queryGoodsList(QueryGoodsListDto model) {
        String name = model.getName() == null ? "":model.getName();
        int delFlag = model.getDelFlag() == null ? 2:model.getDelFlag();
        PageHelper.startPage(model.getPage(), model.getLimit());
 
        List<QueryGoodsListVo> maps = scoreGoodsMapper.queryGoodsList(name,delFlag);
        if(CollUtil.isNotEmpty(maps)){
            for(QueryGoodsListVo queryGoodsListVo : maps){
                long id = queryGoodsListVo.getId();
                Integer stock = scoreGoodsSkuMapper.selectScoreGoodsSkuMapperByGoodId(id);
                queryGoodsListVo.setStock(stock);
            }
        }
        PageInfo pageInfo = new PageInfo(maps);
        int count = Convert.toInt(pageInfo.getTotal());
        Map<String, Object> map = new HashMap<>();
        map.put("data", maps);
        map.put("count", count);
        map.put("code", 0);
        return map;
    }
 
    public Long addGoods(AddGoodsDto model) {
        //新增商品主信息
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        ScoreGoods scoreGoods = objectMapper.convertValue(model, ScoreGoods.class);
        scoreGoods.setDelFlag(Convert.toShort(Constants.DEL_NOT_FLAG));
        scoreGoodsMapper.insertSelective(scoreGoods);
        //样式信息
        List<ScoreGoodsStyleDto> scoreGoodsStyleDtos = model.getScoreGoodsStyles();
        if(CollUtil.isNotEmpty(scoreGoodsStyleDtos)){
            for(ScoreGoodsStyleDto scoreGoodsStyleDto : scoreGoodsStyleDtos){
                ScoreGoodsStyle scoreGoodsStyle = objectMapper.convertValue(scoreGoodsStyleDto, ScoreGoodsStyle.class);
                scoreGoodsStyle.setGoodsId(scoreGoods.getId());
                scoreGoodsStyle.setDelFlag(Convert.toShort(Constants.DEL_NOT_FLAG));
                scoreGoodsStyleMapper.insertSelective(scoreGoodsStyle);
                //规格信息
                List<ScoreGoodsSkuDto> scoreGoodsSkuDtos = scoreGoodsStyleDto.getScoreGoodsSkus();
                if(CollUtil.isNotEmpty(scoreGoodsSkuDtos)){
                    for(ScoreGoodsSkuDto scoreGoodsSkuDto : scoreGoodsSkuDtos){
                        ScoreGoodsSku scoreGoodsSku = objectMapper.convertValue(scoreGoodsSkuDto, ScoreGoodsSku.class);
                        scoreGoodsSku.setGoodsId(scoreGoods.getId());
                        scoreGoodsSku.setStyleId(scoreGoodsStyle.getId());
                        scoreGoodsSku.setDelFlag(Convert.toShort(Constants.DEL_NOT_FLAG));
                        scoreGoodsSkuMapper.insertSelective(scoreGoodsSku);
                    }
                }
            }
        }
        //轮播图信息
        List<String> goodsImages = model.getGoodsImages();
        if(CollUtil.isNotEmpty(goodsImages)){
            for(String goodImage : goodsImages){
                ScoreGoodsImages scoreGoodsImages = new ScoreGoodsImages();
                scoreGoodsImages.setImageUrl(goodImage);
                scoreGoodsImages.setGoodsId(scoreGoods.getId());
                scoreGoodsImages.setDelFlag(Convert.toShort(Constants.DEL_NOT_FLAG));
                scoreGoodsImagesMapper.insert(scoreGoodsImages);
            }
        }
        return scoreGoods.getId();
    }
 
    public void deleteGoods(long id) {
 
        scoreGoodsMapper.updateDelFlagById(id,Convert.toShort(Constants.DEL_FLAG));
 
        Example exampleStyle = new Example(ScoreGoodsStyle.class);
        Example.Criteria criteriaStyle = exampleStyle.createCriteria();
        criteriaStyle.andEqualTo("goodsId",id);
        List<ScoreGoodsStyle> goodsStyles = scoreGoodsStyleMapper.selectByExample(exampleStyle);
        if(CollUtil.isNotEmpty(goodsStyles)){
            for(ScoreGoodsStyle scoreGoodsStyle : goodsStyles){
                scoreGoodsStyleMapper.updateDelFlagById(scoreGoodsStyle.getId(),Convert.toShort(Constants.DEL_FLAG));
            }
        }
 
        Example exampleSku = new Example(ScoreGoodsStyle.class);
        Example.Criteria criteriaSku = exampleSku.createCriteria();
        criteriaSku.andEqualTo("goodsId",id);
        List<ScoreGoodsSku> goodsSkus = scoreGoodsSkuMapper.selectByExample(exampleSku);
        if(CollUtil.isNotEmpty(goodsSkus)){
            for(ScoreGoodsSku scoreGoodsSku : goodsSkus){
                scoreGoodsSkuMapper.updateDelFlagById(scoreGoodsSku.getId(),Convert.toShort(Constants.DEL_FLAG));
            }
        }
    }
 
    public ViewGoodsVo viewGoods(long id) {
        ViewGoodsVo viewGoodsVo = new ViewGoodsVo();
        ScoreGoods scoreGoods = scoreGoodsMapper.selectByPrimaryKey(id);
        if(ObjectUtil.isNotEmpty(scoreGoods)){
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            viewGoodsVo = objectMapper.convertValue(scoreGoods, ViewGoodsVo.class);
//            viewGoodsVo.setCreateTime(scoreGoods.getCreatedTime().toString());
            //轮播图
            List<String> goodsImages = scoreGoodsImagesMapper.selectScoreGoodsImagesByGoodsId(id);
            viewGoodsVo.setGoodsImages(goodsImages);
            //样式
            List<ScoreGoodsStyleVo> scoreGoodsStyleVos = new ArrayList<>();
            List<ScoreGoodsStyle> goodsStyles = scoreGoodsStyleMapper.selectScoreGoodsStyleByGoodsId(id);
            if(CollUtil.isNotEmpty(goodsStyles)){
                for(ScoreGoodsStyle goodsStyle : goodsStyles){
                    ScoreGoodsStyleVo scoreGoodsStyleVo = objectMapper.convertValue(goodsStyle, ScoreGoodsStyleVo.class);
                    //获取规格
                    Long styleId = goodsStyle.getId();
                    List<ScoreGoodsSkuVo> goodsSkus = scoreGoodsSkuMapper.selectScoreGoodsSkuByGoodsIdAndStyleId(id,styleId);
                    scoreGoodsStyleVo.setScoreGoodsSkus(goodsSkus);
                    scoreGoodsStyleVos.add(scoreGoodsStyleVo);
                }
            }
            viewGoodsVo.setScoreGoodsStyles(scoreGoodsStyleVos);
        }
        return viewGoodsVo;
    }
 
    public void updateGoods(UpdateGoodsDto model) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//        ScoreGoods scoreGoods = objectMapper.convertValue(model, ScoreGoods.class);
        ScoreGoods scoreGoods = BeanUtil.copyProperties(model, ScoreGoods.class);
        log.info("抢购时间:{}, {}", scoreGoods.getQgStartTime(), scoreGoods.getQgEndTime());
        scoreGoodsMapper.updateByPrimaryKey(scoreGoods);
 
        //轮播图
        Example exampleImages = new Example(ScoreGoodsImages.class);
        Example.Criteria criteriaImages = exampleImages.createCriteria();
        criteriaImages.andEqualTo("goodsId",scoreGoods.getId());
        scoreGoodsImagesMapper.deleteByExample(exampleImages);
        List<String> goodsImages = model.getGoodsImages();
        if(CollUtil.isNotEmpty(goodsImages)){
            for(String goodImage : goodsImages){
                ScoreGoodsImages scoreGoodsImages = new ScoreGoodsImages();
                scoreGoodsImages.setImageUrl(goodImage);
                scoreGoodsImages.setGoodsId(scoreGoods.getId());
                scoreGoodsImages.setDelFlag(Convert.toShort(Constants.DEL_NOT_FLAG));
                scoreGoodsImagesMapper.insertSelective(scoreGoodsImages);
            }
        }
 
        List<ScoreGoodsStyleDto> scoreGoodsStyleDtos = model.getScoreGoodsStyles();
        if(CollUtil.isNotEmpty(scoreGoodsStyleDtos)){
            Example exampleStyle = new Example(ScoreGoodsStyle.class);
            Example.Criteria criteriaStyle = exampleStyle.createCriteria();
            criteriaStyle.andEqualTo("goodsId",scoreGoods.getId());
            scoreGoodsStyleMapper.deleteByExample(exampleImages);
 
            Example exampleSku = new Example(ScoreGoodsSku.class);
            Example.Criteria criteriaSku = exampleSku.createCriteria();
            criteriaSku.andEqualTo("goodsId",scoreGoods.getId());
            scoreGoodsSkuMapper.deleteByExample(exampleImages);
 
            for(ScoreGoodsStyleDto scoreGoodsStyleDto : scoreGoodsStyleDtos){
                ScoreGoodsStyle scoreGoodsStyle = new ScoreGoodsStyle();
                scoreGoodsStyle.setId(scoreGoodsStyleDto.getId());
                scoreGoodsStyle.setName(scoreGoodsStyleDto.getName());
                scoreGoodsStyle.setGoodsId(scoreGoods.getId());
                scoreGoodsStyleMapper.insertSelective(scoreGoodsStyle);
                //规格
                List<ScoreGoodsSkuDto> scoreGoodsSkuDtos = scoreGoodsStyleDto.getScoreGoodsSkus();
                if(CollUtil.isNotEmpty(scoreGoodsSkuDtos)){
                    for(ScoreGoodsSkuDto scoreGoodsSkuDto : scoreGoodsSkuDtos){
                        ScoreGoodsSku scoreGoodsSku = objectMapper.convertValue(scoreGoodsSkuDto, ScoreGoodsSku.class);
                        scoreGoodsSku.setGoodsId(scoreGoods.getId());
                        scoreGoodsSku.setStyleId(scoreGoodsStyle.getId());
                        scoreGoodsSkuMapper.insertSelective(scoreGoodsSku);
                    }
                }
            }
        }
    }
 
    public void saleGoods(long id, Integer issale) {
        ScoreGoods scoreGoods = new ScoreGoods();
        scoreGoods.setIsSale(issale);
        Example exampleGoods = new Example(ScoreGoods.class);
        Example.Criteria criteriaGoods = exampleGoods.createCriteria();
        criteriaGoods.andEqualTo("id",id);
        scoreGoodsMapper.updateByExampleSelective(scoreGoods,exampleGoods);
    }
 
    public void snapUpGoods(SnapUpGoodsDto model) {
        long id = model.getId();
        Integer isQg = model.getIsQg();
        if(ScoreGoods.ISQG_YES == isQg){
            ScoreGoods scoreGoods = new ScoreGoods();
            scoreGoods.setIsQg(ScoreGoods.ISQG_YES);
            scoreGoods.setQgStartTime(model.getQgStartTime());
            scoreGoods.setQgEndTime(model.getQgEndTime());
            Example exampleGoods = new Example(ScoreGoods.class);
            Example.Criteria criteriaGoods = exampleGoods.createCriteria();
            criteriaGoods.andEqualTo("id",id);
            scoreGoodsMapper.updateByExampleSelective(scoreGoods,exampleGoods);
        }else{
            ScoreGoods scoreGoods = new ScoreGoods();
            scoreGoods.setIsQg(ScoreGoods.ISQG_NO);
            scoreGoods.setQgEndTime(null);
            scoreGoods.setQgStartTime(null);
            Example exampleGoods = new Example(ScoreGoods.class);
            Example.Criteria criteriaGoods = exampleGoods.createCriteria();
            criteriaGoods.andEqualTo("id",id);
            scoreGoodsMapper.updateByExampleSelective(scoreGoods,exampleGoods);
        }
    }
 
    public List<ViewGoodsCategoryListVo> viewGoodsCategoryList(ViewGoodsCategoryListDto viewGoodsCategoryListDto) {
        List<ViewGoodsCategoryListVo> maps = new ArrayList<>();
        if(viewGoodsCategoryListDto.getParentId() == 0L){
            maps = scoreGoodsCategoryMapper.viewGoodsCategoryOneList(viewGoodsCategoryListDto.getParentId());
        }else{
            maps = scoreGoodsCategoryMapper.viewGoodsCategoryList();
        }
        return maps;
    }
 
    public List<GoodsCategoryVo> findCategoryWithChildren() {
        return scoreGoodsCategoryMapper.selectCategoryWithChildren();
    }
 
    public PageInfo<XcxGoodsListVo> findGoodsListInPage(XcxGoodsListDto xcxGoodsListDto) {
 
        PageHelper.startPage(xcxGoodsListDto.getPageNo(), xcxGoodsListDto.getPageSize());
        List<XcxGoodsListVo> data = scoreGoodsMapper.selectXcxGoodsList(xcxGoodsListDto);
        if(StrUtil.isNotEmpty(xcxGoodsListDto.getName())){
            Example exampleSearch = new Example(ScoreGoodsSearch.class);
            Example.Criteria criteriaSearch = exampleSearch.createCriteria();
            criteriaSearch.andEqualTo("contents",xcxGoodsListDto.getName());
            criteriaSearch.andEqualTo("userId",xcxGoodsListDto.getUserId());
            List<ScoreGoodsSearch> scoreGoodsSearches = scoreGoodsSearchMapper.selectByExample(exampleSearch);
            if(CollUtil.isEmpty(scoreGoodsSearches)){
                ScoreGoodsSearch scoreGoodsSearch = new ScoreGoodsSearch();
                scoreGoodsSearch.setUserId(xcxGoodsListDto.getUserId());
                scoreGoodsSearch.setContents(xcxGoodsListDto.getName());
                scoreGoodsSearch.setCreatedTime(new Date());
                scoreGoodsSearchMapper.insert(scoreGoodsSearch);
            }
        }
        return new PageInfo<>(data);
    }
 
    public XcxGoodsDetailVo findGoodsDetails(Long id) {
        XcxGoodsDetailVo data = scoreGoodsMapper.selectGoodsDetailsById(id);
        XcxGoodsListDto dto = new XcxGoodsListDto();
        dto.setId(id);
        List<XcxGoodsListVo> list = scoreGoodsMapper.selectXcxGoodsList(dto);
 
        List<String> images = scoreGoodsImagesMapper.selectScoreGoodsImagesByGoodsId(id);
        data.setImages(images);
        if (CollUtil.isNotEmpty(list)) {
            data.setQuantity(list.get(0).getQuantity());
            data.setStock(list.get(0).getStock());
        }
 
        if (ScoreGoods.ISQG_YES.equals(data.getIsQg())) {
            if (new Date().before(data.getQgStartTime())) {
                data.setIsStartQg(2);
            } else {
                data.setIsStartQg(1);
                Long remain = DateUtil.between(new Date(), data.getQgEndTime(), DateUnit.SECOND, false);
                data.setRemainTime(remain);
            }
        }
        return data;
    }
 
    public List<GoodsSearchVo> goodsSearch(GoodsSearchDto goodsSearchDto) {
        return scoreGoodsSearchMapper.goodsSearch(goodsSearchDto);
    }
 
    public void delGoodsSearch(Long id, String userId) {
        Example exampleSearch = new Example(ScoreGoodsSearch.class);
        Example.Criteria criteriaSearch = exampleSearch.createCriteria();
        criteriaSearch.andEqualTo("id",id);
        criteriaSearch.andEqualTo("userId",userId);
        scoreGoodsSearchMapper.deleteByExample(exampleSearch);
    }
 
    public void delAllSearch(String userId) {
        Example exampleSearch = new Example(ScoreGoodsSearch.class);
        Example.Criteria criteriaSearch = exampleSearch.createCriteria();
        criteriaSearch.andEqualTo("userId",userId);
        scoreGoodsSearchMapper.deleteByExample(exampleSearch);
    }
}