
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 Tiobon.Core.Common.Helper;
using MathNet.Numerics.Distributions;
using NPOI.Util.Collections;

namespace Tiobon.Core.Services;

/// <summary>
/// 课程场景 (服务)
/// </summary>
public class Ghre_CourseSceneServices : BaseServices<Ghre_CourseScene, Ghre_CourseSceneDto, InsertGhre_CourseSceneInput, EditGhre_CourseSceneInput>, IGhre_CourseSceneServices
{
    private readonly IBaseRepository<Ghre_CourseScene> _dal;
    public Ghre_CourseSceneServices(ICaching caching, IBaseRepository<Ghre_CourseScene> dal)
    {
        this._dal = dal;
        base.BaseDal = dal;
        base._caching = caching;
    }

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

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

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

    public override async Task<long> Add(InsertGhre_CourseSceneInput entity)
    {
        if (entity.CourseIds != null && entity.CourseIds.Any())
        {

            entity.CourseId = JsonHelper.ObjToJson(entity.CourseIds);

            var list = Db.Queryable<Ghre_Course>().Where(x => entity.CourseIds.Contains(x.Id.ToString())).Select(x => x.CourseName).ToList();

            entity.CourseName = string.Join("、", list.Select(x => x).ToArray());
        }
        return await base.Add(entity);
    }

    public override async Task<bool> Update(long Id, EditGhre_CourseSceneInput editModel)
    {

        if (editModel.CourseIds != null && editModel.CourseIds.Any())
        {

            editModel.CourseId = JsonHelper.ObjToJson(editModel.CourseIds);

            var list = Db.Queryable<Ghre_Course>().Where(x => editModel.CourseIds.Contains(x.Id.ToString())).Select(x => x.CourseName).ToList();

            editModel.CourseName = string.Join("、", list.Select(x => x).ToArray());
        }
        return await base.Update(Id, editModel);
    }
}