Exemple #1
0
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Insert(T model, string incrementFieldName = null, string sameValuePropertyName = null)
        {
            if (!string.IsNullOrEmpty(sameValuePropertyName))
            {
                PropertyInfo property = model.GetType().GetProperty(sameValuePropertyName);
                string       check    = new ConditionHelper().And(sameValuePropertyName, property.GetValue(model, null), CompareType.Equal).ToString();

                DynamicParameters p = new DynamicParameters();
                p.Add($"@{sameValuePropertyName}", property.GetValue(model, null));

                if (_provider.GetItem <T>(check, p) != null)
                {
                    throw new SameValueException();
                }
            }
            List <string> fieldsBuilder = new List <string>();
            List <string> valuesBuilder = new List <string>();

            if (string.IsNullOrEmpty(incrementFieldName))
            {
                Type           type      = typeof(T);
                PropertyInfo[] propertys = type.GetProperties();

                foreach (var property in propertys)
                {
                    fieldsBuilder.Add($"{property.Name}");
                    valuesBuilder.Add($"@{property.Name}");
                }
            }
            else
            {
                Type           type      = typeof(T);
                PropertyInfo[] propertys = type.GetProperties();
                foreach (var property in propertys)
                {
                    if (property.Name != incrementFieldName)
                    {
                        fieldsBuilder.Add($"{property.Name}");
                        valuesBuilder.Add($"@{property.Name}");
                    }
                }
            }
            return(_provider.Insert(string.Join(",", fieldsBuilder), string.Join(",", valuesBuilder), model));
        }
Exemple #2
0
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="idFiledName">id的字段名</param>
        /// <param name="ids">ids</param>
        /// <returns></returns>
        public bool Del <R>(string idFiledName, params R[] ids) where R : struct
        {
            if (BeginDel != null)
            {
                BeginDel.Invoke(ids as int[]);
            }
            string            condition = new ConditionHelper().And(idFiledName, ids, CompareType.In).ToString();
            DynamicParameters p         = new DynamicParameters();

            p.Add($"@{idFiledName}", ids);
            if (_provider.Delete(condition, p))
            {
                if (EndDel != null)
                {
                    EndDel.Invoke();
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public static ConditionHelper Build(object searchObj)
        {
            Type            objType = searchObj.GetType();
            ConditionHelper result  = new ConditionHelper();

            foreach (PropertyInfo propInfo in objType.GetProperties())
            {
                var attrs = propInfo.GetCustomAttributes(typeof(QueryTypeAttribute), true);
                if (attrs.Length > 0)
                {
                    var value = propInfo.GetValue(searchObj, null);
                    if (value is string)
                    {
                        var str = value as string;
                        result.And(propInfo.Name, ref str, (attrs[0] as QueryTypeAttribute).QueryType);
                    }
                    else
                    {
                        result.And(propInfo.Name, value, (attrs[0] as QueryTypeAttribute).QueryType);
                    }
                }
            }
            return(result);
        }
Exemple #4
0
        /// <summary>
        /// 删除,只适用于主键为Id的删除
        /// </summary>
        /// <param name="ids"></param>
        public bool Del(int[] ids)
        {
            string condition = new ConditionHelper().And("Id", new { Id = ids }, CompareType.In).ToString();

            return(_provider.Delete(condition, new { Id = ids }));
        }
Exemple #5
0
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="offset">略过</param>
        /// <param name="limit">取</param>
        /// <param name="searchModel">查询对象(需要查询的字段需要添加QueryType属性)</param>
        /// <param name="order">排序(order by id desc)</param>
        /// <returns></returns>
        public PageableData <T> GetPage(int offset, int limit, object searchModel, string order = "")
        {
            string condition = ConditionHelper.Build(searchModel).ToString();

            return(_provider.GetPage <T>(condition, searchModel, order, offset, limit));
        }
Exemple #6
0
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model">对象</param>
        /// <param name="fields">更新字段</param>
        /// <param name="idPropertyName">Id字段名</param>
        /// <param name="sameValuePropertyName">检测相同值字段名</param>
        /// <returns></returns>
        public bool Update(T model, string[] fields = null, string idPropertyName = "Id", string sameValuePropertyName = "")
        {
            if (BeginUpdate != null)
            {
                BeginUpdate.Invoke(model);
            }

            PropertyInfo editProperty = model.GetType().GetProperty("EditTime");

            if (editProperty != null)
            {
                editProperty.SetValue(model, DateTime.Now, null);
            }

            PropertyInfo propertyId = model.GetType().GetProperty(idPropertyName);

            if (!string.IsNullOrEmpty(sameValuePropertyName))
            {
                PropertyInfo propertySameValue = model.GetType().GetProperty(sameValuePropertyName);
                string       check             = new ConditionHelper().And(idPropertyName, propertyId.GetValue(model, null), CompareType.Unequal)
                                                 .And(sameValuePropertyName, propertySameValue.GetValue(model, null), CompareType.Equal).ToString();

                DynamicParameters p = new DynamicParameters();
                p.Add($"@{idPropertyName}", propertyId.GetValue(model, null));
                p.Add($"@{sameValuePropertyName}", propertySameValue.GetValue(model, null));

                if (_provider.GetItem <T>(check, p) != null)
                {
                    throw new SameValueException();
                }
            }
            string fieldsStr = "";

            if (fields != null)
            {
                List <string> builder = new List <string>();
                foreach (var field in fields)
                {
                    builder.Add($"{field} = @{field}");
                }
                fieldsStr = string.Join(",", builder.ToArray());
            }
            else
            {
                Type           type      = typeof(T);
                PropertyInfo[] propertys = type.GetProperties();
                List <string>  builder   = new List <string>();
                foreach (var property in propertys)
                {
                    if (property.Name != sameValuePropertyName)
                    {
                        builder.Add($"{property.Name} = @{property.Name}");
                    }
                }
                fieldsStr = string.Join(",", builder);
            }
            string condition = new ConditionHelper().And(idPropertyName, propertyId.GetValue(model, null), CompareType.Equal).ToString();

            if (_provider.Update(condition, fieldsStr, model))
            {
                if (EndUpdate != null)
                {
                    EndUpdate.Invoke(model);
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }