namespace Tiobon.Core.Services;

/// <summary>
/// 招聘参数配置组 (服务)
/// </summary>
public class Ghrh_ConfigGroupServices : BaseServices<Ghrh_ConfigGroup, Ghrh_ConfigGroupDto, InsertGhrh_ConfigGroupInput, EditGhrh_ConfigGroupInput>, IGhrh_ConfigGroupServices
{
    private readonly IBaseRepository<Ghrh_ConfigGroup> _dal;
    public Ghrh_ConfigGroupServices(ICaching caching, IBaseRepository<Ghrh_ConfigGroup> dal)
    {
        this._dal = dal;
        base.BaseDal = dal;
        base._caching = caching;
    }


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

        var configs = await Db.Queryable<Ghrh_Config>().OrderBy(x => x.Sequence).ToListAsync();
        result.result.DT_TableDataT1.ForEach(x =>
        {
            x.Configs = configs.Where(o => o.GroupId == x.Id).ToList();
        });
        return result;
    }
    public async Task<ServiceResult> BulkUpdateValue(List<Ghrh_ConfigGroupDto> entitys)
    {
        for (int i = 0; i < entitys.Count; i++)
        {
            for (int j = 0; j < entitys[i].Configs.Count; j++)
            {
                await Db.Updateable<Ghrh_Config>()
                    .SetColumns(it => new Ghrh_Config()
                    {
                        ConfigValue = entitys[i].Configs[j].ConfigValue
                    }, true)
                   .Where(x => x.Id == entitys[i].Configs[j].Id)
                   .ExecuteCommandAsync();
            }
        }
        return ServiceResult.OprateSuccess();
    }
}