1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- using CZFW.Framework.Interface;
- using CZFW.Framework.Model.Entity;
- using System;
- using System.Collections.Generic;
- using CZFW.Framework.Model;
- using CZFW.Framework.Model.ViewModel;
- using System.Linq;
- namespace CZFW.Framework.Logic
- {
- public class LogicBase<TEntityBase> : SlimLogicBase<TEntityBase>, ILogicBase<TEntityBase>
- where TEntityBase : EntityBase, new()
- {
- public LogicBase(CZDbContext db) : base(db)
- {
- DbContext = db;
- }
- public LogicBase() : base() { }
- private IList<TEntityBase> wrapEntities(IList<TEntityBase> entites, bool initAdd = true, int maxId = 0)
- {
- int count = maxId;
- foreach (var entity in entites)
- {
- if (entity.LastModifyTime == null)
- entity.LastModifyTime = DateTime.Now;
- if (entity.CreatedTime == null && initAdd)
- entity.CreatedTime = DateTime.Now;
- var model = OperatorProvider.Provider.GetCurrent();
- if (model != null)
- {
- if (initAdd)
- entity.CreatedBy = model.UserId;
- entity.LastModifyBy = model.UserId;
- }
- count++;
- if (!entity.SortOrder.HasValue)
- entity.SortOrder = count;
- }
- return entites;
- }
- public override ResultModel AddEntity(TEntityBase entity, bool save = true)
- {
- var list = new List<TEntityBase>();
- list.Add(entity);
- int maxId = Queryable.Count() > 0 ? Queryable.Max(x => x.Id) : 0;
- return base.AddEntity(wrapEntities(list, true, maxId)[0], save);
- }
- public override ResultModel AddEntities(IList<TEntityBase> entities, bool save = true, bool skipInvalid = true)
- {
- int maxId = Queryable.Count() > 0 ? Queryable.Max(x => x.Id) : 0;
- var list = wrapEntities(entities, true, maxId);
- return base.AddEntities(list, save);
- }
- public override ResultModel EditEntity(TEntityBase entity, bool save = true)
- {
- var list = new List<TEntityBase>();
- list.Add(entity);
- return base.EditEntity(wrapEntities(list, false)[0], save);
- }
- public IQueryable<TEntityBase> GetSortedQueryable()
- {
- return Queryable.OrderByDescending(x => x.SortOrder);
- }
- public interface IVideo
- {
- }
- }
- }
|