
//--------------------------------------------------------------------
//     此代码由liquid模板自动生成  byzhouhang 20240909
//     生成时间 {{DateTimeNow}} 
//     对此文件的更改可能会导致不正确的行为，并且如果重新生成代码，这些更改将会丢失。
//--------------------------------------------------------------------
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;
	
[ ApiExplorerSettings(GroupName = "Test")]
public class {{TableName}}Service(IRepository<{{TableName}}> {{LowercaseRepositoryName}},
                                  IMapper _mapper, IUserInfo _userInfo, IStringLocalizer _localizer): BaseService, I{{TableName}}Service
{

	{% if IsPaged %}
	[HttpPost]
	public async Task<PageOutput<{{TableNameView}}>>  Get{{TableName}}List({{TableNameQuery}} inQuery)
	{
			
		    var {{LowercaseQueryableName}} ={{LowercaseRepositoryName}}
{% for field in QueryListFieldList -%}
    {% if field.CSharpType == "string" -%}
                .WhereIf(!string.IsNullOrEmpty(inQuery.{{ field.FieldName }}), t => t.{{ field.FieldName }}.Contains(inQuery.{{ field.FieldName }}))
    {%- elsif field.CSharpType != "DateTime" -%}
                .WhereIf(inQuery.{{ field.FieldName }} != null, t => t.{{ field.FieldName }} == inQuery.{{ field.FieldName }})
    {%- endif %}
{% endfor -%}
                .ProjectTo<{{TableNameView}}>(_mapper.ConfigurationProvider);

            var pageList= await {{LowercaseQueryableName}}.ToPagedListAsync(inQuery);

        return pageList;
	}
	{% else %}
	[HttpPost]
	public async Task<List<{{TableNameView}}>>  Get{{TableName}}List({{TableNameQuery}} inQuery)
	{

		
		    var {{LowercaseQueryableName}} ={{LowercaseRepositoryName}}
{% for field in QueryListFieldList -%}
    {% if field.CSharpType == "string" -%}
                .WhereIf(!string.IsNullOrEmpty(inQuery.{{ field.FieldName }}), t => t.{{ field.FieldName }}.Contains(inQuery.{{ field.FieldName }}))
    {%- elsif field.CSharpType != "DateTime" -%}
                .WhereIf(inQuery.{{ field.FieldName }} != null, t => t.{{ field.FieldName }} == inQuery.{{ field.FieldName }})
    {%- endif %}
{% endfor -%}
			        .ProjectTo<{{TableNameView}}>(_mapper.ConfigurationProvider);
             
			return await {{LowercaseQueryableName}}.ToListAsync();
	}
	{% endif %}


    public async  Task<IResponseOutput> AddOrUpdate{{TableName}}({{TableName}}AddOrEdit addOrEdit{{TableName}})
	{
		    //  在此处拷贝automapper 映射
			  
            CreateMap<{{TableName}}, {{TableNameView}}>();
			CreateMap<{{TableName}},{{TableNameAddOrEdit}}>().ReverseMap();


			var entity = await {{LowercaseRepositoryName}}.InsertOrUpdateAsync(addOrEdit{{TableName}}, true);

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


	[HttpDelete("{{ '{' }}{{LowercaseTableNameId}}:guid{{ '}' }}")]
    public async Task<IResponseOutput> Delete{{TableName}}(Guid {{LowercaseTableNameId}})
	{
			var success = await {{LowercaseRepositoryName}}.DeleteFromQueryAsync(t => t.Id == {{LowercaseTableNameId}},true);
            return ResponseOutput.Ok();
	}
      
}



