| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- declare (strict_types = 1);
- namespace app\model;
- use think\Model;
- /**
- * 角色模型
- */
- class UserRoleModel extends Model
- {
- // 设置表名
- protected $name = 'user_role';
-
- // 设置主键
- protected $pk = 'id';
-
- // 开启自动时间戳
- protected $autoWriteTimestamp = 'int';
-
- // 定义时间戳字段名
- protected $createTime = 'create_time';
- protected $updateTime = 'update_time';
-
- // 设置json类型字段
- protected $json = ['privileges'];
-
- // 设置JSON数据返回数组
- protected $jsonAssoc = true;
-
- /**
- * 获取角色列表
- * @param int $merchantId 商户ID
- * @return \think\Collection
- */
- public static function getRoleList(int $merchantId)
- {
- return self::where('merchant_id', $merchantId)
- ->field('id, role_name, privileges, create_time, update_time')
- ->order('id', 'desc')
- ->select();
- }
-
- /**
- * 获取角色详情
- * @param int $id 角色ID
- * @param int $merchantId 商户ID
- * @return array|\think\Model|null
- */
- public static function getRoleById(int $id, int $merchantId)
- {
- return self::where('id', $id)
- ->where('merchant_id', $merchantId)
- ->find();
- }
-
- /**
- * 创建角色
- * @param array $data 角色数据
- * @return \think\Model
- */
- public static function createRole(array $data)
- {
- return self::create($data);
- }
-
- /**
- * 更新角色
- * @param int $id 角色ID
- * @param int $merchantId 商户ID
- * @param array $data 更新数据
- * @return
- */
- public static function updateRole(int $id, int $merchantId, array $data)
- {
- return self::where('id', $id)
- ->where('merchant_id', $merchantId)
- ->update($data);
- }
-
- /**
- * 删除角色
- * @param int $id 角色ID
- * @param int $merchantId 商户ID
- * @return bool
- */
- public static function deleteRole(int $id, int $merchantId)
- {
- return self::where('id', $id)
- ->where('merchant_id', $merchantId)
- ->delete();
- }
-
- /**
- * 检查角色是否存在
- * @param string $roleName 角色名称
- * @param int $merchantId 商户ID
- * @param int|null $excludeId 排除的ID
- * @return bool
- */
- public static function checkRoleExists(string $roleName, int $merchantId, ?int $excludeId = null): bool
- {
- $query = self::where('role_name', $roleName)
- ->where('merchant_id', $merchantId);
-
- if ($excludeId) {
- $query->where('id', '<>', $excludeId);
- }
-
- return $query->count() > 0;
- }
- }
|