using AutoMapper.Internal.Mappers;
using Microsoft.AspNetCore.Mvc;
using Shentun.Peis.ItemResultTemplates;
using Shentun.Peis.GuidTypes;
using Shentun.Peis.HelperDto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Identity;
using Shentun.Peis.SexHormoneReferenceRanges;
using Shentun.Peis.Models;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp;
using Shentun.Peis.Asbitems;
using Shentun.Peis.Units;
using SqlSugar;
namespace Shentun.Peis.ItemResultTemplates
{
    /// 
    /// 项目结果模板
    /// 
    [ApiExplorerSettings(GroupName = "Work")]
    [Authorize]
    public class ItemResultTemplateAppService : CrudAppService<
            ItemResultTemplate, //The Book entity
            ItemResultTemplateDto, //Used to show books
            Guid, //Primary key of the book entity
            PagedAndSortedResultRequestDto, //Used for paging/sorting
            CreateItemResultTemplateDto,
            UpdateItemResultTemplateDto>
    {
        private readonly IRepository _userRepository;
        private readonly ItemResultTemplateManager _manager;
        private readonly CacheService _cacheService;
        public ItemResultTemplateAppService(
            IRepository repository,
             IRepository userRepository,
            ItemResultTemplateManager manager,
            CacheService cacheService)
            : base(repository)
        {
            this._userRepository = userRepository;
            _manager = manager;
            _cacheService = cacheService;
        }
        /// 
        /// 获取通过主键
        /// 
        /// 
        /// 
        public override async Task GetAsync(Guid id)
        {
            var entity = await Repository.FindAsync(o => o.Id == id);
            //throw new Exception("标准异常测试");
            //throw new BusinessException("业务异常测试");
            //throw new UserFriendlyException("友好异常测试");
            //throw new EntityNotFoundException("未发现实体异常测试");
            //return null;
            if (entity == null)
                return null;
            return ObjectMapper.Map(entity);
        }
        /// 
        /// 获取列表  项目结果模板   
        /// 
        /// 
        /// 
        [RemoteService(false)]
        public override async Task> GetListAsync(PagedAndSortedResultRequestDto input)
        {
            int totalCount = await Repository.CountAsync();
            var entlist = await Repository.GetPagedListAsync(input.SkipCount, input.MaxResultCount, input.Sorting);
            var userList = await _userRepository.GetListAsync();
            var entdto = entlist.Select(s => new ItemResultTemplateDto
            {
                CreationTime = s.CreationTime,
                CreatorId = s.CreatorId,
                ItemId = s.ItemId,
                Id = s.Id,
                LastModificationTime = s.LastModificationTime,
                LastModifierId = s.LastModifierId,
                DiagnosisId = s.DiagnosisId,
                SimpleCode = s.SimpleCode,
                ResultStatusId = s.ResultStatusId,
                Result = s.Result,
                IsResultIntoSummary = s.IsResultIntoSummary,
                DisplayOrder = s.DisplayOrder,
                IsNameIntoSummary = s.IsNameIntoSummary,
                CreatorName = EntityHelper.GetSurnameNoSql(userList, s.CreatorId),
                LastModifierName = EntityHelper.GetSurnameNoSql(userList, s.LastModifierId)
            }).ToList();
            return new PagedResultDto(totalCount, entdto);
        }
        /// 
        /// 获取项目结果模板列表
        /// 
        /// 
        [HttpGet("api/app/ItemResultTemplate/GetItemResultTemplateList")]
        public async Task> GetItemResultTemplateListAsync()
        {
            var entlist = await Repository.GetQueryableAsync();
            var entdto = entlist.Select(s => new ItemResultTemplateDto
            {
                CreationTime = s.CreationTime,
                CreatorId = s.CreatorId,
                ItemId = s.ItemId,
                Id = s.Id,
                LastModificationTime = s.LastModificationTime,
                LastModifierId = s.LastModifierId,
                DiagnosisId = s.DiagnosisId,
                SimpleCode = s.SimpleCode,
                ResultStatusId = s.ResultStatusId,
                Result = s.Result,
                IsResultIntoSummary = s.IsResultIntoSummary,
                DisplayOrder = s.DisplayOrder,
                IsNameIntoSummary = s.IsNameIntoSummary,
                CreatorName = _cacheService.GetSurnameAsync(s.CreatorId).Result,
                LastModifierName = _cacheService.GetSurnameAsync(s.LastModifierId).Result
            }).ToList();
            return entdto;
        }
        /// 
        /// 获取项目结果模板列表  根据项目ID
        /// 
        /// 项目ID
        /// 
        public async Task> GetListInItemIdAsync(Guid ItemId)
        {
            var entlist = await Repository.GetListAsync(m => m.ItemId == ItemId);
            var entdto = entlist.Select(s => new ItemResultTemplateDto
            {
                CreationTime = s.CreationTime,
                CreatorId = s.CreatorId,
                ItemId = s.ItemId,
                Id = s.Id,
                LastModificationTime = s.LastModificationTime,
                LastModifierId = s.LastModifierId,
                DiagnosisId = s.DiagnosisId,
                SimpleCode = s.SimpleCode,
                ResultStatusId = s.ResultStatusId,
                Result = s.Result,
                IsResultIntoSummary = s.IsResultIntoSummary,
                DisplayOrder = s.DisplayOrder,
                IsNameIntoSummary = s.IsNameIntoSummary,
                CreatorName = _cacheService.GetSurnameAsync(s.CreatorId).Result,
                LastModifierName = _cacheService.GetSurnameAsync(s.LastModifierId).Result
            }).ToList();
            return entdto;
        }
        /// 
        /// 创建
        /// 
        /// 
        /// 
        public override async Task CreateAsync(CreateItemResultTemplateDto input)
        {
            var createEntity = ObjectMapper.Map(input);
            var entity = await _manager.CreateAsync(createEntity);
            entity = await Repository.InsertAsync(entity);
            var dto = ObjectMapper.Map(entity);
            return dto;
        }
        /// 
        /// 更新
        /// 
        /// 
        /// 
        /// 
        public override async Task UpdateAsync(Guid id, UpdateItemResultTemplateDto input)
        {
            var entity = await Repository.GetAsync(id);
            var sourceEntity = ObjectMapper.Map(input);
            _manager.UpdateAsync(sourceEntity, entity);
            entity = await Repository.UpdateAsync(entity);
            return ObjectMapper.Map(entity);
        }
        /// 
        /// 删除
        /// 
        /// 
        /// 
        public override Task DeleteAsync(Guid id)
        {
            return base.DeleteAsync(id);
        }
        /// 
        /// 修改排序  置顶,置底
        /// 
        /// 需要修改的ID
        /// 修改方式:1 置顶   2 置底
        /// 
        [HttpPut("api/app/itemresulttemplate/updatemanysort")]
        public async Task UpdateManySortAsync(Guid id, int SortType)
        {
            await _manager.UpdateManySortAsync(id, SortType);
        }
        /// 
        ///  修改排序  拖拽
        /// 
        /// 
        /// 
        [HttpPut("api/app/itemresulttemplate/updatesortmany")]
        public async Task UpdateSortManyAsync(UpdateSortManyDto input)
        {
            await _manager.UpdateSortManyAsync(input);
        }
    }
}