using System;
using System.Collections.Generic;
using System.Linq;
using XYY.Model.Standard.Finance;
using XYY.Data.Standard.Finance;
using XYY.Common.Standard;
using System.Threading.Tasks;

namespace XYY.Service.Standard.Finance
{
    public interface IFirstTemplateService
    {
        Task AddTemplate(FinanceFirstTemplateDto dto);
        Task<List<FinanceFirstTemplateDto>> GetFinanceFirstTemplateDtos();
        Task<List<Finance_FirstTemplateDetail>> GetTemplateDto(int templateId);
        Task<PageResult<Finance_FirstTemplate>> QueryPager(QueryModel queryModel);
        Task RemoreTemplate(FinanceFirstTemplateDto dto);
        Task UpdateTemplate(FinanceFirstTemplateDto dto);
    }

    public class FirstTemplateService : IFirstTemplateService
    {
        private IFinance_FirstTemplateDetailRepository _detailRepository;
        private IFinance_FirstTemplateRepository _templateRepository;

        public FirstTemplateService(
            IFinance_FirstTemplateDetailRepository detailRepository,
            IFinance_FirstTemplateRepository templateRepository
            )
        {
            _detailRepository = detailRepository;
            _templateRepository = templateRepository;
        }

        public async Task AddTemplate(FinanceFirstTemplateDto dto)
        {
            int id = (int)(await _templateRepository.InsertAsync(dto));
            dto.Details.ForEach(x => { x.CreateTime = DateTime.Now; x.TemplateId = id; });
            await _detailRepository.BulkToDBAsync(dto.Details);
        }

        public async Task UpdateTemplate(FinanceFirstTemplateDto dto)
        {
            await _templateRepository.UpdateAsync(dto);
            await _detailRepository.UpdateByConditionAsync(x => x.TemplateId == dto.Id, new { IsDeleted = true });
            dto.Details.ForEach(x => { x.CreateTime = DateTime.Now; x.TemplateId = dto.Id; });
            await _detailRepository.BulkToDBAsync(dto.Details);
        }

        public async Task<PageResult<Finance_FirstTemplate>> QueryPager(QueryModel queryModel)
        {
            queryModel.QueryParamer.Add(new QueryParamer { Filed = "IsDeleted", Value = "False", Method = "Eq" });
            return await _templateRepository.QueryPagerAsync(queryModel);
        }

        public async Task<List<FinanceFirstTemplateDto>> GetFinanceFirstTemplateDtos()
        {
            var dtos = (await _templateRepository.QueryAsync(x => x.IsDeleted == false)).ToList().CopyList<FinanceFirstTemplateDto>().ToList();
            dtos.ForEach(async x => x.Details = await GetTemplateDto(x.Id));
            return dtos;
        }

        public async Task<List<Finance_FirstTemplateDetail>> GetTemplateDto(int templateId)
        {
            return (await _detailRepository.QueryAsync(x => x.TemplateId == templateId && !x.IsDeleted)).ToList();
        }

        public async Task RemoreTemplate(FinanceFirstTemplateDto dto)
        {
            await _templateRepository.UpdateByConditionAsync(x => x.Id == dto.Id, new { IsDeleted = true });
        }
    }
}
