<?php

namespace app\admin\model;

use support\Db;
use support\Model;

/**
 * 地块模型
 * Class Users
 * @package app\admin\model
 */
class LifeFarmLand extends Model
{
    const ROLE_STATUS = 1;

    const IS_DEL_YES = 1;
    const IS_DEL_NO  = 0;
    const GOODS_IS_SHOW = [
        self::IS_DEL_YES => '已删除',
        self::IS_DEL_NO  => '未删除'
    ];
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'life_farm_land';

    public $timestamps = false;

    /**
     * Notes:获取地块列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: YCP
     * Date: 2022/10/17
     */
    public static function getLandList(int $page, int $limit, string $keywords)
    {
        $list = static::select('*')
            ->where(['land_is_del'=>static::IS_DEL_NO])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('land_name', 'like', '%' . $keywords . '%');
            })
            ->orderBy('land_create_time','DESC')
            ->forPage($page, $limit)
            ->get();
        $count = static::where(['land_is_del'=>static::IS_DEL_NO])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('land_name', 'like', '%' . $keywords . '%');
            })
            ->count();
        return [$list, $count];
    }

    /**
     * Notes:获取地块列表
     * @return array
     * User: YCP
     * Date: 2022/10/17
     */
    public static function getLandAll()
    {
        $list = static::select(['land_id','land_name'])
            ->where(['land_is_del'=>static::IS_DEL_NO])
            ->orderBy('land_create_time','DESC')
            ->get();
        return $list;
    }

    /**
     * Notes:获取地块名称
     * @param string $account
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * User: YCP
     * Date: 2022/10/17
     */
    public static function landMation($admin_land)
    {
        $where = [];
        $where['land_is_del'] = static::IS_DEL_NO;
        $mation = static::where($where)
            ->when($admin_land != '', function ($query) use ($admin_land){
                $query->whereIn('land_id', $admin_land);
            })
            ->get('land_name');
        return $mation;
    }

    /**
     * Notes:检测地块名是否存在
     * @param $doctor_name
     * @param $doctor_mobile
     * @return bool
     * User: YCP
     * Date: 2022/10/17
     * 验证条件：地块名存在的情况下
     */
    public static function checkLandName($land_name)
    {
        return static::where(['land_name' => $land_name, 'land_is_del' => static::IS_DEL_NO])->exists();
    }

    /**
     * Notes:检测坐标是否存在
     * @param $doctor_name
     * @param $doctor_mobile
     * @return bool
     * User: YCP
     * Date: 2022/10/17
     * 验证条件：经度纬度同时存在的情况下
     */
    public static function checkLandPoint($land_longitude,$land_latitude)
    {
        return static::where(['land_longitude' => $land_longitude, 'land_latitude' => $land_latitude, 'land_is_del' => static::IS_DEL_NO])->exists();
    }

}
