using DeviceRepair.Models;
using NLog;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;

namespace DeviceRepair.DataAccess
{
    public class SchedulerAccess : DbContext<DriveMaintencePlanInfo>
    {
        private static readonly Logger log = LogManager.GetCurrentClassLogger();

        private static SchedulerAccess schedulerAccess;
        public static SchedulerAccess Instance
        {
            get
            {
                if (schedulerAccess == null)
                    schedulerAccess = new SchedulerAccess();
                return schedulerAccess;
            }
        }

        /// <summary>
        /// 获取当前年月的待保修项数量
        /// </summary>
        /// <returns></returns>
        public APIResponseData GetSchedulerPlanTips(string LoginCode)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = $"数据库通讯失败！" };
            try
            {
                List<DeviceInformationInfo> Devs = DeviceAccess.Instance.GetDevsByLoginAuths(LoginCode);
                if (Devs.Count == 0)
                    return new APIResponseData { Code = -1, Message = $"没有查询到数据！" };

                int[] ids = Devs.Select(x => x.AutoID).ToArray();

                base.db.ChangeDatabase("main");
                int Count = base.db.Queryable<View_CurrentMonthPlanTips>().Where(x => SqlFunc.ContainsArray(ids, x.EquipmentAutoID)).Count();
                apiResponseData.Code = 1;
                apiResponseData.Data = Count;
            }
            catch (SqlSugarException e)
            {
                log.Error(e);
                apiResponseData.Code = -1;
                apiResponseData.Message = e.Message;
            }
            catch (Exception ex)
            {
                log.Error(ex);
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }
    }
}
