<commit_before><?php

namespace Miaoxing\User\Controller\Admin;

use Miaoxing\Plugin\Service\User as UserService;

class User extends \Miaoxing\Plugin\BaseController
{
    protected $controllerName = '用户管理';

    protected $actionPermissions = [
        'index' => '列表',
        'edit,update,moveGroup' => '编辑',
        'destroy' => '删除',
        'show' => '查看',
    ];

    protected $guestPages = [
        'admin/user/keepLogin',
    ];

    /**
     * 展示用户列表
     */
    public function indexAction($req)
    {
        $userTags = wei()->userTag->getAll();

        switch ($req['_format']) {
            case 'json':
            case 'csv':
                $users = wei()->user();
                $users->select('user.*');

                // 分页
                $users->limit($req['rows'])->page($req['page']);

                // 排序
                $users->desc('user.id');

                // 用户筛选
                $this->event->trigger('preUserSearch', [$users, $req]);

                // 根据关注状态显示用户
                switch (true) {
                    case $req['isValid'] === '1':
                        $users->andWhere(['user.isValid' => true]);
                        break;

                    case $req['isValid'] === '0':
                        $users->andWhere(['user.isValid' => false]);
                        break;
                }

                // 平台筛选
                // TODO 如何移到插件中
                if ($req['platform'] == 'self') {
                    $users->andWhere([
                        'wechatOpenId' => '',
                    ]);
                }

                // 分组筛选
                if (wei()->isPresent($req['groupId'])) {
                    $users->andWhere('user.groupId = ?', (string) $req['groupId']);
                }

                // 昵称,姓名
                foreach (['nickName', 'name'] as $field) {
                    if (isset($req[$field])) {
                        $users->andWhere('user.' . $field . ' LIKE ?', '%' . $req[$field] . '%');
                    }
                }

                // 手机号单独查询
                if (isset($req['mobile'])) {
                    $users->andWhere('user.mobile LIKE ?', $req['mobile'] . '%');
                }

                // 兼容老的搜索,如userPicker,待移除
                if ($req['search']) {
                    $users->andWhere(
                        '(user.nickName LIKE ?) OR (user.mobile = ?)',
                        ['%' . $req['search'] . '%', $req['search']]
                    );
                }

                if ($req['filter_empty'] && in_array($req['filter_empty'], $users->getFields())) {
                    $users->andWhere($req['filter_empty'] . " != ''");
                }

                if ($req['tagIds']) {
                    $users
                        ->select('DISTINCT user.*')
                        ->leftJoin('app.user_tags_users', 'user_tags_users.user_id = user.id')
                        ->andWhere(['user_tags_users.tag_id' => explode(',', $req['tagIds'])])
                        ->groupBy('user.id');
                }

                // 导出用户限制
                if ($req['format'] == 'csv' && $users->count() > 10000) {
                    return $this->err('导出用户数超过1W，请联系开发人员后台导出！');
                }

                if ($req['startLastPaidTime']) {
                    $users->andWhere('lastPaidTime >= ?', $req['startLastPaidTime']);
                }

                if ($req['endLastPaidTime']) {
                    $users->andWhere('lastPaidTime <= ?', $req['endLastPaidTime'] . '23:59:59');
                }

                // 触发查找前事件
                $this->event->trigger('preAdminUserListFind', [$req, $users]);

                $data = [];
                foreach ($users->findAll() as $user) {
                    $weChatQrcode = wei()->weChatQrcode()->where('sceneId=?', $user['source'])->find();
                    $source = '';
                    if ($weChatQrcode) {
                        $source = $weChatQrcode->getUser();
                    }

                    $data[] = $user->toArray() + [
                            'sourceUser' => $source ? $source->toArray() : '',
                            'wechat_qrcode' => $weChatQrcode,
                            'group' => $user->getGroup(),
                            'tags' => $user->getTags(),
                        ];
                }

                if ($req['_format'] == 'csv') {
                    return $this->renderCsv($data);
                } else {
                    return $this->suc([
                        'data' => $data,
                        'page' => (int) $req['page'],
                        'rows' => (int) $req['rows'],
                        'records' => $users->count(),
                    ]);
                }

            default:
                $groups = wei()->group()->findAll()->withUngroup();

                $tags = [];
                foreach ($userTags as $userTag) {
                    $tags[] = ['id' => $userTag->id, 'text' => $userTag->name];
                }

                // 获取用户相关平台
                $platforms[] = [
                    'name' => '直接注册',
                    'value' => 'self',
                ];
                $this->event->trigger('userGetPlatform', [&$platforms]);

                return get_defined_vars();
        }
    }

    public function editAction($req)
    {
        $user = wei()->user()->findId($req['id']);

        return get_defined_vars();
    }<commit_msg>解决1对多count不正确<commit_after><?php

namespace Miaoxing\User\Controller\Admin;

use Miaoxing\Plugin\Service\User as UserService;

class User extends \Miaoxing\Plugin\BaseController
{
    protected $controllerName = '用户管理';

    protected $actionPermissions = [
        'index' => '列表',
        'edit,update,moveGroup' => '编辑',
        'destroy' => '删除',
        'show' => '查看',
    ];

    protected $guestPages = [
        'admin/user/keepLogin',
    ];

    /**
     * 展示用户列表
     */
    public function indexAction($req)
    {
        $userTags = wei()->userTag->getAll();

        switch ($req['_format']) {
            case 'json':
            case 'csv':
                $users = wei()->user();
                $users->select('user.*');

                // 分页
                $users->limit($req['rows'])->page($req['page']);

                // 排序
                $users->desc('user.id');

                // 用户筛选
                $this->event->trigger('preUserSearch', [$users, $req]);

                // 根据关注状态显示用户
                switch (true) {
                    case $req['isValid'] === '1':
                        $users->andWhere(['user.isValid' => true]);
                        break;

                    case $req['isValid'] === '0':
                        $users->andWhere(['user.isValid' => false]);
                        break;
                }

                // 平台筛选
                // TODO 如何移到插件中
                if ($req['platform'] == 'self') {
                    $users->andWhere([
                        'wechatOpenId' => '',
                    ]);
                }

                // 分组筛选
                if (wei()->isPresent($req['groupId'])) {
                    $users->andWhere('user.groupId = ?', (string) $req['groupId']);
                }

                // 昵称,姓名
                foreach (['nickName', 'name'] as $field) {
                    if (isset($req[$field])) {
                        $users->andWhere('user.' . $field . ' LIKE ?', '%' . $req[$field] . '%');
                    }
                }

                // 手机号单独查询
                if (isset($req['mobile'])) {
                    $users->andWhere('user.mobile LIKE ?', $req['mobile'] . '%');
                }

                // 兼容老的搜索,如userPicker,待移除
                if ($req['search']) {
                    $users->andWhere(
                        '(user.nickName LIKE ?) OR (user.mobile = ?)',
                        ['%' . $req['search'] . '%', $req['search']]
                    );
                }

                if ($req['filter_empty'] && in_array($req['filter_empty'], $users->getFields())) {
                    $users->andWhere($req['filter_empty'] . " != ''");
                }

                if ($req['tagIds']) {
                    $users
                        ->select('DISTINCT user.*')
                        ->leftJoin('app.user_tags_users', 'user_tags_users.user_id = user.id')
                        ->andWhere(['user_tags_users.tag_id' => explode(',', $req['tagIds'])]);
                }

                // 导出用户限制
                if ($req['format'] == 'csv' && $users->count() > 10000) {
                    return $this->err('导出用户数超过1W，请联系开发人员后台导出！');
                }

                if ($req['startLastPaidTime']) {
                    $users->andWhere('lastPaidTime >= ?', $req['startLastPaidTime']);
                }

                if ($req['endLastPaidTime']) {
                    $users->andWhere('lastPaidTime <= ?', $req['endLastPaidTime'] . '23:59:59');
                }

                // 触发查找前事件
                $this->event->trigger('preAdminUserListFind', [$req, $users]);

                $data = [];
                foreach ($users->findAll() as $user) {
                    $weChatQrcode = wei()->weChatQrcode()->where('sceneId=?', $user['source'])->find();
                    $source = '';
                    if ($weChatQrcode) {
                        $source = $weChatQrcode->getUser();
                    }

                    $data[] = $user->toArray() + [
                            'sourceUser' => $source ? $source->toArray() : '',
                            'wechat_qrcode' => $weChatQrcode,
                            'group' => $user->getGroup(),
                            'tags' => $user->getTags(),
                        ];
                }

                if ($req['_format'] == 'csv') {
                    return $this->renderCsv($data);
                } else {
                    return $this->suc([
                        'data' => $data,
                        'page' => (int) $req['page'],
                        'rows' => (int) $req['rows'],
                        'records' => $users->count(false, 'DISTINCT user.id'),
                    ]);
                }

            default:
                $groups = wei()->group()->findAll()->withUngroup();

                $tags = [];
                foreach ($userTags as $userTag) {
                    $tags[] = ['id' => $userTag->id, 'text' => $userTag->name];
                }

                // 获取用户相关平台
                $platforms[] = [
                    'name' => '直接注册',
                    'value' => 'self',
                ];
                $this->event->trigger('userGetPlatform', [&$platforms]);

                return get_defined_vars();
        }
    }

    public function editAction($req)
    {
        $user = wei()->user()->findId($req['id']);

        return get_defined_vars();
    }
{'fraction_non_alphanumeric': 0.172523670793882, 'fraction_numerical': 0.002640203932993445, 'mean_word_length': 1.0991782916109307, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 146, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6693235', 'n_tokens_mistral': 3261, 'n_tokens_neox': 3045, 'n_words': 435}