Exemple #1
0
        /// <summary>
        /// 新增记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parse2InsertSql">生成完整SQL语句的函数</param>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        protected bool Insert <T>(Parse2InsertSQL parse2InsertSql, T entity)
        {
            bool isSuccess = false;

            if (entity == null)
            {
                return(isSuccess);
            }

            List <ParamInfo> paramInfos = new List <ParamInfo>(); // 入参实体集合
            string           sql        = AssembleInsertSql <T>(parse2InsertSql, entity, paramInfos);

            if (m_IsDebug)
            {
                Logger.WriteMsg2LogFile(sql);
            }

            object countObj = m_Db.ExecNonQuery(sql, paramInfos);

            if (null != countObj)
            {
                isSuccess = Convert.ToInt32(countObj) != 0;
            }

            return(isSuccess);
        }
Exemple #2
0
        /// <summary>
        /// 改变树节点序号
        /// </summary>
        /// <typeparam name="T">树节点类</typeparam>
        /// <param name="dbInstance">数据库操作对象</param>
        /// <param name="dbHelper">数据库操作帮助对象</param>
        /// <param name="idProperty">主键属性名</param>
        /// <param name="pIdProperty">父节点主键属性名</param>
        /// <param name="orderProperty">序号属性名</param>
        /// <param name="id">主键</param>
        /// <param name="sourcePID">源父节点主键</param>
        /// <param name="sourceIndex">源序号</param>
        /// <param name="newPID">新父节点主键</param>
        /// <param name="newIndex">新序号</param>
        /// <returns></returns>
        public static bool ChangeOrder<T>(IDBInstance dbInstance, MSSQLHelper dbHelper, string idProperty, string pIdProperty, string orderProperty,
            long? id, long? sourcePID, long? sourceIndex, long? newPID, long? newIndex)
            where T : class
        {
            PropertyInfo orderProp = typeof(T).GetProperty(orderProperty);

            if (newPID == null || newPID == sourcePID)
            {
                // 在同一父节点下改变序号
                long dVal = newIndex.Value - sourceIndex.Value;
                List<T> tObjs = null;
                if (dVal > 0)
                {
                    // 新序号在旧序号之后
                    tObjs = dbHelper.Query<T>(string.Format(" [{0}] > {1} and [{0}] <= {2} and [{3}] " + (sourcePID == 0 ? "IS NULL" : "=" + sourcePID) + " ",
                        orderProperty, sourceIndex, newIndex, pIdProperty), null, null);
                    for (int i = 0, len = tObjs.Count; i < len; ++i)
                    {
                        orderProp.SetValue(tObjs[i], CalcOrder(orderProp.PropertyType, orderProp.GetValue(tObjs[i], null), -1), null);
                    }
                }
                else if (dVal < 0)
                {
                    // 新序号在旧序号之前
                    tObjs = dbHelper.Query<T>(string.Format(" [{0}] >= {1} and [{0}] < {2} and [{3}] " + (sourcePID == 0 ? "IS NULL" : "=" + sourcePID) + " ",
                        orderProperty, newIndex, sourceIndex, pIdProperty), null, null);
                    for (int i = 0, len = tObjs.Count; i < len; ++i)
                    {
                        orderProp.SetValue(tObjs[i], CalcOrder(orderProp.PropertyType, orderProp.GetValue(tObjs[i], null), 1), null);
                    }
                }

                T targetTObj = typeof(T).GetConstructor(new List<Type>().ToArray()).Invoke(new List<object>().ToArray()) as T;
                targetTObj.GetType().GetProperty(idProperty).SetValue(targetTObj, id, null);
                orderProp.SetValue(targetTObj, newIndex, null);
                tObjs.Add(targetTObj);
                tObjs.ForEach(tInfo =>
                {
                    dbHelper.Update<T>(tInfo);
                });
                return true;
            }
            else
            {
                // 在不同父节点间下改变序号
                List<T> sourceParentChildInfos = dbHelper.Query<T>(string.Format(" [{0}] > {1} and [{2}] " + (sourcePID == 0 ? "IS NULL" : "=" + sourcePID) + " ",
                    orderProperty, sourceIndex, pIdProperty), null, null);
                List<T> targetParentChildInfos = dbHelper.Query<T>(string.Format(" [{0}] >= {1} and [{2}] " + (newPID == 0 ? "IS NULL" : "=" + newPID) + " ",
                    orderProperty, newIndex, pIdProperty), null, null);
                for (int i = 0, len = sourceParentChildInfos.Count; i < len; ++i)
                {
                    orderProp.SetValue(sourceParentChildInfos[i], CalcOrder(orderProp.PropertyType, orderProp.GetValue(sourceParentChildInfos[i], null), -1), null);
                }
                for (int i = 0, len = targetParentChildInfos.Count; i < len; ++i)
                {
                    orderProp.SetValue(targetParentChildInfos[i], CalcOrder(orderProp.PropertyType, orderProp.GetValue(targetParentChildInfos[i], null), 1), null);
                }

                List<T> totalTInfos = new List<T>();
                totalTInfos.AddRange(sourceParentChildInfos);
                totalTInfos.AddRange(targetParentChildInfos);
                totalTInfos.ForEach(tInfo =>
                {
                    dbHelper.Update<T>(tInfo);
                });

                TblAttr tblAttr = typeof(T).GetCustomAttributes(typeof(TblAttr), false)[0] as TblAttr;
                int count = dbInstance.ExecNonQuery(string.Format("UPDATE {0} SET [{1}]=" + (newPID == 0 ? "NULL" : newPID.Value.ToString()) + ",[{2}]=" + newIndex + " WHERE [{3}]=" + id.Value,
                    tblAttr.Name, pIdProperty, orderProperty, idProperty), null);
                return count > 0;
            }
        }