dev-discount
胡锐 3 years ago
parent c47be8cbd7
commit 5b24fb1060
  1. 47
      hai-cweb/src/main/java/com/cweb/controller/HighActivityController.java
  2. 1
      hai-service/src/main/java/com/hai/common/exception/ErrorCode.java
  3. 4
      hai-service/src/main/java/com/hai/common/utils/ResponseMsgUtil.java
  4. 7
      hai-service/src/main/java/com/hai/service/HighActivityAwardSerialNumService.java
  5. 12
      hai-service/src/main/java/com/hai/service/HighActivityAwardUserService.java
  6. 5
      hai-service/src/main/java/com/hai/service/impl/HighActivityAwardSerialNumServiceImpl.java
  7. 36
      hai-service/src/main/java/com/hai/service/impl/HighActivityAwardUserServiceImpl.java
  8. 49
      hai-service/src/main/java/com/hai/service/impl/HighActivityUserLotteryNumServiceImpl.java

@ -17,6 +17,7 @@ import com.hai.model.ResponseData;
import com.hai.service.HighActivityAwardService; import com.hai.service.HighActivityAwardService;
import com.hai.service.HighActivityInfoService; import com.hai.service.HighActivityInfoService;
import com.hai.service.HighActivityUserLotteryNumService; import com.hai.service.HighActivityUserLotteryNumService;
import com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.MapUtils; import org.apache.commons.collections4.MapUtils;
@ -24,6 +25,7 @@ import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DeadlockLoserDataAccessException;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -57,7 +59,7 @@ public class HighActivityController {
@RequestMapping(value="/userLottery",method = RequestMethod.POST) @RequestMapping(value="/userLottery",method = RequestMethod.POST)
@ResponseBody @ResponseBody
@ApiOperation(value = "用户抽奖业务") @ApiOperation(value = "用户抽奖业务")
public ResponseData userLottery(@RequestBody JSONObject body, HttpServletRequest request) { public ResponseData userLottery(@RequestBody JSONObject body, HttpServletRequest request) {
try { try {
// 用户 // 用户
SessionObject sessionObject = userCenter.getSessionObject(request); SessionObject sessionObject = userCenter.getSessionObject(request);
@ -68,14 +70,55 @@ public class HighActivityController {
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); 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())); 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) { } catch (Exception e) {
log.error("HighActivityController -> userLottery() error!",e); log.error("HighActivityController -> userLottery() error!",e);
return ResponseMsgUtil.exception(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) @RequestMapping(value="/getActivityByCoupon",method = RequestMethod.GET)
@ResponseBody @ResponseBody
@ApiOperation(value = "根据卡券查询活动信息") @ApiOperation(value = "根据卡券查询活动信息")
@ -116,4 +159,6 @@ public class HighActivityController {
return ResponseMsgUtil.exception(e); return ResponseMsgUtil.exception(e);
} }
} }
} }

@ -25,6 +25,7 @@ public enum ErrorCode {
WECHAT_DECRYPT_ERROR("3001","微信解密错误->%s"), WECHAT_DECRYPT_ERROR("3001","微信解密错误->%s"),
WECHAT_LOGIN_ERROR("3002","微信登录失败"), WECHAT_LOGIN_ERROR("3002","微信登录失败"),
WECHAT_LOGIN_TEACHER_ERROR("3003","当前微信用户不是老师,请联系管理员"), WECHAT_LOGIN_TEACHER_ERROR("3003","当前微信用户不是老师,请联系管理员"),
SERVER_BUSY_ERROR("3004","服务器繁忙,请稍后重试"),
//////////////////业务异常///////////// //////////////////业务异常/////////////
COMMON_ERROR("2000",""), COMMON_ERROR("2000",""),

@ -60,9 +60,9 @@ public class ResponseMsgUtil {
if(e instanceof BizException){//业务异常处理 if(e instanceof BizException){//业务异常处理
return builderResponse(((BizException)e).getErrorCode(), ((BizException)e).getErrorMsg(), null); 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); return builderResponse(((BaseException)e).getErrorCode(),"服务异常",null);
}else{//未知异常 } else{//未知异常
return builderResponse("999999", "未知异常", null); return builderResponse("999999", "未知异常", null);
} }
} }

@ -17,6 +17,13 @@ public interface HighActivityAwardSerialNumService {
*/ */
void update(HighActivityAwardSerialNum serialNum); void update(HighActivityAwardSerialNum serialNum);
/**
* 根据id查询详情
* @param id
* @return
*/
HighActivityAwardSerialNum getDetail(Long id);
/** /**
* 查询未中奖的编号 * 查询未中奖的编号
* @param activityId * @param activityId

@ -0,0 +1,12 @@
package com.hai.service;
/**
* @author hurui
*/
public interface HighActivityAwardUserService {
/**
* 增加用户中奖记录
*/
void insertAwardUser(Long serialNumId,Long userId);
}

@ -20,6 +20,11 @@ public class HighActivityAwardSerialNumServiceImpl implements HighActivityAwardS
highActivityAwardSerialNumMapper.updateByPrimaryKey(serialNum); highActivityAwardSerialNumMapper.updateByPrimaryKey(serialNum);
} }
@Override
public HighActivityAwardSerialNum getDetail(Long id) {
return highActivityAwardSerialNumMapper.selectByPrimaryKey(id);
}
@Override @Override
public List<HighActivityAwardSerialNum> getNoGainListByActivity(Long activityId) { public List<HighActivityAwardSerialNum> getNoGainListByActivity(Long activityId) {
HighActivityAwardSerialNumExample example = new HighActivityAwardSerialNumExample(); HighActivityAwardSerialNumExample example = new HighActivityAwardSerialNumExample();

@ -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);
}
}
}

@ -1,5 +1,6 @@
package com.hai.service.impl; package com.hai.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.hai.common.exception.ErrorCode; import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp; import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode; import com.hai.common.exception.SysCode;
@ -28,6 +29,9 @@ public class HighActivityUserLotteryNumServiceImpl implements HighActivityUserLo
@Resource @Resource
private HighActivityAwardActualPrizeService highActivityAwardActualPrizeService; private HighActivityAwardActualPrizeService highActivityAwardActualPrizeService;
@Resource
private HighActivityAwardUserService highActivityAwardUserService;
@Resource @Resource
private HighActivityUserLotteryNumMapper highActivityUserLotteryNumMapper; private HighActivityUserLotteryNumMapper highActivityUserLotteryNumMapper;
@ -35,27 +39,11 @@ public class HighActivityUserLotteryNumServiceImpl implements HighActivityUserLo
private HighDiscountUserRelService highDiscountUserRelService; private HighDiscountUserRelService highDiscountUserRelService;
@Override @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) { 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<HighActivityAwardSerialNum> serialNumList = highActivityAwardSerialNumService.getNoGainListByActivity(activityId); List<HighActivityAwardSerialNum> serialNumList = highActivityAwardSerialNumService.getNoGainListByActivity(activityId);
if (serialNumList.size() == 0) { if (serialNumList.size() == 0) {
@ -67,19 +55,27 @@ public class HighActivityUserLotteryNumServiceImpl implements HighActivityUserLo
// 抽中的号码 // 抽中的号码
int index = (int) (Math.random() * serialNumList.size()); int index = (int) (Math.random() * serialNumList.size());
HighActivityAwardSerialNum serialNum = serialNumList.get(index); HighActivityAwardSerialNum serialNum = serialNumList.get(index);
serialNum.setStatus(2);
highActivityAwardSerialNumService.update(serialNum);
// 增加中奖记录
highActivityAwardUserService.insertAwardUser(serialNum.getId(),userId);
// 领取奖品 new Thread(() -> {
receivePrize(serialNum,userId); // 领取奖品
return serialNum; 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) { private void receivePrize(HighActivityAwardSerialNum serialNum, Long userId) {
serialNum.setStatus(2);
highActivityAwardSerialNumService.update(serialNum);
// 查询中奖的奖品 // 查询中奖的奖品
List<HighActivityAwardActualPrize> prizeList = highActivityAwardActualPrizeService.getActualPrizeBySerialNumId(serialNum.getId()); List<HighActivityAwardActualPrize> prizeList = highActivityAwardActualPrizeService.getActualPrizeBySerialNumId(serialNum.getId());
for (HighActivityAwardActualPrize actualPrize : prizeList) { for (HighActivityAwardActualPrize actualPrize : prizeList) {
@ -88,7 +84,6 @@ public class HighActivityUserLotteryNumServiceImpl implements HighActivityUserLo
if (actualPrize.getActualPrizeType() == 2) { if (actualPrize.getActualPrizeType() == 2) {
highDiscountUserRelService.receiveDiscount(userId,actualPrize.getActualPrizeId()); highDiscountUserRelService.receiveDiscount(userId,actualPrize.getActualPrizeId());
} }
} }
} }

Loading…
Cancel
Save