MetaAwardDropService.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package com.izouma.nineth.service;
  2. import com.izouma.nineth.config.AliyunProperties;
  3. import com.izouma.nineth.config.Constants;
  4. import com.izouma.nineth.domain.MetaAwardDrop;
  5. import com.izouma.nineth.dto.MetaAwardDropJsonDTO;
  6. import com.izouma.nineth.dto.MetaRestResult;
  7. import com.izouma.nineth.dto.MetaServiceResult;
  8. import com.izouma.nineth.dto.PageQuery;
  9. import com.izouma.nineth.enums.MetaPropOperationType;
  10. import com.izouma.nineth.exception.BusinessException;
  11. import com.izouma.nineth.repo.MetaAwardDropRepo;
  12. import com.izouma.nineth.utils.JpaUtils;
  13. import com.izouma.nineth.utils.OSSClientUtil;
  14. import com.izouma.nineth.utils.SecurityUtils;
  15. import lombok.AllArgsConstructor;
  16. import org.apache.commons.collections.CollectionUtils;
  17. import org.springframework.data.domain.Page;
  18. import org.springframework.stereotype.Service;
  19. import javax.transaction.Transactional;
  20. import java.io.IOException;
  21. import java.time.LocalDateTime;
  22. import java.util.List;
  23. import java.util.function.BiFunction;
  24. @Service
  25. @AllArgsConstructor
  26. public class MetaAwardDropService {
  27. private AliyunProperties aliyunProperties;
  28. private MetaAwardDropRepo metaAwardDropRepo;
  29. private MetaUserGoldService metaUserGoldService;
  30. private MetaUserPropService metaUserPropService;
  31. public Page<MetaAwardDrop> all(PageQuery pageQuery) {
  32. return metaAwardDropRepo.findAll(JpaUtils.toSpecification(pageQuery, MetaAwardDrop.class), JpaUtils.toPageRequest(pageQuery));
  33. }
  34. @Transactional
  35. public MetaAwardDrop save(MetaAwardDrop record) {
  36. MetaServiceResult result = MetaAwardDrop.checkMetaAtomTask(record);
  37. if (!result.isSuccess()) {
  38. throw new BusinessException(result.getMessage());
  39. }
  40. String operator = SecurityUtils.getAuthenticatedUser().getNickname().concat("(").concat(SecurityUtils.getAuthenticatedUser().getId().toString()).concat(")");
  41. record.setOperator(operator);
  42. record.setOperatingTime(LocalDateTime.now());
  43. MetaAwardDrop save = metaAwardDropRepo.save(record);
  44. String s = "application";
  45. String[] objectKeys = save.getFileUrl().split(s);
  46. if (objectKeys.length != 2) {
  47. throw new BusinessException("Illegal fileUrl");
  48. }
  49. List<MetaAwardDropJsonDTO> metaAwardDropJsonDTOS;
  50. try {
  51. metaAwardDropJsonDTOS = OSSClientUtil.getDataModel(aliyunProperties, s.concat(objectKeys[1]), MetaAwardDropJsonDTO.class);
  52. } catch (IOException e) {
  53. throw new BusinessException(e.getMessage());
  54. }
  55. if (CollectionUtils.isEmpty(metaAwardDropJsonDTOS)) {
  56. throw new BusinessException("数据为空");
  57. }
  58. switch (record.getAwardType()) {
  59. case GOLD:
  60. dropGold(metaAwardDropJsonDTOS, save.getId());
  61. break;
  62. case META_PROP:
  63. dropProp(metaAwardDropJsonDTOS, save.getId());
  64. break;
  65. default:
  66. throw new BusinessException("不支持的奖励类型");
  67. }
  68. return save;
  69. }
  70. private void processDrop(List<MetaAwardDropJsonDTO> metaAwardDropJsonDTOS, Long dropId, BiFunction<MetaAwardDropJsonDTO, Long, MetaRestResult<?>> operation) {
  71. metaAwardDropJsonDTOS.forEach(metaAwardDropJsonDTO -> {
  72. MetaRestResult<?> restResult = operation.apply(metaAwardDropJsonDTO, dropId);
  73. if (restResult.getCode() != Constants.MetaRestCode.success) {
  74. throw new BusinessException(restResult.getMessage());
  75. }
  76. });
  77. }
  78. @Transactional
  79. public void dropGold(List<MetaAwardDropJsonDTO> metaAwardDropJsonDTOS, Long dropId) {
  80. processDrop(metaAwardDropJsonDTOS, dropId, (metaAwardDropJsonDTO, dId) -> {
  81. String message = String.format("玩家[%S]于[%S]通过空投获得金币[%S]个,空投id[%S]", metaAwardDropJsonDTO.getUserId(), LocalDateTime.now(), metaAwardDropJsonDTO.getValue(), dId);
  82. return metaUserGoldService.changeNum(metaAwardDropJsonDTO.getUserId(), metaAwardDropJsonDTO.getValue(), message);
  83. });
  84. }
  85. @Transactional
  86. public void dropProp(List<MetaAwardDropJsonDTO> metaAwardDropJsonDTOS, Long dropId) {
  87. processDrop(metaAwardDropJsonDTOS, dropId, (metaAwardDropJsonDTO, dId) -> {
  88. String message = String.format("玩家[%S]于[%S]通过空投获得道具[%S][%S]个,空投id[%S]", metaAwardDropJsonDTO.getUserId(), LocalDateTime.now(), metaAwardDropJsonDTO.getPropId(), metaAwardDropJsonDTO.getValue(), dId);
  89. return metaUserPropService.operate(metaAwardDropJsonDTO.getUserId(), metaAwardDropJsonDTO.getPropId(), MetaPropOperationType.RECEIVE, metaAwardDropJsonDTO.getValue(), message);
  90. });
  91. }
  92. }