LogicBase.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using CZFW.Framework.Interface;
  2. using CZFW.Framework.Model.Entity;
  3. using System;
  4. using System.Collections.Generic;
  5. using CZFW.Framework.Model;
  6. using CZFW.Framework.Model.ViewModel;
  7. using System.Linq;
  8. namespace CZFW.Framework.Logic
  9. {
  10. public class LogicBase<TEntityBase> : SlimLogicBase<TEntityBase>, ILogicBase<TEntityBase>
  11. where TEntityBase : EntityBase, new()
  12. {
  13. public LogicBase(CZDbContext db) : base(db)
  14. {
  15. DbContext = db;
  16. }
  17. public LogicBase() : base() { }
  18. private IList<TEntityBase> wrapEntities(IList<TEntityBase> entites, bool initAdd = true, int maxId = 0)
  19. {
  20. int count = maxId;
  21. foreach (var entity in entites)
  22. {
  23. if (entity.LastModifyTime == null)
  24. entity.LastModifyTime = DateTime.Now;
  25. if (entity.CreatedTime == null && initAdd)
  26. entity.CreatedTime = DateTime.Now;
  27. var model = OperatorProvider.Provider.GetCurrent();
  28. if (model != null)
  29. {
  30. if (initAdd)
  31. entity.CreatedBy = model.UserId;
  32. entity.LastModifyBy = model.UserId;
  33. }
  34. count++;
  35. if (!entity.SortOrder.HasValue)
  36. entity.SortOrder = count;
  37. }
  38. return entites;
  39. }
  40. public override ResultModel AddEntity(TEntityBase entity, bool save = true)
  41. {
  42. var list = new List<TEntityBase>();
  43. list.Add(entity);
  44. int maxId = Queryable.Count() > 0 ? Queryable.Max(x => x.Id) : 0;
  45. return base.AddEntity(wrapEntities(list, true, maxId)[0], save);
  46. }
  47. public override ResultModel AddEntities(IList<TEntityBase> entities, bool save = true, bool skipInvalid = true)
  48. {
  49. int maxId = Queryable.Count() > 0 ? Queryable.Max(x => x.Id) : 0;
  50. var list = wrapEntities(entities, true, maxId);
  51. return base.AddEntities(list, save);
  52. }
  53. public override ResultModel EditEntity(TEntityBase entity, bool save = true)
  54. {
  55. var list = new List<TEntityBase>();
  56. list.Add(entity);
  57. return base.EditEntity(wrapEntities(list, false)[0], save);
  58. }
  59. public IQueryable<TEntityBase> GetSortedQueryable()
  60. {
  61. return Queryable.OrderByDescending(x => x.SortOrder);
  62. }
  63. public interface IVideo
  64. {
  65. }
  66. }
  67. }