后端 + 前端:优惠劵模板添加

This commit is contained in:
YunaiV
2019-04-05 12:24:18 +08:00
parent f6c847d104
commit 8418641731
9 changed files with 115 additions and 43 deletions

View File

@@ -1,5 +1,6 @@
package cn.iocoder.mall.promotion.application.controller.admins;
import cn.iocoder.common.framework.util.DateUtil;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.promotion.api.CouponService;
import cn.iocoder.mall.promotion.api.bo.CouponTemplateBO;
@@ -11,6 +12,7 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@@ -19,7 +21,7 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@RestController
@RequestMapping("admins/product_recommend")
@RequestMapping("admins/coupon")
@Api("优惠劵(码)模块")
public class AdminsCouponTemplateController {
@@ -33,8 +35,8 @@ public class AdminsCouponTemplateController {
@ApiImplicitParams({
@ApiImplicitParam(name = "title", value = "标题", required = true, example = "优惠劵牛逼"),
@ApiImplicitParam(name = "description", value = "使用说明", example = "我只是描述"),
@ApiImplicitParam(name = "quota", value = "每人限领个数", example = "null - 则表示不限制"),
@ApiImplicitParam(name = "stock", value = "剩余可用库存", example = "null - 则表示无限库存"),
@ApiImplicitParam(name = "quota", value = "每人限领个数", required = true),
@ApiImplicitParam(name = "total", value = "发行总量"),
@ApiImplicitParam(name = "priceAvailable", value = "是否设置满多少金额可用,单位:分", example = "0-不限制大于0-多少金额可用"),
@ApiImplicitParam(name = "rangeType", value = "可用范围的类型", required = true, example = "参见 CouponTemplateRangeTypeEnum 枚举"),
@ApiImplicitParam(name = "rangeValues", value = "指定商品 / 分类列表,使用逗号分隔商品编号"),
@@ -50,13 +52,15 @@ public class AdminsCouponTemplateController {
})
public CommonResult<AdminsCouponTemplateVO> add(@RequestParam(value = "title") String title,
@RequestParam(value = "description", required = false) String description,
@RequestParam(value = "quota", required = false) Integer quota,
@RequestParam(value = "stock", required = false) Integer stock,
@RequestParam(value = "quota") Integer quota,
@RequestParam(value = "total", required = false) Integer total,
@RequestParam(value = "priceAvailable") Integer priceAvailable,
@RequestParam(value = "rangeType") Integer rangeType,
@RequestParam(value = "rangeType", required = false) String rangeValues,
@RequestParam(value = "dateType") Integer dateType,
@DateTimeFormat(pattern = "yyyy-MM-dd")
@RequestParam(value = "validStartTime", required = false) Date validStartTime,
@DateTimeFormat(pattern = "yyyy-MM-dd")
@RequestParam(value = "validEndTime", required = false) Date validEndTime,
@RequestParam(value = "fixedBeginTerm", required = false) Integer fixedBeginTerm,
@RequestParam(value = "fixedEndTerm", required = false) Integer fixedEndTerm,
@@ -65,9 +69,11 @@ public class AdminsCouponTemplateController {
@RequestParam(value = "percentOff", required = false) Integer percentOff,
@RequestParam(value = "discountPriceLimit", required = false) Integer discountPriceLimit) {
// 创建 CouponCardTemplateAddDTO 对象
validStartTime = DateUtil.getDayBegin(validStartTime); // 开始时间,以当前 00:00:00 为准
validEndTime = DateUtil.getDayBegin(validEndTime); // 结束时间,以当前 25:59:59 为准
CouponCardTemplateAddDTO couponCardTemplateAddDTO = new CouponCardTemplateAddDTO()
.setTitle(title).setDescription(description)
.setQuota(quota).setStock(stock)
.setQuota(quota).setTotal(total)
.setPriceAvailable(priceAvailable).setRangeType(rangeType).setRangeValues(rangeValues)
.setDateType(dateType).setValidStartTime(validStartTime).setValidEndTime(validEndTime)
.setFixedBeginTerm(fixedBeginTerm).setFixedEndTerm(fixedEndTerm)

View File

@@ -136,15 +136,6 @@ public class AdminsCouponTemplateVO {
return this;
}
public Integer getStock() {
return stock;
}
public AdminsCouponTemplateVO setStock(Integer stock) {
this.stock = stock;
return this;
}
public Integer getPriceAvailable() {
return priceAvailable;
}

View File

@@ -19,7 +19,7 @@ public class CouponCardTemplateAddDTO {
* 标题
*/
@NotEmpty(message = "标题不能为空")
@Length(min = 6, max = 16, message = "标题长度为 {min}-{max} 位")
@Length(min = 2, max = 16, message = "标题长度为 {min}-{max} 位")
private String title;
/**
* 使用说明
@@ -31,9 +31,8 @@ public class CouponCardTemplateAddDTO {
// ========== 领取规则 BEGIN ==========
/**
* 每人限领个数
*
* null - 则表示不限制
*/
@NotNull(message = "每人限领个数不能为空")
@Min(value = 1, message = "每人限领个数最小为 {value}")
private Integer quota;
/**

View File

@@ -82,11 +82,9 @@ public class CouponTemplateDO extends BaseDO {
*/
private Integer quota;
/**
* 剩余可用库存
*
* null - 则表示无限库存
* 发行总量
*/
private Integer stock;
private Integer total;
// ========== 领取规则 END ==========
// ========== 使用规则 BEGIN ==========
@@ -274,12 +272,12 @@ public class CouponTemplateDO extends BaseDO {
return this;
}
public Integer getStock() {
return stock;
public Integer getTotal() {
return total;
}
public CouponTemplateDO setStock(Integer stock) {
this.stock = stock;
public CouponTemplateDO setTotal(Integer total) {
this.total = total;
return this;
}

View File

@@ -49,8 +49,8 @@ public class CouponServiceImpl implements CouponService {
// 校验优惠类型
CommonResult<Boolean> checkCouponTemplateDateTypeResult = this.checkCouponTemplatePreferentialType(
couponCardTemplateAddDTO.getPreferentialType(), couponCardTemplateAddDTO.getPercentOff(),
couponCardTemplateAddDTO.getPriceOff());
if (checkCouponCodeTemplateDateTypeResult.isError()) {
couponCardTemplateAddDTO.getPriceOff(), couponCardTemplateAddDTO.getPriceAvailable());
if (checkCouponTemplateDateTypeResult.isError()) {
return CommonResult.error(checkCouponTemplateDateTypeResult);
}
// 保存优惠劵模板到数据库
@@ -58,6 +58,7 @@ public class CouponServiceImpl implements CouponService {
.setType(CouponTemplateTypeEnum.CARD.getValue())
.setStatus(CouponTemplateStatusEnum.ENABLE.getValue())
.setStatFetchNum(0);
template.setCreateTime(new Date());
couponTemplateMapper.insert(template);
// 返回成功
return CommonResult.success(CouponTemplateConvert.INSTANCE.convert(template));
@@ -106,6 +107,9 @@ public class CouponServiceImpl implements CouponService {
if (validEndTime == null) {
return CommonResult.error(SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getCode(), "生效结束时间不能为空");
}
if (validStartTime.after(validEndTime)) {
return CommonResult.error(SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getCode(), "生效开始时间不能大于生效结束时间");
}
} else if (CouponTemplateDateTypeEnum.FIXED_TERM.getValue().equals(dateType)) { // 领取日期
if (fixedBeginTerm == null) {
return CommonResult.error(SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getCode(), "领取日期开始时间不能为空");
@@ -119,11 +123,15 @@ public class CouponServiceImpl implements CouponService {
return CommonResult.success(true);
}
private CommonResult<Boolean> checkCouponTemplatePreferentialType(Integer preferentialType, Integer percentOff, Integer priceOff) {
private CommonResult<Boolean> checkCouponTemplatePreferentialType(Integer preferentialType, Integer percentOff,
Integer priceOff, Integer priceAvailable) {
if (CouponTemplatePreferentialTypeEnum.PRICE.getValue().equals(preferentialType)) {
if (priceOff == null) {
return CommonResult.error(SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getCode(), "优惠金额不能为空");
}
if (priceOff >= priceAvailable) {
return CommonResult.error(SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getCode(), "优惠金额不能d大于等于使用金额门槛");
}
} else if (CouponTemplatePreferentialTypeEnum.DISCOUNT.getValue().equals(preferentialType)) {
if (percentOff == null) {
return CommonResult.error(SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getCode(), "折扣百分比不能为空");

View File

@@ -4,7 +4,7 @@
<sql id="FIELDS">
id, title, description, type, code_type,
status, quota, stock, price_available, range_type,
status, quota, total, price_available, range_type,
range_values, date_type, valid_start_time, valid_end_time, fixed_term,
preferential_type, percent_off, price_off, discount_price_limit, stat_fetch_num,
create_time
@@ -100,8 +100,8 @@
<if test="quota != null">
quota = #{quota},
</if>
<if test="stock != null">
stock = #{stock},
<if test="total != null">
total = #{total},
</if>
<if test="priceAvailable != null">
price_available = #{priceAvailable},