<?php

namespace app\admin\model;

use support\Db;
use support\Model;

/**
 * 用户亲属模型
 * Class Users
 * @package app\admin\model
 */
class UserRelatives extends Model
{

    const IS_DEL_YES = 1;
    const IS_DEL_NO  = 0;
    const RELATIVES_IS_SHOW = [
        self::IS_DEL_YES => '已删除',
        self::IS_DEL_NO  => '未删除'
    ];

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

    public $timestamps = false;

    /**
     * Notes:获取会员亲属列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/11/11
     */
    public static function getrelativesList(int $page, int $limit, string $keywords)
    {
        $list = static::select(['user_relatives.*','user.user_real_name'])
            ->where(['relatives_del'=>static::IS_DEL_NO])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('relatives_user_name', 'like', '%' . $keywords . '%');
            })
            ->leftJoin('user','user_id','=','user_relatives.relatives_user_id')
            ->orderBy('relatives_add_time','DESC')
            ->forPage($page, $limit)
            ->get()
            ->toArray();
        $count = static::where(['relatives_del'=>static::IS_DEL_NO])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('relatives_user_name', 'like', '%' . $keywords . '%');
            })
            ->count();
        return [$list, $count];
    }

}
