
//--------------------------------------------------------------------
//     此代码由liquid模板自动生成  byzhouhang 20240909
//     生成时间 2025-02-21 07:47:24Z 
//     对此文件的更改可能会导致不正确的行为，并且如果重新生成代码，这些更改将会丢失。
//--------------------------------------------------------------------
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="_sysFileTypeRepository"></param>
/// <param name="_mapper"></param>
/// <param name="_userInfo"></param>
/// <param name="_localizer"></param>
[ApiExplorerSettings(GroupName = "FileRecord")]

public class SysFileTypeService(IRepository<SysFileType> _sysFileTypeRepository, IRepository<TrialFileType> _trialFileTypeRepository,
                                  IMapper _mapper, IUserInfo _userInfo, IStringLocalizer _localizer) : BaseService, ISysFileTypeService
{


    [HttpPost]
    public async Task<PageOutput<SysFileTypeView>> GetSysFileTypeList(SysFileTypeQuery inQuery)
    {

        var sysFileTypeQueryable = _sysFileTypeRepository
            .WhereIf(inQuery.ArchiveTypeEnum != null, t => t.ArchiveTypeEnum == inQuery.ArchiveTypeEnum)
                  .WhereIf(inQuery.SysTemplateTypeEnum != null, t => t.SysTemplateTypeEnum == inQuery.SysTemplateTypeEnum)
            .WhereIf(inQuery.IsConfirmRecord != null, t => t.IsConfirmRecord == inQuery.IsConfirmRecord)
            .WhereIf(inQuery.SubIdentificationEnum != null, t => t.SubIdentificationEnum == inQuery.SubIdentificationEnum)
            .WhereIf(inQuery.IsEnable != null, t => t.IsEnable == inQuery.IsEnable)
            .WhereIf(inQuery.Name.IsNotNullOrEmpty(), t => t.Name.Contains(inQuery.Name))
            .WhereIf(inQuery.NameCN.IsNotNullOrEmpty(), t => t.NameCN.Contains(inQuery.NameCN))
            .ProjectTo<SysFileTypeView>(_mapper.ConfigurationProvider);

        var defalutSortArray = new string[] { nameof(SysFileTypeView.ArchiveTypeEnum), nameof(SysFileTypeView.ShowOrder)};

        var pageList = await sysFileTypeQueryable.ToPagedListAsync(inQuery, defalutSortArray);

        return pageList;
    }



    public async Task<IResponseOutput> AddOrUpdateSysFileType(SysFileTypeAddOrEdit addOrEditSysFileType)
    {
        var verifyExp = new EntityVerifyExp<SysFileType>()
        {
            VerifyExp = u => u.IsEnable == addOrEditSysFileType.IsEnable && (u.Name == addOrEditSysFileType.Name || u.NameCN == addOrEditSysFileType.NameCN),

            // "当前类型启用的文件类型名称重复"
            VerifyMsg = _localizer["SysFileType_NameRepeat"],

            IsVerify = addOrEditSysFileType.IsEnable == false
        };

        var entity = await _sysFileTypeRepository.InsertOrUpdateAsync(addOrEditSysFileType, true, verifyExp);

        //启用的才进行更新
        if (addOrEditSysFileType.Id != null && addOrEditSysFileType.IsEnable)
        {
            await _trialFileTypeRepository.BatchUpdateNoTrackingAsync(t => t.SysFileTypeId == addOrEditSysFileType.Id, u => new TrialFileType() { Name = addOrEditSysFileType.Name, NameCN = addOrEditSysFileType.NameCN,ShowOrder=addOrEditSysFileType.ShowOrder });
        }

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

    }


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

}



