码迷,mamicode.com
首页 > Windows程序 > 详细

MS-API。AJAS

时间:2020-06-26 21:57:56      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:convert   red   ice   rod   判断   成功   cli   private   var   

//1.获取秒杀商品详情接口
[Route("api/GetProduct")]
[HttpGet]
public ProductInfo GetProduct(int productId = 1)
{
ProductInfo info = new ProductInfo();
info.PId = 1;
info.SPName = "Iphone Xs Max 黑色";
info.SPimage = @"/image/1.png";
info.MarkPrice = 6999.99m;
info.KillPrice = 1999.9m;
info.KillStartTime = DateTime.Now.AddMinutes(-30);
info.KillEndTime = DateTime.Now.AddHours(1);
info.KillStokCount = 100;

info.KillStokCount = productId * 100;

//把秒杀的库存写进Redis里面
SetRedisValue("KillStokCount-" + productId, "100");
return info;
}

 

//2.秒杀下单
private void SetRedisValue(string key,string value)
{
RedisClient redisClient = new RedisClient("127.0.0.1");

redisClient.Set<string>(key, value);

}
/// <summary>
/// 获取Redis中的值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
private string GetRedisValue(string key)
{
RedisClient redisClient = new RedisClient("127.0.0.1");

return redisClient.Get<string>(key);

}

/// <summary>
/// 秒杀下单的接口
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("api/GetKill")]
public int KillOrder(OrderInfo order)
{
//res返回值表示
//1表示下单成功
//2表示库存不足
//3表示 三分钟之内只能点击一次
int res = 0;

#region 判断用户是不是在三分钟之内点击了一次,记录每次点击的时间
//同一个用户 在三分钟之内只能点击一次

//userId-1 2020-06-24 09:40:41
//userId-2 2020-06-24 09:41:20
var userKill = GetRedisValue("UserId-" + order.UserId);

if (!string.IsNullOrEmpty(userKill))
{
DateTime dateTime = Convert.ToDateTime(userKill);
var tmpvalue = DateTime.Now - dateTime;
if (tmpvalue.TotalMinutes < 3)
{
//用户三分钟之内只能点击一次
res = 3;
return res;
}
SetRedisValue("userId" + order.UserId, DateTime.Now.ToString());
}
else
{
//此处表示用户是第一次点击
SetRedisValue("userId" + order.UserId, DateTime.Now.ToString());
}

#endregion

#region 2.判断商品的库存够不够
ProductInfo info = new ProductInfo();

var maney = Convert.ToInt32(GetRedisValue("KillStokCount-" + info.PId));

if (maney > 0)
{
maney = maney - 1;

SetRedisValue("KillStokCount-" + info.PId, maney.ToString());
}
else
{
res = 2;
return res;
}

#endregion


return res;
}

}

------------------------------------------------------

 

MS-API。AJAS

标签:convert   red   ice   rod   判断   成功   cli   private   var   

原文地址:https://www.cnblogs.com/gc1229/p/13196331.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!