Files
hyperf_service/app/Model/AdminUser.php
2025-08-05 15:55:27 +08:00

151 lines
3.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Model;
use App\Constants\Admin\UserCode;
use Hyperf\Database\Model\Builder;
use Hyperf\DbConnection\Model\Model;
/**
* @property int $id
* @property int $bind_user_id
* @property string $username
* @property string $password
* @property string $salt
* @property int $avatar
* @property string $chinese_name
* @property string $mobile
* @property int $status
* @property string $last_login_ip
* @property string $last_login_time
* @property int $is_del
* @property int $role_id
* @property int $section_id
* @property int $city_id
* @property string $create_time
* @property string $update_time
*/
class AdminUser extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'admin_user';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = [];
protected array $guarded = [];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['id' => 'integer', 'avatar' => 'integer', 'status' => 'integer', 'is_del' => 'integer', 'role_id' => 'integer','bind_user_id' => 'integer'];
const CREATED_AT = 'create_time';
const UPDATED_AT = 'update_time';
/**
* @param string $account
* @return Builder|\Hyperf\Database\Model\Model|null
*/
public function getAdminInfoByAccount(string $account): \Hyperf\Database\Model\Model|Builder|null
{
return $this->where('username', $account)->where('is_del',UserCode::IS_NO_DEL)->first();
}
/**
* @param string $name
* @return \Hyperf\Database\Model\Model|Builder|null
*/
public function getAdminInfoByName(string $name): \Hyperf\Database\Model\Model|Builder|null
{
return $this->where('chinese_name', $name)->where('is_del',UserCode::IS_NO_DEL)->first();
}
/**
* @param int $id
* @return \Hyperf\Database\Model\Model|null
*/
public function getAdminInfoById(int $id): \Hyperf\Database\Model\Model|null
{
return $this->where('id', $id)->where('is_del',UserCode::IS_NO_DEL)->first();
}
/**
* @param int $userId
* @return \Hyperf\Database\Model\Model|Builder|AdminUser|null
*/
public function getAdminInfoByBindUserId(int $userId): \Hyperf\Database\Model\Model|Builder|null|AdminUser
{
return $this->where('bind_user_id', $userId)->where('is_del',UserCode::IS_NO_DEL)->first();
}
/**
* 获取所有数据
* @param array $ids
* @return array
*/
public function getDataByIds(array $ids): array
{
$data = $this->whereIn('id',$ids)->get();
if (empty($data)){
return [];
}
$res = [];
foreach ($data->toArray() as $one)
{
$res[$one['id']] = $one;
}
return $res;
}
/**
* @param array $ids
* @return array
*/
public function getChefNameByIds(array $ids): array
{
$data = $this
->join('chef', function ($join) use ($ids) {
$join->on('chef.user_id', '=', 'admin_user.id')
->whereIn('chef.id', $ids)
->where('admin_user.is_del',UserCode::IS_NO_DEL)
->select([
'admin_user.chinese_name',
'chef.id',
'admin_user.username',
]);
})
->get();
if ($data->isEmpty()) return [];
$data = $data->toArray();
return array_column($data,null,'id');
}
/**
* @param array $ids
* @return array
*/
public function getDataArrByIds(array $ids): array
{
$res = $this
->whereIn('id',$ids)
->get();
if ($res->isEmpty()) return [];
return $res->toArray();
}
}