feat(orders.manager):新增用户下单的功能
This commit is contained in:
parent
27dc6c16af
commit
85b6192ba1
@ -3,6 +3,7 @@ package com.jzo2o.orders.base.model.domain;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
@ -20,6 +21,7 @@ import java.time.LocalDateTime;
|
||||
* @since 2023-08-02
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@Accessors(chain = true)
|
||||
@TableName("orders")
|
||||
@ -79,7 +81,7 @@ public class Orders implements Serializable {
|
||||
private Integer ordersStatus;
|
||||
|
||||
/**
|
||||
* 交易状态,0:待支付,1:支付成功
|
||||
* 支付状态,2:待支付,4:支付成功
|
||||
*/
|
||||
private Integer payStatus;
|
||||
|
||||
@ -164,7 +166,7 @@ public class Orders implements Serializable {
|
||||
private Integer display;
|
||||
|
||||
/**
|
||||
* 排序字段(serve_start_time秒级时间戳+订单id后6位)
|
||||
* 排序字段(serve_start_time秒级时间戳+订单id后5位)
|
||||
*/
|
||||
private Long sortBy;
|
||||
|
||||
@ -196,6 +198,4 @@ public class Orders implements Serializable {
|
||||
* 支付渠道
|
||||
*/
|
||||
private String tradingChannel;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@ -1,16 +1,10 @@
|
||||
package com.jzo2o.orders.manager.controller.consumer;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import com.jzo2o.api.market.dto.response.AvailableCouponsResDTO;
|
||||
import com.jzo2o.api.orders.dto.request.OrderCancelReqDTO;
|
||||
import com.jzo2o.api.orders.dto.response.OrderResDTO;
|
||||
import com.jzo2o.api.orders.dto.response.OrderSimpleResDTO;
|
||||
import com.jzo2o.common.model.CurrentUserInfo;
|
||||
import com.jzo2o.common.utils.ObjectUtils;
|
||||
import com.jzo2o.mvc.utils.UserContext;
|
||||
import com.jzo2o.orders.manager.model.dto.OrderCancelDTO;
|
||||
import com.jzo2o.orders.manager.model.dto.request.OrdersPayReqDTO;
|
||||
import com.jzo2o.orders.manager.model.dto.request.PlaceOrderReqDTO;
|
||||
import com.jzo2o.orders.manager.model.dto.response.OrdersPayResDTO;
|
||||
import com.jzo2o.orders.manager.model.dto.response.PlaceOrderResDTO;
|
||||
import com.jzo2o.orders.manager.service.IOrdersCreateService;
|
||||
import com.jzo2o.orders.manager.service.IOrdersManagerService;
|
||||
@ -27,13 +21,14 @@ import java.util.List;
|
||||
* @author itcast
|
||||
*/
|
||||
@RestController("consumerOrdersController")
|
||||
@Api(tags = "用户端-订单相关接口")
|
||||
@Api(tags = "用户端 - 订单相关接口")
|
||||
@RequestMapping("/consumer/orders")
|
||||
public class ConsumerOrdersController {
|
||||
|
||||
@Resource
|
||||
private IOrdersManagerService ordersManagerService;
|
||||
|
||||
@Resource
|
||||
private IOrdersCreateService ordersCreateService;
|
||||
|
||||
@GetMapping("/{id}")
|
||||
@ApiOperation("根据订单id查询")
|
||||
@ -43,14 +38,26 @@ public class ConsumerOrdersController {
|
||||
public OrderResDTO detail(@PathVariable("id") Long id) {
|
||||
return ordersManagerService.getDetail(id);
|
||||
}
|
||||
|
||||
@GetMapping("/consumerQueryList")
|
||||
@ApiOperation("订单滚动分页查询")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "ordersStatus", value = "订单状态,0:待支付,100:派单中,200:待服务,300:服务中,400:待评价,500:订单完成,600:订单取消,700:已关闭", required = false, dataTypeClass = Integer.class),
|
||||
@ApiImplicitParam(name = "sortBy", value = "排序字段", required = false, dataTypeClass = Long.class)
|
||||
@ApiImplicitParam(name = "ordersStatus", value = "订单状态,0:待支付,100:派单中,200:待服务,300:服务中,400:待评价,500:订单完成,600:订单取消,700:已关闭", dataTypeClass = Integer.class),
|
||||
@ApiImplicitParam(name = "sortBy", value = "排序字段", dataTypeClass = Long.class)
|
||||
})
|
||||
public List<OrderSimpleResDTO> consumerQueryList(@RequestParam(value = "ordersStatus", required = false) Integer ordersStatus,
|
||||
@RequestParam(value = "sortBy", required = false) Long sortBy) {
|
||||
return ordersManagerService.consumerQueryList(UserContext.currentUserId(), ordersStatus, sortBy);
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/place")
|
||||
@ApiOperation("下单接口")
|
||||
public PlaceOrderResDTO place(@RequestBody PlaceOrderReqDTO placeOrderReqDTO) {
|
||||
// 默认下单1个服务
|
||||
if (ObjectUtils.isEmpty(placeOrderReqDTO.getPurNum())) {
|
||||
placeOrderReqDTO.setPurNum(1);
|
||||
}
|
||||
|
||||
return ordersCreateService.placeOrder(placeOrderReqDTO);
|
||||
}
|
||||
}
|
||||
@ -1,24 +1,10 @@
|
||||
package com.jzo2o.orders.manager.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.jzo2o.api.market.dto.request.CouponUseBackReqDTO;
|
||||
import com.jzo2o.api.market.dto.response.AvailableCouponsResDTO;
|
||||
import com.jzo2o.api.orders.dto.response.OrderResDTO;
|
||||
import com.jzo2o.api.orders.dto.response.OrderSimpleResDTO;
|
||||
import com.jzo2o.common.model.PageResult;
|
||||
import com.jzo2o.common.model.msg.TradeStatusMsg;
|
||||
import com.jzo2o.orders.base.model.domain.Orders;
|
||||
import com.jzo2o.orders.manager.model.dto.OrderCancelDTO;
|
||||
import com.jzo2o.orders.manager.model.dto.request.OrderPageQueryReqDTO;
|
||||
import com.jzo2o.orders.manager.model.dto.request.OrdersPayReqDTO;
|
||||
import com.jzo2o.orders.manager.model.dto.request.PlaceOrderReqDTO;
|
||||
import com.jzo2o.orders.manager.model.dto.response.OperationOrdersDetailResDTO;
|
||||
import com.jzo2o.orders.manager.model.dto.response.OrdersPayResDTO;
|
||||
import com.jzo2o.orders.manager.model.dto.response.PlaceOrderResDTO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 下单服务类
|
||||
@ -28,6 +14,8 @@ import java.util.List;
|
||||
* @since 2023-07-10
|
||||
*/
|
||||
public interface IOrdersCreateService extends IService<Orders> {
|
||||
|
||||
|
||||
}
|
||||
/**
|
||||
* 客户端下单接口
|
||||
*/
|
||||
PlaceOrderResDTO placeOrder(PlaceOrderReqDTO placeOrderReqDTO);
|
||||
}
|
||||
@ -1,22 +1,113 @@
|
||||
package com.jzo2o.orders.manager.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
|
||||
import com.jzo2o.api.foundations.dto.response.ServeAggregationResDTO;
|
||||
import com.jzo2o.common.expcetions.CommonException;
|
||||
import com.jzo2o.common.expcetions.ForbiddenOperationException;
|
||||
import com.jzo2o.common.utils.DateUtils;
|
||||
import com.jzo2o.common.utils.ObjectUtils;
|
||||
import com.jzo2o.mvc.utils.UserContext;
|
||||
import com.jzo2o.orders.base.constants.RedisConstants;
|
||||
import com.jzo2o.orders.base.enums.OrderPayStatusEnum;
|
||||
import com.jzo2o.orders.base.enums.OrderStatusEnum;
|
||||
import com.jzo2o.orders.base.mapper.OrdersMapper;
|
||||
import com.jzo2o.orders.base.model.domain.Orders;
|
||||
import com.jzo2o.orders.manager.model.dto.request.PlaceOrderReqDTO;
|
||||
import com.jzo2o.orders.manager.model.dto.response.PlaceOrderResDTO;
|
||||
import com.jzo2o.orders.manager.service.IOrdersCreateService;
|
||||
import com.jzo2o.orders.manager.service.client.CustomerClient;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 下单服务类
|
||||
* </p>
|
||||
*
|
||||
* @author itcast
|
||||
* @since 2023-07-10
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class OrdersCreateServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersCreateService {
|
||||
@Resource
|
||||
private CustomerClient customerClient;
|
||||
@Resource
|
||||
private RedisTemplate<String, Long> redisTemplate;
|
||||
@Resource
|
||||
private TransactionTemplate transactionTemplate;
|
||||
|
||||
}
|
||||
/**
|
||||
* 生成订单号(2位年+2位月+2位日+13位序号)
|
||||
*/
|
||||
private Long generateOrderId() {
|
||||
Long idNo = redisTemplate.opsForValue().increment(RedisConstants.Lock.ORDERS_SHARD_KEY_ID_GENERATOR, 1);
|
||||
String orderId = DateUtils.format(LocalDateTime.now(), "yyMMdd") + String.format("%013d", idNo);
|
||||
return Long.valueOf(orderId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlaceOrderResDTO placeOrder(PlaceOrderReqDTO placeOrderReqDTO) {
|
||||
// 获取下单的基本信息(下单人/地址等)
|
||||
AddressBookResDTO address = customerClient.getAddressDetail(placeOrderReqDTO.getAddressBookId());
|
||||
if (ObjectUtils.isEmpty(address.getId())) {
|
||||
throw new ForbiddenOperationException("无法获取地址信息下单失败");
|
||||
}
|
||||
// 获取下单的服务信息
|
||||
ServeAggregationResDTO serve = customerClient.getServeAggregation(placeOrderReqDTO.getServeId());
|
||||
if (ObjectUtils.isEmpty(serve.getId())) {
|
||||
throw new ForbiddenOperationException("无法获取服务信息下单失败");
|
||||
}
|
||||
// 获取订单id
|
||||
Long orderId = generateOrderId();
|
||||
// TODO 获取优惠卷相关信息
|
||||
BigDecimal discountAmount = BigDecimal.ZERO;
|
||||
// 计算价格
|
||||
BigDecimal totalAmount = serve.getPrice().multiply(BigDecimal.valueOf(placeOrderReqDTO.getPurNum()));
|
||||
BigDecimal realPayAmount = totalAmount.subtract(discountAmount);
|
||||
|
||||
// 组装订单信息插入数据库完成下单
|
||||
LocalDateTime serveStartTime = placeOrderReqDTO.getServeStartTime();
|
||||
Long sortColumn = DateUtils.toEpochMilli(serveStartTime) * 100000 + orderId % 100000;
|
||||
Orders orderToPay = Orders.builder()
|
||||
.id(orderId)
|
||||
.userId(UserContext.currentUserId())
|
||||
.serveTypeId(serve.getServeTypeId())
|
||||
.serveTypeName(serve.getServeTypeName())
|
||||
.serveItemId(serve.getServeItemId())
|
||||
.serveItemName(serve.getServeItemName())
|
||||
.serveItemImg(serve.getServeItemImg())
|
||||
.unit(serve.getUnit())
|
||||
.serveId(serve.getId())
|
||||
.ordersStatus(OrderStatusEnum.NO_PAY.getStatus())
|
||||
.payStatus(OrderPayStatusEnum.NO_PAY.getStatus())
|
||||
.price(serve.getPrice())
|
||||
.purNum(placeOrderReqDTO.getPurNum())
|
||||
.totalAmount(totalAmount)
|
||||
.discountAmount(discountAmount)
|
||||
.realPayAmount(realPayAmount)
|
||||
.cityCode(serve.getCityCode())
|
||||
.serveAddress(address.getAddress())
|
||||
.contactsPhone(address.getPhone())
|
||||
.contactsName(address.getName())
|
||||
.serveStartTime(serveStartTime)
|
||||
.lon(address.getLon())
|
||||
.lat(address.getLat())
|
||||
.sortBy(sortColumn).build();
|
||||
|
||||
// 减小事务管理的颗粒度
|
||||
return new PlaceOrderResDTO(
|
||||
transactionTemplate.execute(status -> {
|
||||
if (baseMapper.insert(orderToPay) == 0) {
|
||||
throw new CommonException("下单失败");
|
||||
}
|
||||
return orderToPay.getId();
|
||||
}));
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user