using Cksoft.Data; using DllUfpEntity; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DllUfpDal { public class BtnFuncDal { public IDatabase CurrDb; public BtnFuncDal(IDatabase db) { this.CurrDb = db; } public IEnumerable Get(int start, int length, string order, string sort, string filter, string errorinfo) { var btnfuncs = CurrDb.FindListForCondition($" {filter} order by {sort} {order} limit {start - 1},{length}", ref errorinfo); return btnfuncs; } public int GetCount(string filter) { string sql = $"select count(1) from BtnFunc a where 1=1 {filter}"; return Convert.ToInt32(CurrDb.FindList(sql).FirstOrDefault() ?? "0"); } public BtnFunc Get(string id) { return CurrDb.FindEntityFor(id); } /// /// 添加角色并返回角色Id /// /// /// /// public int Add(BtnFunc func, string userCode) { func.RecCode = func.ModCode = userCode; func.RecTime = func.ModTime = DateTime.Now; string sql = $"insert into BtnFunc (FCode,FName,ModuleName,SystemId,Remark,RecCode,RecTime,ModCode,ModTime,FuncName) " + $"values ('{func.FCode}','{func.FName}','{func.ModuleName}','{func.SystemId}','{func.Remark}','{func.RecCode}','{func.RecTime.Value.ToString("yyyy-MM-dd")}'," + $"'{func.ModCode}','{func.ModTime.Value.ToString("yyyy-MM-dd")}','{func.FuncName}');"; sql += "select @@identity;"; var id = Convert.ToInt32(CurrDb.FindList(sql).FirstOrDefault() ?? "-1"); return id; } public int Update(BtnFunc func, string userCode) { return CurrDb.UpdateFor(func, userCode); } public IEnumerable getRoleFunc(int id) { string sql = $"select * from RoleFunc where FuncId='{id}' and type=2"; return CurrDb.FindList(sql); } public int Delete(int id, ref string msg) { string sql = $"delete from RoleFunc where FuncId='{id}' and type=2"; var res = CurrDb.ExecuteBySql(sql); if (res < 0) { msg = "删除角色功能时失败"; return -1; } res = CurrDb.DeleteFor(id); if (res < 0) { msg = "删除功能主表时失败"; return -1; } return 1; } public int SetBtnFunction(IEnumerable funcIds, int roleId, string usercode) { if (funcIds == null || funcIds.Count() <= 0) { return 1; } foreach (var item in funcIds) { var res = CurrDb.InsertFor(new RoleFunc() { FuncId = item, RoleId = roleId, RecTime = DateTime.Now, ModTime = DateTime.Now, ModCode = usercode, RecCode = usercode, Type = 1 }, usercode); if (res < 0) return -1; } return 1; } public IEnumerable getRoleFuncByRoleId( int roleId) { var sql = $"select * from RoleFunc where roleid={roleId} and type=1"; return CurrDb.FindList(sql); } public int DeleteRoleFuncs(IEnumerable funcIds, int roleId) { var sql = $"delete from RoleFunc where roleid={roleId} and type=1"; if (CurrDb.ExecuteBySql(sql) < 0) return -1; return 1; } public bool IsPermitted(string userCode, string btnFunc, ref string errorinfo) { var staffSql = $"select * from staff where fcode='{userCode}'"; var staff = CurrDb.FindList(staffSql).FirstOrDefault(); if (staff == null) { errorinfo = "用户不存在或已被删除"; return false; } if (staff.IsSA == 1) { return true; } string sql = $@"select a.* from roleFunc a left join btnfunc b on a.funcid=b.id left join role c on a.roleid=c.id left join staffrole d on c.id=d.roleid where 1=1 and a.Type=1 and d.staffcode='{userCode}' and b.fCode='{btnFunc}'"; var entity = CurrDb.FindList(sql).FirstOrDefault(); if (entity == null) { errorinfo = "当前功能未授权,请授权后再试"; return false; } return true; } /// /// 判断请求的接口是否有权限 /// /// 用户工号 /// 模块名称(控制器名称) /// action名称 /// 错误信息 /// public bool IsPermitted(string userCode, string moduleName, string btnFunc, ref string errorinfo) { if (string.IsNullOrEmpty(userCode)) return true; var sql = $"select * from staff where fcode='{userCode}'"; var staff = CurrDb.FindList(sql).FirstOrDefault(); if (staff == null || staff.IsSA == 1) return true; var strsql = $"select * from btnfunc where funcName='{btnFunc}' and modulename='{moduleName}'"; var btn = CurrDb.FindList(strsql).FirstOrDefault(); if (btn == null) return true; sql = $@"select a.* from roleFunc a left join btnfunc b on a.funcid=b.id left join role c on a.roleid=c.id left join staffrole d on c.id=d.roleid where 1=1 and a.Type=1 and d.staffcode='{userCode}' and b.funcName='{btnFunc}' and b.modulename='{moduleName}'"; var entity = CurrDb.FindList(sql).FirstOrDefault(); if (entity == null) { errorinfo = "当前功能未授权,请授权后再试"; return false; } return true; } } }