using System; using System.Linq; using System.Threading.Tasks; using System.Collections.Generic; using HT.Cloud.Code; using SqlSugar; using HT.Cloud.DataBase; using HT.Cloud.Domain.ChartsManage; using Serenity.Services; using iTextSharp.text; using HT.Cloud.Domain.DevicesManage; using Microsoft.AspNetCore.Mvc; using HT.Cloud.Domain.ReportManage; using Quartz; using Serenity.Data; namespace HT.Cloud.Service.ChartsManage { /// /// 创 建:cdl /// 日 期:2023-02-23 11:10 /// 描 述:历史趋势服务类 /// public class HistoryTechChartsService : BaseService, IDenpendency { /// /// 绑定数据仓库的数据库ID /// private string dataDBNumber = GlobalContext.SystemConfig.DataDBNumber; public HistoryTechChartsService(ISqlSugarClient context) : base(context) { } public async Task GetAllTagList(string systemName, string chartTagType) { var groupName = _context.AsTenant().QueryableWithAttr().Where(x => x.GroupDescription == systemName).First().GroupName; var driverId = _context.AsTenant().QueryableWithAttr().Where(x => x.GroupDescription == systemName).First().DriverID; var driverName = _context.AsTenant().QueryableWithAttr().Where(x => x.DriverID == driverId).First().DriverName; var list = _context.AsTenant().GetConnection(dataDBNumber).Ado.SqlQuery($"SELECT * FROM HT_{driverName}_{groupName}_CHART WHERE ChartTagType = '{chartTagType}'"); return list.ToJson(); } public async Task GetDriverGroupList() { List subsystems = new List(); var list_group = _context.AsTenant().QueryableWithAttr().ToList(); int i = 0; foreach (var group in list_group) { subsystems.Add(new GroupNameList() { Lable = "a" + i, Value = group.GroupDescription }); i++; } return subsystems.ToJson(); } public async Task GetGroupSubsystemTag(string groupDescription) { List subsystems = new List(); var groupId = _context.AsTenant().QueryableWithAttr().Where(x => x.GroupDescription == groupDescription).First().GroupID; var grouped = _context.AsTenant().QueryableWithAttr().ToList().Where(x=>x.GroupID==groupId); List list_name_two = grouped.Select(t => t.Subsystem).GroupBy(c => c).Select(c => c.First()).ToList(); int i = 0; foreach (var Meta_Tag in list_name_two) { subsystems.Add(new SubsysytemNameList() { Lable = "a" + i, Value = Meta_Tag }); i++; } return subsystems.ToJson(); } public async Task GetAllSubsystemTag() { List subsystems = new List(); var grouped = _context.AsTenant().QueryableWithAttr().ToList(); List list_name_two = grouped.Select(t => t.Subsystem).GroupBy(c => c).Select(c => c.First()).ToList(); int i = 0; foreach (var Meta_Tag in list_name_two) { subsystems.Add(new SubsysytemNameList() { Lable = "a" + i, Value = Meta_Tag }); i++; } return subsystems.ToJson(); } public async Task GetSubsystemAllTag(string subsysytemName,string groupDescription) { var subsysname = subsysytemName; var groupId = _context.AsTenant().QueryableWithAttr().Where(x => x.GroupDescription == groupDescription).First().GroupID; var response = _context.AsTenant().QueryableWithAttr().Where(x => x.Subsystem == subsysname&&x.GroupID == groupId).Select(x => new { x.TagID, x.Description, x.Units }).ToList(); var reList = new List(); foreach (var subsystem in response) { reList.Add( new SubAllTagReturn() { TagID =(short)subsystem.TagID, Description = subsystem.Description + $"({subsystem.Units})", Units = subsystem.Units } ); } return reList.ToJson(); } public async Task GetChartsTagValue(string table, string tagName, string startdt, string enddt, int timeinterval) { SugarParameter[] sqlParameters = { new SugarParameter("@HTable",table), new SugarParameter("@HTagName",tagName), new SugarParameter("@startdt",startdt), new SugarParameter("@enddt",enddt), new SugarParameter("@timeinterval",timeinterval) }; var dt = _context.AsTenant().GetConnection(dataDBNumber).Ado.UseStoredProcedure().GetDataTable("Query_Chart_Tag", sqlParameters); //* //var ds = db.SqlQuery("Query_Report_Period", sqlParameters);/// var valuelist = DataTableConvertModel.ConvertDataTableToList(dt); var resultlist = new List(); foreach (var value in valuelist) { resultlist.Add(new ChartData() { name = value.HDATETIME, value = value.VALUE }); } return resultlist.ToJson(); } public class SubsysytemNameList { public string Lable { get; set; } public string Value { get; set; } } private class GroupNameList { public string Lable { get; set; } public string Value { get; set; } } public class SubAllTagReturn { //x.TagID, x.Description, x.Units public short TagID { get; set; } public string Description { get; set; } public string Units { get; set; } } public class ChartData { public decimal value { get; set; } public String name { get; set; } } public class HDATETIMEVALUE { public String HDATETIME { get; set; } public decimal VALUE { get; set; } } } }