LiveOrderController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace console\controllers;
  3. use biz\shop\classes\ShopClass;
  4. use bizGhs\live\classes\LiveOrderClass;
  5. use bizGhs\live\classes\LiveOrderCustomClass;
  6. use bizGhs\live\classes\LiveSeatClass;
  7. use common\components\noticeUtil;
  8. use bizGhs\custom\classes\CustomClass;
  9. use bizGhs\order\services\OrderService;
  10. use yii\console\Controller;
  11. use Yii;
  12. class LiveOrderController extends Controller
  13. {
  14. //每二分钟执行一次 ssh 20241006
  15. public function actionSeat()
  16. {
  17. ini_set('memory_limit', '2045M');
  18. set_time_limit(0);
  19. $shopList = ShopClass::getAllByCondition(['ptStyle' => 2], null, '*', null, true);
  20. if (empty($shopList)) {
  21. return false;
  22. }
  23. foreach ($shopList as $shop) {
  24. $mainId = $shop->mainId;
  25. $shopName = $shop->shopName ?? '';
  26. $sjName = $shop->merchantName ?? '';
  27. $shopId = $shop->id ?? 0;
  28. $sjId = $shop->sjId ?? 0;
  29. $name = $sjName . ' ' . $shopName;
  30. $cacheKey = 'live_order_' . $mainId;
  31. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  32. if (empty($has)) {
  33. continue;
  34. }
  35. $connection = Yii::$app->db;
  36. $transaction = $connection->beginTransaction();
  37. try {
  38. $list = LiveOrderClass::getAllByCondition(['mainId' => $mainId, 'switchOrder' => 0], null, '*');
  39. if (empty($list)) {
  40. continue;
  41. }
  42. $customInfo = CustomClass::getAllByCondition(['ownMainId' => $mainId], null, '*', 'id');
  43. $total = [];
  44. $format = [];
  45. $staffId = 0;
  46. $staffName = '';
  47. $historyDate = date("Y-m-d");
  48. foreach ($list as $key => $live) {
  49. $addTime = $live['addTime'];
  50. $historyDate = date("Y-m-d", strtotime($addTime));
  51. $orderSn = $live['orderSn'];
  52. $itemId = $live['itemId'];
  53. $ptItemId = $live['ptItemId'];
  54. $staffId = $live['staffId'] ?? 0;
  55. $staffName = $live['staffName'] ?? '';
  56. $date = date("Y-m-d", strtotime($addTime));
  57. $customList = LiveOrderCustomClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  58. if (empty($customList)) {
  59. continue;
  60. }
  61. $total[$date] = 1;
  62. foreach ($customList as $k => $v) {
  63. $customId = $v['customId'] ?? 0;
  64. $num = $v['num'] ?? 0;
  65. $unitPrice = $v['unitPrice'] ?? 0;
  66. $currentCustom = $customInfo[$customId] ?? [];
  67. if (empty($currentCustom)) {
  68. throw new \Exception("出现不存在的客户 customId:{$customId}");
  69. }
  70. $format[$customId][] = [
  71. 'productId' => $itemId,
  72. 'bigNum' => $num,
  73. 'itemId' => $ptItemId,
  74. 'smallNum' => 0,
  75. 'price' => $unitPrice,
  76. ];
  77. }
  78. }
  79. if (count($total) > 1) {
  80. noticeUtil::push($name . " mainId:{$mainId} live order 有记录不是同一天的", '15280215347');
  81. continue;
  82. }
  83. if (empty($format)) {
  84. noticeUtil::push($name . " mainId:{$mainId} live order 没有找到客户预订", '15280215347');
  85. continue;
  86. }
  87. foreach ($format as $customId => $mat) {
  88. $modifyPrice = 0;
  89. foreach ($mat as $ma) {
  90. $cNum = $ma['bigNum'] ?? 0;
  91. $cPrice = $ma['price'] ?? 0;
  92. $cTotal = bcmul($cNum, $cPrice, 2);
  93. $modifyPrice = bcadd($modifyPrice, $cTotal, 2);
  94. }
  95. $currentCustom = $customInfo[$customId] ?? [];
  96. if (empty($currentCustom)) {
  97. throw new \Exception("出现不存在的客户哦 customId:{$customId}");
  98. }
  99. $customName = $currentCustom['name'] ?? '';
  100. $customMobile = $currentCustom['mobile'] ?? '';
  101. $customPy = $currentCustom['py'] ?? '';
  102. $ghsId = $currentCustom['ghsId'] ?? 0;
  103. $hasPay = 2;
  104. $custom = CustomClass::getCustom($customId);
  105. $post = [];
  106. $post['modifyPrice'] = $modifyPrice;
  107. $post['shopId'] = $shopId;
  108. $post['sendType'] = 0;
  109. $post['transType'] = 0;
  110. $post['sendSide'] = 0;
  111. $post['sendCost'] = 0;
  112. $post['packCost'] = 0;
  113. $post['customId'] = $customId;
  114. $post['customName'] = $customName;
  115. $post['payWay'] = 0;
  116. $post['needPrint'] = 2;
  117. $post['hasPay'] = 2;
  118. $post['product'] = $mat;
  119. $post['deadline'] = date("Y-m-d H:i:s", time() + 86400);
  120. $post['debt'] = 1;
  121. $post['mainId'] = 0;
  122. $post['fromType'] = 1;
  123. $post['expressId'] = 0;
  124. $post['sjId'] = $sjId;
  125. $post['shopAdminId'] = $staffId;
  126. $post['shopAdminName'] = $staffName;
  127. $post['getStaffId'] = $staffId;
  128. $post['getStaffName'] = $staffName;
  129. $post['customMobile'] = $customMobile;
  130. $post['customNamePy'] = $customPy;
  131. $post['customAvatar'] = '';
  132. $post['newVersion'] = 1;
  133. $post['book'] = 0;
  134. $post['wlName'] = '';
  135. $post['dealPrice'] = 1;
  136. $post['ghsId'] = $ghsId;
  137. $post['historyDate'] = $historyDate;
  138. OrderService::createFinishOrder($post, $custom, $hasPay);
  139. }
  140. //清空货位
  141. CustomClass::updateByCondition(['ownMainId' => $mainId], ['seatSn' => '']);
  142. LiveSeatClass::updateByCondition(['mainId' => $mainId], ['customId' => 0, 'customName' => '']);
  143. $transaction->commit();
  144. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
  145. } catch (\Exception $e) {
  146. $transaction->rollBack();
  147. $msg = $e->getMessage();
  148. noticeUtil::push($name . " mainId:{$mainId} live order 报错了:" . $msg, '15280215347');
  149. }
  150. }
  151. }
  152. }