BargainOrderController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace console\controllers;
  3. use addons\Bargain\common\enums\BargainEnum;
  4. use addons\Bargain\common\models\BargainActive;
  5. use addons\Bargain\common\models\BargainOrder;
  6. use addons\GroupBuy\common\enums\GroupBuyEnum;
  7. use common\models\goods\GoodsMarketing;
  8. use common\models\mall\MallAddons;
  9. use yii\console\Controller;
  10. use common\models\order\Order;
  11. use common\models\mall\Mall;
  12. use common\enums\OrderStatusEnum;
  13. use common\enums\StatusEnum;
  14. use yii\console\ExitCode;
  15. /**
  16. * 订单相关定时处理任务
  17. */
  18. class BargainOrderController extends Controller
  19. {
  20. /**
  21. * 自动关闭过期订单
  22. */
  23. public function actionAutoClose()
  24. {
  25. $where = [
  26. ['order_status' => OrderStatusEnum::NOT_PAY],
  27. ['status' => StatusEnum::ENABLED],
  28. ];
  29. return self::actionOrderProcess('close', 'order.payment_expire_time', $where);
  30. }
  31. /**
  32. * @desc:自动结束过期砍价活动
  33. * @Author: hua
  34. * @Date: 2021/12/16
  35. * @Time: 8:57
  36. * @Copyright: copyright (c) 2021 广东七件事集团
  37. */
  38. public function actionAutoFinishActive()
  39. {
  40. try {
  41. $mall_list = Mall::getEnableMallList(true);
  42. $mall_ids = array_column($mall_list, 'id');
  43. if (empty($mall_ids)) {
  44. echo '没有可用商城,不做处理 ... ' . PHP_EOL;
  45. return ExitCode::UNSPECIFIED_ERROR;
  46. }
  47. foreach ($mall_ids as $mall_id) {
  48. if (MallAddons::isInstall($mall_id, BargainEnum::ADDONS_NAME) == 0) continue;
  49. $active_list = BargainActive::findAll(['mall_id' => $mall_id, 'active_status' => BargainEnum::ACTIVE_HANDING]);
  50. if ($active_list) {
  51. foreach ($active_list as $active) {
  52. if ($active->end_time > time()) continue;
  53. $where = [
  54. 'mall_id' => $mall_id,
  55. 'bargain_active_id' => $active->id,
  56. 'order_status' => BargainEnum::ORDER_HANDING,
  57. 'is_pay' => 0,
  58. ];
  59. foreach (BargainOrder::find()->where($where)->each(500) as $bargain_order) {
  60. //已过期,砍价要结束
  61. $data = [
  62. 'id' => $bargain_order->id,
  63. 'user_id' => $bargain_order->user_id,
  64. 'mall_id' => $mall_id,
  65. 'operator_id' => 0,
  66. 'operator_name' => '系统',
  67. ];
  68. $res = BargainOrder::close($data);
  69. if ($res == false) {
  70. echo ' 砍价订单[ID:' . $bargain_order->id . ']过期,执行砍价结束失败, errmsg: ' . BargainOrder::getStaticError() . PHP_EOL;
  71. continue;
  72. }
  73. }
  74. $active->active_status = BargainEnum::ACTIVE_FINISH;
  75. $active->save();
  76. $params['mall_id'] = $mall_id;
  77. $params['marketing_id'] = $active['id'];
  78. $params['marketing_type'] = BargainEnum::ADDONS_NAME;
  79. $params['status'] = StatusEnum::DELETE;
  80. GoodsMarketing::setData($params);
  81. }
  82. }
  83. }
  84. return ExitCode::OK;
  85. } catch (\Exception $e) {
  86. echo '砍价订单,执行砍价结束失败任务处理出错:' . $e->getMessage() . PHP_EOL;
  87. return ExitCode::UNSPECIFIED_ERROR;
  88. }
  89. }
  90. /**
  91. * @name:
  92. * @msg:
  93. * @param {string} $action 操作类型,close:自动关闭订单,confirm:自动确认收货,complete:订单自动过售后
  94. * @param {string} $config_key 相应操作时间间隔配置名称
  95. * @param {int} $order_status 需要进行处理的订单的状态
  96. * @return {int}
  97. */
  98. private static function actionOrderProcess(string $action, string $config_key, array $order_wheres = null): int
  99. {
  100. $action_info_map = [
  101. 'close' => [
  102. 'task_name' => '自动关闭订单',
  103. 'time_radix' => 60, // 时间节点计算的基数,自动关闭订单时间单位是分钟,转换为秒的基数是60
  104. 'method' => 'close',
  105. 'time_field' => 'created_at',
  106. 'default' => 30, // 自动关闭订单默认时间 30 分钟
  107. ],
  108. ];
  109. try {
  110. $mall_list = Mall::getEnableMallList(true);
  111. $mall_ids = array_column($mall_list, 'id');
  112. if (empty($mall_ids)) {
  113. echo '没有可用商城,不做处理 ... ' . PHP_EOL;
  114. return ExitCode::UNSPECIFIED_ERROR;
  115. }
  116. foreach ($mall_ids as $mall_id) {
  117. // 判断用户插件是否安装
  118. if (MallAddons::isInstall($mall_id, BargainEnum::ADDONS_NAME) == 0) continue;
  119. // 订单自动处理时间
  120. $order_auto_handle_time = \Yii::$app->services->setting->mall->get($mall_id, $config_key);
  121. // 如果自动处理时间没有配置,则取默认值
  122. if (empty($order_auto_handle_time)) $order_auto_handle_time = $action_info_map[$action]['default'];
  123. // 订单自动处理时间节点,延后两分钟处理,避免支付回调尚未回来订单已经被取消s
  124. $time_node = time() - $order_auto_handle_time * $action_info_map[$action]['time_radix'] - 120;
  125. $time_field = $action_info_map[$action]['time_field'];
  126. $where = [
  127. 'and',
  128. ['mall_id' => $mall_id],
  129. ['<', $time_field, $time_node]
  130. ];
  131. if (!empty($order_wheres)) {
  132. foreach ($order_wheres as $order_where) {
  133. array_push($where, $order_where);
  134. }
  135. }
  136. $select = 'id,user_id';
  137. foreach (BargainOrder::find()->select($select)->where($where)->each(500) as $order) {
  138. $data = [
  139. 'id' => $order->id,
  140. 'user_id' => $order->user_id,
  141. 'mall_id' => $mall_id,
  142. 'operator_id' => 0,
  143. 'operator_name' => '系统',
  144. ];
  145. $method = $action_info_map[$action]['method'];
  146. if (!BargainOrder::$method($data)) {
  147. echo ' 砍价订单[ID:' . $order->id . ']' . $action_info_map[$action]['task_name'] . '失败, errmsg: ' . BargainOrder::getStaticError() . PHP_EOL;
  148. continue;
  149. }
  150. }
  151. }
  152. return ExitCode::OK;
  153. } catch (\Exception $e) {
  154. echo ' 砍价订单' . $action_info_map[$action]['task_name'] . ' 任务处理出错:' . $e->getMessage() . PHP_EOL;
  155. return ExitCode::UNSPECIFIED_ERROR;
  156. }
  157. }
  158. }