分类统计字段要变成累加字段,否则Upsert时这些字段会被0覆盖
大石头 authored at 2020-10-06 08:28:49
3.55 KiB
Stardust
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using NewLife.Data;
using XCode;
using XCode.Membership;

namespace Stardust.Data.Monitors
{
    /// <summary>应用每日统计。每应用每日统计,用于分析应用健康状况</summary>
    public partial class AppDayStat : Entity<AppDayStat>
    {
        #region 对象操作
        static AppDayStat()
        {
            // 累加字段,生成 Update xx Set Count=Count+1234 Where xxx
            var df = Meta.Factory.AdditionalFields;
            df.Add(nameof(Total));
            df.Add(nameof(Errors));
            df.Add(nameof(TotalCost));

            df.Add(nameof(Apis));
            df.Add(nameof(Https));
            df.Add(nameof(Dbs));
            df.Add(nameof(Mqs));
            df.Add(nameof(Redis));
            df.Add(nameof(Others));

            // 过滤器 UserModule、TimeModule、IPModule
            Meta.Modules.Add<TimeModule>();
        }

        /// <summary>验证数据,通过抛出异常的方式提示验证失败。</summary>
        /// <param name="isNew">是否插入</param>
        public override void Valid(Boolean isNew)
        {
            // 如果没有脏数据,则不需要进行任何处理
            if (!HasDirty) return;

            Cost = Total == 0 ? 0 : (Int32)(TotalCost / Total);
        }
        #endregion

        #region 扩展属性
        /// <summary>应用</summary>
        [XmlIgnore, IgnoreDataMember]
        public AppTracer App => Extends.Get(nameof(App), k => AppTracer.FindByID(AppId));

        /// <summary>应用</summary>
        [Map(nameof(AppId))]
        public String AppName => App + "";
        #endregion

        #region 扩展查询
        /// <summary>根据编号查找</summary>
        /// <param name="id">编号</param>
        /// <returns>实体对象</returns>
        public static AppDayStat FindByID(Int32 id)
        {
            if (id <= 0) return null;

            // 实体缓存
            if (Meta.Session.Count < 1000) return Meta.Cache.Find(e => e.ID == id);

            // 单对象缓存
            return Meta.SingleCache[id];

            //return Find(_.ID == id);
        }
        #endregion

        #region 高级查询
        /// <summary>高级查询</summary>
        /// <param name="appId">应用</param>
        /// <param name="start">统计日期开始</param>
        /// <param name="end">统计日期结束</param>
        /// <param name="key">关键字</param>
        /// <param name="page">分页参数信息。可携带统计和数据权限扩展查询等信息</param>
        /// <returns>实体列表</returns>
        public static IList<AppDayStat> Search(Int32 appId, DateTime start, DateTime end, String key, PageParameter page)
        {
            var exp = new WhereExpression();

            if (appId >= 0) exp &= _.AppId == appId;
            exp &= _.StatDate.Between(start, end);

            return FindAll(exp, page);
        }

        /// <summary>查找一批统计</summary>
        /// <param name="date"></param>
        /// <param name="appIds"></param>
        /// <returns></returns>
        public static IList<AppDayStat> Search(DateTime date, Int32[] appIds)
        {
            var exp = new WhereExpression();
            if (date.Year > 2000) exp &= _.StatDate == date;
            if (appIds != null && appIds.Length > 0) exp &= _.AppId.In(appIds);

            return FindAll(exp, new PageParameter { PageSize = 1000 });
        }
        #endregion

        #region 业务操作
        #endregion
    }
}