商品分类部分提交

This commit is contained in:
YunaiV
2019-03-03 22:54:36 +08:00
parent 359d6766f3
commit 17e1a017c9
22 changed files with 547 additions and 87 deletions

View File

@@ -1,6 +1,8 @@
package cn.iocoder.mall.product.convert;
import cn.iocoder.mall.product.api.bo.ProductCategoryBO;
import cn.iocoder.mall.product.api.dto.ProductCategoryAddDTO;
import cn.iocoder.mall.product.api.dto.ProductCategoryUpdateDTO;
import cn.iocoder.mall.product.dataobject.ProductCategoryDO;
import org.mapstruct.Mapper;
import org.mapstruct.Mappings;
@@ -16,6 +18,13 @@ public interface ProductCategoryConvert {
@Mappings({})
ProductCategoryBO convertToBO(ProductCategoryDO category);
@Mappings({})
List<ProductCategoryBO> convertToBO(List<ProductCategoryDO> categoryList);
@Mappings({})
ProductCategoryDO convert(ProductCategoryAddDTO productCategoryAddDTO);
@Mappings({})
ProductCategoryDO convert(ProductCategoryUpdateDTO productCategoryUpdateDTO);
}

View File

@@ -12,4 +12,14 @@ public interface ProductCategoryMapper {
List<ProductCategoryDO> selectListByPidAndStatusOrderBySort(@Param("pid") Integer pid,
@Param("status") Integer status);
List<ProductCategoryDO> selectList();
ProductCategoryDO selectById(@Param("id") Integer id);
void insert(ProductCategoryDO productCategoryDO);
int update(ProductCategoryDO productCategoryDO);
}

View File

@@ -1,11 +1,11 @@
package cn.iocoder.mall.product.dataobject;
import java.util.Date;
import cn.iocoder.common.framework.dataobject.BaseDO;
/**
* 商品分类
*/
public class ProductCategoryDO {
public class ProductCategoryDO extends BaseDO {
public static final Integer STATUS_ENABLE = 1;
@@ -35,19 +35,11 @@ public class ProductCategoryDO {
* 排序值
*/
private Integer sort;
/**
* 创建时间
*/
private Date createTime;
/**
* 最后更新时间
*/
private Date updateTime;
/**
* 状态
*
* 1-正常
* 2-删除
* 1-开启
* 2-关闭
*/
private Integer status;
@@ -55,72 +47,62 @@ public class ProductCategoryDO {
return id;
}
public void setId(Integer id) {
public ProductCategoryDO setId(Integer id) {
this.id = id;
return this;
}
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
public ProductCategoryDO setPid(Integer pid) {
this.pid = pid;
return this;
}
public String getName() {
return name;
}
public void setName(String name) {
public ProductCategoryDO setName(String name) {
this.name = name;
return this;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
public ProductCategoryDO setDescription(String description) {
this.description = description;
return this;
}
public String getPicUrl() {
return picUrl;
}
public void setPicUrl(String picUrl) {
public ProductCategoryDO setPicUrl(String picUrl) {
this.picUrl = picUrl;
return this;
}
public Integer getSort() {
return sort;
}
public void setSort(Integer sort) {
public ProductCategoryDO setSort(Integer sort) {
this.sort = sort;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
return this;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
public ProductCategoryDO setStatus(Integer status) {
this.status = status;
return this;
}
}

View File

@@ -1,13 +1,21 @@
package cn.iocoder.mall.product.service;
import cn.iocoder.common.framework.dataobject.BaseDO;
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.product.api.ProductCategoryService;
import cn.iocoder.mall.product.api.bo.ProductCategoryBO;
import cn.iocoder.mall.product.api.constant.ProductCategoryConstants;
import cn.iocoder.mall.product.api.constant.ProductErrorCodeEnum;
import cn.iocoder.mall.product.api.dto.ProductCategoryAddDTO;
import cn.iocoder.mall.product.api.dto.ProductCategoryUpdateDTO;
import cn.iocoder.mall.product.convert.ProductCategoryConvert;
import cn.iocoder.mall.product.dao.ProductCategoryMapper;
import cn.iocoder.mall.product.dataobject.ProductCategoryDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
@Service // 实际上不用添加。添加的原因是,必须 Spring 报错提示
@@ -23,4 +31,87 @@ public class ProductCategoryServiceImpl implements ProductCategoryService {
return ProductCategoryConvert.INSTANCE.convertToBO(categoryList);
}
@Override
public List<ProductCategoryBO> getAll() {
List<ProductCategoryDO> categoryList = productCategoryMapper.selectList();
return ProductCategoryConvert.INSTANCE.convertToBO(categoryList);
}
@Override
public CommonResult<ProductCategoryBO> addProductCategory(Integer adminId, ProductCategoryAddDTO productCategoryAddDTO) {
// 校验父分类是否存在
if (ProductCategoryConstants.PID_ROOT.equals(productCategoryAddDTO.getPid())
&& productCategoryMapper.selectById(productCategoryAddDTO.getPid()) == null) {
return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_CATEGORY_PARENT_NOT_EXISTS.getCode());
}
// 保存到数据库
ProductCategoryDO productCategory = ProductCategoryConvert.INSTANCE.convert(productCategoryAddDTO)
.setStatus(ProductCategoryConstants.STATUS_ENABLE);
productCategory.setCreateTime(new Date()).setDeleted(BaseDO.DELETED_NO);
productCategoryMapper.insert(productCategory);
// TODO 操作日志
// 返回成功
return CommonResult.success(ProductCategoryConvert.INSTANCE.convertToBO(productCategory));
}
@Override
public CommonResult<Boolean> updateProductCategory(Integer adminId, ProductCategoryUpdateDTO productCategoryUpdateDTO) {
// 校验分类是否存在
if (productCategoryMapper.selectById(productCategoryUpdateDTO.getId()) == null) {
return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_CATEGORY_NOT_EXISTS.getCode());
}
// 校验不能设置自己为父分类
if (productCategoryUpdateDTO.getId().equals(productCategoryUpdateDTO.getPid())) {
return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_CATEGORY_PARENT_NOT_SELF.getCode());
}
// 校验父分类是否存在
if (ProductCategoryConstants.PID_ROOT.equals(productCategoryUpdateDTO.getPid())
&& productCategoryMapper.selectById(productCategoryUpdateDTO.getPid()) == null) {
return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_CATEGORY_PARENT_NOT_EXISTS.getCode());
}
// 更新到数据库
ProductCategoryDO updateProductCategory = ProductCategoryConvert.INSTANCE.convert(productCategoryUpdateDTO);
productCategoryMapper.update(updateProductCategory);
// TODO 操作日志
return CommonResult.success(true);
}
@Override
public CommonResult<Boolean> updateProductCategoryStatus(Integer adminId, Integer productCategoryId, Integer status) {
// 校验分类是否存在
ProductCategoryDO productCategory = productCategoryMapper.selectById(productCategoryId);
if (productCategory == null) {
return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_CATEGORY_NOT_EXISTS.getCode());
}
// 如果状态相同,则返回错误
if (productCategory.getStatus().equals(status)) {
return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_CATEGORY_STATUS_EQUALS.getCode());
}
// 更新商品分类
ProductCategoryDO updateProductCategory = new ProductCategoryDO().setStatus(status);
productCategoryMapper.update(updateProductCategory);
// TODO 操作日志
return CommonResult.success(true);
}
@Override
public CommonResult<Boolean> deleteProductCategory(Integer admin, Integer productCategoryId) {
// 校验分类是否存在
ProductCategoryDO productCategory = productCategoryMapper.selectById(productCategoryId);
if (productCategory == null) {
return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_CATEGORY_NOT_EXISTS.getCode());
}
// 只有禁用的商品分类才可以删除
if (ProductCategoryConstants.STATUS_ENABLE.equals(productCategory.getStatus())) {
return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_CATEGORY_DELETE_ONLY_DISABLE.getCode());
}
// TODO 芋艿:考虑下,是否需要判断下该分类下是否有商品
// 标记删除商品分类
ProductCategoryDO updateProductCategory = new ProductCategoryDO();
updateProductCategory.setDeleted(BaseDO.DELETED_YES);
productCategoryMapper.update(updateProductCategory);
// TODO 操作日志
return CommonResult.success(true);
}
}

View File

@@ -1,10 +1,10 @@
spring:
# datasource
datasource:
url: jdbc:mysql://127.0.0.1:33061/mall_product?useSSL=false
url: jdbc:mysql://180.167.213.26:13306/mall_product?useSSL=false&useUnicode=true&characterEncoding=UTF-8
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
password: ${MALL_MYSQL_PASSWORD}
# mybatis
mybatis:

View File

@@ -2,14 +2,72 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.iocoder.mall.product.dao.ProductCategoryMapper">
<sql id="FIELDS">
id, pid, name, descrption, pic_url,
sort, status, create_time
</sql>
<select id="selectListByPidAndStatusOrderBySort" resultType="ProductCategoryDO">
SELECT
id, name, pic_url, sort
<include refid="FIELDS" />
FROM product_category
WHERE pid = #{pid}
AND status = #{status}
AND deleted = 0
ORDER BY sort ASC
</select>
<select id="selectList" resultType="ProductCategoryDO">
SELECT
<include refid="FIELDS" />
FROM product_category
WHERE deleted = 0
</select>
</mapper>
<select id="selectById" parameterType="Integer" resultType="ProductCategoryDO">
SELECT
<include refid="FIELDS" />
FROM product_category
WHERE id = #{id}
AND deleted = 0
</select>
<insert id="insert" parameterType="ProductCategoryDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
INSERT INTO product_category (
pid, name, description, pic_url, sort,
status, create_time, deleted
) VALUES (
#{pid}, #{name}, #{description}, #{picUrl}, #{sort},
#{status}, #{createTime}, #{deleted}
)
</insert>
<update id="update" parameterType="ProductCategoryDO">
UPDATE resource
<set>
<if test="pid != null">
pid = #{pid},
</if>
<if test="name != null">
name = #{name},
</if>
<if test="description != null">
description = #{description},
</if>
<if test="picUrl != null">
pic_url = #{picUrl},
</if>
<if test="sort != null">
sort = #{sort},
</if>
<if test="status != null">
status = #{status},
</if>
<if test="deleted != null">
deleted = #{deleted}
</if>
</set>
WHERE id = #{id}
</update>
</mapper>