MS-API。AJAS

2021-01-04 07:27

阅读:426

标签: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(key, value);

}
///


/// 获取Redis中的值
///

///
///
private string GetRedisValue(string key)
{
RedisClient redisClient = new RedisClient("127.0.0.1");

return redisClient.Get(key);

}

///


/// 秒杀下单的接口
///

///
[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 {
//用户三分钟之内只能点击一次
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


评论


亲,登录后才可以留言!