namespace Tiobon.Core.Services;

/// <summary>
/// Ghra_Staff (服务)
/// </summary>
public class Ghra_StaffServices : BaseServices<Ghra_Staff, Ghra_StaffDto, InsertGhra_StaffInput, EditGhra_StaffInput>, IGhra_StaffServices
{
    private readonly IBaseRepository<Ghra_Staff> _dal;
    private IGhra_StaffLicenceServices _ghra_StaffLicenceServices;
    public Ghra_StaffServices(ICaching caching,
    IGhra_StaffLicenceServices ghra_StaffLicenceServices,
         IBaseRepository<Ghra_Staff> dal)
    {
        this._dal = dal;
        base.BaseDal = dal;
        base._caching = caching;
        _ghra_StaffLicenceServices = ghra_StaffLicenceServices;
    }

    public override async Task<ServicePageResult<Ghra_StaffDto>> QueryFilterPage(QueryBody filter, string condition, bool? IsEnable = true)
    {
        var result = await base.QueryFilterPage(filter, condition, IsEnable);

        var deptIds = result.result.DT_TableDataT1.Where(x => x.DeptID != null).Select(x => x.DeptID.Value).ToList();
        var depts = await Db.Queryable<Ghro_Dept>().Where(x => x.DeptID != null && deptIds.Contains(x.DeptID.Value)).ToListAsync();

        var titleIds = result.result.DT_TableDataT1.Where(x => x.TitleID != null).Select(x => x.TitleID.Value).ToList();
        var titles = await Db.Queryable<Ghra_Title>().Where(x => x.TitleID != null && titleIds.Contains(x.TitleID.Value)).ToListAsync();

        for (int i = 0; i < result.result.DT_TableDataT1.Count; i++)
        {
            result.result.DT_TableDataT1[i].GenderLabel = await GetParaLabel("Gender", result.result.DT_TableDataT1[i].Gender);
            result.result.DT_TableDataT1[i].IndateLabel = DateTimeHelper.ConvertToDayString(result.result.DT_TableDataT1[i].Indate);

            result.result.DT_TableDataT1[i].StaffType1Label = await GetParaLabel(result.result.DT_TableDataT1[i].StaffType1);
            result.result.DT_TableDataT1[i].StaffType2Label = await GetParaLabel(result.result.DT_TableDataT1[i].StaffType2);

            if (result.result.DT_TableDataT1[i].DeptID != null)
                result.result.DT_TableDataT1[i].DeptName = depts.Where(o => o.DeptID == result.result.DT_TableDataT1[i].DeptID).FirstOrDefault()?.DeptName;

            if (result.result.DT_TableDataT1[i].TitleID != null)
                result.result.DT_TableDataT1[i].TitleName = titles.Where(o => o.TitleID == result.result.DT_TableDataT1[i].TitleID).FirstOrDefault()?.TitleName;
        }
        return result;
    }

    public async Task<ServiceResult<Ghra_StaffDto>> QuerySimple(int id)
    {
        var entity = await Db.Ado.SqlQuerySingleAsync<Ghra_StaffDto>($"SELECT * FROM Ghra_Staff WHERE StaffID='{id}' ");


        if (entity != null)
        {
            entity.TitleId = entity.TitleID;
            entity.DeptId = entity.DeptID;

            if (entity.DeptId != null)
            {
                var dept = await Db.Queryable<Ghro_Dept>().Where(x => x.DeptID == entity.DeptId).FirstAsync();

                if (dept != null)
                    entity.DeptName = dept.DeptName;

                dept = await Db.Queryable<Ghro_Dept>().Where(x => x.DeptID == dept.ParentDeptID).FirstAsync();
                if (dept != null)
                    entity.ParentDeptName = dept.DeptName;
            }

            var licences = await Db.Ado.SqlQueryAsync<Ghra_StaffLicenceDto>($"SELECT * FROM Ghra_StaffLicence WHERE StaffID='{id}'  AND IsEnable=1");
            //var licences = await _ghra_StaffLicenceServices.QueryDto(x => x.StaffID == id);

            licences.ForEach(x =>
            {
                if (!string.IsNullOrWhiteSpace(x.AttachmentIDs))
                    x.Attachments = JsonHelper.JsonToObj<List<Ghrs_AttachmentDto>>(x.AttachmentIDs);
            });
            entity.Licences = licences;
        }
        return ServiceResult<Ghra_StaffDto>.OprateSuccess("查询成功！", entity);
    }

    public async Task<ServiceResult<string>> QueryEmail(int id)
    {
        string email = string.Empty;
        var entity = await Db.Ado.SqlQuerySingleAsync<Ghra_StaffDto>($"SELECT * FROM Ghra_Staff WHERE StaffID='{id}' ");


        if (entity != null)
            email = entity.Email;
        return ServiceResult<string>.OprateSuccess("查询成功！", email);
    }
}