- demo 项目,增加提交订单接口

This commit is contained in:
YunaiV
2019-09-20 16:53:30 +08:00
parent 73af7c3932
commit 683b9a7a19
29 changed files with 438 additions and 9 deletions

View File

@@ -0,0 +1,17 @@
package cn.iocoder.mall.demo.business.convert;
import cn.iocoder.mall.demo.business.bo.order.DemoOrderAddBO;
import cn.iocoder.mall.demo.business.dataobject.order.DemoOrderDO;
import org.mapstruct.Mapper;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
@Mapper
public interface DemoOrderConvert {
DemoOrderConvert INSTANCE = Mappers.getMapper(DemoOrderConvert.class);
@Mappings({})
DemoOrderDO convert(DemoOrderAddBO object);
}

View File

@@ -1,6 +1,6 @@
package cn.iocoder.mall.demo.business.convert;
import cn.iocoder.mall.demo.business.bo.DemoProductBO;
import cn.iocoder.mall.demo.business.bo.product.DemoProductBO;
import cn.iocoder.mall.demo.business.dataobject.product.DemoProductDO;
import org.mapstruct.Mapper;
import org.mapstruct.Mappings;

View File

@@ -0,0 +1,10 @@
package cn.iocoder.mall.demo.business.dao;
import cn.iocoder.mall.demo.business.dataobject.order.DemoOrderDO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Repository;
@Repository
public interface DemoOrderMapper extends BaseMapper<DemoOrderDO> {
}

View File

@@ -2,9 +2,13 @@ package cn.iocoder.mall.demo.business.dao;
import cn.iocoder.mall.demo.business.dataobject.product.DemoProductDO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface DemoProductMapper extends BaseMapper<DemoProductDO> {
int updateQuantityReduce(@Param("id") Integer id,
@Param("quantity") Integer quantity);
}

View File

@@ -18,6 +18,11 @@ public class DemoOrderDO extends DeletableDO {
*/
private Integer id;
/**
* 用户编号
*/
private Integer userId;
/**
* 商品编号
*/

View File

@@ -1,6 +1,7 @@
package cn.iocoder.mall.demo.business.dataobject.product;
import cn.iocoder.common.framework.dataobject.DeletableDO;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.experimental.Accessors;
@@ -9,6 +10,7 @@ import lombok.experimental.Accessors;
*/
@Data
@Accessors(chain = true)
@TableName(value = "product")
public class DemoProductDO extends DeletableDO {
/**

View File

@@ -1,6 +1,58 @@
package cn.iocoder.mall.demo.business.service;
import cn.iocoder.common.framework.constant.DeletedStatusEnum;
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
import cn.iocoder.mall.demo.business.api.DemoOrderService;
import cn.iocoder.mall.demo.business.api.DemoProductService;
import cn.iocoder.mall.demo.business.bo.order.DemoOrderAddBO;
import cn.iocoder.mall.demo.business.bo.order.DemoOrderCancelBO;
import cn.iocoder.mall.demo.business.bo.product.DemoProductBO;
import cn.iocoder.mall.demo.business.bo.product.DemoProductQuantityReduceBO;
import cn.iocoder.mall.demo.business.constant.OrderStatusEnum;
import cn.iocoder.mall.demo.business.convert.DemoOrderConvert;
import cn.iocoder.mall.demo.business.dao.DemoOrderMapper;
import cn.iocoder.mall.demo.business.dataobject.order.DemoOrderDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class DemoOrderServiceImpl implements DemoOrderService {
@Autowired
private DemoProductService demoProductService;
@Autowired
private DemoOrderMapper demoOrderMapper;
@Override
public int add(DemoOrderAddBO addBO) {
// 产品信息
DemoProductBO productBO = demoProductService.get(addBO.getProductId());
if (productBO == null) { // 商品不存在
throw ServiceExceptionUtil.exception(100000); // TODO 芋艿,错误码
}
int quantity = 1;
if (productBO.getQuantity() < quantity) { // 库存不够
throw ServiceExceptionUtil.exception(100001); // TODO 芋艿,错误码
}
// 扣除库存
demoProductService.updateQuantityReduce(new DemoProductQuantityReduceBO()
.setId(addBO.getProductId()).setQuantity(quantity));
// 创建订单
DemoOrderDO orderDO = DemoOrderConvert.INSTANCE.convert(addBO);
orderDO.setStatus(OrderStatusEnum.WAITING_PAYMENT.getValue())
.setDeleted(DeletedStatusEnum.DELETED_NO.getValue());
demoOrderMapper.insert(orderDO);
// 返回订单编号
return orderDO.getId();
}
@Override
public int cancel(DemoOrderCancelBO cancelBO) {
return 0;
}
}

View File

@@ -1,7 +1,9 @@
package cn.iocoder.mall.demo.business.service;
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
import cn.iocoder.common.framework.vo.PageResult;
import cn.iocoder.mall.demo.business.api.DemoProductService;
import cn.iocoder.mall.demo.business.bo.DemoProductBO;
import cn.iocoder.mall.demo.business.bo.product.*;
import cn.iocoder.mall.demo.business.convert.DemoProductConvert;
import cn.iocoder.mall.demo.business.dao.DemoProductMapper;
import cn.iocoder.mall.demo.business.dataobject.product.DemoProductDO;
@@ -20,4 +22,27 @@ public class DemoProductServiceImpl implements DemoProductService {
return DemoProductConvert.INSTANCE.convert(product);
}
@Override
public PageResult<DemoProductBO> page(DemoProductPageBO page) {
return null;
}
@Override
public int add(DemoProductAddBO product) {
return 0;
}
@Override
public int update(DemoProductUpdateBO product) {
return 0;
}
@Override
public void updateQuantityReduce(DemoProductQuantityReduceBO reduceBO) {
int updateCount = demoProductMapper.updateQuantityReduce(reduceBO.getId(), reduceBO.getQuantity());
if (updateCount == 0) {
throw ServiceExceptionUtil.exception(20000); // TODO 芋艿,错误码
}
}
}

View File

@@ -0,0 +1,12 @@
<?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.demo.business.dao.DemoProductMapper">
<update id="updateQuantityReduce">
UPDATE product
SET quantity = quantity - #{quantity}
WHERE id = #{id}
AND quantity >= #{quantity}
</update>
</mapper>