- demo 项目,增加提交订单接口
This commit is contained in:
parent
73af7c3932
commit
683b9a7a19
|
@ -0,0 +1,28 @@
|
|||
package cn.iocoder.mall.demo.application.controller;
|
||||
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import cn.iocoder.mall.demo.application.convert.DemOrderConvert;
|
||||
import cn.iocoder.mall.demo.application.dto.DemoOrderAddDTO;
|
||||
import cn.iocoder.mall.demo.business.api.DemoOrderService;
|
||||
import cn.iocoder.mall.demo.business.bo.order.DemoOrderAddBO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/order")
|
||||
public class DemoOrderController {
|
||||
|
||||
@Autowired
|
||||
private DemoOrderService demoOrderService;
|
||||
|
||||
@PostMapping("/add")
|
||||
public CommonResult<Integer> add(DemoOrderAddDTO addDTO) {
|
||||
DemoOrderAddBO addBO = DemOrderConvert.INSTANCE.convert(addDTO);
|
||||
addBO.setUserId(10); // TODO 10 用户编号。
|
||||
Integer orderId = demoOrderService.add(addBO);
|
||||
return CommonResult.success(orderId);
|
||||
}
|
||||
|
||||
}
|
|
@ -4,7 +4,7 @@ import cn.iocoder.common.framework.vo.CommonResult;
|
|||
import cn.iocoder.mall.demo.application.convert.DemoProductConvert;
|
||||
import cn.iocoder.mall.demo.application.vo.DemoProductVO;
|
||||
import cn.iocoder.mall.demo.business.api.DemoProductService;
|
||||
import cn.iocoder.mall.demo.business.bo.DemoProductBO;
|
||||
import cn.iocoder.mall.demo.business.bo.product.DemoProductBO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
package cn.iocoder.mall.demo.application.convert;
|
||||
|
||||
import cn.iocoder.mall.demo.application.dto.DemoOrderAddDTO;
|
||||
import cn.iocoder.mall.demo.business.bo.order.DemoOrderAddBO;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mappings;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
@Mapper
|
||||
public interface DemOrderConvert {
|
||||
|
||||
DemOrderConvert INSTANCE = Mappers.getMapper(DemOrderConvert.class);
|
||||
|
||||
@Mappings({})
|
||||
DemoOrderAddBO convert(DemoOrderAddDTO addDTO);
|
||||
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
package cn.iocoder.mall.demo.application.convert;
|
||||
|
||||
import cn.iocoder.mall.demo.application.vo.DemoProductVO;
|
||||
import cn.iocoder.mall.demo.business.bo.DemoProductBO;
|
||||
import cn.iocoder.mall.demo.business.bo.product.DemoProductBO;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mappings;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
package cn.iocoder.mall.demo.application.dto;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
@ApiModel("Demo 订单添加 DTO")
|
||||
@Data
|
||||
public class DemoOrderAddDTO {
|
||||
|
||||
@ApiModelProperty(value = "商品编号", required = true, example = "1")
|
||||
private Integer productId;
|
||||
|
||||
}
|
|
@ -1,4 +1,22 @@
|
|||
package cn.iocoder.mall.demo.application.vo;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
@ApiModel("Demo 商品 BO")
|
||||
@Data
|
||||
public class DemoProductVO {
|
||||
|
||||
@ApiModelProperty(value = "编号", required = true, example = "1")
|
||||
private Integer id;
|
||||
|
||||
@ApiModelProperty(value = "华为 Mate30 Pro", required = true, example = "小王")
|
||||
private String name;
|
||||
|
||||
@ApiModelProperty(value = "价格,单位:分", required = true, example = "10")
|
||||
private Integer price;
|
||||
@ApiModelProperty(value = "库存数量", required = true, example = "100")
|
||||
private Integer quantity;
|
||||
|
||||
}
|
||||
|
|
|
@ -1,7 +1,12 @@
|
|||
package cn.iocoder.mall.demo.business.api;
|
||||
|
||||
import cn.iocoder.mall.demo.business.bo.order.DemoOrderAddBO;
|
||||
import cn.iocoder.mall.demo.business.bo.order.DemoOrderCancelBO;
|
||||
|
||||
public interface DemoOrderService {
|
||||
|
||||
int add(DemoOrderAddBO addBO);
|
||||
|
||||
int cancel(DemoOrderCancelBO cancelBO);
|
||||
|
||||
}
|
||||
|
|
|
@ -1,9 +1,20 @@
|
|||
package cn.iocoder.mall.demo.business.api;
|
||||
|
||||
import cn.iocoder.mall.demo.business.bo.DemoProductBO;
|
||||
import cn.iocoder.common.framework.vo.PageResult;
|
||||
import cn.iocoder.mall.demo.business.bo.product.*;
|
||||
|
||||
public interface DemoProductService {
|
||||
|
||||
DemoProductBO get(Integer id);
|
||||
|
||||
PageResult<DemoProductBO> page(DemoProductPageBO page);
|
||||
|
||||
int add(DemoProductAddBO product);
|
||||
|
||||
int update(DemoProductUpdateBO product);
|
||||
|
||||
// void updateQuantityIncrease();
|
||||
|
||||
void updateQuantityReduce(DemoProductQuantityReduceBO reduceBO);
|
||||
|
||||
}
|
||||
|
|
|
@ -1,4 +0,0 @@
|
|||
package cn.iocoder.mall.demo.business.bo;
|
||||
|
||||
public class DemoProductBO {
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package cn.iocoder.mall.demo.business.bo.order;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* Demo 订单添加 BO
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class DemoOrderAddBO {
|
||||
|
||||
@NotNull(message = "用户编号不能为空")
|
||||
private Integer userId;
|
||||
|
||||
@NotNull(message = "用户编号不能为空")
|
||||
private Integer productId;
|
||||
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package cn.iocoder.mall.demo.business.bo.order;
|
||||
|
||||
public class DemoOrderCancelBO {
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package cn.iocoder.mall.demo.business.bo.product;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
/**
|
||||
* Demo 商品添加 BO
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class DemoProductAddBO {
|
||||
|
||||
/**
|
||||
* 名字
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 价格
|
||||
*/
|
||||
private Integer price;
|
||||
/**
|
||||
* 库存数量
|
||||
*/
|
||||
private Integer quantity;
|
||||
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package cn.iocoder.mall.demo.business.bo.product;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
/**
|
||||
* Demo 商品 BO
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class DemoProductBO {
|
||||
|
||||
/**
|
||||
* 编号
|
||||
*/
|
||||
private Integer id;
|
||||
|
||||
/**
|
||||
* 名字
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 价格
|
||||
*/
|
||||
private Integer price;
|
||||
/**
|
||||
* 库存数量
|
||||
*/
|
||||
private Integer quantity;
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package cn.iocoder.mall.demo.business.bo.product;
|
||||
|
||||
import cn.iocoder.common.framework.vo.PageParam;
|
||||
|
||||
|
||||
public class DemoProductPageBO extends PageParam {
|
||||
|
||||
/**
|
||||
* 名字,模糊搜索
|
||||
*/
|
||||
private String name;
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package cn.iocoder.mall.demo.business.bo.product;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* Demo 商品库存减少 BO
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class DemoProductQuantityReduceBO {
|
||||
|
||||
/**
|
||||
* 商品编号
|
||||
*/
|
||||
@NotNull(message = "商品编号不能为空")
|
||||
private Integer id;
|
||||
|
||||
/**
|
||||
* 减少数量
|
||||
*/
|
||||
@NotNull(message = "减少数量不能为空")
|
||||
@Min(value = 1, message = "减少数量最小为 1")
|
||||
private Integer quantity;
|
||||
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package cn.iocoder.mall.demo.business.bo.product;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
/**
|
||||
* Demo 商品更新 BO
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class DemoProductUpdateBO {
|
||||
|
||||
/**
|
||||
* 编号
|
||||
*/
|
||||
private Integer id;
|
||||
|
||||
/**
|
||||
* 名字
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 价格
|
||||
*/
|
||||
private Integer price;
|
||||
/**
|
||||
* 库存数量
|
||||
*/
|
||||
private Integer quantity;
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package cn.iocoder.mall.demo.business.constant;
|
||||
|
||||
/**
|
||||
* 订单 - status
|
||||
*
|
||||
* @author Sin
|
||||
* @time 2019-03-16 14:06
|
||||
*/
|
||||
public enum OrderStatusEnum {
|
||||
|
||||
WAITING_PAYMENT(1, "等待付款"),
|
||||
WAIT_SHIPMENT(2, "等待发货"),
|
||||
ALREADY_SHIPMENT(3, "已发货"),
|
||||
COMPLETED(4, "已完成"),
|
||||
CLOSED(5, "已关闭");
|
||||
|
||||
/**
|
||||
* 状态值
|
||||
*/
|
||||
private Integer value;
|
||||
/**
|
||||
* 状态名
|
||||
*/
|
||||
private String name;
|
||||
|
||||
OrderStatusEnum(int value, String name) {
|
||||
this.value = value;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -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> {
|
||||
|
||||
}
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
|
|
@ -18,6 +18,11 @@ public class DemoOrderDO extends DeletableDO {
|
|||
*/
|
||||
private Integer id;
|
||||
|
||||
/**
|
||||
* 用户编号
|
||||
*/
|
||||
private Integer userId;
|
||||
|
||||
/**
|
||||
* 商品编号
|
||||
*/
|
||||
|
|
|
@ -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 {
|
||||
|
||||
/**
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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 芋艿,错误码
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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>
|
|
@ -0,0 +1,15 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>demo</artifactId>
|
||||
<groupId>cn.iocoder.mall</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>demo-mq</artifactId>
|
||||
|
||||
|
||||
</project>
|
|
@ -18,6 +18,7 @@
|
|||
<module>demo-business-api</module>
|
||||
<module>demo-business</module>
|
||||
<module>demo-job</module>
|
||||
<module>demo-mq</module>
|
||||
</modules>
|
||||
|
||||
|
||||
|
|
|
@ -28,6 +28,7 @@ public class AdminBO implements Serializable {
|
|||
@ApiModelProperty(value = "创建时间", required = true, example = "时间戳格式")
|
||||
private Date createTime;
|
||||
|
||||
// TODO FROM 芋艿 to :参数命名不正确。
|
||||
@ApiModelProperty(value = "部门ID", required = true, example = "1")
|
||||
private Integer deptmentId;
|
||||
|
||||
|
|
Loading…
Reference in New Issue