<?php

namespace app\common\dao\shared;

use app\common\dao\BaseDao;
use app\common\model\BaseModel;
use app\common\model\shared\SharedProsperityGroupUser;
use think\db\exception\DbException;

class SharedProsperityGroupUserDao extends BaseDao
{

    /**
     * @return BaseModel
     */
    protected function getModel(): string
    {
        return SharedProsperityGroupUser::class;
    }

    /**
     * 获取按团队分组的有效用户ID列表
     * @return array [group_id => [user_id1, user_id2, ...]]
     */
    public function getValidUsersGroupByGroup(): array
    {
        try {
            $rows = $this->getModel()::where('status', 1)
                ->field('group_id, user_id')
                ->select()
                ->toArray();
            $grouped = [];
            foreach ($rows as $row) {
                $grouped[$row['group_id']][] = $row['user_id'];
            }
            return $grouped;
        } catch (\Exception $e) {
            return [];
        }
    }

    /**
     * @param $groupId
     * @return array
     * @author 史晨
     * @date 2026/2/27 11:31
     * 获取团队用户(分页)
     */
    public function getUserByLevel($groupId)
    {
        try {
            return $this->getModel()::where('status', 1)
                ->where(['group_id' => $groupId])
                ->paginate(20)
                ->toArray();
        } catch (DbException $e) {
            return [];
        }
    }
}
