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