doum
2025-12-12 89029e9ade03f3139c6afcd6bac48d9a668875f3
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
package com.doumee.service.business.impl;
 
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.LoginUserInfo;
import com.doumee.core.model.PageData;
import com.doumee.core.model.PageWrap;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.Utils;
import com.doumee.dao.business.CouponJoinMapper;
import com.doumee.dao.business.CouponMapper;
import com.doumee.dao.business.MemberCouponMapper;
import com.doumee.dao.business.ShopMapper;
import com.doumee.dao.business.model.*;
 
import com.doumee.dao.web.dto.CouponDTO;
import com.doumee.service.business.CouponService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.util.Date;
import java.util.List;
import java.util.Objects;
 
/**
 * 优惠券信息表Service实现
 * @author 江蹄蹄
 * @date 2023/03/21 15:48
 */
@Service
public class CouponServiceImpl implements CouponService {
 
    @Autowired
    private CouponMapper couponMapper;
 
    @Autowired
    private CouponJoinMapper couponJoinMapper;
 
 
    @Autowired
    private MemberCouponMapper memberCouponMapper;
 
    @Autowired
    private ShopMapper shopMapper;
 
    @Override
    public Integer create(Coupon coupon) {
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        isCreateParamValid(coupon);
 
        /**平台优惠券是兑换优惠券,商家优惠券是领取优惠券*/
        if(Constants.equalsInteger(coupon.getType(),Constants.ZERO)){
            coupon.setGetMethod(Constants.ZERO);
        }else if(Constants.equalsInteger(coupon.getType(),Constants.ONE)){
          coupon.setGetMethod(Constants.ONE);
        }
        coupon.setIsdeleted(Constants.ZERO);
        coupon.setCreator(user.getId());
        coupon.setCreateDate(new Date());
        coupon.setStatus(Constants.ONE);
        couponMapper.insert(coupon);
        return coupon.getId();
    }
 
    public void isCreateParamValid(Coupon coupon){
 
        if(StringUtils.isBlank(coupon.getName())
                || coupon.getType()==null
                ||coupon.getLimitPrice()==null
                ||coupon.getPrice()==null
                ||coupon.getNum()==null
              /*  ||coupon.getIntegral()==null*/
                ||coupon.getStartDate()==null
                ||coupon.getEndDate()==null
                ||coupon.getValidDays()==null
 
        ){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), ResponseStatus.BAD_REQUEST.getMessage());
        }
        if(Constants.equalsInteger(coupon.getType(),Constants.ZERO)){
            if(coupon.getShopId()==null){
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), ResponseStatus.BAD_REQUEST.getMessage());
            }
            QueryWrapper<Shop> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(Shop::getIsdeleted,Constants.ZERO);
            queryWrapper.lambda().eq(Shop::getId,coupon.getShopId());
            Shop shop=shopMapper.selectOne(queryWrapper);
            if(Objects.isNull(shop)){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "关联的店铺不存在!");
            }
        }
    }
 
    @Override
    public void deleteById(Integer id) {
 
        Coupon query= couponMapper.selectById(id);
        initCouponStatus(query);
        if(!Constants.equalsInteger(query.getCouponStatus(),Constants.ZERO)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "活动已开始/已结束状态,不支持删除操作");
        }
        Coupon coupon=new Coupon();
        coupon.setId(id);
        coupon.setIsdeleted(Constants.ONE);
        couponMapper.updateById(coupon);
    }
 
    @Override
    public void delete(Coupon coupon) {
        UpdateWrapper<Coupon> deleteWrapper = new UpdateWrapper<>(coupon);
        couponMapper.delete(deleteWrapper);
    }
 
    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        couponMapper.deleteBatchIds(ids);
    }
 
    @Override
    public void updateById(Coupon coupon) {
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        isCreateParamValid(coupon);
        Coupon query= couponMapper.selectById(coupon.getId());
        initCouponStatus(query);
        if(!Constants.equalsInteger(query.getCouponStatus(),Constants.ZERO)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "活动已开始/已结束状态,不支持编辑操作");
        }
        coupon.setEditor(user.getId());
        coupon.setEditDate(new Date());
        couponMapper.updateById(coupon);
    }
 
 
    @Override
    public void updateStatus(Coupon coupon) {
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(coupon.getId()==null||coupon.getStatus()==null){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), ResponseStatus.BAD_REQUEST.getMessage());
        }
        coupon.setEditor(user.getId());
        coupon.setEditDate(new Date());
        couponMapper.updateById(coupon);
    }
 
    @Override
    public void updateByIdInBatch(List<Coupon> coupons) {
        if (CollectionUtils.isEmpty(coupons)) {
            return;
        }
        for (Coupon coupon: coupons) {
            this.updateById(coupon);
        }
    }
 
    @Override
    public Coupon findById(Integer id) {
        MPJLambdaWrapper<Coupon> queryWrapper = new MPJLambdaWrapper<>();
 
        queryWrapper.selectAs(Shop::getName,Coupon::getShopName);
        queryWrapper.eq(Coupon::getId,id);
        queryWrapper.leftJoin(Shop.class,Shop::getId,Coupon::getShopId);
        Coupon coupon=couponJoinMapper.selectJoinOne(Coupon.class,queryWrapper);
        return coupon;
    }
 
    @Override
    public Coupon findOne(Coupon coupon) {
        QueryWrapper<Coupon> wrapper = new QueryWrapper<>(coupon);
        return couponMapper.selectOne(wrapper);
    }
 
    @Override
    public List<Coupon> findList(Coupon coupon) {
        QueryWrapper<Coupon> wrapper = new QueryWrapper<>(coupon);
        return couponMapper.selectList(wrapper);
    }
  
    @Override
    public PageData<Coupon> findPage(PageWrap<Coupon> pageWrap) {
        IPage<Coupon> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        MPJLambdaWrapper<Coupon> queryWrapper = new MPJLambdaWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        queryWrapper.selectAll(Coupon.class);
        queryWrapper.select("(select count(id) from member_coupon where ISDELETED=0 and COUPON_ID=t.id) as received");
        queryWrapper.select("(select count(id) from member_coupon where ISDELETED=0 and COUPON_ID=t.id and STATUS=0) as unused");
        queryWrapper.select("(select count(id) from member_coupon where ISDELETED=0 and COUPON_ID=t.id and STATUS=1) as used");
 
        queryWrapper.like(StringUtils.isNotBlank(pageWrap.getModel().getName()),Coupon::getName,pageWrap.getModel().getName());
        queryWrapper.eq(Coupon::getIsdeleted,Constants.ZERO);
        queryWrapper.orderByDesc(Coupon::getCreateDate);
        IPage<Coupon> result =  couponJoinMapper.selectJoinPage(page,Coupon.class, queryWrapper);
        for(Coupon model:result.getRecords()){
            initCouponStatus(model);
        }
 
        return PageData.from(result);
    }
 
 
    public  void initCouponStatus(Coupon coupon){
        if(coupon==null){
            return ;
        }
        long now =System.currentTimeMillis();
         coupon.setCouponStatus(Constants.ZERO);
        if(coupon.getStartDate()!=null&&coupon.getEndDate()!=null&&coupon.getStartDate().getTime()<=now && coupon.getEndDate().getTime()>=now){
            coupon.setCouponStatus(Constants.ONE);
        }else if(coupon.getEndDate()!=null && coupon.getEndDate().getTime()<now){
            coupon.setCouponStatus(Constants.TWO);
        }
 
    }
    @Override
    public long count(Coupon coupon) {
        QueryWrapper<Coupon> wrapper = new QueryWrapper<>(coupon);
        return couponMapper.selectCount(wrapper);
    }
 
    @Override
    public CouponDTO findCouponDTO(Integer shopId, Integer memberId) {
 
        QueryWrapper<Coupon> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Coupon::getShopId,shopId)
                .orderByDesc(Coupon::getCreateDate).last("limit 1");
        Coupon coupon = couponMapper.selectOne(wrapper);
        QueryWrapper<MemberCoupon> memberCouponWrapper = new QueryWrapper<>();
        memberCouponWrapper.lambda()
                .eq(MemberCoupon::getMemberId,memberId)
                .eq(MemberCoupon::getCouponId,coupon.getId());
        MemberCoupon memberCoupon = memberCouponMapper.selectById(memberCouponWrapper);
 
        memberCouponWrapper.clear();
        memberCouponWrapper.lambda().eq(MemberCoupon::getCouponId,coupon.getId());
//        Integer integer = memberCouponMapper.selectCount(memberCouponWrapper);
 
        CouponDTO couponDTO = new CouponDTO();
        BeanUtils.copyProperties(coupon,couponDTO);
        couponDTO.setDrawStatus(Objects.isNull(memberCoupon) ? "un_draw":"has_draw");
        return couponDTO;
    }
 
 
    @Override
    public PageData<CouponDTO> findCouponDTO(PageWrap<CouponDTO> pageWrap) {
 
        MPJLambdaWrapper<Coupon> wrapper = new MPJLambdaWrapper<>();
        IPage<Coupon> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        MPJLambdaWrapper<Coupon> queryWrapper = new MPJLambdaWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        queryWrapper.selectAs(Coupon::getId,CouponDTO::getId)
                    .selectAs(Coupon::getLimitPrice,CouponDTO::getLimitPrice)
                    .selectAs(Coupon::getPrice,CouponDTO::getPrice)
                    .selectAs(Coupon::getName,CouponDTO::getName)
                    .selectAs(Coupon::getStartDate,CouponDTO::getStartDate)
                    .selectAs(Coupon::getEndDate,CouponDTO::getEndDate)
                    .selectAs(Coupon::getNum,CouponDTO::getNum)
                    .selectCount(MemberCoupon::getCouponId,CouponDTO::getDrawNum);
        queryWrapper.leftJoin(MemberCoupon.class,MemberCoupon::getCouponId,Coupon::getId);
 
        if ("not_start".equalsIgnoreCase(pageWrap.getModel().getCouponStatus())){
            queryWrapper.lt(Coupon::getStartDate,new Date());
        }
        if ("having".equalsIgnoreCase(pageWrap.getModel().getCouponStatus())){
            queryWrapper.ge(Coupon::getStartDate,new Date());
            queryWrapper.le(Coupon::getStartDate,new Date());
        }
        if ("over".equalsIgnoreCase(pageWrap.getModel().getCouponStatus())){
            queryWrapper.gt(Coupon::getStartDate,new Date());
        }
 
        IPage<CouponDTO> couponDTOIPage = couponJoinMapper.selectJoinPage(page, CouponDTO.class, wrapper);
 
        if (CollectionUtils.isEmpty(couponDTOIPage.getRecords())){
            return PageData.from(new Page<>());
        }
        Date now = new Date();
        couponDTOIPage.getRecords().stream().forEach(s->{
            if (now.before(s.getStartDate())){
                s.setCouponStatus("not_start");
            }
            if (now.after(s.getStartDate())){
                s.setCouponStatus("over");
            }
        });
        return PageData.from(couponDTOIPage);
    }
}