using System.Data;

namespace Tiobon.Core.Services;

/// <summary>
/// 年度人力申请 (服务)
/// </summary>
public class Ghrh_YearHumanApplyOrderServices : BaseServices<Ghrh_YearHumanApplyOrder, Ghrh_YearHumanApplyOrderDto, InsertGhrh_YearHumanApplyOrderInput, EditGhrh_YearHumanApplyOrderInput>, IGhrh_YearHumanApplyOrderServices
{
    private readonly IBaseRepository<Ghrh_YearHumanApplyOrder> _dal;
    //private readonly Ghrh_YearHumanSettingsServices _ghrh_YearHumanSettingsServices;
    public Ghrh_YearHumanApplyOrderServices(ICaching caching, IBaseRepository<Ghrh_YearHumanApplyOrder> dal)
    {
        this._dal = dal;
        base.BaseDal = dal;
        base._caching = caching;
        //_ghrh_YearHumanSettingsServices = ghrh_YearHumanSettingsServices;
    }

    public async Task<ServiceResult<Ghrh_YearHumanApplyOrderDto>> QueryByWorkId(int objId)
    {
        var entity = await base.QuerySingleDto(x => x.WorkID == objId);
        if (entity != null)
        {
            var items = await Db.Queryable<Ghrh_YearHumanSettings>().Where(x => x.OrderId == entity.Id).ToListAsync();

            entity.Items = Mapper.Map(items).ToANew<List<Ghrh_YearHumanSettingsDto>>();
            if (entity.Items.Any())
            {

                var deptIds = entity.Items.Select(x => x.DeptId).Distinct().ToList();
                var titleIds = entity.Items.Select(x => x.TitleId).Distinct().ToList();
                var jobIds = entity.Items.Select(x => x.JobId).Distinct().ToList();
                var gradeIds = entity.Items.Select(x => x.GradeId).Distinct().ToList();

                if (deptIds.Any())
                {
                    var depts = await Db.Queryable<Ghro_Dept>().Where(x => deptIds.Contains(x.DeptID)).Select(x => new { x.DeptID, x.DeptName }).ToListAsync();
                    entity.Items.ForEach(x => x.DeptName = depts.FirstOrDefault(o => x.DeptId == o.DeptID)?.DeptName);
                }

                if (titleIds.Any())
                {
                    var titles = await Db.Queryable<Ghra_Title>().Where(x => titleIds.Contains(x.TitleID)).ToListAsync();

                    entity.Items.ForEach(rule =>
                    {
                        rule.TitleName = titles.FirstOrDefault(o => rule.TitleId == o.TitleID)?.TitleName;
                    });
                }

                if (gradeIds.Any())
                {
                    var grades = await Db.Queryable<Ghra_Grade>().Where(x => gradeIds.Contains(x.GradeID)).ToListAsync();

                    entity.Items.ForEach(rule =>
                    {
                        rule.GradeName = grades.FirstOrDefault(o => rule.GradeId == o.GradeID)?.GradeName;
                    });
                }

                if (jobIds.Any())
                {
                    var jobs = await Db.Queryable<Ghra_Job>().Where(x => jobIds.Contains(x.JobID)).ToListAsync();

                    entity.Items.ForEach(rule =>
                    {
                        rule.JobName = jobs.FirstOrDefault(o => rule.JobId == o.JobID)?.JobName;
                    });
                }
            }
        }

        return ServiceResult<Ghrh_YearHumanApplyOrderDto>.OprateSuccess("查询成功！", entity);

    }

}