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
{
///
/// 报表服务类
///
public class ZxDailyReportService
{
private static readonly Lazy lazy = new Lazy(() => new ZxDailyReportService());
public static ZxDailyReportService Instance
{
get
{
return lazy.Value;
}
}
private LogHelper log = LogHelper.Instance;
Repository _repository => new Repository("sqlserver");
///
/// 查询报表信息
///
///
public List GetDailyReportInfos()
{
try
{
DateTime day = DateTime.Now - TimeSpan.FromDays(2);
List entity =
_repository.AsQueryable()
.Where(x => x.StartTime >= day)
.OrderByDescending(x => x.StartTime)
.ToList();
return entity;
}
catch (Exception ex)
{
log.Error("报表息获取异常", ex);
return null;
}
}
///
/// 查询报表信息
///
///
public List GetOneDayDailyReportInfos()
{
try
{
DateTime day = DateTime.Now - TimeSpan.FromDays(1);
List entity =
_repository.AsQueryable()
.Where(x => x.StartTime >= day)
.OrderByDescending(x => x.StartTime)
.ToList();
return entity;
}
catch (Exception ex)
{
log.Error("报表息获取异常", ex);
return null;
}
}
///
/// 得到最近的一次符合条件的数据
///
///
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;
}
}
///
/// 根据条件查询报表信息
///
///
///
public List GetDailyReportInfos(Expression> whereExpression)
{
try
{
List entity = _repository.GetList(whereExpression);
return entity;
}
catch (Exception ex)
{
log.Error("报表信息获取异常", ex);
return null;
}
}
///
/// 新增报表信息
///
///
///
public bool InsertDailyReportInfo(ZxDailyReportEntity entity)
{
try
{
return _repository.Insert(entity);
}
catch (Exception ex)
{
log.Error("报表信息添加异常", ex);
return false;
}
}
///
/// 删除报表信息
///
///
///
public bool UpdateDailyReportInfo(ZxDailyReportEntity entity)
{
try
{
return _repository.Update(entity);
}
catch (Exception ex)
{
log.Error("报表信息修改异常", ex);
return false;
}
}
///
/// 删除一个月之前的数据
///
///
public bool DeleteMoreData()
{
try
{
return _repository.Delete(x => x.StartTime <= (DateTime.Now - TimeSpan.FromDays(30)));
}
catch (Exception ex)
{
log.Error("数据删除异常", ex);
return false;
}
}
}
}