| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
- /*
- * @Descripttion:
- * @version:
- * @Author: ewgof
- * @Date: 2021-10-20 14:34:16
- * @LastEditors: ewgof
- * @LastEditTime: 2022-01-20 17:54:35
- */
- namespace console\controllers;
- use yii\console\Controller;
- use common\models\order\Order;
- use common\models\mall\Mall;
- use common\enums\OrderStatusEnum;
- use common\enums\StatusEnum;
- use common\globals\GlobalInvoke;
- use yii\console\ExitCode;
- use common\logic\statistics\StatisticsHandle;
- use common\models\statistics\UserStatisticsTotal;
- /**
- * 订单相关定时处理任务
- */
- class OrderController extends Controller
- {
- /**
- * 自动关闭过期订单
- */
- public function actionAutoClose()
- {
- $where = [
- ['order_status' => OrderStatusEnum::NOT_PAY],
- ['status' => StatusEnum::ENABLED],
- ];
- return self::actionOrderProcess('close', 'order.payment_expire_time', $where);
- // $exite_code = self::actionOrderProcess('close', 'order.payment_expire_time', $where);
- // echo $exite_code;
- // return $exite_code;
- }
- /**
- * 自动确认收货
- */
- public function actionAutoConfirm()
- {
- $where = [
- ['order_status' => OrderStatusEnum::SHIPMENTS],
- ['status' => StatusEnum::ENABLED],
- ['not in', 'order_source', ['ReserveRe', 'Reserve']],
- ['is_feedback' => [OrderStatusEnum::NOT_FEEDBACK, OrderStatusEnum::FEEDBACKED]]
- ];
- return self::actionOrderProcess('confirm', 'order.auto_receiving_time', $where);
- // $exite_code = self::actionOrderProcess('confirm', 'order.auto_receiving_time', $where);
- // echo $exite_code;
- // return $exite_code;
- }
- /**
- * 订单自动过售后
- */
- public function actionAutoComplete()
- {
- $where = [
- ['order_status' => [OrderStatusEnum::SING,OrderStatusEnum::TAKE_FINISH]],
- ['status' => StatusEnum::ENABLED],
- ];
- return self::actionOrderProcess('complete', 'order.can_after_sale_time', $where);
- // $exite_code = self::actionOrderProcess('confirm', 'order.auto_receiving_time', $where);
- // echo $exite_code;
- // return $exite_code;
- }
- /**
- * @name:
- * @msg:
- * @param {string} $action 操作类型,close:自动关闭订单,confirm:自动确认收货,complete:订单自动过售后
- * @param {string} $config_key 相应操作时间间隔配置名称
- * @param {int} $order_status 需要进行处理的订单的状态
- * @return {int}
- */
- private static function actionOrderProcess(string $action, string $config_key, array $order_wheres = null) : int
- {
- $action_info_map = [
- 'close' => [
- 'task_name' => '自动关闭订单',
- 'time_radix' => 60, // 时间节点计算的基数,自动关闭订单时间单位是分钟,转换为秒的基数是60
- 'method' => 'close',
- 'time_field' => 'created_at',
- 'default' => 30, // 自动关闭订单默认时间 30 分钟
- ],
- 'confirm' => [
- 'task_name' => '自动确认收货',
- 'time_radix' => 86400, // 时间节点计算的基数,订单自动确认收货的时间单位是天,转换为秒的基数是86400
- 'method' => 'takeDelivery',
- 'time_field' => 'consign_time',
- 'default' => 7, // 自动确认收货默认时间 7 天
- ],
- 'complete' => [
- 'task_name' => '自动过售后',
- 'time_radix' => 86400, // 时间节点计算的基数,订单自动自动过售后的时间单位是天,转换为秒的基数是86400
- 'method' => 'complete',
- 'time_field' => 'sign_time',
- 'default' => 7, // 自动过售后默认时间 7 天
- ],
- ];
- // var_dump($action_info_map);
- try {
- $mall_list = Mall::getEnableMallList(true);
- $mall_ids = array_column($mall_list, 'id');
- // var_dump($mall_ids);
- if (empty($mall_ids)) {
- echo '没有可用商城,不做处理 ... ' . PHP_EOL;
- return ExitCode::UNSPECIFIED_ERROR;
- }
- foreach ($mall_ids as $mall_id) {
- // 订单自动处理时间
- $order_auto_handle_time = \Yii::$app->services->setting->mall->get($mall_id, $config_key);
- // 如果自动处理时间没有配置,则取默认值 优化1459调整订单售后时间可以调整为设置0
- if (empty($order_auto_handle_time) && $order_auto_handle_time != 0) $order_auto_handle_time = $action_info_map[$action]['default'];
- // 订单自动处理时间节点,延后两分钟处理,避免支付回调尚未回来订单已经被取消s
- $time_node = time() - $order_auto_handle_time * $action_info_map[$action]['time_radix'] - 120;
- $time_field = $action_info_map[$action]['time_field'];
- $where = [
- 'and',
- ['mall_id' => $mall_id],
- ['<', $time_field, $time_node]
- ];
- if (!empty($order_wheres)) {
- foreach ($order_wheres as $order_where) {
- array_push($where, $order_where);
- }
- }
- // var_dump($where);exit;
- $select = 'id,mall_id,user_id,pay_money,marketing_type';
- foreach (Order::find()->select($select)->where($where)->each(500) as $order) {
- $is_continue = false;
- $goods_num = 0;
- if ('confirm' == $action && !empty($order->detail)) {
- foreach ($order->detail as $detail) {
- if ($detail->is_feedback == 1) {
- echo ' 订单[ID:' . $order->id . ', DETAIL-ID:' . $detail->id . ']是售后订单,不执行自动确认收货。' . PHP_EOL;
- $is_continue = true;
- break;
- }
- $goods_num += $detail->num;
- }
- }
- // 如果有在售后中的订单,则直接跳过不处理
- if ($is_continue) continue;
- $invoke_arr = GlobalInvoke::exec(GlobalInvoke::CONSOLE_ORDER_DETAIL, $mall_id, [
- 'order' => $order,
- 'action' => $action,
- 'mall_id' => $mall_id,
- ]);
- $addons_name = $order->marketing_type;
- if($addons_name && isset($invoke_arr[$addons_name])){
- $invoke_result = $invoke_arr[$addons_name];
- $res = isset($invoke_result[$action]) && $invoke_result[$action] === false;
- if($res) continue;
- }
- $data = [
- 'id' => $order->id,
- 'user_id' => $order->user_id,
- 'mall_id' => $mall_id,
- 'operator_id' => 0,
- 'operator_name' => '系统',
- 'is_system_auto' => 1
- ];
- $method = $action_info_map[$action]['method'];
- if (!Order::$method($data)) {
- echo ' 订单[ID:' . $order->id . ']' . $action_info_map[$action]['task_name'] . '失败, errmsg: ' . Order::getStaticError() . PHP_EOL;
- continue;
- }
- if ($action == 'close') {
- //用户数据统计
- StatisticsHandle::execute('order_close', ['id' => $order->id]);
- }
- echo '订单[ID:' . $order->id . ']' . $action_info_map[$action]['task_name'] . '完成' . PHP_EOL;
- // exit;
- }
- }
- return ExitCode::OK;
- } catch (\Exception $e) {
- echo ' 订单' . $action_info_map[$action]['task_name'] . ' 任务处理出错:' . $e->getMessage() . PHP_EOL;
- return ExitCode::UNSPECIFIED_ERROR;
- }
- }
- }
|