gongzuming
2024-09-19 a768dc3daa04d35fedfbe75c0a59b9b2545b85c4
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
package com.mzl.flower.web.v2.coupon;
 
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mzl.flower.base.BaseController;
import com.mzl.flower.base.R;
import com.mzl.flower.base.ReturnDataDTO;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.dto.request.coupon.*;
import com.mzl.flower.dto.response.coupon.CouponTemplateActivyVO;
import com.mzl.flower.dto.response.coupon.CouponTemplatePointVO;
import com.mzl.flower.dto.response.coupon.CouponTemplateVO;
import com.mzl.flower.entity.coupon.CouponTemplateDO;
import com.mzl.flower.enums.*;
import com.mzl.flower.service.coupon.CouponTemplateService2;
import com.mzl.flower.utils.ConverterUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import java.math.BigDecimal;
import java.time.LocalDateTime;
 
 
/**
 * @author @TaoJie
 * @since 2024-08-22
 */
@RestController
@RequestMapping("/api/v2/coupon/avtivy")
@Api(value = "优惠券管理-活动", tags = "优惠券管理-活动")
@Validated
public class CouponTemplateActivyController extends BaseController {
 
    @Autowired
    CouponTemplateService2 couponTemplateService;
 
    @PostMapping("")
    @ApiOperation(value = "新增", notes = "新增")
    public ResponseEntity<ReturnDataDTO> create(@Validated @RequestBody CreateCouponTemplateActivyDTO dto) {
 
        // 信息验证
        valid(dto);
 
        CreateCouponTemplateBO couponTemplateBO = new CreateCouponTemplateBO();
        BeanUtils.copyProperties(dto, couponTemplateBO);
 
        // 设置成活动优惠券
        couponTemplateBO.setCategory(CouponCategoryEnum.ACTIVITY.getStatus());
        couponTemplateBO.setGetUserType(CouponGetUserTypeEnum.ALL.getType());
 
 
        couponTemplateService.createCouponTemplate(couponTemplateBO);
        return returnData(R.SUCCESS.getCode(), null);
 
 
    }
 
    @PutMapping("/{id}")
    @ApiOperation(value = "修改", notes = "修改")
    public ResponseEntity<ReturnDataDTO> update(@PathVariable String id,@Validated @RequestBody CreateCouponTemplateActivyDTO dto) {
 
        // 手动设置id值
        dto.setId(id);
        // 信息验证
        valid(dto);
 
        CouponTemplateDO couponTemplateDO = couponTemplateService.getById(dto.getId());
        if (null == couponTemplateDO) {
            throw new ValidationException("优惠券不存在");
        }
 
        CreateCouponTemplateBO couponTemplateBO = new CreateCouponTemplateBO();
        BeanUtils.copyProperties(couponTemplateDO,couponTemplateBO);
        BeanUtils.copyProperties(dto, couponTemplateBO);
 
        // 设置成活动优惠券
        couponTemplateBO.setCategory(CouponCategoryEnum.ACTIVITY.getStatus());
        couponTemplateBO.setGetUserType(CouponGetUserTypeEnum.ALL.getType());
 
        couponTemplateService.updateCouponTemplate(couponTemplateBO);
 
        return returnData(R.SUCCESS.getCode(), null);
    }
 
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除", notes = "删除")
    public ResponseEntity<ReturnDataDTO> delete(@PathVariable String id) {
 
        CouponTemplateDO couponTemplateDO = couponTemplateService.getById(id);
        if (null == couponTemplateDO) {
            throw new ValidationException("优惠券不存在");
        }
        couponTemplateService.deleteCouponTemplate(id);
        return returnData(R.SUCCESS.getCode(), null);
    }
 
    @GetMapping("/{id}")
    @ApiOperation(value = "详情", notes = "详情")
    public ResponseEntity<ReturnDataDTO> get(@PathVariable String id) {
        CouponTemplateVO couponTemplateVO = couponTemplateService.getDetailById(id);
        return returnData(R.SUCCESS.getCode(), ConverterUtil.transObject(couponTemplateVO, CouponTemplateActivyVO.class));
    }
 
    @GetMapping("/page")
    @ApiOperation(value = "查询-分页", notes = "查询-分页")
    public ResponseEntity<ReturnDataDTO<Page<CouponTemplatePointVO>>> page(Page page, QueryCouponDTO dto) {
        // 设置只查询活动优惠券的
        dto.setCategory(CouponCategoryEnum.ACTIVITY.getStatus());
        Page<CouponTemplateVO> resultPage = couponTemplateService.getPage(page, dto);
        return returnData(R.SUCCESS.getCode(), ConverterUtil.transPage(resultPage, CouponTemplateActivyVO.class));
    }
 
    @GetMapping("/list")
    @ApiOperation(value = "查询-全部", notes = "查询-全部")
    public ResponseEntity<ReturnDataDTO<Page<CouponTemplateVO>>> list(QueryCouponDTO dto) {
        // 设置只查询活动优惠券的
        dto.setCategory(CouponCategoryEnum.ACTIVITY.getStatus());
        return returnData(R.SUCCESS.getCode(), ConverterUtil.transList(couponTemplateService.getList(dto), CouponTemplateActivyVO.class));
    }
 
 
    @PutMapping("/active/{id}")
    @ApiOperation(value = "发布", notes = "发布")
    public ResponseEntity<ReturnDataDTO> active(@PathVariable String id) {
 
        CouponTemplateDO couponTemplateDO = couponTemplateService.getById(id);
        if (null == couponTemplateDO) {
            throw new ValidationException("优惠券不存在");
        }
 
        // 判断当前时间是否在优惠券的领取时间范围内
        LocalDateTime now = LocalDateTime.now();
//        if (couponTemplateDO.getGetStartDate() != null && couponTemplateDO.getGetEndDate() != null) {
//            if (now.isBefore(couponTemplateDO.getGetStartDate()) || now.isAfter(couponTemplateDO.getGetEndDate())) {
//                throw new ValidationException("当前时间不在优惠券领取时间范围内,不能发布优惠券。");
//            }
//        } else {
//            throw new ValidationException("优惠券的领取时间未设置。");
//        }
 
        // 检查结束时间是否存在
        if (couponTemplateDO.getGetEndDate() != null) {
            // 如果当前时间小于结束时间,允许发布
            if (now.isAfter(couponTemplateDO.getGetEndDate())) {
                throw new ValidationException("当前时间已超过优惠券领取结束时间,不能发布优惠券。");
            }
        } else {
            throw new ValidationException("优惠券的领取结束时间未设置。");
        }
 
 
        couponTemplateService.activeCouponTemplate(id);
 
        return returnData(R.SUCCESS.getCode(), null);
    }
 
    @PutMapping("/expire/{id}")
    @ApiOperation(value = "下架", notes = "下架")
    public ResponseEntity<ReturnDataDTO> expire(@PathVariable String id) {
 
        CouponTemplateDO couponTemplateDO = couponTemplateService.getById(id);
        if (null == couponTemplateDO) {
            throw new ValidationException("优惠券不存在");
        }
 
        couponTemplateService.expireCouponTemplate(id);
 
        return returnData(R.SUCCESS.getCode(), null);
    }
 
 
    private void valid(CreateCouponTemplateActivyDTO dto){
 
        if (StringUtils.isNotBlank(dto.getCouponDiscountType())
                && dto.getCouponDiscountType().equals(CouponTypeEnum.DISCOUNT.getType())
                && dto.getMinOrderAmount().compareTo(dto.getCouponDiscountValue()) < 0) {
            throw new ValidationException("订单金额不能小于折扣金额");
        }
        if (StringUtils.isNotBlank(dto.getCouponDiscountType())
                && dto.getCouponDiscountType().equals(CouponTypeEnum.DISCOUNT.getType())
                && dto.getCouponDiscountValue().compareTo(BigDecimal.ZERO) <= 0) {
            throw new ValidationException("折扣金额必须大于0");
        }
 
        if (StringUtils.isNotBlank(dto.getCouponDiscountType())
                && dto.getCouponDiscountType().equals(CouponTypeEnum.ZERO.getType())
                && dto.getMinOrderAmount().compareTo(BigDecimal.ZERO) != 0) {
            throw new ValidationException("无门槛的订单金额必须为0");
        }
 
        // 领取后 固定时间
        if(StringUtils.isNotBlank(dto.getUsageType()) && dto.getUsageType().equals(CouponUsageTypeEnum.FIXED.getType()) ){
            // 如果使用时间是固定时间的话,那么固定时间的开始时间和结束时间就不能为空
            if(dto.getUsageStartDate()==null){
                throw new ValidationException("固定时间开始日期不能为空");
            }
            if(dto.getUsageEndDate()==null){
                throw new ValidationException("固定时间结束日期不能为空");
            }
        }
 
        // 领取后 有效时间
        if(StringUtils.isNotBlank(dto.getUsageType()) && dto.getUsageType().equals(CouponUsageTypeEnum.GET_AFTER_TIME.getType())){
            // 如果使用时间是领取后有效时间的话,那么领取后的时间类型不能为空,且时间不能为空
            if(StringUtils.isBlank(dto.getUsageTimeType())){
                throw new ValidationException("领取后有效时间类型(COUPON_USAGE_TIME_TYPE)不能为空");
            }
 
            if(dto.getUsageTimeNum()==null){
                throw new ValidationException("领取后有效时间整数不能为空");
            }
            if(dto.getUsageTimeNum()<=0){
                throw new ValidationException("领取后有效时间整数需要大于0");
            }
        }
 
        // 用户获取的类型是首页领取的话
        if(StringUtils.isNotBlank(dto.getGetType()) && dto.getGetType().equals(dto.getGetType().equals(CouponGetTypeEnum.HOME.getType()))
        ){
            // 优惠券图片校验
            if(StringUtils.isBlank(dto.getImageUrl())){
                throw new ValidationException("优惠券图片不能为空");
            }
        }
 
    }
 
}