Exemplo n.º 1
0
        private int CountReadIndex = 0; //已读取的 Count 最后的索引位置。

        /// <summary>
        /// 开始搜索指定页数的数据。
        /// </summary>
        /// <param name="pageNum">搜索第几页的数据。当此参数为“0”时,则表示不进行分页,将所有符合条件的数据全部读取出。</param>
        /// <returns>搜索结果集合</returns>
        public TaskLogCollection Search(int pageNum)
        {
            this.CountReadIndex = 0;
            this.m_PageNum      = pageNum;
            TaskLogCollection taskLogs = new TaskLogCollection();
            IList <AC.Base.DateRange.MonthlyForTickRange> lstTickRanges = this.DateRange.GetMonthTickRanges();

            int[] intCounts = new int[lstTickRanges.Count];
            Database.DbConnection dbConn = this.Application.GetDbConnection();

            try
            {
                if (this.PageSize > 0)
                {
                    this.m_RecordsetCount = 0;

                    for (int intIndex = 0; intIndex < lstTickRanges.Count; intIndex++)
                    {
                        AC.Base.DateRange.MonthlyForTickRange range = lstTickRanges[intIndex];
                        string strTableName = Tables.TaskLog.GetTableName(range.Date);
                        if (dbConn.TableIsExist(strTableName))
                        {
                            string strSql = "SELECT COUNT(*) FROM " + strTableName + this.GetSqlWhere(range);
                            System.Data.IDataReader dr = dbConn.ExecuteReader(strSql);
                            if (dr.Read())
                            {
                                intCounts[intIndex]    = Function.ToInt(dr[0]);
                                this.m_RecordsetCount += intCounts[intIndex];
                            }
                        }
                    }

                    this.m_PageCount = this.RecordsetCount / this.PageSize;
                    if ((this.RecordsetCount % this.PageSize) > 0)
                    {
                        this.m_PageCount++;
                    }

                    if (this.PageNum < 1)
                    {
                        this.m_PageNum = 1;
                    }
                    if (this.PageNum > this.PageCount)
                    {
                        this.m_PageNum = this.PageCount;
                    }
                }

                if (this.PageSize == 0 || this.PageCount == 1)
                {
                    this.m_RecordsetCount = 0;
                    if (this.m_OrderIsDesc)
                    {
                        for (int intCountIndex = lstTickRanges.Count - 1; intCountIndex >= 0; intCountIndex--)
                        {
                            AC.Base.DateRange.MonthlyForTickRange tickRange = lstTickRanges[intCountIndex];
                            string strTableName = Tables.TaskLog.GetTableName(tickRange.Date);
                            if (dbConn.TableIsExist(strTableName))
                            {
                                string strSql = "SELECT * FROM " + strTableName + this.GetSqlWhere(tickRange) + this.GetOrderColumn(this.m_OrderIsDesc);
                                System.Data.IDataReader dr = dbConn.ExecuteReader(strSql);
                                while (dr.Read())
                                {
                                    this.m_RecordsetCount++;
                                    taskLogs.Add(DrToTaskLog(dr));
                                }
                                dr.Close();
                            }
                        }
                    }
                    else
                    {
                        for (int intCountIndex = 0; intCountIndex < lstTickRanges.Count; intCountIndex++)
                        {
                            AC.Base.DateRange.MonthlyForTickRange tickRange = lstTickRanges[intCountIndex];
                            string strTableName = Tables.TaskLog.GetTableName(tickRange.Date);
                            if (dbConn.TableIsExist(strTableName))
                            {
                                string strSql = "SELECT * FROM " + strTableName + this.GetSqlWhere(tickRange) + this.GetOrderColumn(this.m_OrderIsDesc);
                                System.Data.IDataReader dr = dbConn.ExecuteReader(strSql);
                                while (dr.Read())
                                {
                                    this.m_RecordsetCount++;
                                    taskLogs.Add(DrToTaskLog(dr));
                                }
                                dr.Close();
                            }
                        }
                    }

                    if (this.m_RecordsetCount == 0)
                    {
                        this.m_PageCount = 0;
                        this.m_PageNum   = 0;
                    }
                    else
                    {
                        this.m_PageCount = 1;
                        this.m_PageNum   = 1;
                    }
                }
                else
                {
                    int intRsStartIndex = this.RecordsetStartIndex;
                    int intRsEndIndex   = this.RecordsetEndIndex;

                    if (this.m_OrderIsDesc)
                    {
                        for (int intCountIndex = intCounts.Length - 1; intCountIndex >= 0; intCountIndex--)
                        {
                            this.Search(dbConn, taskLogs, lstTickRanges, intCounts, intCountIndex, intRsStartIndex, intRsEndIndex);
                        }
                    }
                    else
                    {
                        for (int intCountIndex = 0; intCountIndex < intCounts.Length; intCountIndex++)
                        {
                            this.Search(dbConn, taskLogs, lstTickRanges, intCounts, intCountIndex, intRsStartIndex, intRsEndIndex);
                        }
                    }
                }

                for (int intIndex = 0; intIndex < lstTickRanges.Count; intIndex++)
                {
                    string strLogIds = "";
                    AC.Base.DateRange.MonthlyForTickRange range = lstTickRanges[intIndex];
                    foreach (TaskLog log in taskLogs)
                    {
                        if (range.Contains(log.LogId))
                        {
                            strLogIds += "," + log.LogId.ToString();
                        }
                    }

                    if (strLogIds.Length > 0)
                    {
                        strLogIds = strLogIds.Substring(1);

                        string strTableName;

                        strTableName = Tables.TaskRunLog.GetTableName(range.Date);
                        if (dbConn.TableIsExist(strTableName))
                        {
                            string strSql = "SELECT * FROM " + strTableName + " WHERE " + Tables.TaskRunLog.TaskLogId + " IN (" + strLogIds + ")";
                            System.Data.IDataReader dr = dbConn.ExecuteReader(strSql);
                            while (dr.Read())
                            {
                                long lngLogId = Function.ToLong(dr[Tables.TaskRunLog.TaskLogId]);
                                foreach (TaskLog log in taskLogs)
                                {
                                    if (log.LogId == lngLogId)
                                    {
                                        log.AddTask(dr);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dbConn.Close();
            }
            return(taskLogs);
        }