lixiaojun
2024-07-17 d3be686753cd800fc364ef8c8eff18a43f57969c
Yw.Service.Run.Core/04-dal/02-postgresql/RunRealRecord.cs
@@ -35,7 +35,7 @@
            exp.AndIF(StartTime.HasValue, x => x.DataTime >= StartTime.Value);
            exp.AndIF(EndTime.HasValue, x => x.DataTime <= EndTime.Value);
            exp.AndIF(Run.HasValue && Run.Value, x => x.RSa == RunStatus.Run);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Stop);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Shut);
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
@@ -58,7 +58,7 @@
            var exp = Expressionable.Create<Entity.RunRealRecord>();
            exp.And(x => x.DataTime >= Day.Date && x.DataTime < Day.AddDays(1).Date);
            exp.AndIF(Run.HasValue && Run.Value, x => x.RSa == RunStatus.Run);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Stop);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Shut);
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
@@ -85,7 +85,7 @@
            var exp = Expressionable.Create<Entity.RunRealRecord>();
            exp.And(x => x.DataTime >= StartDay.Date && x.DataTime < EndDay.AddDays(1).Date);
            exp.AndIF(Run.HasValue && Run.Value, x => x.RSa == RunStatus.Run);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Stop);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Shut);
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
@@ -112,7 +112,7 @@
            var exp = Expressionable.Create<Entity.RunRealRecord>();
            exp.And(x => x.DataTime >= StartTime && x.DataTime <= EndTime);
            exp.AndIF(Run.HasValue && Run.Value, x => x.RSa == RunStatus.Run);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Stop);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Shut);
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
@@ -139,7 +139,7 @@
            exp.AndIF(StartTime.HasValue, x => x.DataTime >= StartTime.Value);
            exp.AndIF(EndTime.HasValue, x => x.DataTime <= EndTime.Value);
            exp.AndIF(Run.HasValue && Run.Value, x => x.RSa == RunStatus.Run);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Stop);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Shut);
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
@@ -158,7 +158,7 @@
            var exp = Expressionable.Create<Entity.RunRealRecord>();
            exp.And(x => x.DataTime >= Day.Date && x.DataTime < Day.AddDays(1).Date);
            exp.AndIF(Run.HasValue && Run.Value, x => x.RSa == RunStatus.Run);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Stop);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Shut);
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
@@ -182,7 +182,7 @@
            var exp = Expressionable.Create<Entity.RunRealRecord>();
            exp.And(x => x.DataTime >= StartDay.Date && x.DataTime < EndDay.AddDays(1).Date);
            exp.AndIF(Run.HasValue && Run.Value, x => x.RSa == RunStatus.Run);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Stop);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Shut);
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
@@ -205,7 +205,7 @@
            var exp = Expressionable.Create<Entity.RunRealRecord>();
            exp.And(x => x.DataTime >= StartTime && x.DataTime <= EndTime);
            exp.AndIF(Run.HasValue && Run.Value, x => x.RSa == RunStatus.Run);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Stop);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Shut);
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
@@ -232,7 +232,7 @@
            exp.AndIF(StartTime.HasValue, x => x.DataTime >= StartTime.Value);
            exp.AndIF(EndTime.HasValue, x => x.DataTime <= EndTime.Value);
            exp.AndIF(Run.HasValue && Run.Value, x => x.RSa == RunStatus.Run);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Stop);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Shut);
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
@@ -260,7 +260,7 @@
            exp.And(x => x.ObjectType == ObjectType);
            exp.And(x => x.ObjectID == ObjectID);
            exp.AndIF(Run.HasValue && Run.Value, x => x.RSa == RunStatus.Run);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Stop);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Shut);
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
@@ -289,7 +289,7 @@
            exp.And(x => x.ObjectType == ObjectType);
            exp.And(x => x.ObjectID == ObjectID);
            exp.AndIF(Run.HasValue && Run.Value, x => x.RSa == RunStatus.Run);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Stop);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Shut);
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
@@ -297,6 +297,36 @@
                    .Where(exp.ToExpression())
                    .OrderBy(x => x.DataTime, OrderByType.Asc)
                    .ToList();
            }
        }
        /// <summary>
        /// 通过 ObjectType 和 ObjectID 获取 (限制数量)
        /// </summary>
        public List<Entity.RunRealRecord> GetLimitByObjectTypeAndObjectID(string ObjectType, long ObjectID, DateTime? StartTime, DateTime? EndTime, int Limit, bool? Run = null)
        {
            if (StartTime.HasValue && EndTime.HasValue)
            {
                if (StartTime.Value > EndTime.Value)
                {
                    return default;
                }
            }
            var exp = Expressionable.Create<Entity.RunRealRecord>();
            exp.AndIF(StartTime.HasValue, x => x.DataTime >= StartTime.Value);
            exp.AndIF(EndTime.HasValue, x => x.DataTime <= EndTime.Value);
            exp.And(x => x.ObjectType == ObjectType);
            exp.And(x => x.ObjectID == ObjectID);
            exp.AndIF(Run.HasValue && Run.Value, x => x.RSa == RunStatus.Run);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Shut);
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
                var list = db.Queryable<Entity.RunRealRecord>()
                    .Where(exp.ToExpression())
                    .OrderBy(x => x.DataTime, OrderByType.Asc)
                    .ToList();
                return list?.ToLimitList(Limit);
            }
        }
@@ -310,7 +340,7 @@
            exp.And(x => x.ObjectType == ObjectType);
            exp.And(x => x.ObjectID == ObjectID);
            exp.AndIF(Run.HasValue && Run.Value, x => x.RSa == RunStatus.Run);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Stop);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Shut);
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
@@ -318,6 +348,28 @@
                    .Where(exp.ToExpression())
                    .OrderBy(x => x.DataTime, OrderByType.Asc)
                    .ToList();
            }
        }
        /// <summary>
        /// 通过 ObjectType 和 ObjectID 获取某日的数据 (限制数量)
        /// </summary>
        public List<Entity.RunRealRecord> GetLimitByObjectTypeAndObjectIDOfDay(string ObjectType, long ObjectID, DateTime Day, int Limit, bool? Run = null)
        {
            var exp = Expressionable.Create<Entity.RunRealRecord>();
            exp.And(x => x.DataTime >= Day.Date && x.DataTime < Day.AddDays(1).Date);
            exp.And(x => x.ObjectType == ObjectType);
            exp.And(x => x.ObjectID == ObjectID);
            exp.AndIF(Run.HasValue && Run.Value, x => x.RSa == RunStatus.Run);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Shut);
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
                var list = db.Queryable<Entity.RunRealRecord>()
                    .Where(exp.ToExpression())
                    .OrderBy(x => x.DataTime, OrderByType.Asc)
                    .ToList();
                return list?.ToLimitList(Limit);
            }
        }
@@ -335,7 +387,7 @@
            exp.And(x => x.ObjectType == ObjectType);
            exp.And(x => x.ObjectID == ObjectID);
            exp.AndIF(Run.HasValue && Run.Value, x => x.RSa == RunStatus.Run);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Stop);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Shut);
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
@@ -343,6 +395,32 @@
                    .Where(exp.ToExpression())
                    .OrderBy(x => x.DataTime, OrderByType.Asc)
                    .ToList();
            }
        }
        /// <summary>
        /// 通过 ObjectType 和 ObjectID 获取日期区间内的数据 (限制数量)
        /// </summary>
        public List<Entity.RunRealRecord> GetLimitByObjectTypeAndObjectIDOfDayRange(string ObjectType, long ObjectID, DateTime StartDay, DateTime EndDay, int Limit, bool? Run = null)
        {
            if (StartDay.Date > EndDay.Date)
            {
                return default;
            }
            var exp = Expressionable.Create<Entity.RunRealRecord>();
            exp.And(x => x.DataTime >= StartDay.Date && x.DataTime < EndDay.AddDays(1).Date);
            exp.And(x => x.ObjectType == ObjectType);
            exp.And(x => x.ObjectID == ObjectID);
            exp.AndIF(Run.HasValue && Run.Value, x => x.RSa == RunStatus.Run);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Shut);
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
                var list = db.Queryable<Entity.RunRealRecord>()
                    .Where(exp.ToExpression())
                    .OrderBy(x => x.DataTime, OrderByType.Asc)
                    .ToList();
                return list?.ToLimitList(Limit);
            }
        }
@@ -360,7 +438,7 @@
            exp.And(x => x.ObjectType == ObjectType);
            exp.And(x => x.ObjectID == ObjectID);
            exp.AndIF(Run.HasValue && Run.Value, x => x.RSa == RunStatus.Run);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Stop);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Shut);
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
@@ -368,6 +446,32 @@
                    .Where(exp.ToExpression())
                    .OrderBy(x => x.DataTime, OrderByType.Asc)
                    .ToList();
            }
        }
        /// <summary>
        /// 通过 ObjectType 和 ObjectID 获取时间区间内的数据 (限制数量)
        /// </summary>
        public List<Entity.RunRealRecord> GetLimitByObjectTypeAndObjectIDOfTimeRange(string ObjectType, long ObjectID, DateTime StartTime, DateTime EndTime, int Limit, bool? Run = null)
        {
            if (StartTime > EndTime)
            {
                return default;
            }
            var exp = Expressionable.Create<Entity.RunRealRecord>();
            exp.And(x => x.DataTime >= StartTime && x.DataTime <= EndTime);
            exp.And(x => x.ObjectType == ObjectType);
            exp.And(x => x.ObjectID == ObjectID);
            exp.AndIF(Run.HasValue && Run.Value, x => x.RSa == RunStatus.Run);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Shut);
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
                var list = db.Queryable<Entity.RunRealRecord>()
                    .Where(exp.ToExpression())
                    .OrderBy(x => x.DataTime, OrderByType.Asc)
                    .ToList();
                return list?.ToLimitList(Limit);
            }
        }
@@ -389,7 +493,7 @@
            exp.And(x => x.ObjectType == ObjectType);
            exp.And(x => x.ObjectID == ObjectID);
            exp.AndIF(Run.HasValue && Run.Value, x => x.RSa == RunStatus.Run);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Stop);
            exp.AndIF(Run.HasValue && !Run.Value, x => x.RSa == RunStatus.Shut);
            using (var db = new SqlSugarClient(ConnectionConfig))
            {
@@ -460,10 +564,11 @@
        {
            var sb = new StringBuilder();
            sb.AppendLine("INSERT INTO run_real_record");
            sb.AppendLine($" VALUES(@objecttype,@objectid,@datatime,@rsa,@continueruntime,@totalruntime,@boottimes,@analystatus,@analyinfo)");
            sb.AppendLine($" VALUES(@objecttype,@objectid,@datatime,@rsa,@continuetime,@totalshuttime,@totalruntime,@boottimes,@analystatus,@analyinfo)");
            sb.AppendLine("ON CONFLICT(objecttype, objectid, datatime) DO UPDATE");
            sb.AppendLine("SET rsa = excluded.rsa,");
            sb.AppendLine("continueruntime = excluded.continueruntime,");
            sb.AppendLine("continuetime = excluded.continuetime,");
            sb.AppendLine("totalshuttime = excluded.totalshuttime,");
            sb.AppendLine("totalruntime = excluded.totalruntime,");
            sb.AppendLine("boottimes = excluded.boottimes,");
            sb.AppendLine("analystatus = excluded.analystatus,");
@@ -474,16 +579,19 @@
        //创建参数
        private List<SugarParameter> CreateParameters(Entity.RunRealRecord entity)
        {
            var list = new List<SugarParameter>();
            list.Add(new SugarParameter("@objecttype", entity.ObjectType));
            list.Add(new SugarParameter("@objectid", entity.ObjectID));
            list.Add(new SugarParameter("@datatime", entity.DataTime));
            list.Add(new SugarParameter("@rsa", entity.RSa));
            list.Add(new SugarParameter("@continueruntime", entity.ContinueRunTime));
            list.Add(new SugarParameter("@totalruntime", entity.TotalRunTime));
            list.Add(new SugarParameter("@boottimes", entity.BootTimes));
            list.Add(new SugarParameter("@analystatus", entity.AnalyStatus));
            list.Add(new SugarParameter("@analyinfo", entity.AnalyInfo));
            var list = new List<SugarParameter>
            {
                new SugarParameter("@objecttype", entity.ObjectType),
                new SugarParameter("@objectid", entity.ObjectID),
                new SugarParameter("@datatime", entity.DataTime),
                new SugarParameter("@rsa", entity.RSa),
                new SugarParameter("@continuetime", entity.ContinueTime),
                new SugarParameter("@totalshuttime", entity.TotalShutTime),
                new SugarParameter("@totalruntime", entity.TotalRunTime),
                new SugarParameter("@boottimes", entity.BootTimes),
                new SugarParameter("@analystatus", entity.AnalyStatus),
                new SugarParameter("@analyinfo", entity.AnalyInfo)
            };
            return list;
        }