OrderStatusEnum::NOT_PAY], ['status' => StatusEnum::ENABLED], ]; return self::actionOrderProcess('close', 'order.payment_expire_time', $where); } /** * 自动结束拼团订单 */ public function actionAutoFinish() { try { $mall_list = Mall::getEnableMallList(true); $mall_ids = array_column($mall_list, 'id'); if (empty($mall_ids)) { echo '没有可用商城,不做处理 ... ' . PHP_EOL; return ExitCode::UNSPECIFIED_ERROR; } foreach ($mall_ids as $mall_id) { if (MallAddons::isInstall($mall_id, GroupBuyEnum::ADDONS_NAME) == 0) continue; $where = [ 'mall_id' => $mall_id, 'order_status' => GroupBuyEnum::JOINING, 'is_commander' => 1, ]; foreach (GroupBuyOrder::find()->where($where)->each(500) as $group_buy_order) { $active = GroupBuyActive::findOne(['mall_id' => $group_buy_order->mall_id, 'id' => $group_buy_order->group_buy_active_id]); if (empty($active)) continue; $effective_time = $active->effective_time * 60; if ((time() - $group_buy_order->created_at) > $effective_time) { if ($active->virtual_group_type == 3) { // 开启了虚拟成团 ActiveFinish::handle($mall_id, $group_buy_order['open_group_id'], $active); // 这个状态下的拼团默认成功 continue; } GroupBuyOrder::autoFinish(['order_id'=>$group_buy_order['id'],'open_group_id'=>$group_buy_order['open_group_id']]); } } } return ExitCode::OK; } catch (\Exception $e) { echo '拼团订单,执行拼团结束失败任务处理出错:' . $e->getMessage() . PHP_EOL; return ExitCode::UNSPECIFIED_ERROR; } } /** * @desc:自动结束过期拼团活动 * @Author: hua * @Date: 2021/12/16 * @Time: 8:57 * @Copyright: copyright (c) 2021 广东七件事集团 */ public function actionAutoFinishActive() { try { $mall_list = Mall::getEnableMallList(true); $mall_ids = array_column($mall_list, 'id'); if (empty($mall_ids)) { echo '没有可用商城,不做处理 ... ' . PHP_EOL; return ExitCode::UNSPECIFIED_ERROR; } foreach ($mall_ids as $mall_id) { if (MallAddons::isInstall($mall_id, GroupBuyEnum::ADDONS_NAME) == 0) continue; //可能活动状态已经变成 结束,但是订单没有修改状态 $active_list = GroupBuyActive::findAll(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'group_status' => [GroupBuyEnum::OPEN_GROUP,GroupBuyEnum::GROUP_FINISH]]); if ($active_list) { foreach ($active_list as $active) { if ($active->end_at > time()) continue; $where = [ 'mall_id' => $mall_id, 'group_buy_active_id' => $active->id, 'order_status' => GroupBuyEnum::JOINING, 'is_commander' => 1, ]; foreach (GroupBuyOrder::find()->where($where)->each(500) as $group_buy_order) { $effective_time = $active->effective_time * 60; if ((time() - $group_buy_order->created_at) > $effective_time) { if ($group_buy_order->is_pay) { if ($active->virtual_group_type == 3) { // 开启了虚拟成团 ActiveFinish::handle($group_buy_order->mall_id, $group_buy_order->open_group_id, $active); // 这个状态下的拼团默认成功 continue; } //已过期,拼团要结束 $res = GroupBuyOrder::finishGroupBuyOrder($group_buy_order->mall_id, $group_buy_order->open_group_id); if ($res == false) { echo ' 拼团订单[ID:' . $group_buy_order->id . ']过期,执行拼团结束失败, errmsg: ' . GroupBuyOrder::getStaticError() . PHP_EOL; continue; } } } } $active->group_status = GroupBuyEnum::GROUP_FINISH; $res = $active->save(); $params['mall_id'] = $mall_id; $params['marketing_id'] = $active['id']; $params['marketing_type'] = GroupBuyEnum::ADDONS_NAME; $params['status'] = StatusEnum::DELETE; GoodsMarketing::setData($params); } } } return ExitCode::OK; } catch (\Exception $e) { echo '拼团订单,执行拼团结束失败任务处理出错:' . $e->getMessage() . PHP_EOL; return ExitCode::UNSPECIFIED_ERROR; } } /** * @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 分钟 ], ]; try { $mall_list = Mall::getEnableMallList(true); $mall_ids = array_column($mall_list, 'id'); if (empty($mall_ids)) { echo '没有可用商城,不做处理 ... ' . PHP_EOL; return ExitCode::UNSPECIFIED_ERROR; } foreach ($mall_ids as $mall_id) { // 判断用户插件是否安装 if (MallAddons::isInstall($mall_id, GroupBuyEnum::ADDONS_NAME) == 0) continue; // 订单自动处理时间 $order_auto_handle_time = \Yii::$app->services->setting->mall->get($mall_id, $config_key); // 如果自动处理时间没有配置,则取默认值 if (empty($order_auto_handle_time)) $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); } } $select = 'id,user_id'; foreach (GroupBuyOrder::find()->select($select)->where($where)->each(500) as $order) { $data = [ 'id' => $order->id, 'user_id' => $order->user_id, 'mall_id' => $mall_id, 'operator_id' => 0, 'operator_name' => '系统', ]; $method = $action_info_map[$action]['method']; if (!GroupBuyOrder::$method($data)) { echo ' 拼团订单[ID:' . $order->id . ']' . $action_info_map[$action]['task_name'] . '失败, errmsg: ' . Order::getStaticError() . PHP_EOL; continue; } } } return ExitCode::OK; } catch (\Exception $e) { echo ' 拼团订单' . $action_info_map[$action]['task_name'] . ' 任务处理出错:' . $e->getMessage() . PHP_EOL; return ExitCode::UNSPECIFIED_ERROR; } } }