260 lines
		
	
	
		
			10 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			260 lines
		
	
	
		
			10 KiB
		
	
	
	
		
			C#
		
	
	
	
using langguanApi.Extensions.AutoDI;
 | 
						||
using langguanApi.Model;
 | 
						||
using langguanApi.Model.Dto;
 | 
						||
using MongoDB.Bson;
 | 
						||
using MongoDB.Driver;
 | 
						||
using NPOI.HSSF.Record;
 | 
						||
using System.Diagnostics;
 | 
						||
using System.Linq.Expressions;
 | 
						||
using System.Text.RegularExpressions;
 | 
						||
 | 
						||
namespace langguanApi.Service
 | 
						||
{
 | 
						||
    [ServiceInjection(InjectionType.Transient)]
 | 
						||
    public class Hj212Service : BaseService<Model.HJ212>
 | 
						||
    {
 | 
						||
        private DeviceService _deviceSerive;
 | 
						||
        public Hj212Service(IConfiguration config, DeviceService deviceSerive) : base(config, nameof(Model.HJ212))
 | 
						||
        {
 | 
						||
            _deviceSerive = deviceSerive;
 | 
						||
        }
 | 
						||
        /// <summary>
 | 
						||
        /// 首页数据
 | 
						||
        /// </summary>
 | 
						||
        /// <param name="day"></param>
 | 
						||
        /// <returns></returns>
 | 
						||
        public async Task<object> GetIndexData(int day = 7)
 | 
						||
        {
 | 
						||
            var now = DateTime.UtcNow;         // 获取当前时间(UTC)
 | 
						||
            var sevenDaysAgo = now.AddDays(-2); // 计算 7 天前的时间
 | 
						||
            var pipeline = new[]
 | 
						||
                       {
 | 
						||
                    new BsonDocument("$match", new BsonDocument
 | 
						||
                    {
 | 
						||
                        { "CreateDateTime", new BsonDocument { { "$gte", sevenDaysAgo }, { "$lt", now } } }
 | 
						||
                    }),
 | 
						||
                    new BsonDocument("$project", new BsonDocument
 | 
						||
                    {
 | 
						||
                        { "date", new BsonDocument("$dateToString", new BsonDocument { { "format", "%m-%d" }, { "date", "$CreateDateTime" } }) },
 | 
						||
                        { "a34001", 1 },
 | 
						||
                        { "a34002", 1 },
 | 
						||
                        { "a34004", 1 }
 | 
						||
                    }),
 | 
						||
                    new BsonDocument("$group", new BsonDocument
 | 
						||
                    {
 | 
						||
                        { "_id", "$date" },
 | 
						||
                        { "a34001", new BsonDocument("$sum", "$a34001") },
 | 
						||
                        { "a34002", new BsonDocument("$sum", "$a34002") },
 | 
						||
                        { "a34004", new BsonDocument("$sum", "$a34004") }
 | 
						||
                    }),
 | 
						||
                    new BsonDocument("$sort", new BsonDocument { { "_id", 1 } }) // 按日期排序
 | 
						||
                };
 | 
						||
 | 
						||
            var temp = await base.GetGroupedResultsAsync(pipeline);
 | 
						||
            List<object> result = new List<object>();
 | 
						||
            foreach (var item in temp)
 | 
						||
            {
 | 
						||
                result.Add(new
 | 
						||
                {
 | 
						||
                    date = item["_id"].ToString(),
 | 
						||
                    a34001 = Math.Round(item["a34001"].ToDouble(), 2),
 | 
						||
                    a34002 = Math.Round(item["a34002"].ToDouble(), 2),
 | 
						||
                    a34004 = Math.Round(item["a34004"].ToDouble(), 2)
 | 
						||
                });
 | 
						||
            }
 | 
						||
            return result;
 | 
						||
        }
 | 
						||
 | 
						||
        /// <summary>
 | 
						||
        /// 今日排放数据
 | 
						||
        /// </summary>
 | 
						||
        /// <returns></returns>
 | 
						||
        public async Task<object> GetTodayData()
 | 
						||
        {
 | 
						||
 | 
						||
            //   Expression<Func<Model.HJ212, bool>> exp = filter => filter.CreateDateTime >= DateTime.Now.AddDays(-7);
 | 
						||
            //   CN = 2031是日数据
 | 
						||
            var dateFilter = BuildFilter(DateTime.Now.Date);
 | 
						||
            // 构建查询
 | 
						||
            var conditionFilter = Builders<Model.HJ212>.Filter.Or(
 | 
						||
        Builders<Model.HJ212>.Filter.Eq(x => x.ST, 31),
 | 
						||
        Builders<Model.HJ212>.Filter.And(
 | 
						||
            Builders<Model.HJ212>.Filter.Eq(x => x.ST, 27),
 | 
						||
            Builders<Model.HJ212>.Filter.Eq(x => x.CN, 2031)
 | 
						||
        )
 | 
						||
        );
 | 
						||
            var finalFilter = Builders<Model.HJ212>.Filter.And(dateFilter, conditionFilter);
 | 
						||
            var result = await base.FindListyFilter(finalFilter);
 | 
						||
            // 过滤掉voc设备
 | 
						||
            var voc = result.Where(s => s.ST == 31).ToList().Select(s => s.a05002_Cou +
 | 
						||
                s.a24087_Cou +
 | 
						||
                s.a24088_Cou +
 | 
						||
                s.a25002_Cou +
 | 
						||
                s.a25003_Cou +
 | 
						||
                s.a25005_Cou +
 | 
						||
                s.a00000_Cou).Sum();
 | 
						||
            var cems = result.Where(s => s.ST == 27).ToList().Select(s => s.a21002_Cou +
 | 
						||
                s.a21026_Cou +
 | 
						||
                s.a34013_Cou +
 | 
						||
                s.a00000_Cou).Sum();
 | 
						||
            return new
 | 
						||
            {
 | 
						||
                today = new { voc, cems },
 | 
						||
                week = await GetWeekData()
 | 
						||
            };
 | 
						||
        }
 | 
						||
        /// <summary>
 | 
						||
        /// 一周之内的排放数据
 | 
						||
        /// </summary>
 | 
						||
        /// <returns></returns>
 | 
						||
        public async Task<object> GetWeekData()
 | 
						||
        {
 | 
						||
            //   Expression<Func<Model.HJ212, bool>> exp = filter => filter.CreateDateTime >= DateTime.Now.AddDays(-7);
 | 
						||
            //   CN = 2031是日数据
 | 
						||
            var dateFilter = Builders<Model.HJ212>.Filter.Gte(x => x.CreateDateTime, DateTime.Now.AddDays(-7));
 | 
						||
 | 
						||
            // 构建查询
 | 
						||
            var conditionFilter = Builders<Model.HJ212>.Filter.Or(
 | 
						||
        Builders<Model.HJ212>.Filter.Eq(x => x.ST, 31),
 | 
						||
        Builders<Model.HJ212>.Filter.And(
 | 
						||
            Builders<Model.HJ212>.Filter.Eq(x => x.ST, 27),
 | 
						||
            Builders<Model.HJ212>.Filter.Eq(x => x.CN, 2031)
 | 
						||
        )
 | 
						||
        );
 | 
						||
            // 组合过滤器(先按时间,再按其他条件)
 | 
						||
            var finalFilter = Builders<Model.HJ212>.Filter.And(dateFilter, conditionFilter);
 | 
						||
            var result = await base.FindListyFilter(finalFilter);
 | 
						||
            ////a21026_Cou,a21002_Cou,a34013_Cou
 | 
						||
            var cems = result.Where(s => s.ST == 31).Select(s => new
 | 
						||
            {
 | 
						||
                s.a21002_Cou,
 | 
						||
                s.a21026_Cou,
 | 
						||
                s.a34013_Cou,
 | 
						||
                s.a00000_Cou,
 | 
						||
                date = s.CreateDateTime.ToString("MM-dd")
 | 
						||
            }).GroupBy(g => g.date).Select(s => new
 | 
						||
            {
 | 
						||
                s.Key,
 | 
						||
                value = Math.Round(s.Sum(t => t.a21002_Cou + t.a21026_Cou + t.a34013_Cou + t.a00000_Cou))
 | 
						||
            });
 | 
						||
            //voc
 | 
						||
            var voc = result.Where(s => s.ST == 27).Select(s => new
 | 
						||
            {
 | 
						||
                s.a05002_Cou,
 | 
						||
                s.a24087_Cou,
 | 
						||
                s.a24088_Cou,
 | 
						||
                s.a25002_Cou,
 | 
						||
                s.a25003_Cou,
 | 
						||
                s.a25005_Cou,
 | 
						||
                s.a00000_Cou,
 | 
						||
                date = s.CreateDateTime.ToString("MM-dd")
 | 
						||
            }).GroupBy(g => g.date).Select(s => new
 | 
						||
            {
 | 
						||
                s.Key,
 | 
						||
                value = Math.Round(s.Sum(t => t.a05002_Cou + t.a24087_Cou + t.a24088_Cou + t.a25002_Cou + t.a25005_Cou + t.a00000_Cou))
 | 
						||
            });
 | 
						||
            return new { voc, cems };
 | 
						||
        }
 | 
						||
        private FilterDefinition<Model.HJ212> BuildFilter(DateTime end)
 | 
						||
        {
 | 
						||
            return Builders<Model.HJ212>.Filter.Gte(x => x.CreateDateTime, end);
 | 
						||
        }
 | 
						||
        /// <summary>
 | 
						||
        /// 新加数据
 | 
						||
        /// </summary>
 | 
						||
        /// <param name="hJ212"></param>
 | 
						||
        /// <param name="deviceIp"></param>
 | 
						||
        /// <returns></returns>
 | 
						||
        public async Task Add(Model.HJ212 hJ212, string deviceIp)
 | 
						||
        {
 | 
						||
            ////判断设备类型 tsp 会有经纬度数据
 | 
						||
            //  int deviceType = 1;//设备类型为1 =voc
 | 
						||
 | 
						||
            ////先判断当前设备是否存在
 | 
						||
            //await _deviceSerive.Add(new DeviceAddDto()
 | 
						||
            //{
 | 
						||
            //    deviceMN = hJ212.deviceMN,
 | 
						||
            //    Ip = deviceIp,
 | 
						||
            //    lat = hJ212.lat,
 | 
						||
            //    lng = hJ212.lng,
 | 
						||
            //    DeviceType = deviceType
 | 
						||
            //});
 | 
						||
            await base.CreateAsync(hJ212);
 | 
						||
        }
 | 
						||
        /// <summary>
 | 
						||
        /// 最近10个小时的数据
 | 
						||
        /// </summary>
 | 
						||
        /// <param name="hours"></param>
 | 
						||
        /// <returns></returns>
 | 
						||
        public async Task<List<Model.HJ212>> GetViewTop(int hours = -5)
 | 
						||
        {
 | 
						||
            var date = DateTime.Now.AddHours(-8).AddHours(hours);
 | 
						||
            Expression<Func<Model.HJ212, bool>> exp = filter => filter.CreateDateTime >= date;
 | 
						||
            var result = (await base.GetListWithExp(exp)).ToList();
 | 
						||
 | 
						||
            return result;
 | 
						||
        }
 | 
						||
        /// <summary>
 | 
						||
        /// 按设备号查询数据
 | 
						||
        /// </summary>
 | 
						||
        /// <param name="deviceMn"></param>
 | 
						||
        /// <returns></returns>
 | 
						||
        public async Task<object> GetViewByDeviceMn(string deviceMn)
 | 
						||
        {
 | 
						||
            Expression<Func<Model.HJ212, bool>> exp = filter => filter.deviceMN == deviceMn;
 | 
						||
            var result = (await base.GetListWithExp(exp)).OrderByDescending(s => s.CreateDateTime).Take(60).ToList();
 | 
						||
            List<columnView> list = new List<columnView>();
 | 
						||
            var temp = result.Select(s => new
 | 
						||
            {
 | 
						||
                s.a34001,
 | 
						||
                s.a34002,
 | 
						||
                s.a34004,
 | 
						||
                hour = s.CreateDateTime.AddHours(8).ToString("yyyy-MM-dd HH:mm")
 | 
						||
            }).ToList();
 | 
						||
 | 
						||
            temp.GroupBy(g => new { g.hour }).ToList().ForEach(s =>
 | 
						||
            {
 | 
						||
                var v1 = temp.Where(m => m.hour == s.Key.hour).Sum(t => t.a34001);
 | 
						||
                var v2 = temp.Where(m => m.hour == s.Key.hour).Sum(t => t.a34002);
 | 
						||
                var v3 = temp.Where(m => m.hour == s.Key.hour).Sum(t => t.a34004);
 | 
						||
                list.Add(new columnView() { hour = s.Key.hour, type = "a34001", value = v1 });
 | 
						||
                list.Add(new columnView() { hour = s.Key.hour, type = "a34002", value = Math.Round(v2, 2) });
 | 
						||
                list.Add(new columnView() { hour = s.Key.hour, type = "a34004", value = Math.Round(v3, 2) });
 | 
						||
            });
 | 
						||
 | 
						||
            return list;
 | 
						||
        }
 | 
						||
 | 
						||
        /// <summary>
 | 
						||
        /// 实时的数据
 | 
						||
        /// </summary>
 | 
						||
        /// <returns></returns>
 | 
						||
        public async Task<List<columnView>> Realtime()
 | 
						||
        {
 | 
						||
            Expression<Func<Model.HJ212, bool>> exp = filter => true;
 | 
						||
            var result = (await base.GetListWithExp(exp)).OrderByDescending(s => s.CreateDateTime)
 | 
						||
                .Take(60).OrderBy(s => s.CreateDateTime).ToList();
 | 
						||
            List<columnView> list = new List<columnView>();
 | 
						||
            var temp = result.Select(s => new
 | 
						||
            {
 | 
						||
                s.a34001,
 | 
						||
                s.a34002,
 | 
						||
                s.a34004,
 | 
						||
                hour = s.CreateDateTime.AddHours(8).ToString("yyyy-MM-dd HH:mm")
 | 
						||
            }).ToList();
 | 
						||
 | 
						||
            temp.GroupBy(g => new { g.hour }).ToList().ForEach(s =>
 | 
						||
            {
 | 
						||
                var v1 = temp.Where(m => m.hour == s.Key.hour).Sum(t => t.a34001);
 | 
						||
                var v2 = temp.Where(m => m.hour == s.Key.hour).Sum(t => t.a34002);
 | 
						||
                var v3 = temp.Where(m => m.hour == s.Key.hour).Sum(t => t.a34004);
 | 
						||
                list.Add(new columnView() { hour = s.Key.hour, type = "a34001", value = v1 });
 | 
						||
                list.Add(new columnView() { hour = s.Key.hour, type = "a34002", value = Math.Round(v2, 2) });
 | 
						||
                list.Add(new columnView() { hour = s.Key.hour, type = "a34004", value = Math.Round(v3, 2) });
 | 
						||
            });
 | 
						||
 | 
						||
            return list;
 | 
						||
        }
 | 
						||
    }
 | 
						||
}
 |