SpaceSdkServiceImpl.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package com.fdkankan.manage.service.impl;
  2. import cn.dev33.satoken.stp.StpUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  5. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  6. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  7. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  8. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  9. import com.fdkankan.manage.common.FilePath;
  10. import com.fdkankan.manage.common.ResultCode;
  11. import com.fdkankan.manage.exception.BusinessException;
  12. import com.fdkankan.manage.common.PageInfo;
  13. import com.fdkankan.common.util.DateUtil;
  14. import com.fdkankan.common.util.FileUtils;
  15. import com.fdkankan.manage.entity.SysUser;
  16. import com.fdkankan.manage.service.ISysUserService;
  17. import com.fdkankan.manage.entity.SpaceSdk;
  18. import com.fdkankan.manage.mapper.ISpaceSdkMapper;
  19. import com.fdkankan.manage.service.ISpaceSdkService;
  20. import com.fdkankan.manage.vo.request.SpaceSdkParam;
  21. import com.fdkankan.manage.vo.response.SpaceSdkVo;
  22. import lombok.extern.slf4j.Slf4j;
  23. import org.apache.commons.lang3.StringUtils;
  24. import org.springframework.beans.BeanUtils;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.beans.factory.annotation.Value;
  27. import org.springframework.stereotype.Service;
  28. import org.springframework.web.multipart.MultipartFile;
  29. import java.io.File;
  30. import java.io.IOException;
  31. import java.util.ArrayList;
  32. import java.util.Date;
  33. import java.util.List;
  34. /**
  35. * <p>
  36. * space sdk表 服务实现类
  37. * </p>
  38. *
  39. * @author
  40. * @since 2022-06-14
  41. */
  42. @Service
  43. @Slf4j
  44. public class SpaceSdkServiceImpl extends ServiceImpl<ISpaceSdkMapper, SpaceSdk> implements ISpaceSdkService {
  45. private static String DIR_NAME = "new4dkk/v2/images/_/common/data/";
  46. @Autowired
  47. FYunFileServiceInterface fYunFileServiceInterface;
  48. @Autowired
  49. ISysUserService sysUserService;
  50. @Value("${space-sdk.bucket:4dscene}")
  51. private String sdkBucket;
  52. @Value("${space-sdk.url:https://4dscene.4dage.com/}")
  53. private String prefixAli;
  54. @Override
  55. public String upload(MultipartFile file, String version, String imprintCh, String imprintEn, Integer isTop, Integer platformType) throws IOException {
  56. if (!file.isEmpty()&& file.getSize() <= 0) {
  57. throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
  58. }
  59. String fullFileName = "";
  60. if(platformType == 1){
  61. fullFileName = "SpaceTarget_Unity3D";
  62. }else if(platformType == 2){
  63. fullFileName = "SpaceTarget_UE4";
  64. }
  65. String fileNameAll = "";
  66. // 文件名全名
  67. fileNameAll = file.getOriginalFilename();
  68. // 将文件转字节-> 字符串
  69. // 获取类路径
  70. String resourcePath = FilePath.sdkLocalPath;
  71. log.info("resourcePath: {}", resourcePath);
  72. // 创建目录
  73. String dirPath = resourcePath + DIR_NAME;
  74. FileUtils.createDir(dirPath);
  75. // 拼接唯一文件名
  76. String fileName = fullFileName + "_" + version + "_" + DateUtil.dateStr() + fileNameAll;
  77. // 文件保存路径
  78. String filePath = dirPath + DateUtil.dateStr() + fileNameAll;
  79. // 写文件到本地
  80. File file1 = new File(filePath);
  81. file.transferTo(file1);
  82. log.info("filePath: {}", filePath);
  83. // 上传到阿里云sso
  84. fYunFileServiceInterface.uploadFile(sdkBucket,filePath, DIR_NAME + fileName);
  85. log.info("upload success");
  86. String url = prefixAli + DIR_NAME + fileName;
  87. log.info("upload url: {}" + url);
  88. SpaceSdk managerSdkEntity = new SpaceSdk();
  89. managerSdkEntity.setVersion(version);
  90. managerSdkEntity.setImprintCh(imprintCh);
  91. managerSdkEntity.setImprintEn(imprintEn);
  92. managerSdkEntity.setIsTop(isTop);
  93. managerSdkEntity.setStatus(0);
  94. managerSdkEntity.setPlatformType(platformType.toString());
  95. managerSdkEntity.setFileUrl(url);
  96. managerSdkEntity.setFileName(fullFileName);
  97. managerSdkEntity.setSysUserId(Long.valueOf(StpUtil.getLoginId().toString()));
  98. this.save(managerSdkEntity);
  99. if(isTop == 1){
  100. this.updateTopFBySdkId(managerSdkEntity.getId());//取消所有置顶
  101. }
  102. this.updateById(managerSdkEntity);
  103. // 删除本地文件
  104. FileUtils.deleteFile(filePath);
  105. return url;
  106. }
  107. @Override
  108. public void updateByEntity(SpaceSdk param) {
  109. if(param.getId() == null || StringUtils.isEmpty(param.getPlatformType()) || param.getIsTop()==null){
  110. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  111. }
  112. if(param.getIsTop().equals(1)){
  113. this.updateTopFBySdkId(param.getId());//取消所有置顶
  114. }
  115. this.updateById(param);
  116. }
  117. @Override
  118. public void online(SpaceSdkParam param) {
  119. if(param.getId() == null || param.getStatus() == null){
  120. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  121. }
  122. SpaceSdk managerSdkEntity = this.getById(param.getId());
  123. if(managerSdkEntity == null ){
  124. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  125. }
  126. if(managerSdkEntity.getPublishTime()!=null){
  127. this.updateBySdkId(param.getId(),param.getStatus());
  128. }else{
  129. this.updateBySdkIdPublish(param.getId(),param.getStatus());
  130. }
  131. }
  132. @Override
  133. public void top(SpaceSdkParam param) {
  134. if(param.getId() == null || param.getIsTop()==null){
  135. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  136. }
  137. if(param.getIsTop().equals(1)){
  138. this.updateTopFBySdkId(param.getId());//取消所有置顶
  139. }
  140. LambdaUpdateWrapper<SpaceSdk> updateWrapper = new LambdaUpdateWrapper<>();
  141. updateWrapper.set(SpaceSdk::getIsTop,param.getIsTop())
  142. .eq(SpaceSdk::getId,param.getId());
  143. this.update(updateWrapper);
  144. }
  145. @Override
  146. public PageInfo pageList(SpaceSdkParam param) {
  147. LambdaQueryWrapper<SpaceSdk> queryWrapper = Wrappers.lambdaQuery();
  148. if(param.getStatus()!=null){
  149. queryWrapper.eq(SpaceSdk::getStatus,param.getStatus());
  150. }
  151. if(StringUtils.isNotEmpty(param.getVersion())){
  152. queryWrapper.like(SpaceSdk::getVersion,param.getVersion());
  153. }
  154. if(param.getPlatformType() == null){
  155. param.setPlatformType(1);
  156. }
  157. queryWrapper.eq(SpaceSdk::getPlatformType,param.getPlatformType());
  158. queryWrapper.orderByDesc(SpaceSdk::getIsTop);
  159. queryWrapper.orderByDesc(SpaceSdk::getStatus);
  160. queryWrapper.orderByDesc(SpaceSdk::getCreateTime);
  161. Page<SpaceSdk> page = this.page(new Page<>(param.getPageNum(),param.getPageSize()), queryWrapper);
  162. List<SpaceSdkVo> voList = new ArrayList<>();
  163. for (SpaceSdk record : page.getRecords()) {
  164. SpaceSdkVo vo = new SpaceSdkVo();
  165. BeanUtils.copyProperties(record,vo);
  166. if(record.getSysUserId() !=null){
  167. SysUser user = sysUserService.getById(record.getSysUserId());
  168. if(user != null){
  169. vo.setCreateName(user.getNickName());
  170. }
  171. }
  172. voList.add(vo);
  173. }
  174. Page<SpaceSdkVo> voPage = new Page<>(param.getPageNum(),param.getPageSize());
  175. voPage.setRecords(voList);
  176. voPage.setTotal(page.getTotal());
  177. return PageInfo.PageInfo(voPage);
  178. }
  179. private void updateBySdkId(Long id, Integer status) {
  180. LambdaUpdateWrapper<SpaceSdk> updateWrapper = new LambdaUpdateWrapper<>();
  181. updateWrapper.set(SpaceSdk::getStatus,status == 2 ? 0:status)
  182. .eq(SpaceSdk::getId,id);
  183. this.update(updateWrapper);
  184. }
  185. private void updateTopFBySdkId(Long sdkId) {
  186. SpaceSdk sdk = this.getById(sdkId);
  187. if(sdk!=null){
  188. LambdaUpdateWrapper<SpaceSdk> updateWrapper = new LambdaUpdateWrapper<>();
  189. updateWrapper.set(SpaceSdk::getIsTop,0)
  190. .eq(SpaceSdk::getPlatformType,sdk.getPlatformType());
  191. this.update(updateWrapper);
  192. }
  193. }
  194. private void updateBySdkIdPublish(Long id, Integer status) {
  195. LambdaUpdateWrapper<SpaceSdk> updateWrapper = new LambdaUpdateWrapper<>();
  196. updateWrapper.set(SpaceSdk::getStatus,status == 2 ? 0 :status)
  197. .set(SpaceSdk::getPublishTime,new Date())
  198. .eq(SpaceSdk::getId,id);
  199. this.update(updateWrapper);
  200. }
  201. }