diff --git a/CoreCms.Net.IRepository/Good/ICoreCmsProductsRepository.cs b/CoreCms.Net.IRepository/Good/ICoreCmsProductsRepository.cs index 536d7201..cddeea14 100644 --- a/CoreCms.Net.IRepository/Good/ICoreCmsProductsRepository.cs +++ b/CoreCms.Net.IRepository/Good/ICoreCmsProductsRepository.cs @@ -29,6 +29,12 @@ namespace CoreCms.Net.IRepository /// Task GoodsStaticsTotalWarn(int goodsStocksWarn); + /// + /// 获取库存报警数量(返回商品序列) + /// + /// + /// + Task> GoodsStaticsTotalWarnByGoodIds(int goodsStocksWarn); /// /// 获取关联商品的货品列表数据 diff --git a/CoreCms.Net.IServices/Good/ICoreCmsProductsServices.cs b/CoreCms.Net.IServices/Good/ICoreCmsProductsServices.cs index ac3c0109..a4068161 100644 --- a/CoreCms.Net.IServices/Good/ICoreCmsProductsServices.cs +++ b/CoreCms.Net.IServices/Good/ICoreCmsProductsServices.cs @@ -52,6 +52,12 @@ namespace CoreCms.Net.IServices /// Task GoodsStaticsTotalWarn(int goodsStocksWarn); + /// + /// 获取库存报警数量(返回商品序列) + /// + /// + /// + Task> GoodsStaticsTotalWarnByGoodIds(int goodsStocksWarn); /// /// 获取关联商品的货品列表数据 diff --git a/CoreCms.Net.Repository/Good/CoreCmsProductsRepository.cs b/CoreCms.Net.Repository/Good/CoreCmsProductsRepository.cs index ad68ea2a..b7563638 100644 --- a/CoreCms.Net.Repository/Good/CoreCmsProductsRepository.cs +++ b/CoreCms.Net.Repository/Good/CoreCmsProductsRepository.cs @@ -74,25 +74,72 @@ namespace CoreCms.Net.Repository /// public async Task GoodsStaticsTotalWarn(int goodsStocksWarn) { - var sql = @"SELECT COUNT(*) AS number - FROM ( SELECT t.goodsId - FROM ( SELECT goodsId , - ( CASE WHEN stock < freezeStock THEN 0 - ELSE stock - freezeStock - END ) AS number - FROM CoreCmsProducts - ) t - WHERE t.number < " + goodsStocksWarn + @" - GROUP BY t.goodsId - ) d"; + var sql = @"SELECT COUNT(id) FROM dbo.CoreCmsGoods WHERE id IN + (SELECT goodsId + FROM + ( + SELECT t.goodsId + FROM + ( + SELECT goodsId, + (CASE + WHEN stock < freezeStock THEN + 0 + ELSE + stock - freezeStock + END + ) AS number, + isDel + FROM CoreCmsProducts + WHERE isDel = 0 + ) t + WHERE t.number < @goodsStocksWarnNumber + GROUP BY t.goodsId + ) d) AND isDel =0 "; - var dt = await DbClient.Ado.GetDataTableAsync(sql); + var dt = await DbClient.Ado.GetDataTableAsync(sql, new { goodsStocksWarnNumber = goodsStocksWarn }); var number = dt.Rows[0][0].ObjectToInt(0); return number; } - #endregion + #region 获取库存报警数量(返回商品序列) + /// + /// 获取库存报警数量(返回商品序列) + /// + /// + /// + public async Task> GoodsStaticsTotalWarnByGoodIds(int goodsStocksWarn) + { + var sql = @"SELECT id FROM dbo.CoreCmsGoods WHERE id IN + (SELECT goodsId + FROM + ( + SELECT t.goodsId + FROM + ( + SELECT goodsId, + (CASE + WHEN stock < freezeStock THEN + 0 + ELSE + stock - freezeStock + END + ) AS number, + isDel + FROM CoreCmsProducts + WHERE isDel = 0 + ) t + WHERE t.number < @goodsStocksWarnNumber + GROUP BY t.goodsId + ) d) AND isDel =0 "; + + var result = await DbClient.Ado.SqlQueryAsync(sql, new { goodsStocksWarnNumber = goodsStocksWarn }); + return result; + } + #endregion + + #region 获取关联商品的货品列表数据 /// /// 获取关联商品的货品列表数据 diff --git a/CoreCms.Net.Services/Good/CoreCmsProductsServices.cs b/CoreCms.Net.Services/Good/CoreCmsProductsServices.cs index 00582f53..4a6aaebf 100644 --- a/CoreCms.Net.Services/Good/CoreCmsProductsServices.cs +++ b/CoreCms.Net.Services/Good/CoreCmsProductsServices.cs @@ -318,6 +318,17 @@ namespace CoreCms.Net.Services } + /// + /// 获取库存报警数量(返回商品序列) + /// + /// + /// + public async Task> GoodsStaticsTotalWarnByGoodIds(int goodsStocksWarn) + { + return await _dal.GoodsStaticsTotalWarnByGoodIds(goodsStocksWarn); + } + + #region 获取关联商品的货品列表数据 /// /// 获取关联商品的货品列表数据 diff --git a/CoreCms.Net.Web.Admin/Controllers/Com/ToolsController.cs b/CoreCms.Net.Web.Admin/Controllers/Com/ToolsController.cs index 3bd5622b..8fd3a278 100644 --- a/CoreCms.Net.Web.Admin/Controllers/Com/ToolsController.cs +++ b/CoreCms.Net.Web.Admin/Controllers/Com/ToolsController.cs @@ -1930,7 +1930,7 @@ namespace CoreCms.Net.Web.Admin.Controllers //库存报警 var goodsStaticsTotalWarn = await _productsServices.GoodsStaticsTotalWarn(goodsStocksWarn); - + //返回数据 jm.code = 0; jm.msg = "数据调用成功!"; @@ -1939,11 +1939,9 @@ namespace CoreCms.Net.Web.Admin.Controllers unpaidCount, unshipCount, aftersalesCount, - goodsStaticsTotalWarn + goodsStaticsTotalWarn, }; - - - + return jm; } diff --git a/CoreCms.Net.Web.Admin/Controllers/Good/CoreCmsGoodsController.cs b/CoreCms.Net.Web.Admin/Controllers/Good/CoreCmsGoodsController.cs index 60a2092a..692e15c0 100644 --- a/CoreCms.Net.Web.Admin/Controllers/Good/CoreCmsGoodsController.cs +++ b/CoreCms.Net.Web.Admin/Controllers/Good/CoreCmsGoodsController.cs @@ -231,14 +231,22 @@ namespace CoreCms.Net.Web.Admin.Controllers //获取库存 var allConfigs = await _settingServices.GetConfigDictionaries(); var kc = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.GoodsStocksWarn); + var kcNumer = kc.ObjectToInt(); if (kc != null) { - var kcNumer = kc.ObjectToInt(); - where = where.And(p => p.stock <= kcNumer && p.isDel == false && p.isMarketable); + var ids = await _productsServices.GoodsStaticsTotalWarnByGoodIds(kcNumer); + if (ids.Any()) + { + where = where.And(p=>ids.Contains(p.id)); + } } else { - where = where.And(p => p.stock <= 0 && p.isDel == false && p.isMarketable); + var ids = await _productsServices.GoodsStaticsTotalWarnByGoodIds(0); + if (ids.Any()) + { + where = where.And(p => ids.Contains(p.id)); + } } } @@ -295,13 +303,13 @@ namespace CoreCms.Net.Web.Admin.Controllers if (kc != null) { var kcNumer = kc.ObjectToInt(); - totalWarn = await _coreCmsGoodsServices.GetCountAsync(p => - p.stock <= kcNumer && p.isDel == false && p.isMarketable); + //返回数量 + totalWarn = await _productsServices.GoodsStaticsTotalWarn(kcNumer); } else { - totalWarn = await _coreCmsGoodsServices.GetCountAsync(p => - p.stock <= 0 && p.isDel == false && p.isMarketable); + //返回数量 + totalWarn = await _productsServices.GoodsStaticsTotalWarn(0); } //获取商品分类 diff --git a/CoreCms.Net.Web.Admin/wwwroot/views/index.html b/CoreCms.Net.Web.Admin/wwwroot/views/index.html index 6d911c23..eb8e13d7 100644 --- a/CoreCms.Net.Web.Admin/wwwroot/views/index.html +++ b/CoreCms.Net.Web.Admin/wwwroot/views/index.html @@ -229,7 +229,7 @@ - + 库存报警 {{d.goodsStaticsTotalWarn || '0'}}
{{d.goodsStaticsTotalWarn || '0'}}