|
@@ -2,12 +2,15 @@ package com.yonge.cooleshow.biz.dal.service.impl;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.extension.api.R;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.yonge.cooleshow.auth.api.entity.SysUser;
|
|
|
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
|
|
|
import com.yonge.cooleshow.biz.dal.dao.ActivityPlanDao;
|
|
|
import com.yonge.cooleshow.biz.dal.dao.ActivityPlanEvaluationDao;
|
|
|
+import com.yonge.cooleshow.biz.dal.dao.ActivityRewardChangeStockDao;
|
|
|
import com.yonge.cooleshow.biz.dal.dto.ActivityPlanDto;
|
|
|
+import com.yonge.cooleshow.biz.dal.dto.ActivityPlanRewardDto;
|
|
|
import com.yonge.cooleshow.biz.dal.dto.req.OrderPayReq;
|
|
|
import com.yonge.cooleshow.biz.dal.dto.search.OrderSearch;
|
|
|
import com.yonge.cooleshow.biz.dal.entity.ActivityPlanEvaluation;
|
|
@@ -17,14 +20,18 @@ import com.yonge.cooleshow.biz.dal.dto.search.ActivityPlanSearch;
|
|
|
import com.yonge.cooleshow.biz.dal.entity.*;
|
|
|
import com.yonge.cooleshow.biz.dal.enums.*;
|
|
|
import com.yonge.cooleshow.biz.dal.service.*;
|
|
|
+import com.yonge.cooleshow.biz.dal.support.WrapperUtil;
|
|
|
import com.yonge.cooleshow.biz.dal.vo.*;
|
|
|
import com.yonge.cooleshow.biz.dal.vo.res.OrderCreateRes;
|
|
|
import com.yonge.cooleshow.common.entity.HttpResponseResult;
|
|
|
import com.yonge.cooleshow.common.enums.ActivityTypeEnum;
|
|
|
+import com.yonge.cooleshow.common.enums.CacheNameEnum;
|
|
|
import com.yonge.cooleshow.common.enums.RegistrationMethodEnum;
|
|
|
import com.yonge.toolset.base.exception.BizException;
|
|
|
import com.yonge.toolset.base.util.StringUtil;
|
|
|
+import com.yonge.toolset.payment.util.DistributedLock;
|
|
|
import com.yonge.toolset.thirdparty.message.MessageSenderPluginContext;
|
|
|
+import org.redisson.api.RedissonClient;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.slf4j.Logger;
|
|
@@ -35,6 +42,7 @@ import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
import javax.annotation.Resource;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.util.*;
|
|
@@ -42,22 +50,27 @@ import java.util.Date;
|
|
|
|
|
|
import java.util.List;
|
|
|
import java.util.Optional;
|
|
|
+import java.util.function.Consumer;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
@Service
|
|
|
public class ActivityPlanServiceImpl extends ServiceImpl<ActivityPlanDao, ActivityPlan> implements ActivityPlanService {
|
|
|
private final static Logger log = LoggerFactory.getLogger(ActivityPlanServiceImpl.class);
|
|
|
- @Resource
|
|
|
- private ActivityPlanEvaluationDao activityPlanEvaluationDao;
|
|
|
+ @Autowired
|
|
|
+ private ActivityPlanEvaluationService activityPlanEvaluationService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ActivityPlanStandardService activityPlanStandardService;
|
|
|
|
|
|
@Autowired
|
|
|
private ActivityEvaluationService activityEvaluationService;
|
|
|
|
|
|
@Autowired
|
|
|
- private ActivityRewardService activityRewardService;
|
|
|
+ private ActivityPlanRewardService activityPlanRewardService;
|
|
|
|
|
|
@Autowired
|
|
|
- private ActivityUserRewardService activityUserRewardService;
|
|
|
+ private ActivityRewardService activityRewardService;
|
|
|
|
|
|
@Resource
|
|
|
private SysUserFeignService sysUserFeignService;
|
|
@@ -72,16 +85,59 @@ public class ActivityPlanServiceImpl extends ServiceImpl<ActivityPlanDao, Activi
|
|
|
private SysMessageService sysMessageService;
|
|
|
|
|
|
@Autowired
|
|
|
- private UserOrderService userOrderService;
|
|
|
+ private RedissonClient redissonClient;
|
|
|
+
|
|
|
+ //保存/更新拓展字段
|
|
|
+ private static final Map<ActivityTypeEnum, Consumer<ActivityPlanDto>> saveOrUpdateExpand = new HashMap<>();
|
|
|
+ //开始活动(活动刚开始触发)
|
|
|
+ private static final Map<ActivityTypeEnum, Consumer<Long>> startActivity = new HashMap<>();
|
|
|
+ //开启活动(开启活动按钮)
|
|
|
+ private static final Map<ActivityTypeEnum, Consumer<Long>> openActivity = new HashMap<>();
|
|
|
+ //关闭活动(关闭活动按钮)
|
|
|
+ private static final Map<ActivityTypeEnum, Consumer<Long>> closeActivity = new HashMap<>();
|
|
|
+ //活动中定时轮询
|
|
|
+ private static final Map<ActivityTypeEnum, Consumer<Object>> activityIng = new HashMap<>();
|
|
|
+ //活动完成(活动截止时间到触发)
|
|
|
+ private static final Map<ActivityTypeEnum, Consumer<Long>> successActivity = new HashMap<>();
|
|
|
+
|
|
|
+ @PostConstruct
|
|
|
+ private void init() {
|
|
|
+ /**********保存/更新拓展字段******************/
|
|
|
+ //评测活动
|
|
|
+ saveOrUpdateExpand.put(ActivityTypeEnum.EVALUATION, activityPlanEvaluationService::createOrUpdate);
|
|
|
+ //达标赠送活动
|
|
|
+ saveOrUpdateExpand.put(ActivityTypeEnum.STANDARD_GIFT, activityPlanStandardService::createOrUpdate);
|
|
|
+ /**********开始活动(活动刚开始触发)******************/
|
|
|
+
|
|
|
+ /**********开启活动(开启活动按钮)******************/
|
|
|
+
|
|
|
+ /**********关闭活动(关闭活动按钮)******************/
|
|
|
+ //评测活动
|
|
|
+ closeActivity.put(ActivityTypeEnum.EVALUATION, activityPlanEvaluationService::closeActivity);
|
|
|
+ /**********活动中定时轮询******************/
|
|
|
+ //达标赠送活动
|
|
|
+ activityIng.put(ActivityTypeEnum.STANDARD_GIFT, activityPlanStandardService::activityIng);
|
|
|
+ /**********活动完成(活动截止时间到触发)******************/
|
|
|
+ //评测活动
|
|
|
+ successActivity.put(ActivityTypeEnum.EVALUATION, activityPlanEvaluationService::successActivity);
|
|
|
+ //达标赠送活动
|
|
|
+ successActivity.put(ActivityTypeEnum.STANDARD_GIFT, activityPlanStandardService::successActivity);
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
public ActivityPlanVo detail(Long id) {
|
|
|
ActivityPlanVo detail = baseMapper.detail(id);
|
|
|
if (null != detail) {
|
|
|
if (ActivityTypeEnum.EVALUATION.equals(detail.getActivityType())) {
|
|
|
- detail.setPlanEvaluation(activityPlanEvaluationDao.selectById(detail.getId()));
|
|
|
+ detail.setPlanEvaluation(activityPlanEvaluationService.getById(id));
|
|
|
+ }
|
|
|
+ if (ActivityTypeEnum.STANDARD_GIFT.equals(detail.getActivityType())) {
|
|
|
+ detail.setPlanStandard(activityPlanStandardService.getById(id));
|
|
|
}
|
|
|
}
|
|
|
+ //查询活动奖品
|
|
|
+ List<ActivityPlanRewardDto> activityRewardList = activityPlanRewardService.queryActivityPlanReward(id);
|
|
|
+ detail.setActivityRewardList(activityRewardList);
|
|
|
return detail;
|
|
|
}
|
|
|
|
|
@@ -92,45 +148,38 @@ public class ActivityPlanServiceImpl extends ServiceImpl<ActivityPlanDao, Activi
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public ActivityPlan submit(ActivityPlanDto activityPlan, SysUser user) {
|
|
|
+ public ActivityPlan submit(ActivityPlanDto activityPlan) {
|
|
|
//验证参数
|
|
|
if (!validParam(activityPlan)) {
|
|
|
- return null;
|
|
|
+ throw new BizException("参数异常");
|
|
|
}
|
|
|
|
|
|
if (null == activityPlan.getId()) {
|
|
|
activityPlan.setDraftFlag(1);
|
|
|
- activityPlan.setCreateBy(user.getId());
|
|
|
- activityPlan.setUpdateBy(user.getId());
|
|
|
+ activityPlan.setCreateBy(activityPlan.getUserId());
|
|
|
+ activityPlan.setUpdateBy(activityPlan.getUserId());
|
|
|
save(activityPlan);
|
|
|
-
|
|
|
- ActivityPlanEvaluation planEvaluation = activityPlan.getPlanEvaluation();
|
|
|
- planEvaluation.setId(activityPlan.getId());
|
|
|
- planEvaluation.setCreateBy(user.getId());
|
|
|
- planEvaluation.setUpdateBy(user.getId());
|
|
|
- activityPlanEvaluationDao.insert(planEvaluation);
|
|
|
- return activityPlan;
|
|
|
} else {
|
|
|
ActivityPlan old = getById(activityPlan.getId());
|
|
|
//启用状态,只能修改活动结束时间
|
|
|
if (old.getActivityState().equals(1)) {
|
|
|
if (activityPlan.getActivityEnd().after(new Date())) {
|
|
|
old.setActivityEnd(activityPlan.getActivityEnd());
|
|
|
- old.setUpdateBy(user.getId());
|
|
|
+ old.setUpdateBy(activityPlan.getUserId());
|
|
|
updateById(old);
|
|
|
- return old;
|
|
|
}
|
|
|
- return null;
|
|
|
} else {
|
|
|
- activityPlan.setUpdateBy(user.getId());
|
|
|
+ activityPlan.setUpdateBy(activityPlan.getUserId());
|
|
|
updateById(activityPlan);
|
|
|
-
|
|
|
- ActivityPlanEvaluation planEvaluation = activityPlan.getPlanEvaluation();
|
|
|
- planEvaluation.setUpdateBy(user.getId());
|
|
|
- activityPlanEvaluationDao.updateById(planEvaluation);
|
|
|
- return activityPlan;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ //修改拓展信息
|
|
|
+ Consumer<ActivityPlanDto> afterFunction = saveOrUpdateExpand.get(activityPlan.getActivityType());
|
|
|
+ if (!Objects.isNull(afterFunction)) {
|
|
|
+ afterFunction.accept(activityPlan);
|
|
|
+ }
|
|
|
+ return activityPlan;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -148,34 +197,88 @@ public class ActivityPlanServiceImpl extends ServiceImpl<ActivityPlanDao, Activi
|
|
|
activityPlan.setActivityStart(now);
|
|
|
}
|
|
|
activityPlan.setActivityState(activityState);
|
|
|
+ //开启活动
|
|
|
+ Consumer<Long> afterFunction = openActivity.get(activityPlan.getActivityType());
|
|
|
+ if (!Objects.isNull(afterFunction)) {
|
|
|
+ afterFunction.accept(activityPlan.getId());
|
|
|
+ }
|
|
|
} else {
|
|
|
activityPlan.setActivityState(2);
|
|
|
- //取消活动所有订单
|
|
|
- cancelActivityOrder(detail);
|
|
|
+ //关闭活动
|
|
|
+ Consumer<Long> afterFunction = closeActivity.get(activityPlan.getActivityType());
|
|
|
+ if (!Objects.isNull(afterFunction)) {
|
|
|
+ afterFunction.accept(activityPlan.getId());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
activityPlan.setId(activityId);
|
|
|
activityPlan.setUpdateBy(id);
|
|
|
activityPlan.setUpdateTime(new Date());
|
|
|
-
|
|
|
baseMapper.updateById(activityPlan);
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public Boolean updateDraftFlag(Long activityId, Integer draftFlag, Long userId) {
|
|
|
+ ActivityPlan activityPlan = new ActivityPlan();
|
|
|
+ activityPlan.setId(activityId);
|
|
|
+ //更改活动状态
|
|
|
+ activityPlan.setDraftFlag(draftFlag);
|
|
|
+ activityPlan.setUpdateBy(userId);
|
|
|
+ updateById(activityPlan);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
private Boolean validParam(ActivityPlanDto activityPlan) {
|
|
|
if (null == activityPlan) {
|
|
|
return false;
|
|
|
}
|
|
|
- if (RegistrationMethodEnum.CHARGE.equals(activityPlan.getRegistrationMethod())
|
|
|
- && (null == activityPlan.getRegistrationPrice() || BigDecimal.ZERO.compareTo(activityPlan.getRegistrationPrice()) > 0)) {
|
|
|
- throw new BizException("活动收费时,报名金额不能为空");
|
|
|
+
|
|
|
+ if (!ActivityTypeEnum.STANDARD_GIFT.equals(activityPlan.getActivityType())) {
|
|
|
+
|
|
|
+ if (null == activityPlan.getRegistrationMethod()) {
|
|
|
+ throw new BizException("报名缴费方式不能为空");
|
|
|
+ }
|
|
|
+ if (RegistrationMethodEnum.CHARGE.equals(activityPlan.getRegistrationMethod())
|
|
|
+ && (null == activityPlan.getRegistrationPrice() || BigDecimal.ZERO.compareTo(activityPlan.getRegistrationPrice()) > 0)) {
|
|
|
+ throw new BizException("活动收费时,报名金额不能为空");
|
|
|
+ }
|
|
|
+ if (RegistrationMethodEnum.CHARGE.equals(activityPlan.getRegistrationMethod())
|
|
|
+ && (null == activityPlan.getShareRate())) {
|
|
|
+ throw new BizException("活动收费时,分润比例不能为空");
|
|
|
+ }
|
|
|
+ if (StringUtil.isEmpty(activityPlan.getSubjectUrl())) {
|
|
|
+ throw new BizException("主题图片不能为空");
|
|
|
+ }
|
|
|
+ if (StringUtil.isEmpty(activityPlan.getBackgroundUrl())) {
|
|
|
+ throw new BizException("平铺背景不能为空");
|
|
|
+ }
|
|
|
+ if (StringUtil.isEmpty(activityPlan.getShareUrl())) {
|
|
|
+ throw new BizException("分享图片不能为空");
|
|
|
+ }
|
|
|
+ if (StringUtil.isEmpty(activityPlan.getRuleDescribe())) {
|
|
|
+ throw new BizException("活动规则不能为空");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (ActivityTypeEnum.EVALUATION.equals(activityPlan.getActivityType())) {
|
|
|
+ activityPlan.setActivityClient(ClientEnum.STUDENT);
|
|
|
+ if (null == activityPlan.getPlanEvaluation() || null == activityPlan.getPlanEvaluation().getEvaluationDifficulty()) {
|
|
|
+ throw new BizException("活动类型为评测活动时,评测难度不能为空");
|
|
|
+ }
|
|
|
}
|
|
|
- if (RegistrationMethodEnum.CHARGE.equals(activityPlan.getRegistrationMethod())
|
|
|
- && (null == activityPlan.getShareRate())) {
|
|
|
- throw new BizException("活动收费时,分润比例不能为空");
|
|
|
+
|
|
|
+ if (ActivityTypeEnum.STANDARD_GIFT.equals(activityPlan.getActivityType())) {
|
|
|
+ if (null == activityPlan.getPlanStandard()) {
|
|
|
+ throw new BizException("缺少达标赠送参数");
|
|
|
+ }
|
|
|
+ if (StringUtil.isEmpty(activityPlan.getPlanStandard().getCondition())) {
|
|
|
+ throw new BizException("达标条件不能为空");
|
|
|
+ }
|
|
|
}
|
|
|
- if (ActivityTypeEnum.EVALUATION.equals(activityPlan.getActivityType()) && (null == activityPlan.getPlanEvaluation() || null == activityPlan.getPlanEvaluation().getEvaluationDifficulty())) {
|
|
|
- throw new BizException("活动类型为评测活动时,评测难度不能为空");
|
|
|
+
|
|
|
+ if (null == activityPlan.getActivityClient()) {
|
|
|
+ throw new BizException("活动客户端不能为空");
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
@@ -320,7 +423,6 @@ public class ActivityPlanServiceImpl extends ServiceImpl<ActivityPlanDao, Activi
|
|
|
activityRegistrationService.save(activityRegistration);
|
|
|
|
|
|
sendMessage(activityPlan.getActivityName(), user.getId());
|
|
|
-
|
|
|
return true;
|
|
|
}
|
|
|
|
|
@@ -328,43 +430,63 @@ public class ActivityPlanServiceImpl extends ServiceImpl<ActivityPlanDao, Activi
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public void activityState() {
|
|
|
List<ActivityPlan> list = baseMapper.activityState();
|
|
|
- for (ActivityPlan activityPlan : list) {
|
|
|
- if (activityPlan.getActivityState() == 0) {
|
|
|
- activityPlan.setActivityState(1);
|
|
|
- baseMapper.updateById(activityPlan);
|
|
|
- } else {
|
|
|
- activityPlan.setActivityState(0);
|
|
|
- baseMapper.updateById(activityPlan);
|
|
|
- //取消所有活动订单
|
|
|
- cancelActivityOrder(activityPlan);
|
|
|
- }
|
|
|
- }
|
|
|
- //活动发奖
|
|
|
- List<ActivityPlan> rewardList = baseMapper.activityReward();
|
|
|
- for (ActivityPlan activityPlan : rewardList) {
|
|
|
- List<ActivityUserReward> activityUserRewardList = baseMapper.getUserReward(activityPlan.getId(), activityPlan.getActivityType().getCode());
|
|
|
- activityUserRewardService.saveBatch(activityUserRewardList);
|
|
|
- //活动置为已发奖
|
|
|
- activityPlan.setRewardFlag(1);
|
|
|
- updateById(activityPlan);
|
|
|
+ for (ActivityPlan plan : list) {
|
|
|
+ DistributedLock.of(redissonClient)
|
|
|
+ .runIfLockToFunction(CacheNameEnum.LOCK_ACTIVITY_STOCK.getRedisKey(plan.getId())
|
|
|
+ , (id) -> {
|
|
|
+ ActivityPlan activityPlan = getById(id);
|
|
|
+ if (activityPlan.getActivityState() == 0) {
|
|
|
+ activityPlan.setActivityState(1);
|
|
|
+ baseMapper.updateById(activityPlan);
|
|
|
+
|
|
|
+ //开始活动
|
|
|
+ Consumer<Long> afterFunction = startActivity.get(activityPlan.getActivityType());
|
|
|
+ if (!Objects.isNull(afterFunction)) {
|
|
|
+ afterFunction.accept(activityPlan.getId());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ activityPlan.setActivityState(0);
|
|
|
+ activityPlan.setRewardFlag(1);
|
|
|
+ baseMapper.updateById(activityPlan);
|
|
|
+
|
|
|
+ //完成活动
|
|
|
+ Consumer<Long> afterFunction = successActivity.get(activityPlan.getActivityType());
|
|
|
+ if (!Objects.isNull(afterFunction)) {
|
|
|
+ afterFunction.accept(activityPlan.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }, plan.getId(), 10l);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void cancelActivityOrder(ActivityPlan activityPlan) {
|
|
|
- OrderSearch query = new OrderSearch();
|
|
|
- query.setOrderType(OrderTypeEnum.ACTI_REGIST.getCode());
|
|
|
- query.setGoodType(GoodTypeEnum.ACTI_REGIST.getCode());
|
|
|
- query.setBizId(activityPlan.getId());
|
|
|
- List<UserOrderVo> userOrderVos = userOrderService.selectAllList(query);
|
|
|
- for (UserOrderVo userOrderVo : userOrderVos) {
|
|
|
- OrderPayReq payReq = new OrderPayReq();
|
|
|
- payReq.setOrderNo(userOrderVo.getOrderNo());
|
|
|
- payReq.setUserId(userOrderVo.getUserId());
|
|
|
- payReq.setReason("活动已结束");
|
|
|
- userOrderService.orderCancel(payReq);
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void activityIng() {
|
|
|
+ for (ActivityTypeEnum activityTypeEnum : activityIng.keySet()) {
|
|
|
+ activityIng.get(activityTypeEnum).accept(null);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Boolean saveOrUpdateReward(List<ActivityPlanRewardDto> updateRewardDtoList) {
|
|
|
+ Long activityId = updateRewardDtoList.get(0).getActivityId();
|
|
|
+
|
|
|
+ for (ActivityPlanRewardDto rewardDto : updateRewardDtoList) {
|
|
|
+ activityPlanRewardService.saveOrUpdateReward(rewardDto);
|
|
|
+ }
|
|
|
+ //还有删除的
|
|
|
+ List<Long> rewardIds = updateRewardDtoList.stream().map(ActivityPlanRewardDto::getRewardId).collect(Collectors.toList());
|
|
|
+ List<ActivityPlanReward> delList = activityPlanRewardService.getDelRewardList(activityId, rewardIds);
|
|
|
+ for (ActivityPlanReward reward : delList) {
|
|
|
+ //修改库存
|
|
|
+ activityPlanRewardService.recoveryReward(reward);
|
|
|
+ //删除活动奖品
|
|
|
+ activityPlanRewardService.removeById(reward.getId());
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 参与活动消息
|
|
@@ -383,8 +505,6 @@ public class ActivityPlanServiceImpl extends ServiceImpl<ActivityPlanDao, Activi
|
|
|
} catch (Exception e) {
|
|
|
log.error("活动参与发送消息失败--> {}", e.fillInStackTrace());
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
|
|
|
private void checkActivityPay(Long planId) {
|