一.定义接口
public interface PearlTaskService { Integer getTaskType(); Map<String,Integer> execute(Long userId, GameTask gameTask, String gameCode); }
二.定义抽象类
@Slf4j public abstract class PearlTaskStrategy implements PearlTaskService { protected GameLabelDurationFacade gameLabelDurationFacade; public PearlTaskStrategy(GameLabelDurationFacade gameLabelDurationFacade) { this.gameLabelDurationFacade = gameLabelDurationFacade;//由子类负责初始化 } /** * 查询某一天在线时长 * * @param userId * @param gameCode * @param beginTime * @param endTime * @return */ public int getOnlineDuration(Long userId, String gameCode, LocalDateTime beginTime, LocalDateTime endTime) { Long startTime = System.currentTimeMillis(); JSONObject resultObject = gameLabelDurationFacade.getGameTimeRange(userId, gameCode, beginTime, endTime); Long time = System.currentTimeMillis(); Long seconds = (time - startTime) ; if (resultObject == null) { return 0; } //接口返回的时长单位是秒 Float duration = resultObject.getFloat("total_time"); if (duration == null) { return 0; } //返回分钟 int min = (int) (duration / 60); return min; } }
三.创建业务类
/** * 今日在线任务服务实现类 */ @Slf4j @Service public class TodayOnlineTaskServiceImpl extends PearlTaskStrategy { @Autowired private UserGameTaskService userGameTaskService; @Autowired public TodayOnlineTaskServiceImpl(GameLabelDurationFacade gameLabelDurationFacade) { super(gameLabelDurationFacade);//初始化基类里的接口 } @Override @RedisLock(spEL = "'member_pearl_to_task_#' + #userId", waitTime = 5000L) public Map<String,Integer> execute(Long userId, GameTask gameTask, String gameCode) { Long beginTime = System.currentTimeMillis(); Map<String,Integer> hashMap = new HashMap<>(); int status = 1; LocalDateTime start = LocalDate.now().atStartOfDay(); LocalDateTime end = LocalDateTime.now(); Integer gameTaskId = gameTask.getId(); UserGameTask todayTask = userGameTaskService.findUserGameTask(userId, gameTaskId, start);//查询今日在线时长 int min = getOnlineDuration(userId, gameCode, start, end); log.info("[TodayOnlineTaskServiceImpl.execute]用户id为:{} ,getOnlineDays返回结果 :{}", userId, min); if (todayTask != null) { if (UserGameTaskStatusConstants.UNDONE.getCode() == todayTask.getStatus()) { if (min >= Integer.parseInt(gameTask.getUnitNum())) { status = UserGameTaskStatusConstants.GETTING.getCode(); userGameTaskService.updateStatus(status, null, todayTask.getId()); hashMap.put("status", status); return hashMap; } } else { status = todayTask.getStatus(); } } else { UserGameTask userGameTask = new UserGameTask(); if (min >= Integer.parseInt(gameTask.getUnitNum())) { status = UserGameTaskStatusConstants.GETTING.getCode(); } else { status = UserGameTaskStatusConstants.UNDONE.getCode(); } userGameTask.setStatus(status); userGameTask.setUserId(userId); userGameTask.setGameTaskId(gameTask.getId()); userGameTaskService.insert(userGameTask); } hashMap.put("status", status);return hashMap; } @Override public Integer getTaskType() { return UserGameTaskTypeConstants.TODAY_ONLINE.getCode(); } }
四.调用:使用InitializingBean对接口初始化,根据每个业务类的getTaskType()调用不同的业务类
@Service @Slf4j public class UserGameTaskFacadeImpl implements UserGameTaskFacade, InitializingBean { @Autowired private List<PearlTaskService> services; private Map<Integer, PearlTaskService> pearlTaskStrategyMap; @Autowired private GameTaskService gameTaskService; /** * 福利任务维护 * * @param userGameTaskReqDto * @return */ @Override public List<UserGameTaskRespDto> pearlActivityExecute(UserGameTaskReqDto userGameTaskReqDto) { Long userId = userGameTaskReqDto.getUserId(); Integer gameId = userGameTaskReqDto.getGameId(); GameDto gameDto = gameTaskService.findTaskGameInfoById(gameId); if (gameDto == null) { throw new BusinessException(BusinessConstants.NORMAL_PARAM_ERROR.getCode(), "游戏不存在!"); } String gameCode = gameDto.getCode(); GameTask param = new GameTask(); param.setType(gameDto.getType()); param.setGameId(gameId); param.setOrigin(userGameTaskReqDto.getOrigin()); param.setSys(userGameTaskReqDto.getSys()); //查游戏有哪些活动任务 List<GameTask> gameTasks = gameTaskService.findGameTaskList(param); if (gameTasks == null) { throw new BusinessException(BusinessConstants.NORMAL_BUSINESS_ERROR.getCode(), "游戏任务不存在!"); } List<UserGameTaskRespDto> respDtos = new ArrayList<>(); for (GameTask gameTask : gameTasks) { Integer taskType = gameTask.getTaskType(); UserGameTaskRespDto respDto = new UserGameTaskRespDto(); CopyUtils.copy(respDto, gameTask); respDto.setGameTaskId(gameTask.getId()); respDto.setTaskType(gameTask.getTaskType()); if (taskType.equals(UserGameTaskTypeConstants.TODAY_ONLINE.getCode())) { respDto.setTaskName( UserGameTaskTypeConstants.TODAY_ONLINE.getMsg() + gameTask.getUnitNum() + CommonPearlConstants.minutes); //每日在线时长任务 PearlTaskService pearlTaskService = pearlTaskStrategyMap.get(UserGameTaskTypeConstants.TODAY_ONLINE.getCode()); Map hashMap = pearlTaskService.execute(userId, gameTask, gameCode); respDto.setStatus((Integer) hashMap.get("status")); } if (taskType.equals(UserGameTaskTypeConstants.YESTERDAY_ONLINE.getCode())) { respDto.setTaskName( UserGameTaskTypeConstants.YESTERDAY_ONLINE.getMsg() + gameTask.getUnitNum() + CommonPearlConstants.minutes); //昨日在线时长任务 PearlTaskService pearlTaskService = pearlTaskStrategyMap.get(UserGameTaskTypeConstants.YESTERDAY_ONLINE.getCode()); Map hashMap = pearlTaskService.execute(userId, gameTask, gameCode); respDto.setStatus((Integer) hashMap.get("status")); } respDtos.add(respDto); } return respDtos; } @Override public void afterPropertiesSet() { log.info("[afterPropertiesSet]services :{}",JsonUtils.obj2String(services)); pearlTaskStrategyMap = services.stream().collect(Collectors.toMap(PearlTaskService::getTaskType, Function.identity())); log.info("[afterPropertiesSet]pearlTaskStrategyMap :{}",JsonUtils.obj2String(pearlTaskStrategyMap)); } }
标签:status,getCode,springboot,userId,gameTask,模式,param,public,策略 From: https://www.cnblogs.com/Anti-General/p/17685922.html