187 lines
6.5 KiB
Plaintext
187 lines
6.5 KiB
Plaintext
@{
|
|
ModelData modelData = Model;
|
|
SysTableSchemaViewEntity[] sysTableSchemas = modelData.sysTableSchemas;
|
|
NamespaceObject namespaceObject = modelData.NamespaceObject;
|
|
}
|
|
using CloudBuilder.Core.Authorization;
|
|
using CloudBuilder.Core.DatabaseAccessor.Entity;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
@{
|
|
string entityBase = string.Format("EntityBase<{0}>", @modelData.KeyType);
|
|
if (@modelData.KeyCount > 1)
|
|
{
|
|
entityBase = string.Format("EntityBase<{0}>", @modelData.KeyType);
|
|
}
|
|
else if (@sysTableSchemas[0].Xtype.ToUpper().Trim() != "U")
|
|
{
|
|
entityBase = "ViewEntityBase";
|
|
}
|
|
}
|
|
namespace @namespaceObject.Namespace@{<text>.Entity</text>}
|
|
|
|
{
|
|
[TypeScript]
|
|
[Table("@sysTableSchemas[0].TableName")]
|
|
public class @sysTableSchemas[0].TableName.StringToProerty()Entity : @entityBase
|
|
@if (@sysTableSchemas[0].Xtype.ToUpper().Trim() == "U")
|
|
{<text></text>}
|
|
{
|
|
|
|
public @sysTableSchemas[0].TableName.StringToProerty()Entity()
|
|
{
|
|
}
|
|
|
|
@for (var i = 0; i < sysTableSchemas.Length; i++)
|
|
{
|
|
if (@sysTableSchemas[i].FieldName.ToUpper() == "ID") { continue; }
|
|
if (@sysTableSchemas[i].FieldName.ToUpper() == "ROW_VERSION")
|
|
{
|
|
<text> [Column("row_version")]
|
|
[Timestamp]
|
|
public byte[]? RowVersion { get; set; }
|
|
</text>
|
|
continue;
|
|
}
|
|
|
|
if (@sysTableSchemas[i].Pk > 0 && @modelData.KeyCount < 2)
|
|
{
|
|
<text> [Key]
|
|
</text>
|
|
}
|
|
if (@sysTableSchemas[i].IsIdentity > 0)
|
|
{
|
|
<text> [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
|
</text>
|
|
}
|
|
if (@sysTableSchemas[i].TypeName == "decimal")
|
|
{
|
|
<text> [Column("@sysTableSchemas[i].FieldName",TypeName = "@sysTableSchemas[i].TypeName</text><text>(@sysTableSchemas[i].MaxLength,@sysTableSchemas[i].Scale)")]
|
|
</text>
|
|
}
|
|
else
|
|
{
|
|
<text> [Column("@sysTableSchemas[i].FieldName")]
|
|
</text>
|
|
}
|
|
if (@sysTableSchemas[i].TypeOfName == "string")
|
|
{
|
|
<text> public @sysTableSchemas[i].TypeOfName@{if(@sysTableSchemas[i].IsNullAble=="y"){<text>?</text>}} @sysTableSchemas[i].FieldName.StringToProerty() { get; set; }
|
|
</text>
|
|
}
|
|
else
|
|
{
|
|
<text> public @sysTableSchemas[i].TypeOfName @sysTableSchemas[i].FieldName.StringToProerty() { get; set; }
|
|
</text>
|
|
}
|
|
}
|
|
|
|
@for (var i = 0; i < sysTableSchemas.Length; i++)
|
|
{
|
|
<text> public const string @sysTableSchemas[i].FieldName.ToUpper() = "@sysTableSchemas[i].FieldName.StringToProerty()";
|
|
</text>
|
|
}
|
|
|
|
public const string DB_NAME_@sysTableSchemas[0].TableName.ToUpper() = "@sysTableSchemas[0].TableName";
|
|
@{
|
|
string fs = "";
|
|
}
|
|
@for (var i = 0; i < sysTableSchemas.Length; i++)
|
|
{
|
|
if (@sysTableSchemas[i].FieldName.ToUpper() == "ID") { continue; }
|
|
fs += @sysTableSchemas[i].FieldName + ",";
|
|
}
|
|
@{
|
|
fs = fs.Substring(0, fs.Length - 1);
|
|
} public const string DB_NAME_FIELDS="@(fs)";
|
|
|
|
@for (var i = 0; i < sysTableSchemas.Length; i++)
|
|
{
|
|
<text> public const string DB_@sysTableSchemas[i].FieldName.ToUpper() = "@sysTableSchemas[i].FieldName";
|
|
</text>
|
|
}
|
|
|
|
|
|
public static @sysTableSchemas[0].TableName.StringToProerty()Entity GetInstance()
|
|
{
|
|
return new @sysTableSchemas[0].TableName.StringToProerty()Entity();
|
|
}
|
|
|
|
public virtual void CopyFrom(@sysTableSchemas[0].TableName.StringToProerty()Entity source)
|
|
{
|
|
CopyFrom(source, true);
|
|
}
|
|
|
|
public virtual void CopyFrom(@sysTableSchemas[0].TableName.StringToProerty()Entity source, bool includeSystemFields)
|
|
{
|
|
if (source == null)
|
|
{
|
|
throw new NullReferenceException("Source entity is null.");
|
|
}
|
|
|
|
@for (var i = 0; i < sysTableSchemas.Length; i++)
|
|
{
|
|
if (@sysTableSchemas[i].FieldName != "created_datetime" || @sysTableSchemas[i].FieldName != "created_by"
|
|
|| @sysTableSchemas[i].FieldName != "updated_datetime" || @sysTableSchemas[i].FieldName != "updated_by")
|
|
{
|
|
<text> this.@sysTableSchemas[i].FieldName.StringToProerty() = source.@sysTableSchemas[i].FieldName.StringToProerty();
|
|
</text>
|
|
}
|
|
}
|
|
|
|
if (includeSystemFields)
|
|
{
|
|
@for (var i = 0; i < sysTableSchemas.Length; i++)
|
|
{
|
|
if (@sysTableSchemas[i].FieldName == "created_datetime" || @sysTableSchemas[i].FieldName == "created_by"
|
|
|| @sysTableSchemas[i].FieldName == "updated_datetime" || @sysTableSchemas[i].FieldName == "updated_by")
|
|
{
|
|
<text> this.@sysTableSchemas[i].FieldName.StringToProerty() = source.@sysTableSchemas[i].FieldName.StringToProerty();
|
|
</text>
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
public override string GetSelectFrom()
|
|
{
|
|
return $" select {DB_NAME_FIELDS} from { DB_NAME_@sysTableSchemas[0].TableName.ToUpper()} with(nolock) ";
|
|
}
|
|
|
|
public override string GetSelectCountFrom(string sqlWhere)
|
|
{
|
|
return $" select count(*) from { DB_NAME_@sysTableSchemas[0].TableName.ToUpper()} with(nolock) where 1=1 {sqlWhere}";
|
|
}
|
|
|
|
public override string GetSelectTopFrom(int limit, int start,string orderby,string orderbytag, string sqlWhere)
|
|
{
|
|
if (string.IsNullOrEmpty(orderbytag) || orderbytag.Trim() != "desc") orderbytag = "asc";
|
|
@for (var i = 0; i < sysTableSchemas.Length; i++)
|
|
{
|
|
if (@sysTableSchemas[i].Pk > 0)
|
|
{
|
|
<text> if (string.IsNullOrEmpty(orderby) || !DB_NAME_FIELDS.Contains(orderby)) orderby = DB_@sysTableSchemas[i].FieldName.ToUpper() ; </text>
|
|
break;
|
|
}
|
|
}
|
|
|
|
@if(sysTableSchemas.Sum(x=>x.Pk)==0)
|
|
{
|
|
<text> if (string.IsNullOrEmpty(orderby) || !DB_NAME_FIELDS.Contains(orderby)) orderby = DB_@sysTableSchemas[0].FieldName.ToUpper() ; </text>
|
|
}
|
|
string top = limit > 0 ? $"top {limit}" : "";
|
|
return $" select {top} {DB_NAME_FIELDS} from (select {DB_NAME_FIELDS} ,row_number() over(order by {orderby} {orderbytag}) as num from { DB_NAME_@sysTableSchemas[0].TableName.ToUpper()} with(nolock) where 1=1 {sqlWhere} ) a where num> {limit * (start - 1)}";
|
|
}
|
|
|
|
public @sysTableSchemas[0].TableName.StringToProerty()Entity Clone()
|
|
{
|
|
@sysTableSchemas[0].TableName.StringToProerty()Entity result = new @sysTableSchemas[0].TableName.StringToProerty()Entity();
|
|
result.CopyFrom(this);
|
|
|
|
return result;
|
|
}
|
|
|
|
}
|
|
}
|