225 lines
		
	
	
		
			9.1 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			225 lines
		
	
	
		
			9.1 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)
 | 
						|
        {
 | 
						|
            Expression<Func<Model.HJ212, bool>> exp = filter => filter.CreateDateTime >= DateTime.Now.AddDays(-day);
 | 
						|
            var result = (await base.GetListWithExp(exp)).ToList();
 | 
						|
            var temp = result.Select(s => new
 | 
						|
            {
 | 
						|
                s.a34001,
 | 
						|
                s.a34002,
 | 
						|
                s.a34004,
 | 
						|
                date = s.CreateDateTime.AddHours(8).ToString("MM-dd")
 | 
						|
            }).ToList();
 | 
						|
            return temp.GroupBy(g => new { g.date })
 | 
						|
                    .Select(s => new
 | 
						|
                    {
 | 
						|
                        s.Key.date,
 | 
						|
                        a34001 = Math.Round(s.Sum(t => t.a34001), 2),
 | 
						|
                        a34002 = Math.Round(s.Sum(t => t.a34002), 2),
 | 
						|
                        a34004 = Math.Round(s.Sum(t => t.a34004), 2)
 | 
						|
                    }).ToList();
 | 
						|
 | 
						|
 | 
						|
            //temp.GroupBy(g => new { g.date }).ToList().ForEach(s =>
 | 
						|
            //{
 | 
						|
            //    var v1 = temp.Where(m => m.date == s.Key.date).Sum(t => t.a34001);
 | 
						|
            //    var v2 = temp.Where(m => m.date == s.Key.date).Sum(t => t.a34002);
 | 
						|
            //    var v3 = temp.Where(m => m.date == s.Key.date).Sum(t => t.a34004);
 | 
						|
            //    list.Add(new columnView() { hour = s.Key.date, type = "a34001", value = v1 });
 | 
						|
            //    list.Add(new columnView() { hour = s.Key.date, type = "a34002", value = Math.Round(v2, 2) });
 | 
						|
            //    list.Add(new columnView() { hour = s.Key.date, type = "a34004", value = Math.Round(v3, 2) });
 | 
						|
            //});
 | 
						|
        }
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// 今日排放数据
 | 
						|
        /// </summary>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task<object> GetTodayData()
 | 
						|
        {
 | 
						|
            // 只取当天的日数据
 | 
						|
            Expression<Func<Model.HJ212, bool>> exp = filter => filter.ST == 31 || filter.ST == 27 && filter.CN == 2031 && filter.CreateDateTime >= DateTime.Now.Date;
 | 
						|
            var result = (await base.GetListWithExp(exp)).ToList();
 | 
						|
            // 过滤掉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是日数据
 | 
						|
            Expression<Func<Model.HJ212, bool>> exp = filter => filter.ST == 31 || filter.ST == 27 && filter.CN == 2031 && filter.CreateDateTime >= DateTime.Now.AddDays(-7);
 | 
						|
            ////a21026_Cou,a21002_Cou,a34013_Cou
 | 
						|
            var result = (await base.GetListWithExp(exp)).ToList();
 | 
						|
            //cems
 | 
						|
            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 };
 | 
						|
        }
 | 
						|
        /// <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;
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |