商品列表 结构

耗材库存 列表接口
耗材 库存预警值推送更新
cons.info.change.queue
This commit is contained in:
2026-04-10 13:59:52 +08:00
parent 7e698eee0d
commit b0f0aec94b
23 changed files with 151 additions and 125 deletions

View File

@@ -21,4 +21,6 @@ public interface ProdConsRelationMapper extends BaseMapper<ProdConsRelation> {
List<ProductBriefDTO> getProductListByConId(@Param("conId") Long conId);
List<ProdConsRelationDTO> selectListByProdId(@Param("prodId") Long prodId);
List<ProdConsRelationDTO> selectStockByProdId(@Param("prodId") Long prodId);
}

View File

@@ -15,6 +15,7 @@ import com.czg.product.param.ConsInfoParam;
import com.czg.product.param.ConsSubUnitParam;
import com.czg.product.service.ConsInfoService;
import com.czg.product.vo.ConsStatisticsVo;
import com.czg.product.vo.ConsStockRecord;
import com.czg.sa.StpKit;
import com.czg.service.product.mapper.ConsGroupMapper;
import com.czg.service.product.mapper.ConsInfoMapper;
@@ -99,6 +100,11 @@ public class ConsInfoServiceImpl extends ServiceImpl<ConsInfoMapper, ConsInfo> i
return list;
}
@Override
public List<ConsStockRecord> getConsStockList(Long shopId) {
return listAs(query().eq(ConsInfo::getShopId, shopId).eq(ConsInfo::getIsStock, SystemConstants.OneZero.ONE), ConsStockRecord.class);
}
@Override
public ConsInfoDTO getConsInfoById(Long id) {
Long shopId = StpKit.USER.getShopId(0L);
@@ -178,12 +184,10 @@ public class ConsInfoServiceImpl extends ServiceImpl<ConsInfoMapper, ConsInfo> i
@Override
public void isRefundStockConsInfo(Long id, Integer isRefundStock) {
Long shopId = StpKit.USER.getShopId();
ConsInfo entity = super.getOne(query().eq(ConsInfo::getId, id).eq(ConsInfo::getShopId, shopId));
entity.setIsRefundStock(isRefundStock);
super.updateById(entity);
}
@Override
public void modifySubUnit(ConsSubUnitParam param) {
Long shopId = StpKit.USER.getShopId(0L);

View File

@@ -3,10 +3,12 @@ package com.czg.service.product.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.czg.config.RabbitPublisher;
import com.czg.exception.CzgException;
import com.czg.product.dto.ConsStockFlowDTO;
import com.czg.product.entity.ConsInfo;
@@ -52,6 +54,8 @@ public class ConsStockFlowServiceImpl extends ServiceImpl<ConsStockFlowMapper, C
private final ProductMapper productMapper;
@Resource
private WxAccountUtil wxAccountUtil;
@Resource
private RabbitPublisher rabbitPublisher;
@Override
@Transactional(rollbackFor = Exception.class)
@@ -176,9 +180,9 @@ public class ConsStockFlowServiceImpl extends ServiceImpl<ConsStockFlowMapper, C
}
entity.setSubTotal(NumberUtil.mul(winLossNumber, param.getPrice()));
entity.setRemark(param.getRemark());
saveFlow(entity);
consInfo.setStockNumber(entity.getAfterNumber());
consInfoMapper.update(consInfo);
saveFlow(entity, consInfo.getConWarning());
}
@Override
@@ -220,9 +224,9 @@ public class ConsStockFlowServiceImpl extends ServiceImpl<ConsStockFlowMapper, C
entity.setInOutItem(InOutItemEnum.DAMAGE_OUT.value());
entity.setSubTotal(NumberUtil.mul(param.getNumber(), consInfo.getPrice()));
entity.setImgUrls(JSON.toJSONString(param.getImgUrls()));
saveFlow(entity);
consInfo.setStockNumber(entity.getAfterNumber());
consInfoMapper.update(consInfo);
saveFlow(entity, consInfo.getConWarning());
}
@Override
@@ -233,22 +237,29 @@ public class ConsStockFlowServiceImpl extends ServiceImpl<ConsStockFlowMapper, C
}
@Override
public void saveFlow(ConsStockFlow entity) {
public void saveFlow(ConsStockFlow entity, BigDecimal conWarning) {
super.save(entity);
Long shopId = entity.getShopId();
BigDecimal afterNumber = entity.getAfterNumber();
ConsInfo consInfo = consInfoMapper.selectOneById(entity.getConId());
String shopName = productMapper.getShopName(shopId);
BigDecimal conWarning = consInfo.getConWarning();
// 库存小于警告值,发送消息提醒
if (NumberUtil.isLess(afterNumber, conWarning)) {
List<String> openIdList = consInfoMapper.findOpenIdList(shopId, "con");
if (CollUtil.isEmpty(openIdList)) {
return;
}
String conName = StrUtil.format("{}数量<预警值{}", consInfo.getConName(), conWarning);
ThreadUtil.execAsync(() -> openIdList.parallelStream().forEach(openId ->
wxAccountUtil.sendStockMsg("耗材库存预警", shopName, conName, afterNumber, openId)));
boolean b = sendStockMsg(entity, conWarning);
if (b) {
ThreadUtil.execAsync(() -> rabbitPublisher.sendConsInfoChangeMsg(Convert.toStr(entity.getShopId())));
}
}
@Override
public boolean sendStockMsg(ConsStockFlow entity, BigDecimal warning) {
boolean result = false;
// 库存小于警告值,发送消息提醒
if (NumberUtil.isLess(entity.getAfterNumber(), warning)) {
result = true;
List<String> openIdList = consInfoMapper.findOpenIdList(entity.getShopId(), "con");
if (CollUtil.isEmpty(openIdList)) {
return result;
}
String shopName = productMapper.getShopName(entity.getShopId());
String conName = StrUtil.format("{}数量<预警值{}", entity.getConName(), warning);
ThreadUtil.execAsync(() -> openIdList.parallelStream().forEach(openId ->
wxAccountUtil.sendStockMsg("耗材库存预警", shopName, conName, entity.getAfterNumber(), openId)));
}
return result;
}
}

View File

@@ -80,4 +80,14 @@ public class ProdConsRelationServiceImpl extends ServiceImpl<ProdConsRelationMap
return mapper.selectListByProdId(prodId);
}
/**
* 扣减库存 使用
* @param prodId
* @return
*/
@Override
public List<ProdConsRelationDTO> selectStockByProdId(Long prodId) {
return mapper.selectStockByProdId(prodId);
}
}

View File

@@ -2,43 +2,23 @@ package com.czg.service.product.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.czg.config.RabbitPublisher;
import com.czg.constants.SystemConstants;
import com.czg.product.dto.ProductStockSubtractDTO;
import com.czg.product.entity.ConsInfo;
import com.czg.product.entity.ConsStockFlow;
import com.czg.product.entity.ProdConsRelation;
import com.czg.product.entity.Product;
import com.czg.product.enums.InOutItemEnum;
import com.czg.product.enums.InOutTypeEnum;
import com.czg.product.service.ConsStockFlowService;
import com.czg.product.service.ProductRpcService;
import com.czg.product.service.ProductService;
import com.czg.product.service.ProductStockFlowService;
import com.czg.product.vo.ProductStockVO;
import com.czg.product.vo.ProductVO;
import com.czg.service.RedisService;
import com.czg.service.product.mapper.ConsInfoMapper;
import com.czg.service.product.mapper.ProdConsRelationMapper;
import com.czg.service.product.mapper.ProductMapper;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static com.czg.constant.CacheConstant.ADMIN_CLIENT_PRODUCT_LIST;
import static com.czg.constant.CacheConstant.SHOP_PRODUCT_STOCK;
/**
* 商品RPC远程调用服务接口实现
@@ -60,44 +40,32 @@ public class ProductRpcServiceImpl implements ProductRpcService {
@Override
@Transactional(rollbackFor = Exception.class)
public void paySuccessSubtractStock(Long shopId, Long orderId, List<Map<String, Object>> dataList) {
List<ProductStockSubtractDTO> list = BeanUtil.copyToList(dataList, ProductStockSubtractDTO.class);
List<ProductStockVO> list = BeanUtil.copyToList(dataList, ProductStockVO.class);
if (CollUtil.isEmpty(list)) {
return;
}
List<ProductStockVO> productStockList = new ArrayList<>();
for (ProductStockSubtractDTO dto : list) {
productStockList.add(new ProductStockVO(dto.getProductId(), dto.getNum()));
}
productService.consStockByProduct(InOutTypeEnum.OUT, InOutItemEnum.ORDER_OUT, productStockList, orderId, "订单消费");
productService.consStockByProduct(shopId, InOutTypeEnum.OUT, InOutItemEnum.ORDER_OUT, list, orderId, "订单消费");
}
@Override
@Transactional(rollbackFor = Exception.class)
public void orderCancelRecoverStock(Long shopId, Long orderId, List<Map<String, Object>> dataList) {
List<ProductStockSubtractDTO> list = BeanUtil.copyToList(dataList, ProductStockSubtractDTO.class);
List<ProductStockVO> list = BeanUtil.copyToList(dataList, ProductStockVO.class);
if (CollUtil.isEmpty(list)) {
return;
}
List<ProductStockVO> productStockList = new ArrayList<>();
for (ProductStockSubtractDTO dto : list) {
productStockList.add(new ProductStockVO(dto.getProductId(), dto.getNum()));
}
productService.consStockByProduct(InOutTypeEnum.IN, InOutItemEnum.ORDER_IN, productStockList, orderId, "订单取消/过期返还库存");
productService.consStockByProduct(shopId, InOutTypeEnum.IN, InOutItemEnum.ORDER_IN, list, orderId, "订单取消/过期返还库存");
}
@Override
@Transactional(rollbackFor = Exception.class)
public void orderRefundReturnStock(Long shopId, Long orderId, List<Map<String, Object>> dataList) {
List<ProductStockSubtractDTO> list = BeanUtil.copyToList(dataList, ProductStockSubtractDTO.class);
List<ProductStockVO> list = BeanUtil.copyToList(dataList, ProductStockVO.class);
if (CollUtil.isEmpty(list)) {
return;
}
List<ProductStockVO> productStockList = new ArrayList<>();
for (ProductStockSubtractDTO dto : list) {
productStockList.add(new ProductStockVO(dto.getProductId(), dto.getNum()));
}
productService.consStockByProduct(InOutTypeEnum.IN, InOutItemEnum.ORDER_IN, productStockList, orderId, "订单退菜/退款返还库存");
productService.consStockByProduct(shopId, InOutTypeEnum.IN, InOutItemEnum.ORDER_IN, list, orderId, "订单退菜/退款返还库存");
}
@Override

View File

@@ -2,6 +2,8 @@ package com.czg.service.product.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
@@ -9,6 +11,7 @@ import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONWriter;
import com.czg.config.RabbitPublisher;
import com.czg.constant.CacheConstant;
import com.czg.constants.SystemConstants;
import com.czg.excel.ExcelExportUtil;
@@ -32,6 +35,7 @@ import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@@ -72,6 +76,8 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
private final ProdGroupRelationMapper prodGroupRelationMapper;
private final ProductStockFlowService productStockFlowService;
private final RedisService redisService;
@Resource
private RabbitPublisher rabbitPublisher;
private QueryWrapper buildQueryWrapper(ProductDTO param) {
QueryWrapper queryWrapper = PageUtil.buildSortQueryWrapper();
@@ -334,7 +340,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
}
@Override
public Map<String, Object> getProductCacheList(Long shopId, Long categoryId) {
public List<ProductDTO> getProductCacheList(Long shopId, Long categoryId) {
String key = ADMIN_CLIENT_PRODUCT_LIST + "::" + shopId;
List<ProductDTO> list;
if (categoryId == null) {
@@ -361,8 +367,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
.sorted(Comparator.comparingLong(ProductDTO::getId).reversed())
.toList();
}
List<ConsInfo> consInfos = consInfoMapper.selectListByQuery(query().eq(ConsInfo::getShopId, shopId).eq(ConsInfo::getStatus, SystemConstants.OneZero.ONE));
return Map.of("productList", list, "cons", consInfos);
return list;
}
/**
@@ -731,7 +736,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
productStockFlowService.save(flow);
List<ProductStockVO> productStockList = new ArrayList<>();
productStockList.add(new ProductStockVO(param.getProductId(), BigDecimal.valueOf(param.getNumber())));
consStockByProduct(InOutTypeEnum.OUT, InOutItemEnum.DAMAGE_OUT, productStockList, null, "【商品报损,自动报损相关耗材】");
consStockByProduct(shopId, InOutTypeEnum.OUT, InOutItemEnum.DAMAGE_OUT, productStockList, null, "【商品报损,自动报损相关耗材】");
}
@Override
@@ -758,10 +763,11 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
}
@Override
public void consStockByProduct(InOutTypeEnum type, InOutItemEnum item, List<ProductStockVO> products, Long orderId, String remark) {
public void consStockByProduct(Long shopId, InOutTypeEnum type, InOutItemEnum item, List<ProductStockVO> products, Long orderId, String remark) {
if (CollUtil.isEmpty(products)) return;
boolean isSendUp = false;
for (ProductStockVO product : products) {
List<ProdConsRelationDTO> consList = prodConsRelationService.selectListByProdId(product.productId());
List<ProdConsRelationDTO> consList = prodConsRelationService.selectStockByProdId(product.productId());
if (CollUtil.isEmpty(consList)) return;
for (ProdConsRelationDTO consInfo : consList) {
BigDecimal surplusStock = consInfo.getSurplusStock();
@@ -799,8 +805,13 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
flow.setOrderId(orderId);
flow.setRemark(remark);
consStockFlowService.save(flow);
boolean b = consStockFlowService.sendStockMsg(flow, consInfo.getConWarning());
if (b) isSendUp = b;
}
}
if (isSendUp) {
ThreadUtil.execAsync(() -> rabbitPublisher.sendConsInfoChangeMsg(Convert.toStr(shopId)));
}
}

View File

@@ -23,4 +23,17 @@
inner JOIN tb_cons_info t2 on t1.cons_info_id = t2.id
WHERE t1.product_id = #{prodId}
</select>
<select id="selectStockByProdId" resultType="com.czg.product.dto.ProdConsRelationDTO">
SELECT t1.*,
t2.con_unit,
t2.con_name,
t2.price,
t2.stock_number,
t2.con_warning,
t1.surplus_stock
FROM tb_prod_cons_relation t1
inner JOIN tb_cons_info t2 on t1.cons_info_id = t2.id and t2.is_stock = 1
WHERE t1.product_id = #{prodId}
</select>
</mapper>