- 后端:增加促销活动分页接口

This commit is contained in:
YunaiV 2019-05-07 20:23:20 +08:00
parent d39a416080
commit f44a6d49d7
9 changed files with 192 additions and 48 deletions

View File

@ -158,13 +158,9 @@ public class CartServiceImpl implements CartService {
} }
// TODO 库存相关 // TODO 库存相关
// 查询促销活动 // 查询促销活动
CommonResult<List<PromotionActivityBO>> activityListResult = promotionActivityService.getPromotionActivityListBySpuIds( List<PromotionActivityBO> activityList = promotionActivityService.getPromotionActivityListBySpuIds(
skus.stream().map(sku -> sku.getSpu().getId()).collect(Collectors.toSet()), skus.stream().map(sku -> sku.getSpu().getId()).collect(Collectors.toSet()),
Collections.singletonList(PromotionActivityStatusEnum.RUN.getValue())); Collections.singletonList(PromotionActivityStatusEnum.RUN.getValue()));
if (activityListResult.isError()) {
return CommonResult.error(activityListResult);
}
List<PromotionActivityBO> activityList = activityListResult.getData();
// 拼装结果主要是计算价格 // 拼装结果主要是计算价格
CalcOrderPriceBO calcOrderPriceBO = new CalcOrderPriceBO(); CalcOrderPriceBO calcOrderPriceBO = new CalcOrderPriceBO();
// 1. 创建初始的每一项的数组 // 1. 创建初始的每一项的数组
@ -208,14 +204,9 @@ public class CartServiceImpl implements CartService {
return ServiceExceptionUtil.error(OrderErrorCodeEnum.CARD_ITEM_SKU_NOT_FOUND.getCode()); return ServiceExceptionUtil.error(OrderErrorCodeEnum.CARD_ITEM_SKU_NOT_FOUND.getCode());
} }
// 查询促销活动 // 查询促销活动
CommonResult<List<PromotionActivityBO>> activityListResult = promotionActivityService.getPromotionActivityListBySpuId(sku.getSpuId(), List<PromotionActivityBO> activityList = promotionActivityService.getPromotionActivityListBySpuId(sku.getSpuId(),
Arrays.asList(PromotionActivityStatusEnum.WAIT.getValue(), PromotionActivityStatusEnum.RUN.getValue())); Arrays.asList(PromotionActivityStatusEnum.WAIT.getValue(), PromotionActivityStatusEnum.RUN.getValue()));
if (activityListResult.isError()) { if (activityList.isEmpty()) { // 如果无促销活动则直接返回默认结果即可
return CommonResult.error(activityListResult);
}
// 如果无促销活动则直接返回默认结果即可
List<PromotionActivityBO> activityList = activityListResult.getData();
if (activityList.isEmpty()) {
return CommonResult.success(new CalcSkuPriceBO().setOriginalPrice(sku.getPrice()).setBuyPrice(sku.getPrice())); return CommonResult.success(new CalcSkuPriceBO().setOriginalPrice(sku.getPrice()).setBuyPrice(sku.getPrice()));
} }
// 如果有促销活动则开始做计算 TODO 芋艿因为现在暂时只有限时折扣 + 满减送所以写的比较简单先 // 如果有促销活动则开始做计算 TODO 芋艿因为现在暂时只有限时折扣 + 满减送所以写的比较简单先

View File

@ -44,7 +44,7 @@ public class AdminsProductRecommendController {
@ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, example = "10"), @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, example = "10"),
}) })
public CommonResult<AdminsProductRecommendPageVO> page(@RequestParam(value = "type", required = false) Integer type, public CommonResult<AdminsProductRecommendPageVO> page(@RequestParam(value = "type", required = false) Integer type,
@RequestParam(value = "pageNo", defaultValue = "0") Integer pageNo, @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) { @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
ProductRecommendPageBO result = productRecommendService.getProductRecommendPage(new ProductRecommendPageDTO().setType(type).setPageNo(pageNo).setPageSize(pageSize)); ProductRecommendPageBO result = productRecommendService.getProductRecommendPage(new ProductRecommendPageDTO().setType(type).setPageNo(pageNo).setPageSize(pageSize));
// 获得商品集合 // 获得商品集合

View File

@ -0,0 +1,58 @@
package cn.iocoder.mall.promotion.application.controller.admins;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.promotion.api.PromotionActivityService;
import cn.iocoder.mall.promotion.api.bo.PromotionActivityPageBO;
import cn.iocoder.mall.promotion.api.constant.PromotionActivityStatusEnum;
import cn.iocoder.mall.promotion.api.dto.PromotionActivityPageDTO;
import io.swagger.annotations.Api;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Collections;
import static cn.iocoder.common.framework.vo.CommonResult.success;
@RestController
@RequestMapping("admins/promotion_activity")
@Api("促销活动模块")
public class AdminsPromotionActivityController {
@Reference(validation = "true", version = "${dubbo.provider.PromotionActivityService.version}")
private PromotionActivityService promotionActivityService;
@GetMapping("/page") // TODO 芋艿BO => VO
public CommonResult<PromotionActivityPageBO> page(@RequestParam("title") String title,
@RequestParam("activityType") Integer activityType,
@RequestParam(value = "status") String status,
@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
PromotionActivityPageDTO promotionActivityPageDTO = new PromotionActivityPageDTO()
.setTitle(title).setActivityType(activityType).setPageNo(pageNo).setPageSize(pageSize);
switch (status) {
case "WAIT":
promotionActivityPageDTO.setStatuses(Collections.singleton(PromotionActivityStatusEnum.WAIT.getValue()));
break;
case "RUN":
promotionActivityPageDTO.setStatuses(Collections.singleton(PromotionActivityStatusEnum.RUN.getValue()));
break;
case "END":
promotionActivityPageDTO.setStatuses(Collections.singleton(PromotionActivityStatusEnum.END.getValue()));
break;
case "INVALID":
promotionActivityPageDTO.setStatuses(Collections.singleton(PromotionActivityStatusEnum.INVALID.getValue()));
break;
default:
promotionActivityPageDTO.setStatuses(Arrays.asList(PromotionActivityStatusEnum.WAIT.getValue(),
PromotionActivityStatusEnum.RUN.getValue(), PromotionActivityStatusEnum.END.getValue(),
PromotionActivityStatusEnum.INVALID.getValue()));
}
// 执行查询
return success(promotionActivityService.getPromotionActivityPage(promotionActivityPageDTO));
}
}

View File

@ -1,17 +1,20 @@
package cn.iocoder.mall.promotion.api; package cn.iocoder.mall.promotion.api;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.promotion.api.bo.PromotionActivityBO; import cn.iocoder.mall.promotion.api.bo.PromotionActivityBO;
import cn.iocoder.mall.promotion.api.bo.PromotionActivityPageBO;
import cn.iocoder.mall.promotion.api.dto.PromotionActivityPageDTO;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
public interface PromotionActivityService { public interface PromotionActivityService {
CommonResult<List<PromotionActivityBO>> getPromotionActivityListBySpuId(Integer spuId, List<PromotionActivityBO> getPromotionActivityListBySpuId(Integer spuId,
Collection<Integer> activityStatuses); Collection<Integer> activityStatuses);
CommonResult<List<PromotionActivityBO>> getPromotionActivityListBySpuIds(Collection<Integer> spuIds, List<PromotionActivityBO> getPromotionActivityListBySpuIds(Collection<Integer> spuIds,
Collection<Integer> activityStatuses); Collection<Integer> activityStatuses);
PromotionActivityPageBO getPromotionActivityPage(PromotionActivityPageDTO promotionActivityPageDTO);
} }

View File

@ -0,0 +1,24 @@
package cn.iocoder.mall.promotion.api.bo;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.List;
/**
* 促销活动分页 BO
*/
@Data
@Accessors(chain = true)
public class PromotionActivityPageBO {
/**
* PromotionActivityBO 数组
*/
private List<PromotionActivityBO> list;
/**
* 总量
*/
private Integer total;
}

View File

@ -0,0 +1,37 @@
package cn.iocoder.mall.promotion.api.dto;
import lombok.Data;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Collection;
/**
* 商品推荐分页 DTO
*/
@Data
@Accessors(chain = true)
public class PromotionActivityPageDTO implements Serializable {
/**
* 标题
*
* 模糊匹配
*/
private String title;
/**
* 活动类型
*/
private Integer activityType;
/**
* 状态
*/
private Collection<Integer> statuses;
@NotNull(message = "页码不能为空")
private Integer pageNo;
@NotNull(message = "每页条数不能为空")
private Integer pageSize;
}

View File

@ -16,4 +16,14 @@ public interface PromotionActivityMapper {
void insert(PromotionActivityDO activity); void insert(PromotionActivityDO activity);
List<PromotionActivityDO> selectListByPage(@Param("title") String title,
@Param("activityType") Integer activityType,
@Param("statuses") Collection<Integer> statuses,
@Param("offset") Integer offset,
@Param("limit") Integer limit);
Integer selectCountByPage(@Param("title") String title,
@Param("activityType") Integer activityType,
@Param("statuses") Collection<Integer> statuses);
} }

View File

@ -1,10 +1,11 @@
package cn.iocoder.mall.promotion.biz.service; package cn.iocoder.mall.promotion.biz.service;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.promotion.api.PromotionActivityService; import cn.iocoder.mall.promotion.api.PromotionActivityService;
import cn.iocoder.mall.promotion.api.bo.PromotionActivityBO; import cn.iocoder.mall.promotion.api.bo.PromotionActivityBO;
import cn.iocoder.mall.promotion.api.bo.PromotionActivityPageBO;
import cn.iocoder.mall.promotion.api.constant.PromotionActivityTypeEnum; import cn.iocoder.mall.promotion.api.constant.PromotionActivityTypeEnum;
import cn.iocoder.mall.promotion.api.constant.RangeTypeEnum; import cn.iocoder.mall.promotion.api.constant.RangeTypeEnum;
import cn.iocoder.mall.promotion.api.dto.PromotionActivityPageDTO;
import cn.iocoder.mall.promotion.biz.convert.PromotionActivityConvert; import cn.iocoder.mall.promotion.biz.convert.PromotionActivityConvert;
import cn.iocoder.mall.promotion.biz.dao.PromotionActivityMapper; import cn.iocoder.mall.promotion.biz.dao.PromotionActivityMapper;
import cn.iocoder.mall.promotion.biz.dataobject.PromotionActivityDO; import cn.iocoder.mall.promotion.biz.dataobject.PromotionActivityDO;
@ -25,19 +26,19 @@ public class PromotionActivityServiceImpl implements PromotionActivityService {
private PromotionActivityMapper promotionActivityMapper; private PromotionActivityMapper promotionActivityMapper;
@Override @Override
public CommonResult<List<PromotionActivityBO>> getPromotionActivityListBySpuId(Integer spuId, Collection<Integer> activityStatuses) { public List<PromotionActivityBO> getPromotionActivityListBySpuId(Integer spuId, Collection<Integer> activityStatuses) {
return this.getPromotionActivityListBySpuIds(Collections.singleton(spuId), activityStatuses); return this.getPromotionActivityListBySpuIds(Collections.singleton(spuId), activityStatuses);
} }
@Override @Override
public CommonResult<List<PromotionActivityBO>> getPromotionActivityListBySpuIds(Collection<Integer> spuIds, Collection<Integer> activityStatuses) { public List<PromotionActivityBO> getPromotionActivityListBySpuIds(Collection<Integer> spuIds, Collection<Integer> activityStatuses) {
if (spuIds.isEmpty() || activityStatuses.isEmpty()) { if (spuIds.isEmpty() || activityStatuses.isEmpty()) {
return CommonResult.success(Collections.emptyList()); return Collections.emptyList();
} }
// 查询指定状态的促销活动 // 查询指定状态的促销活动
List<PromotionActivityDO> activityList = promotionActivityMapper.selectListByStatus(activityStatuses); List<PromotionActivityDO> activityList = promotionActivityMapper.selectListByStatus(activityStatuses);
if (activityList.isEmpty()) { if (activityList.isEmpty()) {
return CommonResult.success(Collections.emptyList()); return Collections.emptyList();
} }
// 匹配商品 // 匹配商品
for (Iterator<PromotionActivityDO> iterator = activityList.iterator(); iterator.hasNext();) { for (Iterator<PromotionActivityDO> iterator = activityList.iterator(); iterator.hasNext();) {
@ -64,7 +65,23 @@ public class PromotionActivityServiceImpl implements PromotionActivityService {
} }
} }
// 返回最终结果 // 返回最终结果
return CommonResult.success(PromotionActivityConvert.INSTANCE.convertToBO(activityList)); return PromotionActivityConvert.INSTANCE.convertToBO(activityList);
}
@Override
public PromotionActivityPageBO getPromotionActivityPage(PromotionActivityPageDTO promotionActivityPageDTO) {
PromotionActivityPageBO promotionActivityPageBO = new PromotionActivityPageBO();
// 查询分页数据
int offset = (promotionActivityPageDTO.getPageNo() - 1) * promotionActivityPageDTO.getPageSize();
promotionActivityPageBO.setList(PromotionActivityConvert.INSTANCE.convertToBO(promotionActivityMapper.selectListByPage(
promotionActivityPageDTO.getTitle(), promotionActivityPageDTO.getActivityType(),
promotionActivityPageDTO.getStatuses(),
offset, promotionActivityPageDTO.getPageSize())));
// 查询分页总数
promotionActivityPageBO.setTotal(promotionActivityMapper.selectCountByPage(
promotionActivityPageDTO.getTitle(), promotionActivityPageDTO.getActivityType(),
promotionActivityPageDTO.getStatuses()));
return promotionActivityPageBO;
} }
private boolean isSpuMatchTimeLimitDiscount(Integer spuId, PromotionActivityDO activity) { private boolean isSpuMatchTimeLimitDiscount(Integer spuId, PromotionActivityDO activity) {

View File

@ -59,30 +59,34 @@
<!-- </where>--> <!-- </where>-->
<!-- </select>--> <!-- </select>-->
<!-- <select id="selectListByTitleLike" resultType="PromotionActivityDO">--> <select id="selectListByPage" resultMap="PromotionActivityResultMap">
<!-- SELECT--> SELECT
<!-- <include refid="FIELDS" />--> <include refid="FIELDS" />
<!-- FROM banner--> FROM promotion_activity
<!-- <where>--> WHERE activity_type = #{activityType}
<!-- <if test="title != null">--> <if test="title != null">
<!-- title LIKE "%"#{title}"%"--> AND title LIKE "%"#{title}"%"
<!-- </if>--> </if>
<!-- AND deleted = 0--> AND status IN
<!-- </where>--> <foreach item="status" collection="statuses" separator="," open="(" close=")" index="">
<!-- LIMIT #{offset}, #{limit}--> #{status}
<!-- </select>--> </foreach>
LIMIT #{offset}, #{limit}
</select>
<!-- <select id="selectCountByTitleLike" resultType="Integer">--> <select id="selectCountByPage" resultType="Integer">
<!-- SELECT--> SELECT
<!-- COUNT(1)--> COUNT(1)
<!-- FROM banner--> FROM promotion_activity
<!-- <where>--> WHERE activity_type = #{activityType}
<!-- <if test="title != null">--> <if test="title != null">
<!-- title LIKE "%"#{title}"%"--> AND title LIKE "%"#{title}"%"
<!-- </if>--> </if>
<!-- AND deleted = 0--> AND status IN
<!-- </where>--> <foreach item="status" collection="statuses" separator="," open="(" close=")" index="">
<!-- </select>--> #{status}
</foreach>
</select>
<insert id="insert" parameterType="PromotionActivityDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id"> <insert id="insert" parameterType="PromotionActivityDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
INSERT INTO promotion_activity ( INSERT INTO promotion_activity (