You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
4.9 KiB
C#

using HighWayIot.Log4net;
using HighWayIot.Repository.domain;
using Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace HighWayIot.Repository.service
{
/// <summary>
/// 报表服务类
/// </summary>
public class ZxDailyReportService
{
private static readonly Lazy<ZxDailyReportService> lazy = new Lazy<ZxDailyReportService>(() => new ZxDailyReportService());
public static ZxDailyReportService Instance
{
get
{
return lazy.Value;
}
}
private LogHelper log = LogHelper.Instance;
Repository<ZxDailyReportEntity> _repository => new Repository<ZxDailyReportEntity>("sqlserver");
/// <summary>
/// 查询报表信息
/// </summary>
/// <returns></returns>
public List<ZxDailyReportEntity> GetDailyReportInfos()
{
try
{
DateTime day = DateTime.Now - TimeSpan.FromDays(2);
List<ZxDailyReportEntity> entity =
_repository.AsQueryable()
.Where(x => x.StartTime >= day)
.OrderByDescending(x => x.StartTime)
.ToList();
return entity;
}
catch (Exception ex)
{
log.Error("报表息获取异常", ex);
return null;
}
}
/// <summary>
/// 查询报表信息
/// </summary>
/// <returns></returns>
public List<ZxDailyReportEntity> GetOneDayDailyReportInfos()
{
try
{
DateTime day = DateTime.Now - TimeSpan.FromDays(1);
List<ZxDailyReportEntity> entity =
_repository.AsQueryable()
.Where(x => x.StartTime >= day)
.OrderByDescending(x => x.StartTime)
.ToList();
return entity;
}
catch (Exception ex)
{
log.Error("报表息获取异常", ex);
return null;
}
}
/// <summary>
/// 得到最近的一次符合条件的数据
/// </summary>
/// <returns></returns>
public ZxDailyReportEntity GetNearData(int rgvNo)
{
try
{
ZxDailyReportEntity entity =
_repository.AsQueryable()
.OrderByDescending(x => x.StartTime)
.First(x => x.DeviceNo == rgvNo);
return entity;
}
catch (Exception ex)
{
log.Error("报表信息获取异常或无数据", ex);
return null;
}
}
/// <summary>
/// 根据条件查询报表信息
/// </summary>
/// <param name="whereExpression"></param>
/// <returns></returns>
public List<ZxDailyReportEntity> GetDailyReportInfos(Expression<Func<ZxDailyReportEntity, bool>> whereExpression)
{
try
{
List<ZxDailyReportEntity> entity = _repository.GetList(whereExpression);
return entity;
}
catch (Exception ex)
{
log.Error("报表信息获取异常", ex);
return null;
}
}
/// <summary>
/// 新增报表信息
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public bool InsertDailyReportInfo(ZxDailyReportEntity entity)
{
try
{
return _repository.Insert(entity);
}
catch (Exception ex)
{
log.Error("报表信息添加异常", ex);
return false;
}
}
/// <summary>
/// 删除报表信息
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public bool UpdateDailyReportInfo(ZxDailyReportEntity entity)
{
try
{
return _repository.Update(entity);
}
catch (Exception ex)
{
log.Error("报表信息修改异常", ex);
return false;
}
}
/// <summary>
/// 删除一个月之前的数据
/// </summary>
/// <returns></returns>
public bool DeleteMoreData()
{
try
{
return _repository.Delete(x => x.StartTime <= (DateTime.Now - TimeSpan.FromDays(30)));
}
catch (Exception ex)
{
log.Error("数据删除异常", ex);
return false;
}
}
}
}