
//--------------------------------------------------------------------
//     此代码由liquid模板自动生成  byzhouhang 20240909
//     生成时间 2025-02-21 07:47:30Z 
//     对此文件的更改可能会导致不正确的行为，并且如果重新生成代码，这些更改将会丢失。
//--------------------------------------------------------------------
using IRaCIS.Core.Domain.Models;
using Microsoft.AspNetCore.Mvc;
using IRaCIS.Core.Application.Interfaces;
using IRaCIS.Core.Application.ViewModel;
using IRaCIS.Core.Infrastructure.Extention;
using System.Threading.Tasks;
using IRaCIS.Core.Infra.EFCore;
namespace IRaCIS.Core.Application.Service;

/// <summary>
/// 项目-一般文件记录
/// </summary>
/// <param name="_trialNormalRecordRepository"></param>
/// <param name="_mapper"></param>
/// <param name="_userInfo"></param>
/// <param name="_localizer"></param>
[ApiExplorerSettings(GroupName = "FileRecord")]
public class TrialNormalRecordService(IRepository<TrialNormalRecord> _trialNormalRecordRepository,
       IRepository<TrialFile> _trialFileRepository,
                                  IMapper _mapper, IUserInfo _userInfo, IStringLocalizer _localizer) : BaseService, ITrialNormalRecordService
{


    [HttpPost]
    public async Task<PageOutput<TrialNormalRecordView>> GetTrialNormalRecordList(TrialNormalRecordQuery inDto)
    {

        var trialNormalRecordQueryable = _trialNormalRecordRepository
                .Where(x => x.TrialFileTypeId == inDto.TrialFileTypeId)
                     .WhereIf(inDto.IsAuthorizedView != null, x => x.IsAuthorizedView == inDto.IsAuthorizedView)
                 .WhereIf(inDto.FileName.IsNotNullOrEmpty(), x => x.TrialFileRecord.FileName.Contains(inDto.FileName))
                .ProjectTo<TrialNormalRecordView>(_mapper.ConfigurationProvider);

        var pageList = await trialNormalRecordQueryable.ToPagedListAsync(inDto);

        return pageList;
    }



    public async Task<IResponseOutput> AddOrUpdateTrialNormalRecord(TrialNormalRecordAddOrEdit inDto)
    {
        if (inDto.TrialFileRecord != null) inDto.TrialFileRecord.TrialFileTypeId = inDto.TrialFileTypeId;
        if (inDto.HistoryFileRecord != null) inDto.HistoryFileRecord.TrialFileTypeId = inDto.TrialFileTypeId;


        if (inDto.Id != null)
        {
            var trialFinalRecord = await _trialNormalRecordRepository.Where(x => x.Id == inDto.Id.Value).FirstNotNullAsync();

            List<Guid?> ids = new List<Guid?>() {
                trialFinalRecord.TrialFileRecordId,
                trialFinalRecord.HistoryFileRecordId,
            };

            var fileIds = ids.Where(x => x != null).Select(x => (Guid)x).ToList();
            await _trialFileRepository.BatchDeleteNoTrackingAsync(x => fileIds.Contains(x.Id));
        }

        var entity = await _trialNormalRecordRepository.InsertOrUpdateAsync(inDto, true);


        return ResponseOutput.Ok(entity.Id.ToString());

    }

    /// <summary>
    /// 批量新增一般文件记录
    /// </summary>
    /// <param name="inDto"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<IResponseOutput> BatchAddTrialNormalRecord(BatchAddTrialNormalRecordInDto inDto)
    {
        foreach (var item in inDto.TrialNormalRecordList)
        {
            if (item.TrialFileRecord != null) item.TrialFileRecord.TrialFileTypeId = item.TrialFileTypeId;
            if (item.HistoryFileRecord != null) item.HistoryFileRecord.TrialFileTypeId = item.TrialFileTypeId;

            var entity = await _trialNormalRecordRepository.InsertFromDTOAsync(item);
        }

         var result=  await _trialNormalRecordRepository.SaveChangesAsync();
        return ResponseOutput.Ok(result);
    }


    /// <summary>
    /// 授权文档
    /// </summary>
    /// <param name="inDto"></param>
    /// <returns></returns>
    public async Task<IResponseOutput> AuthorizedTTrialNormalRecord(AuthorizedTrialFinalRecordInDto inDto)
    {
        await _trialNormalRecordRepository.UpdatePartialFromQueryAsync(t => inDto.Ids.Contains(t.Id), t => new TrialNormalRecord() { IsAuthorizedView = inDto.IsAuthorizedView });
        await _trialNormalRecordRepository.SaveChangesAsync();
        return ResponseOutput.Ok();
    }



    [HttpDelete("{trialNormalRecordId:guid}")]
    public async Task<IResponseOutput> DeleteTrialNormalRecord(Guid trialNormalRecordId)
    {
        var success = await _trialNormalRecordRepository.DeleteFromQueryAsync(t => t.Id == trialNormalRecordId, true);
        return ResponseOutput.Ok();
    }

    /// <summary>
    /// 批量删除一般文件记录
    /// </summary>
    /// <param name="inDto"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<IResponseOutput> DeleteTrialNormalRecordList(DeleteTrialNormalRecordListInDto inDto)
    {
        var success = await _trialNormalRecordRepository.DeleteFromQueryAsync(t => inDto.Ids.Contains(t.Id), true);
        return ResponseOutput.Ok();
    }

}



