订单详情接口整理

2022-02-19 00:00:00 接口 订单 整理

目标图
《订单详情接口整理》

其实很简单,根据订单的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.订单表设计
《订单详情接口整理》

《订单详情接口整理》

    原文作者:思乐
    原文地址: https://blog.csdn.net/weixin_42992035/article/details/83144282
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。

相关文章