后端:增加商品推荐增删改查接口

This commit is contained in:
YunaiV
2019-03-31 13:14:43 +08:00
parent 1f827e2d2c
commit b333340c20
21 changed files with 1057 additions and 10 deletions

View File

@@ -0,0 +1,30 @@
package cn.iocoder.mall.promotion.biz.convert;
import cn.iocoder.mall.promotion.api.bo.ProductRecommendBO;
import cn.iocoder.mall.promotion.api.dto.ProductRecommendAddDTO;
import cn.iocoder.mall.promotion.api.dto.ProductRecommendUpdateDTO;
import cn.iocoder.mall.promotion.biz.dataobject.ProductRecommendDO;
import org.mapstruct.Mapper;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import java.util.List;
@Mapper
public interface ProductRecommendConvert {
ProductRecommendConvert INSTANCE = Mappers.getMapper(ProductRecommendConvert.class);
@Mappings({})
ProductRecommendBO convertToBO(ProductRecommendDO banner);
@Mappings({})
List<ProductRecommendBO> convertToBO(List<ProductRecommendDO> bannerList);
@Mappings({})
ProductRecommendDO convert(ProductRecommendAddDTO bannerAddDTO);
@Mappings({})
ProductRecommendDO convert(ProductRecommendUpdateDTO bannerUpdateDTO);
}

View File

@@ -0,0 +1,30 @@
package cn.iocoder.mall.promotion.biz.dao;
import cn.iocoder.mall.promotion.biz.dataobject.ProductRecommendDO;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface ProductRecommendMapper {
ProductRecommendDO selectById(@Param("id") Integer id);
ProductRecommendDO selectByProductSpuIdAndType(@Param("productSpuId") Integer productSpuId,
@Param("type") Integer type);
List<ProductRecommendDO> selectListByTypeAndStatus(@Param("type") Integer type,
@Param("status") Integer status);
List<ProductRecommendDO> selectPageByType(@Param("type") Integer type,
@Param("offset") Integer offset,
@Param("limit") Integer limit);
Integer selectCountByType(@Param("type") Integer type);
void insert(ProductRecommendDO bannerDO);
int update(ProductRecommendDO bannerDO);
}

View File

@@ -0,0 +1,94 @@
package cn.iocoder.mall.promotion.biz.dataobject;
import cn.iocoder.common.framework.dataobject.DeletableDO;
/**
* 商品推荐 DO
*/
public class ProductRecommendDO extends DeletableDO {
/**
* 编号
*/
private Integer id;
/**
* 类型
*
* {@link cn.iocoder.mall.promotion.api.constant.ProductRecommendType}
*/
private Integer type;
/**
* 商品 Spu 编号
*/
private Integer productSpuId;
// TODO 芋艿,商品 spu 名
/**
* 排序
*/
private Integer sort;
/**
* 状态
*
* {@link cn.iocoder.common.framework.constant.CommonStatusEnum}
*/
private Integer status;
/**
* 备注
*/
private String memo;
public Integer getId() {
return id;
}
public ProductRecommendDO setId(Integer id) {
this.id = id;
return this;
}
public Integer getType() {
return type;
}
public ProductRecommendDO setType(Integer type) {
this.type = type;
return this;
}
public Integer getProductSpuId() {
return productSpuId;
}
public ProductRecommendDO setProductSpuId(Integer productSpuId) {
this.productSpuId = productSpuId;
return this;
}
public Integer getSort() {
return sort;
}
public ProductRecommendDO setSort(Integer sort) {
this.sort = sort;
return this;
}
public Integer getStatus() {
return status;
}
public ProductRecommendDO setStatus(Integer status) {
this.status = status;
return this;
}
public String getMemo() {
return memo;
}
public ProductRecommendDO setMemo(String memo) {
this.memo = memo;
return this;
}
}

View File

@@ -75,6 +75,10 @@ public class BannerServiceImpl implements BannerService {
if (!CommonStatusEnum.isValid(status)) {
return CommonResult.error(SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getCode(), "变更状态必须是开启1或关闭2"); // TODO 有点搓
}
// 校验 Banner 存在
if (bannerMapper.selectById(bannerId) == null) {
return ServiceExceptionUtil.error(PromotionErrorCodeEnum.BANNER_NOT_EXISTS.getCode());
}
// 更新到数据库
BannerDO updateBanner = new BannerDO().setId(bannerId).setStatus(status);
bannerMapper.update(updateBanner);

View File

@@ -0,0 +1,133 @@
package cn.iocoder.mall.promotion.biz.service;
import cn.iocoder.common.framework.constant.CommonStatusEnum;
import cn.iocoder.common.framework.constant.DeletedStatusEnum;
import cn.iocoder.common.framework.constant.SysErrorCodeEnum;
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.product.api.ProductSpuService;
import cn.iocoder.mall.promotion.api.ProductRecommendService;
import cn.iocoder.mall.promotion.api.bo.ProductRecommendBO;
import cn.iocoder.mall.promotion.api.bo.ProductRecommendPageBO;
import cn.iocoder.mall.promotion.api.constant.PromotionErrorCodeEnum;
import cn.iocoder.mall.promotion.api.dto.ProductRecommendAddDTO;
import cn.iocoder.mall.promotion.api.dto.ProductRecommendPageDTO;
import cn.iocoder.mall.promotion.api.dto.ProductRecommendUpdateDTO;
import cn.iocoder.mall.promotion.biz.convert.ProductRecommendConvert;
import cn.iocoder.mall.promotion.biz.dao.ProductRecommendMapper;
import cn.iocoder.mall.promotion.biz.dataobject.ProductRecommendDO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
@Service // 实际上不用添加。添加的原因是,必须 Spring 报错提示
@com.alibaba.dubbo.config.annotation.Service(validation = "true")
public class ProductRecommendServiceImpl implements ProductRecommendService {
@Reference(validation = "true")
private ProductSpuService productSpuService;
@Autowired
private ProductRecommendMapper productRecommendMapper;
@Override
public CommonResult<List<ProductRecommendBO>> getProductRecommendList(Integer type, Integer status) {
List<ProductRecommendDO> productRecommends = productRecommendMapper.selectListByTypeAndStatus(type, status);
return CommonResult.success(ProductRecommendConvert.INSTANCE.convertToBO(productRecommends));
}
@Override
public CommonResult<ProductRecommendPageBO> getProductRecommendPage(ProductRecommendPageDTO productRecommendPageDTO) {
ProductRecommendPageBO productRecommendPageBO = new ProductRecommendPageBO();
// 查询分页数据
int offset = (productRecommendPageDTO.getPageNo() - 1) * productRecommendPageDTO.getPageSize();
productRecommendPageBO.setList(ProductRecommendConvert.INSTANCE.convertToBO(productRecommendMapper.selectPageByType(productRecommendPageDTO.getType(),
offset, productRecommendPageDTO.getPageSize())));
// 查询分页总数
productRecommendPageBO.setTotal(productRecommendMapper.selectCountByType(productRecommendPageDTO.getType()));
return CommonResult.success(productRecommendPageBO);
}
@Override
public CommonResult<ProductRecommendBO> addProductRecommend(Integer adminId, ProductRecommendAddDTO productRecommendAddDTO) {
// 校验参数
if (!CommonStatusEnum.isValid(productRecommendAddDTO.getType())) {
return CommonResult.error(SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getCode(), "推荐类型必须是新品1或热卖2"); // TODO 有点搓
}
// 校验商品不存在
if (productSpuService.getProductSpu(productRecommendAddDTO.getProductSpuId()) == null) {
return ServiceExceptionUtil.error(PromotionErrorCodeEnum.PRODUCT_RECOMMEND_PRODUCT_NOT_EXISTS.getCode());
}
// 校验商品是否已经推荐
if (productRecommendMapper.selectByProductSpuIdAndType(productRecommendAddDTO.getProductSpuId(), productRecommendAddDTO.getType()) != null) {
return ServiceExceptionUtil.error(PromotionErrorCodeEnum.PRODUCT_RECOMMEND_EXISTS.getCode());
}
// 保存到数据库
ProductRecommendDO productRecommend = ProductRecommendConvert.INSTANCE.convert(productRecommendAddDTO).setStatus(CommonStatusEnum.ENABLE.getValue());
productRecommend.setDeleted(DeletedStatusEnum.DELETED_NO.getValue()).setCreateTime(new Date());
productRecommendMapper.insert(productRecommend);
// 返回成功
return CommonResult.success(ProductRecommendConvert.INSTANCE.convertToBO(productRecommend));
}
@Override
public CommonResult<Boolean> updateProductRecommend(Integer adminId, ProductRecommendUpdateDTO productRecommendUpdateDTO) {
// 校验参数
if (!CommonStatusEnum.isValid(productRecommendUpdateDTO.getType())) {
return CommonResult.error(SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getCode(), "推荐类型必须是新品1或热卖2"); // TODO 有点搓
}
// 校验更新的商品推荐存在
if (productRecommendMapper.selectById(productRecommendUpdateDTO.getId()) == null) {
return ServiceExceptionUtil.error(PromotionErrorCodeEnum.PRODUCT_RECOMMEND_NOT_EXISTS.getCode());
}
// 校验商品不存在
if (productSpuService.getProductSpu(productRecommendUpdateDTO.getProductSpuId()) == null) {
return ServiceExceptionUtil.error(PromotionErrorCodeEnum.PRODUCT_RECOMMEND_PRODUCT_NOT_EXISTS.getCode());
}
// 校验商品是否已经推荐
ProductRecommendDO existProductRecommend = productRecommendMapper.selectByProductSpuIdAndType(productRecommendUpdateDTO.getProductSpuId(), productRecommendUpdateDTO.getType());
if (existProductRecommend != null && !existProductRecommend.getId().equals(productRecommendUpdateDTO.getId())) {
return ServiceExceptionUtil.error(PromotionErrorCodeEnum.PRODUCT_RECOMMEND_EXISTS.getCode());
}
// 更新到数据库
ProductRecommendDO updateProductRecommend = ProductRecommendConvert.INSTANCE.convert(productRecommendUpdateDTO);
productRecommendMapper.update(updateProductRecommend);
// 返回成功
return CommonResult.success(true);
}
@Override
public CommonResult<Boolean> updateProductRecommendStatus(Integer adminId, Integer productRecommendId, Integer status) {
// 校验参数
if (!CommonStatusEnum.isValid(status)) {
return CommonResult.error(SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getCode(), "变更状态必须是开启1或关闭2"); // TODO 有点搓
}
// 校验更新的商品推荐存在
if (productRecommendMapper.selectById(productRecommendId) == null) {
return ServiceExceptionUtil.error(PromotionErrorCodeEnum.PRODUCT_RECOMMEND_NOT_EXISTS.getCode());
}
// 更新到数据库
ProductRecommendDO updateProductRecommend = new ProductRecommendDO().setId(productRecommendId).setStatus(status);
productRecommendMapper.update(updateProductRecommend);
// 返回成功
return CommonResult.success(true);
}
@Override
public CommonResult<Boolean> deleteProductRecommend(Integer adminId, Integer productRecommendId) {
// 校验更新的商品推荐存在
if (productRecommendMapper.selectById(productRecommendId) == null) {
return ServiceExceptionUtil.error(PromotionErrorCodeEnum.PRODUCT_RECOMMEND_NOT_EXISTS.getCode());
}
// 更新到数据库
ProductRecommendDO updateProductRecommend = new ProductRecommendDO().setId(productRecommendId);
updateProductRecommend.setDeleted(DeletedStatusEnum.DELETED_YES.getValue());
productRecommendMapper.update(updateProductRecommend);
// 返回成功
return CommonResult.success(true);
}
}

View File

@@ -0,0 +1,125 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.iocoder.mall.promotion.biz.dao.ProductRecommendMapper">
<sql id="FIELDS">
id, type, product_spu_id, sort,
status, memo, create_time
</sql>
<!-- <select id="selectListByPidAndStatusOrderBySort" resultType="BannerDO">-->
<!-- SELECT-->
<!-- <include refid="FIELDS" />-->
<!-- FROM product_recommend-->
<!-- WHERE pid = #{pid}-->
<!-- AND status = #{status}-->
<!-- AND deleted = 0-->
<!-- ORDER BY sort ASC-->
<!-- </select>-->
<!-- <select id="selectList" resultType="BannerDO">-->
<!-- SELECT-->
<!-- <include refid="FIELDS" />-->
<!-- FROM product_recommend-->
<!-- WHERE deleted = 0-->
<!-- </select>-->
<select id="selectById" parameterType="Integer" resultType="BannerDO">
SELECT
<include refid="FIELDS" />
FROM product_recommend
WHERE id = #{id}
AND deleted = 0
</select>
<select id="selectByProductSpuIdAndType" resultType="BannerDO">
SELECT
<include refid="FIELDS" />
FROM product_recommend
<where>
<if test="productSpuId != null">
product_spu_id = #{productSpuId}
</if>
<if test="type != null">
type = #{type}
</if>
AND deleted = 0
</where>
</select>
<select id="selectListByTypeAndStatus" parameterType="Integer" resultType="BannerDO">
SELECT
<include refid="FIELDS" />
FROM product_recommend
<where>
<if test="type != null">
type = #{type}
</if>
<if test="status != null">
status = #{status}
</if>
AND deleted = 0
</where>
</select>
<select id="selectPageByType" resultType="BannerDO">
SELECT
<include refid="FIELDS" />
FROM product_recommend
<where>
<if test="type != null">
type = #{type}
</if>
AND deleted = 0
</where>
LIMIT #{offset}, #{limit}
</select>
<select id="selectCountByType" resultType="Integer">
SELECT
COUNT(1)
FROM product_recommend
<where>
<if test="type != null">
type = #{type}
</if>
AND deleted = 0
</where>
</select>
<insert id="insert" parameterType="BannerDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
INSERT INTO product_recommend (
type, product_spu_id, sort, status, memo,
create_time, deleted
) VALUES (
#{type}, #{productSpuId}, #{sort}, #{status}, #{memo},
#{createTime}, #{deleted}
)
</insert>
<update id="update" parameterType="BannerDO">
UPDATE product_recommend
<set>
<if test="type != null">
type = #{type},
</if>
<if test="productSpuId != null">
product_spu_id = #{productSpuId},
</if>
<if test="sort != null">
sort = #{sort},
</if>
<if test="status != null">
status = #{status},
</if>
<if test="memo != null">
memo = #{memo},
</if>
<if test="deleted != null">
deleted = #{deleted}
</if>
</set>
WHERE id = #{id}
</update>
</mapper>