From 5b24fb1060107a4a7020c9b88e065534b7df2f00 Mon Sep 17 00:00:00 2001 From: hurui <177768073@qq.com> Date: Tue, 28 Sep 2021 23:47:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4diam?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/HighActivityController.java | 47 +++++++++++++++++- .../com/hai/common/exception/ErrorCode.java | 1 + .../com/hai/common/utils/ResponseMsgUtil.java | 4 +- .../HighActivityAwardSerialNumService.java | 7 +++ .../service/HighActivityAwardUserService.java | 12 +++++ ...HighActivityAwardSerialNumServiceImpl.java | 5 ++ .../HighActivityAwardUserServiceImpl.java | 36 ++++++++++++++ ...HighActivityUserLotteryNumServiceImpl.java | 49 +++++++++---------- 8 files changed, 131 insertions(+), 30 deletions(-) create mode 100644 hai-service/src/main/java/com/hai/service/HighActivityAwardUserService.java create mode 100644 hai-service/src/main/java/com/hai/service/impl/HighActivityAwardUserServiceImpl.java diff --git a/hai-cweb/src/main/java/com/cweb/controller/HighActivityController.java b/hai-cweb/src/main/java/com/cweb/controller/HighActivityController.java index 4d8e3ce7..0d4371d6 100644 --- a/hai-cweb/src/main/java/com/cweb/controller/HighActivityController.java +++ b/hai-cweb/src/main/java/com/cweb/controller/HighActivityController.java @@ -17,6 +17,7 @@ import com.hai.model.ResponseData; import com.hai.service.HighActivityAwardService; import com.hai.service.HighActivityInfoService; import com.hai.service.HighActivityUserLotteryNumService; +import com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.commons.collections4.MapUtils; @@ -24,6 +25,7 @@ import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DeadlockLoserDataAccessException; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; @@ -57,7 +59,7 @@ public class HighActivityController { @RequestMapping(value="/userLottery",method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "用户抽奖业务") - public ResponseData userLottery(@RequestBody JSONObject body, HttpServletRequest request) { + public ResponseData userLottery(@RequestBody JSONObject body, HttpServletRequest request) { try { // 用户 SessionObject sessionObject = userCenter.getSessionObject(request); @@ -68,14 +70,55 @@ public class HighActivityController { throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); } + // 活动信息 + HighActivityInfo activityInfo = highActivityInfoService.getDetailById(body.getLong("activityId")); + if (activityInfo == null) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到数据"); + } + if (!activityInfo.getStatus().equals(ActivityInfoStatus.status2.getNumber())) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "活动已结束"); + } + Date currentDate = new Date(); + if (currentDate.getTime() < activityInfo.getStartTime().getTime()) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "活动还未开始"); + } + if (currentDate.getTime() > activityInfo.getEndTime().getTime()) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "活动已结束"); + } + // 校验用户是否有抽奖次数 + if (highActivityUserLotteryNumService.getLotteryNum(body.getLong("activityId"), userInfoModel.getHighUser().getId()) <= 0) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "抽奖次数已经用完了"); + } + return ResponseMsgUtil.success(highActivityUserLotteryNumService.userLottery(body.getLong("activityId"),userInfoModel.getHighUser().getId())); + } catch (DeadlockLoserDataAccessException deadlockLoserDataAccessException) { + log.error("HighActivityController -> userLottery() error!", "服务器繁忙"); + return ResponseMsgUtil.builderResponse(ErrorCode.SERVER_BUSY_ERROR.getCode(),ErrorCode.SERVER_BUSY_ERROR.getMsg(),null); + } catch (Exception e) { log.error("HighActivityController -> userLottery() error!",e); return ResponseMsgUtil.exception(e); } } + @RequestMapping(value="/getLotteryNum",method = RequestMethod.GET) + @ResponseBody + @ApiOperation(value = "查询抽奖数量") + public ResponseData getLotteryNum(@RequestParam(name = "activityId", required = true) Long activityId, + HttpServletRequest request) { + try { + // 用户 + SessionObject sessionObject = userCenter.getSessionObject(request); + HighUserModel userInfoModel = (HighUserModel) sessionObject.getObject(); + + return ResponseMsgUtil.success(highActivityUserLotteryNumService.getLotteryNum(activityId,userInfoModel.getHighUser().getId())); + } catch (Exception e) { + log.error("HighActivityController -> getLotteryNum() error!",e); + return ResponseMsgUtil.exception(e); + } + } + @RequestMapping(value="/getActivityByCoupon",method = RequestMethod.GET) @ResponseBody @ApiOperation(value = "根据卡券查询活动信息") @@ -116,4 +159,6 @@ public class HighActivityController { return ResponseMsgUtil.exception(e); } } + + } diff --git a/hai-service/src/main/java/com/hai/common/exception/ErrorCode.java b/hai-service/src/main/java/com/hai/common/exception/ErrorCode.java index 6cb0cd62..f2d7e223 100644 --- a/hai-service/src/main/java/com/hai/common/exception/ErrorCode.java +++ b/hai-service/src/main/java/com/hai/common/exception/ErrorCode.java @@ -25,6 +25,7 @@ public enum ErrorCode { WECHAT_DECRYPT_ERROR("3001","微信解密错误->%s"), WECHAT_LOGIN_ERROR("3002","微信登录失败"), WECHAT_LOGIN_TEACHER_ERROR("3003","当前微信用户不是老师,请联系管理员"), + SERVER_BUSY_ERROR("3004","服务器繁忙,请稍后重试"), //////////////////业务异常///////////// COMMON_ERROR("2000",""), diff --git a/hai-service/src/main/java/com/hai/common/utils/ResponseMsgUtil.java b/hai-service/src/main/java/com/hai/common/utils/ResponseMsgUtil.java index 92e281c2..8c80e76d 100644 --- a/hai-service/src/main/java/com/hai/common/utils/ResponseMsgUtil.java +++ b/hai-service/src/main/java/com/hai/common/utils/ResponseMsgUtil.java @@ -60,9 +60,9 @@ public class ResponseMsgUtil { if(e instanceof BizException){//业务异常处理 return builderResponse(((BizException)e).getErrorCode(), ((BizException)e).getErrorMsg(), null); - }else if(e instanceof BaseException){//系统异常处理 + } else if(e instanceof BaseException){//系统异常处理 return builderResponse(((BaseException)e).getErrorCode(),"服务异常",null); - }else{//未知异常 + } else{//未知异常 return builderResponse("999999", "未知异常", null); } } diff --git a/hai-service/src/main/java/com/hai/service/HighActivityAwardSerialNumService.java b/hai-service/src/main/java/com/hai/service/HighActivityAwardSerialNumService.java index f9ce25c1..4d0c3dbd 100644 --- a/hai-service/src/main/java/com/hai/service/HighActivityAwardSerialNumService.java +++ b/hai-service/src/main/java/com/hai/service/HighActivityAwardSerialNumService.java @@ -17,6 +17,13 @@ public interface HighActivityAwardSerialNumService { */ void update(HighActivityAwardSerialNum serialNum); + /** + * 根据id查询详情 + * @param id + * @return + */ + HighActivityAwardSerialNum getDetail(Long id); + /** * 查询未中奖的编号 * @param activityId diff --git a/hai-service/src/main/java/com/hai/service/HighActivityAwardUserService.java b/hai-service/src/main/java/com/hai/service/HighActivityAwardUserService.java new file mode 100644 index 00000000..c73fa587 --- /dev/null +++ b/hai-service/src/main/java/com/hai/service/HighActivityAwardUserService.java @@ -0,0 +1,12 @@ +package com.hai.service; + +/** + * @author hurui + */ +public interface HighActivityAwardUserService { + + /** + * 增加用户中奖记录 + */ + void insertAwardUser(Long serialNumId,Long userId); +} diff --git a/hai-service/src/main/java/com/hai/service/impl/HighActivityAwardSerialNumServiceImpl.java b/hai-service/src/main/java/com/hai/service/impl/HighActivityAwardSerialNumServiceImpl.java index 6da4034e..6ef05377 100644 --- a/hai-service/src/main/java/com/hai/service/impl/HighActivityAwardSerialNumServiceImpl.java +++ b/hai-service/src/main/java/com/hai/service/impl/HighActivityAwardSerialNumServiceImpl.java @@ -20,6 +20,11 @@ public class HighActivityAwardSerialNumServiceImpl implements HighActivityAwardS highActivityAwardSerialNumMapper.updateByPrimaryKey(serialNum); } + @Override + public HighActivityAwardSerialNum getDetail(Long id) { + return highActivityAwardSerialNumMapper.selectByPrimaryKey(id); + } + @Override public List getNoGainListByActivity(Long activityId) { HighActivityAwardSerialNumExample example = new HighActivityAwardSerialNumExample(); diff --git a/hai-service/src/main/java/com/hai/service/impl/HighActivityAwardUserServiceImpl.java b/hai-service/src/main/java/com/hai/service/impl/HighActivityAwardUserServiceImpl.java new file mode 100644 index 00000000..754e2c61 --- /dev/null +++ b/hai-service/src/main/java/com/hai/service/impl/HighActivityAwardUserServiceImpl.java @@ -0,0 +1,36 @@ +package com.hai.service.impl; + +import com.hai.dao.HighActivityAwardUserMapper; +import com.hai.entity.HighActivityAwardSerialNum; +import com.hai.entity.HighActivityAwardUser; +import com.hai.service.HighActivityAwardSerialNumService; +import com.hai.service.HighActivityAwardUserService; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.Date; + +@Service("highActivityAwardUserService") +public class HighActivityAwardUserServiceImpl implements HighActivityAwardUserService { + + @Resource + private HighActivityAwardUserMapper highActivityAwardUserMapper; + + @Resource + private HighActivityAwardSerialNumService highActivityAwardSerialNumService; + + @Override + public void insertAwardUser(Long serialNumId,Long userId) { + // 查询中奖号码 + HighActivityAwardSerialNum detail = highActivityAwardSerialNumService.getDetail(serialNumId); + if (detail != null) { + HighActivityAwardUser awardUser = new HighActivityAwardUser(); + awardUser.setCreateTime(new Date()); + awardUser.setActivityAwardSerialNumId(serialNumId); + awardUser.setUserId(userId); + awardUser.setActivityInfoId(detail.getActivityInfoId()); + awardUser.setActivityAwardId(detail.getActivityAwardId()); + highActivityAwardUserMapper.insert(awardUser); + } + } +} diff --git a/hai-service/src/main/java/com/hai/service/impl/HighActivityUserLotteryNumServiceImpl.java b/hai-service/src/main/java/com/hai/service/impl/HighActivityUserLotteryNumServiceImpl.java index 7c335e50..9ba8d9c4 100644 --- a/hai-service/src/main/java/com/hai/service/impl/HighActivityUserLotteryNumServiceImpl.java +++ b/hai-service/src/main/java/com/hai/service/impl/HighActivityUserLotteryNumServiceImpl.java @@ -1,5 +1,6 @@ package com.hai.service.impl; +import com.alibaba.fastjson.JSONObject; import com.hai.common.exception.ErrorCode; import com.hai.common.exception.ErrorHelp; import com.hai.common.exception.SysCode; @@ -28,6 +29,9 @@ public class HighActivityUserLotteryNumServiceImpl implements HighActivityUserLo @Resource private HighActivityAwardActualPrizeService highActivityAwardActualPrizeService; + @Resource + private HighActivityAwardUserService highActivityAwardUserService; + @Resource private HighActivityUserLotteryNumMapper highActivityUserLotteryNumMapper; @@ -35,27 +39,11 @@ public class HighActivityUserLotteryNumServiceImpl implements HighActivityUserLo private HighDiscountUserRelService highDiscountUserRelService; @Override - @Transactional(propagation= Propagation.REQUIRES_NEW,isolation= Isolation.SERIALIZABLE) + @Transactional(rollbackFor = Exception.class, + propagation= Propagation.REQUIRES_NEW, + isolation= Isolation.SERIALIZABLE, + timeout = 30) public Object userLottery(Long activityId, Long userId) { - // 活动信息 - HighActivityInfo activityInfo = highActivityInfoService.getDetailById(activityId); - if (activityInfo == null) { - throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到数据"); - } - if (!activityInfo.getStatus().equals(ActivityInfoStatus.status2.getNumber())) { - throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "活动已结束"); - } - Date currentDate = new Date(); - if (currentDate.getTime() < activityInfo.getStartTime().getTime()) { - throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "活动还未开始"); - } - if (currentDate.getTime() > activityInfo.getEndTime().getTime()) { - throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "活动已结束"); - } - // 校验用户是否有抽奖次数 - if (getLotteryNum(activityId, userId) <= 0) { - throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "抽奖次数已经用完了"); - } // 查询活动未中奖的号码 List serialNumList = highActivityAwardSerialNumService.getNoGainListByActivity(activityId); if (serialNumList.size() == 0) { @@ -67,19 +55,27 @@ public class HighActivityUserLotteryNumServiceImpl implements HighActivityUserLo // 抽中的号码 int index = (int) (Math.random() * serialNumList.size()); HighActivityAwardSerialNum serialNum = serialNumList.get(index); + serialNum.setStatus(2); + highActivityAwardSerialNumService.update(serialNum); + + // 增加中奖记录 + highActivityAwardUserService.insertAwardUser(serialNum.getId(),userId); - // 领取奖品 - receivePrize(serialNum,userId); - return serialNum; + new Thread(() -> { + // 领取奖品 + receivePrize(serialNum,userId); + }).start(); + + JSONObject jsonObject = new JSONObject(); + jsonObject.put("activityAwardId", serialNum.getActivityAwardId()); + jsonObject.put("serialNum", serialNum.getSerialNum()); + return jsonObject; } /** * 领取奖品 */ private void receivePrize(HighActivityAwardSerialNum serialNum, Long userId) { - serialNum.setStatus(2); - highActivityAwardSerialNumService.update(serialNum); - // 查询中奖的奖品 List prizeList = highActivityAwardActualPrizeService.getActualPrizeBySerialNumId(serialNum.getId()); for (HighActivityAwardActualPrize actualPrize : prizeList) { @@ -88,7 +84,6 @@ public class HighActivityUserLotteryNumServiceImpl implements HighActivityUserLo if (actualPrize.getActualPrizeType() == 2) { highDiscountUserRelService.receiveDiscount(userId,actualPrize.getActualPrizeId()); } - } }