@Override public InvitationCodeDTO validateInvitationCode( String invitationCode, Result result, Map<String, Object> resultData) { InvitationCodeDTO invitationCodeDTO = null; if (StringUtil.isNotEmpty(invitationCode)) { invitationCodeDTO = findInvitationCodeByCode(invitationCode); if (invitationCodeDTO == null || InvitationCodeStatus.BE_USED.equals(invitationCodeDTO.getStatus()) || InvitationCodeStatus.INVALID_OVERDUE.equals(invitationCodeDTO.getStatus())) { result.setSuccess(false); resultData.put("invitationCode", "error"); } else if (InvitationCodeStatus.OVERDUE.equals(invitationCodeDTO.getStatus())) { result.setSuccess(false); resultData.put("invitationCode", "overdue"); } else if (InvitationCodeStatus.EFFECTIVE.equals(invitationCodeDTO.getStatus())) { result.setSuccess(true); } else { result.setSuccess(false); resultData.put("invitationCode", "error"); } } else { result.setSuccess(false); resultData.put("invitationCode", "empty"); } return invitationCodeDTO; }
@Override public void updateInvitationCodeToUsed(String code) { NotificationWriter writer = notificationDaoManager.getWriter(); InvitationCodeDTO dto = writer.findInvitationCodeByCode(code); if (dto == null) { LOG.warn("can't find InvitationCode by code:{}", code); return; } Object status = writer.begin(); try { InvitationCode invitationCode = writer.getById(InvitationCode.class, dto.getId()); invitationCode.setStatus(InvitationCodeStatus.BE_USED); invitationCode.setUsageTime(System.currentTimeMillis()); writer.save(invitationCode); writer.commit(status); } finally { writer.rollback(status); } }
@Override public List<InvitationCodeDTO> createInvitationCodes( InvitationCodeType invitationCodeType, OperatorType inviterType, Long inviterId, OperatorType inviteeType, List<Long> inviteeIds, Long expirationTime, boolean checkingDuplicated) throws Exception { List<InvitationCodeDTO> invitationCodeDTOList = new ArrayList<InvitationCodeDTO>(); if (inviterType == null || inviterId == null || inviteeType == null || CollectionUtil.isEmpty(inviteeIds)) { LOG.warn( "parameters can't be null:[inviterType:" + inviterType + ",inviterId:" + inviterId + ",inviteeType:" + inviteeType + ",inviteeIds:" + inviteeIds); return invitationCodeDTOList; } InvitationCodeDTO dto; String code; List<String> codeList = RandomUtils.randomAlphabeticList(inviteeIds.size()); IConfigService configService = ServiceManager.getService(IConfigService.class); String effectiveDate; if (checkingDuplicated) { long beginning = System.currentTimeMillis(); while (duplicatedInvitationCodeDuplicate(codeList)) { codeList = RandomUtils.randomAlphabeticList(inviteeIds.size()); if (System.currentTimeMillis() - beginning > CREATE_INVITATION_CODE_TIMEOUT) { throw new Exception("create invitationCode timeout exception!"); } } } for (int i = 0; i < codeList.size(); i++) { dto = new InvitationCodeDTO(); dto.setInviterId(inviterId); dto.setInviterType(inviterType); dto.setInviteeId(inviteeIds.get(i)); dto.setInviteeType(inviteeType); dto.setStatus(InvitationCodeStatus.EFFECTIVE); dto.setInviteTime(System.currentTimeMillis()); dto.setType(invitationCodeType); if (expirationTime == null) { effectiveDate = configService.getConfig("INVITATION_CODE_EFFECTIVE_DATE", ShopConstant.BC_SHOP_ID); if (StringUtil.isEmpty(effectiveDate)) { dto.setExpirationTime(System.currentTimeMillis() + DEFAULT_EXPIRATION_TIME); } else { dto.setExpirationTime( System.currentTimeMillis() + Long.valueOf( configService.getConfig( "INVITATION_CODE_EFFECTIVE_DATE", ShopConstant.BC_SHOP_ID))); } } else { dto.setExpirationTime(expirationTime); } code = codeList.get(i); dto.setCode(code); invitationCodeDTOList.add(dto); } saveInvitationCodes(invitationCodeDTOList); return invitationCodeDTOList; }