using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static OrmTest.Unitadfa1231; namespace OrmTest { public class Unitadfa1231 { public static void Init() { var db = NewUnitTest.Db; db.CurrentConnectionConfig.ConfigureExternalServices.EntityNameService = (x, p) => //处理列名 { if (!p.DbTableName.Contains("_")) p.DbTableName = UtilMethods.ToUnderLine(p.DbTableName);//ToUnderLine驼峰转下划线方法 }; db.CurrentConnectionConfig.ConfigureExternalServices.EntityService = (x, p) => //处理列名 { if (!p.DbColumnName.Contains("_")) p.DbColumnName = UtilMethods.ToUnderLine(p.DbColumnName);//ToUnderLine驼峰转下划线方法 }; db.CodeFirst.InitTables(); var NeedStudyUserCountList = db.Queryable() .InnerJoin((a, b) => a.LawsStudyRuleId == b.LawsStudyRuleId) .InnerJoin((a, b, c) => b.RoleId == c.RoleId) .Where((a, b, c) => a.status == 1 && a.type == 1) .Select((a, b, c) => new { rindex = SqlFunc.RowNumber($"{b.NeedStudyLength} desc ", $"{a.Id},{c.UserId}"), Id = a.Id, LawsStudyRuleId = a.LawsStudyRuleId, UserId = c.UserId, NeedStudyLength = b.NeedStudyLength }) .MergeTable() .Where(u => u.rindex == 1) .GroupBy(u => u.LawsStudyRuleId) .Select(u => new { LawsStudyRuleId = u.LawsStudyRuleId, NeedStudyUserCount = SqlFunc.AggregateCount(u.UserId) }) .ToList(); } [SugarTable("laws_regulations", "法律法规表")] public class LawsRegulations : EntityBase { /// /// 分类 (0目录 1文件) /// public int type { get; set; } /// /// 父Id /// public long pid { get; set; } /// /// 名称 /// public string title { get; set; } /// /// 状态(0失效 1正常) /// public int status { get; set; } /// /// 法规学习规则表Id /// public long LawsStudyRuleId { get; set; } /// /// 失效时间 /// public DateTime? invalidTime { get; set; } /// /// (过期时间)失效时间 /// public DateTime? expireTime { get; set; } /// /// 是否新法速递 /// public Boolean IsNewLaw { get; set; } /// /// 是否系统设定,禁止修改 /// public Boolean IsSystem { get; set; } /// /// 排序 /// public int orderNo { get; set; } /// /// 备注 /// public string remark { get; set; } } [SugarTable("laws_study_role", "角色学习法规需要时长表")] public class LawsStudyRole : EntityBaseId { /// /// 法规学习规则表Id /// public long LawsStudyRuleId { get; set; } /// /// 角色Id /// public long RoleId { get; set; } /// /// 学习所需时长(分钟) /// public int NeedStudyLength { get; set; } [Navigate(NavigateType.OneToMany, nameof(SysUserRole.RoleId), nameof(RoleId))] public List SysUserRoleList { get; set; } } [SugarTable(null, "系统用户角色表")] public class SysUserRole : EntityBaseId { /// /// 用户Id /// [SugarColumn(ColumnDescription = "用户Id")] public long UserId { get; set; } /// /// 角色Id /// [SugarColumn(ColumnDescription = "角色Id")] public long RoleId { get; set; } /// /// 角色 } } }