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
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.CreateCouponRecordDTO;
import com.mzl.flower.dto.request.coupon.QueryCouponRecordDTO;
import com.mzl.flower.dto.response.coupon.CouponRecordResultVO;
import com.mzl.flower.dto.response.coupon.CouponRecordVO;
import com.mzl.flower.entity.coupon.CouponRecordDO;
import com.mzl.flower.entity.coupon.CouponTemplateDO;
import com.mzl.flower.enums.CouponStatusEnum;
import com.mzl.flower.service.coupon.CouponRecordService;
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.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
 
/**
* @author @TaoJie
* @since 2024-08-27
*/
@RestController
@RequestMapping("/v2/coupon-record")
@Api(value = "优惠券记录管理-活动", tags = "优惠券记录管理")
@Validated
public class CouponRecordController extends BaseController {
    
    @Autowired
    CouponRecordService couponRecordService;
 
    @Autowired
    private CouponTemplateService2 couponTemplateService;
 
 
    @PostMapping("")
    @ApiOperation(value = "新增", notes = "新增")
    public ResponseEntity<ReturnDataDTO> create(@Validated @RequestBody CreateCouponRecordDTO dto) {
 
        final CouponTemplateDO couponTemplateDO = couponTemplateService.getById(dto.getCouponId());
        if(null==couponTemplateDO){
            throw new ValidationException("优惠券不存在");
        }
 
        if(StringUtils.isNotBlank(couponTemplateDO.getStatus()) && couponTemplateDO.getStatus().equals(CouponStatusEnum.INACTIVE.getStatus())){
            throw new ValidationException("优惠券还未发布");
        }
        if(StringUtils.isNotBlank(couponTemplateDO.getStatus()) && couponTemplateDO.getStatus().equals(CouponStatusEnum.EXPIRED.getStatus())){
            throw new ValidationException("优惠券已下架");
        }
 
        couponRecordService.createCouponRecord(dto);
        return returnData(R.SUCCESS.getCode(), null);
    }
 
    @PutMapping("/{id}")
    @ApiOperation(value = "修改", notes = "修改")
    public ResponseEntity<ReturnDataDTO>  update(@PathVariable String id,@Validated @RequestBody CreateCouponRecordDTO dto) {
 
        dto.setId(id);
 
        CouponRecordDO couponRecordDO= couponRecordService.getById(dto.getId());
        if(null==couponRecordDO){
            throw new ValidationException("优惠券记录不存在");
        }
 
        return returnData(R.SUCCESS.getCode(), couponRecordService.updateCouponRecord(dto));
    }
 
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除", notes = "删除")
    public ResponseEntity<ReturnDataDTO> delete(@PathVariable String id ) {
 
        CouponRecordDO couponRecordDO= couponRecordService.getById(id);
        if(null==couponRecordDO){
            throw new ValidationException("优惠券记录不存在");
        }
        couponRecordService.deleteCouponRecord(id);
        return returnData(R.SUCCESS.getCode(), null);
    }
 
    @GetMapping("/{id}")
    @ApiOperation(value = "详情", notes = "详情")
    public ResponseEntity<ReturnDataDTO>  get(@PathVariable String id) {
        CouponRecordVO couponRecordVO=couponRecordService.getCouponRecordById(id);
        return returnData(R.SUCCESS.getCode(),ConverterUtil.transObject(couponRecordVO, CouponRecordResultVO.class));
    }
 
    @GetMapping("/page")
    @ApiOperation(value = "查询-分页", notes = "查询-分页")
    public ResponseEntity<ReturnDataDTO<Page<CouponRecordVO>>> page(
            Page page, QueryCouponRecordDTO dto
    ) {
        return returnData(R.SUCCESS.getCode(), ConverterUtil.transPage(couponRecordService.getPage(page,dto), CouponRecordResultVO.class));
    }
 
    @GetMapping("/list")
    @ApiOperation(value = "查询-全部", notes = "查询-全部")
    public ResponseEntity<ReturnDataDTO<Page<CouponRecordVO>>> list(QueryCouponRecordDTO dto
    ) {
        return returnData(R.SUCCESS.getCode(),ConverterUtil.transList(couponRecordService.getList(dto), CouponRecordResultVO.class));
 
    }
 
}