<?php

namespace App\Models;

use App\Entities\HPILOEntity;

class HPILOModel extends CommonModel
{
	protected $table            = 'tw_hpilo';
	// protected $primaryKey       = 'uid';
	// protected $useAutoIncrement = true;
	protected $allowedFields    = ['customer', 'ip', 'port', 'id', 'passwd', 'model', 'processor', 'memory', 'health', 'power', 'detail', 'status', 'created_at'];
	protected $validationRules      = [
		'uid'               => 'if_exist|numeric',
		'customer'          => 'if_exist|string',
		'id'                => 'if_exist|string',
		'passwd'            => 'if_exist|string',
		'ip'                => 'if_exist|string',
		'port'              => 'if_exist|numeric',
		'model'             => 'if_exist|string',
		'processor'         => 'if_exist|string',
		'memory'            => 'if_exist|numeric',
		'health'            => 'if_exist|string',
		'power'             => 'if_exist|string',
		'detail'            => 'if_exist|string',
		'status'            => 'if_exist|in_list[use,unuse]',
		'updated_at'        => 'if_exist|valid_date',
		'created_at'        => 'if_exist|valid_date',
	];
	public function getEntityByField($field, $value): ?HPILOEntity
	{
		return $this->asObject(HPILOEntity::class)->where($field, $value)->first();
	}
	public function getEntity(int $uid): ?HPILOEntity
	{
		return $this->getEntityByField($this->primaryKey, $uid);
	}
	public function create(array $datas): HPILOEntity
	{
		return $this->create_process(new HPILOEntity($datas));
	}
	public function modify(HPILOEntity $entity, array $datas): HPILOEntity
	{
		foreach ($datas as $field => $value) {
			if ($entity->$field != $datas[$field]) {
				$entity->$field =  $value;
			}
		}
		return $this->modify_process($entity);
	}

	//Index관련
	public function setIndexWordFilter(string $word)
	{
		parent::setIndexWordFilter($word);
		$this->orLike('customer', $word, 'both');
	}
	public function setIndexOrderBy($field, $order = 'ASC')
	{
		$this->orderBy("health", "ASC");
		parent::setIndexOrderBy($field, $order);
	}
}
