GhsOrderController.php 4.8 KB

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