Order.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\service;
  13. use app\api\model\Order as OrderModel;
  14. use app\api\model\order\Delivery as DeliveryModel;
  15. use app\api\model\OrderAddress as OrderAddressModel;
  16. use app\common\service\Order as OrderService;
  17. use app\common\service\Express as ExpressService;
  18. use app\common\enum\order\OrderStatus as OrderStatusEnum;
  19. use cores\exception\BaseException;
  20. /**
  21. * 订单服务类
  22. * Class Order
  23. * @package app\common\service
  24. */
  25. class Order extends OrderService
  26. {
  27. /**
  28. * 获取物流信息
  29. * @param int $orderId 订单ID
  30. * @return mixed
  31. * @throws BaseException
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\DbException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. */
  36. public function express(int $orderId)
  37. {
  38. // 获取发货单列表
  39. $model = new DeliveryModel;
  40. $list = $model->getList($orderId);
  41. // 整理物流跟踪信息
  42. return $this->getExpressTraces($orderId, $list);
  43. }
  44. /**
  45. * 整理物流跟踪信息
  46. * @param int $orderId 订单ID
  47. * @param $list
  48. * @return mixed
  49. * @throws BaseException
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. */
  54. private function getExpressTraces(int $orderId, $list)
  55. {
  56. // 订单收货地址
  57. $address = OrderAddressModel::detail(['order_id' => $orderId]);
  58. // 整理物流跟踪信息
  59. $Express = new ExpressService;
  60. foreach ($list as $item) {
  61. if (!empty($item['express'])) {
  62. $item['traces'] = $Express->traces(
  63. $item['express'],
  64. $item['express_no'],
  65. $address,
  66. $this->getStoreId()
  67. );
  68. }
  69. }
  70. return $list;
  71. }
  72. /**
  73. * 获取某商品的购买件数
  74. * @param int $userId 用户ID
  75. * @param int $goodsId 商品ID
  76. * @param int $orderSource
  77. * @return int
  78. */
  79. public static function getGoodsBuyNum(int $userId, int $goodsId, int $orderSource): int
  80. {
  81. return (int) (new OrderModel)->setBaseQuery('order', [['order_goods', 'order_id']])
  82. ->where('order_goods.user_id', '=', $userId)
  83. ->where('order_goods.goods_id', '=', $goodsId)
  84. ->where('order.order_source', '=', $orderSource)
  85. ->where('order.order_status', '<>', OrderStatusEnum::CANCELLED)
  86. ->where('order.is_delete', '=', 0)
  87. ->sum('order_goods.total_num');
  88. }
  89. }