• 暂时停更一段时间!
  • 近期网站将陆续进行前端页面改造!
  • 招募网站编辑,联系站长!

从零到企业级SSM电商项目实战教程(四十二)前后台订单列表,订单详情,发货功能开发

文章目录[隐藏]

OrderController.java

@RequestMapping("detail.do")
@ResponseBody
public ServerResponse detail(HttpSession session, Long orderNo) {
    User user = (User) session.getAttribute(Const.CURRENT_USER);
    if (user == null) {
        return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
    }

    return iOrderService.getOrderDetail(user.getId(), orderNo);
}

@RequestMapping("list.do")
@ResponseBody
public ServerResponse list(HttpSession session, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "20") int pageSize) {
    User user = (User) session.getAttribute(Const.CURRENT_USER);
    if (user == null) {
        return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
    }

    return iOrderService.getOrderList(user.getId(), pageNum, pageSize);
}

IOrderService.java

ServerResponse<OrderVo> getOrderDetail(Integer userId, Long orderNo);

ServerResponse<PageInfo> getOrderList(Integer userId, int pageNum, int pageSize);

//backend
ServerResponse<PageInfo> manageList(int pageNum, int pageSize);

ServerResponse<OrderVo> manageDetail(Long orderNo);

ServerResponse<PageInfo> manageSearch(Long orderNo, int pageNum, int pageSize);

ServerResponse<String> manageSendGoods(Long orderNo);

OrderServiceImpl.java

public ServerResponse<OrderVo> getOrderDetail(Integer userId, Long orderNo) {
    Order order = orderMapper.selectByUserIdAndOrderNo(userId, orderNo);
    if (order != null) {
        List<OrderItem> orderItemList = orderItemMapper.getByOrderNoUserId(orderNo, userId);
        OrderVo orderVo = assembleOrderVo(order, orderItemList);
        return ServerResponse.createBySuccess(orderVo);
    }
    return ServerResponse.createByErrorMessage("没有找到该订单");

}

public ServerResponse<PageInfo> getOrderList(Integer userId, int pageNum, int pageSize) {
    PageHelper.startPage(pageNum, pageSize);
    List<Order> orderList = orderMapper.selectByUserId(userId);

    List<OrderVo> orderVoList = assembleOrderVoList(orderList, userId);
    PageInfo pageResult = new PageInfo(orderList);
    pageResult.setList(orderVoList);
    return ServerResponse.createBySuccess(pageResult);
}

private List<OrderVo> assembleOrderVoList(List<Order> orderList, Integer userId) {
    List<OrderVo> orderVoList = Lists.newArrayList();
    for (Order order : orderList) {
        List<OrderItem> orderItemList = Lists.newArrayList();
        if (userId == null) {
            //todo 管理员查询时不需要传参 userId
            orderItemList = orderItemMapper.getByOrderNo(order.getOrderNo());
        } else {
            orderItemList = orderItemMapper.getByOrderNoUserId(order.getOrderNo(), userId);
        }
        OrderVo orderVo = assembleOrderVo(order, orderItemList);
        orderVoList.add(orderVo);
    }
    return orderVoList;
}

//backend
public ServerResponse<PageInfo> manageList(int pageNum, int pageSize) {
    PageHelper.startPage(pageNum, pageSize);

    List<Order> orderList = orderMapper.selectAllOrder();
    List<OrderVo> orderVoList = this.assembleOrderVoList(orderList, null);
    PageInfo pageResult = new PageInfo(orderList);
    pageResult.setList(orderVoList);
    return ServerResponse.createBySuccess(pageResult);
}

public ServerResponse<OrderVo> manageDetail(Long orderNo) {
    Order order = orderMapper.selectByOrderNo(orderNo);
    if (order != null) {
        List<OrderItem> orderItemList = orderItemMapper.getByOrderNo(orderNo);
        OrderVo orderVo = assembleOrderVo(order, orderItemList);
        return ServerResponse.createBySuccess(orderVo);
    }
    return ServerResponse.createByErrorMessage("订单不存在");

}

public ServerResponse<PageInfo> manageSearch(Long orderNo, int pageNum, int pageSize) {
    PageHelper.startPage(pageNum, pageSize);
    Order order = orderMapper.selectByOrderNo(orderNo);
    if (order != null) {
        List<OrderItem> orderItemList = orderItemMapper.getByOrderNo(orderNo);
        OrderVo orderVo = assembleOrderVo(order, orderItemList);

        PageInfo pageResult = new PageInfo(Lists.newArrayList(order));
        pageResult.setList(Lists.newArrayList(orderVo));
        return ServerResponse.createBySuccess(pageResult);
    }
    return ServerResponse.createByErrorMessage("订单不存在!");
}

public ServerResponse<String> manageSendGoods(Long orderNo) {
    Order order = orderMapper.selectByOrderNo(orderNo);
    if (order != null) {
        if (order.getStatus() == Const.OrderStatusEnum.PAID.getCode()) {
            order.setStatus(Const.OrderStatusEnum.SHIPPED.getCode());
            order.setSendTime(new Date());
            orderMapper.updateByPrimaryKeySelective(order);
            return ServerResponse.createBySuccess("发货成功");
        }
    }
    return ServerResponse.createByErrorMessage("订单不存在");
}

OrderMapper.java

List<Order> selectByUserId(Integer userId);

List<Order> selectAllOrder();

OrderMapper.xml

<select id="selectByUserId" resultMap="BaseResultMap" parameterType="int">
  SELECT
  <include refid="Base_Column_List"/>
  from mmall_order
  where user_id = #{userId}
  order by create_time desc
</select>

<select id="selectAllOrder" resultMap="BaseResultMap">
  SELECT
  <include refid="Base_Column_List"/>
  from mmall_order
  ORDER BY  create_time desc
</select>

OrderItemMapper.java

List<OrderItem> getByOrderNo(@Param("orderNo")Long orderNo);

OrderItemMapper.xml

<select id="getByOrderNo" parameterType="map" resultMap="BaseResultMap">
  SELECT
  <include refid="Base_Column_List"/>
  from mmall_order_item
  where order_no = #{orderNo}
</select>

OrderManageController.java

controller 包下的 backedn 包下创建 OrderManageController.java

package com.mmall.controller.backend;

import com.github.pagehelper.PageInfo;
import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.IOrderService;
import com.mmall.service.IUserService;
import com.mmall.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @Author: Geekerstar(jikewenku.com)
 * @Date: 2018/6/25 13:08
 * @Description:
 */
@RestController
@RequestMapping("/manage/order")
public class OrderManageController {


    @Autowired
    private IUserService iUserService;
    @Autowired
    private IOrderService iOrderService;

    @RequestMapping("list.do")
    @ResponseBody
    public ServerResponse<PageInfo> orderList(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.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录管理员");

        }
        if(iUserService.checkAdminRole(user).isSuccess()){
            //填充我们增加产品的业务逻辑
            return iOrderService.manageList(pageNum,pageSize);
        }else{
            return ServerResponse.createByErrorMessage("无权限操作");
        }
    }

    @RequestMapping("detail.do")
    @ResponseBody
    public ServerResponse<OrderVo> orderDetail(HttpSession session, Long orderNo){

        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录管理员");

        }
        if(iUserService.checkAdminRole(user).isSuccess()){
            //填充我们增加产品的业务逻辑

            return iOrderService.manageDetail(orderNo);
        }else{
            return ServerResponse.createByErrorMessage("无权限操作");
        }
    }



    @RequestMapping("search.do")
    @ResponseBody
    public ServerResponse<PageInfo> orderSearch(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.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录管理员");

        }
        if(iUserService.checkAdminRole(user).isSuccess()){
            //填充我们增加产品的业务逻辑
            return iOrderService.manageSearch(orderNo,pageNum,pageSize);
        }else{
            return ServerResponse.createByErrorMessage("无权限操作");
        }
    }



    @RequestMapping("send_goods.do")
    @ResponseBody
    public ServerResponse<String> orderSendGoods(HttpSession session, Long orderNo){

        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录管理员");

        }
        if(iUserService.checkAdminRole(user).isSuccess()){
            //填充我们增加产品的业务逻辑
            return iOrderService.manageSendGoods(orderNo);
        }else{
            return ServerResponse.createByErrorMessage("无权限操作");
        }
    }

}

喜欢 (0)
[247507792@qq.com]
分享 (0)

邀请您免费 注册账号 登录 即可参与讨论!