@@ -15,6 +15,7 @@ import cn.ysk.cashier.repository.product.TbProductSkuRepository;
import cn.ysk.cashier.repository.product.TbProductStockDetailRepository ;
import cn.ysk.cashier.repository.shop.TbShopInfoRepository ;
import cn.ysk.cashier.repository.shop.TbShopUnitRepository ;
import cn.ysk.cashier.service.LogService ;
import cn.ysk.cashier.service.TbProductStockOperateService ;
import cn.ysk.cashier.service.product.StockService ;
import cn.ysk.cashier.service.product.TbProductService ;
@@ -61,6 +62,7 @@ public class StockServiceImpl implements StockService {
private final TbProductStockDetailRepository tbProductStockDetailRepository ;
private final TbProductRepository tbProductRepository ;
private final WxMsgUtils wxMsgUtils ;
private final LogService logService ;
@PersistenceContext
private EntityManager em ;
@@ -332,70 +334,153 @@ public class StockServiceImpl implements StockService {
@Override
@Transactional
public void updateProductStatus ( StockUpdateValueVO updateValueVO ) {
if ( ! " 0 " . equals ( updateValueVO . getUpdateValue ( ) ) & & ! " 1 " . equals ( updateValueVO . getUpdateValue ( ) ) ) {
throw new BadRequestException ( " 无效值 " ) ;
public void updateProductStatus ( List < StockUpdateValueVO> updateValueVOs ) {
TbProduct product = new TbProduct ( ) ;
for ( StockUpdateValueVO updateValueVO : updateValueVOs ) {
if ( ! updateValueVO . isSku ( ) ) {
if ( ! updateValueVO . isSku ( ) ) {
if ( product = = null ) {
product = tbProductRepository . getById ( Integer . valueOf ( updateValueVO . getId ( ) ) ) ;
}
productUp ( updateValueVO , product ) ;
} else {
if ( product = = null ) {
product = tbProductRepository . selectBySkuId ( Integer . valueOf ( updateValueVO . getId ( ) ) ) ;
}
productSkuUp ( updateValueVO , product ) ;
}
}
}
if ( StringUtils . isBlank ( updateValueVO . getTargetId ( ) ) ) {
throw new BadRequestException ( " 更新id不能为空 " ) ;
}
public void productUp ( StockUpdateValueVO updateValueVO , TbProduct product ) {
if ( Objects . isNull ( product ) ) {
throw new BadRequestException ( " 该商品不存在 " ) ;
}
StringBuilder sqlQuery = new StringBuilder ( " update tb_product " ) ;
switch ( updateValueVO . getUpdate Key ( ) ) {
case " pauseSaleSku " :
sqlQuery = new StringBuilder ( " update tb_product_sku set is_pause_sale = " ) .
append ( updateValueVO . getUpdateValue ( ) ) .
append ( " where id = " ) .
append ( updateValueVO . getTargetId ( ) ) .
append ( " ; " ) ;
Query nativeQuery = em . createNativeQuery ( String . valueOf ( sqlQuery ) ) ;
nativeQuery . executeUpdate ( ) ;
TbProduct product = tbProductRepository . selectBySkuId ( Integer . valueOf ( updateValueVO . getTargetId ( ) ) ) ;
TbProductSku tbProductSku = tbProductSkuRepository . findById ( Integer . valueOf ( updateValueVO . getTargetId ( ) ) ) . orElse ( null ) ;
// 推送微信操作消息
if ( product ! = null & & tbProductSku ! = null ) {
wxMsgUtils . aboardOperationMsg ( ( " 0 " . equals ( updateValueVO . getUpdateValue ( ) ) ? " 关闭售罄: " : " 开启售罄: " ) + product . getName ( ) + " / " + tbProductSku . getSpecSnap ( ) , Integer . valueOf ( updateValueVO . getShopId ( ) ) ) ;
} else {
log . warn ( " 推送微信操作消息失败, 未查询到商品信息, skuId: {} " , updateValueVO . getTargetId ( ) ) ;
StringBuilder description = new StringBuilder ( " 商品 " + product . getName ( ) ) ;
switch ( updateValueVO . getKey ( ) ) {
case " pauseSale " : //商品 暂停销售
if ( ! " 0 " . equals ( updateValueVO . getValue ( ) ) & & ! " 1 " . equals ( updateValueVO . getValue ( ) ) ) {
throw new BadRequestException ( " 无效值 " ) ;
}
return ;
case " stock " :
sqlQuery . append ( " set is_stock = " ) . append ( updateValueVO . getUpdateValue ( ) ) ;
sqlQuery . append ( " set is_pause_sale = " ) . append ( updateValueVO . getValue ( ) ) ;
description . append ( " 修改为 " + ( " 0 " . equals ( updateValueVO . getValue ( ) ) ? " 非售罄 " : " 已售罄 " ) ) ;
break ;
case " distribute " :
sqlQuery . append ( " set is_distribute = " ) . append ( updateValueVO . getUpdate Value ( ) ) ;
break ;
case " pauseSale " :
sqlQuery . append ( " set is_pause_sale = " ) . append ( updateValueVO . getUpdateValue ( ) ) ;
TbProduct product1 = tbProductRepository . getById ( Integer . valueOf ( updateValueVO . getTargetId ( ) ) ) ;
// 推送微信操作消息
if ( product1 ! = null ) {
wxMsgUtils . aboardOperationMsg ( ( " 0 " . equals ( updateValueVO . getUpdateValue ( ) ) ? " 关闭售罄: " : " 开启售罄: " ) + product1 . getName ( ) , Integer . valueOf ( updateValueVO . getShopId ( ) ) ) ;
} else {
log . warn ( " 推送微信操作消息失败, 未查询到商品信息, skuId: {} " , updateValueVO . getTargetId ( ) ) ;
case " grounding " : //上下架
if ( ! " 0 " . equals ( updateValueVO . getValue ( ) ) & & ! " 1 " . equals ( updateValueVO . getValue ( ) ) ) {
throw new BadRequestException ( " 无效值 " ) ;
}
tbProductSkuRepository . updateGroundingByProId ( product . getId ( ) . toString ( ) , Integer . valueOf ( updateValueVO . getValue ( ) ) ) ;
description . append ( " 0 " . equals ( updateValueVO . getValue ( ) ) ? " 已下架 " : " 已上架 " ) ;
sqlQuery . append ( " set is_grounding = " ) . append ( updateValueVO . getValue ( ) ) ;
break ;
case " stockNumber " : //库存
description . append ( " 库存数量修改为: " + updateValueVO . getValue ( ) + " 原库存: " + product . getStockNumber ( ) ) ;
sqlQuery . append ( " set stock_number = " ) . append ( updateValueVO . getValue ( ) ) ;
break ;
// case "stock"://库存开关
// if (!"0".equals(updateValueVO.getValue()) && !"1".equals(updateValueVO.getValue())) {
// throw new BadRequestException("无效值");
// }
// description.append("库存开关" + ("0".equals(updateValueVO.getValue()) ? "已关闭" : "已开启"));
// sqlQuery.append(" set is_stock = ").append(updateValueVO.getValue());
// break;
// case "distribute"://共享库存
// if (!"0".equals(updateValueVO.getValue()) && !"1".equals(updateValueVO.getValue())) {
// throw new BadRequestException("无效值");
// }
// description.append("库存共享 " + ("0".equals(updateValueVO.getValue()) ? "已关闭" : "已开启"));
// sqlQuery.append(" set is_distribute = ").append(updateValueVO.getValue());
// break;
default :
throw new BadRequestException ( " 无效更新类型 " ) ;
}
sqlQuery . append ( " where " ) ;
if ( StringUtils . isNotBlank ( updateValueVO . getShopId ( ) ) ) {
sqlQuery . append ( " shop_id = " ) . append ( updateValueVO . getShopId ( ) ) ;
sqlQuery . append ( " and " ) ;
}
sqlQuery . append ( " id = " ) . append ( updateValueVO . getTar getId( ) ) ;
sqlQuery . append ( " id = " ) . append ( updateValueVO . getId ( ) ) ;
sqlQuery . append ( " ; " ) ;
Query nativeQuery = em . createNativeQuery ( String . valueOf ( sqlQuery ) ) ;
nativeQuery . executeUpdate ( ) ;
if ( " pauseSale " . equals ( updateValueVO . getKey ( ) ) ) {
wxMsgUtils . aboardOperationMsg ( ( " 0 " . equals ( updateValueVO . getValue ( ) ) ? " 关闭售罄: " : " 开启售罄: " ) + product . getName ( ) ,
Integer . valueOf ( updateValueVO . getShopId ( ) ) ) ;
}
logService . save ( description . toString ( ) ,
" cn.ysk.cashier.controller.product.StockController.updateProductStatus() " ,
JSONUtil . toJSONString ( updateValueVO ) ) ;
}
public void productSkuUp ( StockUpdateValueVO updateValueVO , TbProduct product ) {
if ( Objects . isNull ( product ) ) {
throw new BadRequestException ( " 该商品不存在 " ) ;
}
TbProductSku tbProductSku = tbProductSkuRepository . findById ( Integer . valueOf ( updateValueVO . getId ( ) ) ) . orElse ( null ) ;
if ( Objects . isNull ( tbProductSku ) ) {
throw new BadRequestException ( " 该商品规格不存在 " ) ;
}
StringBuilder sqlQuery = new StringBuilder ( " update tb_product_sku " ) ;
StringBuilder description = new StringBuilder ( " 商品 " + product . getName ( ) + " 规格: " + tbProductSku . getSpecSnap ( ) ) ;
switch ( updateValueVO . getKey ( ) ) {
case " pauseSale " : //sku售罄
if ( ! " 0 " . equals ( updateValueVO . getValue ( ) ) & & ! " 1 " . equals ( updateValueVO . getValue ( ) ) ) {
throw new BadRequestException ( " 无效值 " ) ;
}
sqlQuery . append ( " set is_pause_sale = " ) . append ( updateValueVO . getValue ( ) ) ;
description . append ( " 修改为 " + ( " 0 " . equals ( updateValueVO . getValue ( ) ) ? " 非售罄 " : " 已售罄 " ) ) ;
break ;
case " grounding " : //sku上下架
if ( ! " 0 " . equals ( updateValueVO . getValue ( ) ) & & ! " 1 " . equals ( updateValueVO . getValue ( ) ) ) {
throw new BadRequestException ( " 无效值 " ) ;
}
sqlQuery . append ( " set is_grounding = " ) . append ( updateValueVO . getValue ( ) ) ;
description . append ( " 0 " . equals ( updateValueVO . getValue ( ) ) ? " 已下架 " : " 已上架 " ) ;
break ;
case " salePrice " : //价格
description . append ( " 修改价格为 " + updateValueVO . getValue ( ) ) ;
sqlQuery . append ( " set sale_price = " ) . append ( updateValueVO . getValue ( ) ) ;
if ( ( " sku " ) . equals ( product . getTypeEnum ( ) ) ) {
BigDecimal bigDecimal = tbProductSkuRepository . searchMinSalePrice ( Integer . valueOf ( tbProductSku . getProductId ( ) ) , tbProductSku . getId ( ) ) ;
if ( bigDecimal . compareTo ( new BigDecimal ( updateValueVO . getValue ( ) ) ) < 0 ) {
tbProductRepository . upLowPrice ( product . getId ( ) , bigDecimal ) ;
} else {
tbProductRepository . upLowPrice ( product . getId ( ) , new BigDecimal ( updateValueVO . getValue ( ) ) ) ;
}
} else {
tbProductRepository . upLowPrice ( product . getId ( ) , new BigDecimal ( updateValueVO . getValue ( ) ) ) ;
}
break ;
default :
throw new BadRequestException ( " 无效更新类型 " ) ;
}
sqlQuery . append ( " where " ) ;
if ( StringUtils . isNotBlank ( updateValueVO . getShopId ( ) ) ) {
sqlQuery . append ( " shop_id = " ) . append ( updateValueVO . getShopId ( ) ) ;
sqlQuery . append ( " and " ) ;
}
sqlQuery . append ( " id = " ) . append ( updateValueVO . getId ( ) ) ;
sqlQuery . append ( " ; " ) ;
Query nativeQuery = em . createNativeQuery ( String . valueOf ( sqlQuery ) ) ;
nativeQuery . executeUpdate ( ) ;
if ( " pauseSale " . equals ( updateValueVO . getKey ( ) ) ) {
// 推送微信操作消息
wxMsgUtils . aboardOperationMsg (
( " 0 " . equals ( updateValueVO . getValue ( ) ) ? " 关闭售罄: " : " 开启售罄: " ) + product . getName ( ) + " / " + tbProductSku . getSpecSnap ( ) ,
Integer . valueOf ( updateValueVO . getShopId ( ) ) ) ;
}
logService . save ( description . toString ( ) ,
" cn.ysk.cashier.controller.product.StockController.updateProductStatus() " ,
JSONUtil . toJSONString ( updateValueVO ) ) ;
}
@Override
public void updateProductWarnLine ( StockUpdateWarnLineVO stockUpdateWarnLineVO ) {
TbShopInfo shopInfo = tbShopInfoRepository . findById ( Integer . valueOf ( stockUpdateWarnLineVO . getShopId ( ) ) ) . orElse ( null ) ;