<?php

namespace app\admin\model;

use support\Db;
use support\Model;

/**
 * 管家模型
 * Class Housekeeper
 * @package app\admin\model
 */
class Housekeeper extends Model
{
    const UPDATED_AT = 'housekeeper_update_time';//模型自动更新时间字段

    const DEL_NO  = 0;
    const DEL_YES = 1;
    const DEL_STATUS = [
        self::DEL_NO  => '未删除',
        self::DEL_YES => '已删除'
    ];
    /**
     *
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'housekeeper';

    protected $dateFormat = 'U';//操作数据库时间格式 时间戳

    /**
     * Notes:获取管家列表
     * @param $keywords
     * @param $page
     * @param $limit
     * @return array
     * User: yym
     * Date: 2022/10/28
     */
    public static function getHousekeeperList($keywords, $page, $limit)
    {
        $list = static::where(['housekeeper_del' => static::DEL_NO])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('housekeeper_name', 'like', '%' . $keywords . '%')
                    ->orWhere('housekeeper_real_name', 'like', '%' . $keywords . '%')
                    ->orWhere('housekeeper_phone', 'like', '%' . $keywords . '%');
            })
            ->forPage($page, $limit)
            ->get()
            ->toArray();
        $count = static::where(['housekeeper_del' => static::DEL_NO])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('housekeeper_name', 'like', '%' . $keywords . '%')
                    ->orWhere('housekeeper_real_name', 'like', '%' . $keywords . '%')
                    ->orWhere('housekeeper_phone', 'like', '%' . $keywords . '%');
            })
            ->count();
        return [$list, $count];
    }

    /**
     * Notes:检测账号是否存在
     * @param $account
     * @return bool
     * User: yym
     * Date: 2022/10/28
     */
    public static function checkAccount($account)
    {
        return static::where(['housekeeper_name' => $account, 'housekeeper_del' => static::DEL_NO])->exists();
    }

    /**
     * Notes:检测账号是否存在
     * @param $account
     * @param $id
     * @return bool
     * User: yym
     * Date: 2022/10/28
     */
    public static function checkAccountNo($account, $id)
    {
        return static::where(['housekeeper_name' => $account, 'housekeeper_del' => static::DEL_NO])->where('housekeeper_id', '!=', $id)->exists();
    }

    /**
     * Notes:插入数据
     * @param array $data
     * @return false|int
     * User: yym
     * Date: 2022/10/28
     */
    public static function insertData(array $data)
    {
        if(empty($data))
        {
            return false;
        }
        return static::insertGetId($data);
    }

    /**
     * Notes:根据主键更新数据
     * @param array $data
     * @param int $id
     * @return false|int
     * User: yym
     * Date: 2022/10/28
     */
    public static function updateData(array $data, int $id)
    {
        if(empty($data) || empty($id))
        {
            return false;
        }
        return static::where(['housekeeper_id' => $id])->update($data);
    }

    /**
     * Notes:获取详情
     * @param int $id
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * User: yym
     * Date: 2022/10/28
     */
    public static function getInfo(int $id)
    {
        return static::where(['housekeeper_id' => $id, 'housekeeper_del' => static::DEL_NO])->first();
    }
}
