/// <summary> /// 申请一个新的序列号 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="handle"></param> /// <returns></returns> public CommonContext NewSequence <T>( IUserSession session, Action <TB_sys_sequence, string> handle) where T : TBObject { const string DEFAULT_PREFIX = "AA"; const string DEFAULT_FORMAT = "{0:00000000}"; const string SHORT_FORMAT = "{0:0000}"; var table = this.TableInfo <T>().TableName; var seq = GetTable <TB_sys_sequence>().SingleOrDefault(s => s.Entity == table); if (seq == null) { throw DTException.NotFound <TB_sys_sequence>(table); } seq.Count += seq.Step; if (seq.Count > seq.Limit) { throw new DTException("自增 ID 超出下限", s => s.Record("table", table)); } var prefix = string.IsNullOrEmpty(seq.Prefix.Trim()) ? DEFAULT_PREFIX : seq.Prefix; if (seq.ShortForm) { handle(seq, prefix + string.Format(SHORT_FORMAT, seq.Count)); } else { handle(seq, prefix + string.Format(DEFAULT_FORMAT, seq.Count)); } Endorse(session, seq, false); return(this); }
public T Retrieve(Expression <Func <T, bool> > selector, bool exceptionIfNotExists = false) { T tObject = _Context.GetTable <T>().Where(selector).SingleOrDefault(); if (exceptionIfNotExists && tObject == null) { throw DTException.NotFound <T>(selector.ToString()); } return(tObject); }
/// <summary> /// 更新单个对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="handle"></param> /// <returns></returns> public CommonContext Single <T>( Expression <Func <T, bool> > get, Action <T> handle = null) where T : TBObject { var obj = GetTable <T>().SingleOrDefault(get); if (obj == null) { throw DTException.NotFound <T>(get.ToStringEx()); } if (handle != null) { handle(obj); } return(this); }
/// <summary> /// 更新单个对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="session"></param> /// <param name="get"></param> /// <param name="handle"></param> /// <returns></returns> public CommonContext Update <T>(IUserSession session, Expression <Func <T, bool> > get, Action <T> handle = null) where T : TBObject { var obj = GetTable <T>().SingleOrDefault(get); if (obj == null) { throw DTException.NotFound <T>(get.ToStringEx()); } if (handle != null) { handle(obj); } Endorse <T>(session, obj, false); return(this); }
/// <summary> /// 修改部门的父部门 /// </summary> /// <param name="obj"></param> /// <param name="newParentId"></param> public CommonContext ResolveParent(TB_department obj, string newParentId) { if (string.IsNullOrEmpty(obj.Id)) { throw new ArgumentException("obj 对象 id 未指明"); } if (obj.Id == newParentId) { throw new ArgumentException("不能自选定为父亲"); } var oldPath = obj.Path + obj.Id + "/"; var subTree = Departments.Where(d => d.Path.StartsWith(oldPath)).ToList(); if (string.IsNullOrEmpty(newParentId)) { // 放置根 obj.ParentId = null; obj.Path = "/"; } else { Departments.SingleOrDefault(p => p.Id == newParentId).IfNN(parent => { // 检查循环设置的情况 if (parent.Path.StartsWith(oldPath)) { throw new Exception("不能循环设置树"); } obj.ParentId = parent.Id; obj.Path = parent.Path + parent.Id + "/"; }, () => { throw DTException.NotFound <TB_department>(newParentId); }); } subTree.ForEach(c => c.Path = c.Path.Replace(oldPath, obj.Path + obj.Id + "/")); return(this); }