using static Tiobon.Core.Model.Consts;

namespace Tiobon.Core.Services;

public partial class ReportServices : BaseServices<Ghrh_Resume, Ghrh_ResumeDto, InsertGhrh_ResumeInput, EditGhrh_ResumeInput>, IReportServices
{
    IHttpContextAccessor _httpContextAccessor;

    private readonly IGhra_StaffServices _staffServices;
    public ReportServices(IHttpContextAccessor httpContextAccessor, ICaching caching = null, IGhra_StaffServices staffServices = null)
    {
        _httpContextAccessor = httpContextAccessor;
        _caching = caching;
        _staffServices = staffServices;
    }

    #region 获取新入职人员列表
    /// <summary>
    /// 获取新入职人员列表
    /// </summary>
    /// <param name="filter"></param>
    /// <param name="condition"></param>
    /// <param name="IsEnable"></param>
    /// <returns></returns>
    public async Task<ServicePageResult<Ghra_StaffDto>> QueryNewStaffAsync(QueryBody filter, string condition, bool? IsEnable = true)
    {
        int month = 3;

        var config = await Db.Queryable<Ghre_Config>().Where(x => x.ConfigCode == "New_Staff_Month").FirstAsync();
        if (config != null)
        {
            try
            {
                month = Convert.ToInt32(config.ConfigValue);
            }
            catch (Exception)
            {

            }
        }
        var dateTime = DateTime.Now.AddMonths(-month);

        if (filter.orderBy.IsNotEmptyOrNull())
            filter.orderBy = "Indate DESC";

        condition = $"Indate >= '{DateTimeHelper.ConvertToDayString(dateTime)}' AND Indate <= '{DateTimeHelper.ConvertToDayString(DateTime.Now)}' AND OutDate IS NULL";
        var result = await _staffServices.QueryFilterPage(filter, condition, IsEnable);

        return result;
    }
    #endregion

    #region 面试官-简历筛选通过率
    /// <summary>
    /// 面试官-简历筛选通过率：筛选通过人数 / 待筛选人数 × 100%
    /// </summary>
    /// <param name="langId"></param>
    /// <returns></returns>
    public async Task<ServiceResult<dynamic>> QueryESSResumeScreenPassRate(int langId = 1)
    {

        dynamic data = new ExpandoObject();
        var series = new JArray();
        var XAxis = new List<string>();
        var date = DateTime.Now.AddMonths(-11);
        var data1 = new List<int>();
        var data2 = new List<int>();
        var data3 = new List<decimal>();

        var startDate = DateTime.Now.AddMonths(-11);
        var endDate = DateTime.Now.AddMonths(1);

        startDate = new DateTime(startDate.Year, startDate.Month, 1);
        endDate = new DateTime(endDate.Year, endDate.Month, 1);

        var orderIds = await Db.Queryable<Ghrh_InterviewRecord>()
            .Where(x => x.StaffId == App.User.StaffId && x.CreateTime >= startDate && x.CreateTime < endDate)
            .Select(x => x.OrderId)
            .Distinct()
            .ToListAsync();

        var orders = await Db.Queryable<Ghrh_InterviewOrder>().Where(x => orderIds.Contains(x.Id)).ToListAsync();
        for (int i = 1; i <= 12; i++)
        {
            var date1 = DateTimeHelper.ConvertToMonthString(date);
            XAxis.Add(date1);

            var group = orders
                .Where(x => x.RecommendTime != null && x.RecommendTime.Value.Date.Month == date.Month && x.RecommendTime.Value.Date.Year == date.Year)
                .ToList();

            var count = group.Count;
            var count1 = group.Where(x => x.Status == DIC_INTERVIEW_ORDER_STATUS.Fail).Count();
            var count2 = count - count1;
            data1.Add(count);
            data2.Add(count2);
            decimal count3 = 0;
            if (count2 > 0 && count > 0)
                count3 = ((decimal)count2 / count) * 100;
            data3.Add(StringHelper.TrimDecimal(count3, 2));
            date = date.AddMonths(1);
        }

        var item = new JObject
        {
            new JProperty("color", "rgba(43, 165, 177, 1)"),
            new JProperty("data", data1),
            new JProperty("name", await QueryLangValue("F_ResumeMaintenance_0231", "待筛选人数")),
        };
        series.Add(item);

        item = new JObject
        {
            new JProperty("color", "rgba(255, 152, 18, 1)"),
            new JProperty("data", data2),
            new JProperty("name", await QueryLangValue("F_ResumeMaintenance_0230", "筛选通过人数")),
        };
        series.Add(item);

        item = new JObject
        {
            new JProperty("color", "rgba(255, 147, 0, 1)"),
            new JProperty("data", data3),
            new JProperty("name", await QueryLangValue("F_ResumeMaintenance_0232", "简历筛选通过率")),
        };
        series.Add(item);

        data.XAxis = XAxis;
        data.series = series;
        return ServiceResult<dynamic>.OprateSuccess("获取成功", data);
    }
    #endregion

    #region 面试官/HR推荐 - 面试通过率
    /// <summary>
    /// 面试官/HR推荐 - 面试通过率：面试官面试通过人数 / 已面试人数 × 100%
    /// </summary>
    /// <param name="langId"></param>
    /// <returns></returns>
    public async Task<ServiceResult<dynamic>> QueryInterviewPassRate(int langId = 1, int? staffId = null)
    {

        dynamic data = new ExpandoObject();
        var series = new JArray();
        var XAxis = new List<string>();
        var date = DateTime.Now.AddMonths(-11);
        var data1 = new List<int>();
        var data2 = new List<int>();
        var data3 = new List<decimal>();

        var startDate = DateTime.Now.AddMonths(-11);
        var endDate = DateTime.Now.AddMonths(1);

        startDate = new DateTime(startDate.Year, startDate.Month, 1);
        endDate = new DateTime(endDate.Year, endDate.Month, 1);

        var orderIds = await Db.Queryable<Ghrh_InterviewRecord>()
            .WhereIF(staffId != null, x => x.StaffId == staffId)
            .Where(x => x.CreateTime >= startDate && x.CreateTime < endDate)
            .Select(x => x.OrderId)
            .Distinct()
            .ToListAsync();

        var orders = await Db.Queryable<Ghrh_InterviewOrder>().Where(x =>
        orderIds.Contains(x.Id) &&
        x.Status != DIC_INTERVIEW_ORDER_STATUS.Fail &&
        x.Status != DIC_INTERVIEW_ORDER_STATUS.HasRecommended).ToListAsync();
        for (int i = 1; i <= 12; i++)
        {
            var date1 = DateTimeHelper.ConvertToMonthString(date);
            XAxis.Add(date1);

            var group = orders
                .Where(x => x.RecommendTime != null && x.RecommendTime.Value.Date.Month == date.Month && x.RecommendTime.Value.Date.Year == date.Year)
                .ToList();

            var count = group.Count;
            var count1 = group.Where(x => x.IsPass == true).Count();
            var count2 = count - count1;
            data1.Add(count);
            data2.Add(count2);
            decimal count3 = 0;
            if (count2 > 0 && count > 0)
                count3 = ((decimal)count2 / count) * 100;
            data3.Add(StringHelper.TrimDecimal(count3, 2));
            date = date.AddMonths(1);
        }

        var item = new JObject
        {
            new JProperty("color", "rgba(43, 165, 177, 1)"),
            new JProperty("data", data1),
            new JProperty("name", await QueryLangValue("F_ResumeMaintenance_0234", "已面试人数")),
        };
        series.Add(item);

        item = new JObject
        {
            new JProperty("color", "rgba(255, 152, 18, 1)"),
            new JProperty("data", data2),
            new JProperty("name", await QueryLangValue("F_ResumeMaintenance_0235", "面试通过人数")),
        };
        series.Add(item);

        item = new JObject
        {
            new JProperty("color", "rgba(255, 147, 0, 1)"),
            new JProperty("data", data3),
            new JProperty("name", await QueryLangValue("F_ResumeMaintenance_0233", "面试官面试通过率")),
        };
        series.Add(item);

        data.XAxis = XAxis;
        data.series = series;
        return ServiceResult<dynamic>.OprateSuccess("获取成功", data);
    }
    #endregion 

    #region 面试官/HR推荐 - 发OFFER率
    /// <summary>
    /// 面试官/HR推荐 - 发OFFER率：已发offer人数 / 面试通过人数 × 100％
    /// </summary>
    /// <param name="langId"></param>
    /// <returns></returns>
    public async Task<ServiceResult<dynamic>> QuerySendOfferRate(int langId = 1, int? staffId = null)
    {

        dynamic data = new ExpandoObject();
        var series = new JArray();
        var XAxis = new List<string>();
        var date = DateTime.Now.AddMonths(-11);
        var data1 = new List<int>();
        var data2 = new List<int>();
        var data3 = new List<decimal>();

        var startDate = DateTime.Now.AddMonths(-11);
        var endDate = DateTime.Now.AddMonths(1);

        startDate = new DateTime(startDate.Year, startDate.Month, 1);
        endDate = new DateTime(endDate.Year, endDate.Month, 1);

        var orderIds = await Db.Queryable<Ghrh_InterviewRecord>()
            .WhereIF(staffId != null, x => x.StaffId == staffId)
            .Where(x => x.CreateTime >= startDate && x.CreateTime < endDate)
            .Select(x => x.OrderId)
            .Distinct()
            .ToListAsync();

        var orders = await Db.Queryable<Ghrh_InterviewOrder>().Where(x =>
        orderIds.Contains(x.Id) &&
        x.Status == DIC_INTERVIEW_ORDER_STATUS.HasSendOffer ||
        x.Status == DIC_INTERVIEW_ORDER_STATUS.HasOffer).ToListAsync();
        for (int i = 1; i <= 12; i++)
        {
            var date1 = DateTimeHelper.ConvertToMonthString(date);
            XAxis.Add(date1);

            var group = orders
                .Where(x => x.RecommendTime != null && x.RecommendTime.Value.Date.Month == date.Month && x.RecommendTime.Value.Date.Year == date.Year && x.IsPass == true)
                .ToList();

            var count = group.Count;
            var count1 = group.Where(x => x.IsPass == true).Count();
            var count2 = count - count1;
            data1.Add(count);
            data2.Add(count2);
            decimal count3 = 0;
            if (count2 > 0 && count > 0)
                count3 = ((decimal)count2 / count) * 100;
            data3.Add(StringHelper.TrimDecimal(count3, 2));
            date = date.AddMonths(1);
        }

        var item = new JObject
        {
            new JProperty("color", "rgba(43, 165, 177, 1)"),
            new JProperty("data", data1),
            new JProperty("name", await QueryLangValue("F_ResumeMaintenance_0235", "面试通过人数")),
        };
        series.Add(item);

        item = new JObject
        {
            new JProperty("color", "rgba(255, 152, 18, 1)"),
            new JProperty("data", data2),
            new JProperty("name", await QueryLangValue("F_ResumeMaintenance_0236", "已发offer人数")),
        };
        series.Add(item);

        item = new JObject
        {
            new JProperty("color", "rgba(255, 147, 0, 1)"),
            new JProperty("data", data3),
            new JProperty("name", await QueryLangValue("F_ResumeMaintenance_0237", "已发OFFER率")),
        };
        series.Add(item);

        data.XAxis = XAxis;
        data.series = series;
        return ServiceResult<dynamic>.OprateSuccess("获取成功", data);
    }
    #endregion 
}
