1. 程式人生 > >訂單詳情介面整理

訂單詳情介面整理

目標圖 在這裡插入圖片描述

其實很簡單,根據訂單的id查詢資料庫資訊就行了 1.介面層

 @ApiOperation(value = "會員訂單詳情介面", notes = "會員訂單詳情業務", produces = "application/json")
    @PostMapping("/logisticsOrder")
    public @ResponseBody BaseResponse<Map<String, Object>> orderList(@RequestHeader(name = "Authorization", defaultValue = "token") String token,
                                                                     @RequestBody MemberOrderDetailRequest memberOrderDetailRequest) {
        _logger.info("會員訂單詳情介面");
        Map<String, Object> dataMap=new HashMap<>();
        String errorMsg = "";
        try {
            // 查詢訂單列表
            ServiceResult<Orders> serviceResult = ordersService.getOrderWithOPById(memberOrderDetailRequest.getId());
            // 查詢訂單操作的日誌
            ServiceResult<List<OrderLog>> orderLogResult = orderLogService.getOrderLogByOrderId(memberOrderDetailRequest.getId());

            Orders orders = serviceResult.getResult();
            Integer userid = memberOrderDetailRequest.getMemberId();
            if (orders == null) {
                dataMap.put("info", "訂單不存在");
                return new BaseResponse<>(false,dataMap,"訂單不存在");
            } else if (userid.intValue() != orders.getMemberId().intValue()) {
                dataMap.put("info", "您無權檢視他人資訊");
                return  new BaseResponse<>(false,dataMap,"您無權檢視他人資訊");
            }
            // 計算是否顯示退貨和換貨按鈕
            Date createDate = orders.getCreateTime();
            long createTime = 0;
            if (createDate != null) {
                createTime = createDate.getTime();
            }
            long newTime = new Date().getTime();
            //超過十五天就不能退換貨了
            if (newTime - createTime < 15 * 24 * 60 * 60 * 1000) {
                orders.setIsShowBackAndExchange(true);
            } else {
                orders.setIsShowBackAndExchange(false);
            }

            List<OrderLog> logList = orderLogResult.getResult();
            if (orders != null && orders.getLogisticsId() > 0) {
                // 快遞100查詢物流資訊
                ServiceResult<CourierCompany> courierResult = courierCompanyService
                    .getCourierCompanyById(orders.getLogisticsId());
                
                //快遞公司資訊
                CourierCompany courierCompany = courierResult.getResult();
                if (courierCompany != null) {
                    String url = "http://api.kuaidi100.com/api?id=" + EjavashopConfig.KUAIDI100_KEY;
                    url += "&com=" + courierCompany.getCompanyMark();
                    url += "&nu=" + orders.getLogisticsNumber();
                    url += "&show=0";
                    url += "&muti=1";
                    url += "&order=asc";

                    String sendGet = HttpClientUtil.sendGet(url);

                    Map<String, Object> fromJson = JsonUtil.fromJson(sendGet);
                    Object status = null;
                    if (fromJson != null) {
                        status = fromJson.get("status");
                    }
                    // 查詢結果狀態: 0:物流單暫無結果, 1:查詢成功, 2:接口出現異常
                    if (status != null && "1".equals(status.toString())) {
                        List<Map<String, String>> list =  (List<Map<String, String>>) fromJson.get("data");
                       
                        for (Map<String, String> map : list) {
                            OrderLog orderLog = new OrderLog();
                            SimpleDateFormat simpleDateFormat = new java.text.SimpleDateFormat(
                                "yyyy-MM-dd HH:mm:ss");
                            try {
                                orderLog.setCreateTime(simpleDateFormat.parse(map.get("time")));
                                orderLog.setContent(map.get("context"));
                                orderLog.setOperatingName(courierCompany.getCompanyName());
                            } catch (ParseException e) {
                                _logger.error(e.getMessage(), e);
                            }
                            //把快遞進度資訊傳給前端
                            logList.add(orderLog);
                        }
                    } else {
                        _logger.error("物流資訊查詢錯誤:status=" + status.toString());
                        _logger.error("物流資訊查詢錯誤:message=" + fromJson.get("message"));
                        _logger.error("物流公司:" + courierCompany.getCompanyName());
                        _logger.error("物流單號:" + orders.getLogisticsNumber());
                    }

                    Collections.sort(logList, new Comparator<OrderLog>() {
                        public int compare(OrderLog arg0, OrderLog arg1) {
                            return arg0.getCreateTime().compareTo(arg1.getCreateTime());
                        }
                    });
                }
            }

            dataMap.put("訂單快遞操作日誌", logList);
            dataMap.put("訂單、網單及產品圖片資訊", orders);
        } catch (Exception e) {
            if (e instanceof BusinessException)
                errorMsg = e.getMessage();
            else
                e.printStackTrace();
        }

        dataMap.put("errorMsg", errorMsg);
        return new BaseResponse<>(true,dataMap,null);
    }

然後我們來看看業務層getOrderWithOPById的方法,介面定義的方法

 ServiceResult<Orders> getOrderWithOPById(Integer orderId);

3.介面實現類

    @Override
    public ServiceResult<Orders> getOrderWithOPById(Integer orderId) {
        ServiceResult<Orders> serviceResult = new ServiceResult<Orders>();
        try {
            serviceResult.setResult(ordersModel.getOrderWithOPById(orderId));
        } catch (BusinessException be) {
            serviceResult.setSuccess(false);
            serviceResult.setMessage(be.getMessage());
            log.error(
                "[OrderService][getOrderWithOPById]根據訂單id取訂單、網單及產品圖片資訊時發生異常:" + be.getMessage());
        } catch (Exception e) {
            serviceResult.setError(ConstantsEJS.SERVICE_RESULT_CODE_SYSERROR, "服務異常,請聯絡系統管理員。");
            log.error("[OrderService][getOrderWithOPById]根據訂單id 取訂單、網單及產品圖片資訊時發生異常:", e);
        }
        return serviceResult;
    }

4.model層

 public Orders getOrderWithOPById(Integer orderId) {
        Orders orders = ordersWriteDao.get(orderId);
        if (orders == null) {
            throw new BusinessException("訂單資訊獲取失敗,請重試!");
        }

        //根據訂單id查詢網單
        List<OrdersProduct> orderProductList = null;
//.IS_PARENT_1表示是總訂單,0表示不是總訂單,如果是總訂單就用訂單號來查網單,否則用id查
        if (orders.getIsParent().intValue() == Orders.IS_PARENT_1) {
            orderProductList = ordersProductWriteDao.getByOrdersPsn(orders.getOrderSn());
        } else {
            orderProductList = ordersProductWriteDao.getByOrderId(orders.getId());
        }

        if (orderProductList.size() == 0) {
            throw new BusinessException("網單資訊獲取失敗,請聯絡管理員!");
        }
        //根據產品id查小圖路徑,為什麼要遍歷呢。。---因為你購買的商品不止一個吖有很多個,比如你買了電腦A,價格100元,電腦
        //B,價格150元。那個就有兩個網單表A,B。訂單表的價格就是A+B的價格250
        for (OrdersProduct op : orderProductList) {
            String productLeadLittle = frontProductPictureUtil
                .getproductLeadLittle(op.getProductId());
            op.setProductLeadLittle(productLeadLittle);
        }
        //把網單表的資料儲存進orders,傳給前端
        orders.setOrderProductList(orderProductList);

        return orders;
    }

5.一部分網單表資料庫設計:下面還有是否退換貨建立時間等。。

6.訂單表設計 在這裡插入圖片描述

在這裡插入圖片描述