Ejemplo n.º 1
0
        public static Sys_Config_ListPage GetListConfig(string moduleName, out Sys_Config_ListPage M_Model, out List <Sys_Config_Fieled> D_List)
        {
            ADOSys_Config_ListPage ListInstance   = new ADOSys_Config_ListPage();
            ADOSys_Config_Fieled   FieledInstance = new ADOSys_Config_Fieled();

            M_Model = null;
            D_List  = null;
            try
            {
                if (!string.IsNullOrEmpty(moduleName))
                {
                    //第一步:根据传入模块获取Grid配置
                    List <Sys_Config_ListPage> _ListPage = ListInstance.GetListByWhere(" AND M_ModuleCode='" +
                                                                                       moduleName + "'");
                    if (_ListPage.Count > 0)
                    {
                        M_Model = _ListPage[0];
                    }
                    //第二步:根据模块获取所有字段
                    D_List = FieledInstance.GetListByWhere(" AND D_ModuleCode='" + moduleName + "'");
                }
                else
                {
                    M_Model = null;
                    D_List  = null;
                }
            }
            catch (System.Exception ex)
            {
                M_Model = null;
                D_List  = null;
            }

            return(M_Model);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name='model'>实体</param>
        /// <returns>bool</returns>
        public bool Update(Sys_Config_ListPage model)
        {
            bool result  = false;
            var  e       = new ModelExceptions();
            int  _rseult = instance.Update(model);

            if (_rseult > 0)
            {
                result = true;
            }
            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 获取实体数据
        /// </summary>
        /// <param name='strCondition'>条件(AND Code='11')</param>
        /// <returns>model</returns>
        public Sys_Config_ListPage GetModel(string strCondition)
        {
            List <Sys_Config_ListPage> list  = instance.GetListByWhere(strCondition);
            Sys_Config_ListPage        model = new Sys_Config_ListPage();

            if (list != null && list.Count > 0)
            {
                model = list[0];
            }
            else
            {
                model = null;
            }
            return(model);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 插入数据
        /// </summary>
        /// <param name='model'>实体</param>
        /// <param name='model'>是否完成验证</param>
        /// <returns>bool</returns>
        public bool Insert(Sys_Config_ListPage model, bool IsValid)
        {
            var  e      = new ModelExceptions();
            bool result = false;

            if (e.IsValid && IsValid)
            {
                //完成了验证,开始更新数据库了
                int _result = instance.Add(model);
                if (_result > 0)
                {
                    result = true;
                }
            }
            return(result);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 逻辑删除数据
        /// </summary>
        /// <param name='model'>model</param>
        /// <returns>bool</returns>
        public bool Delete(string Condition)
        {
            bool result = false;
            List <Sys_Config_ListPage> list = instance.GetListByWhere(Condition);

            if (list.Count > 0)
            {
                Sys_Config_ListPage model = list[0];
                model.Stat = 1;
                int _rseult = instance.Update(model);
                if (_rseult > 0)
                {
                    result = true;
                }
            }
            return(result);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 插入数据
        /// </summary>
        /// <param name='model'>实体</param>
        /// <returns>bool</returns>
        public bool Insert(Sys_Config_ListPage model)
        {
            bool result = false;

            try
            {
                int _result = instance.Add(model);
                if (_result > 0)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(result);
        }
Ejemplo n.º 7
0
        // Modify BY QB
        // Modify:
        // Modify Date:2011-3-12
        /// <summary>
        /// 通用字典转换
        /// </summary>
        /// <param name="Module"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DataTable ConvertDict(string Module, DataTable dt)
        {
            QX.DAL.ADOBse_Dict       dictInstance = new QX.DAL.ADOBse_Dict();
            Sys_Config_ListPage      M_Model      = new Sys_Config_ListPage();
            List <Sys_Config_Fieled> D_List       = new List <Sys_Config_Fieled>();

            Bll_Comm.GetListConfig(Module, out M_Model, out D_List);
            if (M_Model != null && D_List.Count > 0 && dt.Rows.Count > 0)
            {
                List <Sys_Config_Fieled> newDList = D_List.Where(o => !string.IsNullOrEmpty(o.D_DictKey)).ToList();
                for (var i = 0; i < dt.Rows.Count; i++)
                {
                    foreach (var item in newDList)
                    {
                        var model = dictInstance.GetListByWhere(" AND Dict_Key='" + item.D_DictKey + "' AND Dict_Code='" + dt.Rows[i][item.D_Index] + "'");
                        if (model.Count > 0)
                        {
                            dt.Rows[i][item.D_Index] = model.First().Dict_Name;
                        }
                    }
                }
            }
            return(dt);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 动态解析查询条件
        /// </summary>
        /// <param name="txt"></param>
        /// <param name="module"></param>
        /// <returns></returns>
        public static string BuildSearch(this string txt, string module)
        {
            var           QueryLeftTree = string.Empty;
            StringBuilder strQuery      = new StringBuilder();
            //Dictionary<string, string> dict = ReturnBuild();
            Dictionary <string, string> dict = new Dictionary <string, string>();
            MetaOper         mod             = JsonConvert.DeserializeObject <MetaOper>(txt);
            List <MetaRules> oper            = JsonConvert.DeserializeObject <List <MetaRules> >(mod.rules.ToString());

            ADOBse_Dict              DictInstance = new ADOBse_Dict();
            Sys_Config_ListPage      M_Model      = new Sys_Config_ListPage();
            List <Sys_Config_Fieled> D_List       = new List <Sys_Config_Fieled>();

            GetListConfig(module, out M_Model, out D_List);

            var D_List_Dict = D_List.Where(o => !string.IsNullOrEmpty(o.D_DictKey) && (string.IsNullOrEmpty(o.D_Hidden) || o.D_Hidden == "false"));

            var Str_DictList = D_List.Select(o => o.D_Index);
            var Str_Oper     = oper.Select(o => o.field);


            if (D_List_Dict.Count() > 0 && Str_DictList.Intersect(Str_Oper).Count() > 0)
            {
                for (int i = 0; i < oper.Count; i++)
                {
                    string        _oper       = string.Empty;
                    StringBuilder ConvertData = new StringBuilder();
                    if (i != oper.Count - 1)
                    {
                        _oper = "  " + mod.groupOp + "  ";
                    }
                    var D_ListQuery = D_List_Dict.Where(o => o.D_Index == oper[i].field);

                    if (D_ListQuery.Count() > 0)
                    {
                        dict = ReturnBuildDict();

                        //构建查询条件Field
                        QueryLeftTree = !string.IsNullOrEmpty(D_ListQuery.First().D_DBField) ? D_ListQuery.First().D_DBField : oper[i].field;

                        //获取字典Code
                        if (!string.IsNullOrEmpty(oper[i].data))
                        {
                            var DictResult = DictInstance.GetListByWhere(" AND Dict_Key!=Dict_Code AND Dict_Name like '%" + oper[i].data + "%'");
                            if (DictResult.Count > 0)
                            {
                                foreach (var item in DictResult)
                                {
                                    ConvertData.Append("'" + item.Dict_Code + "',");
                                }
                            }

                            strQuery.Append(dict[oper[i].op].Replace("{field}", QueryLeftTree).Replace("{data}", ConvertData.ToString().TrimEnd(',')) + _oper);
                        }
                    }
                    else
                    {
                        dict = ReturnBuild();
                        if (i != oper.Count - 1)
                        {
                            _oper = "  " + mod.groupOp + "  ";
                        }

                        strQuery.Append(dict[oper[i].op].Replace("{field}", oper[i].field).Replace("{data}", oper[i].data) + _oper);
                    }
                }
            }
            else
            {
                if (oper.Count > 0)
                {
                    dict = ReturnBuild();
                    for (int i = 0; i < oper.Count; i++)
                    {
                        string _oper = string.Empty;
                        if (i != oper.Count - 1)
                        {
                            _oper = "  " + mod.groupOp + "  ";
                        }

                        strQuery.Append(dict[oper[i].op].Replace("{field}", oper[i].field).Replace("{data}", oper[i].data) + _oper);
                    }
                }
            }

            return(strQuery.ToString());
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 获取实体数据
        /// </summary>
        /// <param name='strCondition'>条件(AND Code='11')</param>
        /// <returns>model</returns>
        public Sys_Config_ListPage GetModel(int id)
        {
            Sys_Config_ListPage model = instance.GetByKey(id);

            return(model);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 初始化Grid
        /// </summary>
        /// <param name="ModuleCode"></param>
        /// <param name="Namespace"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public DataTable GetListPageAllRecords(string ModuleCode, string Namespace, Dictionary <string, string> param)
        {
            //读出配置存储过程及参数
            Bll_Sys_Config_ListPage ConfigMInstance = new Bll_Sys_Config_ListPage();
            Bll_Bse_Dict            DictInstance    = new Bll_Bse_Dict();
            Bll_Sys_Config_Fieled   FieldInstance   = new Bll_Sys_Config_Fieled();

            Sys_Config_ListPage      config    = ConfigMInstance.GetListByCode(" AND M_ModuleCode='" + ModuleCode + "' ")[0];
            List <Sys_Config_Fieled> ListField = FieldInstance.GetListByCode(" AND D_ModuleCode='" + ModuleCode + "'");


            List <SqlParameter> list   = new List <SqlParameter>();
            SqlParameter        search = new SqlParameter("@Search", param["@Search"]);

            list.Add(search);
            SqlParameter sidx = new SqlParameter("@Sidx", param["@Sidx"]);

            list.Add(sidx);
            SqlParameter sord = new SqlParameter("@Sord", param["@Sord"]);

            list.Add(sord);
            SqlParameter userid = new SqlParameter("@Userid", param["@Userid"]);

            list.Add(userid);
            SqlParameter deptid = new SqlParameter("@Deptid", param["@Deptid"]);

            list.Add(deptid);
            SqlParameter modulecode = new SqlParameter("@ModuleCode", ModuleCode);

            list.Add(modulecode);
            SqlParameter _namespace = new SqlParameter("@NameSpace", Namespace);

            list.Add(_namespace);
            SqlParameter InlineType = new SqlParameter("@InlineType", param["@InlineType"]);

            list.Add(InlineType);
            SqlParameter GeneralType = new SqlParameter("@GeneralType", param["@GeneralType"]);

            list.Add(GeneralType);
            DataTable newDt = instance.idb.RunProcReturnDatatable(config.M_SQL, list.ToArray());


            #region Old,已被废弃掉的代码片段 By Ye Fei  2011-01-30
            //首先获取config_filed表中有数据字典的列及相关数据字典标示
            //var newList = ListField.Where(o => !string.IsNullOrEmpty(o.D_DictKey) && o.D_EditType.ToLower()=="dict" && o.D_Hidden!="true" );
            //if (newList.Count()>0)
            //{
            //    for (int i = 0; i < newDt.Rows.Count; i++)
            //    {
            //        foreach (Sys_Config_Fieled item in newList)
            //        {
            //            var DictResult = DictInstance.GetListByCode(" AND Dict_Code='" +
            //                                                          newDt.Rows[i][item.D_Index].ToString()
            //                                                          + "' AND Dict_Key='"+item.D_DictKey+"' ");
            //            if (DictResult.Count > 0)
            //            {
            //                newDt.Rows[i][item.D_Index] = DictResult.FirstOrDefault().Dict_Name;
            //            }

            //            if (param["@InlineType"].Length > 0)
            //            {
            //                //物料动态字典取值
            //                string dynKey = param["@InlineType"] + "TYPE";
            //                var Result = DictInstance.GetListByCode(" AND Dict_Code='" +
            //                                                         newDt.Rows[i][item.D_Index].ToString()
            //                                                         + "' AND Dict_Key='" + dynKey.ToUpper() + "' ");
            //                if (Result.Count > 0)
            //                {
            //                    newDt.Rows[i][item.D_Index] = Result.FirstOrDefault().Dict_Name;
            //                }
            //            }

            //        }
            //    }
            //}

            ///排序
            //if (!string.IsNullOrEmpty(config.M_TableName))
            //{
            //    Bll_Comm commInstance = new Bll_Comm();
            //    DataSet ds = commInstance.GetTableInfo(config.M_TableName);
            //    string TableKey = commInstance.GetTableKey(ds);
            //    if (!string.IsNullOrEmpty(TableKey))
            //    {
            //        DataView dv = newDt.DefaultView;
            //        dv.Sort = TableKey + " desc";
            //        newDt = dv.ToTable();
            //    }
            //}
            #endregion
            return(newDt);
        }
Ejemplo n.º 11
0
        public ActionResult GridEdit(string oper, string id)
        {
            string ModuleCode = Request["ModuleCode"] == null ? "" : Request["ModuleCode"].ToString();
            string NameSpace  = Request["NameSpace"] == null ? "" : Request["NameSpace"].ToString();
            string Form       = Request["form"] == null ? "" : Request["form"];
            string FormId     = Request["btn"];

            oper = !string.IsNullOrEmpty(oper) ? oper : (Request["oper"] != null ? Request["oper"] : "");
            id   = !string.IsNullOrEmpty(id) ? id : (Request["id"] != null ? Request["id"] : "");

            string TableName = "";
            string InertSQL  = "";
            string UpdateSQL = "";
            string DeleteSQL = "";
            string TableKey  = "";
            string filterKey = "";
            bool   result    = false;
            string strResult = "fail";
            Dictionary <string, string> FormValue = new Dictionary <string, string>();
            Sys_Config_ListPage         model     = new Sys_Config_ListPage();
            List <Sys_Config_Fieled>    listfiled = new List <Sys_Config_Fieled>();
            StringBuilder ErrorList = new StringBuilder();

            if (!string.IsNullOrEmpty(ModuleCode) && !string.IsNullOrEmpty(NameSpace))
            {
                // var _model = instance.GetListByCode(" AND M_ModuleCode='" + ModuleCode + "' AND M_NameSpace='" + NameSpace + "' ");
                var _model = instance.GetListByCode(" AND M_ModuleCode='" + ModuleCode + "' ");
                model = _model.Count > 0 ? _model[0] : null;
                //var listfiled = filedInstance.GetListByCode(" AND D_ModuleCode='" + ModuleCode + "' AND D_NameSpace='" + NameSpace + "' ");
                listfiled = filedInstance.GetListByCode(" AND D_ModuleCode='" + ModuleCode + "'");
                TableName = model.M_TableName;
                TableKey  = listfiled.Where(o => o.D_IsKey == 1).FirstOrDefault().D_Index;
                filterKey = string.Format("{0}='{1}'", TableKey, id);
                if (!string.IsNullOrEmpty(Form))
                {
                    FormValue = FormParse(Form);
                    string _key   = "";
                    string _value = "";
                    string _upkey = "";
                    filterKey = string.Format("{0}='{1}'", TableKey, FormValue[TableKey]);

                    var newListFiled = listfiled.Where(o => string.IsNullOrEmpty(o.D_EditHidden) ||
                                                       o.D_EditHidden == "false" ||
                                                       !string.IsNullOrEmpty(o.D_DefaultValue) ||
                                                       o.D_EditType == "loadall");

                    if (!string.IsNullOrEmpty(FormId))
                    {
                        #region 扩展


                        if (FormId.Contains("btn1"))
                        {
                            newListFiled = listfiled.Where(o => !string.IsNullOrEmpty(o.D_UDEF1) &&
                                                           JsonConvert.DeserializeObject <FormEdit>(o.D_UDEF1).Show.ToLower() == "true")
                                           .OrderBy(o => JsonConvert.DeserializeObject <FormEdit>(o.D_UDEF1).Order).ToList();
                        }
                        else if (FormId.Contains("btn2"))
                        {
                            newListFiled = listfiled.Where(o => !string.IsNullOrEmpty(o.D_UDEF2) &&
                                                           JsonConvert.DeserializeObject <FormEdit>(o.D_UDEF2).Show.ToLower() == "true")
                                           .OrderBy(o => JsonConvert.DeserializeObject <FormEdit>(o.D_UDEF2).Order).ToList();
                        }
                        else if (FormId.Contains("btn3"))
                        {
                            newListFiled = listfiled.Where(o => !string.IsNullOrEmpty(o.D_UDEF3) &&
                                                           JsonConvert.DeserializeObject <FormEdit>(o.D_UDEF3).Show.ToLower() == "true")
                                           .OrderBy(o => JsonConvert.DeserializeObject <FormEdit>(o.D_UDEF3).Order).ToList();
                        }
                        else if (FormId.Contains("btn4"))
                        {
                            newListFiled = listfiled.Where(o => !string.IsNullOrEmpty(o.D_UDEF4) &&
                                                           JsonConvert.DeserializeObject <FormEdit>(o.D_UDEF4).Show.ToLower() == "true")
                                           .OrderBy(o => JsonConvert.DeserializeObject <FormEdit>(o.D_UDEF4).Order).ToList();
                        }
                        #endregion
                    }

                    foreach (var item in newListFiled)
                    {
                        try
                        {
                            if (!string.IsNullOrEmpty(FormValue[item.D_Index]))
                            {
                                if (!string.IsNullOrEmpty(item.D_Type) && item.D_Type.Contains("int"))
                                {
                                    _value += "" + FormValue[item.D_Index] + "" + ",";
                                    _upkey += item.D_Index + "=" + FormValue[item.D_Index] + "" + ",";
                                }
                                else
                                {
                                    if (item.D_Type.Contains("datetime"))
                                    {
                                        FormValue[item.D_Index] = FormValue[item.D_Index].Replace("+", " ");
                                    }
                                    _value += "'" + FormValue[item.D_Index] + "'" + ",";
                                    _upkey += item.D_Index + "='" + FormValue[item.D_Index] + "'" + ",";
                                }
                                _key += item.D_Index + ",";
                            }
                            else
                            {
                                if (!string.IsNullOrEmpty(item.D_Type) && (item.D_Type.Contains("int") || item.D_Type.Contains("decimal")))
                                {
                                    _value += "0,";
                                    _upkey += item.D_Index + "=0" + ",";
                                }
                                else
                                {
                                    if (item.D_Type.Contains("datetime"))
                                    {
                                        FormValue[item.D_Index] = "1900-01-01";
                                    }
                                    _value += "'" + FormValue[item.D_Index] + "'" + ",";
                                    _upkey += item.D_Index + "='" + FormValue[item.D_Index] + "'" + ",";
                                }
                                _key += item.D_Index + ",";
                            }
                        }
                        catch (System.Exception ex)
                        {
                            ErrorList.AppendLine(ex.ToString());
                            continue;
                        }
                    }
                    //拼接Add sql
                    _key     = _key;   // +" CreateTime,";
                    _value   = _value; // +"'" + DateTime.Now + "',";
                    InertSQL = "INSERT INTO  " + TableName + " (" + _key.TrimEnd(',') + ") VALUES (" + _value.TrimEnd(',') + ")  ";
                    //拼接update sql
                    _upkey    = _upkey;//+ " UpdateTime='" + DateTime.Now + "',";
                    UpdateSQL = "UPDATE " + TableName + " SET " + _upkey.TrimEnd(',') + " WHERE " + filterKey;
                }
                //拼接 Delete Sql
                DeleteSQL = "UPDATE " + TableName + " SET Stat=1 WHERE " + filterKey;
            }

            switch (oper)
            {
            case "add":
                result    = instance.SaveData(InertSQL);
                strResult = result ? "success" : "fail";

                //功能日志
                this.OpLog(model.M_ModuleCode,
                           "添加:" + model.M_Title + ",编码:" + FormValue[TableKey],
                           strResult + "," + InertSQL + "," + ErrorList.ToString());

                return(Content(strResult));

            case "edit":
                result    = instance.SaveData(UpdateSQL);
                strResult = result ? "success" : "fail";

                //功能日志
                this.OpLog(model.M_ModuleCode,
                           "修改:" + model.M_Title + ",编码:" + FormValue[TableKey],
                           strResult + "," + InertSQL + "," + ErrorList.ToString());

                return(Content(strResult));

            case "del":
                result    = instance.SaveData(DeleteSQL);
                strResult = result ? "success" : "fail";

                //功能日志
                this.OpLog(model.M_ModuleCode,
                           "删除:" + model.M_Title + ",编码:" + id,
                           strResult + "," + InertSQL + "," + ErrorList.ToString());

                return(Content(strResult));

            case "view":
                DataTable dt = instance.ListDataByCode(TableName, filterKey);

                DataTable newDt = dt.Clone();
                //string json = JsonConvert.SerializeObject(dt, new DataTableConverter(), new JsonDateConverter("yyyy-MM-dd"));

                for (var x = 0; x < newDt.Columns.Count; x++)
                {
                    var D_Index = dt.Columns[x].ColumnName;

                    var tmpModel = listfiled.Where(o => o.D_Index == D_Index);
                    if (tmpModel.Count() > 0)
                    {
                        if (tmpModel.FirstOrDefault().D_EditType == "datetime")
                        {
                            newDt.Columns[x].DataType = typeof(string);
                        }
                        else if (tmpModel.FirstOrDefault().D_EditType == "date")
                        {
                            newDt.Columns[x].DataType = typeof(string);
                        }
                    }
                }

                newDt.Rows.Clear();
                for (var i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow newRow = newDt.NewRow();
                    for (var j = 0; j < dt.Columns.Count; j++)
                    {
                        var D_Index = dt.Columns[j].ColumnName;

                        var tmpModel = listfiled.Where(o => o.D_Index == D_Index);
                        if (tmpModel.Count() > 0)
                        {
                            if (tmpModel.FirstOrDefault().D_EditType == "datetime")
                            {
                                newRow[D_Index] = dt.Rows[i][j] != null && !string.IsNullOrEmpty(dt.Rows[i][j].ToString())
                                       ? JsonConvert.SerializeObject(DateTime.Parse(dt.Rows[i][j].ToString()), new JsonDateConverter("yyyy-MM-dd HH:mm:ss")).Replace("\"", "") : "";
                            }
                            else if (tmpModel.FirstOrDefault().D_EditType == "date")
                            {
                                //newDt.Columns[j].DataType = typeof(string);
                                newRow[D_Index] = dt.Rows[i][j] != null && !string.IsNullOrEmpty(dt.Rows[i][j].ToString())
                                       ? JsonConvert.SerializeObject(DateTime.Parse(dt.Rows[i][j].ToString()), new JsonDateConverter("yyyy-MM-dd")).Replace("\"", "") : "";
                            }
                            else
                            {
                                newRow[D_Index] = dt.Rows[i][j];
                            }
                        }
                    }
                    newDt.Rows.Add(newRow);
                }
                string json = JsonConvert.SerializeObject(newDt, new DataTableConverter());
                return(JavaScript(json));
            }
            return(Content(""));
        }
Ejemplo n.º 12
0
        /// <summary>
        /// JqGrid通用分页序列化,根据数据配置字段进行时间格式化
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public static string JsonForJqgridForRpt(string Module, DataTable dt, int pageIndex, int pageSize, int totalCount)
        {
            StringBuilder jsonBuilder = new StringBuilder();

            StringBuilder sumBuilder = new StringBuilder();

            int total = 0;
            int page  = totalCount / pageSize;

            total = totalCount > pageSize ? totalCount / pageSize : 1;
            if (totalCount > pageSize)
            {
                if (totalCount % pageSize > 0)
                {
                    total = total + 1;
                }
            }
            if (totalCount == 0)
            {
                total     = 0;
                pageIndex = 0;
            }

            jsonBuilder.Append("{\"Page\":" + pageIndex + ",\"Total\":" + total + ",\"Records\":" + totalCount + ",\"Rows\":");


            #region 对时间进行格式化

            Sys_Config_ListPage      M_Model   = new Sys_Config_ListPage();
            List <Sys_Config_Fieled> listfiled = new List <Sys_Config_Fieled>();
            GetListConfig(Module, out M_Model, out listfiled);

            DataTable newDt = dt.Clone();
            //DataTable resultDt = dt.Clone();

            //string json = JsonConvert.SerializeObject(dt, new DataTableConverter(), new JsonDateConverter("yyyy-MM-dd"));

            for (var x = 0; x < newDt.Columns.Count; x++)
            {
                var D_Index = dt.Columns[x].ColumnName;

                var tmpModel = listfiled.Where(o => o.D_Index == D_Index);
                if (tmpModel.Count() > 0)
                {
                    if (tmpModel.FirstOrDefault().D_EditType == "datetime")
                    {
                        newDt.Columns[x].DataType = typeof(string);
                    }
                    else if (tmpModel.FirstOrDefault().D_EditType == "date")
                    {
                        newDt.Columns[x].DataType = typeof(string);
                    }
                }
            }

            var D_SumList = listfiled.Where(o => !string.IsNullOrEmpty(o.D_Summary));

            newDt.Rows.Clear();
            for (var i = 0; i < dt.Rows.Count; i++)
            {
                DataRow newRow = newDt.NewRow();

                for (var j = 0; j < dt.Columns.Count; j++)
                {
                    var D_Index  = dt.Columns[j].ColumnName;
                    var tmpModel = listfiled.Where(o => o.D_Index == D_Index);
                    if (tmpModel.Count() > 0)
                    {
                        if (tmpModel.FirstOrDefault().D_EditType == "datetime")
                        {
                            newRow[D_Index] = dt.Rows[i][D_Index] != null && !string.IsNullOrEmpty(dt.Rows[i][D_Index].ToString())
                                ? JsonConvert.SerializeObject(DateTime.Parse(dt.Rows[i][D_Index].ToString()), new JsonDateConverter("yyyy-MM-dd HH:mm:ss")).Replace("\"", " ") : "";
                        }
                        else if (tmpModel.FirstOrDefault().D_EditType == "date")
                        {
                            //newDt.Columns[j].DataType = typeof(string);
                            newRow[D_Index] = dt.Rows[i][D_Index] != null && !string.IsNullOrEmpty(dt.Rows[i][D_Index].ToString())
                                ? JsonConvert.SerializeObject(DateTime.Parse(dt.Rows[i][D_Index].ToString()), new JsonDateConverter("yyyy-MM-dd")).Replace("\"", " ") : "";
                        }
                        else
                        {
                            newRow[D_Index] = dt.Rows[i][D_Index];
                        }
                    }
                }

                newDt.Rows.Add(newRow);
            }
            #endregion

            if (D_SumList.Count() > 0 && newDt.Rows.Count > 0)
            {
                sumBuilder.Append(",\"UserData\":{");
                StringBuilder sb = new StringBuilder();
                foreach (var item in D_SumList)
                {
                    if (newDt.Columns.Contains(item.D_Index))
                    {
                        var val = newDt.Compute(string.Format("Sum({0})", item.D_Index), "");
                        //var d = decimal.Parse(val.ToString());
                        sb.AppendFormat("\"{0}\":{1},", item.D_Index, val);
                    }
                }
                sumBuilder.Append(sb.ToString().TrimEnd(','));
                sumBuilder.Append(" }");
            }
            else
            {
                sumBuilder.Append(",\"UserData\":{");
                StringBuilder sb = new StringBuilder();
                foreach (var item in D_SumList)
                {
                    //if (newDt.Columns.Contains(item.D_Index))
                    //{
                    //var val = newDt.Compute(string.Format("Sum({0})", item.D_Index), "");
                    //var d = decimal.Parse(val.ToString());
                    sb.AppendFormat("\"{0}\":{1},", item.D_Index, "0");
                    //}
                }
                sumBuilder.Append(sb.ToString().TrimEnd(','));
                sumBuilder.Append(" }");
            }

            jsonBuilder.Append(JsonConvert.SerializeObject(newDt, new DataTableConverter()));
            if (!string.IsNullOrEmpty(sumBuilder.ToString()))
            {
                jsonBuilder.Append(sumBuilder.ToString());
            }
            jsonBuilder.Append("}");
            return(jsonBuilder.ToString());
        }
Ejemplo n.º 13
0
        /// <summary>
        /// JqGrid通用分页序列化,根据数据配置字段进行时间格式化
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public static string JsonForJqgrid(string Module, DataTable dt, int pageIndex, int pageSize, int totalCount)
        {
            StringBuilder jsonBuilder = new StringBuilder();
            int           total       = 0;
            int           page        = totalCount / pageSize;

            total = totalCount > pageSize ? totalCount / pageSize : 1;
            if (totalCount > pageSize)
            {
                if (totalCount % pageSize > 0)
                {
                    total = total + 1;
                }
            }
            if (totalCount == 0)
            {
                total     = 0;
                pageIndex = 0;
            }

            jsonBuilder.Append("{\"Page\":" + pageIndex + ",\"Total\":" + total + ",\"Records\":" + totalCount + ",\"Rows\":");


            #region 对时间进行格式化

            Sys_Config_ListPage      M_Model   = new Sys_Config_ListPage();
            List <Sys_Config_Fieled> listfiled = new List <Sys_Config_Fieled>();
            GetListConfig(Module, out M_Model, out listfiled);

            DataTable newDt = dt.Clone();
            //DataTable resultDt = dt.Clone();

            //string json = JsonConvert.SerializeObject(dt, new DataTableConverter(), new JsonDateConverter("yyyy-MM-dd"));

            for (var x = 0; x < newDt.Columns.Count; x++)
            {
                var D_Index = dt.Columns[x].ColumnName;

                var tmpModel = listfiled.Where(o => o.D_Index == D_Index);
                if (tmpModel.Count() > 0)
                {
                    if (tmpModel.FirstOrDefault().D_EditType == "datetime")
                    {
                        newDt.Columns[x].DataType = typeof(string);
                    }
                    else if (tmpModel.FirstOrDefault().D_EditType == "date")
                    {
                        newDt.Columns[x].DataType = typeof(string);
                    }
                }
            }

            newDt.Rows.Clear();
            for (var i = 0; i < dt.Rows.Count; i++)
            {
                DataRow newRow = newDt.NewRow();
                for (var j = 0; j < dt.Columns.Count; j++)
                {
                    var D_Index  = dt.Columns[j].ColumnName;
                    var tmpModel = listfiled.Where(o => o.D_Index == D_Index);
                    if (tmpModel.Count() > 0)
                    {
                        if (tmpModel.FirstOrDefault().D_EditType == "datetime")
                        {
                            newRow[D_Index] = dt.Rows[i][D_Index] != null && !string.IsNullOrEmpty(dt.Rows[i][D_Index].ToString())
                                ? JsonConvert.SerializeObject(DateTime.Parse(dt.Rows[i][D_Index].ToString()), new JsonDateConverter("yyyy-MM-dd HH:mm:ss")).Replace("\"", " ") : "";
                        }
                        else if (tmpModel.FirstOrDefault().D_EditType == "date")
                        {
                            //newDt.Columns[j].DataType = typeof(string);
                            newRow[D_Index] = dt.Rows[i][D_Index] != null && !string.IsNullOrEmpty(dt.Rows[i][D_Index].ToString())
                                ? JsonConvert.SerializeObject(DateTime.Parse(dt.Rows[i][D_Index].ToString()), new JsonDateConverter("yyyy-MM-dd")).Replace("\"", " ") : "";
                        }
                        else
                        {
                            newRow[D_Index] = dt.Rows[i][D_Index];
                        }
                    }
                }
                newDt.Rows.Add(newRow);
            }
            #endregion


            jsonBuilder.Append(JsonConvert.SerializeObject(newDt, new DataTableConverter()));
            //jsonBuilder.Append(" ,\"UserData\":{ \"PUD_Num\":100 } ");
            jsonBuilder.Append("}");
            return(jsonBuilder.ToString());
        }