using CommonLib.DBUtils.SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WCSPro.Model;
namespace WCSPro.IDAL
{
public interface IRoleRightsDAL : IBaseDAL<RoleRights>
{
List<Role> GetAllRoles();
List<RightsItem> GetAllRightsItems();
List<RightsItem> GetAllRightsSource();
List<RightsItem> GetRoleRights(Role role);
bool UpdateRoleRights(Role role, List<RoleRights> roleRights);
bool AddRole(Role role);
bool ExistsRoleName(string roleName);
bool ExistsRole(string gid);
bool ExistsRole(Role role);
bool DeleteRole(Role role);
bool DeleteRole(string gid);
}
}
using CommonLib.DBUtils.SqlSugar;
using SqlSugar;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;
using WCSPro.IDAL;
using WCSPro.Model;
namespace WCSPro.DAL
{
public class RoleRightsDAL : BaseDAL<RoleRights>, IRoleRightsDAL
{
public RoleRightsDAL(ISqlSugarClient db) : base(db)
{
Context.DbMaintenance.CreateDatabase();
Context.CodeFirst.SetStringDefaultLength(200).InitTables<Role>();
Context.CodeFirst.SetStringDefaultLength(200).InitTables<RightsItem>();
Context.CodeFirst.SetStringDefaultLength(200).InitTables<RoleRights>();
InitRights();
}
public bool AddRole(Role role)
{
return Context.Insertable(role).ExecuteCommand() == 1;
}
public bool DeleteRole(Role role)
{
return Context.Deleteable(role).ExecuteCommand() == 1;
}
public bool DeleteRole(string gid)
{
return Context.Deleteable<Role>(gid).ExecuteCommand() == 1;
}
public bool ExistsRole(string gid)
{
return Context.Queryable<Role>().Any(t => t.GID == gid);
}
public bool ExistsRole(Role role)
{
return ExistsRole(role.GID);
}
public bool ExistsRoleName(string roleName)
{
return Context.Queryable<Role>().Any(t => t.Name.ToUpper() == roleName.ToUpper());
}
public List<RightsItem> GetAllRightsItems()
{
return Context.Queryable<RightsItem>().ToList();
}
public List<Role> GetAllRoles()
{
return Context.Queryable<Role>().ToList();
}
public List<RightsItem> GetRoleRights(Role role)
{
var rightQ = Context.Queryable<RoleRights>().Where(t => t.RoleGID == role.GID);
return Context.Queryable<RightsItem>()
.LeftJoin(rightQ, (ri, rr) => ri.RightsCode == rr.RightCode)
.OrderBy((ri, rr) => ri.RightsCode)
.Select((ri, rr) => new RightsItem()
{
RightsCode = ri.RightsCode,
ParentCode = ri.ParentCode,
RightsDescription = ri.RightsDescription,
RightsName = ri.RightsName,
Authorized = !string.IsNullOrWhiteSpace(rr.RoleGID)
}).ToList();
}
public bool UpdateRoleRights(Role role, List<RoleRights> roleRights)
{
try
{
Context.Ado.BeginTran();
Context.Deleteable<RoleRights>().Where(t => t.RoleGID == role.GID).ExecuteCommand();
Context.Insertable(roleRights).ExecuteCommand();
Context.Ado.CommitTran();
return true;
}
catch (Exception ex)
{
Context.Ado.RollbackTran();
throw ex;
}
}
private List<RightsItem> ALLRights { get; } = new List<RightsItem>()
{
new RightsItem() { RightsCode="00", RightsName="MAIN", RightsDescription="MAIN"},
new RightsItem() { RightsCode="00.001", ParentCode="00", RightsName="SCAN MODE", RightsDescription="MAIN -> SCAN MODE"},
new RightsItem() { RightsCode="00.002", ParentCode="00", RightsName="RECIPE", RightsDescription="MAIN -> RECIPE"},
new RightsItem() { RightsCode="00.003", ParentCode="00", RightsName="JOB", RightsDescription="MAIN -> JOB"},
new RightsItem() { RightsCode="00.004", ParentCode="00", RightsName="System View", RightsDescription="MAIN -> System View"},
new RightsItem() { RightsCode="00.005", ParentCode="00", RightsName="Safety", RightsDescription="MAIN -> Safety"},
new RightsItem() { RightsCode="01", RightsName="MAINT", RightsDescription="MAINT"},
new RightsItem() { RightsCode="01.001", ParentCode="01", RightsName="P&ID Main", RightsDescription="MAINT -> P&ID Main"},
new RightsItem() { RightsCode="01.002", ParentCode="01", RightsName="P&ID Chem", RightsDescription="MAINT -> P&ID Chem"},
new RightsItem() { RightsCode="01.003", ParentCode="01", RightsName="I/O List", RightsDescription="MAINT -> I/O List"},
new RightsItem() { RightsCode="01.004", ParentCode="01", RightsName="PUMP", RightsDescription="MAINT -> PUMP"},
new RightsItem() { RightsCode="01.005", ParentCode="01", RightsName="Load Port", RightsDescription="MAINT -> Load Port"},
new RightsItem() { RightsCode="01.006", ParentCode="01", RightsName="Robot", RightsDescription="MAINT -> Robot"},
new RightsItem() { RightsCode="01.007", ParentCode="01", RightsName="MOTION", RightsDescription="MAINT -> MOTION"},
new RightsItem() { RightsCode="01.008", ParentCode="01", RightsName="MFC/LFC", RightsDescription="MAINT -> MFC/LFC"},
new RightsItem() { RightsCode="01.009", ParentCode="01", RightsName="Run Test", RightsDescription="MAINT -> Run Test"},
new RightsItem() { RightsCode="01.010", ParentCode="01", RightsName="DEV TEST", RightsDescription="MAINT -> DEV TEST"},
new RightsItem() { RightsCode="01.011", ParentCode="01", RightsName="RFID", RightsDescription="MAINT -> RFID"},
new RightsItem() { RightsCode="01.012", ParentCode="01", RightsName="Heater", RightsDescription="MAINT -> Heater"},
new RightsItem() { RightsCode="02", RightsName="ICPMS", RightsDescription="ICPMS"},
new RightsItem() { RightsCode="02.001", ParentCode="02", RightsName="ICP-Mass", RightsDescription="ICPMS -> ICP-Mass"},
new RightsItem() { RightsCode="02.002", ParentCode="02", RightsName="Calibration", RightsDescription="ICPMS -> Calibration"},
new RightsItem() { RightsCode="02.003", ParentCode="02", RightsName="Sol Blank", RightsDescription="ICPMS -> Sol Blank"},
new RightsItem() { RightsCode="02.004", ParentCode="02", RightsName="Sensitivity", RightsDescription="ICPMS -> Sensitivity"},
new RightsItem() { RightsCode="02.005", ParentCode="02", RightsName="SelfValidation", RightsDescription="ICPMS -> SelfValidation"},
new RightsItem() { RightsCode="02.006", ParentCode="02", RightsName="Parameter", RightsDescription="ICPMS -> Parameter"},
new RightsItem() { RightsCode="02.007", ParentCode="02", RightsName="Result", RightsDescription="ICPMS -> Result"},
new RightsItem() { RightsCode="02.008", ParentCode="02", RightsName="Periodic Table", RightsDescription="ICPMS -> Periodic Table"},
new RightsItem() { RightsCode="02.009", ParentCode="02", RightsName="PreAnalysis", RightsDescription="ICPMS -> PreAnalysis"},
new RightsItem() { RightsCode="02.010", ParentCode="02", RightsName="Sample Result", RightsDescription="ICPMS -> Sample Result"},
new RightsItem() { RightsCode="03", RightsName="PARAM", RightsDescription="PARAM"},
new RightsItem() { RightsCode="04", RightsName="Data", RightsDescription="Data"},
new RightsItem() { RightsCode="04.001", ParentCode="04", RightsName="USERS", RightsDescription="Data -> USERS"},
new RightsItem() { RightsCode="04.002", ParentCode="04", RightsName="ROLES", RightsDescription="Data -> ROLES"},
new RightsItem() { RightsCode="05", RightsName="LOG", RightsDescription="LOG"},
};
private void InitRights()
{
List<RightsItem> dbls = this.GetAllRightsItems();
var diffls = this.ALLRights.Except(dbls, new RightsItemComparer());
if (diffls != null && diffls.Count() > 0)
{
Context.Insertable(diffls.ToList()).ExecuteCommand();
}
}
public List<RightsItem> GetAllRightsSource()
{
return this.ALLRights;
}
}
public class RightsItemComparer : IEqualityComparer<RightsItem>
{
public bool Equals(RightsItem x, RightsItem y)
{
return x.RightsCode == y.RightsCode;
}
public int GetHashCode(RightsItem obj)
{
return obj.RightsCode.GetHashCode();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
namespace WCSPro.Model
{
[SugarTable("RoleRightsMap", "角色权限映射表")]
public class RoleRights
{
[SugarColumn(IsPrimaryKey = true, ColumnDataType = "varchar(36)")]
public string RoleGID { get; set; } = string.Empty;
[SugarColumn(ColumnDataType = "varchar(16)", IsPrimaryKey = true)]
public string RightCode { get; set; } = string.Empty;
}
}
using PropertyChanged;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
using System.ComponentModel.DataAnnotations;
using System.Xml.Linq;
namespace WCSPro.Model
{
[AddINotifyPropertyChangedInterface]
public class Role
{
[SugarColumn(IsPrimaryKey = true, ColumnDataType = "varchar(36)")]
public string GID { get; set; } = Guid.NewGuid().ToString();
[SugarColumn(ColumnDataType = "varchar(32)")]
public string Name { get; set; }
public override string ToString()
{
return Name;
}
}
}
using PropertyChanged;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WCSPro.Model
{
[SugarTable("Rights", "权限表")]
[AddINotifyPropertyChangedInterface]
public class RightsItem
{
[SugarColumn(ColumnDataType = "varchar(16)", IsPrimaryKey = true)]
public string RightsCode { get; set; } = string.Empty;
[SugarColumn(ColumnDataType = "varchar(32)")]
public string RightsName { get; set; } = string.Empty;
[SugarColumn(ColumnDataType = "varchar(128)")]
public string RightsDescription { get; set; } = string.Empty;
[SugarColumn(ColumnDataType = "varchar(8)")]
public string ParentCode { get; set; } = string.Empty;
[SugarColumn(IsIgnore = true)]
public bool Authorized { get; set; }
}
}