server/aspnetapp/Services/IProductService.cs

(开头部分) 3KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using aspnetapp.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace aspnetapp.Services
{
    /// <summary>
    /// 商品服务
    /// </summary>
    public class IProductService
    {
        private readonly CounterContext _context;
        private IMemoryCache _memoryCache;

        public IProductService(CounterContext productDbContext, IMemoryCache cache)
        {
            _context = productDbContext;
            this._memoryCache = cache ?? throw new ArgumentNullException(nameof(cache));
        }

        public bool SubStock(int id)
        {
            lock (this)
            {
                // 1、获取商品库存
                GoodsEntity? stocks = getPorductStocks(id);

                // 2、判断商品库存是否为空
                if (stocks?.StoresNum == 0)
                {
                    // 2.1 失败消息
                    Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}:不好意思,商品:{stocks.Name},已售罄");
                    return false;
                }

                // 3、成功消息
                Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}:恭喜你,成功");

                // 4、扣减商品库存
                subtracProductStocks(stocks);

                return true;
            }
        }

        public bool AddStock(int id)
        {
            lock (this)
            {
                // 1、获取商品库存
                GoodsEntity? stocks = getPorductStocks(id);

                // 2、商品库存
                addtracProductStocks(stocks);

后面还有 60 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。