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> Get25DailyReportInfos()
        {
            try
            {
                List<ZxDailyReportEntity> entity =
                    _repository.AsQueryable()
                    .OrderByDescending(x => x.StartTime)
                    .Take(25)
                    .ToList();
                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;
            }
        }
    }
}
