Esempio n. 1
0
        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));
        }
Esempio n. 2
0
        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);
            }
        }
Esempio n. 3
0
        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;
        }
Esempio n. 4
0
        public bool Del(int[] ids)
        {
            string condition = new ConditionHelper().And("Id", new { Id = ids }, CompareType.In).ToString();

            return(_provider.Delete(condition, new { Id = ids }));
        }
Esempio n. 5
0
        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));
        }
Esempio n. 6
0
        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);
            }
        }