| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- package com.izouma.nineth.service;
- import com.izouma.nineth.config.AliyunProperties;
- import com.izouma.nineth.config.Constants;
- import com.izouma.nineth.domain.MetaAwardDrop;
- import com.izouma.nineth.dto.MetaAwardDropJsonDTO;
- import com.izouma.nineth.dto.MetaRestResult;
- import com.izouma.nineth.dto.MetaServiceResult;
- import com.izouma.nineth.dto.PageQuery;
- import com.izouma.nineth.enums.MetaPropOperationType;
- import com.izouma.nineth.exception.BusinessException;
- import com.izouma.nineth.repo.MetaAwardDropRepo;
- import com.izouma.nineth.utils.JpaUtils;
- import com.izouma.nineth.utils.OSSClientUtil;
- import com.izouma.nineth.utils.SecurityUtils;
- import lombok.AllArgsConstructor;
- import org.apache.commons.collections.CollectionUtils;
- import org.springframework.data.domain.Page;
- import org.springframework.stereotype.Service;
- import javax.transaction.Transactional;
- import java.io.IOException;
- import java.time.LocalDateTime;
- import java.util.List;
- import java.util.function.BiFunction;
- @Service
- @AllArgsConstructor
- public class MetaAwardDropService {
- private AliyunProperties aliyunProperties;
- private MetaAwardDropRepo metaAwardDropRepo;
- private MetaUserGoldService metaUserGoldService;
- private MetaUserPropService metaUserPropService;
- public Page<MetaAwardDrop> all(PageQuery pageQuery) {
- return metaAwardDropRepo.findAll(JpaUtils.toSpecification(pageQuery, MetaAwardDrop.class), JpaUtils.toPageRequest(pageQuery));
- }
- @Transactional
- public MetaAwardDrop save(MetaAwardDrop record) {
- MetaServiceResult result = MetaAwardDrop.checkMetaAtomTask(record);
- if (!result.isSuccess()) {
- throw new BusinessException(result.getMessage());
- }
- String operator = SecurityUtils.getAuthenticatedUser().getNickname().concat("(").concat(SecurityUtils.getAuthenticatedUser().getId().toString()).concat(")");
- record.setOperator(operator);
- record.setOperatingTime(LocalDateTime.now());
- MetaAwardDrop save = metaAwardDropRepo.save(record);
- String s = "application";
- String[] objectKeys = save.getFileUrl().split(s);
- if (objectKeys.length != 2) {
- throw new BusinessException("Illegal fileUrl");
- }
- List<MetaAwardDropJsonDTO> metaAwardDropJsonDTOS;
- try {
- metaAwardDropJsonDTOS = OSSClientUtil.getDataModel(aliyunProperties, s.concat(objectKeys[1]), MetaAwardDropJsonDTO.class);
- } catch (IOException e) {
- throw new BusinessException(e.getMessage());
- }
- if (CollectionUtils.isEmpty(metaAwardDropJsonDTOS)) {
- throw new BusinessException("数据为空");
- }
- switch (record.getAwardType()) {
- case GOLD:
- dropGold(metaAwardDropJsonDTOS, save.getId());
- break;
- case META_PROP:
- dropProp(metaAwardDropJsonDTOS, save.getId());
- break;
- default:
- throw new BusinessException("不支持的奖励类型");
- }
- return save;
- }
- private void processDrop(List<MetaAwardDropJsonDTO> metaAwardDropJsonDTOS, Long dropId, BiFunction<MetaAwardDropJsonDTO, Long, MetaRestResult<?>> operation) {
- metaAwardDropJsonDTOS.forEach(metaAwardDropJsonDTO -> {
- MetaRestResult<?> restResult = operation.apply(metaAwardDropJsonDTO, dropId);
- if (restResult.getCode() != Constants.MetaRestCode.success) {
- throw new BusinessException(restResult.getMessage());
- }
- });
- }
- @Transactional
- public void dropGold(List<MetaAwardDropJsonDTO> metaAwardDropJsonDTOS, Long dropId) {
- processDrop(metaAwardDropJsonDTOS, dropId, (metaAwardDropJsonDTO, dId) -> {
- String message = String.format("玩家[%S]于[%S]通过空投获得金币[%S]个,空投id[%S]", metaAwardDropJsonDTO.getUserId(), LocalDateTime.now(), metaAwardDropJsonDTO.getValue(), dId);
- return metaUserGoldService.changeNum(metaAwardDropJsonDTO.getUserId(), metaAwardDropJsonDTO.getValue(), message);
- });
- }
- @Transactional
- public void dropProp(List<MetaAwardDropJsonDTO> metaAwardDropJsonDTOS, Long dropId) {
- processDrop(metaAwardDropJsonDTOS, dropId, (metaAwardDropJsonDTO, dId) -> {
- String message = String.format("玩家[%S]于[%S]通过空投获得道具[%S][%S]个,空投id[%S]", metaAwardDropJsonDTO.getUserId(), LocalDateTime.now(), metaAwardDropJsonDTO.getPropId(), metaAwardDropJsonDTO.getValue(), dId);
- return metaUserPropService.operate(metaAwardDropJsonDTO.getUserId(), metaAwardDropJsonDTO.getPropId(), MetaPropOperationType.RECEIVE, metaAwardDropJsonDTO.getValue(), message);
- });
- }
- }
|