GhsOrderController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. $condition = [
  34. 'payStatus' => 1,
  35. 'status' => ['in', [2, 3]],
  36. 'payTime' => ['between', [$minPayTime, $maxPayTime]],
  37. ];
  38. $this->batchConfirmReachByMainId($condition);
  39. Yii::info('批发订单自动完结脚本成功执行');
  40. }
  41. // 脚本二:历史补偿,处理付款超过 10 天未完结订单
  42. public function actionSetOldOrderToFinish()
  43. {
  44. $maxPayTime = date('Y-m-d H:i:s', strtotime('-9 day'));
  45. $condition = [
  46. 'payStatus' => 1,
  47. 'status' => ['in', [2, 3]],
  48. 'payTime <' => $maxPayTime,
  49. ];
  50. $this->batchConfirmReachByMainId($condition);
  51. Yii::info('批发订单自动完结历史补偿脚本成功执行');
  52. }
  53. /**
  54. * 按批发店 mainId 分批处理订单自动收货完结
  55. * @param array $extraCondition
  56. */
  57. private function batchConfirmReachByMainId($extraCondition)
  58. {
  59. $ghsShops = ShopClass::getAllByCondition(['ptStyle' => 2, 'live' => 1], null, 'mainId');
  60. if (empty($ghsShops)) {
  61. return;
  62. }
  63. $mainIds = array_unique(array_filter(array_column($ghsShops, 'mainId')));
  64. if (empty($mainIds)) {
  65. return;
  66. }
  67. $params = ['staffId' => 0, 'staffName' => '系统', 'notice' => false]; // notice设置为 false,实现不发送发货通知
  68. foreach ($mainIds as $mainId) {
  69. $condition = array_merge(['mainId' => $mainId], $extraCondition);
  70. $orders = OrderClass::getAllByCondition($condition, 'id asc', '*', null, true);
  71. if (empty($orders)) {
  72. continue;
  73. }
  74. foreach ($orders as $order) {
  75. try {
  76. OrderClass::confirmReach($order, $params);
  77. } catch (\Exception $e) {
  78. $orderId = $order->id ?? 0;
  79. Yii::error(
  80. "批发订单自动完结失败 mainId={$mainId}, orderId={$orderId}, message={$e->getMessage()}",
  81. 'ghsOrderAutoFinish'
  82. );
  83. }
  84. }
  85. }
  86. }
  87. }