<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Model;

/**
 * @property $id 主键ID
 * @property $user_sn  客户编码
 * @property $name  用户名
 * @property $email  邮箱
 * @property $email_verified_at  邮箱校验时间
 * @property $password  密码
 * @property $phone  手机号码
 * @property $remark  备注
 * @property $reg_source  注册来源 1网站 2人工新增
 * @property $remember_token  记住登陆token
 * @property $account_properties  账号属性,1是个人,2是企业
 * @property $status  状态,1是正常,-1是禁用
 * @property $company_name  公司名称
 * @property $first_name  名字
 * @property $sale_id  业务员
 * @property $sale_name  业务员名字
 * @property $last_name  姓氏
 * @property $created_time  创建时间
 * @property $update_time  更新时间
 */
class User extends BaseModel
{
    use HasDateTimeFormatter;
    protected $table = 'users';
    public $timestamps = false;
    const STATUS_NORMAL = 1;
    const STATUS_DISABLE = -1;

    const REG_SOURCE_WEB = 1;
    const REG_SOURCE_MANUAL = 2;

    const ACCOUNT_PROPERTIES_PERSONAL = 1;
    const ACCOUNT_PROPERTIES_ENTERPRISE = 2;
    //查看权限
    public static $ruleViewList = [
        self::SEM_USER_VIEW_ALL, //查看所有
        self::SEM_USER_VIEW_SUB,//查看下级
    ];
    const SEM_USER_VIEW_ALL = "sem_user_viewAllList";//查看所有
    const SEM_USER_VIEW_SUB = "sem_user_viewSubList";//查看下级

    public static function insertData($data)
    {
        return self::insertGetId($data);
    }


    public static function updateById($id, $update)
    {
        return self::where("id", $id)->update($update);
    }


    public static function getInfoByUserId($userId)
    {
        $res = self::where('id', $userId)->first();
        return ($res) ? $res->toArray() : [];
    }

    // 批量获取用户信息
    public static function getInfoByUserIds($userId)
    {
        return self::whereIn('id', $userId)->get()->keyBy('id')->toArray();
    }

    public static function getListByIdArr($userIdArr)
    {
        $res = self::wherein('id', $userIdArr)->get();
        return ($res) ? $res->toArray() : [];
    }


    public static function updateByIdArr($idArr, $update)
    {
        return self::whereIn("id", $idArr)->update($update);
    }

    public function user_address()
    {
        return $this->hasMany(UserAddress::class, 'user_id', 'id');
    }


}