OrderController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace console\controllers;
  3. use bizGhs\order\classes\OrderClass;
  4. use bizGhs\order\models\Order;
  5. use bizGhs\order\services\OrderService;
  6. use common\components\dict;
  7. use common\components\noticeUtil;
  8. use common\components\util;
  9. use Yii;
  10. use yii\console\Controller;
  11. use bizHd\purchase\classes\PurchaseClass;
  12. use bizHd\purchase\services\PurchaseService;
  13. /**
  14. * 订单相关
  15. */
  16. class OrderController extends Controller
  17. {
  18. //待配送订单,超过24小时自动完成 ./yii order/auto-finish
  19. public function actionAutoFinish()
  20. {
  21. //好像没有用了
  22. util::stop();
  23. $remain = time() - 86400;
  24. $date = date("Y-m-d H:i:s", $remain);
  25. $query = new \yii\db\Query();
  26. $query->from(Order::tableName());
  27. $query->where(['status' => OrderClass::ORDER_STATUS_UN_SEND])->andWhere(['<=', 'addTime', $date]);
  28. $query->orderBy('deadline ASC');
  29. //注意哪个平台的行为,低层很多方法会使用到
  30. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'ghs');
  31. //如果有报错,抛出异常
  32. Yii::$app->params['errorReport'] = 1;
  33. //24小时后的自动确认,不需要微信通知
  34. Yii::$app->params['noNeedWxNotice'] = 1;
  35. foreach ($query->batch() as $orderInfoList) {
  36. print_r($orderInfoList);continue;
  37. foreach ($orderInfoList as $orderInfo) {
  38. $connection = Yii::$app->db;//事务处理
  39. $transaction = $connection->beginTransaction();
  40. $id = $orderInfo['id'] ?? 0;
  41. $orderSn = $orderInfo['orderSn'] ?? '';
  42. $status = $orderInfo['status'] ?? OrderClass::ORDER_STATUS_UN_PAY;
  43. $addTime = $orderInfo['addTime'] ?? '';
  44. $prefix = "订单{$orderSn},ID{$id},创建于{$addTime},状态是{$status}";
  45. if ($status != OrderClass::ORDER_STATUS_UN_SEND) {
  46. noticeUtil::push($prefix . " 自动完成操作时,状态不对", '15280215347');
  47. continue;
  48. }
  49. if (empty($addTime) && $addTime > $date) {
  50. noticeUtil::push($prefix . " 自动完成操作时,时间不对", '15280215347');
  51. continue;
  52. }
  53. try {
  54. $unSendOrder = OrderClass::getById($id, true);
  55. OrderClass::selfSend($unSendOrder);
  56. $hasSendOrder = OrderClass::getById($id, true);
  57. OrderService::reach($hasSendOrder);
  58. $transaction->commit();
  59. //noticeUtil::push($prefix . " 【已自动完成】 SUCCESS", '15280215347');
  60. } catch (\Exception $e) {
  61. $transaction->rollBack();
  62. $msg = $e->getMessage();
  63. noticeUtil::push("销售单自动完成报错了!订单号:{$orderSn},错误信息:{$msg}", '15280215347');
  64. }
  65. }
  66. }
  67. }
  68. //商家自己开的订单,3小时后没确认自动确认为欠款 ./yii order/auto-confirm
  69. public function actionAutoConfirm()
  70. {
  71. //好像没有用了
  72. util::stop();
  73. $remain = time() - 10800;
  74. $date = date("Y-m-d H:i:s", $remain);
  75. $query = new \yii\db\Query();
  76. $query->from(Order::tableName());
  77. $query->where(['status' => OrderClass::ORDER_STATUS_UN_PAY])->andWhere(['<=', 'addTime', $date]);
  78. $query->orderBy('addTime ASC');
  79. //注意哪个平台的行为,低层很多方法会使用到
  80. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'ghs');
  81. //如果有报错,抛出异常
  82. Yii::$app->params['errorReport'] = 1;
  83. //24小时后的自动确认,不需要微信通知
  84. Yii::$app->params['noNeedWxNotice'] = 1;
  85. foreach ($query->batch() as $orderInfoList) {
  86. foreach ($orderInfoList as $orderInfo) {
  87. $connection = Yii::$app->db;//事务处理
  88. $transaction = $connection->beginTransaction();
  89. $id = $orderInfo['id'] ?? 0;
  90. try {
  91. $order = OrderClass::getLockById($id);
  92. $orderId = $order->id ?? '';
  93. $shopAdminId = $order->shopAdminId ?? 0;
  94. if(empty($shopAdminId)){
  95. noticeUtil::push($prefix . " 自动确认失败,不是员工开单 orderId:{$orderId}", '15280215347');
  96. continue;
  97. }
  98. $purchaseId = $order->purchaseId ?? 0;
  99. $purchase = PurchaseClass::getById($purchaseId, true);
  100. if (empty($purchase)) {
  101. noticeUtil::push($prefix . " 自动确认失败,没有找到采购单 orderId:{$orderId}", '15280215347');
  102. continue;
  103. }
  104. $payWay = dict::getDict('payWay', 'debtPay');
  105. //不需要打印
  106. $order->needPrint = dict::getDict('needPrint', 'noNeed');
  107. $order->save();
  108. //支付
  109. PurchaseService::payAfter($purchase, $payWay);
  110. OrderService::payAfter($order, $payWay);
  111. //自己送
  112. $info = OrderClass::getById($id, true);
  113. OrderClass::selfSend($info);
  114. //确认送达,并且不需要微信通知
  115. Yii::$app->params['noNeedWxNotice'] = 1;
  116. $info = OrderClass::getById($id, true);
  117. OrderService::reach($info);
  118. $transaction->commit();
  119. noticeUtil::push($prefix . " 【已自动确认】 SUCCESS", '15280215347');
  120. } catch (\Exception $e) {
  121. $transaction->rollBack();
  122. $msg = $e->getMessage();
  123. noticeUtil::push("自动确认订单报错!订单号:{$orderSn},错误信息:{$msg}", '15280215347');
  124. }
  125. }
  126. }
  127. }
  128. }