package com.mall.service.impl;
import com.mall.common.*;
import com.mall.common.AppException;
import com.mall.controller.vo.*;
import com.mall.dao.GoodsMapper;
import com.mall.dao.OrderItemMapper;
import com.mall.dao.OrderMapper;
import com.mall.dao.ShoppingCartItemMapper;
import com.mall.entity.Goods;
import com.mall.entity.Order;
import com.mall.entity.OrderItem;
import com.mall.entity.StockNumDTO;
import com.mall.service.OrderService;
import com.mall.util.BeanUtil;
import com.mall.util.NumberUtil;
import com.mall.util.PageQueryUtil;
import com.mall.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.groupingBy;
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private OrderMapper newBeeMallOrderMapper;
@Autowired
private OrderItemMapper newBeeMallOrderItemMapper;
@Autowired
private ShoppingCartItemMapper newBeeMallShoppingCartItemMapper;
@Autowired
private GoodsMapper newBeeMallGoodsMapper;
@Override
public PageResult getNewBeeMallOrdersPage(PageQueryUtil pageUtil) {
List<Order> newBeeMallOrders = newBeeMallOrderMapper.findNewBeeMallOrderList(pageUtil);
int total = newBeeMallOrderMapper.getTotalNewBeeMallOrders(pageUtil);
PageResult pageResult = new PageResult(newBeeMallOrders, total, pageUtil.getLimit(), pageUtil.getPage());
return pageResult;
}
@Override
@Transactional
public String updateOrderInfo(Order newBeeMallOrder) {
Order temp = newBeeMallOrderMapper.selectByPrimaryKey(newBeeMallOrder.getOrderId());
//不为空且orderStatus>=0且状态为出库之前可以修改部分信息
if (temp != null && temp.getOrderStatus() >= 0 && temp.getOrderStatus() < 3) {
temp.setTotalPrice(newBeeMallOrder.getTotalPrice());
temp.setUserAddress(newBeeMallOrder.getUserAddress());
temp.setUpdateTime(new Date());
if (newBeeMallOrderMapper.updateByPrimaryKeySelective(temp) > 0) {
return ServiceResultEnum.SUCCESS.getResult();
}
return ServiceResultEnum.DB_ERROR.getResult();
}
return ServiceResultEnum.DATA_NOT_EXIST.getResult();
}
@Override
@Transactional
public String checkDone(Long[] ids) {
//查询所有的订单 判断状态 修改状态和更新时间
List<Order> orders = newBeeMallOrderMapper.selectByPrimaryKeys(Arrays.asList(ids));
String errorOrderNos = "";
if (!CollectionUtils.isEmpty(orders)) {
for (Order newBeeMallOrder : orders) {
if (newBeeMallOrder.getIsDeleted() == 1) {
errorOrderNos += newBeeMallOrder.getOrderNo() + " ";
continue;
}
if (newBeeMallOrder.getOrderStatus() != 1) {
errorOrderNos += newBeeMallOrder.getOrderNo() + " ";
}
}
if (StringUtils.isEmpty(errorOrderNos)) {
//订单状态正常 可以执行配货完成操作 修改订单状态和更新时间
if (newBeeMallOrderMapper.checkDone(Arrays.asList(ids)) > 0) {
return ServiceResultEnum.SUCCESS.getResult();
} else {
return ServiceResultEnum.DB_ERROR.getResult();
}
} else {
//订单此时不可执行出库操作
if (errorOrderNos.length() > 0 && errorOrderNos.length() < 100) {
return errorOrderNos + "订单的状态不是支付成功无法执行出库操作";
} else {
return "你选择了太多状态不是支付成功的订单,无法执行配货完成操作";
}
}
}
//未查询到数据 返回错误提示
return ServiceResultEnum.DATA_NOT_EXIST.getResult();
}
@Override
@Transactional
public String checkOut(Long[] ids) {
//查询所有的订单 判断状态 修改状态和更新时间
List<Order> orders = newBeeMallOrderMapper.selectByPrimaryKeys(Arrays.asList(ids));
String errorOrderNos = "";
if (!CollectionUtils.isEmpty(orders)) {
for (Order newBeeMallOrder : orders) {
if (newBeeMallOrder.getIsDeleted() == 1) {
errorOrderNos += newBeeMallOrder.getOrderNo() + " ";
continue;
}
if (newBeeMallOrder.getOrderStatus() != 1 && newBeeMallOrder.getOrderStatus() != 2) {
errorOrderNos += newBeeMallOrder.getOrderNo() + " ";
}
}
if (StringUtils.isEmpty(errorOrderNos)) {
//订单状态正常 可以执行出库操作 修改订单状态和更新时间
if (newBeeMallOrderMapper.checkOut(Arrays.asList(ids)) > 0) {
return ServiceResultEnum.SUCCESS.getResult();
} else {
return ServiceResultEnum.DB_ERROR.getResult();
}
} else {
//订单此时不可执行出库操作
if (errorOrderNos.length() > 0 && errorOrderNos.length() < 100) {
return errorOrderNos + "订单的状态不是支付成功或配货完成无法执行出库操作";
} else {
return "你选择了太多状态不是支付成功或配货完成的订单,无法执行出库操作";
}
}
}
//未查询到数据 返回错误提示
return ServiceResultEnum.DATA_NOT_EXIST.getResult();
}
@Override
@Transactional
public String closeOrder(Long[] ids) {
//查询所有的订单 判断状态 修改状态和更新时间
List<Order> orders = newBeeMallOrderMapper.selectByPrimaryKeys(Arrays.asList(ids));
String errorOrderNos = "";
if (!CollectionUtils.isEmpty(orders)) {
for (Order newBeeMallOrder : orders) {
// isDeleted=1 一定为已关闭订单
if (newBeeMallOrder.getIsDeleted() == 1) {
errorOrderNos += newBeeMallOrder.getOrderNo() + " ";
continue;
}
//已关闭或者已完成无法关闭订单
if (newBeeMallOrder.getOrderStatus() == 4 || newBeeMallOrder.getOrderStatus() < 0) {
errorOrderNos += newBeeMallOrder.getOrderNo() + " ";
}
}
if (StringUtils.isEmpty(errorOrderNos)) {
//订单状态正常 可以执行关闭操作 修改订单状态和更新时间
if (newBeeMallOrderMapper.closeOrder(Arrays.asList(ids), OrderStatusEnum.ORDER_CLOSED_BY_JUDGE.getOrderStatus()) > 0) {
return ServiceResultEnum.SUCCESS.getResult();
} else {
return ServiceResultEnum.DB_ERROR.getResult();
}
} else {
//订单此时不可执行关闭操作
if (errorOrderNos.length() > 0 && errorOrderNos.length() < 100) {
return errorOrderNos + "订单不能执行关闭操作";
} else {
return "你选择的订单不能执行关闭操作";
}
}
}
//未查询到数据 返回错误提示
return ServiceResultEnum.DATA_NOT_EXIST.getRe