<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core.dll" #>
<#@ assembly name="System.Data.dll" #>
<#@ assembly name="System.Data.DataSetExtensions.dll" #>
<#@ assembly name="System.Xml.dll" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Xml" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Data" #>
<#@ import namespace="System.Data.SqlClient" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>

<#@ include file="$(ProjectDir)DbHelper.ttinclude"  #>
<#@ include file="$(ProjectDir)ModelAuto.ttinclude"	#>
<# var manager = new Manager(Host, GenerationEnvironment, true); #>


//在这里设置 isPage 是否生成分页的还是不分页

<# 
	var isPage=true;
 #>

<# 
	var OutputPath1 =Path.GetDirectoryName(Host.TemplateFile+"..");
	OutputPath1=Path.Combine(OutputPath1,"Services_New");
	if (!Directory.Exists(OutputPath1))
	{
	    Directory.CreateDirectory(OutputPath1);
	}
#>


<# foreach (var item in DbHelper.GetDbTablesNew(config.ConnectionString, config.DbDatabase,config.TableName))
   {
		 var tableName=item.ToString();
		manager.StartBlock(tableName+"Service"+".cs",OutputPath1);//文件名
#>
//--------------------------------------------------------------------
//     此代码由T4模板自动生成  byzhouhang 20210918
//	   生成时间 <#=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")#> 
//     对此文件的更改可能会导致不正确的行为，并且如果重新生成代码，这些更改将会丢失。
//--------------------------------------------------------------------

using IRaCIS.Core.Domain.Models;
using Microsoft.AspNetCore.Mvc;
using IRaCIS.Core.Application.Interfaces;
using IRaCIS.Core.Application.ViewModel;
namespace IRaCIS.Core.Application.Service
{	
	/// <summary>
	/// <#=tableName#>Service
	/// </summary>	
	[ ApiExplorerSettings(GroupName = "Test")]
	public class <#=tableName#>Service: BaseService, I<#=tableName#>Service
    {

			 private readonly IRepository<<#=tableName#>> _<#=char.ToLower(tableName[0]) + tableName.Substring(1)#>Repository;

		public <#=tableName#>Service(IRepository<<#=tableName#>> <#=char.ToLower(tableName[0]) + tableName.Substring(1)#>Repository)
		{
            _<#=char.ToLower(tableName[0]) + tableName.Substring(1)#>Repository = <#=char.ToLower(tableName[0]) + tableName.Substring(1)#>Repository;        
		}

	   <# if(isPage){#>
	    public async Task<PageOutput<<#=tableName#>View>>  Get<#=tableName#>List(<#=tableName#>Query inQuery)
		{
			
		     var	 <#=char.ToLower(tableName[0]) + tableName.Substring(1)#>Queryable =

			    _<#=char.ToLower(tableName[0]) + tableName.Substring(1)#>Repository
			  .ProjectTo<<#=tableName#>View>(_mapper.ConfigurationProvider);

                var pageList= await <#=char.ToLower(tableName[0]) + tableName.Substring(1)#>Queryable
				.ToPagedListAsync(inQuery.PageIndex, inQuery.PageSize, string.IsNullOrWhiteSpace(inQuery.SortField) ? "Id" : inQuery.SortField,
                inQuery.Asc);

            return pageList;
		}
	    <# }  else {#>

		public async Task<List<<#=tableName#>View>>  Get<#=tableName#>List(<#=tableName#>Query inQuery)
		{

		
		     var <#=char.ToLower(tableName[0]) + tableName.Substring(1)#>Queryable = _<#=char.ToLower(tableName[0]) + tableName.Substring(1)#>Repository
			 .ProjectTo<<#=tableName#>View>(_mapper.ConfigurationProvider);
             
			 return await <#=char.ToLower(tableName[0]) + tableName.Substring(1)#>Queryable.ToListAsync();
		}
	    <# }#>


        public async  Task<IResponseOutput> AddOrUpdate<#=tableName#>(<#=tableName#>AddOrEdit addOrEdit<#=tableName#>)
		{
		      //  在此处拷贝automapper 映射
			  

                 CreateMap<<#=tableName#>, <#=tableName#>View>();
			  //  CreateMap< <#=tableName#>,<#=tableName#>AddOrEdit>().ReverseMap();


			    var entity = await _<#=char.ToLower(tableName[0]) + tableName.Substring(1)#>Repository.InsertOrUpdateAsync(addOrEdit<#=tableName#>, true);

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


		[HttpDelete("{<#=char.ToLower(tableName[0]) + tableName.Substring(1)#>Id:guid}")]
        public async Task<IResponseOutput> Delete<#=tableName#>(Guid <#=char.ToLower(tableName[0]) + tableName.Substring(1)#>Id)
		{
			  var success = await _<#=char.ToLower(tableName[0]) + tableName.Substring(1)#>Repository.DeleteFromQueryAsync(t => t.Id == <#=char.ToLower(tableName[0]) + tableName.Substring(1)#>Id,true);
              return ResponseOutput.Ok();
		}

       
    }
} 
<#
   manager.EndBlock(); 
   }
  manager.Process(true);
 #>
