Example #1
0
        /// <summary>
        /// 获取该集合中符合筛选条件的设备集合。
        /// </summary>
        /// <param name="filters">设备筛选条件。</param>
        /// <returns></returns>
        public DeviceCollection GetDevices(DeviceFilterCollection filters)
        {
            DeviceCollection devices = new DeviceCollection(false);

            foreach (Device device in this)
            {
                if (filters.DeviceFilterCheck(device))
                {
                    devices.Add(device);
                }
            }

            return(devices);
        }
Example #2
0
        /// <summary>
        /// 搜索指定页数的月数据。
        /// </summary>
        /// <param name="pageNum">搜索第几页的数据。</param>
        /// <returns></returns>
        public IList <V> Search(int pageNum)
        {
            List <V> lstValues = new List <V>();

            this.PageNum        = pageNum;
            this.RecordsetCount = 0;
            this.PageCount      = 0;

            Database.DbConnection dbConn = this.Application.GetDbConnection();

            if (dbConn == null)
            {
                throw new Exception("当前系统未配置数据库连接,无法使用搜索功能。");
            }

            try
            {
                if (dbConn.TableIsExist(this.GetDataTableName()))
                {
                    string strWhere = Tables.Device.TableName + "." + Tables.Device.DeviceId + "=" + this.GetDataTableName() + ".DeviceId";
                    if ((this.m_StartDate.Year * 12 + this.m_StartDate.Month) == (this.m_EndDate.Year * 12 + this.m_EndDate.Month))
                    {
                        strWhere += " AND " + this.GetDataTableName() + ".DateNum=" + (this.StartDate.Year * 10000 + this.StartDate.Month * 100);
                    }
                    else
                    {
                        strWhere += " AND " + this.GetDataTableName() + ".DateNum>=" + (this.StartDate.Year * 10000 + this.StartDate.Month * 100) + " AND " + this.GetDataTableName() + ".DateNum<=" + (this.EndDate.Year * 10000 + this.EndDate.Month * 100);
                    }
                    strWhere += this.GetSqlWhere();

                    if (this.Filters.Count > 0)
                    {
                        string strFilterWhere = this.Filters.GetFilterSql(true);
                        if (strFilterWhere != null && strFilterWhere.Length > 0)
                        {
                            strWhere += " AND (" + strFilterWhere + ")";
                        }
                    }

                    if (strWhere.Length > 0)
                    {
                        strWhere = " WHERE " + strWhere;
                    }

                    string strTableNames = "";
                    if (this.m_TableNames != null && this.m_TableNames.Length > 0)
                    {
                        foreach (string name in this.m_TableNames)
                        {
                            strTableNames += "," + name;
                        }
                    }

                    string strSql;
                    // 查询结果记录集总数。
                    strSql = "SELECT COUNT(" + this.GetDataTableName() + ".DeviceId) FROM " + Tables.Device.TableName + strTableNames + "," + this.GetDataTableName() + strWhere;
                    this.RecordsetCount = Function.ToInt(dbConn.ExecuteScalar(strSql));

                    // 计算总页数并检查当前页是否在页数范围内。
                    if (this.RecordsetCount == 0)
                    {
                        this.PageCount = 0;
                        this.PageNum   = 0;
                    }
                    else
                    {
                        if (this.PageSize > 0)
                        {
                            this.PageCount = this.RecordsetCount / this.PageSize;
                            if ((this.RecordsetCount % this.PageSize) > 0)
                            {
                                this.PageCount++;
                            }

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

                        System.Data.IDataReader dr = null;
                        if (this.PageCount == 1)
                        {
                            strSql = "SELECT " + this.GetSelectColumn() + " FROM " + Tables.Device.TableName + strTableNames + "," + this.GetDataTableName() + strWhere + this.GetOrder(true, this.IsAscend ? false : true);
                            dr     = dbConn.ExecuteReader(strSql);
                        }
                        else if (this.PageCount > 1)
                        {
                            if (this.PageNum == 1)
                            {
                                dr = dbConn.GetDataReader(this.PageSize, this.GetSelectColumn(), Tables.Device.TableName + strTableNames + "," + this.GetDataTableName(), strWhere, this.GetOrder(true, this.IsAscend ? false : true));
                            }
                            else if (this.PageNum == this.PageCount)
                            {
                                dr = dbConn.GetDataReader(this.PageSize, this.RecordsetCount, this.GetSelectColumn(), Tables.Device.TableName + strTableNames + "," + this.GetDataTableName(), strWhere, this.GetOrder(true, this.IsAscend ? true : false), this.GetOrder(false, this.IsAscend ? false : true));
                            }
                            else
                            {
                                dr = dbConn.GetDataReader(this.PageSize, this.PageNum, this.GetSelectColumn(), Tables.Device.TableName + strTableNames + "," + this.GetDataTableName(), strWhere, this.GetOrder(true, this.IsAscend ? false : true), this.GetOrder(false, this.IsAscend ? true : false), this.GetOrder(false, this.IsAscend ? false : true));
                            }
                        }

                        if (dr != null)
                        {
                            DeviceCollection devices = new DeviceCollection(true);

                            while (dr.Read())
                            {
                                int    intDeviceId = Function.ToInt(dr[Tables.Device.DeviceId]);
                                Device device      = this.Application.GetDeviceInstance(intDeviceId);
                                if (device != null)
                                {
                                    device.SetDataReader(dr);
                                }
                                else
                                {
                                    DeviceType deviceType = this.Application.DeviceTypeSort.GetDeviceType(Function.ToString(dr[Tables.Device.DeviceType]));
                                    if (deviceType != null)
                                    {
                                        device = deviceType.CreateDevice();
                                        device.SetApplication(this.Application);
                                        device.SetDataReader(dr);

                                        this.Application.SetDeviceInstance(device);
                                    }
                                }
                                if (device != null)
                                {
                                    devices.Add(device);

                                    lstValues.Add(this.DoDataReader(device, dr));
                                }
                            }
                            dr.Close();
                        }
                    }
                }
                else
                {
                    this.RecordsetCount = 0;
                    this.PageCount      = 0;
                    this.PageNum        = 0;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dbConn.Close();
            }

            return(lstValues);
        }