ProductExpressService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace addons\TinyShop\services\order;
  3. use Yii;
  4. use yii\helpers\Json;
  5. use common\components\Service;
  6. use common\enums\StatusEnum;
  7. use common\helpers\StringHelper;
  8. use addons\TinyShop\common\models\order\ProductExpress;
  9. use addons\TinyShop\common\enums\ProductExpressShippingTypeEnum;
  10. /**
  11. * Class ProductExpressService
  12. * @package addons\TinyShop\services\order
  13. * @author jianyan74 <751393839@qq.com>
  14. */
  15. class ProductExpressService extends Service
  16. {
  17. /**
  18. * 获取物流追踪状态
  19. *
  20. * @param $order_id
  21. * @return array
  22. */
  23. public function getStatusByOrderId($order_id, $member_id)
  24. {
  25. $data = $this->findByOrderIdAndMemberId($order_id, $member_id);
  26. $order = Yii::$app->tinyShopService->order->findById($order_id);
  27. $address = [
  28. 'receiver_realname' => $order['receiver_realname'],
  29. 'receiver_name' => $order['receiver_name'],
  30. 'receiver_mobile' => StringHelper::hideStr($order['receiver_mobile'], 3),
  31. 'receiver_details' => $order['receiver_details'],
  32. 'receiver_zip' => $order['receiver_zip'],
  33. 'receiver_longitude' => $order['receiver_longitude'],
  34. 'receiver_latitude' => $order['receiver_latitude'],
  35. ];
  36. foreach ($data as &$record) {
  37. !is_array($record['order_product_ids']) && $record['order_product_ids'] = Json::decode($record['order_product_ids']);
  38. $record['address'] = $address;
  39. $record['order_product'] = Yii::$app->tinyShopService->orderProduct->findByIds($record['order_product_ids']);
  40. //物流追踪
  41. $record['trace'] = [];
  42. // 需要物流
  43. if ($record['shipping_type'] == ProductExpressShippingTypeEnum::LOGISTICS) {
  44. $record['trace'] = Yii::$app->services->extendLogistics->query($record['express_no'], $record['express_company'], $record['buyer_mobile'], true);
  45. }
  46. }
  47. return [
  48. 'count' => count($data),
  49. 'data' => $data,
  50. ];
  51. }
  52. /**
  53. * 重置订单商品获取快递号
  54. *
  55. * @param $product
  56. * @param $order_id
  57. * @return mixed
  58. */
  59. public function regroupProduct($product, $order_id)
  60. {
  61. $list = $this->findByOrderId($order_id);
  62. foreach ($product as &$item) {
  63. $item['express'] = '';
  64. foreach ($list as $record) {
  65. if (in_array($item['id'], $record['order_product_ids'])) {
  66. $item['express'] = $record['express_company'] . ' | ' . $record['express_no'];
  67. if ($record['shipping_type'] == ProductExpressShippingTypeEnum::NOT_LOGISTICS) {
  68. $item['express'] = '无需物流';
  69. }
  70. }
  71. }
  72. }
  73. return $product;
  74. }
  75. /**
  76. * @param $order_id
  77. * @param $buyer_id
  78. * @return array|\yii\db\ActiveRecord[]
  79. */
  80. public function findByOrderIdAndMemberId($order_id, $buyer_id)
  81. {
  82. return ProductExpress::find()
  83. ->where([
  84. 'order_id' => $order_id,
  85. 'buyer_id' => $buyer_id,
  86. 'status' => StatusEnum::ENABLED
  87. ])
  88. ->asArray()
  89. ->all();
  90. }
  91. /**
  92. * @param $order_id
  93. * @return array|\yii\db\ActiveRecord[]
  94. */
  95. public function findByOrderId($order_id)
  96. {
  97. return ProductExpress::find()
  98. ->where(['order_id' => $order_id, 'status' => StatusEnum::ENABLED])
  99. ->all();
  100. }
  101. }
粤ICP备19079148号