|
|
|
@ -0,0 +1,233 @@
|
|
|
|
|
|
|
|
package com.cyl.h5.service;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
|
|
|
|
|
import com.cyl.h5.pojo.dto.OrderCreateDTO;
|
|
|
|
|
|
|
|
import com.cyl.h5.pojo.dto.OrderProductListDTO;
|
|
|
|
|
|
|
|
import com.cyl.h5.pojo.vo.OrderCalcVO;
|
|
|
|
|
|
|
|
import com.cyl.h5.pojo.vo.SkuViewDTO;
|
|
|
|
|
|
|
|
import com.cyl.h5.pojo.vo.form.OrderSubmitForm;
|
|
|
|
|
|
|
|
import com.cyl.manager.oms.domain.Order;
|
|
|
|
|
|
|
|
import com.cyl.manager.oms.domain.OrderOperateHistory;
|
|
|
|
|
|
|
|
import com.cyl.manager.oms.mapper.OrderItemMapper;
|
|
|
|
|
|
|
|
import com.cyl.manager.oms.mapper.OrderMapper;
|
|
|
|
|
|
|
|
import com.cyl.manager.oms.mapper.OrderOperateHistoryMapper;
|
|
|
|
|
|
|
|
import com.cyl.manager.oms.service.OrderItemService;
|
|
|
|
|
|
|
|
import com.cyl.manager.pms.domain.Product;
|
|
|
|
|
|
|
|
import com.cyl.manager.pms.domain.Sku;
|
|
|
|
|
|
|
|
import com.cyl.manager.pms.mapper.ProductMapper;
|
|
|
|
|
|
|
|
import com.cyl.manager.pms.mapper.SkuMapper;
|
|
|
|
|
|
|
|
import com.cyl.manager.ums.domain.Member;
|
|
|
|
|
|
|
|
import com.cyl.manager.ums.domain.MemberAddress;
|
|
|
|
|
|
|
|
import com.cyl.manager.ums.domain.MemberCart;
|
|
|
|
|
|
|
|
import com.cyl.manager.ums.mapper.MemberAddressMapper;
|
|
|
|
|
|
|
|
import com.cyl.manager.ums.mapper.MemberCartMapper;
|
|
|
|
|
|
|
|
import com.ruoyi.common.constant.Constants;
|
|
|
|
|
|
|
|
import com.ruoyi.common.utils.IDGenerator;
|
|
|
|
|
|
|
|
import com.ruoyi.framework.config.LocalDataUtil;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
|
|
|
public class H5OrderService {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
private MemberAddressMapper memberAddressMapper;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
private SkuMapper skuMapper;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
private ProductMapper productMapper;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
private MemberCartMapper memberCartMapper;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
private OrderMapper orderMapper;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
private OrderItemMapper orderItemMapper;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
private OrderOperateHistoryMapper orderOperateHistoryMapper;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
private OrderItemService orderItemService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
|
|
|
|
public Long submit(OrderSubmitForm form) {
|
|
|
|
|
|
|
|
Member member = (Member) LocalDataUtil.getVar(Constants.MEMBER_INFO);
|
|
|
|
|
|
|
|
//只支持快递
|
|
|
|
|
|
|
|
Long addressId = form.getAddressId();
|
|
|
|
|
|
|
|
if (addressId == null){
|
|
|
|
|
|
|
|
throw new RuntimeException("收获地址不能为空");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
MemberAddress memberAddress = memberAddressMapper.selectById(addressId);
|
|
|
|
|
|
|
|
if (memberAddress == null){
|
|
|
|
|
|
|
|
throw new RuntimeException("收货地址不能为空");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//sku不能为空
|
|
|
|
|
|
|
|
List<OrderProductListDTO> skuList = form.getSkuList();
|
|
|
|
|
|
|
|
if (CollectionUtil.isEmpty(skuList)){
|
|
|
|
|
|
|
|
throw new RuntimeException("商品SKU信息不能为空");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//将sku信息转换为 key:skuId ,value:购买数量
|
|
|
|
|
|
|
|
Map<Long, Integer> skuQuantityMap = skuList.stream().collect(Collectors.toMap(OrderProductListDTO::getSkuId, OrderProductListDTO::getQuantity));
|
|
|
|
|
|
|
|
//查询所有sku信息
|
|
|
|
|
|
|
|
Map<Long, Sku> querySkuMap = skuMapper
|
|
|
|
|
|
|
|
.selectBatchIds(skuList.stream().map(OrderProductListDTO::getSkuId).collect(Collectors.toList()))
|
|
|
|
|
|
|
|
.stream().collect(Collectors.toMap(Sku::getId, it -> it));
|
|
|
|
|
|
|
|
//计算商品总额、订单总额(订单总金额=商品总金额,因为暂时没有运费等概念)
|
|
|
|
|
|
|
|
BigDecimal productTotalAmount = BigDecimal.ZERO;
|
|
|
|
|
|
|
|
BigDecimal orderTotalAmount = BigDecimal.ZERO;
|
|
|
|
|
|
|
|
for (OrderProductListDTO dto : skuList){
|
|
|
|
|
|
|
|
if (!querySkuMap.containsKey(dto.getSkuId())){
|
|
|
|
|
|
|
|
throw new RuntimeException("商品SKU不存在");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Sku sku = querySkuMap.get(dto.getSkuId());
|
|
|
|
|
|
|
|
Product product = productMapper.selectById(sku.getProductId());
|
|
|
|
|
|
|
|
if (product == null){
|
|
|
|
|
|
|
|
throw new RuntimeException("商品不存在");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Constants.PublishStatus.UNDERCARRIAGE.equals(product.getPublishStatus())){
|
|
|
|
|
|
|
|
throw new RuntimeException("商品" + product.getName() + "已下架");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
productTotalAmount = productTotalAmount.add(sku.getPrice().multiply(BigDecimal.valueOf(skuQuantityMap.get(sku.getId()))));
|
|
|
|
|
|
|
|
orderTotalAmount = orderTotalAmount.add(sku.getPrice().multiply(BigDecimal.valueOf(skuQuantityMap.get(sku.getId()))));
|
|
|
|
|
|
|
|
dto.setSku(sku);
|
|
|
|
|
|
|
|
dto.setProduct(product);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
LocalDateTime optTime = LocalDateTime.now();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//生成一个统一的订单号
|
|
|
|
|
|
|
|
Long orderId = IDGenerator.generateId();
|
|
|
|
|
|
|
|
//创建订单
|
|
|
|
|
|
|
|
Order order = new Order();
|
|
|
|
|
|
|
|
order.setId(orderId);
|
|
|
|
|
|
|
|
order.setOrderSn(this.getOrderIdPrefix() + orderId);
|
|
|
|
|
|
|
|
order.setMemberId(member.getId());
|
|
|
|
|
|
|
|
order.setMemberUsername(member.getNickname());
|
|
|
|
|
|
|
|
order.setPayType(Constants.PayType.WECHAT);
|
|
|
|
|
|
|
|
order.setTotalAmount(orderTotalAmount);
|
|
|
|
|
|
|
|
order.setPurchasePrice(BigDecimal.ZERO);
|
|
|
|
|
|
|
|
order.setFreightAmount(BigDecimal.ZERO);
|
|
|
|
|
|
|
|
order.setPayAmount(orderTotalAmount);
|
|
|
|
|
|
|
|
//暂时为接入支付,直接设置为待发货
|
|
|
|
|
|
|
|
order.setStatus(Constants.OrderStatus.SEND);
|
|
|
|
|
|
|
|
order.setAftersaleStatus(1);
|
|
|
|
|
|
|
|
order.setReceiverName(memberAddress.getName());
|
|
|
|
|
|
|
|
order.setReceiverPhone(memberAddress.getPhoneHidden());
|
|
|
|
|
|
|
|
order.setReceiverPostCode(memberAddress.getPostCode());
|
|
|
|
|
|
|
|
order.setReceiverProvince(memberAddress.getProvince());
|
|
|
|
|
|
|
|
order.setReceiverCity(memberAddress.getCity());
|
|
|
|
|
|
|
|
order.setReceiverDistrict(memberAddress.getDistrict());
|
|
|
|
|
|
|
|
order.setReceiverProvinceId(memberAddress.getProvinceId());
|
|
|
|
|
|
|
|
order.setReceiverCityId(memberAddress.getCityId());
|
|
|
|
|
|
|
|
order.setReceiverDistrictId(memberAddress.getDistrictId());
|
|
|
|
|
|
|
|
order.setReceiverDetailAddress(memberAddress.getDetailAddress());
|
|
|
|
|
|
|
|
order.setNote(form.getNote());
|
|
|
|
|
|
|
|
order.setConfirmStatus(0);
|
|
|
|
|
|
|
|
order.setDeleteStatus(0);
|
|
|
|
|
|
|
|
order.setPaymentTime(optTime);
|
|
|
|
|
|
|
|
order.setCreateTime(optTime);
|
|
|
|
|
|
|
|
order.setCreateBy(member.getId());
|
|
|
|
|
|
|
|
int rows = orderMapper.insert(order);
|
|
|
|
|
|
|
|
if (rows < 1){
|
|
|
|
|
|
|
|
throw new RuntimeException("订单新增失败");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 保存orderItem
|
|
|
|
|
|
|
|
orderItemService.saveOrderItem(member, optTime, orderId, skuList);
|
|
|
|
|
|
|
|
// 保存订单操作记录
|
|
|
|
|
|
|
|
OrderOperateHistory orderOperateHistory = new OrderOperateHistory();
|
|
|
|
|
|
|
|
orderOperateHistory.setOrderId(orderId);
|
|
|
|
|
|
|
|
orderOperateHistory.setOperateMan(member.getId() + "");
|
|
|
|
|
|
|
|
orderOperateHistory.setOrderStatus(Constants.OrderStatus.SEND);
|
|
|
|
|
|
|
|
orderOperateHistory.setCreateTime(optTime);
|
|
|
|
|
|
|
|
orderOperateHistory.setCreateBy(member.getId());
|
|
|
|
|
|
|
|
rows = orderOperateHistoryMapper.insert(orderOperateHistory);
|
|
|
|
|
|
|
|
if (rows < 1){
|
|
|
|
|
|
|
|
throw new RuntimeException("保存订单操作记录失败");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//若来源为购物车,删除购物车
|
|
|
|
|
|
|
|
if (Constants.OrderFrom.CART.equals(form.getFrom())){
|
|
|
|
|
|
|
|
List<Long> skuIdList = skuList.stream().map(OrderProductListDTO::getSkuId).collect(Collectors.toList());
|
|
|
|
|
|
|
|
LambdaUpdateWrapper<MemberCart> wrapper = Wrappers.lambdaUpdate();
|
|
|
|
|
|
|
|
wrapper.eq(MemberCart::getMemberId, member.getId());
|
|
|
|
|
|
|
|
wrapper.in(MemberCart::getSkuId, skuIdList);
|
|
|
|
|
|
|
|
rows = memberCartMapper.delete(wrapper);
|
|
|
|
|
|
|
|
if (rows < 1){
|
|
|
|
|
|
|
|
throw new RuntimeException("删除购物车失败");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//当前返回成功消息,接入支付后可返回payId
|
|
|
|
|
|
|
|
return orderId;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public OrderCalcVO addOrderCheck(OrderCreateDTO orderCreateDTO) {
|
|
|
|
|
|
|
|
OrderCalcVO res = new OrderCalcVO();
|
|
|
|
|
|
|
|
List<SkuViewDTO> skuList = new ArrayList<>();
|
|
|
|
|
|
|
|
List<OrderProductListDTO> list = orderCreateDTO.getSkuList();
|
|
|
|
|
|
|
|
if (CollectionUtil.isEmpty(list)){
|
|
|
|
|
|
|
|
throw new RuntimeException("商品SKU信息不能为空");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//将购买的sku信息转化为key:skuId value:数量
|
|
|
|
|
|
|
|
Map<Long, Integer> quantityMap = list.stream().
|
|
|
|
|
|
|
|
collect(Collectors.toMap(OrderProductListDTO::getSkuId, OrderProductListDTO::getQuantity, (v1, v2) -> v2));
|
|
|
|
|
|
|
|
//查询所有sku信息
|
|
|
|
|
|
|
|
Set<Long> collect = list.stream().map(OrderProductListDTO::getSkuId).collect(Collectors.toSet());
|
|
|
|
|
|
|
|
Map<Long, Sku> querySkuMap = skuMapper.selectBatchIds(collect).stream().collect(Collectors.toMap(Sku::getId, it -> it));
|
|
|
|
|
|
|
|
//计算商品总金额、订单总金额
|
|
|
|
|
|
|
|
BigDecimal productTotalAmount = BigDecimal.ZERO;
|
|
|
|
|
|
|
|
BigDecimal orderTotalAmount = BigDecimal.ZERO;
|
|
|
|
|
|
|
|
for (OrderProductListDTO dto : list){
|
|
|
|
|
|
|
|
if (!querySkuMap.containsKey(dto.getSkuId())){
|
|
|
|
|
|
|
|
throw new RuntimeException("商品SKU不存在");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Sku sku = querySkuMap.get(dto.getSkuId());
|
|
|
|
|
|
|
|
//查product
|
|
|
|
|
|
|
|
Product product = productMapper.selectById(sku.getProductId());
|
|
|
|
|
|
|
|
if (product == null){
|
|
|
|
|
|
|
|
throw new RuntimeException("商品不存在");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Constants.PublishStatus.UNDERCARRIAGE.equals(product.getPublishStatus())){
|
|
|
|
|
|
|
|
throw new RuntimeException("商品" + product.getName() + "已下架");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
BigDecimal addAmount = sku.getPrice().multiply(BigDecimal.valueOf(dto.getQuantity()));
|
|
|
|
|
|
|
|
//由于目前没有运费等数据,暂时订单总金额=商品总金额了
|
|
|
|
|
|
|
|
productTotalAmount = productTotalAmount.add(addAmount);
|
|
|
|
|
|
|
|
orderTotalAmount = orderTotalAmount.add(addAmount);
|
|
|
|
|
|
|
|
//封装sku信息
|
|
|
|
|
|
|
|
SkuViewDTO skuViewDTO = new SkuViewDTO();
|
|
|
|
|
|
|
|
skuViewDTO.setPic(product.getPic());
|
|
|
|
|
|
|
|
skuViewDTO.setPrice(sku.getPrice());
|
|
|
|
|
|
|
|
skuViewDTO.setProductId(product.getId());
|
|
|
|
|
|
|
|
skuViewDTO.setProductName(product.getName());
|
|
|
|
|
|
|
|
skuViewDTO.setQuantity(quantityMap.get(sku.getId()));
|
|
|
|
|
|
|
|
skuViewDTO.setSkuId(sku.getId());
|
|
|
|
|
|
|
|
skuViewDTO.setSpData(sku.getSpData());
|
|
|
|
|
|
|
|
skuList.add(skuViewDTO);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
res.setSkuList(skuList);
|
|
|
|
|
|
|
|
res.setOrderTotalAmount(orderTotalAmount);
|
|
|
|
|
|
|
|
res.setProductTotalAmount(productTotalAmount);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private String getOrderIdPrefix(){
|
|
|
|
|
|
|
|
LocalDateTime time = LocalDateTime.now();
|
|
|
|
|
|
|
|
return time.format(DateTimeFormatter.ofPattern("yyMMdd")) + "-";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|