<#@ 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=false;
 #>

<# 
	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.Infra.EFCore;
using IRaCIS.Core.Infrastructure.Extention;
namespace IRaCIS.Core.Application.Contracts
{	
	/// <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 PageOutput<<#=tableName#>View>  Get<#=tableName#>List(<#=tableName#>Query query<#=tableName#>)
		{
			
		     var	 <#=char.ToLower(tableName[0]) + tableName.Substring(1)#>Queryable =

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

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

            return pageList;
		}
	    <# }  else {#>

		public List<<#=tableName#>View>  Get<#=tableName#>List(<#=tableName#>Query query<#=tableName#>)
		{

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


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

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

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


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

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