Example #1
0
        /// <summary>
        /// 生成主键查询条件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="keyList"></param>
        /// <returns></returns>
        SuuchaExpression KeyCondition <T>(List <object[]> keyList)
        {
            var condition = SuuchaExpression.Equal(SuuchaExpression.Constant(0), SuuchaExpression.Constant(1));

            PropertyInfo[] pis = EntityTool.GetKeyProperty <T>();
            foreach (var values in keyList)
            {
                if (values.Length != pis.Length)
                {
                    throw new Exception("主键值数量必须为:" + pis.Length);
                }
                SuuchaBinaryExpression kExp = null;
                for (int i = 0; i < pis.Length; i++)
                {
                    kExp = 0 == i?SuuchaExpression.Equal(pis[i].Name, values[i]) : SuuchaExpression.And(kExp, SuuchaExpression.Equal(pis[i].Name, values[i]));
                }
                condition = SuuchaExpression.Or(condition, kExp);
            }
            return(condition);
        }
Example #2
0
        public async Task <object> Modify(string token, dynamic data, string json = "")
        {
            try
            {
                data = data ?? JsonConvert.DeserializeObject <dynamic>(json);
                if (data == null)
                {
                    throw new Exception("解析Json对象失败");
                }
                PropertyInfo[] pis = EntityTool.GetKeyProperty <T>();
                if (data.GetType() == typeof(JObject))
                {
                    string str = data.ToString();
                    T      t   = JsonConvert.DeserializeObject <T>(str);
                    if (t is BaseEntity)
                    {
                        (t as BaseEntity).Modifier = new UserService().GetByToken(token).Name;
                    }
                    JObject       jObject = data as JObject;
                    List <object> ids     = new List <object>();
                    foreach (var p in pis)
                    {
                        PropertyInfo tp = t.GetType().GetProperty(p.Name, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
                        if (tp == null)
                        {
                            throw new Exception("实体未包含主键:" + p.Name);
                        }
                        ids.Add(tp.GetValue(t));
                    }
                    T model = await Get(ids.ToArray());

                    if (model == null)
                    {
                        throw new Exception("查找修改对象失败");
                    }
                    foreach (var item in jObject)
                    {
                        PropertyInfo mp = model.GetType().GetProperty(item.Key, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
                        if (mp != null)
                        {
                            PropertyInfo tp = t.GetType().GetProperty(item.Key, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
                            mp.SetValue(model, tp.GetValue(t));
                        }
                    }
                    List <T> list = new List <T>()
                    {
                        model
                    };
                    OnEntityModifing?.Invoke(ref list);
                    int r = await Put(model);

                    OnEntityModified?.Invoke(ref list);
                    return(r);
                }
                else if (data.GetType() == typeof(JArray))
                {
                    string   str       = data.ToString();
                    JArray   jarray    = JsonConvert.DeserializeObject(str) as JArray;
                    List <T> list      = JsonConvert.DeserializeObject <List <T> >(str);
                    List <T> modellist = new List <T>();
                    for (int i = 0; i < list.Count; i++)
                    {
                        var           t       = list[i];
                        JObject       jObject = jarray[i] as JObject;
                        List <object> ids     = new List <object>();
                        if (t is BaseEntity)
                        {
                            (t as BaseEntity).Modifier = new UserService().GetByToken(token).Name;
                        }
                        foreach (var p in pis)
                        {
                            PropertyInfo tp = t.GetType().GetProperty(p.Name, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
                            if (tp == null)
                            {
                                throw new Exception("实体未包含主键:" + p.Name);
                            }
                            ids.Add(tp.GetValue(t));
                        }
                        T model = await Get(ids.ToArray());

                        if (model == null)
                        {
                            throw new Exception("查找修改对象失败");
                        }
                        foreach (var item in jObject)
                        {
                            PropertyInfo mp = model.GetType().GetProperty(item.Key, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
                            if (mp != null)
                            {
                                PropertyInfo tp = t.GetType().GetProperty(item.Key, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
                                mp.SetValue(model, tp.GetValue(t));
                            }
                        }
                        modellist.Add(model);
                    }
                    OnEntityModifing?.Invoke(ref modellist);
                    int r = await PutList(modellist);

                    OnEntityModified?.Invoke(ref modellist);
                    return(r);
                }
                throw new Exception("解析Json对象失败");
            }
            catch (Exception e)
            {
                throw e;
            }
        }