GhsOrderController.php 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace console\controllers;
  3. use biz\shop\classes\ShopClass;
  4. use bizGhs\order\classes\OrderClass;
  5. use common\components\noticeUtil;
  6. use Yii;
  7. use yii\console\Controller;
  8. class GhsOrderController extends Controller
  9. {
  10. // 脚本一:每天 03:20 执行,处理付款时间 7~10 天前订单
  11. public function actionAutoFinish()
  12. {
  13. ini_set('memory_limit', '2045M');
  14. set_time_limit(0);
  15. $minPayTime = date('Y-m-d H:i:s', strtotime('-10 day'));
  16. $maxPayTime = date('Y-m-d H:i:s', strtotime('-7 day'));
  17. $extraCondition = [
  18. 'payStatus' => 1,
  19. 'status' => ['in', [2, 3]],
  20. 'payTime' => ['between', [$minPayTime, $maxPayTime]],
  21. ];
  22. $ghsShops = ShopClass::getAllByCondition(['ptStyle' => 2, 'live' => 1], null, 'mainId');
  23. if (!empty($ghsShops)) {
  24. $mainIds = array_unique(array_filter(array_column($ghsShops, 'mainId')));
  25. if (!empty($mainIds)) {
  26. $params = ['staffId' => 0, 'staffName' => '系统', 'notice' => false]; // notice设置为 false,实现不发送发货通知
  27. foreach ($mainIds as $mainId) {
  28. $condition = array_merge(['mainId' => $mainId], $extraCondition);
  29. $orders = OrderClass::getAllByCondition($condition, 'id asc', '*', null, true);
  30. if (empty($orders)) {
  31. continue;
  32. }
  33. foreach ($orders as $order) {
  34. $connection = Yii::$app->db;
  35. $transaction = $connection->beginTransaction();
  36. try {
  37. OrderClass::confirmReach($order, $params);
  38. $transaction->commit();
  39. } catch (\Exception $e) {
  40. $transaction->rollBack();
  41. $orderId = $order->id ?? 0;
  42. noticeUtil::push("订单自动完结失败 orderId={$orderId}, message={$e->getMessage()}", '15280215347');
  43. }
  44. }
  45. }
  46. }
  47. }
  48. Yii::info('批发订单自动完结脚本成功执行');
  49. }
  50. // 脚本二:历史补偿,处理付款超过 10 天未完结订单
  51. public function actionAutoFinishHistory()
  52. {
  53. $maxPayTime = date('Y-m-d H:i:s', strtotime('-1 day'));
  54. $extraCondition = [
  55. 'payStatus' => 1,
  56. 'status' => ['in', [2, 3]],
  57. 'payTime<' => $maxPayTime,
  58. ];
  59. $ghsShops = ShopClass::getAllByCondition(['ptStyle' => 2, 'live' => 1], null, 'mainId');
  60. if (!empty($ghsShops)) {
  61. $mainIds = array_unique(array_filter(array_column($ghsShops, 'mainId')));
  62. if (!empty($mainIds)) {
  63. $params = ['staffId' => 0, 'staffName' => '系统', 'notice' => false]; // notice设置为 false,实现不发送发货通知
  64. foreach ($mainIds as $mainId) {
  65. $condition = array_merge(['mainId' => $mainId], $extraCondition);
  66. $orders = OrderClass::getAllByCondition($condition, 'id asc', '*', null, true);
  67. if (empty($orders)) {
  68. continue;
  69. }
  70. foreach ($orders as $order) {
  71. $connection = Yii::$app->db;
  72. $transaction = $connection->beginTransaction();
  73. try {
  74. OrderClass::confirmReach($order, $params);
  75. $transaction->commit();
  76. echo $order->id . " ok \n";
  77. } catch (\Exception $e) {
  78. $transaction->rollBack();
  79. $orderId = $order->id ?? 0;
  80. noticeUtil::push("自动完结失败,orderId={$orderId}, message={$e->getMessage()}", '15280215347');
  81. }
  82. }
  83. }
  84. }
  85. }
  86. Yii::info('批发订单自动完结历史补偿脚本成功执行');
  87. }
  88. }