예제 #1
0
        /// <summary>
        /// 根据参数获取分页实体数据
        /// </summary>
        /// <returns></returns>
        public DataSet GetPageData(string getpagedataEntityParams)
        {
            List <string> list         = StringPlus.GetStrArray(getpagedataEntityParams, "_;_");
            int           pageSize     = 100;
            int           pageIndex    = 1;
            string        entityName   = list[0];
            string        pageSizeStr  = list[1];
            string        pageIndexStr = list[2];
            string        colums       = list[3];
            string        tmpwhere     = list[4].TrimEnd();

            string where = tmpwhere.EndsWith("and", StringComparison.CurrentCultureIgnoreCase) ? tmpwhere.Remove(tmpwhere.Length - 3) : list[4];
            string order = list[5];

            if (!int.TryParse(pageSizeStr, out pageSize) || !int.TryParse(pageIndexStr, out pageIndex))
            {
                return(null);
            }
            if (pageSize != -1)
            {
                pageSize = pageSize < 1 ? 100 : pageSize;
            }
            if (pageIndex != -1)
            {
                pageIndex = pageIndex < 1 ? 1 : pageIndex;
            }

            StringBuilder sbSql = new StringBuilder();

            if (string.IsNullOrWhiteSpace(entityName))
            {
                return(null);
            }
            if (string.IsNullOrWhiteSpace(colums) || colums == "*")
            {
                colums = "*";
            }

            DataTable dtStruct = GetTableStruct(entityName);
            bool      falseDel = dtStruct.Columns.Contains("is_del");

            if (falseDel)
            {
                where += (string.IsNullOrWhiteSpace(where) ? "" : " and") + " (is_del!='1' or is_del is null)";
            }

            sbSql.AppendFormat("select count(*) from {0}", entityName);
            if (!string.IsNullOrWhiteSpace(where))
            {
                sbSql.AppendFormat(" where {0}", where);
            }

            if (!string.IsNullOrWhiteSpace(order))
            {
                sbSql.AppendFormat(" order by {0}", order);
            }

            object objCount = MySQLHelper.GetSingle(sbSql.ToString());

            if (objCount == null)
            {
                return(null);
            }
            int count = Convert.ToInt32(objCount);

            if (count < 1)
            {
                return(null);
            }
            DataTable dtCount = new DataTable("tmo_count");

            dtCount.Columns.AddRange(new DataColumn[]
            {
                new DataColumn("count"), new DataColumn("pageCount"),
                new DataColumn("pageIndex"), new DataColumn("pageSize")
            });
            DataRow drCount = dtCount.NewRow();

            drCount["count"] = count;
            int pageCount = pageSize != -1 ? (int)Math.Ceiling((double)count / pageSize) : 1;

            drCount["pageCount"] = pageCount;
            if (pageIndex != -1)
            {
                pageIndex = pageIndex > pageCount ? pageCount : pageIndex;
            }
            drCount["pageIndex"] = pageIndex;
            drCount["pageSize"]  = pageSize;
            dtCount.Rows.Add(drCount);

            int       rowStart = pageSize != -1 && pageIndex != -1 ? (pageIndex - 1) * pageSize : -1;
            int       rowEnd   = pageSize != -1 && pageIndex != -1 ? pageSize : -1;
            DataTable dt       = GetData(TmoShare.GetDataEntityParams(entityName, colums, rowStart, rowEnd, "", "", where, order));

            dt = TmoShare.DataTableVerify(dt);
            if (dt != null)
            {
                dt.TableName = "tmo_data";
            }
            else
            {
                return(null);
            }
            DataSet ds = new DataSet("tmo_entity");

            ds.Tables.Add(dt);
            ds.Tables.Add(dtCount);
            return(ds);
        }