123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- 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<BtnFunc> Get(int start, int length, string order, string sort, string filter, string errorinfo)
- {
- var btnfuncs = CurrDb.FindListForCondition<BtnFunc>($" {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<string>(sql).FirstOrDefault() ?? "0");
- }
- public BtnFunc Get(string id)
- {
- return CurrDb.FindEntityFor<BtnFunc>(id);
- }
- /// <summary>
- /// 添加角色并返回角色Id
- /// </summary>
- /// <param name="role"></param>
- /// <param name="userCode"></param>
- /// <returns></returns>
- 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<string>(sql).FirstOrDefault() ?? "-1");
- return id;
- }
- public int Update(BtnFunc func, string userCode)
- {
- return CurrDb.UpdateFor(func, userCode);
- }
- public IEnumerable<RoleFunc> getRoleFunc(int id)
- {
- string sql = $"select * from RoleFunc where FuncId='{id}' and type=2";
- return CurrDb.FindList<RoleFunc>(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<BtnFunc>(id);
- if (res < 0)
- {
- msg = "删除功能主表时失败";
- return -1;
- }
- return 1;
- }
- public int SetBtnFunction(IEnumerable<int> funcIds, int roleId, string usercode)
- {
- if (funcIds == null || funcIds.Count() <= 0)
- {
- return 1;
- }
- foreach (var item in funcIds)
- {
- var res = CurrDb.InsertFor<RoleFunc>(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<RoleFunc> getRoleFuncByRoleId( int roleId)
- {
- var sql = $"select * from RoleFunc where roleid={roleId} and type=1";
- return CurrDb.FindList<RoleFunc>(sql);
- }
- public int DeleteRoleFuncs(IEnumerable<int> 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<Staff>(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<RoleFunc>(sql).FirstOrDefault();
- if (entity == null)
- {
- errorinfo = "当前功能未授权,请授权后再试";
- return false;
- }
- return true;
- }
- /// <summary>
- /// 判断请求的接口是否有权限
- /// </summary>
- /// <param name="userCode">用户工号</param>
- /// <param name="moduleName">模块名称(控制器名称)</param>
- /// <param name="btnFunc">action名称</param>
- /// <param name="errorinfo">错误信息</param>
- /// <returns></returns>
- 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<Staff>(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<BtnFunc>(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<RoleFunc>(sql).FirstOrDefault();
- if (entity == null)
- {
- errorinfo = "当前功能未授权,请授权后再试";
- return false;
- }
- return true;
- }
- }
- }
|