mitao
2024-05-23 79e1fa7c5b198d16f43832bb5896b5072f3e5f11
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
package com.ruoyi.goods.service.impl;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.enums.ListingStatusEnum;
import com.ruoyi.common.core.enums.StartStatusEnum;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.page.BeanUtils;
import com.ruoyi.common.core.utils.page.PageDTO;
import com.ruoyi.goods.controller.management.DTO.GoodsGroupPurchaseDTO;
import com.ruoyi.goods.controller.management.DTO.GoodsGroupPurchaseQuery;
import com.ruoyi.goods.controller.management.VO.GoodsGroupPurchaseVO;
import com.ruoyi.goods.domain.GoodsGroupPurchase;
import com.ruoyi.goods.mapper.GoodsGroupPurchaseMapper;
import com.ruoyi.goods.service.IGoodsGroupPurchaseService;
import com.ruoyi.system.api.domain.dto.ListStatusDTO;
import com.ruoyi.system.api.feignClient.OrderClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
 
/**
 * <p>
 * 商品团购表 服务实现类
 * </p>
 *
 * @author mitao
 * @since 2024-05-16
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class GoodsGroupPurchaseServiceImpl extends ServiceImpl<GoodsGroupPurchaseMapper, GoodsGroupPurchase> implements IGoodsGroupPurchaseService {
 
    private final OrderClient orderClient;
 
    /**
     * 获取团购商品列表的分页数据
     *
     * @param query 团购商品查询对象
     * @return PageDTO < GoodsGroupPurchaseVO>
     */
    @Override
    public PageDTO<GoodsGroupPurchaseVO> getGoodsGroupPurchasePage(GoodsGroupPurchaseQuery query) {
        return PageDTO.of(
                baseMapper.getGoodsGroupPurchasePage(query.getGoodsSkuName(),
                        new Page<>(query.getPageCurr(), query.getPageSize())));
    }
 
    /**
     * 添加/编辑 团购商品
     *
     * @param dto 团购商品数据传输对象
     */
    @Override
    public void saveGoodsGroupPurchase(GoodsGroupPurchaseDTO dto) {
        GoodsGroupPurchase goodsGroupPurchase = BeanUtils.copyBean(dto, GoodsGroupPurchase.class);
        if (StringUtils.isNull(dto.getId())) {
            this.save(goodsGroupPurchase);
        } else {
            GoodsGroupPurchase groupPurchase = this.getById(dto.getId());
            if (StringUtils.isNull(groupPurchase)) {
                throw new ServiceException("团购商品不存在");
            }
            this.updateById(goodsGroupPurchase);
        }
        //TODO 添加调度任务 处理团购商品开始结束
    }
 
    /**
     * 查看详情
     *
     * @param id 团购商品id
     * @return GoodsGroupPurchaseVO 团购商品视图对象
     */
    @Override
    public GoodsGroupPurchaseVO getGoodsGroupPurchaseDetail(Long id) {
        GoodsGroupPurchase groupPurchase = this.getById(id);
        if (StringUtils.isNull(groupPurchase)) {
            throw new ServiceException("团购商品不存在");
        }
        GoodsGroupPurchaseVO vo = BeanUtils.copyBean(groupPurchase,
                GoodsGroupPurchaseVO.class);
        //远程调用订单服务查询商品购买总数
        Integer num = orderClient.getGroupPurchasesGoodsNum(groupPurchase.getId()).getData();
        vo.setBuyNumber(num);
        return vo;
    }
 
    /**
     * 下架/上架 团购商品
     *
     * @param dto 商品上下架状态对象
     */
    @Override
    public void updStatus(ListStatusDTO dto) {
        GoodsGroupPurchase groupPurchase = this.getById(dto.getId());
        if (StringUtils.isNull(groupPurchase)) {
            throw new ServiceException("团购商品不存在");
        }
        //判断商品状态
        if (dto.getListingStatus().equals(ListingStatusEnum.REMOVED_FROM_THE_SHELF)
                && groupPurchase.getStartStatus().equals(StartStatusEnum.STARTED)) {
            throw new ServiceException("该商品已开始团购,不能下架");
        }
        //修改状态
        groupPurchase.setListingStatus(dto.getListingStatus());
        this.updateById(groupPurchase);
    }
 
    /**
     * 团购商品开始团购
     *
     * @param groupPurchaseId 团购商品id
     */
    @Override
    public void startGroupPurchase(Long groupPurchaseId) {
        log.info(">>>>>>>>>>>>>>>>>>>>{}团购商品开始团购<<<<<<<<<<<<<<<<<<<<", groupPurchaseId);
        GoodsGroupPurchase groupPurchase = this.getById(groupPurchaseId);
        if (StringUtils.isNotNull(groupPurchase)) {
            this.lambdaUpdate().set(GoodsGroupPurchase::getStartStatus, StartStatusEnum.STARTED)
                    .eq(GoodsGroupPurchase::getId, groupPurchaseId).update();
            //TODO 通知小程序
        }
    }
 
    /**
     * 团购商品结束团购
     *
     * @param groupPurchaseId 团购商品id
     */
    @Override
    public void endGroupPurchase(Long groupPurchaseId) {
        log.info(">>>>>>>>>>>>>>>>>>>>{}团购商品结束团购<<<<<<<<<<<<<<<<<<<<", groupPurchaseId);
        GoodsGroupPurchase groupPurchase = this.getById(groupPurchaseId);
        if (StringUtils.isNotNull(groupPurchase)) {
            this.lambdaUpdate().set(GoodsGroupPurchase::getStartStatus, StartStatusEnum.ENDED)
                    .eq(GoodsGroupPurchase::getId, groupPurchaseId).update();
            //TODO 通知小程序
        }
    }
}