Browse Source

检查任务权限和完成任务

master
zhizhi wu 3 years ago
parent
commit
45099ecc6d
  1. 2
      src/main/java/com/ccsens/braintraining/api/RaffleController.java
  2. 1
      src/main/java/com/ccsens/braintraining/api/WxTencentController.java
  3. 4
      src/main/java/com/ccsens/braintraining/bean/vo/RaffleVo.java
  4. 3
      src/main/java/com/ccsens/braintraining/service/IRaffleService.java
  5. 111
      src/main/java/com/ccsens/braintraining/service/RaffleService.java
  6. 1
      src/main/java/com/ccsens/braintraining/util/BrainTrainingCodeError.java
  7. 4
      src/main/resources/application.yml
  8. 4
      src/main/resources/mapper_dao/RaffleDao.xml

2
src/main/java/com/ccsens/braintraining/api/RaffleController.java

@ -88,7 +88,7 @@ public class RaffleController {
@RequestMapping(value = "/doTask", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
public JsonResponse<RaffleVo.DoTask> doTask(@ApiParam @Validated @RequestBody QueryDto<RaffleDto.DoTask> params) {
log.info("完成任务:{}", params);
RaffleVo.DoTask prizes = raffleService.doTask(params.getParam(), params.getUserId());
RaffleVo.DoTask prizes = raffleService.doTask(params.getParam(), params.getUserId(), params.getToken(), params.getDeviceId());
log.info("完成任务结束:{}", prizes);
return JsonResponse.newInstance().ok(prizes);
}

1
src/main/java/com/ccsens/braintraining/api/WxTencentController.java

@ -37,6 +37,7 @@ import java.util.UUID;
public class WxTencentController {
private String token = "ccsensChuanKong";
// 北京:yYIEmttGjhbcWUoPg9agVGrF4XPnOCh8QpsRr6s4ACk
private String encodingAesKey = "nysgv5sKS5HPMMsHiVcDOE9BBCtX7Ho5DSv3s0qS9b9";
@Resource

4
src/main/java/com/ccsens/braintraining/bean/vo/RaffleVo.java

@ -121,9 +121,9 @@ public class RaffleVo {
@ApiModel("用户做任务-响应")
public static class DoTask {
@ApiModelProperty("增加的次数")
private Integer addTimes;
private Integer addTimes = 0;
@ApiModelProperty("总抽奖次数")
private Long totalTimes;
private Integer totalTimes = 0;
}
@Data

3
src/main/java/com/ccsens/braintraining/service/IRaffleService.java

@ -47,7 +47,7 @@ public interface IRaffleService {
* @param userId 用户
* @return 成功与否
*/
RaffleVo.DoTask doTask(RaffleDto.DoTask param, Long userId);
RaffleVo.DoTask doTask(RaffleDto.DoTask param, Long userId, String token, String deviceId);
/**
* 检查用户关于任务的完成情况
@ -58,4 +58,5 @@ public interface IRaffleService {
* @return 完成情况
*/
RaffleVo.CheckTask checkTask(RaffleDto.DoTask param, Long userId, String token, String deviceId);
}

111
src/main/java/com/ccsens/braintraining/service/RaffleService.java

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

1
src/main/java/com/ccsens/braintraining/util/BrainTrainingCodeError.java

@ -13,6 +13,7 @@ public class BrainTrainingCodeError extends CodeError {
public static final Code RAFFLE_ACTIVE_NOT_OPEN = new Code(502,"抽奖活动暂未开放,请开发后再参与活动。",true);
public static final Code RAFFLE_ACTIVE_NOT_TIMES = new Code(503,"您的抽奖机会已经用完了。",true);
public static final Code RAFFLE_ACTIVE_PRIZE_NO = new Code(504,"亟待运营人员补充奖品,请您稍后再试。",true);
public static final Code RAFFLE_TIMES_REFRESH = new Code(505,"页面停留太久了,请刷新后重试。",true);
}

4
src/main/resources/application.yml

@ -1,4 +1,4 @@
spring:
profiles:
active: dev
include: common, util-dev
active: test
include: common, util-test

4
src/main/resources/mapper_dao/RaffleDao.xml

@ -117,7 +117,7 @@
t1.id = t2.task_id
AND t1.active_id = #{activeId}
AND t2.user_id = #{userId}
AND t2.finish_type = 0
AND t2.finish_type = 1
AND t1.rec_status = 0
AND t2.rec_status = 0
) t,
@ -201,7 +201,7 @@
AND t.task_id = t2.id
AND t1.id = #{taskId}
AND t.user_id = #{userId}
AND t.finish_type > 0
AND t.finish_type in (1,2)
AND t1.rec_status = 0
AND p1.rec_status = 0
AND t2.rec_status = 0

Loading…
Cancel
Save