From 182ef569ad04938739b016d7cdd52458baa5f01e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E4=BF=8A?= <1533057238@qq.com> Date: Thu, 4 Jun 2020 22:35:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=8F=E4=BF=8A=E7=9A=84=E4=BE=9D=E8=B5=96?= =?UTF-8?q?=E6=B3=A8=E5=85=A5=E4=B8=8Ejson?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\347\216\213\344\277\212/WebAPI10086.sln" | 25 +++++ .../Controllers/WeatherForecastController.cs" | 47 ++++++++++ .../Domain/Admin10086DbContext.cs" | 24 +++++ .../WebAPI10086/Domain/BaseEntity.cs" | 32 +++++++ .../WebAPI10086/Domain/DbInitializeHelper.cs" | 91 +++++++++++++++++++ .../WebAPI10086/Domain/Entity/Brand.cs" | 33 +++++++ .../WebAPI10086/Domain/Entity/Market.cs" | 23 +++++ .../WebAPI10086/Domain/Entity/Purchase.cs" | 23 +++++ .../WebAPI10086/Domain/Helper/JsonHelper.cs" | 21 +++++ .../WebAPI10086/Program.cs" | 26 ++++++ .../Properties/launchSettings.json" | 30 ++++++ .../WebAPI10086/Startup.cs" | 52 +++++++++++ .../WebAPI10086/WeatherForecast.cs" | 15 +++ .../WebAPI10086/WebAPI10086.csproj" | 13 +++ .../WebAPI10086/appsettings.Development.json" | 9 ++ .../WebAPI10086/appsettings.json" | 10 ++ 16 files changed, 474 insertions(+) create mode 100644 "\347\216\213\344\277\212/WebAPI10086.sln" create mode 100644 "\347\216\213\344\277\212/WebAPI10086/Controllers/WeatherForecastController.cs" create mode 100644 "\347\216\213\344\277\212/WebAPI10086/Domain/Admin10086DbContext.cs" create mode 100644 "\347\216\213\344\277\212/WebAPI10086/Domain/BaseEntity.cs" create mode 100644 "\347\216\213\344\277\212/WebAPI10086/Domain/DbInitializeHelper.cs" create mode 100644 "\347\216\213\344\277\212/WebAPI10086/Domain/Entity/Brand.cs" create mode 100644 "\347\216\213\344\277\212/WebAPI10086/Domain/Entity/Market.cs" create mode 100644 "\347\216\213\344\277\212/WebAPI10086/Domain/Entity/Purchase.cs" create mode 100644 "\347\216\213\344\277\212/WebAPI10086/Domain/Helper/JsonHelper.cs" create mode 100644 "\347\216\213\344\277\212/WebAPI10086/Program.cs" create mode 100644 "\347\216\213\344\277\212/WebAPI10086/Properties/launchSettings.json" create mode 100644 "\347\216\213\344\277\212/WebAPI10086/Startup.cs" create mode 100644 "\347\216\213\344\277\212/WebAPI10086/WeatherForecast.cs" create mode 100644 "\347\216\213\344\277\212/WebAPI10086/WebAPI10086.csproj" create mode 100644 "\347\216\213\344\277\212/WebAPI10086/appsettings.Development.json" create mode 100644 "\347\216\213\344\277\212/WebAPI10086/appsettings.json" diff --git "a/\347\216\213\344\277\212/WebAPI10086.sln" "b/\347\216\213\344\277\212/WebAPI10086.sln" new file mode 100644 index 0000000..9f57f32 --- /dev/null +++ "b/\347\216\213\344\277\212/WebAPI10086.sln" @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29519.87 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebAPI10086", "WebAPI10086\WebAPI10086.csproj", "{A5CA951F-19B0-4899-8335-B5B31A28445A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {A5CA951F-19B0-4899-8335-B5B31A28445A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A5CA951F-19B0-4899-8335-B5B31A28445A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A5CA951F-19B0-4899-8335-B5B31A28445A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A5CA951F-19B0-4899-8335-B5B31A28445A}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {4AC2DBF8-9F42-48FC-9C6E-6165090447BD} + EndGlobalSection +EndGlobal diff --git "a/\347\216\213\344\277\212/WebAPI10086/Controllers/WeatherForecastController.cs" "b/\347\216\213\344\277\212/WebAPI10086/Controllers/WeatherForecastController.cs" new file mode 100644 index 0000000..52b8442 --- /dev/null +++ "b/\347\216\213\344\277\212/WebAPI10086/Controllers/WeatherForecastController.cs" @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using WebAPI10086.Domain.Helper; + +namespace WebAPI10086.Controllers +{ + [ApiController] + [Route("[controller]")] + public class WeatherForecastController : ControllerBase + { + private readonly Admin10086DbContext _db; + + private static readonly string[] Summaries = new[] + { + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" + }; + + private readonly ILogger _logger; + + public WeatherForecastController(ILogger logger,Admin10086DbContext dbContext) + { + _logger = logger; + _db = dbContext; + } + + [HttpGet] + public string Get() + { + + //var list = new List(); + //using (var db = new Admin10086DbContext()) + //{ + // list = db.Brands.ToList(); + //} + //string str = JsonConvert.SerializeObject(list, Formatting.Indented); + var str = _db.Brands.Include(x=>x.Purchase).ToList(); + return JsonHelper.SerializeObject(str); + } + } +} + \ No newline at end of file diff --git "a/\347\216\213\344\277\212/WebAPI10086/Domain/Admin10086DbContext.cs" "b/\347\216\213\344\277\212/WebAPI10086/Domain/Admin10086DbContext.cs" new file mode 100644 index 0000000..ba1dde4 --- /dev/null +++ "b/\347\216\213\344\277\212/WebAPI10086/Domain/Admin10086DbContext.cs" @@ -0,0 +1,24 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using WebAPI10086.Domain.Entity; + +namespace WebAPI10086 +{ + public class Admin10086DbContext : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)//构建器 + { + optionsBuilder.UseSqlServer("server=DESKTOP-S3544UE\\SQLEXPRESS;database=Admin1008611;uid=sa;pwd = 123456;"); + } + + public DbSet MarKets { get; set; } + + public DbSet Purchases { get; set; } + + public DbSet Brands { get; set; } + + } +} diff --git "a/\347\216\213\344\277\212/WebAPI10086/Domain/BaseEntity.cs" "b/\347\216\213\344\277\212/WebAPI10086/Domain/BaseEntity.cs" new file mode 100644 index 0000000..405524a --- /dev/null +++ "b/\347\216\213\344\277\212/WebAPI10086/Domain/BaseEntity.cs" @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebAPI10086 +{ + public class BaseEntity + { + public BaseEntity() + { + IsActived = true; + IsDeleted = false; + CreatedTime = DateTime.Now; + UpdateTime = DateTime.Now; + } + + public int Id { set; get; } + + public bool IsActived { set; get; } + + public bool IsDeleted { set; get; } + + public DateTime CreatedTime { set; get; } + + public DateTime UpdateTime { set; get; } + + public string Remarks { set; get; } + + } +} + diff --git "a/\347\216\213\344\277\212/WebAPI10086/Domain/DbInitializeHelper.cs" "b/\347\216\213\344\277\212/WebAPI10086/Domain/DbInitializeHelper.cs" new file mode 100644 index 0000000..e0a8fda --- /dev/null +++ "b/\347\216\213\344\277\212/WebAPI10086/Domain/DbInitializeHelper.cs" @@ -0,0 +1,91 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using WebAPI10086.Domain.Entity; + +namespace WebAPI10086.Controllers +{ + public class DbInitializeHelper + { + public static void Initilizer() + { + using (var db = new Admin10086DbContext()) + { + db.Database.EnsureCreated(); + + var tempBrand = db.Purchases.Any(); + if (!tempBrand) + { + var Purchase1 = new Purchase + { + CommodityName = "鸿星尔克", + UnitPrice = 156.9M, + Quantuty = 100 + }; + + var Purchase2 = new Purchase + { + CommodityName = "耐克", + UnitPrice = 140.88M, + Quantuty = 100 + }; + db.Purchases.AddRange(Purchase1, Purchase2); + + db.SaveChanges(); + + var Maret1 = new Market + { + + CommodityName = Purchase1.CommodityName, + UnitPrice = 256.9M, + Quantuty = 60 + + }; + + var Maret2 = new Market + { + CommodityName = Purchase2.CommodityName, + UnitPrice = 340.88M, + Quantuty = 50 + }; + + db.MarKets.AddRange(Maret1, Maret2); + + db.SaveChanges(); + + db.Brands.AddRange(new Brand[] + { + new Brand + { + CommodityName = Purchase1.CommodityName, + PurchaseQuantuty = Purchase1.Quantuty, + PurchaseMoney = Purchase1.Quantuty * Purchase1.UnitPrice, + MarketQuantuty = Maret1.Quantuty, + MarketMoney = Maret1.Quantuty * Maret1.UnitPrice, + BalanceQuantuty = Purchase1.Quantuty - Maret1.Quantuty, + RetaubedProFits = (Maret1.Quantuty * Maret1.UnitPrice) - (Purchase1.Quantuty * Purchase1.UnitPrice), + PurchaseId = Purchase1.Id, + MarKetId = Maret1.Id + },new Brand + { + CommodityName = Purchase2.CommodityName, + PurchaseQuantuty = Purchase2.Quantuty, + PurchaseMoney = Purchase2.Quantuty * Purchase2.UnitPrice, + MarketQuantuty = Maret2.Quantuty, + MarketMoney = Maret2.Quantuty * Maret2.UnitPrice, + BalanceQuantuty = Purchase2.Quantuty - Maret2.Quantuty, + RetaubedProFits = (Maret2.Quantuty * Maret2.UnitPrice) - (Purchase2.Quantuty * Purchase2.UnitPrice), + PurchaseId = Purchase2.Id, + MarKetId = Maret2.Id + } + }); ; + + db.SaveChanges(); + + } + } + + } + } +} diff --git "a/\347\216\213\344\277\212/WebAPI10086/Domain/Entity/Brand.cs" "b/\347\216\213\344\277\212/WebAPI10086/Domain/Entity/Brand.cs" new file mode 100644 index 0000000..38df01d --- /dev/null +++ "b/\347\216\213\344\277\212/WebAPI10086/Domain/Entity/Brand.cs" @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using WebAPI10086.Domain.Entity; + +namespace WebAPI10086 +{ + public class Brand : BaseEntity + { + public string CommodityName { get; set; } + + public int PurchaseQuantuty { get; set; } //进货总量 + + public decimal PurchaseMoney { set; get; } // 进货总金额 + + public int MarketQuantuty { get; set; } //售出总量 + + public decimal MarketMoney { set; get; } // 售出总金额 + + public int BalanceQuantuty { get; set; } //结存数量 + + public decimal RetaubedProFits { get; set; } //净利润 + + public int PurchaseId { get; set; } + + public int MarKetId { get; set; } + + public Purchase Purchase { get; set; } + + public Market Market { get; set; } + } +} diff --git "a/\347\216\213\344\277\212/WebAPI10086/Domain/Entity/Market.cs" "b/\347\216\213\344\277\212/WebAPI10086/Domain/Entity/Market.cs" new file mode 100644 index 0000000..dad5273 --- /dev/null +++ "b/\347\216\213\344\277\212/WebAPI10086/Domain/Entity/Market.cs" @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebAPI10086.Domain.Entity +{ + public class Market : BaseEntity + { + public string CommodityName { get; set; } // 商品名称 + + public decimal UnitPrice { set; get; } //单价 + + public int Quantuty { set; get; } // 数量 + + + public int PurchasesId { set; get; } + + public Purchase Purchase { set; get; } + + public IEnumerable Brans { get; set; } + } +} diff --git "a/\347\216\213\344\277\212/WebAPI10086/Domain/Entity/Purchase.cs" "b/\347\216\213\344\277\212/WebAPI10086/Domain/Entity/Purchase.cs" new file mode 100644 index 0000000..23ccbc1 --- /dev/null +++ "b/\347\216\213\344\277\212/WebAPI10086/Domain/Entity/Purchase.cs" @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using WebAPI10086.Domain.Entity; + +namespace WebAPI10086 +{ + public class Purchase : BaseEntity + { + public string CommodityName { set; get; } // 商品名称 + + public decimal UnitPrice { set; get; } //单价 + + public int Quantuty { set; get; } // 数量 + + public IEnumerable Brands { get; set; } + + public IEnumerable Markets { get; set; } + } +} + + diff --git "a/\347\216\213\344\277\212/WebAPI10086/Domain/Helper/JsonHelper.cs" "b/\347\216\213\344\277\212/WebAPI10086/Domain/Helper/JsonHelper.cs" new file mode 100644 index 0000000..e02d248 --- /dev/null +++ "b/\347\216\213\344\277\212/WebAPI10086/Domain/Helper/JsonHelper.cs" @@ -0,0 +1,21 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebAPI10086.Domain.Helper +{ + public class JsonHelper + { + public static string SerializeObject(object obj) + { + return JsonConvert.SerializeObject(obj, Formatting.Indented, new JsonSerializerSettings + { + ReferenceLoopHandling = ReferenceLoopHandling.Ignore, + DateFormatString = "yyyy-MM-dd HH:mm:ss" + }); + } + + } +} diff --git "a/\347\216\213\344\277\212/WebAPI10086/Program.cs" "b/\347\216\213\344\277\212/WebAPI10086/Program.cs" new file mode 100644 index 0000000..46aaeac --- /dev/null +++ "b/\347\216\213\344\277\212/WebAPI10086/Program.cs" @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace WebAPI10086 +{ + 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(); + }); + } +} diff --git "a/\347\216\213\344\277\212/WebAPI10086/Properties/launchSettings.json" "b/\347\216\213\344\277\212/WebAPI10086/Properties/launchSettings.json" new file mode 100644 index 0000000..a6b559e --- /dev/null +++ "b/\347\216\213\344\277\212/WebAPI10086/Properties/launchSettings.json" @@ -0,0 +1,30 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:60921", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "WebAPI10086": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "applicationUrl": "http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git "a/\347\216\213\344\277\212/WebAPI10086/Startup.cs" "b/\347\216\213\344\277\212/WebAPI10086/Startup.cs" new file mode 100644 index 0000000..0509f2c --- /dev/null +++ "b/\347\216\213\344\277\212/WebAPI10086/Startup.cs" @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +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 WebAPI10086.Controllers; + +namespace WebAPI10086 +{ + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + services.AddDbContext(); + services.AddControllers(); + } + + // 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.UseRouting(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + + DbInitializeHelper.Initilizer(); + } + } +} diff --git "a/\347\216\213\344\277\212/WebAPI10086/WeatherForecast.cs" "b/\347\216\213\344\277\212/WebAPI10086/WeatherForecast.cs" new file mode 100644 index 0000000..2c7cdd5 --- /dev/null +++ "b/\347\216\213\344\277\212/WebAPI10086/WeatherForecast.cs" @@ -0,0 +1,15 @@ +using System; + +namespace WebAPI10086 +{ + public class WeatherForecast + { + public DateTime Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string Summary { get; set; } + } +} diff --git "a/\347\216\213\344\277\212/WebAPI10086/WebAPI10086.csproj" "b/\347\216\213\344\277\212/WebAPI10086/WebAPI10086.csproj" new file mode 100644 index 0000000..620192f --- /dev/null +++ "b/\347\216\213\344\277\212/WebAPI10086/WebAPI10086.csproj" @@ -0,0 +1,13 @@ + + + + netcoreapp3.0 + + + + + + + + + diff --git "a/\347\216\213\344\277\212/WebAPI10086/appsettings.Development.json" "b/\347\216\213\344\277\212/WebAPI10086/appsettings.Development.json" new file mode 100644 index 0000000..e203e94 --- /dev/null +++ "b/\347\216\213\344\277\212/WebAPI10086/appsettings.Development.json" @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } + } +} diff --git "a/\347\216\213\344\277\212/WebAPI10086/appsettings.json" "b/\347\216\213\344\277\212/WebAPI10086/appsettings.json" new file mode 100644 index 0000000..d9d9a9b --- /dev/null +++ "b/\347\216\213\344\277\212/WebAPI10086/appsettings.json" @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +} -- Gitee