
using Tiobon.Core.IServices;
using Tiobon.Core.Model.Models;
using Tiobon.Core.Services.BASE;
using Tiobon.Core.IRepository.Base;
using Tiobon.Core.Common.Caches;
using Tiobon.Core.Common;
using Tiobon.Core.Model;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace Tiobon.Core.Services;

/// <summary>
/// 培训证书规则 (服务)
/// </summary>
public class Ghre_CertificateRuleServices : BaseServices<Ghre_CertificateRule, Ghre_CertificateRuleDto, InsertGhre_CertificateRuleInput, EditGhre_CertificateRuleInput>, IGhre_CertificateRuleServices
{
    private readonly IBaseRepository<Ghre_CertificateRule> _dal;
    private readonly IGhre_CourseServices _ghre_CourseServices;
    private readonly IGhre_CourseSceneServices _ghre_CourseSceneServices;
    public Ghre_CertificateRuleServices(ICaching caching,
        IGhre_CourseServices ghre_CourseServices,
        IGhre_CourseSceneServices ghre_CourseSceneServices,
        IBaseRepository<Ghre_CertificateRule> dal)
    {
        this._dal = dal;
        base.BaseDal = dal;
        base._caching = caching;
        _ghre_CourseServices = ghre_CourseServices;
        _ghre_CourseSceneServices = ghre_CourseSceneServices;
    }

    public override async Task<ServicePageResult<Ghre_CertificateRuleDto>> QueryFilterPage(QueryBody filter, string condition, bool? IsEnable = true)
    {
        if (filter.jsonParam != null)
            foreach (JProperty jProperty in filter.jsonParam.Properties())
            {
                var name = jProperty.Name;
                var value = jProperty.Value.ToString();
                if (!string.IsNullOrWhiteSpace(value) && name == "CourseName")
                {
                    var jsonParam = JsonConvert.DeserializeObject<JsonParam>(value);

                    switch (jsonParam.operationKey)
                    {
                        case "Equal":
                            condition = $" ( CourseSceneId='{jsonParam.columnValue}' or CourseId='{jsonParam.columnValue}' )";
                            break;
                        case "NotEqual":
                            condition = $"( CourseSceneId!='{{jsonParam.columnValue}}' AND CourseId!='{{jsonParam.columnValue}}' )\";";
                            break;
                        default:
                            break;
                    }
                }
            }
        var data = await base.QueryFilterPage(filter, condition, IsEnable);
        var courseIds = data.result.DT_TableDataT1.Where(x => x.CourseSceneId != null || x.CourseId != null).Select(x => x.CourseId ?? x.CourseSceneId).Distinct().ToList();

        var courses = await _ghre_CourseServices.Query(x => courseIds.Contains(x.Id));
        var courseScenes = await _ghre_CourseSceneServices.Query(x => courseIds.Contains(x.Id));

        data.result.DT_TableDataT1.ForEach(async x =>
        {
            x.RuleTypeLabel = await GetParaLabel("TrainingCertificateRuleType", x.RuleType);
            x.SendRuleLabel = await GetParaLabel("TrainingCertificateSendRule", x.SendRule);

            string staticType = x.StaticType == "month" ? "月" : (x.StaticType == "year" ? "年" : "天");
            if (x.ValidityType == "StaticDuration")
                x.ValidityLabel = $"自颁发日起 【{x.StaticNum}】{staticType}内有效";
            else
            if (x.ValidityType == "Unlimited")
                x.ValidityLabel = $"自颁发日起终生有效";
            else
            if (x.ValidityType == "Option")
                x.ValidityLabel = $"有效期至【{x.ValidityTime.Value.ToString("yyyy-MM-dd")}】";
            if (x.CourseSceneId != null)
                x.SceneOrCourseName = courseScenes.FirstOrDefault(o => o.Id == x.CourseSceneId)?.SceneName;

            if (x.CourseId != null)
                x.SceneOrCourseName = courses.FirstOrDefault(o => o.Id == x.CourseId)?.CourseName;
        });
        return data;

    }
}