订单管理模块前台用户包括4个模块:创建订单、取消订单、查询订单明细、查询订单List。
后台管理员模块主要有四个模块:订单List、订单detail、订单search、订单发货。
1.前台
1.创建订单流程:
1.根据userId获取购物车当中已经勾选的商品
2.根据userId,cartList,校验产品状态和库存,并组装orderItemList
3.从orderItemList计算总价
4.根据userId,shippingId,payment生成订单(组装order),此时订单的状态要设置称未支付状态,并且订单号的生成策略是当前时间+当前时间%9来计算的。
5.遍历orderItem并设置其订单号OrderNo,并批量插入orderItem
6.由orderItemList,减库存
7.清空购物车(清空所有选中的)
8.组装orderVo,返回给前端,orderVo中包括order、orderItem的信息
2.取消订单流程
1.首先查询订单是否已付款,已付款无法取消
2.创建updateOrder,更新订单的状态为已取消
3.查询订单明细
1.根据userId, orderNo查询订单order,
2.然后由userId, orderNo查询orderItemList,
3.使用order、orderItemList组装orderVo
4.查询订单List
1.根据userID查询orderList
2.由orderList,userId组装orderVoList
3.返回
2.后台
1.订单List
1.selectAll()查询orderList
2.由orderList,userId组装orderVoList,userId=null
3.返回
2.订单detail
1.由orderNo查Order
2.不为null时,由orderNo查orderItemList
3.使用order,orderItemList组装orderVo
3.订单search
在本次当中使用的根据orderNo搜索
4.订单发货
1.根据orderNo查询order
2.根据order查看订单的支付状态,若已支付,则跟新订单的状态为已发货。
3.代码
***************************Controller 前台*********************************
package com.mall.controller.portal;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.demo.trade.config.Configs;
import com.google.common.collect.Maps;
import com.mall.common.Const;
import com.mall.common.ResponseCode;
import com.mall.common.ServerResponse;
import com.mall.pojo.User;
import com.mall.service.IOrderService;
import com.mall.vo.OrderProductVo;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Iterator;
import java.util.Map;
@Controller
@RequestMapping("/order/")
public class OrderController {
private static final Logger logger= LoggerFactory.getLogger(OrderController.class);
@Autowired
private IOrderService iOrderService;
@ResponseBody
@RequestMapping("create.do")
public ServerResponse create(HttpSession session,Integer shippingId){
User user=(User) session.getAttribute(Const.CURRENT_USER);
if(user==null){
return ServerResponse.createByErrorCodeMsg(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getMsg());
}
return iOrderService.createOrder(user.getId(),shippingId);
}
@ResponseBody
@RequestMapping("detail.do")
public ServerResponse detail(HttpSession session,Long orderNo){
User user=(User) session.getAttribute(Const.CURRENT_USER);
if(user==null){
return ServerResponse.createByErrorCodeMsg(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getMsg());
}
return iOrderService.detailOrder(user.getId(),orderNo);
}
@ResponseBody
@RequestMapping("list.do")
public ServerResponse list(HttpSession session, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,@RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
User user=(User) session.getAttribute(Const.CURRENT_USER);
if(user==null){
return ServerResponse.createByErrorCodeMsg(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getMsg());
}
return iOrderService.listOrder(user.getId(),pageNum,pageSize);
}
@ResponseBody
@RequestMapping("cancel.do")
public ServerResponse cancel(HttpSession session,Long orderNo){
User user=(User) session.getAttribute(Const.CURRENT_USER);
if(user==null){
return ServerResponse.createByErrorCodeMsg(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getMsg());
}
return iOrderService.cancelOrder(user.getId(),orderNo);
}
//获取购物车当中已经选中的商品
@ResponseBody
@RequestMapping("get_order_cart_product.do")
public ServerResponse getOrderCartProduct(HttpSession session){
User user=(User) session.getAttribute(Const.CURRENT_USER);
if(user==null){
return ServerResponse.createByErrorCodeMsg(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getMsg());
}
return iOrderService.getOrderCartProduct(user.getId());
}
}
***************************Controller 后台*********************************
package com.mall.controller.backend;
import com.github.pagehelper.PageInfo;
import com.mall.common.Const;
import com.mall.common.ResponseCode;
import com.mall.common.ServerResponse;
import com.mall.pojo.User;
import com.mall.service.IOrderService;
import com.mall.service.IUserService;
import com.mall.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
@Controller
@RequestMapping("/manage/order/")
public class OrderManageController {
@Autowired
private IUserService iUserService;
@Autowired
private IOrderService iOrderService;
@ResponseBody
@RequestMapping("list.do")
public ServerResponse list(HttpSession session, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
User user=(User)session.getAttribute(Const.CURRENT_USER);
if(user == null){
return ServerResponse.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getMsg());
}
ServerResponse response=iUserService.checkUserRole(user);
if(response.isSuccess()){
return iOrderService.managerList(pageNum, pageSize);
}
return response;
}
@ResponseBody
@RequestMapping("detail.do")
public ServerResponse detail(HttpSession session, Long orderNo){
User user=(User)session.getAttribute(Const.CURRENT_USER);
if(user == null){
return ServerResponse.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getMsg());
}
ServerResponse response=iUserService.checkUserRole(user);
if(response.isSuccess()){
return iOrderService.managerDetail(orderNo);
}
return response;
}
@ResponseBody
@RequestMapping("search.do")
public ServerResponse search(HttpSession session, Long orderNo, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
User user=(User)session.getAttribute(Const.CURRENT_USER);
if(user == null){
return ServerResponse.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getMsg());
}
ServerResponse response=iUserService.checkUserRole(user);
if(response.isSuccess()){
return iOrderService.managerSearch(orderNo,pageNum,pageSize);
}
return response;
}
@ResponseBody
@RequestMapping("send_goods.do")
public ServerResponse sendGoods(HttpSession session, Long orderNo){
User user=(User)session.getAttribute(Const.CURRENT_USER);
if(user == null){
return ServerResponse.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getMsg());
}
ServerResponse response=iUserService.checkUserRole(user);
if(response.isSuccess()){
return iOrderService.managerSendGoods(orderNo);
}
return response;
}
}
***************************Service*****************************************
package com.mall.service.impl;
import com.alipay.api.AlipayResponse;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.demo.trade.config.Configs;
import com.alipay.demo.trade.model.ExtendParams;
import com.alipay.demo.trade.model.GoodsDetail;
import com.alipay.demo.trade.model.builder.AlipayTradePrecreateRequestBuilder;
import com.alipay.demo.trade.model.result.AlipayF2FPrecreateResult;
import com.alipay.demo.trade.service.AlipayTradeService;
import com.alipay.demo.trade.service.impl.AlipayTradeServiceImpl;
import com.alipay.demo.trade.utils.ZxingUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mall.common.Const;
import com.mall.common.ServerResponse;
import com.mall.dao.*;
import com.mall.pojo.*;
import com.mall.service.IOrderService;
import com.mall.util.BigDecimalUtil;
import com.mall.util.DateTimeUtils;
import com.mall.util.FTPUtil;
import com.mall.util.PropertiesUtil;
import com.mall.vo.OrderItemVo;
import com.mall.vo.OrderProductVo;
import com.mall.vo.OrderVo;
import com.mall.vo.ShippingVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.aspectj.weaver.ast.Or;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.File;
import java.math.BigDecimal;
import java.util.*;
@Service("iOrderService")
public class OrderServiceImpl implements IOrderService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private OrderItemMapper orderItemMapper;
@Autowired
private PayInfoMapper payInfoMapper;
@Autowired
private CartMapper cartMapper;
@Autowired
private ProductMapper productMapper;
@Autowired
private ShippingMapper shippingMapper;
private static final Logger log= LoggerFactory.getLogger(OrderServiceImpl.class);
public ServerResponse createOrder(Integer userId,Integer shippingId){
//从购物车当中获取数据(已勾选的商品)
List cartList=cartMapper.selectChckedCartByUserId(userId);
//校验产品状态和库存
ServerResponse> response=this.getCartOrderItem(userId,cartList);
if(!response.isSuccess()){
return response;
}
//从orderItemList计算总价
List orderItemList=response.getData();
BigDecimal payment=this.getOrderTotalPrice(orderItemList);
//生成订单
Order order=this.assembleOrder(userId,shippingId,payment);
if(order==null){
return ServerResponse.createByErrorMsg("生成订单错误");
}
//遍历orderItem并设置其订单号OrderNo
for (OrderItem orderItem:orderItemList){
orderItem.setOrderNo(order.getOrderNo());
}
//mybatis批量插入orderItem
orderItemMapper.batchInsert(orderItemList);
//生成订单成功,减库存
this.reduceProductStock(orderItemList);
//清空购物车
this.clearCart(cartList);
//返回给前端数据,返回的数据当中包括order、orderItem的信息
OrderVo orderVo=this.assembleOrderVo(order,orderItemList);
return ServerResponse.createBySuccess(orderVo);
}
public ServerResponse detailOrder(Integer userId,Long orderNo){
Order order=orderMapper.selectOrderByUserIdOrderNo(userId, orderNo);
if(order !=null){
List orderItemList=orderItemMapper.getOrderItemByOrderNoUserId(orderNo, userId);
OrderVo orderVo=this.assembleOrderVo(order,orderItemList);
return ServerResponse.createBySuccess(orderVo);
}
return ServerResponse.createByErrorMsg("该用户不存在该订单");
}
public ServerResponse listOrder(Integer userId,int pageNum,int pageSize){
PageHelper.startPage(pageNum, pageSize);
List orderList=orderMapper.selectOrderByUserId(userId);
List orderVoList=this.assembleOrderVoList(orderList,userId);
PageInfo pageInfo=new PageInfo(orderList);
pageInfo.setList(orderVoList);
return ServerResponse.createBySuccess(pageInfo);
}
public ServerResponse cancelOrder(Integer userId,Long orderNo){
Order order=orderMapper.selectOrderByUserIdOrderNo(userId, orderNo);
if(order == null){
return ServerResponse.createByErrorMsg("该用户此订单不存在");
}
if(order.getStatus() != Const.OrderStatusEnum.NP_PAY.getCode()){
return ServerResponse.createByErrorMsg("已付款,无法取消订单");
}
Order updateOrder = new Order();
updateOrder.setId(order.getId());
updateOrder.setStatus(Const.OrderStatusEnum.CANCELED.getCode());
int rowCount=orderMapper.updateByPrimaryKey(updateOrder);
if(rowCount > 0){
return ServerResponse.createBySuccess();
}
return ServerResponse.createByError();
}
public ServerResponse getOrderCartProduct(Integer userId){
OrderProductVo orderProductVo=new OrderProductVo();
//从购物车当中获取数据
List cartList=cartMapper.selectChckedCartByUserId(userId);
//校验库存
ServerResponse response=this.getCartOrderItem(userId,cartList);
if(!response.isSuccess()){
return response;
}
List orderItemList=(List) response.getData();
List orderProductVoList=Lists.newArrayList();
//计算总价
/*BigDecimal payment=new BigDecimal("0");
for (OrderItem orderItem:orderItemList){
payment=BigDecimalUtil.add(payment.doubleValue(),orderItem.getTotalPrice().doubleValue());
}*/
BigDecimal payment=this.getOrderTotalPrice(orderItemList);
orderProductVo.setProductTotalPrice(payment);
orderProductVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix"));
return ServerResponse.createBySuccess(orderProductVo);
}
private ServerResponse> getCartOrderItem(Integer userId,List cartList){
List orderItemList=Lists.newArrayList();
if(CollectionUtils.isEmpty(cartList)){
return ServerResponse.createByErrorMsg("购物车为空");
}
//校验购物车当中的数据,包括产品的状态和数量(库存)
for (Cart cartItem:cartList){
OrderItem orderItem=new OrderItem();
Product product=productMapper.selectByPrimaryKey(cartItem.getProductId());
if(product != null){
//校验产品的状态
if(Const.ProductStatusEnum.ON_SALE.getCode() != product.getStatus()){
return ServerResponse.createByErrorMsg("产品:"+product.getName()+"产品不是在线售卖状态");
}
//校验商品的数量
if(cartItem.getQuantity() > product.getStock()){
return ServerResponse.createByErrorMsg("产品:"+product.getName()+"库存不足");
}
//组装OrderItem
orderItem.setUserId(userId);
orderItem.setProductId(product.getId());
orderItem.setProductName(product.getName());
orderItem.setProductImage(product.getMainImage());
orderItem.setCurrentUnitPrice(product.getPrice());
orderItem.setQuantity(cartItem.getQuantity());
orderItem.setTotalPrice(BigDecimalUtil.mul(orderItem.getCurrentUnitPrice().doubleValue(),orderItem.getQuantity()));
orderItemList.add(orderItem);
}
}
return ServerResponse.createBySuccess(orderItemList);
}
private BigDecimal getOrderTotalPrice(List orderItemList){
BigDecimal payment=new BigDecimal("0");
for(OrderItem orderItem:orderItemList){
payment=BigDecimalUtil.add(payment.doubleValue(),orderItem.getTotalPrice().doubleValue());
}
return payment;
}
private Order assembleOrder(Integer userId,Integer shippingId,BigDecimal payment){
Order order=new Order();
long orderNo=this.generatorOrderNo();
order.setOrderNo(orderNo);
order.setStatus(Const.OrderStatusEnum.NP_PAY.getCode());
order.setPostage(0);
order.setUserId(userId);
order.setPaymentType(Const.PaymentTypeEnum.ONLINE_PAY.getCode());
order.setPayment(payment);
order.setShippingId(shippingId);
int rowCount=orderMapper.insert(order);
if(rowCount>0){
return order;
}
return null;
}
private Long generatorOrderNo(){
long currentTime=System.currentTimeMillis();
return currentTime+new Random().nextInt(1000);
}
private void reduceProductStock(List orderItemList){
for(OrderItem orderItem:orderItemList){
Product product=productMapper.selectByPrimaryKey(orderItem.getProductId());
product.setStock(product.getStock()-orderItem.getQuantity());
int rowCount=productMapper.updateByPrimaryKey(product);
}
}
private void clearCart(List cartList){
for (Cart cartItem:cartList){
cartMapper.deleteByPrimaryKey(cartItem.getId());
}
}
private OrderVo assembleOrderVo(Order order, List orderItemList){
OrderVo orderVo=new OrderVo();
orderVo.setOrderNo(order.getOrderNo());
orderVo.setPayment(order.getPayment());
orderVo.setPaymentType(order.getPaymentType());
orderVo.setPaymentTypeDesc(Const.PaymentTypeEnum.codeOf(order.getPaymentType()).getDesc());
Shipping shipping=shippingMapper.selectByPrimaryKey(order.getShippingId());
if(shipping !=null){
orderVo.setReceiverName(shipping.getReceiverName());
orderVo.setShippingVo(this.assembleShippingVo(shipping));
}
orderVo.setPaymentTime(DateTimeUtils.dateToStr(order.getPaymentTime()));
orderVo.setCreateTime(DateTimeUtils.dateToStr(order.getCreateTime()));
orderVo.setCloseTime(DateTimeUtils.dateToStr(order.getCloseTime()));
orderVo.setSendTime(DateTimeUtils.dateToStr(order.getSendTime()));
orderVo.setEndTime(DateTimeUtils.dateToStr(order.getEndTime()));
orderVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix"));
List orderItemVoList=Lists.newArrayList();
for (OrderItem orderItem:orderItemList){
OrderItemVo orderItemVo=this.assembleOrderItemVo(orderItem);
orderItemVoList.add(orderItemVo);
}
orderVo.setOrderItemVoList(orderItemVoList);
return orderVo;
}
private ShippingVo assembleShippingVo(Shipping shipping){
ShippingVo shippingVo=new ShippingVo();
shippingVo.setReceiverName(shipping.getReceiverName());
shippingVo.setReceiverAddress(shipping.getReceiverAddress());
shippingVo.setReceiverCity(shipping.getReceiverCity());
shippingVo.setReceiverDistrict(shipping.getReceiverDistrict());
shippingVo.setReceiverMobile(shipping.getReceiverMobile());
shippingVo.setReceiverProvince(shipping.getReceiverProvince());
shippingVo.setReceiverPhone(shipping.getReceiverPhone());
shippingVo.setReceiverZip(shipping.getReceiverZip());
return shippingVo;
}
private OrderItemVo assembleOrderItemVo(OrderItem orderItem){
OrderItemVo orderItemVo=new OrderItemVo();
orderItemVo.setOrderNo(orderItem.getOrderNo());
orderItemVo.setProductId(orderItem.getProductId());
orderItemVo.setProductName(orderItem.getProductName());
orderItemVo.setProductImage(orderItem.getProductImage());
orderItemVo.setCurrentUnitPrice(orderItem.getCurrentUnitPrice());
orderItemVo.setQuantity(orderItem.getQuantity());
orderItemVo.setTotalPrice(orderItem.getTotalPrice());
orderItemVo.setCreateTime(DateTimeUtils.dateToStr(orderItem.getCreateTime()));
return orderItemVo;
}
private List assembleOrderVoList(List orderList,Integer userId){
List orderVoList=Lists.newArrayList();
for(Order order:orderList){
List orderItemList=Lists.newArrayList();
if(userId==null ){
//todo 管理员查询,无需传userId
orderItemList=orderItemMapper.getOrderItemByOrderNo(order.getOrderNo());
}else {
orderItemList=orderItemMapper.getOrderItemByOrderNoUserId(order.getOrderNo(),order.getUserId());
}
OrderVo orderVo=this.assembleOrderVo(order,orderItemList);
orderVoList.add(orderVo);
}
return orderVoList;
}
//backend
public ServerResponse managerList(int pageNum,int pageSize){
PageHelper.startPage(pageNum, pageSize);
List orderList=orderMapper.selectAll();
List orderVoList=this.assembleOrderVoList(orderList,null);
PageInfo pageInfo=new PageInfo(orderList);
pageInfo.setList(orderVoList);
return ServerResponse.createBySuccess(pageInfo);
}
public ServerResponse managerDetail(Long orderNo){
Order order=orderMapper.selectOrderByOrderNo(orderNo);
if(order != null){
List orderItemList=orderItemMapper.getOrderItemByOrderNo(orderNo);
OrderVo orderVo=this.assembleOrderVo(order,orderItemList);
return ServerResponse.createBySuccess(orderVo);
}
return ServerResponse.createByErrorMsg("订单不存在");
}
public ServerResponse managerSearch(Long orderNo,int pageNum,int pageSize){
PageHelper.startPage(pageNum, pageSize);
Order order=orderMapper.selectOrderByOrderNo(orderNo);
if(order != null){
List orderItemList=orderItemMapper.getOrderItemByOrderNo(orderNo);
OrderVo orderVo=this.assembleOrderVo(order,orderItemList);
PageInfo pageInfo=new PageInfo(Lists.newArrayList(order));
pageInfo.setList(Lists.newArrayList(orderVo));
return ServerResponse.createBySuccess(pageInfo);
}
return ServerResponse.createByErrorMsg("订单不存在");
}
public ServerResponse managerSendGoods(Long orderNo){
Order order=orderMapper.selectOrderByOrderNo(orderNo);
if(order != null){
if(order.getStatus() == Const.OrderStatusEnum.PAID.getCode()){
order.setStatus(Const.OrderStatusEnum.SHIPPING.getCode());
order.setSendTime(new Date());
orderMapper.updateByPrimaryKey(order);
return ServerResponse.createBySuccessMsg("发货成功");
}
}
return ServerResponse.createByErrorMsg("订单不存在");
}
}
*******************mybatis批量插入,id是自增的***********************
insert into mmall_order_item (id,order_no,user_id, product_id,
product_name, product_image, current_unit_price,
quantity, total_price, create_time,
update_time)
values
(
#{item.id},#{item.orderNo},#{item.userId},#{item.productId},
#{item.productName},#{item.productImage},#{item.currentUnitPrice},
#{item.quantity},#{item.totalPrice},now(),
now()
)