LiveOrderController.php 7.7 KB

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