diff --git a/Src/Asp.Net/PgSqlTest/PgSqlTest.csproj b/Src/Asp.Net/PgSqlTest/PgSqlTest.csproj index 5f70f3cf4..c7b048c14 100644 --- a/Src/Asp.Net/PgSqlTest/PgSqlTest.csproj +++ b/Src/Asp.Net/PgSqlTest/PgSqlTest.csproj @@ -103,7 +103,12 @@ + + + + + diff --git a/Src/Asp.Net/PgSqlTest/UnitTest/Main.cs b/Src/Asp.Net/PgSqlTest/UnitTest/Main.cs index 58301cf29..3e1626c1d 100644 --- a/Src/Asp.Net/PgSqlTest/UnitTest/Main.cs +++ b/Src/Asp.Net/PgSqlTest/UnitTest/Main.cs @@ -31,6 +31,7 @@ namespace OrmTest } public static void Init() { + UintOneToOneDto.Init(); Unitadfaafsd.Init(); CrossDatabase02.Init(); UinitCustomConvert.Init(); diff --git a/Src/Asp.Net/PgSqlTest/UnitTest/Models/SignInRecord.cs b/Src/Asp.Net/PgSqlTest/UnitTest/Models/SignInRecord.cs new file mode 100644 index 000000000..3a6c74758 --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/UnitTest/Models/SignInRecord.cs @@ -0,0 +1,61 @@ +using SqlSugar; +using System; + + +/// +/// 签到表 +/// +[SugarTable(null, "签到表")] +public class SignInRecord +{ + + /// + /// 报名用户Id + /// + [SugarColumn(ColumnDescription = "用户Id")] + public long UserId { get; set; } + + /// + /// 系统用户 + /// + [SugarColumn(IsIgnore = true)] + [Navigate(NavigateType.OneToOne, nameof(UserId))] + public SysUser sysUser { get; set; } + + /// + /// 签到日期 + /// + [SugarColumn(ColumnDescription = "签到日期")] + public DateTime SignInDate { get; set; } + /// + /// 上午签到时间 + /// + [SugarColumn(ColumnDescription = "上午签到时间")] + public DateTime? MorningSignInTime { get; set; } + /// + /// 上午签到地点 + /// + [SugarColumn(ColumnDescription = "上午签到地点", Length = 64)] + public string MorningSignInAddress { get; set; } + /// + /// 上午签到结果 + /// + [SugarColumn(ColumnDescription = "上午签到结果")] + public SignInResultEnum MorningSignInResult { get; set; } = 0; + /// + /// 下午签到时间 + /// + [SugarColumn(ColumnDescription = "下午签到时间")] + public DateTime? AfternoonSignInTime { get; set; } + /// + /// 下午签到地点 + /// + [SugarColumn(ColumnDescription = "下午签到地点", Length = 64)] + public string AfternoonSignInAddress { get; set; } + /// + /// 下午签到结果 + /// + [SugarColumn(ColumnDescription = "下午签到结果")] + public SignInResultEnum AfternoonSignInResult { get; set; } = 0; + +} \ No newline at end of file diff --git a/Src/Asp.Net/PgSqlTest/UnitTest/Models/SignInRecordOutput.cs b/Src/Asp.Net/PgSqlTest/UnitTest/Models/SignInRecordOutput.cs new file mode 100644 index 000000000..9316dfb3d --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/UnitTest/Models/SignInRecordOutput.cs @@ -0,0 +1,36 @@ +using System; + + +public class SignInRecordOutput +{ + + /// + /// 用户Id + /// + public long UserId { get; set; } + /// + /// 用户 + /// + public SysUser sysUser { get; set; } + + /// + /// 需签到次数 + /// + public long SignInNeedCount { get; set; } + /// + /// 签到次数 + /// + public long SignInCount { get; set; } + /// + /// 迟到次数 + /// + public long LateCount { get; set; } + /// + /// 请假次数 + /// + public long LeaveCount { get; set; } + /// + /// 出勤率 + /// + public double AttendanceRate { get; set; } +} diff --git a/Src/Asp.Net/PgSqlTest/UnitTest/Models/SignInResultEnum.cs b/Src/Asp.Net/PgSqlTest/UnitTest/Models/SignInResultEnum.cs new file mode 100644 index 000000000..20242421a --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/UnitTest/Models/SignInResultEnum.cs @@ -0,0 +1,32 @@ +using System.ComponentModel; + +/// +/// 签到结果枚举 +/// +[Description("签到结果枚举")] +public enum SignInResultEnum +{ + /// + /// 未签到 + /// + [Description("未签到")] + None = 0, + + /// + /// 已签到 + /// + [Description("已签到")] + SignedIn = 1, + + /// + /// 迟到 + /// + [Description("迟到")] + Late = 2, + + /// + /// 请假 + /// + [Description("请假")] + Leave = 3, +} \ No newline at end of file diff --git a/Src/Asp.Net/PgSqlTest/UnitTest/Models/SysUser.cs b/Src/Asp.Net/PgSqlTest/UnitTest/Models/SysUser.cs new file mode 100644 index 000000000..d1c168a33 --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/UnitTest/Models/SysUser.cs @@ -0,0 +1,120 @@ +using SqlSugar; +using System; + + +/// +/// 系统用户表 +/// +[SugarTable(null, "系统用户表")] +public class SysUser +{ + /// + /// 雪花Id + /// + [SugarColumn(ColumnDescription = "Id", IsPrimaryKey = true, IsIdentity = false)] + public virtual long Id { get; set; } + + /// + /// 真实姓名 + /// + [SugarColumn(ColumnDescription = "真实姓名", Length = 32)] + public virtual string RealName { get; set; } + + /// + /// 昵称 + /// + [SugarColumn(ColumnDescription = "昵称", Length = 32)] + public string NickName { get; set; } + + /// + /// 头像 + /// + [SugarColumn(ColumnDescription = "头像", Length = 512)] + public string Avatar { get; set; } + + /// + /// 年龄 + /// + [SugarColumn(ColumnDescription = "年龄")] + public int Age { get; set; } + + /// + /// 出生日期 + /// + [SugarColumn(ColumnDescription = "出生日期")] + public DateTime? Birthday { get; set; } + + /// + /// 民族 + /// + [SugarColumn(ColumnDescription = "民族", Length = 32)] + public string Nation { get; set; } + + /// + /// 手机号码 + /// + [SugarColumn(ColumnDescription = "手机号码", Length = 16)] + public string Phone { get; set; } + + + /// + /// 身份证号 + /// + [SugarColumn(ColumnDescription = "身份证号", Length = 32)] + public string IdCardNum { get; set; } + + /// + /// 邮箱 + /// + [SugarColumn(ColumnDescription = "邮箱", Length = 64)] + public string Email { get; set; } + + /// + /// 地址 + /// + [SugarColumn(ColumnDescription = "地址", Length = 256)] + public string Address { get; set; } + + /// + /// 政治面貌 + /// + [SugarColumn(ColumnDescription = "政治面貌", Length = 16)] + public string PoliticalOutlook { get; set; } + + /// + /// 毕业院校 + /// COLLEGE + [SugarColumn(ColumnDescription = "毕业院校", Length = 128)] + public string College { get; set; } + + /// + /// 办公电话 + /// + [SugarColumn(ColumnDescription = "办公电话", Length = 16)] + public string OfficePhone { get; set; } + + /// + /// 紧急联系人 + /// + [SugarColumn(ColumnDescription = "紧急联系人", Length = 32)] + public string EmergencyContact { get; set; } + + /// + /// 紧急联系人电话 + /// + [SugarColumn(ColumnDescription = "紧急联系人电话", Length = 16)] + public string EmergencyPhone { get; set; } + + /// + /// 紧急联系人地址 + /// + [SugarColumn(ColumnDescription = "紧急联系人地址", Length = 256)] + public string EmergencyAddress { get; set; } + + /// + /// 个人简介 + /// + [SugarColumn(ColumnDescription = "个人简介", Length = 512)] + public string Introduction { get; set; } + +} \ No newline at end of file diff --git a/Src/Asp.Net/PgSqlTest/UnitTest/UintOneToOneDto.cs b/Src/Asp.Net/PgSqlTest/UnitTest/UintOneToOneDto.cs new file mode 100644 index 000000000..b3e179bba --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/UnitTest/UintOneToOneDto.cs @@ -0,0 +1,81 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + internal class UintOneToOneDto + { + public static void Init() + { + var db = NewUnitTest.Db; + db.CurrentConnectionConfig.ConfigureExternalServices = new ConfigureExternalServices() + { + EntityService = (s, y) => + { + if (y.IsPrimarykey != true && y.IsIdentity != true) + { + y.IsNullable = true; + } + } + }; + //生成表 + db.CodeFirst.SetStringDefaultLength(50).InitTables(); + db.CodeFirst.SetStringDefaultLength(50).InitTables(); + db.DbMaintenance.TruncateTable(); + //生成数据 + SysUser sysuser = new SysUser() + { + Id = 1, + RealName = "Test", + Age = 20, + Phone = "1311111111", + IdCardNum = "1111", + Nation = "中国" + }; + + SignInRecord signInRecord = new SignInRecord() + { + UserId = 1, + SignInDate = (DateTime.Now), + MorningSignInTime = DateTime.Now, + MorningSignInAddress = "Addr", + MorningSignInResult = SignInResultEnum.SignedIn, + AfternoonSignInTime = DateTime.Now, + AfternoonSignInAddress = "Addr", + AfternoonSignInResult = SignInResultEnum.Late, + }; + + db.DbMaintenance.TruncateTable(); + db.DbMaintenance.TruncateTable(); + db.Insertable(sysuser).ExecuteCommand(); + db.Insertable(signInRecord).ExecuteCommand(); + + //查询测试 + var query = db.Queryable() + //**** Bug1, 当有x => x.sysUser.ToList(it => 时,同时使用Select的话,sysUser不会赋值。去掉Select则可以。 + + // .Includes(x => x.sysUser) + .Includes(x => x.sysUser.ToList(it=>new SysUser() { Nation=it.Nation })) + .GroupBy(g => new { g.UserId }) + .Select(it => new SignInRecordOutput + { + UserId = it.UserId, + sysUser = it.sysUser, + + SignInCount = SqlFunc.AggregateCount(SqlFunc.IF(it.MorningSignInResult == SignInResultEnum.SignedIn || it.AfternoonSignInResult == SignInResultEnum.SignedIn).Return(1).End()), + LateCount = SqlFunc.AggregateCount(SqlFunc.IF(it.MorningSignInResult == SignInResultEnum.Late || it.AfternoonSignInResult == SignInResultEnum.Late).Return(1).End()), + LeaveCount = SqlFunc.AggregateCount(SqlFunc.IF(it.MorningSignInResult == SignInResultEnum.Leave || it.AfternoonSignInResult == SignInResultEnum.Leave).Return(1).End()), + }) .ToList(); + + if (query.First().sysUser == null) + { + throw new Exception("unit error"); + } + } + + } +}