namespace Tiobon.Core.Services;

/// <summary>
/// 多岗维护 (服务)
/// </summary>
public class Ghre_MultipleTitleServices : BaseServices<Ghre_MultipleTitle, Ghre_MultipleTitleDto, InsertGhre_MultipleTitleInput, EditGhre_MultipleTitleInput>, IGhre_MultipleTitleServices
{
    private readonly IBaseRepository<Ghre_MultipleTitle> _dal;
    public Ghre_MultipleTitleServices(ICaching caching, IBaseRepository<Ghre_MultipleTitle> dal)
    {
        this._dal = dal;
        base.BaseDal = dal;
        base._caching = caching;
    }

    #region 查询
    public override async Task<ServicePageResult<Ghre_MultipleTitleDto>> QueryFilterPage(QueryBody filter, string condition, bool? IsEnable = true)
    {
        var result = await base.QueryFilterPage(filter, condition, IsEnable);
        var list = result.result.DT_TableDataT1;

        var staffIds = list.Where(x => x.StaffId != null).Select(x => x.StaffId).ToList();
        var titleId = list.Where(x => x.TitleId1 != null).Select(x => x.TitleId1).ToList();

        titleId.AddRange(list.Where(x => x.TitleId2 != null).Select(x => x.TitleId2));
        titleId.AddRange(list.Where(x => x.TitleId3 != null).Select(x => x.TitleId3));
        titleId.AddRange(list.Where(x => x.TitleId4 != null).Select(x => x.TitleId4));
        titleId.AddRange(list.Where(x => x.TitleId5 != null).Select(x => x.TitleId5));
        titleId = titleId.Distinct().ToList();

        var staffs = await Db.Queryable<Ghra_Staff>().Where(x => staffIds.Contains(x.StaffID)).ToListAsync();
        var titles = await Db.Queryable<Ghra_Title>().Where(x => titleId.Contains(x.TitleID)).ToListAsync();

        list.ForEach(x =>
        {
            x.StaffName = staffs.Where(o => o.StaffID == x.StaffId).FirstOrDefault()?.StaffName;
            x.EffectiveDate1 = DateTimeHelper.ConvertToDayString(x.EffectiveDate);
            if (x.TitleId1 != null)
                x.TitleName1 = titles.Where(o => o.TitleID == x.TitleId1).FirstOrDefault()?.TitleName;
            if (x.TitleId2 != null)
                x.TitleName2 = titles.Where(o => o.TitleID == x.TitleId2).FirstOrDefault()?.TitleName;
            if (x.TitleId3 != null)
                x.TitleName3 = titles.Where(o => o.TitleID == x.TitleId3).FirstOrDefault()?.TitleName;
            if (x.TitleId4 != null)
                x.TitleName4 = titles.Where(o => o.TitleID == x.TitleId4).FirstOrDefault()?.TitleName;
            if (x.TitleId5 != null)
                x.TitleName5 = titles.Where(o => o.TitleID == x.TitleId5).FirstOrDefault()?.TitleName;
        });

        return result;

    }
    #endregion

    public override async Task<long> Add(InsertGhre_MultipleTitleInput entity)
    {

        await Db.Updateable<Ghre_MultipleTitle>()
           .SetColumns(it => new Ghre_MultipleTitle() { IsEnable = 0 })
           .Where(it => it.StaffId == entity.StaffId)
           .ExecuteCommandAsync();

        entity.TitleId1 = entity.TitleIds.Count > 0 ? entity.TitleIds[0] : null;
        entity.TitleId2 = entity.TitleIds.Count > 1 ? entity.TitleIds[1] : null;
        entity.TitleId3 = entity.TitleIds.Count > 2 ? entity.TitleIds[2] : null;
        entity.TitleId4 = entity.TitleIds.Count > 3 ? entity.TitleIds[3] : null;
        entity.TitleId5 = entity.TitleIds.Count > 4 ? entity.TitleIds[4] : null;

        var result = await base.Add(entity);

        return result;
    }

    public override async Task<bool> Update(long Id, EditGhre_MultipleTitleInput entity)
    {

        entity.TitleId1 = entity.TitleIds.Count > 0 ? entity.TitleIds[0] : null;
        entity.TitleId2 = entity.TitleIds.Count > 1 ? entity.TitleIds[1] : null;
        entity.TitleId3 = entity.TitleIds.Count > 2 ? entity.TitleIds[2] : null;
        entity.TitleId4 = entity.TitleIds.Count > 3 ? entity.TitleIds[3] : null;
        entity.TitleId5 = entity.TitleIds.Count > 4 ? entity.TitleIds[4] : null;
        var result = await base.Update(Id, entity);
        return result;
    }

    public override async Task<ServiceFormResult<Ghre_MultipleTitleDto>> QueryForm(QueryForm body)
    {
        var result = await base.QueryForm(body);

        result.result.DT_TableDataT1[0].TitleIds = new List<int>();
        if (result.result.DT_TableDataT1[0].TitleId1 != null)
            result.result.DT_TableDataT1[0].TitleIds.Add(result.result.DT_TableDataT1[0].TitleId1.Value);

        if (result.result.DT_TableDataT1[0].TitleId2 != null)
            result.result.DT_TableDataT1[0].TitleIds.Add(result.result.DT_TableDataT1[0].TitleId2.Value);

        if (result.result.DT_TableDataT1[0].TitleId3 != null)
            result.result.DT_TableDataT1[0].TitleIds.Add(result.result.DT_TableDataT1[0].TitleId3.Value);

        if (result.result.DT_TableDataT1[0].TitleId4 != null)
            result.result.DT_TableDataT1[0].TitleIds.Add(result.result.DT_TableDataT1[0].TitleId4.Value);

        if (result.result.DT_TableDataT1[0].TitleId5 != null)
            result.result.DT_TableDataT1[0].TitleIds.Add(result.result.DT_TableDataT1[0].TitleId5.Value);
        return result;

    }
}