131 lines
3.5 KiB
PHP
131 lines
3.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Model;
|
|
|
|
use Hyperf\DbConnection\Db;
|
|
|
|
/**
|
|
* @property int $role_id
|
|
* @property string $role_name
|
|
* @property int $account_type
|
|
* @property int $belong_id
|
|
* @property string $checked_menus
|
|
* @property int $status
|
|
* @property int $rank
|
|
* @property string $deleted_at
|
|
* @property string $create_time
|
|
* @property string $update_time
|
|
*/
|
|
class Role extends Model
|
|
{
|
|
/**
|
|
* The table associated with the model.
|
|
*/
|
|
protected ?string $table = 'role';
|
|
|
|
protected string $primaryKey = 'role_id';
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*/
|
|
protected array $fillable = [];
|
|
|
|
/**
|
|
* The attributes that should be cast to native types.
|
|
*/
|
|
protected array $casts = ['role_id' => 'integer', 'account_type' => 'integer', 'belong_id' => 'integer', 'status' => 'integer', 'rank' => 'integer'];
|
|
|
|
public static function list(int $belong_id, int $account_type, string $role_name)
|
|
{
|
|
$model = self::where('belong_id', $belong_id)
|
|
->where('account_type', $account_type);
|
|
if ($role_name != '') {
|
|
$model = $model->where("role_name", "like", "%$role_name%");
|
|
}
|
|
return $model->select(["role_id", "role_name", "status", "create_time", "rank", "checked_menus"])
|
|
->get()->each(function ($item) {
|
|
$item['menus'] = RoleMenu::getMenu($item['role_id']);
|
|
})->toArray();
|
|
}
|
|
|
|
public static function options(int $belong_id, int $account_type)
|
|
{
|
|
return self::where('belong_id', $belong_id)
|
|
->where("status", 1)
|
|
->where('account_type', $account_type)
|
|
->orderByDesc("rank")
|
|
->select(["role_id", "role_name"])
|
|
->get()->toArray();
|
|
}
|
|
|
|
/**
|
|
* 添加
|
|
* @param array $data
|
|
* @return bool
|
|
*/
|
|
public static function add(array $data)
|
|
{
|
|
$data['create_time'] = date("Y-m-d H:i:s");
|
|
$menus = $data['menus'];
|
|
unset($data['menus']);
|
|
Db::beginTransaction();
|
|
$role_id = self::insertGetId($data);
|
|
if (!$role_id) {
|
|
Db::rollBack();
|
|
return false;
|
|
}
|
|
$role_menu = [];
|
|
foreach ($menus as $menu_id) {
|
|
$role_menu[] = ['role_id' => $role_id, 'menu_id' => $menu_id];
|
|
}
|
|
if (!empty($role_menu)) {
|
|
$res = RoleMenu::insert($role_menu);
|
|
if (!$res) {
|
|
Db::rollBack();
|
|
return false;
|
|
}
|
|
}
|
|
Db::commit();
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 修改
|
|
* @param array $data
|
|
* @return bool
|
|
*/
|
|
public static function edit(array $data)
|
|
{
|
|
$data['update_time'] = date("Y-m-d H:i:s");
|
|
$menus = $data['menus'];
|
|
unset($data['menus']);
|
|
Db::beginTransaction();
|
|
$res = self::where("role_id", $data['role_id'])->update($data);
|
|
if (!$res) {
|
|
Db::rollBack();
|
|
return false;
|
|
}
|
|
// 删除之前的
|
|
$res1 = RoleMenu::where('role_id', $data['role_id'])->delete();
|
|
if (!$res1) {
|
|
Db::rollBack();
|
|
return false;
|
|
}
|
|
$role_menu = [];
|
|
foreach ($menus as $menu_id) {
|
|
$role_menu[] = ['role_id' => $data['role_id'], 'menu_id' => $menu_id];
|
|
}
|
|
// 重新添加
|
|
if (!empty($role_menu)) {
|
|
if (!RoleMenu::insert($role_menu)) {
|
|
Db::rollBack();
|
|
return false;
|
|
}
|
|
}
|
|
Db::commit();
|
|
return true;
|
|
}
|
|
}
|