server/app/Model/Role.php

171 lines
4.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Model;
use Hyperf\DbConnection\Db;
/**
* @property int $role_id
* @property int $account_type
* @property int $belong_id
* @property string $role_name
* @property string $checked_menus
* @property int $status
* @property int $del_flag
* @property string $remark
* @property int $rank
* @property string $create_time
* @property string $update_time
*/
class Role extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'role';
/**
* 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', 'del_flag' => 'integer', 'rank' => 'integer'];
/**
* 部门列表
* @param array $account
* @param array $param
* @return array
*/
static function roles(array $account, array $param): array
{
$model = self::where('del_flag', 0)
->where('belong_id', $account['belong_id'])
->where('account_type', $account['account_type']);
if (isset($param['role_name']) && $param['role_name'] != '') {
$model = $model->where("role_name", "like", "%{$param['role_name']}%");
}
return $model->select(["role_id", "role_name", "status", "remark", "create_time", "rank", "checked_menus"])
->orderByDesc("rank")
->orderByDesc("role_id")
->get()->each(function ($item) {
$item['menus'] = RoleMenu::getMenu($item['role_id']);
})->toArray();
}
/**
* 添加
* @param array $account
* @param array $data
* @return bool
*/
public static function add(array $account, array $data)
{
$data['account_type'] = $account['account_type'];
$data['belong_id'] = $account['belong_id'];
$data['create_time'] = date("Y-m-d H:i:s");
$data['del_flag'] = 0;
$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 $account
* @param array $data
* @return bool
*/
public static function edit(array $account, 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'])
->where('belong_id', $account['belong_id'])
->where('account_type', $account['account_type'])
->where('del_flag', 0)
->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;
}
/**
* 删除
* @param array $account
* @param string $ids
* @return int
*/
public static function del(array $account, string $ids)
{
return self::whereIn('role_id', explode(",", $ids))
->where("del_flag", 0)
->where('belong_id', $account['belong_id'])
->where('account_type', $account['account_type'])
->update([
'update_time' => date('Y-m-d H:i:s'),
'del_flag' => 1
]);
}
/**
* @param array $account
* @return array
*/
public static function options(array $account): array
{
return self::where('del_flag', 0)
->where('belong_id', $account['belong_id'])
->where('account_type', $account['account_type'])
->orderByDesc("rank")
->orderByDesc("role_id")
->select(["role_id", "role_name"])
->get()->toArray();
}
}