<%-- 
Name:Winson.Entity
Author: winson
Ver:3.3
Description: 实体类代码生成
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Debug="False" Description="Create a list of properties from database table." ResponseEncoding="UTF-8"%>
<%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema" Category="数据库" Description="连接数据库对象." %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="表名" Description="数据源表" %>
<%@ Property Name="ProjectName" Type="System.String" Default="Winson" Optional="False" Category="项目" Description="项目名称" %>
<%@ Property Name="UseIdentityID" Type="System.Boolean" Default="False" Optional="False" Category="项目" Description="是否使用自增主键" %>
<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="可获取数据类型" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
//=============================================================================
// This project was built by Winson.Framework 3.3
//
// Copyright (c) 2007-2009 Winson 
//
// Create Date: <%= DateTime.Now.ToString() %>
//=============================================================================

using System;

namespace <%=ProjectName%>.EntityOP.Entity
{	
	/// <summary>
	<%if (SourceTable.Description!=""){%>
	///<%=SourceTable.Description%> 实体类
	<%}else{%>
    /// <%= ConvertToPascal(SourceTable.Name) %>表实体类
	<%}%>
    /// </summary>
	public class <%= ConvertToPascal(SourceTable.Name.Replace(" ","")) %>DS
    {
        public <%= ConvertToPascal(SourceTable.Name.Replace(" ","")) %>DS(){}
        
		#region 成员
		<% 
        foreach (ColumnSchema column in  SourceTable.Columns) { 
			//Response.WriteLine(column.SystemType.FullName);						
				%>
		private <%= CSharpAlias[GetTypeName(column.SystemType.FullName)] %> _<%= column.Name.ToLower() %>;
		<% if(!column.IsPrimaryKeyMember){%>
		private bool _<%= column.Name.ToLower() %>SetValue = false;
			<%}}%>
			
				
		<% int n=0;
        foreach (ColumnSchema column in  SourceTable.Columns) { %>
			
		/// <summary>
		<%if(column.Description!=""){%>
		/// <%= column.Description %>
		<%}else{%>
		/// 获取或设置<%= ConvertToPascal(column.Name.ToLower()) %>(可读写)
		<%}%>
		/// </summary>
		public <%= CSharpAlias[GetTypeName(column.SystemType.FullName)] %> <%= ConvertToPascal(column.Name.ToLower()) %>
		{ 
			get {return _<%= column.Name.ToLower() %>;} 
			set { _<%= column.Name.ToLower() %> = value; <%			
			if(!column.IsPrimaryKeyMember){
			%>_<%= column.Name.ToLower() %>SetValue = true;<%}					
			%>}
		}
		<%
		n++;}%>   
		/// <summary>
		/// 获取字段数量(只读)
		/// </summary>
        public int Count{get { return <%=n%>; }}
        #endregion

		#region 索引器
        public object this[int index, int type, int maxID]
        {			 
			get
			{
				if(type==1)
                {
                    #region 返回字段名
                    switch (index)
                    {
                        <%for (int i=0;i<SourceTable.Columns.Count;i++) {
                            Response.WriteLine("\t\t\t\t\t\t case {0}:",i);
                                switch(SourceTable.Columns[i].SystemType.FullName) 
                                {   
                                    case "System.String":
                                        Response.WriteLine("\t\t\t\t\t\t\t if((_{0} != null && _{0}.Length > 0) || _{0}SetValue)",SourceTable.Columns[i].Name.ToLower());
                                        Response.WriteLine("\t\t\t\t\t\t\t\t return \"{0}\";",ConvertToPascal(SourceTable.Columns[i].Name.ToLower()));
                                        Response.WriteLine("\t\t\t\t\t\t\t else");
                                        Response.WriteLine("\t\t\t\t\t\t\t\t return null;");
                                        break;
                                    case "System.DateTime":
                                        Response.WriteLine("\t\t\t\t\t\t\t if((_{0} != DateTime.MaxValue && _{0} != DateTime.MinValue) || _{0}SetValue)",SourceTable.Columns[i].Name.ToLower());
                                        Response.WriteLine("\t\t\t\t\t\t\t\t return \"{0}\";",ConvertToPascal(SourceTable.Columns[i].Name.ToLower()));
                                        Response.WriteLine("\t\t\t\t\t\t\t else");
                                        Response.WriteLine("\t\t\t\t\t\t\t\t return null;");
                                        break;
                                    case "System.Int32":
                                    case "System.Int64":
									case "System.Int16":
                                    case "System.Double":
                                    case "System.SByte":
									case "System.Byte":
                                    case "System.UInt32":
                                    case "System.UInt64":
									case "System.Decimal":
									case "System.Single":
									    if(i==0)
                                        {
											if(UseIdentityID)
											{
											Response.WriteLine("\t\t\t\t\t\t\t if (_{0} > 0)",SourceTable.Columns[i].Name.ToLower());
                                            Response.WriteLine("\t\t\t\t\t\t\t\t return \"{0}\";",ConvertToPascal(SourceTable.Columns[i].Name.ToLower()));
                                            Response.WriteLine("\t\t\t\t\t\t\t else");
                                            Response.WriteLine("\t\t\t\t\t\t\t\t return null;");
											}else{										
                                            Response.WriteLine("\t\t\t\t\t\t\t if (maxID > 0 || _{0} > 0)",SourceTable.Columns[i].Name.ToLower());
                                            Response.WriteLine("\t\t\t\t\t\t\t\t return \"{0}\";",ConvertToPascal(SourceTable.Columns[i].Name.ToLower()));
                                            Response.WriteLine("\t\t\t\t\t\t\t else");
                                            Response.WriteLine("\t\t\t\t\t\t\t\t return null;");}
                                        }
                                        else
                                        {                                           
											Response.WriteLine("\t\t\t\t\t\t\t if(_{0} != 0 || _{0}SetValue)",SourceTable.Columns[i].Name.ToLower());
											Response.WriteLine("\t\t\t\t\t\t\t\t return \"{0}\";",ConvertToPascal(SourceTable.Columns[i].Name.ToLower()));
											Response.WriteLine("\t\t\t\t\t\t\t else");
											Response.WriteLine("\t\t\t\t\t\t\t\t return null;");
                                        }                                        
                                        break;   
                                    case "System.Boolean":                                        
                                        	Response.WriteLine("\t\t\t\t\t\t\t if(_{0}SetValue)",SourceTable.Columns[i].Name.ToLower());
											Response.WriteLine("\t\t\t\t\t\t\t\t return \"{0}\";",ConvertToPascal(SourceTable.Columns[i].Name.ToLower()));
											Response.WriteLine("\t\t\t\t\t\t\t else");
											Response.WriteLine("\t\t\t\t\t\t\t\t return null;");
                                        break;                                
									case "System.Byte[]":
                                            Response.WriteLine("\t\t\t\t\t\t\t if(_{0} != null || _{0}SetValue)",SourceTable.Columns[i].Name.ToLower());
											Response.WriteLine("\t\t\t\t\t\t\t\t return \"{0}\";",ConvertToPascal(SourceTable.Columns[i].Name.ToLower()));
											Response.WriteLine("\t\t\t\t\t\t\t else");
											Response.WriteLine("\t\t\t\t\t\t\t\t return null;");
                                        break;									
                                } 
                            }%> 
                        default:
                            return null;                           
                    }
                    #endregion
                }
                else
                {
                    #region 返回字段值
                    switch (index)
                    {
                        <%for (int i=0;i<SourceTable.Columns.Count;i++) {
                            Response.WriteLine("\t\t\t\t\t\t case {0}:",i);
							 	if(i==0 && HasPrimaryKeyName(SourceTable))
                                {
									if(UseIdentityID)
												Response.WriteLine("\t\t\t\t\t\t\t\t return null;");
									else{
                                    Response.WriteLine("\t\t\t\t\t\t\t if (maxID > 0)");
                                    Response.WriteLine("\t\t\t\t\t\t\t\t return maxID;");
                                    Response.WriteLine("\t\t\t\t\t\t\t else if({0} > 0)",ConvertToPascal(SourceTable.Columns[i].Name.ToLower()));
                                    Response.WriteLine("\t\t\t\t\t\t\t\t return  {0};",ConvertToPascal(SourceTable.Columns[i].Name.ToLower()));
                                    Response.WriteLine("\t\t\t\t\t\t\t else");
                                    Response.WriteLine("\t\t\t\t\t\t\t\t return null;");}
                                }
                                else
                                {                                    
                                    Response.WriteLine("\t\t\t\t\t\t\t\t return {0};",ConvertToPascal(SourceTable.Columns[i].Name.ToLower()));
                                }                                
                            }%> 
                        default:
                            return null;                           
                    }
                    #endregion
                }
			}	            
        }
		#endregion
	}
}

<script runat="template">
//骆驼命名转换，首字大写
public string ConvertToPascal(string str)
{
	string [] tempStr = str.Split('_');
	string temp="";
	if(tempStr.Length > 0)
	{		
		for(int i=0;i<tempStr.Length;i++)
			temp+=tempStr[i].Substring(0,1).ToUpper()+tempStr[i].Substring(1).ToLower()+"_";
		return temp.Substring(0,temp.Length-1);
	}
	else
    	return str.Substring(0,1).ToUpper() + str.Substring(1).ToLower();
}
//判断是否有主键
public bool HasPrimaryKeyName(TableSchema table)
{
    if (table.PrimaryKey != null)
    {
        return true;
    }
    else
    {
        throw new ApplicationException("此模板需要有主键的表");		
    }
}
//获取数据类型
public string GetTypeName(string FullName)
{
	switch(FullName) 
	{
		case "System.Int16":
		case "System.Byte":
			return "Int16";
			break;
		default:
			return FullName;
			break;
	}
}

</script>

