OrderController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 console\controllers;
  11. use yii\console\Controller;
  12. use common\models\order\Order;
  13. use common\models\mall\Mall;
  14. use common\enums\OrderStatusEnum;
  15. use common\enums\StatusEnum;
  16. use common\globals\GlobalInvoke;
  17. use yii\console\ExitCode;
  18. use common\logic\statistics\StatisticsHandle;
  19. use common\models\statistics\UserStatisticsTotal;
  20. /**
  21. * 订单相关定时处理任务
  22. */
  23. class OrderController extends Controller
  24. {
  25. /**
  26. * 自动关闭过期订单
  27. */
  28. public function actionAutoClose()
  29. {
  30. $where = [
  31. ['order_status' => OrderStatusEnum::NOT_PAY],
  32. ['status' => StatusEnum::ENABLED],
  33. ];
  34. return self::actionOrderProcess('close', 'order.payment_expire_time', $where);
  35. // $exite_code = self::actionOrderProcess('close', 'order.payment_expire_time', $where);
  36. // echo $exite_code;
  37. // return $exite_code;
  38. }
  39. /**
  40. * 自动确认收货
  41. */
  42. public function actionAutoConfirm()
  43. {
  44. $where = [
  45. ['order_status' => OrderStatusEnum::SHIPMENTS],
  46. ['status' => StatusEnum::ENABLED],
  47. ['not in', 'order_source', ['ReserveRe', 'Reserve']],
  48. ['is_feedback' => [OrderStatusEnum::NOT_FEEDBACK, OrderStatusEnum::FEEDBACKED]]
  49. ];
  50. return self::actionOrderProcess('confirm', 'order.auto_receiving_time', $where);
  51. // $exite_code = self::actionOrderProcess('confirm', 'order.auto_receiving_time', $where);
  52. // echo $exite_code;
  53. // return $exite_code;
  54. }
  55. /**
  56. * 订单自动过售后
  57. */
  58. public function actionAutoComplete()
  59. {
  60. $where = [
  61. ['order_status' => [OrderStatusEnum::SING,OrderStatusEnum::TAKE_FINISH]],
  62. ['status' => StatusEnum::ENABLED],
  63. ];
  64. return self::actionOrderProcess('complete', 'order.can_after_sale_time', $where);
  65. // $exite_code = self::actionOrderProcess('confirm', 'order.auto_receiving_time', $where);
  66. // echo $exite_code;
  67. // return $exite_code;
  68. }
  69. /**
  70. * @name:
  71. * @msg:
  72. * @param {string} $action 操作类型,close:自动关闭订单,confirm:自动确认收货,complete:订单自动过售后
  73. * @param {string} $config_key 相应操作时间间隔配置名称
  74. * @param {int} $order_status 需要进行处理的订单的状态
  75. * @return {int}
  76. */
  77. private static function actionOrderProcess(string $action, string $config_key, array $order_wheres = null) : int
  78. {
  79. $action_info_map = [
  80. 'close' => [
  81. 'task_name' => '自动关闭订单',
  82. 'time_radix' => 60, // 时间节点计算的基数,自动关闭订单时间单位是分钟,转换为秒的基数是60
  83. 'method' => 'close',
  84. 'time_field' => 'created_at',
  85. 'default' => 30, // 自动关闭订单默认时间 30 分钟
  86. ],
  87. 'confirm' => [
  88. 'task_name' => '自动确认收货',
  89. 'time_radix' => 86400, // 时间节点计算的基数,订单自动确认收货的时间单位是天,转换为秒的基数是86400
  90. 'method' => 'takeDelivery',
  91. 'time_field' => 'consign_time',
  92. 'default' => 7, // 自动确认收货默认时间 7 天
  93. ],
  94. 'complete' => [
  95. 'task_name' => '自动过售后',
  96. 'time_radix' => 86400, // 时间节点计算的基数,订单自动自动过售后的时间单位是天,转换为秒的基数是86400
  97. 'method' => 'complete',
  98. 'time_field' => 'sign_time',
  99. 'default' => 7, // 自动过售后默认时间 7 天
  100. ],
  101. ];
  102. // var_dump($action_info_map);
  103. try {
  104. $mall_list = Mall::getEnableMallList(true);
  105. $mall_ids = array_column($mall_list, 'id');
  106. // var_dump($mall_ids);
  107. if (empty($mall_ids)) {
  108. echo '没有可用商城,不做处理 ... ' . PHP_EOL;
  109. return ExitCode::UNSPECIFIED_ERROR;
  110. }
  111. foreach ($mall_ids as $mall_id) {
  112. // 订单自动处理时间
  113. $order_auto_handle_time = \Yii::$app->services->setting->mall->get($mall_id, $config_key);
  114. // 如果自动处理时间没有配置,则取默认值 优化1459调整订单售后时间可以调整为设置0
  115. if (empty($order_auto_handle_time) && $order_auto_handle_time != 0) $order_auto_handle_time = $action_info_map[$action]['default'];
  116. // 订单自动处理时间节点,延后两分钟处理,避免支付回调尚未回来订单已经被取消s
  117. $time_node = time() - $order_auto_handle_time * $action_info_map[$action]['time_radix'] - 120;
  118. $time_field = $action_info_map[$action]['time_field'];
  119. $where = [
  120. 'and',
  121. ['mall_id' => $mall_id],
  122. ['<', $time_field, $time_node]
  123. ];
  124. if (!empty($order_wheres)) {
  125. foreach ($order_wheres as $order_where) {
  126. array_push($where, $order_where);
  127. }
  128. }
  129. // var_dump($where);exit;
  130. $select = 'id,mall_id,user_id,pay_money,marketing_type';
  131. foreach (Order::find()->select($select)->where($where)->each(500) as $order) {
  132. $is_continue = false;
  133. $goods_num = 0;
  134. if ('confirm' == $action && !empty($order->detail)) {
  135. foreach ($order->detail as $detail) {
  136. if ($detail->is_feedback == 1) {
  137. echo ' 订单[ID:' . $order->id . ', DETAIL-ID:' . $detail->id . ']是售后订单,不执行自动确认收货。' . PHP_EOL;
  138. $is_continue = true;
  139. break;
  140. }
  141. $goods_num += $detail->num;
  142. }
  143. }
  144. // 如果有在售后中的订单,则直接跳过不处理
  145. if ($is_continue) continue;
  146. $invoke_arr = GlobalInvoke::exec(GlobalInvoke::CONSOLE_ORDER_DETAIL, $mall_id, [
  147. 'order' => $order,
  148. 'action' => $action,
  149. 'mall_id' => $mall_id,
  150. ]);
  151. $addons_name = $order->marketing_type;
  152. if($addons_name && isset($invoke_arr[$addons_name])){
  153. $invoke_result = $invoke_arr[$addons_name];
  154. $res = isset($invoke_result[$action]) && $invoke_result[$action] === false;
  155. if($res) continue;
  156. }
  157. $data = [
  158. 'id' => $order->id,
  159. 'user_id' => $order->user_id,
  160. 'mall_id' => $mall_id,
  161. 'operator_id' => 0,
  162. 'operator_name' => '系统',
  163. 'is_system_auto' => 1
  164. ];
  165. $method = $action_info_map[$action]['method'];
  166. if (!Order::$method($data)) {
  167. echo ' 订单[ID:' . $order->id . ']' . $action_info_map[$action]['task_name'] . '失败, errmsg: ' . Order::getStaticError() . PHP_EOL;
  168. continue;
  169. }
  170. if ($action == 'close') {
  171. //用户数据统计
  172. StatisticsHandle::execute('order_close', ['id' => $order->id]);
  173. }
  174. echo '订单[ID:' . $order->id . ']' . $action_info_map[$action]['task_name'] . '完成' . PHP_EOL;
  175. // exit;
  176. }
  177. }
  178. return ExitCode::OK;
  179. } catch (\Exception $e) {
  180. echo ' 订单' . $action_info_map[$action]['task_name'] . ' 任务处理出错:' . $e->getMessage() . PHP_EOL;
  181. return ExitCode::UNSPECIFIED_ERROR;
  182. }
  183. }
  184. }