砍价活动,库存逻辑处理。
This commit is contained in:
parent
1b97836bea
commit
3ca6dcbec9
@ -34,6 +34,7 @@ import com.suisung.mall.shop.activity.service.ShopActivityCutpriceService;
|
|||||||
import com.suisung.mall.shop.activity.service.ShopActivityGroupbookingHistoryService;
|
import com.suisung.mall.shop.activity.service.ShopActivityGroupbookingHistoryService;
|
||||||
import com.suisung.mall.shop.activity.service.ShopActivityGroupbookingService;
|
import com.suisung.mall.shop.activity.service.ShopActivityGroupbookingService;
|
||||||
import com.suisung.mall.shop.base.service.AccountBaseConfigService;
|
import com.suisung.mall.shop.base.service.AccountBaseConfigService;
|
||||||
|
import com.suisung.mall.shop.order.service.ShopOrderInfoService;
|
||||||
import com.suisung.mall.shop.store.service.ShopStoreActivityBaseService;
|
import com.suisung.mall.shop.store.service.ShopStoreActivityBaseService;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -74,6 +75,10 @@ public class ShopActivityCutpriceServiceImpl extends BaseServiceImpl<ShopActivit
|
|||||||
@Autowired
|
@Autowired
|
||||||
private ShopStoreActivityBaseService shopStoreActivityBaseService;
|
private ShopStoreActivityBaseService shopStoreActivityBaseService;
|
||||||
|
|
||||||
|
@Lazy
|
||||||
|
@Autowired
|
||||||
|
private ShopOrderInfoService shopOrderInfoService;
|
||||||
|
|
||||||
@Lazy
|
@Lazy
|
||||||
@Autowired
|
@Autowired
|
||||||
private AccountService accountService;
|
private AccountService accountService;
|
||||||
@ -300,6 +305,7 @@ public class ShopActivityCutpriceServiceImpl extends BaseServiceImpl<ShopActivit
|
|||||||
queryWrapper.eq("activity_id", activity_id).eq("user_id", user_id);
|
queryWrapper.eq("activity_id", activity_id).eq("user_id", user_id);
|
||||||
ShopActivityCutprice cutprice_row = findOne(queryWrapper);
|
ShopActivityCutprice cutprice_row = findOne(queryWrapper);
|
||||||
|
|
||||||
|
// 立即参与活动?
|
||||||
boolean is_join_activity = false;
|
boolean is_join_activity = false;
|
||||||
|
|
||||||
// 如果用户未参与该砍价活动,则创建新的砍价记录
|
// 如果用户未参与该砍价活动,则创建新的砍价记录
|
||||||
@ -307,6 +313,8 @@ public class ShopActivityCutpriceServiceImpl extends BaseServiceImpl<ShopActivit
|
|||||||
// 需要检查活动有效期和活动商品库存是否足够
|
// 需要检查活动有效期和活动商品库存是否足够
|
||||||
Pair<Boolean, String> check_result = checkCutPriceExpiredAndStock(activityBase);
|
Pair<Boolean, String> check_result = checkCutPriceExpiredAndStock(activityBase);
|
||||||
if (!check_result.getFirst()) {
|
if (!check_result.getFirst()) {
|
||||||
|
// 库存不够,立即更改活动状态为已结束
|
||||||
|
shopStoreActivityBaseService.updateActivityState(activity_id, StateCode.ACTIVITY_STATE_FINISHED);
|
||||||
throw new ApiException(check_result.getSecond());
|
throw new ApiException(check_result.getSecond());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -441,6 +449,15 @@ public class ShopActivityCutpriceServiceImpl extends BaseServiceImpl<ShopActivit
|
|||||||
updateCutPriceState(cutprice.getAc_id(), null, CommonConstant.CutPrice_Order_State_Expired);
|
updateCutPriceState(cutprice.getAc_id(), null, CommonConstant.CutPrice_Order_State_Expired);
|
||||||
return CommonResult.failed(I18nUtil._("活动已结束!"));
|
return CommonResult.failed(I18nUtil._("活动已结束!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 需要检查活动有效期和活动商品库存是否足够
|
||||||
|
Pair<Boolean, String> check_result = checkCutPriceExpiredAndStock(activityBase);
|
||||||
|
if (!check_result.getFirst()) {
|
||||||
|
// 库存不够,立即更改活动状态为已结束
|
||||||
|
shopStoreActivityBaseService.updateActivityState(cutprice.getActivity_id(), StateCode.ACTIVITY_STATE_FINISHED);
|
||||||
|
throw new ApiException(check_result.getSecond());
|
||||||
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return CommonResult.failed(I18nUtil._("系统繁忙,请稍后再试!"));
|
return CommonResult.failed(I18nUtil._("系统繁忙,请稍后再试!"));
|
||||||
}
|
}
|
||||||
@ -651,6 +668,16 @@ public class ShopActivityCutpriceServiceImpl extends BaseServiceImpl<ShopActivit
|
|||||||
return Pair.of(false, I18nUtil._("活动信息不能为空"));
|
return Pair.of(false, I18nUtil._("活动信息不能为空"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (shopStoreActivityBase.getActivity_id() == null || shopStoreActivityBase.getActivity_id() <= 0) {
|
||||||
|
return Pair.of(false, I18nUtil._("活动ID无效"));
|
||||||
|
}
|
||||||
|
|
||||||
|
//activity_state 活动状态(ENUM):0-未开启;1-正常;2-已结束;3-管理员关闭;4-商家关闭
|
||||||
|
if (shopStoreActivityBase.getActivity_state() != null
|
||||||
|
&& !CommonConstant.Enable.equals(shopStoreActivityBase.getActivity_state())) {
|
||||||
|
return Pair.of(false, I18nUtil._("活动已结束,下次再参与!"));
|
||||||
|
}
|
||||||
|
|
||||||
// 检查活动是否过期
|
// 检查活动是否过期
|
||||||
boolean isActivityTimeValid = shopStoreActivityBaseService.isActivityTimeValid(
|
boolean isActivityTimeValid = shopStoreActivityBaseService.isActivityTimeValid(
|
||||||
shopStoreActivityBase.getActivity_starttime(),
|
shopStoreActivityBase.getActivity_starttime(),
|
||||||
@ -658,28 +685,28 @@ public class ShopActivityCutpriceServiceImpl extends BaseServiceImpl<ShopActivit
|
|||||||
new Date()
|
new Date()
|
||||||
);
|
);
|
||||||
if (!isActivityTimeValid) {
|
if (!isActivityTimeValid) {
|
||||||
return Pair.of(false, I18nUtil._("该活动已过期,下次早点来!"));
|
return Pair.of(false, I18nUtil._("该活动已过期,下次再参与!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查活动商品库存
|
// 检查活动商品库存
|
||||||
Integer productCount = shopStoreActivityBase.getProduct_count();
|
Integer productCount = shopStoreActivityBase.getProduct_count();
|
||||||
if (CheckUtil.isEmpty(productCount) || productCount <= 0) {
|
if (CheckUtil.isEmpty(productCount) || productCount <= 0) {
|
||||||
return Pair.of(false, I18nUtil._("活动商品库存不足,请稍后再来。"));
|
return Pair.of(false, I18nUtil._("活动商品库存未设置,请联系店长!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查询已占用库存的砍价订单数量
|
// 查询已占用库存的砍价订单数量
|
||||||
QueryWrapper<ShopActivityCutprice> queryWrapper = new QueryWrapper<>();
|
// QueryWrapper<ShopActivityCutprice> queryWrapper = new QueryWrapper<>();
|
||||||
queryWrapper.eq("activity_id", shopStoreActivityBase.getActivity_id())
|
// queryWrapper.eq("activity_id", shopStoreActivityBase.getActivity_id())
|
||||||
.in("state", Arrays.asList(
|
// .isNotNull("order_id").likeRight("order_id", "DD_")
|
||||||
CommonConstant.CutPrice_Order_State_Finished,
|
// .eq("state", CommonConstant.CutPrice_Order_State_Finished);
|
||||||
CommonConstant.CutPrice_Order_State_CutFinished,
|
// long recordCount = count(queryWrapper);
|
||||||
CommonConstant.CutPrice_Order_State_ING
|
|
||||||
));
|
// 实时查询活动的订单数量
|
||||||
long recordCount = count(queryWrapper);
|
long recordCount = shopOrderInfoService.fetchActivityOrderSuccessCount(Convert.toStr(shopStoreActivityBase.getActivity_id()), Convert.toStr(StateCode.ACTIVITY_TYPE_CUTPRICE));
|
||||||
|
|
||||||
// 判断库存是否充足(当已占用库存大于等于总库存时,表示库存不足)
|
// 判断库存是否充足(当已占用库存大于等于总库存时,表示库存不足)
|
||||||
if (recordCount >= productCount) {
|
if (recordCount >= productCount) {
|
||||||
return Pair.of(false, I18nUtil._("活动商品库存不足,请稍后再来。"));
|
return Pair.of(false, I18nUtil._("活动商品库存不足,请稍后再参与!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 所有检查通过
|
// 所有检查通过
|
||||||
|
|||||||
@ -143,4 +143,13 @@ public interface ShopOrderInfoService extends IBaseService<ShopOrderInfo> {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
List<BookingArgDTO> genBookingOrderArgList(String storeId);
|
List<BookingArgDTO> genBookingOrderArgList(String storeId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取某个活动订单成功数量
|
||||||
|
*
|
||||||
|
* @param activityId 活动ID
|
||||||
|
* @param activityTypeId 活动类型ID, 可选参数
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
long fetchActivityOrderSuccessCount(String activityId, String activityTypeId);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,6 +7,7 @@ import cn.hutool.core.util.NumberUtil;
|
|||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.json.JSONObject;
|
import cn.hutool.json.JSONObject;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||||
import com.suisung.mall.common.api.CommonResult;
|
import com.suisung.mall.common.api.CommonResult;
|
||||||
@ -1231,6 +1232,49 @@ public class ShopOrderInfoServiceImpl extends BaseServiceImpl<ShopOrderInfoMappe
|
|||||||
return bookingArgList;
|
return bookingArgList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取某个活动订单成功数量
|
||||||
|
*
|
||||||
|
* @param activityId 活动ID
|
||||||
|
* @param activityTypeId 活动类型ID, 可选参数
|
||||||
|
* @return 活动订单成功数量
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public long fetchActivityOrderSuccessCount(String activityId, String activityTypeId) {
|
||||||
|
// 参数校验
|
||||||
|
if (StrUtil.isBlank(activityId)) {
|
||||||
|
logger.warn("活动Id无效!");
|
||||||
|
return 0L;
|
||||||
|
}
|
||||||
|
|
||||||
|
LambdaQueryWrapper<ShopOrderInfo> queryWrapper = new LambdaQueryWrapper<>();
|
||||||
|
queryWrapper.eq(ShopOrderInfo::getActivity_id, activityId);
|
||||||
|
|
||||||
|
// 如果activityTypeId不为空,则添加条件
|
||||||
|
if (StrUtil.isNotBlank(activityTypeId)) {
|
||||||
|
queryWrapper.eq(ShopOrderInfo::getActivity_type_id, activityTypeId);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加订单状态条件
|
||||||
|
List<Integer> successOrderStates = Arrays.asList(
|
||||||
|
StateCode.ORDER_STATE_WAIT_PAID,
|
||||||
|
StateCode.ORDER_STATE_PICKING,
|
||||||
|
StateCode.ORDER_STATE_WAIT_SHIPPING,
|
||||||
|
StateCode.ORDER_STATE_SHIPPED,
|
||||||
|
StateCode.ORDER_STATE_RECEIVED,
|
||||||
|
StateCode.ORDER_STATE_FINISH);
|
||||||
|
|
||||||
|
queryWrapper.in(ShopOrderInfo::getOrder_state_id, successOrderStates);
|
||||||
|
|
||||||
|
try {
|
||||||
|
return count(queryWrapper);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("获取活动订单出错, activityId: {}, activityTypeId: {}",
|
||||||
|
activityId, activityTypeId, e);
|
||||||
|
return 0L;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据一个或多个店铺id获取有效店铺的有效营业时间段
|
* 根据一个或多个店铺id获取有效店铺的有效营业时间段
|
||||||
|
|||||||
@ -199,4 +199,14 @@ public interface ShopStoreActivityBaseService extends IBaseService<ShopStoreActi
|
|||||||
Boolean isCutPriceActivity(Integer activity_id);
|
Boolean isCutPriceActivity(Integer activity_id);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改活动状态
|
||||||
|
*
|
||||||
|
* @param activity_id
|
||||||
|
* @param activity_state
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
boolean updateActivityState(Integer activity_id, Integer activity_state);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4433,6 +4433,46 @@ public class ShopStoreActivityBaseServiceImpl extends BaseServiceImpl<ShopStoreA
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改活动状态
|
||||||
|
*
|
||||||
|
* @param activity_id 活动ID
|
||||||
|
* @param activity_state 活动状态
|
||||||
|
* @return boolean 是否更新成功
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean updateActivityState(Integer activity_id, Integer activity_state) {
|
||||||
|
// 参数校验
|
||||||
|
if (activity_id == null || activity_id <= 0) {
|
||||||
|
logger.warn("活动ID无效: {}", activity_id);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (activity_state == null) {
|
||||||
|
logger.warn("活动状态不能为空: activity_id={}", activity_id);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 直接使用LambdaUpdateWrapper提高性能,避免创建额外实体对象
|
||||||
|
boolean result = lambdaUpdate()
|
||||||
|
.eq(ShopStoreActivityBase::getActivity_id, activity_id)
|
||||||
|
.set(ShopStoreActivityBase::getActivity_state, activity_state)
|
||||||
|
.update();
|
||||||
|
|
||||||
|
if (result) {
|
||||||
|
logger.info("活动状态更新成功,活动ID: {},新状态: {}", activity_id, activity_state);
|
||||||
|
} else {
|
||||||
|
logger.warn("活动状态更新失败,活动ID: {},状态: {}", activity_id, activity_state);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("活动状态更新异常,活动ID: {},状态: {}", activity_id, activity_state, e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public boolean removeActivityBase(Integer activity_id, ShopStoreActivityBase activity_row) {
|
public boolean removeActivityBase(Integer activity_id, ShopStoreActivityBase activity_row) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user