
using System.Data;
using System.Net.Mail;
using Newtonsoft.Json;
using Tiobon.Core.Common;
using Tiobon.Core.Common.Caches;
using Tiobon.Core.IRepository.Base;
using Tiobon.Core.IServices;
using Tiobon.Core.Model;
using Tiobon.Core.Model.Models;
using Tiobon.Core.Services.BASE;

namespace Tiobon.Core.Services
{
    /// <summary>
    /// 课件 (服务)
    /// </summary>
    public class Ghre_CourseWareServices : BaseServices<Ghre_CourseWare, Ghre_CourseWareDto, InsertGhre_CourseWareInput, EditGhre_CourseWareInput>, IGhre_CourseWareServices
    {
        private readonly IBaseRepository<Ghre_CourseWare> _dal;
        private IGhre_CourseServices _ghre_CourseServices;
        private IGhre_CourseWareAttachmentServices _ghre_CourseWareAttachmentServices;

        public Ghre_CourseWareServices(ICaching caching,
            IGhre_CourseServices ghre_CourseServices,
            IGhre_CourseWareAttachmentServices ghre_CourseWareAttachmentServices,
            IBaseRepository<Ghre_CourseWare> dal)
        {
            this._dal = dal;
            base.BaseDal = dal;
            base._caching = caching;
            _ghre_CourseServices = ghre_CourseServices;
            _ghre_CourseWareAttachmentServices = ghre_CourseWareAttachmentServices;
        }

        public override async Task<long> Add(InsertGhre_CourseWareInput entity)
        {
            if (entity.CourseIds2.Any())
            {
                entity.CourseIds = JsonConvert.SerializeObject(entity.CourseIds2);
                var courses = await _ghre_CourseServices.Query(x => entity.CourseIds2.Contains(x.Id));
                entity.CourseNames = string.Join(",", courses.Select(x => x.CourseName));
            }
            var result = await base.Add(entity);
            entity.Attachments.ForEach(x => x.CourseWareId = result);
            await _ghre_CourseWareAttachmentServices.Add(entity.Attachments);
            return result;
        }

        public override async Task<bool> Update(long Id, EditGhre_CourseWareInput editModel)
        {
            if (editModel.CourseIds2.Any())
            {
                editModel.CourseIds = JsonConvert.SerializeObject(editModel.CourseIds2);
                var courses = await _ghre_CourseServices.Query(x => editModel.CourseIds2.Contains(x.Id));
                editModel.CourseNames = string.Join(",", courses.Select(x => x.CourseName));
            }

            await _ghre_CourseWareAttachmentServices.Delete(x => x.CourseWareId == Id);
            editModel.Attachments.ForEach(x => x.CourseWareId = Id);
            await _ghre_CourseWareAttachmentServices.Add(editModel.Attachments);

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

        public override async Task<ServiceFormResult> QueryForm(QueryForm body)
        {
            var result = await base.QueryForm(body);
            string courseIds = result.result.DT_TableDataT1[0].CourseIds;
            if (!string.IsNullOrWhiteSpace(courseIds))
                result.result.DT_TableDataT1[0].CourseIds2 = JsonConvert.DeserializeObject<List<long>>(courseIds);

            else result.result.DT_TableDataT1[0].CourseIds2 = new List<long>();


            result.result.DT_TableDataT1[0].Attachments = await _ghre_CourseWareAttachmentServices.Query(x => x.CourseWareId == body.id);
            return result;

        }

        public override async Task<ServicePageResult<Ghre_CourseWareDto>> QueryFilterPage(QueryBody body)
        {
            var result = await base.QueryFilterPage(body);
            result.result.DT_TableDataT1.ForEach(x =>
            {
                if (!string.IsNullOrWhiteSpace(x.CourseIds))
                    x.CourseIds2 = JsonConvert.DeserializeObject<List<long>>(x.CourseIds);
            });

            return result;

        }
    }
}