<commit_before><?php

class Users_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 用户登录
     *
     * @param array $data
     *
     * @return bool
     */
    public function login(array $data): bool
    {
        $res = $this->db
            ->select('id,name,mail')
            ->from('users_base')
            ->where(['mail' => $data['mail'], 'password' => md5($data['password'])])
            ->get();

        if ($res->num_rows()) {
            $info       = $res->result();
            $this->id   = $info[0]->id;
            $this->name = $info[0]->name;
            $this->mail = $info[0]->mail;

            return true;
        } else {
            return false;
        }

    }

    /**
     * 用户注册
     *
     * @param array $data
     *
     * @return string
     */
    public function register(array $data): string
    {
        if ($this->emailExists($data['mail'])) {
            return '该邮箱已被注册';
        }
        if ($this->usernameExists($data['name'])) {
            return '用户名已被占用';
        }

        $this->db->trans_start();
        $this->db->insert(
            'users_base',
            [
                'mail'     => $data['mail'],
                'name'     => $data['name'],
                'password' => <PASSWORD>($data['password'])
            ]);
        $this->db->insert(
            'avatar_url',
            [
                'url' => 'http://7xlx4u.com1.z0.glb.clouddn.com/o_1aqt96pink2kvkhj13111r15tr7.jpg?imageView2/1/w/100/h/100'
            ]);
        $this->db->insert(
            'users_detail',
            [
                'id'       => $this->db->insert_id(),
                'sex'      => 3,
                'birthday' => '1970-01-01'
            ]);

        $this->mail     = $data['mail'];
        $this->name     = $data['name'];
        $this->password = $data['password'];
        $this->id       = $this->db->insert_id();

        @$this->db->trans_complete();

        if ($this->db->trans_status()) {
            return '';
        } else {
            return '注册失败';
        }
    }

    /**
     * 获取用户信息
     *
     * @param int $id
     *
     * @return array
     */
    public function getUserInfo(int $id): array
    {
        $res = $this->db
            ->select('users_base.id,url,mail,name,sex,birthday')
            ->from('users_base')
            ->join('avatar_url', 'users_base.id = avatar_url.user_id', 'left')
            ->join('users_detail', 'users_base.id = users_detail.id', 'left')
            ->where(['users_base.id' => $id])
            ->get();

        $result = $res->result()[0];
//        switch ($result->sex){
//            case '0':
//                $result->sex = 'male';
//                break;
//            case '1':
//                $result->sex = 'female';
//                break;
//            case '2':
//                $result->sex = 'secrecy';
//                break;
//        }

        return
            [
                'id'         => $result->id,
                'avatar_url' => $result->url,
                'mail'       => $result->mail,
                'name'       => $result->name,
                'sex'        => $result->sex,
                'birthday'   => $result->birthday
            ];
    }

    /**
     * 修改用户信息<commit_msg>更正获取用户信息时返回的sex<commit_after><?php

class Users_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 用户登录
     *
     * @param array $data
     *
     * @return bool
     */
    public function login(array $data): bool
    {
        $res = $this->db
            ->select('id,name,mail')
            ->from('users_base')
            ->where(['mail' => $data['mail'], 'password' => md5($data['password'])])
            ->get();

        if ($res->num_rows()) {
            $info       = $res->result();
            $this->id   = $info[0]->id;
            $this->name = $info[0]->name;
            $this->mail = $info[0]->mail;

            return true;
        } else {
            return false;
        }

    }

    /**
     * 用户注册
     *
     * @param array $data
     *
     * @return string
     */
    public function register(array $data): string
    {
        if ($this->emailExists($data['mail'])) {
            return '该邮箱已被注册';
        }
        if ($this->usernameExists($data['name'])) {
            return '用户名已被占用';
        }

        $this->db->trans_start();
        $this->db->insert(
            'users_base',
            [
                'mail'     => $data['mail'],
                'name'     => $data['name'],
                'password' => <PASSWORD>($data['password'])
            ]);
        $this->db->insert(
            'avatar_url',
            [
                'url' => 'http://7xlx4u.com1.z0.glb.clouddn.com/o_1aqt96pink2kvkhj13111r15tr7.jpg?imageView2/1/w/100/h/100'
            ]);
        $this->db->insert(
            'users_detail',
            [
                'id'       => $this->db->insert_id(),
                'sex'      => 3,
                'birthday' => '1970-01-01'
            ]);

        $this->mail     = $data['mail'];
        $this->name     = $data['name'];
        $this->password = $data['password'];
        $this->id       = $this->db->insert_id();

        @$this->db->trans_complete();

        if ($this->db->trans_status()) {
            return '';
        } else {
            return '注册失败';
        }
    }

    /**
     * 获取用户信息
     *
     * @param int $id
     *
     * @return array
     */
    public function getUserInfo(int $id): array
    {
        $res = $this->db
            ->select('users_base.id,url,mail,name,sex_detail.sex AS sex,birthday')
            ->from('users_base')
            ->join('avatar_url', 'users_base.id = avatar_url.user_id', 'left')
            ->join('users_detail', 'users_base.id = users_detail.id', 'left')
            ->join('sex_detail', 'users_base.id = sex_detail.id', 'left')
            ->where(['users_base.id' => $id])
            ->get();

        $result = $res->result()[0];
//        switch ($result->sex){
//            case '0':
//                $result->sex = 'male';
//                break;
//            case '1':
//                $result->sex = 'female';
//                break;
//            case '2':
//                $result->sex = 'secrecy';
//                break;
//        }

        return
            [
                'id'         => $result->id,
                'avatar_url' => $result->url,
                'mail'       => $result->mail,
                'name'       => $result->name,
                'sex'        => $result->sex,
                'birthday'   => $result->birthday
            ];
    }

    /**
     * 修改用户信息
{'fraction_non_alphanumeric': 0.1800709639266706, 'fraction_numerical': 0.01212300413956239, 'mean_word_length': 1.2224047306176085, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 144, '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': '2535805', 'n_tokens_mistral': 2205, 'n_tokens_neox': 2124, 'n_words': 321}