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

namespace DeviceRepair.DataAccess
{
    public class TsSFCAccess : DbContext<InspBatchInfo>
    {
        private static TsSFCAccess manager;
        public static TsSFCAccess Instance
        {
            get
            {
                if (manager == null)
                    manager = new TsSFCAccess();
                return manager;
            }
        }

        /// <summary>
        /// 判断批次是否存在
        /// </summary>
        /// <param name="Batch"></param>
        /// <returns></returns>
        public APIResponseData GetBatchInfoToStaff(string Batch)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = $"获取数据失败！" };
            try
            {
                db.ChangeDatabase("data");
                int Count = db.Queryable<InspBatchInfo>().Count(x => x.Batch.ToUpper().Equals(Batch.ToUpper()));

                apiResponseData.Code = 1;
                apiResponseData.Message = "";
                apiResponseData.Data = Count;
            }
            catch (SqlSugarException ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 判断当前账户是否PE QE
        /// </summary>
        /// <param name="UserCode"></param>
        /// <param name="UserName"></param>
        /// <param name="Post"></param>
        /// <returns></returns>
        public APIResponseData CurrentIsManager(string UserCode)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = $"获取数据失败！" };
            try
            {
                db.ChangeDatabase("addon");
                int Count = db.Queryable<UserPostsInfo>().Count(x => x.UserCode == UserCode);

                apiResponseData.Code = 1;
                apiResponseData.Message = "";
                apiResponseData.Data = Count;
            }
            catch (SqlSugarException ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 获取当前批次的QE PE邮箱
        /// </summary>
        /// <param name="Batch"></param>
        /// <returns></returns>
        public APIResponseData CurrentBatchManagerEmail(string Batch)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = $"获取数据失败！" };
            List<string> emailLst = new List<string>();
            try
            {
                db.ChangeDatabase("data");
                InspBatchInfo BatchInfo = db.Queryable<InspBatchInfo>().First(x => x.Batch.ToUpper().Equals(Batch.ToUpper()));
                if (BatchInfo != null)
                {
                    db.ChangeDatabase("addon");
                    List<ResourceAllocationsInfo> allocationsInfos = db.Queryable<ResourceAllocationsInfo>().Where(x => x.Product.Trim().Equals(BatchInfo.Product.ToUpper()) &&
                    x.Technology.Trim().ToUpper() == BatchInfo.Technology.Trim().ToUpper()).ToList();

                    if (allocationsInfos != null)
                    {
                        Guid[] guids = allocationsInfos.Select(x => x.Staff).ToArray();
                        List<StaffsInfo> staffs = db.Queryable<StaffsInfo>().Where(x => SqlFunc.ContainsArray(guids, x.Guid) && (x.Post == "0" || x.Post == "1")).ToList();
                        if (staffs != null && staffs.Count > 0)
                        {
                            emailLst.AddRange(staffs.Select(x => x.EMail).ToArray());
                        }
                    }
                }

                apiResponseData.Code = 1;
                apiResponseData.Message = "";
                apiResponseData.Data = emailLst;
            }
            catch (SqlSugarException ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

    }
}
