浏览代码

谷养正道商城 商品库存预警提醒

wangxy 1 周之前
父节点
当前提交
0512555bc6

+ 11 - 0
fs-admin/src/main/java/com/fs/his/controller/FsGuCoinGoodsController.java

@@ -85,4 +85,15 @@ public class FsGuCoinGoodsController extends BaseController
     {
         return toAjax(fsGuCoinGoodsService.deleteFsGuCoinGoodsByGoodsIds(goodsIds));
     }
+
+    /**
+     * 库存预警列表(总后台弹窗提醒用)
+     * 返回上架中且库存<=预警阈值的商品
+     */
+    @PreAuthorize("@ss.hasPermi('his:guCoinGoods:list')")
+    @GetMapping("/warnList")
+    public AjaxResult warnList()
+    {
+        return AjaxResult.success(fsGuCoinGoodsService.selectWarnList());
+    }
 }

+ 4 - 0
fs-service/src/main/java/com/fs/his/domain/FsGuCoinGoods.java

@@ -65,6 +65,10 @@ public class FsGuCoinGoods extends BaseEntity
     @Excel(name = "库存")
     private Long stock;
 
+    /** 库存预警阈值(库存<=此值触发预警,默认按初始库存10%自动填充) */
+    @Excel(name = "库存预警阈值")
+    private Long stockWarn;
+
     /** 详情 */
     @Excel(name = "详情")
     private String descs;

+ 11 - 0
fs-service/src/main/java/com/fs/his/mapper/FsGuCoinGoodsMapper.java

@@ -38,4 +38,15 @@ public interface FsGuCoinGoodsMapper {
      */
     @Update("update fs_gu_coin_goods set stock = stock + #{num} where goods_id = #{goodsId}")
     int addStock(@Param("goodsId") Long goodsId, @Param("num") int num);
+
+    /**
+     * 库存预警列表:上架中(status=1)且库存<=预警阈值的商品
+     */
+    List<FsGuCoinGoods> selectWarnList();
+
+    /**
+     * 库存耗尽自动下架:stock<=0 时将 status 置为 0
+     */
+    @Update("update fs_gu_coin_goods set status = 0, update_time = NOW() where goods_id = #{goodsId} and stock <= 0 and status = 1")
+    int autoOffShelfIfZero(@Param("goodsId") Long goodsId);
 }

+ 10 - 0
fs-service/src/main/java/com/fs/his/service/IFsGuCoinGoodsService.java

@@ -22,4 +22,14 @@ public interface IFsGuCoinGoodsService {
     public int updateFsGuCoinGoods(FsGuCoinGoods fsGuCoinGoods);
 
     public int deleteFsGuCoinGoodsByGoodsIds(Long[] goodsIds);
+
+    /**
+     * 库存预警列表:上架中且库存<=预警阈值的商品(总后台弹窗用)
+     */
+    public List<FsGuCoinGoods> selectWarnList();
+
+    /**
+     * 库存耗尽自动下架(stock<=0 时 status 置为 0)
+     */
+    public int autoOffShelfIfZero(Long goodsId);
 }

+ 21 - 0
fs-service/src/main/java/com/fs/his/service/impl/FsGuCoinGoodsServiceImpl.java

@@ -35,12 +35,23 @@ public class FsGuCoinGoodsServiceImpl implements IFsGuCoinGoodsService
     @Override
     public int insertFsGuCoinGoods(FsGuCoinGoods fsGuCoinGoods) {
         fsGuCoinGoods.setCreateTime(DateUtils.getNowDate());
+        // 库存预警阈值:未传时按初始库存的10%自动填充(向下取整,最小1)
+        if (fsGuCoinGoods.getStockWarn() == null) {
+            long stock = fsGuCoinGoods.getStock() == null ? 0L : fsGuCoinGoods.getStock();
+            long warn = Math.max(stock / 10, 1L);
+            fsGuCoinGoods.setStockWarn(warn);
+        }
         return fsGuCoinGoodsMapper.insertFsGuCoinGoods(fsGuCoinGoods);
     }
 
     @Override
     public int updateFsGuCoinGoods(FsGuCoinGoods fsGuCoinGoods) {
         fsGuCoinGoods.setUpdateTime(DateUtils.getNowDate());
+        // 修改库存时若未显式传预警阈值且库存被调整,则按新库存10%重算阈值
+        if (fsGuCoinGoods.getStockWarn() == null && fsGuCoinGoods.getStock() != null) {
+            long warn = Math.max(fsGuCoinGoods.getStock() / 10, 1L);
+            fsGuCoinGoods.setStockWarn(warn);
+        }
         return fsGuCoinGoodsMapper.updateFsGuCoinGoods(fsGuCoinGoods);
     }
 
@@ -48,4 +59,14 @@ public class FsGuCoinGoodsServiceImpl implements IFsGuCoinGoodsService
     public int deleteFsGuCoinGoodsByGoodsIds(Long[] goodsIds) {
         return fsGuCoinGoodsMapper.deleteFsGuCoinGoodsByGoodsIds(goodsIds);
     }
+
+    @Override
+    public List<FsGuCoinGoods> selectWarnList() {
+        return fsGuCoinGoodsMapper.selectWarnList();
+    }
+
+    @Override
+    public int autoOffShelfIfZero(Long goodsId) {
+        return fsGuCoinGoodsMapper.autoOffShelfIfZero(goodsId);
+    }
 }

+ 9 - 0
fs-service/src/main/java/com/fs/his/service/impl/FsGuCoinOrderServiceImpl.java

@@ -39,6 +39,7 @@ import com.fs.his.param.FsGuCoinOrderParam;
 import com.fs.his.param.PayOrderParam;
 import com.fs.his.service.IFsExpressService;
 import com.fs.his.service.IFsGuCoinCartService;
+import com.fs.his.service.IFsGuCoinGoodsService;
 import com.fs.his.service.IFsGuCoinOrderService;
 import com.fs.his.service.IFsStorePaymentService;
 import com.fs.his.service.IFsUserGuCoinLogsService;
@@ -97,6 +98,8 @@ public class FsGuCoinOrderServiceImpl implements IFsGuCoinOrderService
     private IFsStorePaymentService storePaymentService;
     @Autowired
     private IFsGuCoinCartService fsGuCoinCartService;
+    @Autowired
+    private IFsGuCoinGoodsService fsGuCoinGoodsService;
 
     /** 聚水潭固定店铺编号(与积分订单一致,从配置注入) */
     @Value("${jst.shop_code:''}")
@@ -265,6 +268,8 @@ public class FsGuCoinOrderServiceImpl implements IFsGuCoinOrderService
         if (fsGuCoinGoodsMapper.subStock(goods.getGoodsId(), 1) <= 0) {
             throw new CustomException("库存不足");
         }
+        // 库存耗尽自动下架(前端无法兑换)
+        fsGuCoinGoodsService.autoOffShelfIfZero(goods.getGoodsId());
 
         // 生成订单编号
         String orderCode;
@@ -410,6 +415,8 @@ public class FsGuCoinOrderServiceImpl implements IFsGuCoinOrderService
             if (fsGuCoinGoodsMapper.subStock(goods.getGoodsId(), num) <= 0) {
                 throw new CustomException("商品库存不足: " + goods.getGoodsName());
             }
+            // 库存耗尽自动下架(前端无法兑换)
+            fsGuCoinGoodsService.autoOffShelfIfZero(goods.getGoodsId());
 
             long goodsGuCoin = goods.getGuCoin() == null ? 0L : goods.getGuCoin();
             BigDecimal goodsCash = goods.getCash() == null ? BigDecimal.ZERO : goods.getCash();
@@ -562,6 +569,8 @@ public class FsGuCoinOrderServiceImpl implements IFsGuCoinOrderService
             if (fsGuCoinGoodsMapper.subStock(goods.getGoodsId(), 1) <= 0) {
                 throw new CustomException("商品库存不足: " + goods.getGoodsName());
             }
+            // 库存耗尽自动下架(前端无法兑换)
+            fsGuCoinGoodsService.autoOffShelfIfZero(goods.getGoodsId());
         }
 
         // 生成订单编号

+ 12 - 0
fs-service/src/main/resources/mapper/his/FsGuCoinGoodsMapper.xml

@@ -16,6 +16,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         <result property="cash"       column="cash"        />
         <result property="sort"       column="sort"        />
         <result property="stock"      column="stock"       />
+        <result property="stockWarn"  column="stock_warn"  />
         <result property="descs"      column="descs"       />
         <result property="barCode"    column="bar_code"    />
         <result property="tempId"     column="temp_id"     />
@@ -58,6 +59,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="cash != null">cash,</if>
             <if test="sort != null">sort,</if>
             <if test="stock != null">stock,</if>
+            <if test="stockWarn != null">stock_warn,</if>
             <if test="descs != null">descs,</if>
             <if test="barCode != null">bar_code,</if>
             <if test="tempId != null">temp_id,</if>
@@ -75,6 +77,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="cash != null">#{cash},</if>
             <if test="sort != null">#{sort},</if>
             <if test="stock != null">#{stock},</if>
+            <if test="stockWarn != null">#{stockWarn},</if>
             <if test="descs != null">#{descs},</if>
             <if test="barCode != null">#{barCode},</if>
             <if test="tempId != null">#{tempId},</if>
@@ -96,6 +99,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="cash != null">cash = #{cash},</if>
             <if test="sort != null">sort = #{sort},</if>
             <if test="stock != null">stock = #{stock},</if>
+            <if test="stockWarn != null">stock_warn = #{stockWarn},</if>
             <if test="descs != null">descs = #{descs},</if>
             <if test="barCode != null">bar_code = #{barCode},</if>
             <if test="tempId != null">temp_id = #{tempId},</if>
@@ -115,4 +119,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             #{goodsId}
         </foreach>
     </delete>
+
+    <!-- 库存预警列表:上架中且库存<=预警阈值 -->
+    <select id="selectWarnList" resultMap="FsGuCoinGoodsResult">
+        select * from fs_gu_coin_goods
+        where status = 1
+          and stock &lt;= stock_warn
+        order by stock asc, goods_id desc
+    </select>
 </mapper>