# Template

Title: [简单三层]_模版_实体_Sugar
TemplateTypeName: 实体
ChangeTime: 2022-03-21 13:57:25.310
Content:
using System;
using System.Collections.Generic;
using System.Linq;
using SqlSugar;
namespace @Model.name_space
{
    /// <summary>
    /// @((Model.Description+"").Replace("\r","").Replace("\n",""))
    ///</summary>
    [SugarTable("@(Model.TableName)")]
    public partial class @(Model.ClassName)Entity
    {
        public @(Model.ClassName)Entity()
        { 
            this.Id=Guid.NewGuid();
            this.IsDeleted=false;
            this.CreateTime = DateTime.Now;
        }
@foreach (var item in Model.PropertyGens)
{
    var isPrimaryKey = item.IsPrimaryKey ? ",IsPrimaryKey = true" : "";
    var isIdentity = item.IsIdentity ? ",IsIdentity = true" : "";
    var isNull=(item.IsNullable&&item.Type!="string"&&item.IsSpecialType==false&&item.Type!="byte[]")?"?":"";
    var isIgnore=(item.IsIgnore?",IsIgnore = true":"");
    var isJson=(item.CodeType.StartsWith("json")?",IsJson= true":"");

    var newPropertyName=item.PropertyName; //这里可以用C#处理 实体属性的显式格式
    //想和数据库一样就用 newPropertyName=item.DbColumnName
    if(System.Text.RegularExpressions.Regex.IsMatch(newPropertyName.Substring(0,1), "[0-9]"))
    {
        newPropertyName="_"+newPropertyName;//处理属性名开头为数字情况
    }
    if(newPropertyName==Model.ClassName)
    {
        newPropertyName="_"+newPropertyName;//处理属性名不能等于类名
    }


    var desc=(item.Description+"").Replace("\r","").Replace("\n","");//处理换行

    if(isIgnore!="")
    {
       isPrimaryKey =isIdentity =isNull="";
     }
        @:/// <summary>
        @:/// @(desc) 
        @if(item.DefaultValue!=null)
        {
        @:/// 默认值: @Raw(item.DefaultValue)
        }
        @:///</summary>
        @: [SugarColumn(ColumnName="@item.DbColumnName" @(isPrimaryKey) @(isIdentity) @(isIgnore) @(isJson))]
        @: public @Raw(item.Type)@isNull @newPropertyName { get; set; }
    }
    }
}


Title: [简单三层]_模版_服务_Sugar
TemplateTypeName: 业务
ChangeTime: 2022-04-07 20:38:40.030
Content:
using SqlSugar;
using Yi.Framework.Interface;
using Yi.Framework.Model.Models;
using Yi.Framework.Repository;

namespace Yi.Framework.Service
{
    public partial class @(Model.ClassName)Service : Repository<@(Model.ClassName)Entity>, I@(Model.ClassName)Service
    {
        public @(Model.ClassName)Service(ISqlSugarClient context) : base(context)
        {
        }
    }
}


Title: [简单三层]_模版_API_Sugar
TemplateTypeName: 前端
ChangeTime: 2022-04-07 20:41:36.148
Content:
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Yi.Framework.Common.Models;
using Yi.Framework.Interface;
using Yi.Framework.Model.Models;
using Yi.Framework.Repository;
using Yi.Framework.WebCore;
using Yi.Framework.WebCore.AttributeExtend;
using Yi.Framework.WebCore.AuthorizationPolicy;

namespace Yi.Framework.ApiMicroservice.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class @(Model.ClassName)Controller : BaseCrudController<@(Model.ClassName)Entity>
    {
        public @(Model.ClassName)Controller(ILogger<@(Model.ClassName)Entity> logger, I@(Model.ClassName)Service i@(Model.ClassName)Service) : base(logger, i@(Model.ClassName)Service)
        {
        }
    }
}


Title: [简单三层]_模板_接口_sugar
TemplateTypeName: 业务
ChangeTime: 2022-04-03 19:40:06.337
Content:
using Yi.Framework.Model.Models;
using Yi.Framework.Repository;

namespace Yi.Framework.Interface
{
   public partial interface I@(Model.ClassName)Service: IRepository<@(Model.ClassName)Entity>
    {   
    }
}


# FileInfo

Name: .net 标准类库
Json: { "name":"命名空间类库名","nuget":[{ "name":"SqlSugarCore","version":"5.0.3.4" }]}
Suffix: csproj
ChangeTime: 2021-11-26 11:00:09.1143579
PojectId: 00000000-0000-0000-0000-000000000000
Content:
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <RootNamespace>@Model.name</RootNamespace>
    <AssemblyName>@Model.name</AssemblyName>
  </PropertyGroup>
  <ItemGroup>
   @foreach(var item in  Model.nuget)
    {
         <PackageReference Include="@item.name" Version="@item.version" />
    }
  </ItemGroup>
  @Raw(Model.reference)
</Project>


Name: .net core 5.0 类库
Json: { "name":"命名空间类库名","nuget":[{ "name":"SqlSugarCore","version":"5.0.3.4" }]}
Suffix: csproj
ChangeTime: 2021-11-26 11:00:09.1207074
PojectId: 00000000-0000-0000-0000-000000000000
Content:
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <RootNamespace>@Model.name</RootNamespace>
    <AssemblyName>@Model.name</AssemblyName>
  </PropertyGroup>
  <ItemGroup>
   @foreach(var item in  Model.nuget)
    {
         <PackageReference Include="@item.name" Version="@item.version" />
    }
  </ItemGroup>
  @Raw(Model.reference)
</Project>


Name: .net core 5.0 web 项目
Json: { "name":"命名空间类库名","nuget":[{ "name":"SqlSugarCore","version":"5.0.3.4" }]}
Suffix: csproj
ChangeTime: 2021-11-26 11:00:09.1262623
PojectId: 00000000-0000-0000-0000-000000000000
Content:
<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
   @foreach(var item in  Model.nuget)
    {
         <PackageReference Include="@item.name" Version="@item.version" />
    }
  </ItemGroup>
  @Raw(Model.reference)
</Project>


Name: .net core 3.1 类库
Json: { "name":"命名空间类库名","nuget":[{ "name":"SqlSugarCore","version":"5.0.3.4" }]}
Suffix: csproj
ChangeTime: 2021-11-26 11:00:09.1371644
PojectId: 00000000-0000-0000-0000-000000000000
Content:
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <RootNamespace>@Model.name</RootNamespace>
    <AssemblyName>@Model.name</AssemblyName>
  </PropertyGroup>
  <ItemGroup>
   @foreach(var item in  Model.nuget)
    {
         <PackageReference Include="@item.name" Version="@item.version" />
    }
  </ItemGroup>
  @Raw(Model.reference)
</Project>


Name: Repository.cs
Json: {"name":"DbContext", "name_space":"命名空间" }
Suffix: cs
ChangeTime: 2021-11-26 11:00:09.1417512
PojectId: 00000000-0000-0000-0000-000000000000
Content:
using SqlSugar;
using System.Collections.Generic;
using System.Linq;
namespace @Model.name_space
{
     /***这里面写的代码不会给覆盖,如果要重新生成请删除 Repository.cs ***/

    /// <summary>
    /// 仓储模式
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class Repository<T> : SimpleClient<T> where T : class, new()
    {

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="context"></param>
        public Repository(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null
        {
            if (context == null)
            {
                base.Context = Db;
            }
        }

        /// <summary>
        /// SqlSugarScope操作数据库是线程安的可以单例
        /// </summary>
        public static SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig()
        {
            DbType = [请设置DbType],
            ConnectionString =  [请设置ConStr],
            IsAutoCloseConnection = true
        },
         db =>
         {
             //如果用单例配置要统一写在这儿
             db.Aop.OnLogExecuting = (s, p) =>
             {

             };

         });

        /// <summary>
        /// 仓储扩展方法:单表查询通用分页 
        /// </summary>
        /// <returns></returns>
        public object CommonPage(QueryParameters pars, int pageIndex, int pagesize)
        {
            int tolCount = 0;
            var sugarParamters = pars.Parameters.Select(it => (IConditionalModel)new ConditionalModel()
            {
                ConditionalType = it.ConditionalType,
                FieldName = it.FieldName,
                FieldValue = it.FieldValue
            }).ToList();
            var query = Db.Queryable<T>();
            if (pars.OrderBys != null)
            {
                foreach (var item in pars.OrderBys)
                {
                    query.OrderBy(item.ToSqlFilter());//格式 id asc或者 id desc
                }
            }
            var result = query.Where(sugarParamters).ToPageList(1, 2, ref tolCount);
            return new
            {
                count = tolCount,
                data = result
            };
        }

        /// <summary>
        /// 仓储扩展方法:多表查询通用分页 
        /// 用法  CommonPage(db.Queryable<JoinTable1,JoinTable2>(...).Select(it=new class(){..}).MergeTable(),pars,orderbys,pageIndex,pagesize)
        /// </summary>
        /// <returns></returns>
        public object CommonPage<ViewModel>(ISugarQueryable<ViewModel> query, QueryParameters pars, int pageIndex, int pagesize)
        {
            int tolCount = 0;
            var sugarParamters = pars.Parameters.Select(it => (IConditionalModel)new ConditionalModel()
            {
                ConditionalType = it.ConditionalType,
                FieldName = it.FieldName,
                FieldValue = it.FieldValue
            }).ToList();
            if (pars.OrderBys != null)
            {
                foreach (var item in pars.OrderBys)
                {
                    query.OrderBy(item.ToSqlFilter());//格式 id asc或者 id desc
                }
            }
            var result = query.Where(sugarParamters).ToPageList(1, 2, ref tolCount);
            return new
            {
                count = tolCount,
                data = result
            };
        }

    }
    /// <summary>
    /// 通用查询参数
    /// </summary>
    public class QueryParameters
    {
        public List<QueryParameter> Parameters { get; set; }
        public List<string> OrderBys { get; set; }

    }
    /// <summary>
    /// 通用查询参数
    /// </summary>
    public class QueryParameter
    {
        public string FieldName { get; set; }
        public string FieldValue { get; set; }
        public ConditionalType ConditionalType
        {
            get; set;
        }

    }
}


Name: API文件[Startup]
Json: {"name":"Startup", "name_space":"命名空间" }
Suffix: cs
ChangeTime: 2021-11-26 11:00:09.1470274
PojectId: 00000000-0000-0000-0000-000000000000
Content:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace @Model.name_space{
 
    public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }
    public readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {

        services.AddControllers();
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebFirst.Api", Version = "v1" });

        });

        //配置JSON.NET
        services.AddControllers().AddNewtonsoftJson(opt =>
        {
                //忽略循环引用
                opt.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

                //不改变字段大小
                opt.SerializerSettings.ContractResolver = new DefaultContractResolver();
        });

        //配置可以跨域
        services.AddCors(options =>
        {
            options.AddPolicy(MyAllowSpecificOrigins,
              builder => builder.AllowAnyOrigin()
                        .AllowAnyHeader()
                       .WithMethods("GET", "POST", "HEAD", "PUT", "DELETE", "OPTIONS")
                         );
        });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.InjectJavascript("");
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "API");
                c.DocExpansion(Swashbuckle.AspNetCore.SwaggerUI.DocExpansion.None);
                c.DefaultModelsExpandDepth(-1);
            });
        }
        app.UseCors(MyAllowSpecificOrigins);//添加这个

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}
}
 


Name: API文件[Program]
Json: {"name":"Program", "name_space":"命名空间" }
Suffix: cs
ChangeTime: 2021-11-26 11:00:09.1521286
PojectId: 00000000-0000-0000-0000-000000000000
Content:
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace @Model.name_space
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }
}


Name: API文件[appsettings.json]
Json: {"name":"appsettings"}
Suffix: json
ChangeTime: 2021-11-26 11:00:09.1573357
PojectId: 00000000-0000-0000-0000-000000000000
Content:
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}


# CodeType

Name: int
CSharepType: int
DbType:
[{"Name":"int","Length":null,"DecimalDigits":null},{"Name":"int4","Length":null,"DecimalDigits":null},{"Name":"number","Length":9,"DecimalDigits":0},{"Name":"integer","Length":null,"DecimalDigits":null}]


Name: nString10
CSharepType: string
DbType:
[{"Name":"nvarchar","Length":10,"DecimalDigits":null},{"Name":"varchar","Length":10,"DecimalDigits":null}]


Name: nString36
CSharepType: string
DbType:
[{"Name":"nvarchar","Length":36,"DecimalDigits":null},{"Name":"varchar","Length":36,"DecimalDigits":null}]


Name: nString100
CSharepType: string
DbType:
[{"Name":"nvarchar","Length":100,"DecimalDigits":null},{"Name":"varchar","Length":100,"DecimalDigits":null}]


Name: nString200
CSharepType: string
DbType:
[{"Name":"nvarchar","Length":200,"DecimalDigits":null},{"Name":"varchar","Length":200,"DecimalDigits":null}]


Name: nString500
CSharepType: string
DbType:
[{"Name":"nvarchar","Length":500,"DecimalDigits":null},{"Name":"varchar","Length":500,"DecimalDigits":null}]


Name: nString2000
CSharepType: string
DbType:
[{"Name":"nvarchar","Length":2000,"DecimalDigits":null},{"Name":"varchar","Length":2000,"DecimalDigits":null}]


Name: maxString
CSharepType: string
DbType:
[{"Name":"text","Length":null,"DecimalDigits":null},{"Name":"clob","Length":null,"DecimalDigits":null}]


Name: bool
CSharepType: bool
DbType:
[{"Name":"bit","Length":null,"DecimalDigits":null},{"Name":"number","Length":1,"DecimalDigits":null},{"Name":"boolean","Length":null,"DecimalDigits":null}]


Name: DateTime
CSharepType: DateTime
DbType:
[{"Name":"datetime","Length":null,"DecimalDigits":null},{"Name":"date","Length":null,"DecimalDigits":null}]


Name: decimal_18_8
CSharepType: decimal
DbType:
[{"Name":"decimal","Length":18,"DecimalDigits":8},{"Name":"number","Length":18,"DecimalDigits":8},{"Name":"numeric","Length":18,"DecimalDigits":8}]


Name: decimal_18_4
CSharepType: decimal
DbType:
[{"Name":"decimal","Length":18,"DecimalDigits":4},{"Name":"number","Length":18,"DecimalDigits":4},{"Name":"money","Length":0,"DecimalDigits":0}]


Name: guid
CSharepType: Guid
DbType:
[{"Name":"uniqueidentifier","Length":null,"DecimalDigits":null},{"Name":"guid","Length":null,"DecimalDigits":null},{"Name":"char","Length":36,"DecimalDigits":null}]


Name: byte
CSharepType: byte
DbType:
[{"Name":"tinyint","Length":null,"DecimalDigits":null},{"Name":"varbit","Length":null,"DecimalDigits":null},{"Name":"number","Length":3,"DecimalDigits":null}]


Name: short
CSharepType: short
DbType:
[{"Name":"short","Length":null,"DecimalDigits":null},{"Name":"int2","Length":null,"DecimalDigits":null},{"Name":"int16","Length":null,"DecimalDigits":null},{"Name":"smallint","Length":null,"DecimalDigits":null},{"Name":"number","Length":5,"DecimalDigits":null}]


Name: long
CSharepType: long
DbType:
[{"Name":"long","Length":null,"DecimalDigits":null},{"Name":"int8","Length":null,"DecimalDigits":null},{"Name":"int64","Length":null,"DecimalDigits":null},{"Name":"bigint","Length":null,"DecimalDigits":null},{"Name":"number","Length":19,"DecimalDigits":null}]


Name: byteArray
CSharepType: byte[]
DbType:
[{"Name":"blob","Length":null,"DecimalDigits":null},{"Name":"longblob","Length":null,"DecimalDigits":null},{"Name":"binary","Length":null,"DecimalDigits":null}]


Name: json_default
CSharepType: object
DbType:
[{"Name":"json","Length":null,"DecimalDigits":null},{"Name":"varchar","Length":3999,"DecimalDigits":null}]


