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 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 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 metaAwardDropJsonDTOS, Long dropId, BiFunction> 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 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 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); }); } }