Skip to content

Commit

Permalink
秒杀活动模拟项目
Browse files Browse the repository at this point in the history
  • Loading branch information
shenniubuxing3 committed Jun 29, 2017
0 parents commit 5a0ac41
Show file tree
Hide file tree
Showing 452 changed files with 145,226 additions and 0 deletions.
Binary file added SeckillPro/.vs/SeckillPro/v15/.suo
Binary file not shown.
1,008 changes: 1,008 additions & 0 deletions SeckillPro/.vs/config/applicationhost.config

Large diffs are not rendered by default.

206 changes: 206 additions & 0 deletions SeckillPro/SeckillPro.Api/Controllers/OrderController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using SeckillPro.Com.Model.ApiModel;
using SeckillPro.Com.Tool;
using SeckillPro.Com.Model.CrmModel;
using SeckillPro.Com.Model;
using SeckillPro.Com.Common;
using System.IO;

namespace SeckillPro.Api.Controllers
{
[Route("api/[controller]/[action]")]
public class OrderController : Controller
{
//redis引用
private StackRedis _redis = StackRedis.Current;

// GET api/values
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}

// GET api/values/5
[HttpGet("{id}")]
public string Get(int id)
{
return "value";
}

/// <summary>
/// 抢单
/// </summary>
/// <returns></returns>
[HttpPost]
public async Task<MoQiangGouRp> SubmitQiangGouOrder()
{
var rp = new MoQiangGouRp { RpMsg = EnumHelper.EmOrderStatus.抢购失败.ToString() };
try
{
var strRq = string.Empty;
using (var stream = Request.Body)
{
using (var reader = new StreamReader(stream))
{
strRq = await reader.ReadToEndAsync();
}
}
if (string.IsNullOrWhiteSpace(strRq)) { return rp; }

var rq = JsonConvert.DeserializeObject<MoQiangGouRq>(strRq);
if (rq.ShoppingId <= 0 || rq.MemberRq == null) { return rp; }

#region 验证

//登录验证
if (string.IsNullOrWhiteSpace(rq.MemberRq.Token))
{
rp.RpMsg = $"未登录,请登录后重试。";
return rp;
}
var sessionData = await _redis.Get<MoUserInfo>(rq.MemberRq.Token);
if (sessionData == null)
{
rp.RpMsg = $"登录失效,请重新登录!";
return rp;
}

//库存验证
var shopsKey = EnumHelper.EmDataKey.ShoppingHash.ToString();
var shop = await _redis.GetHashField<MoShopping>(shopsKey, rq.ShoppingId.ToString());
if (shop == null) { return rp; }
else if (shop.MaxNum <= 0)
{
rp.RpMsg = $"你太慢了,商品:{shop.Name},已经被抢完了!";
return rp;
}
else if (shop.MaxNum < rq.Num)
{
rp.RpMsg = $"库存不足,商品:{shop.Name},只剩{shop.MaxNum}了!";
return rp;
}
else if (shop.MaxGouNum < rq.Num)
{
rp.RpMsg = $"一个账号每次最多只能抢购【{shop.Name}{shop.MaxGouNum}件。";
return rp;
}
#endregion

#region 加入订单list中

var orderInfo = new MoOrderInfo();
orderInfo.OrderId = await DataKeyHelper.Current.GetKeyId(EnumHelper.EmDataKey.OrderId);
orderInfo.OrderStatus = (int)EnumHelper.EmOrderStatus.排队抢购中;
orderInfo.CreatTime = DateTime.Now;
orderInfo.Num = rq.Num;
orderInfo.ShoppingId = rq.ShoppingId;
orderInfo.UserId = sessionData.UserId;
orderInfo.MoShopping = shop;
//记录所有订单

//记录会员名下的订单
var isAddOrder = await _redis.SetOrUpdateHashsField<MoOrderInfo>($"User_{orderInfo.UserId}", orderInfo.OrderId.ToString(), orderInfo);
if (isAddOrder <= 0)
{
return rp;
}
rp.OrderId = orderInfo.OrderId;
rp.OrderStatus = orderInfo.OrderStatus;
rp.CreatTime = orderInfo.CreatTime;
rp.RpStatus = 1;
rp.RpMsg = EnumHelper.EmOrderStatus.排队抢购中.ToString();
#endregion

#region 各种验证无误后,加入抢购队列,并返回抢购中...

var isAddQiangQueue = await _redis.SetList<MoOrderInfo>($"{EnumHelper.EmDataKey.QiangOrderEqueue.ToString()}_{orderInfo.ShoppingId}", orderInfo);

#endregion
}
catch (Exception ex)
{
rp.RpMsg = "抢购活动正在高峰期,请稍后重试";
}
return rp;
}

[HttpPost]
public async Task<MoOrderDetailRp> GetOrderDetail()
{
var rp = new MoOrderDetailRp();
try
{
//获取请求参数
var strRq = string.Empty;
using (var stream = Request.Body)
{
using (var reader = new StreamReader(stream))
{
strRq = await reader.ReadToEndAsync();
}
}
if (string.IsNullOrWhiteSpace(strRq)) { return rp; }

var rq = JsonConvert.DeserializeObject<MoOrderDetailRq>(strRq);

#region 验证

#region 登录验证
if (rq.MemberRq == null || string.IsNullOrWhiteSpace(rq.MemberRq.Token))
{
rp.RpMsg = $"未登录,请登录后重试。";
return rp;
}
var sessionData = await _redis.Get<MoUserInfo>(rq.MemberRq.Token);
if (sessionData == null)
{
rp.RpMsg = $"登录失效,请重新登录!";
return rp;
}
#endregion

if (rq.OrderId <= 0)
{
rp.RpMsg = $"参数不正确!";
return rp;
}
#endregion

var orderDetail = await _redis.GetHashField<MoOrderInfo>($"User_{sessionData.UserId}", rq.OrderId.ToString());
if (orderDetail == null)
{
rp.RpMsg = $"订单查询失败,无此订单!";
return rp;
}

rp.OrderId = orderDetail.OrderId;
rp.Num = orderDetail.Num;
rp.OrderStatus = orderDetail.OrderStatus;
rp.PayOutTime = orderDetail.PayOutTime;
rp.CreatTime = orderDetail.CreatTime;
rp.ShoppingId = orderDetail.ShoppingId;

var shop = await _redis.GetHashField<MoShopping>(EnumHelper.EmDataKey.ShoppingHash.ToString(), rp.ShoppingId.ToString());
if (shop == null)
{
rp.RpMsg = $"订单查询失败,请稍后重试!";
return rp;
}
rp.MoShopping = shop;
rp.RpStatus = 1;
rp.RpMsg = "订单查询成功";
}
catch (Exception ex)
{
rp.RpMsg = "查询超时,请稍后重试";
}
return rp;
}
}
}
27 changes: 27 additions & 0 deletions SeckillPro/SeckillPro.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;

namespace SeckillPro.Api
{
public class Program
{
public static void Main(string[] args)
{
var builder = new WebHostBuilder()
.UseKestrel();
//参数指定端口
builder = args.Length > 0 ? builder.UseUrls(args[0]) : builder;
var host = builder.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();
host.Run();
}
}
}
29 changes: 29 additions & 0 deletions SeckillPro/SeckillPro.Api/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:4545/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/order/get",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"SeckillPro.Api": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/values",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:19300"
}
}
}
25 changes: 25 additions & 0 deletions SeckillPro/SeckillPro.Api/SeckillPro.Api.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<RuntimeIdentifiers>win7-x64;ubuntu.16.04-x64</RuntimeIdentifiers>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SeckillPro.Com\SeckillPro.Com.csproj" />
</ItemGroup>

</Project>
43 changes: 43 additions & 0 deletions SeckillPro/SeckillPro.Api/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace SeckillPro.Api
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}

public IConfigurationRoot Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();

app.UseMvc();
}
}
}
10 changes: 10 additions & 0 deletions SeckillPro/SeckillPro.Api/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
8 changes: 8 additions & 0 deletions SeckillPro/SeckillPro.Api/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
}
}
Loading

0 comments on commit 5a0ac41

Please sign in to comment.