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

/// <summary>
/// 用户协议和隐私采集服务
/// </summary>
/// <param name="_userAgreementRepository"></param>
/// <param name="_mapper"></param>
/// <param name="_userInfo"></param>
/// <param name="_localizer"></param>
[ApiExplorerSettings(GroupName = "FileRecord")]
public class UserAgreementService(IRepository<UserAgreement> _userAgreementRepository,
                                  IMapper _mapper, IUserInfo _userInfo, IStringLocalizer _localizer): BaseService, IUserAgreementService
{

    /// <summary>
    /// 获取用户协议和隐私采集列表
    /// </summary>
    /// <param name="inQuery"></param>
    /// <returns></returns>
    [HttpPost]
	public async Task<PageOutput<UserAgreementView>>  GetUserAgreementList(UserAgreementQuery inQuery)
	{
		    var userAgreementQueryable =_userAgreementRepository
             .WhereIf(inQuery.StartEffectiveDate.HasValue, t => t.EffectiveDate >= inQuery.StartEffectiveDate.Value)
             .WhereIf(inQuery.EndEffectiveDate.HasValue, t => t.EffectiveDate <= inQuery.EndEffectiveDate.Value)
             .WhereIf(inQuery.FileContent != null, t => t.FileContent.Contains(inQuery.FileContent))
             .WhereIf(inQuery.FileName != null, t => t.FileName.Contains(inQuery.FileName))
             .WhereIf(inQuery.FileVersion != null, t => t.FileVersion.Contains(inQuery.FileVersion))
             .WhereIf(inQuery.IsCurrentVersion != null, t => t.IsCurrentVersion==inQuery.IsCurrentVersion)
             .WhereIf(inQuery.UserAgreementTypeEnum != null, t => t.UserAgreementTypeEnum == inQuery.UserAgreementTypeEnum)
             .WhereIf(inQuery.StartUpdateDate.HasValue, t => t.UpdateDate >= inQuery.StartUpdateDate.Value)
             .WhereIf(inQuery.EndUpdateDate.HasValue, t => t.UpdateDate <= inQuery.EndUpdateDate.Value)
            .ProjectTo<UserAgreementView>(_mapper.ConfigurationProvider);
            var pageList= await userAgreementQueryable.ToPagedListAsync(inQuery);

        return pageList;
	}

    /// <summary>
    /// 获取当前版本的用户协议和隐私采集
    /// </summary>
    /// <returns></returns>
    [AllowAnonymous]
    [HttpPost]
    public async Task<List<UserAgreementView>> GetCurrentVersionUserAgreements()
    {
        var userAgreementList = await _userAgreementRepository.Where(x=>x.IsCurrentVersion)
            .ProjectTo<UserAgreementView>(_mapper.ConfigurationProvider)
            .ToListAsync();
        return userAgreementList;
    }

    /// <summary>
    /// 根据Id获取用户协议和隐私采集
    /// </summary>
    /// <param name="inDto"></param>
    /// <returns></returns>
    [AllowAnonymous]
    [HttpPost]
    public async Task<UserAgreementView> GetUserAgreementById(GetUserAgreementByIdInDto inDto)
    {
        var userAgreement = await _userAgreementRepository.Where(x => x.Id == inDto.Id)
            .ProjectTo<UserAgreementView>(_mapper.ConfigurationProvider)
            .FirstNotNullAsync();
        return userAgreement;
    }


    /// <summary>
    /// 设置为当前版本
    /// </summary>
    /// <param name="inDto"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<IResponseOutput> SetCurrentVersion(SetUserAgreementCurrentVersionInDto inDto)
    {
        var userAgreement=await _userAgreementRepository.Where(x=>x.Id==inDto.Id).FirstNotNullAsync();
        await _userAgreementRepository.UpdatePartialFromQueryAsync(
            x => x.UserAgreementTypeEnum== userAgreement.UserAgreementTypeEnum&&x.IsCurrentVersion,
            x => new UserAgreement { IsCurrentVersion = false },true);

        await _userAgreementRepository.UpdatePartialFromQueryAsync(
            inDto.Id,
            x => new UserAgreement { IsCurrentVersion = true },true);

        return ResponseOutput.Ok();
    }

    /// <summary>
    /// 新增或者修改用户协议和隐私采集
    /// </summary>
    /// <param name="addOrEditUserAgreement"></param>
    /// <returns></returns>
    [HttpPost]
    public async  Task<IResponseOutput> AddOrUpdateUserAgreement(UserAgreementAddOrEdit addOrEditUserAgreement)
	{
	    var entity = await _userAgreementRepository.InsertOrUpdateAsync(addOrEditUserAgreement, true);
        return ResponseOutput.Ok(entity.Id.ToString());
		
	}

    /// <summary>
    /// 删除用户协议和隐私采集
    /// </summary>
    /// <param name="userAgreementId"></param>
    /// <returns></returns>
	[HttpDelete("{userAgreementId:guid}")]
    public async Task<IResponseOutput> DeleteUserAgreement(Guid userAgreementId)
	{
			var success = await _userAgreementRepository.DeleteFromQueryAsync(t => t.Id == userAgreementId,true);
            return ResponseOutput.Ok();
	}
      
}



