namespace Tiobon.Core.Services;

/// <summary>
/// 考核模板 (服务)
/// </summary>
public class Ghrp_PerformanceTemplateServices : BaseServices<Ghrp_PerformanceTemplate, Ghrp_PerformanceTemplateDto, InsertGhrp_PerformanceTemplateInput, EditGhrp_PerformanceTemplateInput>, IGhrp_PerformanceTemplateServices
{
    private readonly IBaseRepository<Ghrp_PerformanceTemplate> _dal;
    public Ghrp_PerformanceTemplateServices(ICaching caching, IBaseRepository<Ghrp_PerformanceTemplate> dal)
    {
        this._dal = dal;
        base.BaseDal = dal;
        base._caching = caching;
    }

    public override async Task<ServicePageResult<Ghrp_PerformanceTemplateDto>> QueryFilterPage(QueryBody filter, string condition, bool? IsEnable = true)
    {

        var result = await base.QueryFilterPage(filter, condition, IsEnable);

        result.result.DT_TableDataT1.ForEach(async x =>
        {
            x.StageItem = await GetParaLabel("PerformanceStageItem", x.StageItem);
            x.MGShowType = await GetParaLabel("PerformanceMGShowType", x.MGShowType);
            x.TemplateType = await GetParaLabel("PerformanceTemplateType", x.TemplateType);
            //x.MGShowType = await GetParaLabelListString("Performance", "PerformanceMGShowType", x.MGShowType);
            //x.TemplateType = await GetParaLabelListString("Performance", "PerformanceTemplateType", x.TemplateType);
            x.StaffGroupName = Db.Ado.GetString("select StaffGroupName from Ghra_StaffGroup where ID=@StaffGroupID"
                , new { StaffGroupID = x.StaffGroupID });
            x.PeriodTypeName = Db.Ado.GetString("select PeriodTypeName from Ghrp_PerformancePeriodType where ID=@PerformancePeriodTypeID"
                , new { PerformancePeriodTypeID = x.PerformancePeriodTypeID });
        });
        return result;
    }

    public override async Task<long> Add(InsertGhrp_PerformanceTemplateInput entity)
    {
        if (entity.PerformancePeriodTypeID != null)
            entity.PeriodTypeNo = await Db.Queryable<Ghrp_PerformancePeriodType>().Where(a => a.Id == entity.PerformancePeriodTypeID).Select(x => x.PeriodTypeNo).FirstAsync();
        var result = await base.Add(entity);

        return result;
    }

    public override async Task<bool> Update(long Id, EditGhrp_PerformanceTemplateInput editModel)
    {
        if (editModel.PerformancePeriodTypeID != null)
            editModel.PeriodTypeNo = await Db.Queryable<Ghrp_PerformancePeriodType>().Where(a => a.Id == editModel.PerformancePeriodTypeID).Select(x => x.PeriodTypeNo).FirstAsync();

        var result = await base.Update(Id, editModel, null, ["Status"]);
        return result;
    }
}