/// <summary>
        /// APP通用数据访问接口(CRUD)
        /// </summary>
        /// <param name="parameters">请求接口所带的参数模型对象</param>
        /// <returns>请求结果</returns>
        public JsonResult Index(CommonApiModel parameters)
        {
            var bllEntity  = BllFactory.GetBllInstance(parameters.TableName);
            var entityType = bllEntity.GetType();
            var methodName = parameters.Operate.ToString();

            var invokeParameters = PrepareInvokeParameters(parameters);

            try
            {
                if (parameters.Operate == DbOperateType.BulkInsert)
                {
                    entityType.InvokeMember(methodName, BindingFlags.InvokeMethod, null, bllEntity, invokeParameters);

                    return(Json(ErrorModel.BulkInsertSuccess));
                }

                var result  = entityType.InvokeMember(methodName, BindingFlags.InvokeMethod, null, bllEntity, invokeParameters);
                var jsonObj = ErrorModel.GetDataSuccess(result, parameters.TableName);

                return(new CustomJsonResult {
                    Data = jsonObj,
                    MaxJsonLength = int.MaxValue,
                    FormateStr = "yyyy-MM-dd HH:mm:ss"
                });
            }
            catch (Exception ex)
            {
                ExceptionLogBll.ExceptionPersistence("AppController.cs", "AppController", ex);

                return(Json(ErrorModel.InputError, JsonRequestBehavior.AllowGet));
            }
        }
Exemple #2
0
        private List <Func <bool> > GetDelegates(Dictionary <string, object> data)
        {
            var delegates = new List <Func <bool> >();

            foreach (var couple in data)
            {
                var t    = couple.Key;
                var json = JsonHelper.Serialize(couple.Value);

                // 1. 打开主键插入开关 SET IDENTITY_INSERT [dbo].[ExamType] ON
                // 2. 插入数据
                // 3. 关闭主键插入开关 SET IDENTITY_INSERT [dbo].[ExamType] OFF
                delegates.Add(() =>
                {
                    var bllInstance = BllFactory.GetBllInstance(t) as IBll;

                    var format   = "SET IDENTITY_INSERT [dbo].[{0}] {1};";
                    var setOnSql = string.Format(format, t, "ON");
                    bllInstance.ExecuteSql(setOnSql);

                    bllInstance.BulkInsert(json);

                    var setOffSql = string.Format(format, t, "OFF");
                    bllInstance.ExecuteSql(setOffSql);

                    return(true);
                });
            }

            return(delegates);
        }
Exemple #3
0
        /// <summary>
        /// 对数据的批量更新
        /// </summary>
        /// <param name="tableName">目标表格名称</param>
        /// <param name="startId">当前更新记录开始位置(不包括所传入的id)</param>
        /// <param name="updateType">更新类型枚举</param>
        public static void BulkUpdate(string tableName, int startId, DataUpdateType updateType = DataUpdateType.Insert)
        {
            var bllEntity = BllFactory.GetBllInstance(tableName);
            var bllType   = bllEntity.GetType();

            try
            {
                var whereStr = "Id>" + startId;
                var idList   = (IEnumerable <object>)bllType.InvokeMember("QuerySingleColumn", BindingFlags.InvokeMethod, null, bllEntity, new object[] { whereStr, "Id" });

                var updateModelList = idList.Select(id => new DbUpdateLog
                {
                    TableName  = tableName,
                    TargetId   = (int)id,
                    UpdateType = (int)updateType,
                    UpdateTime = DateTime.Now
                });

                UpdateBll.BulkInsert(updateModelList);
            }
            catch (Exception ex)
            {
                ExceptionLogBll.ExceptionPersistence("DataUpdateLog.cs", "DataUpdateLog", ex);
            }
        }
Exemple #4
0
        private List <Func <bool> > CreateDbupadwLogDelegates(Dictionary <string, object> data)
        {
            var delegates = new List <Func <bool> >();

            foreach (var couple in data)
            {
                var t = couple.Key;
                if (t.Equals("DbUpdateLog"))
                {
                    var json = JsonHelper.Serialize(couple.Value);
                    delegates.Add(() =>
                    {
                        var bllInstance = BllFactory.GetBllInstance(t) as IBll;
                        var format      = "SET IDENTITY_INSERT [dbo].[{0}] {1};";
                        var setOnSql    = string.Format(format, t, "ON");
                        bllInstance.ExecuteSql(setOnSql);

                        bllInstance.BulkInsert(json);

                        var setOffSql = string.Format(format, t, "OFF");
                        bllInstance.ExecuteSql(setOffSql);

                        return(true);
                    });
                }
            }
            return(delegates);
        }
        public JsonResult GetListForVue(string json)
        {
            var param      = JsonHelper.Deserialize <JObject>(json);
            var page       = param.Value <int>("page");
            var size       = param.Value <int>("size");
            var order      = param.Value <string>("order"); // 排序字段
            var desc       = param.Value <bool>("desc");    // 是否降序
            var table      = param.Value <string>("table");
            var conditions = param.Value <JObject>("conditions");
            var query      = BuildQueryCondition(table, conditions);

            var bll = BllFactory.GetBllInstance(table);

            if (bll != null)
            {
                var type = bll.GetType();

                var arguments = new object[] { page, size, query, order, desc, 0 };
                var list      = type.InvokeMember("QueryPageList", BindingFlags.InvokeMethod, null, bll,
                                                  arguments);

                return(Json(ErrorModel.GetDataSuccess(new {
                    total = (int)arguments[5],
                    list
                })));
            }

            return(Json(ErrorModel.GetDataFailed));
        }
        public JsonResult GetSingle(int id, string target)
        {
            if (string.IsNullOrEmpty(target) || id == 0)
            {
                return(Json(ErrorModel.InputError));
            }

            var bllInstance = BllFactory.GetBllInstance(target);

            if (bllInstance == null)
            {
                return(Json(ErrorModel.InputError));
            }

            var type = bllInstance.GetType();
            var data = type.InvokeMember("QuerySingle", BindingFlags.InvokeMethod, null, bllInstance,
                                         new object[] { id, null });

            if (data == null)
            {
                return(Json(ErrorModel.GetDataFailed));
            }

            return(Json(ErrorModel.GetDataSuccess(data)));
        }
        /// <summary>
        /// 获取数据列表的公共方法(通过表名反映获取)
        /// </summary>
        /// <returns></returns>
        public JsonResult GetList()
        {
            var instructorId = Request["instructorId"].ToInt32();
            var month        = Request["month"].ToDateTime();
            var tableName    = Request["target"];

            if (string.IsNullOrEmpty(tableName))
            {
                return(Json(ErrorModel.InputError));
            }

            var json = JqueryDataTableAjaxHelper.GetPageListJson(
                Request,
                (pageIndex, pageSize) =>
            {
                var bllInstance = BllFactory.GetBllInstance(tableName);
                if (bllInstance != null)
                {
                    var type = bllInstance.GetType();

                    try
                    {
                        // build condition
                        var conditionList = new List <string>();
                        if (instructorId > 0)
                        {
                            conditionList.Add("InstructorId=" + instructorId);
                        }

                        if (month != default(DateTime))
                        {
                            var start = month.AddDays(-1);
                            var end   = month.AddMonths(1);
                            conditionList.Add($"UploadTime>'{start}' AND UploadTime<'{end}'");
                        }

                        var condition  = string.Join(" AND ", conditionList);
                        var orderField = Request["order"] ?? "UploadTime";
                        var arguments  = new object[] { pageIndex, pageSize, condition, orderField, true, 0 };
                        var list       = type.InvokeMember("QueryPageList", BindingFlags.InvokeMethod, null, bllInstance,
                                                           arguments);

                        return(new KeyValuePair <int, object>((int)arguments[5], list));
                    }
                    catch (Exception ex)
                    {
                        ExceptionLogBll.ExceptionPersistence("InstructorController.cs", "InstructorController", ex);

                        return(new KeyValuePair <int, object>());
                    }
                }

                return(new KeyValuePair <int, object>());
            });

            return(Json(json));
        }
Exemple #8
0
        private static Dictionary <string, int> GetNewStartsDict()
        {
            var tables        = Utils.GetClient2ServerTables();
            var newStartsDict = new Dictionary <string, int>();

            tables.ForEach(t =>
            {
                var bllInstance = BllFactory.GetBllInstance(t) as IBll;
                var maxId       = (int)bllInstance.GetMaxId();
                newStartsDict.Add(t, maxId);
            });
            return(newStartsDict);
        }
Exemple #9
0
        private List <Func <bool> > CreateUpdateDelegates(Dictionary <string, object> data)
        {
            var delegates = new List <Func <bool> >();

            foreach (var couple in data)
            {
                var t           = couple.Key;
                var list        = couple.Value as List <object>;
                var bllInstance = BllFactory.GetBllInstance(t) as IBll;
                foreach (var o in list)
                {
                    delegates.Add(() => bllInstance.Update(o));
                }
            }

            return(delegates);
        }
        public JsonResult GetList(GetListParams parameters)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var bllInstance = BllFactory.GetBllInstance(parameters.TableName);
                    if (bllInstance == null)
                    {
                        return(Json(ErrorModel.InputError));
                    }

                    var condition = string.IsNullOrEmpty(parameters.Conditions)
                        ? string.Empty
                        : parameters.Conditions.Replace("###", " AND ");

                    var fields = string.IsNullOrEmpty(parameters.Fields)
                        ? null
                        : parameters.Fields.Split(new[] { "###" }, StringSplitOptions.RemoveEmptyEntries);

                    object data;
                    var    type = bllInstance.GetType();
                    if (parameters.PageIndex <= 0 || parameters.PageSize <= 0)
                    {
                        data = type.InvokeMember("QueryList", BindingFlags.InvokeMethod, null, bllInstance,
                                                 new object[] { condition, fields, null, parameters.OrderField, parameters.Desending });
                    }
                    else
                    {
                        data = type.InvokeMember("QueryPageList", BindingFlags.InvokeMethod, null, bllInstance,
                                                 new object[] { parameters.PageIndex, parameters.PageSize, condition, fields, null, parameters.OrderField, parameters.Desending });
                    }

                    return(Json(ErrorModel.GetDataSuccess(data)));
                }
                catch (Exception ex)
                {
                    ExceptionLogBll.ExceptionPersistence(nameof(CommonController), nameof(CommonController), ex);

                    return(Json(ErrorModel.InputError));
                }
            }

            return(Json(ErrorModel.InputError));
        }
Exemple #11
0
        private List <Func <bool> > CreateDeleteDelegates(Dictionary <string, object> data)
        {
            var delegates = new List <Func <bool> >();
            var logs      = data[nameof(DbUpdateLog)] as List <DbUpdateLog>;
            var groups    = logs.GroupBy(item => item.TableName);

            foreach (var g in groups)
            {
                var table = g.Key;
                var list  = g.ToList();
                if (list.Any())
                {
                    var bll = BllFactory.GetBllInstance(table) as IBll;
                    var ids = list.Select(o => o.TargetId);
                    var sql = $"DELETE FROM {table} WHERE Id IN({string.Join(",", ids)})";
                    delegates.Add(() => bll.ExecuteSql(sql) > 0);
                }
            }
            return(delegates);
        }
Exemple #12
0
        private List <string> GetFilePathList(IEnumerable <DbUpdateLog> dbLog, string table, string filePathField)
        {
            var result  = new List <string>();
            var groups  = dbLog.GroupBy(o => o.TableName);
            var fileLog = groups.SingleOrDefault(g => g.Key == table);

            if (fileLog != null)
            {
                try
                {
                    var idList    = fileLog.Select(o => o.TargetId);
                    var condition = $" Id IN ({string.Join(",", idList)})";
                    var bll       = BllFactory.GetBllInstance(table) as IBll;
                    var data      = bll.QueryList(condition);
                    foreach (var o in data)
                    {
                        var path = (string)ReflectorHelper.GetPropertyValue(o, filePathField);
                        result.Add(path);
                    }
                }
                catch (Exception ex)
                {
                    new ExceptionLogBll().Insert(new ExceptionLog
                    {
                        ClassName  = nameof(ServerSync),
                        FileName   = nameof(ServerSync),
                        HappenTime = DateTime.Now,
                        Instance   = ex.Source,
                        Message    = ex.Message,
                        Source     = 1,
                        MethodName = nameof(ServerSync.GetFilePathList),
                        StackTrace = ex.StackTrace
                    });
                }
            }

            return(result);
        }
Exemple #13
0
        public Dictionary <string, object> GetNewData(Dictionary <string, int> startsDict)
        {
            var tables = Utils.GetClient2ServerTables();
            var dict   = new Dictionary <string, object>();

            tables.ForEach(table =>
            {
                var maxId = 0;
                if (startsDict.ContainsKey(table))
                {
                    maxId = startsDict[table];
                }

                var bllInstance = BllFactory.GetBllInstance(table) as IBll;
                var list        = bllInstance.QueryList($"Id>{maxId}");
                if (list.Any())
                {
                    dict.Add(table, list);
                }
            });

            return(dict);
        }
Exemple #14
0
        /// <summary>
        /// 向服务端数据库添加客户端新产生的数据
        /// </summary>
        /// <param name="data">从客户端导出的 json 格式的数据集</param>
        public bool AddNewData(Dictionary <string, object> data)
        {
            var delegates = new List <Func <bool> >();

            foreach (var couple in data)
            {
                var table = couple.Key;
                var json  = JsonHelper.Serialize(couple.Value);
                delegates.Add(() =>
                {
                    var bllInstance = BllFactory.GetBllInstance(table) as IBll;
                    bllInstance.BulkInsert(json);
                    return(true);
                });
            }

            if (delegates.Any())
            {
                var bll = new TraficFilesBll();
                return(bll.ExecuteTransation(delegates.ToArray()));
            }

            return(true);
        }
Exemple #15
0
        /// <summary>
        /// 将源数据库中需要拷贝到 sqlite 数据库中的所有表的所有数据拷贝入 sqlite 数据库
        /// </summary>
        private void CopyFromSourceDb()
        {
            foreach (var pair in _tableFields)
            {
                // 根据表名获取对应的 bll 对象
                object bll;
                try
                {
                    // 对无法创建对应的 Bll 层对象的表名
                    // 程序会认为它不存在于服务器数据库中
                    // 因此对此类表予以忽略
                    bll = BllFactory.GetBllInstance(pair.Key);
                }
                catch (Exception ex)
                {
                    continue;
                }

                var list  = ReflectorHelper.InvokeMethod(bll, "QueryAll") as IEnumerable;
                var table = GetDataTable(pair.Value, list);

                Sqlite.BulkInsert(_sqliteConnStr, pair.Key, table);
            }
        }