promotion:完善限时折扣的添加逻辑

pull/2/head
YunaiV 2022-11-06 00:48:07 +08:00
parent 544597d6e2
commit 6d48bd1ed8
16 changed files with 308 additions and 44 deletions

View File

@ -1,14 +1,57 @@
package cn.iocoder.yudao.module.product.controller.admin.sku;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.module.product.controller.admin.sku.vo.ProductSkuOptionRespVO;
import cn.iocoder.yudao.module.product.convert.sku.ProductSkuConvert;
import cn.iocoder.yudao.module.product.dal.dataobject.sku.ProductSkuDO;
import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO;
import cn.iocoder.yudao.module.product.service.sku.ProductSkuService;
import cn.iocoder.yudao.module.product.service.spu.ProductSpuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
@Api(tags = "管理后台 - 商品 sku")
@RestController
@RequestMapping("/product/sku")
@Validated
public class ProductSkuController {
@Resource
private ProductSkuService productSkuService;
@Resource
private ProductSpuService productSpuService;
@GetMapping("/get-option-list")
@ApiOperation("获得商品 SKU 选项的列表")
// @PreAuthorize("@ss.hasPermission('product:sku:query')")
public CommonResult<List<ProductSkuOptionRespVO>> getSkuOptionList() {
// 获得 SKU 列表
List<ProductSkuDO> skus = productSkuService.getSkuList();
if (CollUtil.isEmpty(skus)) {
return success(Collections.emptyList());
}
// 获得对应的 SPU 映射
Map<Long, ProductSpuDO> spuMap = productSpuService.getSpuMap(convertSet(skus, ProductSkuDO::getSpuId));
// 转换为返回结果
List<ProductSkuOptionRespVO> skuVOs = ProductSkuConvert.INSTANCE.convertList05(skus);
skuVOs.forEach(sku -> MapUtils.findAndThen(spuMap, sku.getSpuId(),
spu -> sku.setSpuId(spu.getId()).setSpuName(spu.getName())));
return success(skuVOs);
}
}

View File

@ -0,0 +1,31 @@
package cn.iocoder.yudao.module.product.controller.admin.sku.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@ApiModel(value = "管理后台 - 商品 SKU 选项 Response VO", description = "用于前端 SELECT 选项")
@Data
public class ProductSkuOptionRespVO {
@ApiModelProperty(value = "主键", required = true, example = "1024")
private Long id;
@ApiModelProperty(value = "商品 SKU 名字", example = "红色")
private String name;
@ApiModelProperty(value = "销售价格", required = true, example = "100", notes = "单位:分")
private String price;
@ApiModelProperty(value = "库存", required = true, example = "100")
private Integer stock;
// ========== 商品 SPU 信息 ==========
@ApiModelProperty(value = "商品 SPU 编号", required = true, example = "1")
private Long spuId;
@ApiModelProperty(value = "商品 SPU 名字", required = true, example = "iPhone 11")
private String spuName;
}

View File

@ -9,7 +9,7 @@ import lombok.ToString;
import java.util.Date;
import java.util.List;
@ApiModel("管理后台 - 商品sku Response VO")
@ApiModel("管理后台 - 商品 SKU Response VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)

View File

@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.product.convert.sku;
import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuRespDTO;
import cn.iocoder.yudao.module.product.controller.admin.sku.vo.ProductSkuCreateOrUpdateReqVO;
import cn.iocoder.yudao.module.product.controller.admin.sku.vo.ProductSkuOptionRespVO;
import cn.iocoder.yudao.module.product.controller.admin.sku.vo.ProductSkuRespVO;
import cn.iocoder.yudao.module.product.controller.admin.spu.vo.ProductSpuDetailRespVO;
import cn.iocoder.yudao.module.product.dal.dataobject.sku.ProductSkuDO;
@ -11,7 +12,7 @@ import org.mapstruct.factory.Mappers;
import java.util.List;
/**
* sku Convert
* SKU Convert
*
* @author
*/
@ -36,4 +37,6 @@ public interface ProductSkuConvert {
List<ProductSkuRespDTO> convertList04(List<ProductSkuDO> list);
List<ProductSkuOptionRespVO> convertList05(List<ProductSkuDO> skus);
}

View File

@ -97,6 +97,7 @@ public class ProductSkuDO extends BaseDO {
*
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class Property {
@ -130,19 +131,5 @@ public class ProductSkuDO extends BaseDO {
}
// TODO ========== 待定字段yv =========
// TODO brokerage一级返佣
// TODO brokerage_two二级返佣
// TODO pink_price拼团价
// TODO pink_stock拼团库存
// TODO seckill_price秒杀价
// TODO seckill_stock秒杀库存
// TODO integral需要积分
// TODO ========== 待定字段cf =========
// TODO type 活动显示排序 0=默认 1=秒 2=砍价 3=拼团
// TODO quota 活动限购数量
// TODO quota_show 活动限购数量显示
}

View File

@ -14,22 +14,29 @@ import java.util.List;
public interface ProductSkuService {
/**
* sku
* SKU
*
* @param id
*/
void deleteSku(Long id);
/**
* sku
* SKU
*
* @param id
* @return sku
* @return SKU
*/
ProductSkuDO getSku(Long id);
/**
* sku
* SKU
*
* @return sku
*/
List<ProductSkuDO> getSkuList();
/**
* SKU
*
* @param ids
* @return sku

View File

@ -61,6 +61,11 @@ public class ProductSkuServiceImpl implements ProductSkuService {
return productSkuMapper.selectById(id);
}
@Override
public List<ProductSkuDO> getSkuList() {
return productSkuMapper.selectList();
}
@Override
public List<ProductSkuDO> getSkuList(Collection<Long> ids) {
return productSkuMapper.selectBatchIds(ids);

View File

@ -9,6 +9,9 @@ import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
/**
* SPU Service
@ -63,6 +66,16 @@ public interface ProductSpuService {
*/
List<ProductSpuDO> getSpuList(Collection<Long> ids);
/**
* SPU
*
* @param ids
* @return SPU
*/
default Map<Long, ProductSpuDO> getSpuMap(Collection<Long> ids) {
return convertMap(getSpuList(ids), ProductSpuDO::getId);
}
/**
* SPU
*

View File

@ -130,11 +130,11 @@ public class CouponTemplateBaseVO {
|| fixedEndTerm != null;
}
@AssertTrue(message = "折扣百分比需要大于等于 1")
@AssertTrue(message = "折扣百分比需要大于等于 1,小于等于 99")
@JsonIgnore
public boolean isDiscountPercentValid() {
return ObjectUtil.notEqual(discountType, PromotionDiscountTypeEnum.PERCENT.getType())
|| (discountPercent != null && discountPercent >= 1);
|| (discountPercent != null && discountPercent >= 1 && discountPercent<= 99);
}
@AssertTrue(message = "优惠金额不能为空")

View File

@ -1,10 +1,15 @@
package cn.iocoder.yudao.module.promotion.controller.admin.discount.vo;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.validation.InEnum;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionDiscountTypeEnum;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.Date;
@ -47,10 +52,31 @@ public class DiscountActivityBaseVO {
@NotNull(message = "商品 SKU 编号不能为空")
private Long skuId;
@ApiModelProperty(value = "折扣价格,单位:分", required = true, example = "1000")
@NotNull(message = "折扣价格不能为空")
@Min(value = 1, message = "折扣价格必须大于 0")
@ApiModelProperty(value = "优惠类型", required = true, example = "1", notes = "参见 PromotionDiscountTypeEnum 枚举")
@NotNull(message = "优惠类型不能为空")
@InEnum(PromotionDiscountTypeEnum.class)
private Integer discountType;
@ApiModelProperty(value = "折扣百分比", example = "80", notes = "例如说80% 为 80")
private Integer discountPercent;
@ApiModelProperty(value = "优惠金额", example = "10", notes = "单位:分")
@Min(value = 0, message = "优惠金额需要大于等于 0")
private Integer discountPrice;
@AssertTrue(message = "折扣百分比需要大于等于 1小于等于 99")
@JsonIgnore
public boolean isDiscountPercentValid() {
return ObjectUtil.notEqual(discountType, PromotionDiscountTypeEnum.PERCENT.getType())
|| (discountPercent != null && discountPercent >= 1 && discountPercent<= 99);
}
@AssertTrue(message = "优惠金额不能为空")
@JsonIgnore
public boolean isDiscountPriceValid() {
return ObjectUtil.notEqual(discountType, PromotionDiscountTypeEnum.PRICE.getType())
|| discountPrice != null;
}
}
}

View File

@ -5,7 +5,8 @@ import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import javax.validation.constraints.NotNull;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.List;
@ApiModel("管理后台 - 限时折扣活动创建 Request VO")
@ -17,7 +18,8 @@ public class DiscountActivityCreateReqVO extends DiscountActivityBaseVO {
/**
*
*/
@NotNull(message = "商品列表不能为空")
@NotEmpty(message = "商品列表不能为空")
@Valid
private List<Product> products;
}

View File

@ -6,6 +6,8 @@ import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
@ -22,7 +24,8 @@ public class DiscountActivityUpdateReqVO extends DiscountActivityBaseVO {
/**
*
*/
@NotNull(message = "商品列表不能为空")
@NotEmpty(message = "商品列表不能为空")
@Valid
private List<DiscountActivityCreateReqVO.Product> products;
}

View File

@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.promotion.dal.dataobject.discount;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionDiscountTypeEnum;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
@ -41,8 +42,23 @@ public class DiscountProductDO extends BaseDO {
* ProductSkuDO id
*/
private Long skuId;
/**
*
*
*
* {@link PromotionDiscountTypeEnum}
*/
private Integer discountType;
/**
*
*
* 80% 80
*/
private Integer discountPercent;
/**
*
*
* {@link #discountType} {@link PromotionDiscountTypeEnum#PRICE}
*/
private Integer discountPrice;

View File

@ -0,0 +1,9 @@
import request from '@/utils/request'
// 获得商品 SKU 选项的列表
export function getSkuOptionList() {
return request({
url: '/product/sku/get-option-list',
method: 'get',
})
}

View File

@ -1,6 +1,6 @@
import request from '@/utils/request'
// 创建商品spu
// 创建商品 SPU
export function createSpu(data) {
return request({
url: '/product/spu/create',
@ -9,7 +9,7 @@ export function createSpu(data) {
})
}
// 更新商品spu
// 更新商品 SPU
export function updateSpu(data) {
return request({
url: '/product/spu/update',
@ -18,7 +18,7 @@ export function updateSpu(data) {
})
}
// 删除商品spu
// 删除商品 SPU
export function deleteSpu(id) {
return request({
url: '/product/spu/delete?id=' + id,
@ -34,7 +34,7 @@ export function getSpu(id) {
})
}
// 获得商品spu详情
// 获得商品 SPU 详情
export function getSpuDetail(id) {
return request({
url: '/product/spu/get/detail?id=' + id,
@ -42,7 +42,7 @@ export function getSpuDetail(id) {
})
}
// 获得商品spu分页
// 获得商品 SPU 分页
export function getSpuPage(query) {
return request({
url: '/product/spu/page',

View File

@ -70,7 +70,7 @@
@pagination="getList"/>
<!-- 对话框(添加 / 修改) -->
<el-dialog :title="title" :visible.sync="open" width="600px" v-dialogDrag append-to-body>
<el-dialog :title="title" :visible.sync="open" width="1000px" v-dialogDrag append-to-body>
<el-form ref="form" :model="form" :rules="rules" label-width="80px">
<el-form-item label="活动名称" prop="name">
<el-input v-model="form.name" placeholder="请输入活动名称" />
@ -80,7 +80,54 @@
</el-form-item>
<el-form-item label="活动时间" prop="startAndEndTime">
<el-date-picker clearable v-model="form.startAndEndTime" type="datetimerange" :default-time="['00:00:00', '23:59:59']"
value-format="timestamp" placeholder="选择开始时间" style="width: 480px" />
value-format="timestamp" placeholder="选择开始时间" style="width: 880px" />
</el-form-item>
<el-form-item label="商品选择">
<el-select v-model="form.skuIds" placeholder="请选择活动商品" clearable size="small"
multiple filterable style="width: 400px" @change="changeFormSku">
<el-option v-for="item in productSkus" :key="item.id" :label="item.name" :value="item.id">
<span style="float: left">{{ item.spuName }} &nbsp; {{ item.name}}</span>
<span style="float: right; color: #8492a6; font-size: 13px">{{ (item.price / 100.0).toFixed(2) }}</span>
</el-option>
</el-select>
<el-table v-loading="loading" :data="form.products">
<el-table-column label="商品名称" align="center" width="200">
<template slot-scope="scope">
{{ scope.row.spuName }} &nbsp; {{ scope.row.name}}
</template>
</el-table-column>
<el-table-column label="商品价格" align="center" prop="price">
<template slot-scope="scope">
{{ (scope.row.price / 100.0).toFixed(2) }}
</template>
</el-table-column>
<el-table-column label="库存" align="center" prop="stock" />
<el-table-column label="优惠类型" align="center">
<template slot-scope="scope">
<el-select v-model="scope.row.discountType" placeholder="请选择优惠类型">
<el-option v-for="dict in getDictDatas(DICT_TYPE.PROMOTION_DISCOUNT_TYPE)"
:key="dict.value" :label="dict.label" :value="parseInt(dict.value)"/>
</el-select>
</template>
</el-table-column>
<el-table-column label="优惠" align="center" prop="startTime" width="250">
<template slot-scope="scope">
<el-form-item v-if="scope.row.discountType === PromotionDiscountTypeEnum.PRICE.type" prop="discountPrice">
<el-input-number v-model="scope.row.discountPrice" placeholder="请输入优惠金额"
style="width: 190px" :precision="2" :min="0" />
</el-form-item>
<el-form-item v-if="scope.row.discountType === PromotionDiscountTypeEnum.PERCENT.type" prop="discountPercent">
<el-input-number v-model="scope.row.discountPercent" placeholder="请输入优惠折扣"
style="width: 190px" :precision="1" :min="1" :max="9.9" />
</el-form-item>
</template>
</el-table-column>
<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
<template slot-scope="scope">
<el-button size="mini" type="text" icon="el-icon-delete" @click="removeFormSku(scope.row.skuId)"></el-button>
</template>
</el-table-column>
</el-table>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
@ -93,8 +140,11 @@
<script>
import { createDiscountActivity, updateDiscountActivity, deleteDiscountActivity, getDiscountActivity, getDiscountActivityPage } from "@/api/mall/promotion/discountActivity";
import {PromotionActivityStatusEnum, PromotionProductScopeEnum} from "@/utils/constants";
import {closeRewardActivity} from "@/api/mall/promotion/rewardActivity";
import {
PromotionActivityStatusEnum, PromotionDiscountTypeEnum,
PromotionProductScopeEnum
} from "@/utils/constants";
import { getSkuOptionList } from "@/api/mall/product/sku";
export default {
name: "DiscountActivity",
@ -115,7 +165,7 @@ export default {
//
title: "",
//
open: false,
open: true,
//
queryParams: {
pageNo: 1,
@ -125,17 +175,38 @@ export default {
createTime: [],
},
//
form: {},
form: {
skuIds: [], // SKU
products: [], //
// products: [{
// id: 2,
// name: '',
// price: 500,
// stock: 20,
// spuId: 1,
// spuName: 'iPhone 14 Pro',
// discountType: 1,
// }, {
// id: 10,
// name: '',
// price: 1000,
// stock: 100,
// spuId: 20,
// spuName: 'iPhone 14 Pro',
// discountType: 2,
// }]
},
//
rules: {
name: [{ required: true, message: "活动名称不能为空", trigger: "blur" }],
startAndEndTime: [{ required: true, message: "活动时间不能为空", trigger: "blur" }],
},
//
productSpus: [], // TODO
// SKU
productSkus: [],
// v-if 使
PromotionProductScopeEnum: PromotionProductScopeEnum,
PromotionActivityStatusEnum: PromotionActivityStatusEnum,
PromotionDiscountTypeEnum: PromotionDiscountTypeEnum,
};
},
created() {
@ -151,6 +222,10 @@ export default {
this.total = response.data.total;
this.loading = false;
});
// SKU
getSkuOptionList().then(response => {
this.productSkus = response.data;
});
},
/** 取消按钮 */
cancel() {
@ -166,6 +241,8 @@ export default {
startTime: undefined,
endTime: undefined,
remark: undefined,
skuIds: [],
products: [],
};
this.resetForm("form");
},
@ -240,7 +317,49 @@ export default {
this.getList();
this.$modal.msgSuccess("关闭成功");
}).catch(() => {});
}
},
/** 当 Form 的 SKU 发生变化时 */
changeFormSku(skuIds) {
//
skuIds.forEach(skuId => {
// SKU
const sku = this.productSkus.find(item => item.id === skuId);
if (!sku) {
// debugger
return;
}
//
const product = this.form.products.find(item => item.skuId === skuId);
if (product) {
// debugger
return;
}
this.form.products.push({
skuId: sku.id,
name: sku.name,
price: sku.price,
stock: sku.stock,
spuId: sku.spuId,
spuName: sku.spuName,
discountType: PromotionDiscountTypeEnum.PRICE.type,
});
});
//
this.form.products.map((product, index) => {
if (!skuIds.includes(product.skuId)) {
this.form.products.splice(index, 1);
}
});
},
/** 移除 Form 的 SKU */
removeFormSku(skuId) {
this.form.skuIds.map((id, index) => {
if (skuId === id) {
this.form.skuIds.splice(index, 1);
}
});
this.changeFormSku(this.form.skuIds);
},
}
};
}
</script>