/// <summary> /// /// </summary> /// <param name="type">P:排口 C:企业</param> /// <param name="id"></param> /// <param name="startTimeMonitor"></param> /// <param name="endTimeMonitor"></param> /// <param name="startTimeAvg"></param> /// <param name="endTimeAvg"></param> /// <param name="compamyType">1:污染源 2:VOCs</param> /// <returns></returns> public DataTable AnalysisData(string type, string id, DateTime startTimeMonitor, DateTime endTimeMonitor, DateTime startTimeAvg, DateTime endTimeAvg, string compamyType, string dataType) { DataTable monitorData = BaseMonitorData(type, id, startTimeMonitor, endTimeMonitor, compamyType, dataType); Dictionary <string, decimal> avgDic = MonitorAvgData(type, id, startTimeAvg, endTimeAvg, compamyType, dataType); List <string> avgKeys = avgDic.Keys.ToList(); //添加平均列(赋初值)和百分比列 avgKeys.ForEach(key => { DataColumn avgColumn = new DataColumn(key + "_Avg"); avgColumn.DefaultValue = avgDic[key]; DataColumn percentColumn = new DataColumn(key + "_Percent"); monitorData.Columns.Add(avgColumn); monitorData.Columns.Add(percentColumn); }); //计算百分比 for (int i = 0; i < monitorData.Rows.Count; i++) { DataRow row = monitorData.Rows[i]; avgKeys.ForEach(item => { decimal data = StringHelper.DynamicToDecimal(row[item]); decimal avg = StringHelper.DynamicToDecimal(row[item + "_Avg"]); if (avg == 0) { return; } row[item + "_Percent"] = (data / avg * 100).ToString("0.00"); }); } return(monitorData); }
/// <summary> /// 计算监测数据的平均平均值 /// </summary> /// <param name="type"></param> /// <param name="id"></param> /// <param name="startTime"></param> /// <param name="endTime"></param> /// <param name="compamyType"></param> /// <returns></returns> public Dictionary <string, decimal> MonitorAvgData(string type, string id, DateTime startTime, DateTime endTime, string compamyType, string dataType) { /* * 1.获取监测数据 * 2.去掉不需要的不需要统计的列,这里只是统计排放量 * 3.将列名和均值保存在字典中 */ string dataClass = "1" == compamyType?string.Join(",", MonitorData.POLLUTION_MONITOR_ITEM_TYPE) : string.Join(",", MonitorData.VOCS_MONITOR_ITEM_TYPE); DataTable monitorData = MonitorData.GetMonitorData("002", startTime, endTime, type, id, dataType, dataClass); DataTable exchangeTable = ColumnNameExchange(type, id, compamyType, monitorData); Dictionary <string, decimal> dic = new Dictionary <string, decimal>(); List <dynamic> data = exchangeTable.ToDynamicList(); for (int i = 0; i < exchangeTable.Columns.Count; i++) { DataColumn column = exchangeTable.Columns[i]; if ("DATA_TIME" == column.ColumnName) { continue; } decimal?avgData = data.Average(item => { if (item[column.ColumnName] is DBNull) { return(null); } decimal temp = StringHelper.DynamicToDecimal(item[column.ColumnName]); return(temp); }); dic.Add(column.ColumnName, Math.Round(avgData.HasValue?avgData.Value:0, 2)); } return(dic); }
/// <summary> /// 区域监测数据统计 /// </summary> /// <param name="areaCode">区域编码,多个实用逗号分隔</param> /// <param name="startTime">开始时间</param> /// <param name="endTime">结束时间</param> /// <param name="dataType">day:日数据、hour:小时数据、month:月数据</param> /// <returns></returns> public static List <dynamic> GetAreaMonitorData(string areaCode, DateTime startTime, DateTime endTime, string dataType, string dataClass) { string[] areaCodeArray = areaCode.Split(','); List <dynamic> companyList = GetCompanyInfo(areaCode); List <string> companyIDList = new List <string>(); companyList.ForEach(c => { if (!string.IsNullOrEmpty(StringHelper.DynamicToString(c["ID"]))) { companyIDList.Add(StringHelper.DynamicToString(c["ID"])); } }); DataTable table = MonitorData.GetMonitorData("2", startTime, endTime, "C", companyIDList, dataType, dataClass); DataTable resultTable = new DataTable(); resultTable.Columns.Add(new DataColumn("AREA_CODE")); resultTable.Columns.Add(new DataColumn("AREA_NAME")); resultTable.Columns.Add(new DataColumn("DATA_TIME")); List <string> colunmNameList = new List <string>(); foreach (DataColumn column in table.Columns) { if (column.ColumnName.Contains("_Value")) { resultTable.Columns.Add(new DataColumn(column.ColumnName)); colunmNameList.Add(column.ColumnName); } } List <dynamic> monitorData = table.ToDynamicList(); foreach (string code in areaCodeArray) { List <string> cID = new List <string>(); try { companyList.Where(item => StringHelper.DynamicToString(item["AREA"]) == code).ToList().ForEach(item => { cID.Add(StringHelper.DynamicToString(item["ID"])); }); } catch (Exception ex) { }; if (cID.Count < 1) { continue; } var areaCompanyMonitorData = monitorData.Where(item => cID.Contains(StringHelper.DynamicToString(item["ID"]))).ToList(); List <string> existTime = new List <string>(); areaCompanyMonitorData.ForEach(item => { string dateTime = StringHelper.DynamicToString(item["DATA_TIME"]); if (existTime.Contains(dateTime)) { return; } existTime.Add(dateTime); List <dynamic> list = areaCompanyMonitorData.Where(c => dateTime == StringHelper.DynamicToString(c["DATA_TIME"])).ToList(); DataRow row = resultTable.NewRow(); row["DATA_TIME"] = item["DATA_TIME"]; row["AREA_CODE"] = code; row["AREA_NAME"] = GetAreaInfo(code)[0]["AREA_TEXT"]; colunmNameList.ForEach(column => { if (string.IsNullOrEmpty(column)) { return; } string[] itemCodeArray = column.Split('_'); string mianItemCode = itemCodeArray[0]; string childItemCode = itemCodeArray.Length > 2 ? itemCodeArray[1] : ""; //string calculateType = MonitorData.MonitorItemCalculateType(mianItemCode, childItemCode); string calculateType = "+"; if ("+" == calculateType) { row[column] = list.Sum(c => { decimal temp = StringHelper.DynamicToDecimal(c[column]); return(temp); }).ToString(); } if ("/" == calculateType) { row[column] = list.Average(c => { decimal temp = StringHelper.DynamicToDecimal(c[column]); return(temp); }).ToString(); } }); resultTable.Rows.Add(row); }); } return(resultTable.ToDynamicList()); }