SplitTableDeleteByObjectProvider.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8. namespace SqlSugar
  9. {
  10. public class SplitTableDeleteByObjectProvider<T> where T : class, new()
  11. {
  12. public ISqlSugarClient Context;
  13. public DeleteableProvider<T> deleteobj;
  14. public T [] deleteObjects { get; set; }
  15. public int ExecuteCommand()
  16. {
  17. List<GroupModel> groupModels;
  18. int result;
  19. GroupDataList(deleteObjects, out groupModels, out result);
  20. foreach (var item in groupModels.GroupBy(it => it.GroupName))
  21. {
  22. var addList = item.Select(it => it.Item).ToList();
  23. result += this.Context.Deleteable<T>().Where(addList).AS(item.Key).ExecuteCommand();
  24. }
  25. return result;
  26. }
  27. public async Task<int> ExecuteCommandAsync()
  28. {
  29. List<GroupModel> groupModels;
  30. int result;
  31. GroupDataList(deleteObjects, out groupModels, out result);
  32. foreach (var item in groupModels.GroupBy(it => it.GroupName))
  33. {
  34. var addList = item.Select(it => it.Item).ToList();
  35. result +=await this.Context.Deleteable<T>().Where(addList).AS(item.Key).ExecuteCommandAsync();
  36. }
  37. return result;
  38. }
  39. private void GroupDataList(T[] datas, out List<GroupModel> groupModels, out int result)
  40. {
  41. var attribute = typeof(T).GetCustomAttribute<SplitTableAttribute>() as SplitTableAttribute;
  42. Check.Exception(attribute == null, $"{typeof(T).Name} need SplitTableAttribute");
  43. groupModels = new List<GroupModel>();
  44. var db = this.Context;
  45. foreach (var item in datas)
  46. {
  47. var value = db.SplitHelper<T>().GetValue(attribute.SplitType, item);
  48. var tableName = db.SplitHelper<T>().GetTableName(attribute.SplitType, value);
  49. groupModels.Add(new GroupModel() { GroupName = tableName, Item = item });
  50. }
  51. result = 0;
  52. }
  53. internal class GroupModel
  54. {
  55. public string GroupName { get; set; }
  56. public T Item { get; set; }
  57. }
  58. }
  59. }