砍价逻辑修改
This commit is contained in:
parent
2e7612ac93
commit
19756ee7e3
@ -137,10 +137,14 @@ public class CommonConstant {
|
|||||||
//3-开业(活动)筹备中;
|
//3-开业(活动)筹备中;
|
||||||
public final static Integer Store_Biz_State_PreActivity = 3;
|
public final static Integer Store_Biz_State_PreActivity = 3;
|
||||||
|
|
||||||
//用户砍价订单状态:1-砍价已完成下单;2-砍价未下单已取消;3-砍价助力进行中;4-砍价过期失效;6-砍价助力已完成待下单;
|
//用户砍价订单状态:1-砍价已完成下单;
|
||||||
public static final Integer CutPrice_Order_State_Finished = 1;
|
public static final Integer CutPrice_Order_State_Finished = 1;
|
||||||
|
//2-砍价未下单已取消;
|
||||||
public static final Integer CutPrice_Order_State_Canceled = 2;
|
public static final Integer CutPrice_Order_State_Canceled = 2;
|
||||||
|
//3-砍价助力进行中;
|
||||||
public static final Integer CutPrice_Order_State_ING = 3;
|
public static final Integer CutPrice_Order_State_ING = 3;
|
||||||
|
//4-砍价过期失效;
|
||||||
public static final Integer CutPrice_Order_State_Expired = 4;
|
public static final Integer CutPrice_Order_State_Expired = 4;
|
||||||
|
//6-砍价助力已完成待下单;
|
||||||
public static final Integer CutPrice_Order_State_CutFinished = 6;
|
public static final Integer CutPrice_Order_State_CutFinished = 6;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -66,4 +66,7 @@ public class ShopActivityCutprice implements Serializable {
|
|||||||
@ApiModelProperty(value = "乐观锁")
|
@ApiModelProperty(value = "乐观锁")
|
||||||
private Integer version;
|
private Integer version;
|
||||||
|
|
||||||
|
@ApiModelProperty(value = "更新时间")
|
||||||
|
private Date updated_at;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -49,4 +49,12 @@ public interface ShopActivityCutpriceService extends IBaseService<ShopActivityCu
|
|||||||
Pair<Boolean, String> checkCutPriceExpiredAndStock(ShopStoreActivityBase shopStoreActivityBase);
|
Pair<Boolean, String> checkCutPriceExpiredAndStock(ShopStoreActivityBase shopStoreActivityBase);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改某个砍价订单状态
|
||||||
|
*
|
||||||
|
* @param ac_id 活动自增Id
|
||||||
|
* @param state 状态
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
Boolean updateCutPriceState(Integer ac_id, Integer state);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,6 +10,7 @@ import cn.hutool.core.util.StrUtil;
|
|||||||
import cn.hutool.json.JSONObject;
|
import cn.hutool.json.JSONObject;
|
||||||
import cn.hutool.json.JSONUtil;
|
import cn.hutool.json.JSONUtil;
|
||||||
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.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.suisung.mall.common.api.CommonResult;
|
import com.suisung.mall.common.api.CommonResult;
|
||||||
import com.suisung.mall.common.api.StateCode;
|
import com.suisung.mall.common.api.StateCode;
|
||||||
@ -259,6 +260,7 @@ public class ShopActivityCutpriceServiceImpl extends BaseServiceImpl<ShopActivit
|
|||||||
return toMobileResult(cutpricePage);
|
return toMobileResult(cutpricePage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 砍价活动详情(注:除了获取活动详情信息,还创建砍价订单记录和查询砍价历史历史)
|
* 砍价活动详情(注:除了获取活动详情信息,还创建砍价订单记录和查询砍价历史历史)
|
||||||
*
|
*
|
||||||
@ -301,8 +303,12 @@ public class ShopActivityCutpriceServiceImpl extends BaseServiceImpl<ShopActivit
|
|||||||
boolean is_join_activity = false;
|
boolean is_join_activity = false;
|
||||||
|
|
||||||
// 如果用户未参与该砍价活动,则创建新的砍价记录
|
// 如果用户未参与该砍价活动,则创建新的砍价记录
|
||||||
|
if (cutprice_row == null) {
|
||||||
// 需要检查活动有效期和活动商品库存是否足够
|
// 需要检查活动有效期和活动商品库存是否足够
|
||||||
if (cutprice_row == null && shopStoreActivityBaseService.isActivityTimeValid(activityBase, new Date())) {
|
Pair<Boolean, String> check_result = checkCutPriceExpiredAndStock(activityBase);
|
||||||
|
if (!check_result.getFirst()) {
|
||||||
|
throw new ApiException(check_result.getSecond());
|
||||||
|
}
|
||||||
|
|
||||||
cutprice_row = new ShopActivityCutprice();
|
cutprice_row = new ShopActivityCutprice();
|
||||||
|
|
||||||
@ -378,87 +384,114 @@ public class ShopActivityCutpriceServiceImpl extends BaseServiceImpl<ShopActivit
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 砍价
|
* 自己或朋友砍价
|
||||||
*
|
*
|
||||||
* @param ac_id 活动 Id
|
* @param ac_id 活动 Id
|
||||||
* @param user_id 砍价用户
|
* @param user_id 砍价用户
|
||||||
* @return
|
* @return CommonResult 砍价结果
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
public CommonResult doCutPrice(Integer ac_id, Integer user_id) {
|
public CommonResult doCutPrice(Integer ac_id, Integer user_id) {
|
||||||
// 参数校验
|
// 参数校验
|
||||||
if (ac_id == null || ac_id <= 0) {
|
if (ac_id == null || ac_id <= 0) {
|
||||||
throw new ApiException(I18nUtil._("活动ID无效!"));
|
return CommonResult.failed(I18nUtil._("活动ID无效!"));
|
||||||
}
|
}
|
||||||
if (user_id == null || user_id <= 0) {
|
if (user_id == null || user_id <= 0) {
|
||||||
throw new ApiException(I18nUtil._("用户ID无效!"));
|
return CommonResult.failed(I18nUtil._("用户ID无效!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取砍价记录
|
// 获取砍价记录
|
||||||
ShopActivityCutprice cutprice_row = get(ac_id);
|
ShopActivityCutprice shopActivityCutprice = get(ac_id);
|
||||||
if (cutprice_row == null) {
|
if (shopActivityCutprice == null) {
|
||||||
throw new ApiException(I18nUtil._("未找到砍价记录!"));
|
return CommonResult.failed(I18nUtil._("抱歉,砍价记录已失效!"));
|
||||||
|
}
|
||||||
|
|
||||||
|
Integer activity_id = shopActivityCutprice.getActivity_id();
|
||||||
|
// 检查活动状态是否正常
|
||||||
|
ShopStoreActivityBase shopStoreActivityBase = shopStoreActivityBaseService.get(shopActivityCutprice.getActivity_id());
|
||||||
|
if (shopStoreActivityBase == null) {
|
||||||
|
// 活动不存在,更新砍价订单状态为已取消
|
||||||
|
updateCutPriceState(shopActivityCutprice.getAc_id(), CommonConstant.CutPrice_Order_State_Canceled);
|
||||||
|
return CommonResult.failed(I18nUtil._("抱歉,砍价活动已失效!"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 活动状态不是正常状态
|
||||||
|
if (!ObjectUtil.equal(shopStoreActivityBase.getActivity_state(), StateCode.ACTIVITY_STATE_NORMAL)) {
|
||||||
|
// 如果活动已结束或已关闭,更新砍价订单状态为已取消
|
||||||
|
if (ObjectUtil.equal(shopStoreActivityBase.getActivity_state(), StateCode.ACTIVITY_STATE_FINISHED)
|
||||||
|
|| ObjectUtil.equal(shopStoreActivityBase.getActivity_state(), StateCode.ACTIVITY_STATE_CLOSED)) {
|
||||||
|
updateCutPriceState(shopActivityCutprice.getAc_id(), CommonConstant.CutPrice_Order_State_Canceled);
|
||||||
|
}
|
||||||
|
return CommonResult.failed(I18nUtil._("抱歉,砍价活动已失效!"));
|
||||||
|
}
|
||||||
|
|
||||||
|
Date now = new Date();
|
||||||
|
Long expired_at = shopActivityCutprice.getExpired_at();
|
||||||
|
|
||||||
|
// 检查砍价订单是否过期
|
||||||
|
if (CheckUtil.isNotEmpty(expired_at) && expired_at < now.getTime()) {
|
||||||
|
// 砍价订单已过期,更新状态为已过期
|
||||||
|
updateCutPriceState(shopActivityCutprice.getAc_id(), CommonConstant.CutPrice_Order_State_Expired);
|
||||||
|
return CommonResult.failed(I18nUtil._("砍价已过期,下次早点来!"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查活动是否已结束(活动时间已过)
|
||||||
|
if (!shopStoreActivityBaseService.isActivityTimeValid(shopStoreActivityBase, now)) {
|
||||||
|
// 活动已结束,更新砍价订单状态为已过期
|
||||||
|
updateCutPriceState(shopActivityCutprice.getAc_id(), CommonConstant.CutPrice_Order_State_Expired);
|
||||||
|
return CommonResult.failed(I18nUtil._("砍价已过期,下次早点来!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否是帮别人砍价且次数已用完
|
// 检查是否是帮别人砍价且次数已用完
|
||||||
if (!cutprice_row.getUser_id().equals(user_id)) {
|
if (!shopActivityCutprice.getUser_id().equals(user_id)) {
|
||||||
DateTime today = DateUtil.beginOfDay(new Date());
|
DateTime today = DateUtil.beginOfDay(new Date());
|
||||||
Integer num = accountBaseConfigService.getConfig("user_cutprice_num", 0);
|
Integer num = accountBaseConfigService.getConfig("user_cutprice_num", 0);
|
||||||
Integer cut_num = Convert.toInt(redisService.hGet("cutprice-" + today, user_id.toString()), 0);
|
Integer cut_num = Convert.toInt(redisService.hGet("cutprice-" + today, user_id.toString()), 0);
|
||||||
|
|
||||||
if (num > 0 && cut_num >= num) {
|
if (num > 0 && cut_num >= num) {
|
||||||
throw new ApiException(I18nUtil._("今日帮砍次数已用完!"));
|
return CommonResult.failed(I18nUtil._("今日帮砍次数已用完!"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BigDecimal ac_sale_price = cutprice_row.getAc_sale_price();
|
BigDecimal ac_sale_price = shopActivityCutprice.getAc_sale_price();
|
||||||
BigDecimal ac_mix_limit_price = cutprice_row.getAc_mix_limit_price();
|
BigDecimal ac_mix_limit_price = shopActivityCutprice.getAc_mix_limit_price();
|
||||||
|
|
||||||
|
// 检查价格数据是否完整
|
||||||
|
if (ac_sale_price == null || ac_mix_limit_price == null) {
|
||||||
|
return CommonResult.failed(I18nUtil._("价格数据异常!"));
|
||||||
|
}
|
||||||
|
|
||||||
// 检查是否已达到最低价
|
// 检查是否已达到最低价
|
||||||
if (ac_sale_price == null || ac_mix_limit_price == null) {
|
if (NumberUtil.isLessOrEqual(ac_sale_price, ac_mix_limit_price)) {
|
||||||
throw new ApiException(I18nUtil._("价格数据异常!"));
|
return CommonResult.failed(I18nUtil._("已达到最低价!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ObjectUtil.compare(ac_sale_price, ac_mix_limit_price) <= 0) {
|
|
||||||
throw new ApiException(I18nUtil._("已达到最低价!"));
|
|
||||||
}
|
|
||||||
|
|
||||||
Integer activity_id = cutprice_row.getActivity_id();
|
// 检查是否已经帮过好友砍了价
|
||||||
ShopStoreActivityBase storeActivityBase = shopStoreActivityBaseService.get(activity_id);
|
|
||||||
if (storeActivityBase == null) {
|
|
||||||
throw new ApiException(I18nUtil._("未找到活动信息!"));
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查活动是否有效
|
|
||||||
if (!shopStoreActivityBaseService.isActivityTimeValid(Convert.toMap(String.class, Object.class, storeActivityBase))) {
|
|
||||||
throw new ApiException(I18nUtil._("该活动不存在或已过期!"));
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查是否已经帮过好友砍了价?
|
|
||||||
QueryWrapper<ShopActivityCutpriceHistory> queryWrapper = new QueryWrapper<>();
|
QueryWrapper<ShopActivityCutpriceHistory> queryWrapper = new QueryWrapper<>();
|
||||||
queryWrapper.eq("user_id", user_id).eq("ac_id", ac_id);
|
queryWrapper.eq("user_id", user_id).eq("ac_id", ac_id);
|
||||||
long ach_num = shopActivityCutpriceHistoryService.count(queryWrapper);
|
long ach_num = shopActivityCutpriceHistoryService.count(queryWrapper);
|
||||||
if (ach_num > 0) {
|
if (ach_num > 0) {
|
||||||
throw new ApiException(I18nUtil._("已经帮过好友砍了价!"));
|
return CommonResult.failed(I18nUtil._("已经帮好友砍过价!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算砍价金额
|
// 计算砍价金额
|
||||||
BigDecimal cut_price = shopStoreActivityBaseService.getCutDownPrice(storeActivityBase, cutprice_row);
|
BigDecimal cut_price = shopStoreActivityBaseService.getCutDownPrice(shopStoreActivityBase, shopActivityCutprice);
|
||||||
if (cut_price == null || cut_price.compareTo(BigDecimal.ZERO) <= 0) {
|
if (cut_price == null || cut_price.compareTo(BigDecimal.ZERO) <= 0) {
|
||||||
throw new ApiException(I18nUtil._("砍价失败!"));
|
return CommonResult.failed(I18nUtil._("砍价失败!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取商品ID
|
// 获取商品ID
|
||||||
String str_activity_rule = storeActivityBase.getActivity_rule();
|
String str_activity_rule = shopStoreActivityBase.getActivity_rule();
|
||||||
if (StrUtil.isBlank(str_activity_rule)) {
|
if (StrUtil.isBlank(str_activity_rule)) {
|
||||||
throw new ApiException(I18nUtil._("活动规则数据异常!"));
|
return CommonResult.failed(I18nUtil._("活动规则数据异常!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
JSONObject activity_rule = JSONUtil.parseObj(str_activity_rule);
|
JSONObject activity_rule = JSONUtil.parseObj(str_activity_rule);
|
||||||
Long item_id = activity_rule.get("item_id", Long.class);
|
Long item_id = activity_rule.get("item_id", Long.class);
|
||||||
if (item_id == null) {
|
if (item_id == null) {
|
||||||
throw new ApiException(I18nUtil._("未找到商品信息!"));
|
return CommonResult.failed(I18nUtil._("未找到商品信息!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建砍价历史记录
|
// 创建砍价历史记录
|
||||||
@ -476,14 +509,14 @@ public class ShopActivityCutpriceServiceImpl extends BaseServiceImpl<ShopActivit
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 更新砍价信息
|
// 更新砍价信息
|
||||||
cutprice_row.setAc_sale_price(NumberUtil.sub(ac_sale_price, cut_price));
|
shopActivityCutprice.setAc_sale_price(NumberUtil.sub(ac_sale_price, cut_price));
|
||||||
cutprice_row.setAc_num(cutprice_row.getAc_num() + 1);
|
shopActivityCutprice.setAc_num(shopActivityCutprice.getAc_num() + 1);
|
||||||
if (!edit(cutprice_row)) {
|
if (!edit(shopActivityCutprice)) {
|
||||||
throw new ApiException(I18nUtil._("更新砍价信息失败!"));
|
throw new ApiException(I18nUtil._("更新砍价信息失败!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新帮砍次数(如果不是自己砍自己的话)
|
// 更新帮砍次数(如果不是自己砍自己的话)
|
||||||
if (!cutprice_row.getUser_id().equals(user_id)) {
|
if (!shopActivityCutprice.getUser_id().equals(user_id)) {
|
||||||
DateTime today = DateUtil.beginOfDay(new Date());
|
DateTime today = DateUtil.beginOfDay(new Date());
|
||||||
Integer cut_num = Convert.toInt(redisService.hGet("cutprice-" + today, user_id.toString()), 0);
|
Integer cut_num = Convert.toInt(redisService.hGet("cutprice-" + today, user_id.toString()), 0);
|
||||||
redisService.hSet("cutprice-" + today, user_id.toString(), cut_num + 1, 24 * 60 * 60 * 1000);
|
redisService.hSet("cutprice-" + today, user_id.toString(), cut_num + 1, 24 * 60 * 60 * 1000);
|
||||||
@ -492,6 +525,7 @@ public class ShopActivityCutpriceServiceImpl extends BaseServiceImpl<ShopActivit
|
|||||||
return CommonResult.success(ach_data);
|
return CommonResult.success(ach_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 砍价活动是否符合立即下单了?
|
* 砍价活动是否符合立即下单了?
|
||||||
*
|
*
|
||||||
@ -577,14 +611,73 @@ public class ShopActivityCutpriceServiceImpl extends BaseServiceImpl<ShopActivit
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Pair<Boolean, String> checkCutPriceExpiredAndStock(ShopStoreActivityBase shopStoreActivityBase) {
|
public Pair<Boolean, String> checkCutPriceExpiredAndStock(ShopStoreActivityBase shopStoreActivityBase) {
|
||||||
|
// 参数校验
|
||||||
|
if (shopStoreActivityBase == null) {
|
||||||
|
return Pair.of(false, I18nUtil._("活动信息不能为空"));
|
||||||
|
}
|
||||||
|
|
||||||
boolean isActivityTimeValid = shopStoreActivityBaseService.isActivityTimeValid(shopStoreActivityBase.getActivity_starttime(), shopStoreActivityBase.getActivity_endtime(), new Date());
|
// 检查活动是否过期
|
||||||
|
boolean isActivityTimeValid = shopStoreActivityBaseService.isActivityTimeValid(
|
||||||
|
shopStoreActivityBase.getActivity_starttime(),
|
||||||
|
shopStoreActivityBase.getActivity_endtime(),
|
||||||
|
new Date()
|
||||||
|
);
|
||||||
if (!isActivityTimeValid) {
|
if (!isActivityTimeValid) {
|
||||||
return Pair.of(false, I18nUtil._("该活动已过期,下次早点来。"));
|
return Pair.of(false, I18nUtil._("该活动已过期,下次早点来!"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查活动商品库存
|
||||||
|
Integer productCount = shopStoreActivityBase.getProduct_count();
|
||||||
|
if (CheckUtil.isEmpty(productCount) || productCount <= 0) {
|
||||||
|
return Pair.of(false, I18nUtil._("活动商品库存不足,请稍后再来。"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查询已占用库存的砍价订单数量
|
||||||
|
QueryWrapper<ShopActivityCutprice> queryWrapper = new QueryWrapper<>();
|
||||||
|
queryWrapper.eq("activity_id", shopStoreActivityBase.getActivity_id())
|
||||||
|
.in("state", Arrays.asList(
|
||||||
|
CommonConstant.CutPrice_Order_State_Finished,
|
||||||
|
CommonConstant.CutPrice_Order_State_CutFinished,
|
||||||
|
CommonConstant.CutPrice_Order_State_ING
|
||||||
|
));
|
||||||
|
long recordCount = count(queryWrapper);
|
||||||
|
|
||||||
|
// 判断库存是否充足(当已占用库存大于等于总库存时,表示库存不足)
|
||||||
|
if (recordCount >= productCount) {
|
||||||
|
return Pair.of(false, I18nUtil._("活动商品库存不足,请稍后再来。"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 所有检查通过
|
||||||
|
return Pair.of(true, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return null;
|
/**
|
||||||
|
* 修改某个砍价订单状态
|
||||||
|
*
|
||||||
|
* @param ac_id 活动自增Id
|
||||||
|
* @param state 状态
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Boolean updateCutPriceState(Integer ac_id, Integer state) {
|
||||||
|
// 参数校验
|
||||||
|
if (ac_id == null || ac_id <= 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (state == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
UpdateWrapper<ShopActivityCutprice> updateWrapper = new UpdateWrapper<>();
|
||||||
|
updateWrapper.eq("ac_id", ac_id);
|
||||||
|
updateWrapper.set("state", state);
|
||||||
|
return update(updateWrapper);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("更新砍价订单状态失败,ac_id={}, state={}", ac_id, state, e);
|
||||||
|
throw new ApiException(I18nUtil._("更新砍价订单状态失败"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user