/// <summary> /// 将实体数据传输到服务端,由服务端DataProvider删除实体,用于需要触发领域事件或同步缓存的情况 /// </summary> /// <typeparam name="TEntity">实体类型</typeparam> /// <param name="p_entity">待删除的行</param> /// <param name="p_isNotify">是否提示删除结果</param> /// <returns>true 删除成功</returns> public static async Task <bool> DeleteBySvc <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 = EntitySchema.Get(typeof(TEntity)); bool suc = await new UnaryRpc( typeof(TSvc).Name, "EntityAccess.Delete", p_entity, model.Schema.Name ).Call <bool>(); if (p_isNotify) { if (suc) { Kit.Msg("删除成功!"); } else { Kit.Warn("删除失败!"); } } return(suc); }
/// <summary> /// 单表批量删除 /// </summary> /// <param name="p_list"></param> /// <param name="p_isNotify"></param> /// <returns></returns> static async Task <bool> BatchDeleteSameType(IList p_list, bool p_isNotify) { var model = EntitySchema.Get(p_list.GetType().GetGenericArguments()[0]); if (model.OnDeleting != null) { foreach (var item in p_list) { if (item != null && !await OnDeleting(model, item)) { return(false); } } } Dict dt = model.Schema.GetDeleteSql(p_list); bool suc = await BatchExec(new List <Dict> { dt }) > 0; if (p_isNotify) { if (suc) { Kit.Msg("删除成功!"); } else { Kit.Warn("删除失败!"); } } return(suc); }
/// <summary> /// 单表增删改,列表中的实体类型相同 /// </summary> /// <param name="p_list"></param> /// <param name="p_isNotify"></param> /// <returns></returns> static async Task <bool> BatchSaveSameType(IList p_list, bool p_isNotify) { var model = EntitySchema.Get(p_list.GetType().GetGenericArguments()[0]); if (model.OnSaving != null) { foreach (var item in p_list) { if (item != null && !await OnSaving(model, item)) { return(false); } } } var dts = model.Schema.GetBatchSaveSql(p_list); // 不需要保存 if (dts == null || dts.Count == 0) { if (p_isNotify) { Kit.Msg(_unchangedMsg); } return(false); } if (await BatchExec(dts) > 0) { if (p_list is Table tbl) { tbl.AcceptChanges(); } else { foreach (var row in p_list.OfType <Row>()) { if (row.IsChanged || row.IsAdded) { row.AcceptChanges(); } } } if (p_isNotify) { Kit.Msg("保存成功!"); } return(true); } if (p_isNotify) { Kit.Warn("保存失败!"); } return(false); }
/// <summary> /// 根据主键删除实体对象,仅支持单主键id,依靠数据库的级联删除自动删除子实体 /// </summary> /// <typeparam name="TEntity">实体类型</typeparam> /// <param name="p_id">主键</param> /// <param name="p_isNotify">是否提示删除结果</param> /// <returns>true 删除成功</returns> public static async Task <bool> DelByID <TEntity>(long p_id, bool p_isNotify = true) { var model = EntitySchema.Get(typeof(TEntity)); bool suc = await Exec(model.Schema.SqlDelete, new { id = p_id }) == 1; if (p_isNotify) { Kit.Msg(suc ? "删除成功!" : "删除失败!"); } return(suc); }
/// <summary> /// 根据主键值删除实体对象,仅支持单主键,主键列名内部确定 /// </summary> /// <typeparam name="TEntity">实体类型</typeparam> /// <param name="p_id">主键值</param> /// <param name="p_isNotify">是否提示删除结果</param> /// <returns>true 删除成功</returns> public static bool DelByID <TEntity>(object p_id, bool p_isNotify = true) where TEntity : Entity { bool suc = _db.DelByPK <TEntity>(p_id); if (p_isNotify) { Kit.Msg(suc ? "删除成功!" : "删除失败!"); } return(suc); }
/// <summary> /// 执行重做操作 /// </summary> public void Redo() { if (CanRedo) { CmdAction action = _redoStack.Pop(); action.Redo(); if (_undoStack.Count < _depth) { _undoStack.Push(action); } OnCmdChanged(); } else { Kit.Msg("无操作可重做!"); } }
/// <summary> /// 执行撤消操作 /// </summary> public void Undo() { if (CanUndo) { CmdAction action = _undoStack.Pop(); action.Undo(); if (_redoStack.Count < _depth) { _redoStack.Push(action); } OnCmdChanged(); } else { Kit.Msg("没有可撤消的操作!"); } }
/// <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 = EntitySchema.Get(typeof(TEntity)); if (model.OnSaving != null) { // 保存前外部校验,不合格在外部抛出异常 if (!await OnSaving(model, p_entity)) { return(false); } } Dict dt = model.Schema.GetSaveSql(p_entity); int cnt = await Exec((string)dt["text"], (Dict)dt["params"]); if (cnt > 0) { p_entity.AcceptChanges(); if (p_isNotify) { Kit.Msg("保存成功!"); } return(true); } if (p_isNotify) { Kit.Warn("保存失败!"); } return(false); }
/// <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 = EntitySchema.Get(typeof(TEntity)); if (model.OnDeleting != null) { if (!await OnDeleting(model, p_entity)) { return(false); } } Dict dt = model.Schema.GetDeleteSql(new List <Row> { p_entity }); bool suc = await Exec((string)dt["text"], ((List <Dict>)dt["params"])[0]) == 1; if (p_isNotify) { if (suc) { Kit.Msg("删除成功!"); } else { Kit.Warn("删除失败!"); } } return(suc); }
/// <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); }
/// <summary> /// 将实体数据传输到服务端,由服务端DataProvider保存实体,用于需要触发领域事件或同步缓存的情况 /// </summary> /// <typeparam name="TEntity">实体类型</typeparam> /// <param name="p_entity">待保存的实体</param> /// <param name="p_isNotify">是否提示保存结果</param> /// <returns>是否成功</returns> public static async Task <bool> SaveBySvc <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 = EntitySchema.Get(typeof(TEntity)); bool suc = await new UnaryRpc( typeof(TSvc).Name, "EntityAccess.Save", p_entity, model.Schema.Name ).Call <bool>(); if (suc) { p_entity.AcceptChanges(); if (p_isNotify) { Kit.Msg("保存成功!"); } return(true); } if (p_isNotify) { Kit.Warn("保存失败!"); } return(false); }
/// <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); }
/// <summary> /// 多表批量删除 /// </summary> /// <param name="p_list"></param> /// <param name="p_isNotify"></param> /// <returns></returns> static async Task <bool> BatchDeleteMultiTypes(IList p_list, bool p_isNotify) { var dts = new List <Dict>(); foreach (var item in p_list) { if (item is Entity entity) { var model = EntitySchema.Get(item.GetType()); if (model.OnDeleting != null) { if (!await OnDeleting(model, entity)) { return(false); } } dts.Add(model.Schema.GetDeleteSql(new List <Row> { entity })); } else if (item is IList clist && clist.Count > 0) { Type tp = item.GetType(); if (tp.IsGenericType && tp.GetGenericArguments()[0].IsSubclassOf(typeof(Entity))) { // IList<Entity> 或 Table<Entity> var model = EntitySchema.Get(tp.GetGenericArguments()[0]); if (model.OnDeleting != null) { foreach (var ci in clist) { if (!await OnDeleting(model, ci)) { return(false); } } } dts.Add(model.Schema.GetDeleteSql(clist)); } } } // 不需要删除 if (dts == null || dts.Count == 0) { if (p_isNotify) { Kit.Msg(_unchangedMsg); } return(true); } bool suc = await BatchExec(dts) > 0; if (p_isNotify) { if (suc) { Kit.Msg("删除成功!"); } else { Kit.Warn("删除失败!"); } } return(suc); }
/// <summary> /// 多表增删改 /// </summary> /// <param name="p_list"></param> /// <param name="p_isNotify"></param> /// <returns></returns> static async Task <bool> BatchSaveMultiTypes(IList p_list, bool p_isNotify) { var dts = new List <Dict>(); foreach (var item in p_list) { if (item is Entity entity) { if (entity.IsAdded || entity.IsChanged) { var model = EntitySchema.Get(item.GetType()); if (model.OnSaving != null) { if (!await OnSaving(model, entity)) { return(false); } } dts.Add(model.Schema.GetSaveSql(entity)); } } else if (item is IList clist) { // 不判断列表项数0,因可能Table<Entity>只包含删除列表的情况! Type tp = item.GetType(); if (tp.IsGenericType && tp.GetGenericArguments()[0].IsSubclassOf(typeof(Entity))) { // IList<Entity> 或 Table<Entity> var model = EntitySchema.Get(tp.GetGenericArguments()[0]); if (model.OnSaving != null) { foreach (var ci in clist) { if (!await OnSaving(model, ci)) { return(false); } } } var cdts = model.Schema.GetBatchSaveSql(clist); if (cdts != null && cdts.Count > 0) { dts.AddRange(cdts); } } } } // 不需要保存 if (dts == null || dts.Count == 0) { if (p_isNotify) { Kit.Msg(_unchangedMsg); } return(true); } bool suc = await BatchExec(dts) > 0; if (suc) { foreach (var item in p_list) { if (item is Entity entity) { entity.AcceptChanges(); } else if (item is Table tbl) { tbl.AcceptChanges(); tbl.DeletedRows?.Clear(); } else if (item is IList clist && clist.Count > 0) { foreach (var ci in clist) { if (ci is Row row && (row.IsAdded || row.IsChanged)) { row.AcceptChanges(); } } } } if (p_isNotify) { Kit.Msg("保存成功!"); } return(true); } if (p_isNotify) { Kit.Warn("保存失败!"); } return(false); }
/// <summary> /// 批量删除实体,单表或多表,列表类型支持: /// <para>Table<Entity>,单表删除</para> /// <para>List<Entity>,单表删除</para> /// <para>IList,多表删除,成员可为Entity,List<Entity>,Table<Entity>的混合</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); }
/// <summary> /// 一个事务内批量保存实体数据,根据实体状态执行增改,Table<Entity>支持删除,列表类型支持: /// <para>Table<Entity>,单表增删改</para> /// <para>List<Entity>,单表增改</para> /// <para>IList,多表增删改,成员可为Entity,List<Entity>,Table<Entity>的混合</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); }