Beispiel #1
0
 /// <summary>
 /// 删除前外部校验,不合格在外部抛出异常
 /// </summary>
 /// <param name="p_model"></param>
 /// <param name="p_entity"></param>
 /// <returns></returns>
 static async Task <bool> OnDeleting(SqliteEntitySchema p_model, object p_entity)
 {
     try
     {
         if (p_model.OnDeleting.ReturnType == typeof(Task))
         {
             await(Task) p_model.OnDeleting.Invoke(p_entity, null);
         }
         else
         {
             p_model.OnDeleting.Invoke(p_entity, null);
         }
     }
     catch (Exception ex)
     {
         if (ex.InnerException is KnownException kex)
         {
             Kit.Warn(kex.Message);
         }
         else
         {
             Kit.Warn(ex.Message);
         }
         return(false);
     }
     return(true);
 }
Beispiel #2
0
        /// <summary>
        /// 保存实体数据
        /// </summary>
        /// <typeparam name="TEntity">实体类型</typeparam>
        /// <param name="p_entity">待保存的实体</param>
        /// <param name="p_isNotify">是否提示保存结果</param>
        /// <returns>是否成功</returns>
        public static async Task <bool> Save <TEntity>(TEntity p_entity, bool p_isNotify = true)
            where TEntity : Entity
        {
            if (p_entity == null ||
                (!p_entity.IsAdded && !p_entity.IsChanged))
            {
                if (p_isNotify)
                {
                    Kit.Warn(_unchangedMsg);
                }
                return(false);
            }

            var model = SqliteEntitySchema.Get(typeof(TEntity));

            if (model.OnSaving != null)
            {
                // 保存前外部校验,不合格在外部抛出异常
                if (!await OnSaving(model, p_entity))
                {
                    return(false);
                }
            }

            if (_db.Save(p_entity))
            {
                if (p_isNotify)
                {
                    Kit.Msg("保存成功!");
                }
                return(true);
            }

            if (p_isNotify)
            {
                Kit.Warn("保存失败!");
            }
            return(false);
        }
Beispiel #3
0
        /// <summary>
        /// 删除实体
        /// </summary>
        /// <typeparam name="TEntity">实体类型</typeparam>
        /// <param name="p_entity">待删除的行</param>
        /// <param name="p_isNotify">是否提示删除结果</param>
        /// <returns>true 删除成功</returns>
        public static async Task <bool> Delete <TEntity>(TEntity p_entity, bool p_isNotify = true)
            where TEntity : Entity
        {
            if (p_entity == null || p_entity.IsAdded)
            {
                if (p_isNotify)
                {
                    Kit.Warn(_saveError);
                }
                return(false);
            }

            var model = SqliteEntitySchema.Get(typeof(TEntity));

            if (model.OnDeleting != null)
            {
                if (!await OnDeleting(model, p_entity))
                {
                    return(false);
                }
            }

            bool suc = _db.Delete(p_entity);

            if (p_isNotify)
            {
                if (suc)
                {
                    Kit.Msg("删除成功!");
                }
                else
                {
                    Kit.Warn("删除失败!");
                }
            }
            return(suc);
        }
Beispiel #4
0
        /// <summary>
        /// 批量删除实体,单表或多表,列表类型支持:
        /// <para>Table&lt;Entity&gt;,单表删除</para>
        /// <para>List&lt;Entity&gt;,单表删除</para>
        /// <para>IList,多表删除,成员可为Entity,List&lt;Entity&gt;,Table&lt;Entity&gt;的混合</para>
        /// </summary>
        /// <param name="p_list">待删除实体列表</param>
        /// <param name="p_isNotify">是否提示删除结果</param>
        /// <returns>true 删除成功</returns>
        public static async Task <bool> BatchDelete(IList p_list, bool p_isNotify = true)
        {
            if (p_list == null || p_list.Count == 0)
            {
                if (p_isNotify)
                {
                    Kit.Warn(_saveError);
                }
                return(false);
            }

            // 触发外部删除前处理
            Type tp = p_list.GetType();

            if (tp.IsGenericType &&
                tp.GetGenericArguments()[0].IsSubclassOf(typeof(Entity)))
            {
                // 列表中的实体类型相同
                var model = SqliteEntitySchema.Get(tp.GetGenericArguments()[0]);
                if (model.OnDeleting != null)
                {
                    foreach (var ci in p_list)
                    {
                        if (!await OnDeleting(model, ci))
                        {
                            return(false);
                        }
                    }
                }
            }
            else
            {
                // 多类型
                foreach (var item in p_list)
                {
                    if (item is Entity entity)
                    {
                        var model = SqliteEntitySchema.Get(item.GetType());
                        if (model.OnDeleting != null)
                        {
                            if (!await OnDeleting(model, entity))
                            {
                                return(false);
                            }
                        }
                    }
                    else if (item is IList clist &&
                             clist.Count > 0 &&
                             (tp = item.GetType()) != null &&
                             tp.IsGenericType &&
                             tp.GetGenericArguments()[0].IsSubclassOf(typeof(Entity)))
                    {
                        // IList<Entity> 或 Table<Entity>
                        var model = SqliteEntitySchema.Get(tp.GetGenericArguments()[0]);
                        if (model.OnDeleting != null)
                        {
                            foreach (var ci in clist)
                            {
                                if (!await OnDeleting(model, ci))
                                {
                                    return(false);
                                }
                            }
                        }
                    }
                }
            }

            if (_db.BatchDelete(p_list))
            {
                if (p_isNotify)
                {
                    Kit.Msg("删除成功!");
                }
                return(true);
            }

            if (p_isNotify)
            {
                Kit.Warn("删除失败!");
            }
            return(false);
        }
Beispiel #5
0
        /// <summary>
        /// 一个事务内批量保存实体数据,根据实体状态执行增改,Table&lt;Entity&gt;支持删除,列表类型支持:
        /// <para>Table&lt;Entity&gt;,单表增删改</para>
        /// <para>List&lt;Entity&gt;,单表增改</para>
        /// <para>IList,多表增删改,成员可为Entity,List&lt;Entity&gt;,Table&lt;Entity&gt;的混合</para>
        /// </summary>
        /// <param name="p_list">待保存列表</param>
        /// <param name="p_isNotify">是否提示保存结果</param>
        /// <returns>true 保存成功</returns>
        public static async Task <bool> BatchSave(IList p_list, bool p_isNotify = true)
        {
            if (p_list == null || p_list.Count == 0)
            {
                if (p_isNotify)
                {
                    Kit.Warn(_unchangedMsg);
                }
                return(false);
            }

            // 触发外部保存前处理
            Type tp = p_list.GetType();

            if (tp.IsGenericType &&
                tp.GetGenericArguments()[0].IsSubclassOf(typeof(Entity)))
            {
                // 单表增删改,列表中的实体类型相同

                // 不判断列表项数0,因可能Table<Entity>只包含删除列表的情况!
                // IList<Entity> 或 Table<Entity>
                var model = SqliteEntitySchema.Get(tp.GetGenericArguments()[0]);
                if (model.OnSaving != null)
                {
                    foreach (var ci in p_list)
                    {
                        if (!await OnSaving(model, ci))
                        {
                            return(false);
                        }
                    }
                }
            }
            else
            {
                // 多表增删改
                foreach (var item in p_list)
                {
                    if (item is Entity entity)
                    {
                        if (entity.IsAdded || entity.IsChanged)
                        {
                            var model = SqliteEntitySchema.Get(item.GetType());
                            if (model.OnSaving != null)
                            {
                                if (!await OnSaving(model, entity))
                                {
                                    return(false);
                                }
                            }
                        }
                    }
                    else if (item is IList clist &&
                             (tp = item.GetType()) != null &&
                             tp.IsGenericType &&
                             tp.GetGenericArguments()[0].IsSubclassOf(typeof(Entity)))
                    {
                        // 不判断列表项数0,因可能Table<Entity>只包含删除列表的情况!
                        // IList<Entity> 或 Table<Entity>
                        var model = SqliteEntitySchema.Get(tp.GetGenericArguments()[0]);
                        if (model.OnSaving != null)
                        {
                            foreach (var ci in clist)
                            {
                                if (!await OnSaving(model, ci))
                                {
                                    return(false);
                                }
                            }
                        }
                    }
                    //else
                    //{
                    //    throw new Exception($"批量保存不支持[{item.GetType().Name}]类型!");
                    //}
                }
            }

            if (_db.BatchSave(p_list))
            {
                if (p_isNotify)
                {
                    Kit.Msg("保存成功!");
                }
                return(true);
            }

            if (p_isNotify)
            {
                Kit.Warn("保存失败!");
            }
            return(false);
        }