<?php

namespace app\admin\model;

use app\admin\model\UserAuth;
use support\Model;

/**
 * 悦享家用户信息模型
 * Class Users
 * @package app\admin\model
 */
class User extends Model
{
    const UPDATED_AT = 'user_update_time';

    const REAL_NO  = '0';
    const REAL_YES = '1';
    const REAL_STATUS = [
        self::REAL_NO  => '未认证',
        self::REAL_YES => '已认证'
    ];

    const PACKAGE_NO  = '0';
    const PACKAGE_YES = '1';
    const PACKAGE_STATUS = [
        self::PACKAGE_NO  => '未购买',
        self::PACKAGE_YES => '已购买'
    ];

    const TOURIST  = '0';
    const ORDINARY = '1';
    const FORMAL   = '2';
    const INSIDE   = '3';
    const USER_CATEGORY = [
        self::TOURIST  => '游客会员',
        self::ORDINARY => '普通会员',
        self::FORMAL   => '正式会员',
        self::INSIDE   => '内部会员'
    ];

    const NO_OWNER = '0';
    const PROSPECTIVE_OWNER = '1';
    const YES_OWNER = '2';
    const USER_OWNER = [
        self::NO_OWNER => '非业主',
        self::PROSPECTIVE_OWNER => '准业主',
        self::YES_OWNER => '业主'
    ];

    protected $dateFormat = 'U';
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'user';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'user_id';


    /**
     * Notes:MembersAuth关联模型
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * User: yym
     * Date: 2022/8/4
     */
    public function member_auth_list()
    {
        return $this->hasMany(UserAuth::class, 'auth_user_id', 'user_id');
    }

    /**
     * Notes:关联未审核信息
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     * User: yym
     * Date: 2022/8/4
     */
    public function member_auth()
    {
        return $this->hasOne(UserAuth::class, 'auth_user_id', 'user_id')->whereIn('auth_status', [UserAuth::WAIT, UserAuth::RETURN, UserAuth::CANCEL]);
    }

    /**
     * Notes:一对多关联会员权益包
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * User: yym
     * Date: 2022/8/5
     */
    public function members_package()
    {
        return $this->hasMany(UserPackageLog::class, 'log_user_id', 'user_id')->whereIn('log_package_status', [UserPackageLog::NORMAL]);
    }

    /**
     * Notes:Users插入会员
     * @param int $mobile
     * @return int
     * User: yym
     * Date: 2022/7/25
     */
    public static function insertUsers(int $mobile): int
    {
        $insert = array(
            'user_name' => $mobile,
            'user_category' => 'MEMBER',
            'user_addtimes' => time()
        );
        return static::insertGetId($insert);
    }

    /**
     * Notes:获取会员信息
     * @param int $user_id
     * @return object|null
     * User: yym
     * Date: 2022/7/26
     */
    public static function getUserInfo(int $user_id)
    {
        return static::where(['user.user_id' => $user_id])
            ->select('user.*')
            ->with(['member_auth_list', 'member_auth', 'members_package' => function($query){
                $query->join('package', 'package_id', '=', 'user_package_log.log_package_id');
            }])
            ->first();
    }

    /**
     * Notes:获取会员列表
     * @param string $keywords
     * @param int $category
     * @param int $status
     * @param int $page
     * @param int $limit
     * @return array
     * User: yym
     * Date: 2022/8/4
     */
    public static function getUserList(string $keywords, int $category, int $status, int $page, int $limit)
    {
        $list = static::where('user.user_id', '>', 0)
            ->select('user.*')
            ->with(['member_auth_list', 'member_auth', 'members_package' => function($query){
                $query->select('user_package_log.*', 'package.*', 'package_price as package_money')->join('package', 'package_id', '=', 'user_package_log.log_package_id');
            }])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where(function ($query1) use ($keywords){
                    $query1->where('user.user_account', 'like', '%' . $keywords . '%')
                        ->orWhere('user.user_phone', 'like', '%' . $keywords . '%')
                        ->orWhere('user.user_real_name', 'like', '%' . $keywords . '%');
                });
            })
            ->when($category !== '', function ($query) use ($category){
                $query->where('user.user_type', $category);
            })
            ->when($status != '', function ($query) use ($status){
                $ids = UserAuth::getListIds($status);
                $member_ids = array(0);
                if(!empty($ids))
                {
                    $member_ids = array_unique(array_column($ids, 'auth_user_id'));
                }
                $query->whereIn('user.user_id', $member_ids);
            })
            ->orderBy('user.user_create_time', 'desc')
            ->forPage($page, $limit)
            ->get()
            ->toArray();
        $count = static::where('user.user_id', '>', 0)
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where(function ($query1) use ($keywords){
                    $query1->where('user.user_account', 'like', '%' . $keywords . '%')
                        ->orWhere('user.user_phone', 'like', '%' . $keywords . '%')
                        ->orWhere('user.user_real_name', 'like', '%' . $keywords . '%');
                });
            })
            ->when($category !== '', function ($query) use ($category){
                $query->where('user.user_type', $category);
            })
            ->when($status != '', function ($query) use ($status){
                $ids = UserAuth::getListIds($status);
                $member_ids = array(0);
                if(!empty($ids))
                {
                    $member_ids = array_unique(array_column($ids, 'auth_user_id'));
                }
                $query->whereIn('user.user_id', $member_ids);
            })
            ->count();
        return [$list, $count];
    }

    /**
     * Notes:根据条件搜索会员列表
     * @param string $keywords
     * @param string $field
     * @return array
     * User: yym
     * Date: 2022/8/12
     */
    public static function getUserListKeywords(string $keywords, string $field)
    {
        return static::select($field)
            ->leftJoin('members', 'member_id', '=', 'users.user_id')
            ->where('members.member_name', 'like', '%' . $keywords . '%')
            ->orWhere('members.member_mobile', 'like', '%' . $keywords . '%')
            ->orWhere('users.user_name', 'like', '%' . $keywords . '%')
            ->get()
            ->toArray();
    }

    /**
     * Notes:根据主键更新会员信息
     * @param int $member_id
     * @param array $update
     * @return false|int
     * User: yym
     * Date: 2022/7/28
     */
    public static function updateMember(int $member_id, array $update)
    {
        if(empty($update) || $member_id <= 0)
        {
            return false;
        }
        return static::where(['user_id' => $member_id])->update($update);
    }

    /**
     * Notes:获取用户信息
     * @param int $member_id
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * User: yym
     * Date: 2022/9/14
     */
    public static function getMemberInfo(int $member_id)
    {
        return static::where(['user_id' => $member_id])->first();
    }

    /**
     * Notes:用户账号唯一判断
     * @param int $user_account
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * User: ZQ
     * Date: 2022/11/21
     */
    public static function getUserAccount($user_account)
    {
        return static::where(['user_account' => $user_account])->first();
    }

    /**
     * Notes:用户身份证号唯一判断
     * @param int $user_account
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * User: ZQ
     * Date: 2022/11/21
     */
    public static function getUserCard($user_card_id)
    {
        return static::where(['user_card_id' => $user_card_id])->first();
    }

}
