Example #1
0
        /// <summary>
        /// 获取分组数据记录集。
        /// </summary>
        /// <returns></returns>
        protected System.Data.IDataReader GetDataReader()
        {
            this.DbConnection = this.Application.GetDbConnection();

            if (this.DbConnection == null)
            {
                throw new Exception("当前系统未配置数据库连接,无法使用分组统计功能。");
            }

            if (this.DbConnection.TableIsExist(this.GetDataTableName()))
            {
                string strSumColumn = "";
                for (int intIndex = 1; intIndex <= 96; intIndex++)
                {
                    strSumColumn += "," + this.GetAggregateType().GetName() + "(" + this.GetDataTableName() + ".Value" + intIndex + ") AS V" + intIndex;
                }

                string strFrom = Tables.Device.TableName + "," + this.GetDataTableName();
                foreach (string tName in this.m_TableNames)
                {
                    strFrom += "," + tName;
                }

                string strWhere = Tables.Device.TableName + "." + Tables.Device.DeviceId + "=" + this.GetDataTableName() + ".DeviceId";
                if (this.StartDate == this.EndDate)
                {
                    strWhere += " AND " + this.GetDataTableName() + ".DateNum=" + Function.ToIntDate(this.StartDate);
                }
                else
                {
                    strWhere += " AND " + this.GetDataTableName() + ".DateNum>=" + Function.ToIntDate(this.StartDate) + " AND " + this.GetDataTableName() + ".DateNum<=" + Function.ToIntDate(this.EndDate);
                }
                strWhere += this.GetSqlWhere();

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

                string strSql = "SELECT " + this.GetGroupColumnName() + strSumColumn + " FROM " + strFrom + strWhere + " GROUP BY " + this.GetGroupColumnName();
                System.Data.IDataReader dr = this.DbConnection.ExecuteReader(strSql);
                return(dr);
            }

            return(null);
        }
        /// <summary>
        /// 保存设备曲线数据段。
        /// </summary>
        /// <param name="tableTypeF">96及以下点数数据表类型声明。</param>
        /// <param name="tableNameF">96及以下点数数据表名。</param>
        /// <param name="tableTypeS">96以上点数数据表类型声明。</param>
        /// <param name="tableNameS">96以上点数数据表名。</param>
        /// <param name="mergeOption">当转换后的点数小于目前曲线点数时,如何合并目前的数据。</param>
        /// <param name="splitOption">当转换后的点数大于目前曲线点数时,如何拆分目前的数据。</param>
        /// <param name="dbConnection">活动的数据库连接。</param>
        /// <param name="deviceId">设备编号。</param>
        /// <param name="date">数据起始日期及时间。使用其中的年、月、日、时、分、秒。</param>
        /// <param name="curvePoint">该数据的点数类型。</param>
        /// <param name="values">曲线数据值,该数组长度必须是 CurvePointOptions 中指定的长度之一。</param>
        /// <param name="save96Point">当 curvePoint 大于 96 点时是否另外计算一份96点的数据进行保存。当 curvePoint 小于等于 96 点时该参数无任何作用。</param>
        /// <param name="keyColumns">该数据所属数据表的额外主键字段,及这些主键字段的数值。</param>
        public static void Save(Type tableTypeF, string tableNameF, Type tableTypeS, string tableNameS, CurveMergeOptions mergeOption, CurveSplitOptions splitOption, Database.DbConnection dbConnection, int deviceId, DateTime date, CurvePointOptions curvePoint, decimal?[] values, CurvePartDataSave96PointOptions save96Point, params KeyValuePair <string, string>[] keyColumns)
        {
            CurvePartValue _CurvePartValue = new CurvePartValue(curvePoint, date.Hour * 10000 + date.Minute * 100 + date.Second, values);
            string         strSql;
            string         strTableName;
            bool           bolSave96 = true; //是否保存96点数据

            if (_CurvePartValue.CurvePoint > CurvePointOptions.Point96)
            {
                //储存分秒数据表
                strTableName = Database.DbConnection.GetMonthlyName(tableNameS, date.Year, date.Month);
                if (dbConnection.TableIsExist(strTableName) == false)
                {
                    dbConnection.CreateTable(tableTypeS, strTableName);
                }

                string strWhere = "";
                foreach (KeyValuePair <string, string> kvp in keyColumns)
                {
                    strWhere += " AND " + kvp.Key + "=" + kvp.Value;
                }
                strSql = "Delete From " + strTableName + " Where DateNum=" + Function.ToIntDate(date) + " AND TimeNum>=" + _CurvePartValue.StartTimeNum + " AND TimeNum<=" + _CurvePartValue.EndTimeNum + " AND DeviceId=" + deviceId + strWhere;
                dbConnection.ExecuteNonQuery(strSql);

                string strValue  = "";
                string strColumn = "";
                foreach (KeyValuePair <string, string> kvp in keyColumns)
                {
                    strValue  += "," + kvp.Value;
                    strColumn += "," + kvp.Key;
                }
                for (int intIndex = 0; intIndex < _CurvePartValue.Values.Length; intIndex++)
                {
                    try
                    {
                        strSql = Function.ToIntDate(date) + "," + _CurvePartValue.GetTimeNum(intIndex) + "," + deviceId + strValue + "," + Function.SqlDecimal(values[intIndex]);
                        strSql = "INSERT INTO " + strTableName + " (DateNum,TimeNum,DeviceId" + strColumn + ",DataValue) VALUES (" + strSql + ")";
                        dbConnection.ExecuteNonQuery(strSql);
                    }
                    catch { }
                }

                if (save96Point == CurvePartDataSave96PointOptions.No)
                {
                    bolSave96 = false;
                }
                else if (save96Point == CurvePartDataSave96PointOptions.Auto)
                {
                    if (curvePoint == CurvePointOptions.Point144)
                    {
                        if (values.Length <= 3)
                        {
                            if (((_CurvePartValue.EndTimeNum / 100) % 100) != 20 && ((_CurvePartValue.EndTimeNum / 100) % 100) != 50)
                            {
                                bolSave96 = false;
                            }
                        }
                    }
                    else
                    {
                        if (values.Length <= (curvePoint.GetPointCount() / CurvePointOptions.Point96.GetPointCount()))
                        {
                            if (curvePoint.GetTimeNum(_CurvePartValue.EndTimeNum, 1) != CurvePointOptions.Point96.GetTimeNum(_CurvePartValue.EndTimeNum, 1))
                            {
                                bolSave96 = false;
                            }
                        }
                    }
                }

                if (bolSave96)
                {
                    int intStartTimeNum96 = CurvePointOptions.Point96.FormatTimeNum(_CurvePartValue.StartTimeNum);
                    if (curvePoint == CurvePointOptions.Point144)
                    {
                        if (((_CurvePartValue.StartTimeNum / 100) % 100) == 20)
                        {
                            intStartTimeNum96 = (intStartTimeNum96 / 10000) * 10000;
                        }
                        else if (((_CurvePartValue.StartTimeNum / 100) % 100) == 50)
                        {
                            intStartTimeNum96 = (intStartTimeNum96 / 10000) * 10000 + 3000;
                        }
                    }

                    if (intStartTimeNum96 < _CurvePartValue.StartTimeNum)
                    {
                        //如果数据的起始时间不是每小时的00分、15分、30分、45分,则从数据库取出该一刻钟缺少的数据。
                        CurvePartValue _CurvePartValueTemp = new CurvePartValue(curvePoint, intStartTimeNum96, _CurvePartValue.EndTimeNum);
                        for (int intIndex = 0; intIndex < _CurvePartValue.Values.Length; intIndex++)
                        {
                            _CurvePartValueTemp.SetValue(_CurvePartValue.GetTimeNum(intIndex), _CurvePartValue.Values[intIndex]);
                        }

                        strSql = "SELECT * FROM " + strTableName + " WHERE DateNum=" + Function.ToIntDate(date) + " AND TimeNum>=" + intStartTimeNum96 + " AND TimeNum<" + _CurvePartValue.StartTimeNum + " AND DeviceId=" + deviceId + strWhere;
                        System.Data.IDataReader dr = dbConnection.ExecuteReader(strSql);
                        while (dr.Read())
                        {
                            _CurvePartValueTemp.SetValue(Function.ToInt(dr["TimeNum"]), Function.ToDecimal(dr["DataValue"]));
                        }
                        dr.Close();

                        //转换为96点数据
                        _CurvePartValueTemp.Convert(CurvePointOptions.Point96, mergeOption, splitOption);
                        _CurvePartValue = _CurvePartValueTemp;
                    }
                }
            }

            if (bolSave96)
            {
                //96点数据储存
                strTableName = Database.DbConnection.GetMonthlyName(tableNameF, date.Year, date.Month);
                if (dbConnection.TableIsExist(strTableName) == false)
                {
                    dbConnection.CreateTable(tableTypeF, strTableName);
                }

                if (_CurvePartValue.CurvePoint > CurvePointOptions.Point96)
                {
                    _CurvePartValue.Convert(CurvePointOptions.Point96, mergeOption, splitOption);
                }

                try
                {
                    string strValue  = "";
                    string strColumn = "";
                    foreach (KeyValuePair <string, string> kvp in keyColumns)
                    {
                        strValue  += "," + kvp.Value;
                        strColumn += "," + kvp.Key;
                    }

                    string strColumnValues = "";
                    string strColumnNames  = "";
                    for (int intIndex = 0; intIndex < _CurvePartValue.Values.Length; intIndex++)
                    {
                        strColumnValues += "," + Function.SqlDecimal(_CurvePartValue.Values[intIndex]);
                        strColumnNames  += ",Value" + ((_CurvePartValue.GetPointIndex(intIndex) * (96 / _CurvePartValue.CurvePoint.GetPointCount())) + 1);
                    }
                    strSql = Function.ToIntDate(date) + "," + deviceId + strValue + strColumnValues;
                    strSql = "INSERT INTO " + strTableName + " (DateNum,DeviceId" + strColumn + strColumnNames + ") VALUES (" + strSql + ")";
                    dbConnection.ExecuteNonQuery(strSql);
                }
                catch
                {
                    string strWhere = "";
                    foreach (KeyValuePair <string, string> kvp in keyColumns)
                    {
                        strWhere += " AND " + kvp.Key + "=" + kvp.Value;
                    }

                    string strColumns = "";
                    for (int intIndex = 0; intIndex < _CurvePartValue.Values.Length; intIndex++)
                    {
                        strColumns += ",Value" + ((_CurvePartValue.GetPointIndex(intIndex) * (96 / _CurvePartValue.CurvePoint.GetPointCount())) + 1) + "=" + Function.SqlDecimal(_CurvePartValue.Values[intIndex]);
                    }
                    strSql = "UPDATE " + strTableName + " Set " + strColumns.Substring(1) + " Where DateNum=" + Function.ToIntDate(date) + " AND DeviceId=" + deviceId + strWhere;
                    dbConnection.ExecuteNonQuery(strSql);
                }
            }
        }
Example #3
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);
        }
Example #4
0
        /// <summary>
        /// 保存设备曲线数据。
        /// </summary>
        /// <param name="tableTypeF">96及以下点数数据表类型声明。</param>
        /// <param name="tableNameF">96及以下点数数据表名。</param>
        /// <param name="tableTypeS">96以上点数数据表类型声明。</param>
        /// <param name="tableNameS">96以上点数数据表名。</param>
        /// <param name="mergeOption">当转换后的点数小于目前曲线点数时,如何合并目前的数据。</param>
        /// <param name="splitOption">当转换后的点数大于目前曲线点数时,如何拆分目前的数据。</param>
        /// <param name="dbConnection">活动的数据库连接。</param>
        /// <param name="deviceId">设备编号。</param>
        /// <param name="date">数据日期。只使用其中的年、月、日。</param>
        /// <param name="values">曲线数据值,该数组长度必须是 CurvePointOptions 中指定的长度之一。</param>
        /// <param name="keyColumns">该数据所属数据表的额外主键字段,及这些主键字段的数值。</param>
        public static void Save(Type tableTypeF, string tableNameF, Type tableTypeS, string tableNameS, CurveMergeOptions mergeOption, CurveSplitOptions splitOption, Database.DbConnection dbConnection, int deviceId, DateTime date, decimal?[] values, params KeyValuePair <string, string>[] keyColumns)
        {
            CurveValue _CurveValue = new CurveValue(values);
            string     strSql;
            string     strTableName;

            if (_CurveValue.CurvePoint > CurvePointOptions.Point96)
            {
                //储存分秒数据表
                strTableName = Database.DbConnection.GetMonthlyName(tableNameS, date.Year, date.Month);
                if (dbConnection.TableIsExist(strTableName) == false)
                {
                    dbConnection.CreateTable(tableTypeS, strTableName);
                }

                string strWhere = "";
                foreach (KeyValuePair <string, string> kvp in keyColumns)
                {
                    strWhere += " AND " + kvp.Key + "=" + kvp.Value;
                }
                strSql = "Delete From " + strTableName + " Where DateNum=" + Function.ToIntDate(date) + " AND DeviceId=" + deviceId + strWhere;
                dbConnection.ExecuteNonQuery(strSql);

                string strValue  = "";
                string strColumn = "";
                foreach (KeyValuePair <string, string> kvp in keyColumns)
                {
                    strValue  += "," + kvp.Value;
                    strColumn += "," + kvp.Key;
                }
                for (int intIndex = 0; intIndex < _CurveValue.Values.Length; intIndex++)
                {
                    try
                    {
                        strSql = Function.ToIntDate(date) + "," + _CurveValue.CurvePoint.GetTimeNum(intIndex) + "," + deviceId + strValue + "," + Function.SqlDecimal(values[intIndex]);
                        strSql = "INSERT INTO " + strTableName + " (DateNum,TimeNum,DeviceId" + strColumn + ",DataValue) VALUES (" + strSql + ")";
                        dbConnection.ExecuteNonQuery(strSql);
                    }
                    catch { }
                }
            }

            //96点数据储存
            strTableName = Database.DbConnection.GetMonthlyName(tableNameF, date.Year, date.Month);
            if (dbConnection.TableIsExist(strTableName) == false)
            {
                dbConnection.CreateTable(tableTypeF, strTableName);
            }

            if (_CurveValue.CurvePoint > CurvePointOptions.Point96)
            {
                _CurveValue.Convert(CurvePointOptions.Point96, mergeOption, splitOption);
            }

            try
            {
                string strValue  = "";
                string strColumn = "";
                foreach (KeyValuePair <string, string> kvp in keyColumns)
                {
                    strValue  += "," + kvp.Value;
                    strColumn += "," + kvp.Key;
                }

                string strColumnValues = "";
                string strColumnNames  = "";
                for (int intIndex = 0; intIndex < _CurveValue.Values.Length; intIndex++)
                {
                    strColumnValues += "," + Function.SqlDecimal(_CurveValue.Values[intIndex]);
                    strColumnNames  += ",Value" + ((intIndex * (96 / _CurveValue.CurvePoint.GetPointCount())) + 1);
                }
                strSql = Function.ToIntDate(date) + "," + deviceId + strValue + strColumnValues;
                strSql = "INSERT INTO " + strTableName + " (DateNum,DeviceId" + strColumn + strColumnNames + ") VALUES (" + strSql + ")";
                dbConnection.ExecuteNonQuery(strSql);
            }
            catch
            {
                string strWhere = "";
                foreach (KeyValuePair <string, string> kvp in keyColumns)
                {
                    strWhere += " AND " + kvp.Key + "=" + kvp.Value;
                }

                string strColumns = "";
                for (int intIndex = 0; intIndex < _CurveValue.Values.Length; intIndex++)
                {
                    strColumns += ",Value" + ((intIndex * (96 / _CurveValue.CurvePoint.GetPointCount())) + 1) + "=" + Function.SqlDecimal(_CurveValue.Values[intIndex]);
                }
                strSql = "UPDATE " + strTableName + " Set " + strColumns.Substring(1) + " Where DateNum=" + Function.ToIntDate(date) + " AND DeviceId=" + deviceId + strWhere;
                dbConnection.ExecuteNonQuery(strSql);
            }
        }