namespace Tiobon.Core.Services;

/// <summary>
/// 学分规则 (服务)
/// </summary>
public class Ghre_CreditRuleServices : BaseServices<Ghre_CreditRule, Ghre_CreditRuleDto, InsertGhre_CreditRuleInput, EditGhre_CreditRuleInput>, IGhre_CreditRuleServices
{
    private readonly IBaseRepository<Ghre_CreditRule> _dal;
    public Ghre_CreditRuleServices(ICaching caching, IBaseRepository<Ghre_CreditRule> dal)
    {
        this._dal = dal;
        base.BaseDal = dal;
        base._caching = caching;
    }

    public override async Task<ServicePageResult<Ghre_CreditRuleDto>> QueryFilterPage(QueryBody filter, string condition, bool? IsEnable = true)
    {
        var data = await base.QueryFilterPage(filter, condition, IsEnable);
        data.result.DT_TableDataT1.ForEach(x =>
        {
            if (x.RuleType == "StudyCompletedPercent")
                x.RuleType = "学习完成";
            else if (x.RuleType == "ExamPassPercent")
                x.RuleType = "考试合格";
            else if (x.RuleType == "ScoreRanges")
                x.RuleType = "分数段";
        });
        return data;
    }
    public override async Task<ServiceFormResult<Ghre_CreditRuleDto>> QueryForm(QueryForm body)
    {
        var result = await base.QueryForm(body);

        var DT_TableDataT1 = result.result.DT_TableDataT1;
        DT_TableDataT1.ForEach(t =>
        {
            if (!string.IsNullOrWhiteSpace(t.ScoreRange))
                t.ScoreRanges = JsonHelper.JsonToObj<List<CreditRuleScoreRange>>(t.ScoreRange);
            else
                t.ScoreRanges = new List<CreditRuleScoreRange>();
        });

        result.result.DT_TableDataT1 = DT_TableDataT1;
        return result;
    }

    public override async Task<long> Add(InsertGhre_CreditRuleInput entity)
    {
        if (entity.ScoreRanges != null && entity.ScoreRanges.Any())
            entity.ScoreRange = JsonHelper.ObjToJson(entity.ScoreRanges);

        return await base.Add(entity);
    }

    public override async Task<bool> Update(long Id, EditGhre_CreditRuleInput editModel)
    {
        var courseIds = new List<long>();
        if (editModel.ScoreRanges != null && editModel.ScoreRanges.Any())
            editModel.ScoreRange = JsonHelper.ObjToJson(editModel.ScoreRanges);

        return await base.Update(Id, editModel);
    }
}