|
|
@ -19,8 +19,10 @@ import com.ccsens.braintraining.persist.mapper.RaffleTaskParamMapper; |
|
|
|
import com.ccsens.braintraining.persist.mapper.RaffleTimesMapper; |
|
|
|
import com.ccsens.braintraining.util.BrainTrainingCodeError; |
|
|
|
import com.ccsens.braintraining.util.BrainTrainingConstant; |
|
|
|
import com.ccsens.util.WebConstant; |
|
|
|
import com.ccsens.util.bean.dto.QueryDto; |
|
|
|
import com.ccsens.util.exception.BaseException; |
|
|
|
import com.ccsens.wechatutil.bean.dto.WxTemplateMessage; |
|
|
|
import com.github.pagehelper.PageHelper; |
|
|
|
import com.github.pagehelper.PageInfo; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
@ -129,6 +131,7 @@ public class RaffleService implements IRaffleService { |
|
|
|
raffleRecordMapper.insertSelective(record); |
|
|
|
// 设置奖品剩余数-1
|
|
|
|
raffleDao.decreasePrize(prize.getPrizeId()); |
|
|
|
|
|
|
|
return prize; |
|
|
|
} |
|
|
|
} |
|
|
@ -141,11 +144,58 @@ public class RaffleService implements IRaffleService { |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public RaffleVo.DoTask doTask(RaffleDto.DoTask param, Long userId) { |
|
|
|
public RaffleVo.DoTask doTask(RaffleDto.DoTask param, Long userId, String token, String deviceId) { |
|
|
|
// 判断活动是否是当前的活动
|
|
|
|
RaffleTask task = raffleDao.getRunningTask(param.getTaskId()); |
|
|
|
log.info("任务:{}", task); |
|
|
|
if (task == null) { |
|
|
|
throw new BaseException(BrainTrainingCodeError.RAFFLE_ACTIVE_NOT_OPEN); |
|
|
|
} |
|
|
|
// 判断有没有今天检查的结果
|
|
|
|
RaffleTimesExample timesExample = new RaffleTimesExample(); |
|
|
|
timesExample.createCriteria().andTaskIdEqualTo(param.getTaskId()).andUserIdEqualTo(userId) |
|
|
|
.andFinishTypeEqualTo(BrainTrainingConstant.Raffle.TASK_FINISH_NO); |
|
|
|
timesExample.setOrderByClause("id desc limit 1"); |
|
|
|
List<RaffleTimes> raffleTimes = raffleTimesMapper.selectByExample(timesExample); |
|
|
|
log.info("检查有无权限的记录:{}", raffleTimes); |
|
|
|
if (CollectionUtil.isEmpty(raffleTimes)) { |
|
|
|
// 没有调用检查接口,未检查则不允许存储结果
|
|
|
|
throw new BaseException(BrainTrainingCodeError.RAFFLE_TIMES_REFRESH); |
|
|
|
} |
|
|
|
byte finishType = BrainTrainingConstant.Raffle.TASK_FINISH_YES; |
|
|
|
// 判断是否完成任务
|
|
|
|
if (task.getType() == BrainTrainingConstant.Raffle.TASK_TYPE_SUBSCRIBE) { |
|
|
|
// 关注公众号
|
|
|
|
// 查询该用户是否已经关注了
|
|
|
|
// 查询公众号appId
|
|
|
|
String appId = getAppId(param); |
|
|
|
finishType = pageSubscript(token, deviceId, appId); |
|
|
|
} else if (task.getType() == BrainTrainingConstant.Raffle.TASK_TYPE_PERFECT) { |
|
|
|
// TODO 查询详情表
|
|
|
|
} |
|
|
|
|
|
|
|
// 判断
|
|
|
|
RaffleTimes updateTimes = new RaffleTimes(); |
|
|
|
updateTimes.setId(raffleTimes.get(0).getId()); |
|
|
|
updateTimes.setFinishType(finishType); |
|
|
|
raffleTimesMapper.updateByPrimaryKeySelective(updateTimes); |
|
|
|
|
|
|
|
return null; |
|
|
|
RaffleVo.DoTask doTask = new RaffleVo.DoTask(); |
|
|
|
doTask.setAddTimes(finishType == BrainTrainingConstant.Raffle.TASK_FINISH_YES ? task.getIncreaseTimes() : 0); |
|
|
|
// 计算剩余抽奖数
|
|
|
|
Integer remainTimes = raffleDao.countTimes(task.getActiveId(), userId); |
|
|
|
doTask.setTotalTimes(remainTimes); |
|
|
|
return doTask; |
|
|
|
} |
|
|
|
|
|
|
|
private String getAppId(RaffleDto.DoTask param) { |
|
|
|
RaffleTaskParamExample paramExample = new RaffleTaskParamExample(); |
|
|
|
paramExample.createCriteria().andTaskIdEqualTo(param.getTaskId()).andKeyWordEqualTo(BrainTrainingConstant.Raffle.TASK_PARAM_APP_ID); |
|
|
|
paramExample.setOrderByClause("id desc limit 1"); |
|
|
|
List<RaffleTaskParam> params = raffleTaskParamMapper.selectByExample(paramExample); |
|
|
|
if (CollectionUtil.isEmpty(params)) { |
|
|
|
throw new BaseException(BrainTrainingCodeError.SETTING_ERROR); |
|
|
|
} |
|
|
|
return params.get(0).getKeyValue(); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
@ -153,13 +203,13 @@ public class RaffleService implements IRaffleService { |
|
|
|
RaffleVo.CheckTask checkTask = new RaffleVo.CheckTask(); |
|
|
|
// 判断活动是否是当前的活动
|
|
|
|
RaffleTask task = raffleDao.getRunningTask(param.getTaskId()); |
|
|
|
log.info("活动:{}", task); |
|
|
|
log.info("任务:{}", task); |
|
|
|
if (task == null) { |
|
|
|
checkTask.setFinishStatus(BrainTrainingConstant.Raffle.TASK_FINISH_END); |
|
|
|
return checkTask; |
|
|
|
} |
|
|
|
// 判断是否已经有记录
|
|
|
|
List<RaffleTimes> raffleTimes = null; |
|
|
|
List<RaffleTimes> raffleTimes; |
|
|
|
RaffleTimesExample timesExample = new RaffleTimesExample(); |
|
|
|
timesExample.createCriteria().andTaskIdEqualTo(param.getTaskId()) |
|
|
|
.andUserIdEqualTo(userId).andFinishTypeGreaterThan(BrainTrainingConstant.Raffle.TASK_FINISH_NO); |
|
|
@ -173,7 +223,6 @@ public class RaffleService implements IRaffleService { |
|
|
|
break; |
|
|
|
default: |
|
|
|
throw new BaseException(BrainTrainingCodeError.SETTING_ERROR); |
|
|
|
|
|
|
|
} |
|
|
|
// 执行的次数大于任务最大的执行次数,则不能再做
|
|
|
|
if (raffleTimes.size() >= task.getRunTimes()) { |
|
|
@ -186,39 +235,63 @@ public class RaffleService implements IRaffleService { |
|
|
|
// 查询记录中有没有关注的是同一个公众号的
|
|
|
|
Integer recordTimes = raffleDao.countSubscribe(param.getTaskId(), userId); |
|
|
|
if (recordTimes != null && recordTimes > 0) { |
|
|
|
//以前关注过,记录
|
|
|
|
checkTask.setFinishStatus(BrainTrainingConstant.Raffle.TASK_FINISH_BEFORE); |
|
|
|
saveTimes(userId, param.getTaskId(), BrainTrainingConstant.Raffle.TASK_FINISH_BEFORE); |
|
|
|
return checkTask; |
|
|
|
} |
|
|
|
// 查询该用户是否已经关注了
|
|
|
|
// 查询公众号appId
|
|
|
|
RaffleTaskParamExample paramExample = new RaffleTaskParamExample(); |
|
|
|
paramExample.createCriteria().andTaskIdEqualTo(param.getTaskId()).andKeyWordEqualTo(BrainTrainingConstant.Raffle.TASK_PARAM_APP_ID); |
|
|
|
paramExample.setOrderByClause("id desc limit 1"); |
|
|
|
List<RaffleTaskParam> params = raffleTaskParamMapper.selectByExample(paramExample); |
|
|
|
if (CollectionUtil.isEmpty(params)) { |
|
|
|
throw new BaseException(BrainTrainingCodeError.SETTING_ERROR); |
|
|
|
} |
|
|
|
String appId = params.get(0).getKeyValue(); |
|
|
|
String appId = getAppId(param); |
|
|
|
byte status = pageSubscript(token, deviceId, appId); |
|
|
|
|
|
|
|
checkTask.setFinishStatus(status); |
|
|
|
saveTimes(userId, param.getTaskId(), status); |
|
|
|
return checkTask; |
|
|
|
|
|
|
|
} else if (task.getType() == BrainTrainingConstant.Raffle.TASK_TYPE_PERFECT) { |
|
|
|
// TODO 查询详情表
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
checkTask.setFinishStatus(BrainTrainingConstant.Raffle.TASK_FINISH_NO); |
|
|
|
saveTimes(userId, param.getTaskId(), BrainTrainingConstant.Raffle.TASK_FINISH_NO); |
|
|
|
return checkTask; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 删除审核记录,新添加记录 |
|
|
|
* @param userId userId |
|
|
|
* @param taskId 任务ID |
|
|
|
* @param status 任务完成状态 |
|
|
|
*/ |
|
|
|
private void saveTimes(Long userId, Long taskId, byte status) { |
|
|
|
RaffleTimesExample timesExample = new RaffleTimesExample(); |
|
|
|
timesExample.createCriteria().andTaskIdEqualTo(taskId).andUserIdEqualTo(userId) |
|
|
|
.andFinishTypeEqualTo(BrainTrainingConstant.Raffle.TASK_FINISH_NO) |
|
|
|
.andRecStatusEqualTo(WebConstant.REC_STATUS.Normal.value); |
|
|
|
RaffleTimes delTime = new RaffleTimes(); |
|
|
|
delTime.setRecStatus(WebConstant.REC_STATUS.Deleted.value); |
|
|
|
raffleTimesMapper.updateByExampleSelective(delTime, timesExample); |
|
|
|
RaffleTimes times = new RaffleTimes(); |
|
|
|
times.setId(snowflake.nextId()); |
|
|
|
times.setTaskId(taskId); |
|
|
|
times.setUserId(userId); |
|
|
|
times.setFinishType(status); |
|
|
|
raffleTimesMapper.insertSelective(times); |
|
|
|
} |
|
|
|
|
|
|
|
/*** |
|
|
|
* 判断用户有无关注该公众号 |
|
|
|
* @param token 用户token |
|
|
|
* @param deviceId 用户设备ID |
|
|
|
* @param appId 公众号appID |
|
|
|
* @return 状态 0:未关注 1:关注 |
|
|
|
*/ |
|
|
|
private byte pageSubscript(String token, String deviceId, String appId) { |
|
|
|
TallDto.Tencent tencent = new TallDto.Tencent(); |
|
|
|
tencent.setAppId(appId); |
|
|
|
QueryDto<TallDto.Tencent> dto = new QueryDto<>(); |
|
|
|
dto.setParam(tencent); |
|
|
|
log.info("调用判断接口, dto:{}, token:{}", dto, token); |
|
|
|
Map<String, String> tokenMap = new HashMap<>(); |
|
|
|
Map<String, String> tokenMap = new HashMap<>(4); |
|
|
|
tokenMap.put(BrainTrainingConstant.User.AUTHORIZATION, token); |
|
|
|
tokenMap.put(BrainTrainingConstant.User.DEVICE_ID, deviceId); |
|
|
|
HttpResponse execute = HttpUtil.createPost(subscriptWxUrl).addHeaders(tokenMap).body(JSONObject.toJSONString(dto)).execute(); |
|
|
|