StoreHangOrderServices.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\order\cashier;
  12. use app\dao\order\StoreHangOrderDao;
  13. use app\services\BaseServices;
  14. use app\services\order\StoreCartServices;
  15. use app\services\order\StoreOrderServices;
  16. use crmeb\traits\ServicesTrait;
  17. /**
  18. * Class StoreHangOrderServices
  19. * @package app\services\order\cashier
  20. * @mixin StoreHangOrderDao
  21. */
  22. class StoreHangOrderServices extends BaseServices
  23. {
  24. use ServicesTrait;
  25. /**
  26. * StoreHangOrderServices constructor.
  27. * @param StoreHangOrderDao $dao
  28. */
  29. public function __construct(StoreHangOrderDao $dao)
  30. {
  31. $this->dao = $dao;
  32. }
  33. /**
  34. * 获取挂单10个,历史下单20个
  35. * @param int $storeId
  36. * @param int $staffId
  37. * @return array
  38. * @throws \think\db\exception\DataNotFoundException
  39. * @throws \think\db\exception\DbException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. */
  42. public function getHangOrder(int $storeId, int $staffId)
  43. {
  44. /** @var StoreCartServices $service */
  45. $service = app()->make(StoreCartServices::class);
  46. $data = $service->getHangOrder($storeId, $staffId)->limit(10)->order('add_time desc')->select()->toArray();
  47. foreach ($data as &$item) {
  48. $item['is_check'] = 0;
  49. }
  50. $uid = $data ? array_column($data, 'uid') : [];
  51. /** @var StoreOrderServices $orderService */
  52. $orderService = app()->make(StoreOrderServices::class);
  53. $user = $orderService->getOrderHistoryList($storeId, $staffId, $uid, 20);
  54. foreach ($user as &$item) {
  55. $item['is_check'] = 1;
  56. }
  57. [$usec, $sec] = explode(" ", microtime());
  58. $msec = round($usec * 1000);
  59. $tourist = [[
  60. 'is_check' => 1,
  61. 'tourist_uid' => $msec,
  62. 'uid' => 0,
  63. 'nickname' => '',
  64. 'avatar' => '',
  65. 'staff_id' => $staffId,
  66. 'store_id' => $storeId,
  67. 'cart_id' => '',
  68. 'add_time' => time()
  69. ]];
  70. return array_merge($data, $tourist, $user);
  71. }
  72. /**
  73. * 获取挂单区列表
  74. * @param int $storeId
  75. * @param int $staffId
  76. * @return array
  77. * @throws \think\db\exception\DataNotFoundException
  78. * @throws \think\db\exception\DbException
  79. * @throws \think\db\exception\ModelNotFoundException
  80. */
  81. public function getHangOrderList(int $storeId, int $staffId, string $search)
  82. {
  83. [$page, $limit] = $this->getPageValue();
  84. /** @var StoreCartServices $service */
  85. $service = app()->make(StoreCartServices::class);
  86. $data = $service->getHangOrder($storeId, $staffId, $search, $page, $limit)->order('add_time desc')->select()->toArray();
  87. /** @var CashierOrderServices $make */
  88. $make = app()->make(CashierOrderServices::class);
  89. foreach ($data as &$item) {
  90. $item['id'] = $item['cart_id'];
  91. $item['_add_time'] = date('Y.m.d H:i:s', $item['add_time']);
  92. try {
  93. $computeOrder = $make->computeOrder($item['uid'], $storeId, explode(',', $item['cart_id']) ?: []);
  94. $item['price'] = $computeOrder['payPrice'];
  95. } catch (\Throwable $e) {
  96. $item['price'] = 0;
  97. }
  98. }
  99. $count = $service->getHangOrder($storeId, $staffId, $search)->count();
  100. return compact('data', 'count');
  101. }
  102. /**
  103. * 保存挂单
  104. * @param int $uid
  105. * @param array $cartIds
  106. * @param string $price
  107. * @param int $storeId
  108. * @param int $staffId
  109. * @param string $touristUid
  110. * @return \crmeb\basic\BaseModel|\think\Model
  111. */
  112. public function saveHang(int $uid, array $cartIds, string $price, int $storeId, int $staffId, string $touristUid = '')
  113. {
  114. $cartIds = implode(',', $cartIds);
  115. if ($uid && $this->dao->count(['uid' => $uid, 'store_id' => $storeId, 'staff_id' => $staffId])) {
  116. $this->dao->delete(['uid' => $uid, 'store_id' => $storeId, 'staff_id' => $staffId]);
  117. } else if ($touristUid && $this->dao->count(['tourist_uid' => $touristUid, 'store_id' => $storeId, 'staff_id' => $staffId])) {
  118. $this->dao->delete(['tourist_uid' => $touristUid, 'store_id' => $storeId, 'staff_id' => $staffId]);
  119. }
  120. return $this->dao->save([
  121. 'uid' => $uid,
  122. 'cart_ids' => $cartIds,
  123. 'store_id' => $storeId,
  124. 'staff_id' => $staffId,
  125. 'tourist_uid' => $touristUid,
  126. 'price' => $price
  127. ]);
  128. }
  129. }