CashierOrderController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /*
  3. * @Descripttion:
  4. * @version:
  5. * @Author: ewgof
  6. * @Date: 2021-10-20 14:34:16
  7. * @LastEditors: ewgof
  8. * @LastEditTime: 2022-01-20 17:54:35
  9. */
  10. namespace addons\Store\console\controllers;
  11. use common\enums\AddonsEnum;
  12. use common\models\mall\MallAddons;
  13. use yii\console\Controller;
  14. use common\models\mall\Mall;
  15. use common\enums\OrderStatusEnum;
  16. use common\enums\StatusEnum;
  17. use yii\console\ExitCode;
  18. use addons\Store\common\models\StoreCashierOrder;
  19. /**
  20. * 订单相关定时处理任务
  21. */
  22. class CashierOrderController extends Controller
  23. {
  24. /**
  25. * 自动关闭过期订单
  26. */
  27. public function actionAutoClose()
  28. {
  29. $where = [
  30. ['pay_status' => OrderStatusEnum::NOT_PAY],
  31. ['status' => StatusEnum::ENABLED],
  32. ];
  33. return self::actionOrderProcess('close', 'order.payment_expire_time', $where);
  34. }
  35. /**
  36. * @name:
  37. * @msg:
  38. * @param {string} $action 操作类型,close:自动关闭订单,confirm:自动确认收货,complete:订单自动过售后
  39. * @param {string} $config_key 相应操作时间间隔配置名称
  40. * @param {int} pay_status 需要进行处理的订单的状态
  41. * @return {int}
  42. */
  43. private static function actionOrderProcess(string $action, string $config_key, array $order_wheres = null) : int
  44. {
  45. $action_info_map = [
  46. 'close' => [
  47. 'task_name' => '自动关闭订单',
  48. 'time_radix' => 60, // 时间节点计算的基数,自动关闭订单时间单位是分钟,转换为秒的基数是60
  49. 'method' => 'close',
  50. 'time_field' => 'created_at',
  51. 'default' => 5, // 自动关闭订单默认时间 20 分钟
  52. ],
  53. ];
  54. try {
  55. $mall_list = Mall::getEnableMallList(true);
  56. $mall_ids = array_column($mall_list, 'id');
  57. if (empty($mall_ids)) {
  58. echo '没有可用商城,不做处理 ... ' . PHP_EOL;
  59. return ExitCode::UNSPECIFIED_ERROR;
  60. }
  61. foreach ($mall_ids as $mall_id) {
  62. $is_install = MallAddons::isInstall($mall_id, AddonsEnum::ADDONS_NAME_STORE);
  63. if(empty($is_install)) continue;
  64. // 如果自动处理时间没有配置,则取默认值
  65. $order_auto_handle_time = $action_info_map[$action]['default'];
  66. // 订单自动处理时间节点,延后两分钟处理,避免支付回调尚未回来订单已经被取消s
  67. $time_node = time() - $order_auto_handle_time * $action_info_map[$action]['time_radix'] - 120;
  68. $time_field = $action_info_map[$action]['time_field'];
  69. $where = [
  70. 'and',
  71. ['mall_id' => $mall_id],
  72. ['<', $time_field, $time_node]
  73. ];
  74. if (!empty($order_wheres)) {
  75. foreach ($order_wheres as $order_where) {
  76. array_push($where, $order_where);
  77. }
  78. }
  79. $select = 'id,mall_id,user_id,pay_money';
  80. foreach (StoreCashierOrder::find()->select($select)->where($where)->each(500) as $order) {
  81. $data = [
  82. 'id' => $order->id,
  83. 'user_id' => $order->user_id,
  84. 'mall_id' => $mall_id,
  85. 'operator_id' => 0,
  86. 'operator_name' => '系统',
  87. ];
  88. $method = $action_info_map[$action]['method'];
  89. if (!StoreCashierOrder::$method($data)) {
  90. echo ' 订单[ID:' . $order->id . ']' . $action_info_map[$action]['task_name'] . '失败, errmsg: ' . StoreCashierOrder::getStaticError() . PHP_EOL;
  91. continue;
  92. }
  93. }
  94. }
  95. return ExitCode::OK;
  96. } catch (\Exception $e) {
  97. echo ' 订单' . $action_info_map[$action]['task_name'] . ' 任务处理出错:' . $e->getMessage() . PHP_EOL;
  98. return ExitCode::UNSPECIFIED_ERROR;
  99. }
  100. }
  101. }