SpaceSdkServiceImpl.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package com.fdkankan.manage.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  4. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  7. import com.fdkankan.common.constant.ErrorCode;
  8. import com.fdkankan.common.exception.BusinessException;
  9. import com.fdkankan.common.response.PageInfo;
  10. import com.fdkankan.common.util.DateUtil;
  11. import com.fdkankan.common.util.FileUtils;
  12. import com.fdkankan.manage.util.UploadToOssUtil;
  13. import com.fdkankan.manage.entity.SpaceSdk;
  14. import com.fdkankan.manage.mapper.ISpaceSdkMapper;
  15. import com.fdkankan.manage.service.ISpaceSdkService;
  16. import com.fdkankan.manage.vo.request.SpaceSdkParam;
  17. import lombok.extern.slf4j.Slf4j;
  18. import org.apache.commons.lang3.StringUtils;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.beans.factory.annotation.Value;
  21. import org.springframework.stereotype.Service;
  22. import org.springframework.web.multipart.MultipartFile;
  23. import java.io.IOException;
  24. import java.util.Base64;
  25. import java.util.Date;
  26. /**
  27. * <p>
  28. * space sdk表 服务实现类
  29. * </p>
  30. *
  31. * @author
  32. * @since 2022-06-14
  33. */
  34. @Service
  35. @Slf4j
  36. public class SpaceSdkServiceImpl extends ServiceImpl<ISpaceSdkMapper, SpaceSdk> implements ISpaceSdkService {
  37. private static String DIR_NAME = "new4dkk/v2/images/_/common/data/";
  38. @Autowired
  39. private UploadToOssUtil uploadToOssUtil;
  40. @Value("${oss.prefix.sdk}")
  41. private String prefixAli;
  42. @Override
  43. public String upload(MultipartFile file, String version, String imprintCh, String imprintEn, Integer isTop, Integer platformType) throws IOException {
  44. if (!file.isEmpty()&& file.getSize() <= 0) {
  45. throw new BusinessException(ErrorCode.FILE_NOT_EXIST);
  46. }
  47. String fullFileName = "";
  48. if(platformType == 1){
  49. fullFileName = "SpaceTarget_Unity3D";
  50. }else if(platformType == 2){
  51. fullFileName = "SpaceTarget_UE4";
  52. }
  53. String fileNameAll = "";
  54. // 文件名全名
  55. fileNameAll = file.getOriginalFilename();
  56. // 将文件转字节-> 字符串
  57. String fileContent = Base64.getEncoder().encodeToString(file.getBytes());
  58. // 获取类路径
  59. String resourcePath = FileUtils.getResource();
  60. log.info("resourcePath: {}", resourcePath);
  61. // 创建目录
  62. String dirPath = resourcePath + DIR_NAME;
  63. FileUtils.createDir(dirPath);
  64. // 拼接唯一文件名
  65. String fileName = fullFileName + "_" + version + "_" + DateUtil.dateStr() + fileNameAll;
  66. // 文件保存路径
  67. String filePath = dirPath + DateUtil.dateStr() + fileNameAll;
  68. // 写文件到本地
  69. FileUtils.base64ToFileWriter(fileContent, filePath);
  70. log.info("filePath: {}", filePath);
  71. // 上传到阿里云sso
  72. uploadToOssUtil.uploadSdk(filePath, DIR_NAME + fileName);
  73. log.info("upload success");
  74. String url = prefixAli + DIR_NAME + fileName;
  75. log.info("upload url: {}" + url);
  76. SpaceSdk managerSdkEntity = new SpaceSdk();
  77. if(isTop == 1){
  78. this.updateTopFBySdkId(platformType.toString());//取消所有置顶
  79. }
  80. managerSdkEntity.setVersion(version);
  81. managerSdkEntity.setImprintCh(imprintCh);
  82. managerSdkEntity.setImprintEn(imprintEn);
  83. managerSdkEntity.setIsTop(isTop);
  84. managerSdkEntity.setStatus(0);
  85. managerSdkEntity.setPlatformType(platformType.toString());
  86. managerSdkEntity.setFileUrl(url);
  87. managerSdkEntity.setFileName(fullFileName);
  88. this.save(managerSdkEntity);
  89. // 删除本地文件
  90. FileUtils.deleteFile(filePath);
  91. return url;
  92. }
  93. @Override
  94. public void updateByEntity(SpaceSdk param) {
  95. if(param.getId() == null || StringUtils.isEmpty(param.getPlatformType()) || param.getIsTop()==null){
  96. throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
  97. }
  98. if(param.getIsTop().equals(1)){
  99. this.updateTopFBySdkId(param.getPlatformType());//取消所有置顶
  100. }
  101. if(!this.updateById(param)){
  102. throw new BusinessException(ErrorCode.ERROR_MSG);
  103. }
  104. }
  105. @Override
  106. public void online(SpaceSdkParam param) {
  107. if(param.getId() == null || param.getStatus() == null){
  108. throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
  109. }
  110. SpaceSdk managerSdkEntity = this.getById(param.getId());
  111. if(managerSdkEntity == null ){
  112. throw new BusinessException(-1,"获取记录为空");
  113. }
  114. if(managerSdkEntity.getPublishTime()!=null){
  115. this.updateBySdkId(param.getId(),param.getStatus());
  116. }else{
  117. this.updateBySdkIdPublish(param.getId(),param.getStatus());
  118. }
  119. }
  120. @Override
  121. public void top(SpaceSdkParam param) {
  122. if(param.getId() == null || param.getIsTop()==null){
  123. throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
  124. }
  125. if(param.getIsTop().equals(1)){
  126. this.updateTopFBySdkId(param.getPlatformType());//取消所有置顶
  127. }
  128. LambdaUpdateWrapper<SpaceSdk> updateWrapper = new LambdaUpdateWrapper<>();
  129. updateWrapper.set(SpaceSdk::getIsTop,param.getIsTop())
  130. .eq(SpaceSdk::getId,param.getId());
  131. this.update(updateWrapper);
  132. }
  133. @Override
  134. public PageInfo pageList(SpaceSdkParam param) {
  135. LambdaQueryWrapper<SpaceSdk> queryWrapper = Wrappers.lambdaQuery();
  136. if(param.getStatus()!=null){
  137. queryWrapper.eq(SpaceSdk::getStatus,param.getStatus());
  138. }
  139. if(StringUtils.isNotEmpty(param.getVersion())){
  140. queryWrapper.like(SpaceSdk::getVersion,param.getVersion());
  141. }
  142. if(StringUtils.isNotEmpty(param.getPlatformType())){
  143. queryWrapper.like(SpaceSdk::getPlatformType,param.getPlatformType());
  144. }
  145. queryWrapper.orderByDesc(SpaceSdk::getIsTop);
  146. queryWrapper.orderByDesc(SpaceSdk::getPublishTime);
  147. Page<SpaceSdk> page = this.page(new Page<>(param.getPageNum(),param.getPageSize()), queryWrapper);
  148. return PageInfo.PageInfo(page);
  149. }
  150. private void updateBySdkId(Long id, Integer status) {
  151. LambdaUpdateWrapper<SpaceSdk> updateWrapper = new LambdaUpdateWrapper<>();
  152. updateWrapper.set(SpaceSdk::getStatus,status)
  153. .eq(SpaceSdk::getId,id);
  154. this.update(updateWrapper);
  155. }
  156. private void updateTopFBySdkId(String platformType) {
  157. LambdaUpdateWrapper<SpaceSdk> updateWrapper = new LambdaUpdateWrapper<>();
  158. updateWrapper.set(SpaceSdk::getIsTop,0)
  159. .eq(SpaceSdk::getPlatformType,platformType);
  160. this.update(updateWrapper);
  161. }
  162. private void updateBySdkIdPublish(Long id, Integer status) {
  163. LambdaUpdateWrapper<SpaceSdk> updateWrapper = new LambdaUpdateWrapper<>();
  164. updateWrapper.set(SpaceSdk::getStatus,status)
  165. .set(SpaceSdk::getPublishTime,new Date())
  166. .eq(SpaceSdk::getId,id);
  167. this.update(updateWrapper);
  168. }
  169. }