14 changed files with 431 additions and 0 deletions
Unified View
Diff Options
-
34dating-agency-mall-constant/src/main/java/com/qniao/dam/domian/aggregate/paymentorder/constant/PaymentMethodEnum.java
-
40dating-agency-mall-constant/src/main/java/com/qniao/dam/domian/aggregate/paymentorder/constant/PaymentOrderStatusEnum.java
-
40dating-agency-mall-entity/src/main/java/com/qniao/dam/domain/aggregate/paymentorder/entity/PaymentOrder.java
-
31dating-agency-mall-server/src/main/java/com/qniao/dam/api/command/order/user/OrderUserCommandController.java
-
35dating-agency-mall-server/src/main/java/com/qniao/dam/api/command/order/user/request/UserSubmitOrderDto.java
-
23dating-agency-mall-server/src/main/java/com/qniao/dam/api/command/order/user/response/UserSubmitOrderVo.java
-
86dating-agency-mall-server/src/main/java/com/qniao/dam/application/service/order/OrderApplicationService.java
-
13dating-agency-mall-server/src/main/java/com/qniao/dam/domain/aggregate/order/OrderAggregate.java
-
20dating-agency-mall-server/src/main/java/com/qniao/dam/domain/aggregate/paymentorder/PaymentOrderAggregate.java
-
7dating-agency-mall-server/src/main/java/com/qniao/dam/domain/aggregate/paymentorder/repository/PaymentOrderRepository.java
-
27dating-agency-mall-server/src/main/java/com/qniao/dam/domain/service/order/SubmitOrderDomainService.java
-
7dating-agency-mall-server/src/main/java/com/qniao/dam/infrastructure/persistent/dao/domain/PaymentOrderDao.java
-
31dating-agency-mall-server/src/main/java/com/qniao/dam/infrastructure/persistent/repository/impl/PaymentOrderRepositoryImpl.java
-
37dating-agency-mall-server/src/main/java/com/qniao/dam/infrastructure/utils/SnowFlakeUtil.java
@ -0,0 +1,34 @@ |
|||||
|
package com.qniao.dam.domian.aggregate.paymentorder.constant; |
||||
|
|
||||
|
import com.baomidou.mybatisplus.annotation.EnumValue; |
||||
|
import com.fasterxml.jackson.annotation.JsonCreator; |
||||
|
import com.fasterxml.jackson.annotation.JsonFormat; |
||||
|
import com.fasterxml.jackson.annotation.JsonValue; |
||||
|
import lombok.Getter; |
||||
|
|
||||
|
@Getter |
||||
|
@JsonFormat(shape = JsonFormat.Shape.OBJECT) |
||||
|
public enum PaymentMethodEnum { |
||||
|
|
||||
|
WECHAT_PAY(1, "微信付款"); |
||||
|
|
||||
|
@EnumValue |
||||
|
@JsonValue |
||||
|
private final Integer value; |
||||
|
private final String desc; |
||||
|
|
||||
|
PaymentMethodEnum(Integer value, String desc) { |
||||
|
this.value = value; |
||||
|
this.desc = desc; |
||||
|
} |
||||
|
|
||||
|
@JsonCreator |
||||
|
public static PaymentMethodEnum get(Object code) { |
||||
|
for (PaymentMethodEnum e : PaymentMethodEnum.values()) { |
||||
|
if (e.getValue().equals(code)) { |
||||
|
return e; |
||||
|
} |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,40 @@ |
|||||
|
package com.qniao.dam.domian.aggregate.paymentorder.constant; |
||||
|
|
||||
|
import com.baomidou.mybatisplus.annotation.EnumValue; |
||||
|
import com.fasterxml.jackson.annotation.JsonCreator; |
||||
|
import com.fasterxml.jackson.annotation.JsonFormat; |
||||
|
import com.fasterxml.jackson.annotation.JsonValue; |
||||
|
import lombok.Getter; |
||||
|
|
||||
|
@Getter |
||||
|
@JsonFormat(shape = JsonFormat.Shape.OBJECT) |
||||
|
public enum PaymentOrderStatusEnum { |
||||
|
|
||||
|
UNPAID(1, "未付款"), |
||||
|
|
||||
|
PARTIAL_PAYMENT(2, "部分付款"), |
||||
|
|
||||
|
PAID(3, "已付款"), |
||||
|
|
||||
|
CANCELED(4, "已取消"); |
||||
|
|
||||
|
@EnumValue |
||||
|
@JsonValue |
||||
|
private final Integer value; |
||||
|
private final String desc; |
||||
|
|
||||
|
PaymentOrderStatusEnum(Integer value, String desc) { |
||||
|
this.value = value; |
||||
|
this.desc = desc; |
||||
|
} |
||||
|
|
||||
|
@JsonCreator |
||||
|
public static PaymentOrderStatusEnum get(Object code) { |
||||
|
for (PaymentOrderStatusEnum e : PaymentOrderStatusEnum.values()) { |
||||
|
if (e.getValue().equals(code)) { |
||||
|
return e; |
||||
|
} |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,40 @@ |
|||||
|
package com.qniao.dam.domain.aggregate.paymentorder.entity; |
||||
|
|
||||
|
import com.baomidou.mybatisplus.annotation.TableName; |
||||
|
import com.qniao.dam.domain.aggregate.order.entity.OrderItem; |
||||
|
import com.qniao.dam.domian.aggregate.paymentorder.constant.PaymentMethodEnum; |
||||
|
import com.qniao.dam.domian.aggregate.paymentorder.constant.PaymentOrderStatusEnum; |
||||
|
import com.qniao.domain.Entity; |
||||
|
import io.swagger.annotations.ApiModelProperty; |
||||
|
import lombok.Data; |
||||
|
import lombok.EqualsAndHashCode; |
||||
|
|
||||
|
import java.math.BigDecimal; |
||||
|
import java.time.LocalDateTime; |
||||
|
|
||||
|
@Data |
||||
|
@EqualsAndHashCode(callSuper = true) |
||||
|
@TableName("da_order") |
||||
|
public class PaymentOrder extends Entity<OrderItem> { |
||||
|
|
||||
|
@ApiModelProperty("订单标识") |
||||
|
private Long orderId; |
||||
|
|
||||
|
@ApiModelProperty("应付金额") |
||||
|
private BigDecimal payableAmount; |
||||
|
|
||||
|
@ApiModelProperty("未付金额") |
||||
|
private BigDecimal unpaidAmount; |
||||
|
|
||||
|
@ApiModelProperty("实付金额") |
||||
|
private BigDecimal paidAmount; |
||||
|
|
||||
|
@ApiModelProperty("付款时间") |
||||
|
private LocalDateTime paidTime; |
||||
|
|
||||
|
@ApiModelProperty("付款方式") |
||||
|
private PaymentMethodEnum paymentMethod; |
||||
|
|
||||
|
@ApiModelProperty("状态") |
||||
|
private PaymentOrderStatusEnum status; |
||||
|
} |
||||
@ -0,0 +1,31 @@ |
|||||
|
package com.qniao.dam.api.command.order.user; |
||||
|
|
||||
|
import com.qniao.dam.api.command.order.user.request.UserSubmitOrderDto; |
||||
|
import com.qniao.dam.api.command.order.user.response.UserSubmitOrderVo; |
||||
|
import com.qniao.dam.application.service.order.OrderApplicationService; |
||||
|
import com.qniao.dam.domain.aggregate.order.entity.Order; |
||||
|
import io.swagger.annotations.Api; |
||||
|
import io.swagger.annotations.ApiOperation; |
||||
|
import org.springframework.web.bind.annotation.*; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
import javax.validation.Valid; |
||||
|
|
||||
|
@RestController |
||||
|
@RequestMapping("user") |
||||
|
@Api(tags = "订单") |
||||
|
public class OrderUserCommandController { |
||||
|
|
||||
|
@Resource |
||||
|
private OrderApplicationService orderApplicationService; |
||||
|
|
||||
|
@ApiOperation("用户提交订单") |
||||
|
@PostMapping("submit/order") |
||||
|
public UserSubmitOrderVo userSubmitOrder(@RequestBody @Valid UserSubmitOrderDto dto, |
||||
|
@RequestParam("userId") Long userId) { |
||||
|
Order order = dto.trans2Domain(); |
||||
|
order.setUserId(userId); |
||||
|
return orderApplicationService.submit(dto.trans2Domain()); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,35 @@ |
|||||
|
package com.qniao.dam.api.command.order.user.request; |
||||
|
|
||||
|
import com.qniao.dam.domain.aggregate.order.entity.Order; |
||||
|
import com.qniao.dam.domain.aggregate.order.entity.OrderItem; |
||||
|
import com.qniao.domain.Trans2DomainAssembler; |
||||
|
import io.swagger.annotations.ApiModelProperty; |
||||
|
import lombok.Data; |
||||
|
|
||||
|
import javax.validation.constraints.NotNull; |
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
|
||||
|
@Data |
||||
|
public class UserSubmitOrderDto implements Trans2DomainAssembler<Order> { |
||||
|
|
||||
|
@ApiModelProperty("征婚资料标识") |
||||
|
@NotNull(message = "征婚资料标识不能为空") |
||||
|
private Long miId; |
||||
|
|
||||
|
@ApiModelProperty("产品规格标识") |
||||
|
@NotNull(message = "产品规格标识不能为空") |
||||
|
private Long productSpecId; |
||||
|
|
||||
|
@Override |
||||
|
public Order trans2Domain() { |
||||
|
Order order = new Order(); |
||||
|
order.setMiId(miId); |
||||
|
List<OrderItem> orderItemList = new ArrayList<>(); |
||||
|
OrderItem orderItem = new OrderItem(); |
||||
|
orderItem.setProductSpecId(productSpecId); |
||||
|
orderItem.setQuantity(1); |
||||
|
order.setOrderItemList(orderItemList); |
||||
|
return order; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,23 @@ |
|||||
|
package com.qniao.dam.api.command.order.user.response; |
||||
|
|
||||
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize; |
||||
|
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; |
||||
|
import io.swagger.annotations.ApiModelProperty; |
||||
|
import lombok.AllArgsConstructor; |
||||
|
import lombok.Data; |
||||
|
import lombok.NoArgsConstructor; |
||||
|
|
||||
|
@AllArgsConstructor |
||||
|
@NoArgsConstructor |
||||
|
@Data |
||||
|
public class UserSubmitOrderVo { |
||||
|
|
||||
|
@ApiModelProperty("订单标识") |
||||
|
@JsonSerialize(using = ToStringSerializer.class) |
||||
|
private Long orderId; |
||||
|
|
||||
|
@ApiModelProperty("付款单标识") |
||||
|
@JsonSerialize(using = ToStringSerializer.class) |
||||
|
private Long paymentOrderId; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,86 @@ |
|||||
|
package com.qniao.dam.application.service.order; |
||||
|
|
||||
|
import cn.hutool.core.collection.CollUtil; |
||||
|
import com.qniao.dam.api.command.order.user.response.UserSubmitOrderVo; |
||||
|
import com.qniao.dam.domain.aggregate.order.entity.Order; |
||||
|
import com.qniao.dam.domain.aggregate.order.entity.OrderItem; |
||||
|
import com.qniao.dam.domain.aggregate.paymentorder.entity.PaymentOrder; |
||||
|
import com.qniao.dam.domain.aggregate.product.entity.Product; |
||||
|
import com.qniao.dam.domain.aggregate.product.repository.ProductRepository; |
||||
|
import com.qniao.dam.domain.aggregate.productspec.entity.ProductSpec; |
||||
|
import com.qniao.dam.domain.aggregate.productspec.repository.ProductSpecRepository; |
||||
|
import com.qniao.dam.domain.service.order.SubmitOrderDomainService; |
||||
|
import com.qniao.dam.infrastructure.utils.SnowFlakeUtil; |
||||
|
import com.qniao.dam.query.product.ProductQueryService; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
import java.math.BigDecimal; |
||||
|
|
||||
|
@Service |
||||
|
public class OrderApplicationService { |
||||
|
|
||||
|
@Resource |
||||
|
private SubmitOrderDomainService submitOrderDomainService; |
||||
|
@Resource |
||||
|
private ProductSpecRepository productSpecRepository; |
||||
|
@Resource |
||||
|
private ProductRepository productRepository; |
||||
|
@Resource |
||||
|
private SnowFlakeUtil snowFlakeUtil; |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* todo 库存校验 暂时不用考虑 |
||||
|
* |
||||
|
* @param order |
||||
|
* @return |
||||
|
*/ |
||||
|
public UserSubmitOrderVo submit(Order order) { |
||||
|
//1. 填充订单信息 |
||||
|
fillOrderInfo(order); |
||||
|
//2. 订单金额计算 |
||||
|
countOrderAmount(order); |
||||
|
//3. 设置订单号 |
||||
|
order.setOrderCode(snowFlakeUtil.getSnowflakeOrderCode()); |
||||
|
//4. 组织付款单信息 |
||||
|
PaymentOrder paymentOrder = makePaymentOrder(order); |
||||
|
submitOrderDomainService.handle(order,paymentOrder); |
||||
|
return new UserSubmitOrderVo(order.getId(), paymentOrder.getId()); |
||||
|
} |
||||
|
|
||||
|
private PaymentOrder makePaymentOrder(Order order) { |
||||
|
PaymentOrder paymentOrder = new PaymentOrder(); |
||||
|
paymentOrder.setPayableAmount(order.getSettlementAmount()); |
||||
|
paymentOrder.setUnpaidAmount(paymentOrder.getPayableAmount()); |
||||
|
paymentOrder.setPaidAmount(BigDecimal.ZERO); |
||||
|
return paymentOrder; |
||||
|
} |
||||
|
|
||||
|
private void countOrderAmount(Order order) { |
||||
|
if (CollUtil.isNotEmpty(order.getOrderItemList())) { |
||||
|
BigDecimal totalSettlementAmount = BigDecimal.ZERO; |
||||
|
for (OrderItem orderItem : order.getOrderItemList()) { |
||||
|
orderItem.setSettlementAmount(orderItem.getUnitSettlementPrice().multiply(BigDecimal.valueOf(orderItem.getQuantity()))); |
||||
|
totalSettlementAmount = totalSettlementAmount.add(orderItem.getSettlementAmount()); |
||||
|
} |
||||
|
order.setSettlementAmount(totalSettlementAmount); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void fillOrderInfo(Order order) { |
||||
|
if (CollUtil.isNotEmpty(order.getOrderItemList())) { |
||||
|
order.getOrderItemList().forEach(orderItem -> { |
||||
|
ProductSpec productSpec = productSpecRepository.load(orderItem.getProductSpecId()); |
||||
|
Product product = productRepository.load(productSpec.getProductId()); |
||||
|
orderItem.setProductId(product.getId()); |
||||
|
orderItem.setProductType(product.getProductType()); |
||||
|
orderItem.setMainCategory(product.getMainCategory()); |
||||
|
orderItem.setSubCategory(product.getSubCategory()); |
||||
|
orderItem.setProductTitle(product.getProductTitle()); |
||||
|
orderItem.setProductDesc(product.getProductDesc()); |
||||
|
orderItem.setUnitSettlementPrice(productSpec.getUnitSellingPrice()); |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,7 +1,20 @@ |
|||||
package com.qniao.dam.domain.aggregate.order; |
package com.qniao.dam.domain.aggregate.order; |
||||
|
|
||||
|
import com.qniao.dam.domain.aggregate.order.entity.Order; |
||||
|
import com.qniao.dam.domain.aggregate.order.repository.OrderRepository; |
||||
|
import com.qniao.dam.domian.aggregate.order.constant.OrderStatusEnum; |
||||
import org.springframework.stereotype.Service; |
import org.springframework.stereotype.Service; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
|
||||
@Service |
@Service |
||||
public class OrderAggregate { |
public class OrderAggregate { |
||||
|
|
||||
|
@Resource |
||||
|
private OrderRepository orderRepository; |
||||
|
|
||||
|
public void submit(Order order) { |
||||
|
order.setStatus(OrderStatusEnum.UNPAID); |
||||
|
orderRepository.save(order); |
||||
|
} |
||||
} |
} |
||||
@ -0,0 +1,20 @@ |
|||||
|
package com.qniao.dam.domain.aggregate.paymentorder; |
||||
|
|
||||
|
import com.qniao.dam.domain.aggregate.paymentorder.entity.PaymentOrder; |
||||
|
import com.qniao.dam.domain.aggregate.paymentorder.repository.PaymentOrderRepository; |
||||
|
import com.qniao.dam.domian.aggregate.paymentorder.constant.PaymentOrderStatusEnum; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
|
||||
|
@Service |
||||
|
public class PaymentOrderAggregate { |
||||
|
|
||||
|
@Resource |
||||
|
private PaymentOrderRepository paymentOrderRepository; |
||||
|
|
||||
|
public void create(PaymentOrder paymentOrder) { |
||||
|
paymentOrder.setStatus(PaymentOrderStatusEnum.UNPAID); |
||||
|
paymentOrderRepository.save(paymentOrder); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,7 @@ |
|||||
|
package com.qniao.dam.domain.aggregate.paymentorder.repository; |
||||
|
|
||||
|
import com.qniao.dam.domain.aggregate.paymentorder.entity.PaymentOrder; |
||||
|
import com.qniao.domain.Repository; |
||||
|
|
||||
|
public interface PaymentOrderRepository extends Repository<PaymentOrder, Long> { |
||||
|
} |
||||
@ -0,0 +1,27 @@ |
|||||
|
package com.qniao.dam.domain.service.order; |
||||
|
|
||||
|
import com.qniao.dam.domain.aggregate.order.OrderAggregate; |
||||
|
import com.qniao.dam.domain.aggregate.order.entity.Order; |
||||
|
import com.qniao.dam.domain.aggregate.paymentorder.PaymentOrderAggregate; |
||||
|
import com.qniao.dam.domain.aggregate.paymentorder.entity.PaymentOrder; |
||||
|
import com.qniao.dam.domian.aggregate.paymentorder.constant.PaymentOrderStatusEnum; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.springframework.transaction.annotation.Transactional; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
|
||||
|
@Service |
||||
|
public class SubmitOrderDomainService { |
||||
|
|
||||
|
@Resource |
||||
|
private OrderAggregate orderAggregate; |
||||
|
@Resource |
||||
|
private PaymentOrderAggregate paymentOrderAggregate; |
||||
|
|
||||
|
@Transactional(rollbackFor = Exception.class) |
||||
|
public void handle(Order order, PaymentOrder paymentOrder) { |
||||
|
orderAggregate.submit(order); |
||||
|
paymentOrder.setOrderId(order.getId()); |
||||
|
paymentOrderAggregate.create(paymentOrder); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,7 @@ |
|||||
|
package com.qniao.dam.infrastructure.persistent.dao.domain; |
||||
|
|
||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
||||
|
import com.qniao.dam.domain.aggregate.paymentorder.entity.PaymentOrder; |
||||
|
|
||||
|
public interface PaymentOrderDao extends BaseMapper<PaymentOrder> { |
||||
|
} |
||||
@ -0,0 +1,31 @@ |
|||||
|
package com.qniao.dam.infrastructure.persistent.repository.impl; |
||||
|
|
||||
|
import com.qniao.dam.domain.aggregate.paymentorder.entity.PaymentOrder; |
||||
|
import com.qniao.dam.domain.aggregate.paymentorder.repository.PaymentOrderRepository; |
||||
|
import com.qniao.dam.infrastructure.persistent.dao.domain.PaymentOrderDao; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
import java.util.Objects; |
||||
|
|
||||
|
@Service |
||||
|
public class PaymentOrderRepositoryImpl implements PaymentOrderRepository { |
||||
|
|
||||
|
@Resource |
||||
|
private PaymentOrderDao paymentOrderDao; |
||||
|
|
||||
|
@Override |
||||
|
public PaymentOrder load(Long id) { |
||||
|
return paymentOrderDao.selectById(id); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public Long save(PaymentOrder entity) { |
||||
|
if (Objects.isNull(entity.getId()) || Objects.isNull(paymentOrderDao.selectById(entity.getId()))) { |
||||
|
paymentOrderDao.insert(entity); |
||||
|
} else { |
||||
|
paymentOrderDao.updateById(entity); |
||||
|
} |
||||
|
return entity.getId(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,37 @@ |
|||||
|
package com.qniao.dam.infrastructure.utils; |
||||
|
|
||||
|
import cn.hutool.core.lang.Snowflake; |
||||
|
import cn.hutool.core.net.NetUtil; |
||||
|
import cn.hutool.core.util.IdUtil; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
import java.text.SimpleDateFormat; |
||||
|
import java.time.LocalDate; |
||||
|
|
||||
|
@Component |
||||
|
public class SnowFlakeUtil { |
||||
|
|
||||
|
private long workerId = 1; |
||||
|
private long datacenter = 1; |
||||
|
private Snowflake snowflake = IdUtil.createSnowflake(workerId, datacenter); |
||||
|
|
||||
|
{ |
||||
|
workerId = NetUtil.ipv4ToLong(NetUtil.getLocalhostStr()); |
||||
|
System.out.println(workerId); |
||||
|
} |
||||
|
|
||||
|
public long getSnowflakeId() { |
||||
|
return snowflake.nextId(); |
||||
|
} |
||||
|
|
||||
|
public String getSnowflakeOrderCode() { |
||||
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); |
||||
|
return "D" + dateFormat.format(LocalDate.now()) + getSnowflakeId(); |
||||
|
} |
||||
|
|
||||
|
public long snowflakeId(long workerId, long datacenterId) { |
||||
|
Snowflake snowflake = IdUtil.createSnowflake(workerId, datacenterId); |
||||
|
return snowflake.nextId(); |
||||
|
} |
||||
|
|
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save