server/app/Model/Post.php

72 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Model;
/**
* @property int $post_id
* @property int $account_type
* @property int $belong_id
* @property string $post_name
* @property int $status
* @property int $rank
* @property string $create_time
* @property string $update_time
* @property string $deleted_at
*/
class Post extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'post';
protected string $primaryKey = 'post_id';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = [];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['post_id' => 'integer', 'account_type' => 'integer', 'belong_id' => 'integer', 'rank' => 'integer', 'status' => 'integer'];
public static function list(int $belong_id, int $account_type, string $post_name, bool $export = false): array
{
$model = self::where('belong_id', $belong_id)
->where('account_type', $account_type);
if ($post_name != '') {
$model = $model->where('post_name', "like", "%$post_name%");
}
$model = $model->orderByDesc("rank")->orderByDesc("post_id")
->select(["post_id", "post_name", "rank", "status", "create_time"]);
if (!$export) {
// 非导出数据
return $model->get()->toArray();
}
$header = ['岗位名称', '状态', '创建时间'];
$data = [];
foreach ($model->get() as $row) {
$data[] = [
$row->post_name,
$row->status ? '启用' : '禁用',
$row->create_time->format('Y-m-d H:i:s'),
];
}
return [$header, $data];
}
public static function options(int $belong_id, int $account_type)
{
return self::where('belong_id', $belong_id)
->where('account_type', $account_type)
->where("status", 1)
->orderByDesc("rank")->orderByDesc("post_id")
->select(["post_id", "post_name"])
->get()->toArray();
}
}