CameraVersionServiceImpl.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.extension.plugins.pagination.Page;
  5. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  6. import com.fdkankan.common.constant.ErrorCode;
  7. import com.fdkankan.common.exception.BusinessException;
  8. import com.fdkankan.common.response.PageInfo;
  9. import com.fdkankan.common.util.DateUtil;
  10. import com.fdkankan.common.util.FileMd5Util;
  11. import com.fdkankan.common.util.FileUtils;
  12. import com.fdkankan.common.validation.ValidationUtils;
  13. import com.fdkankan.manage.common.ResultCode;
  14. import com.fdkankan.manage.util.MangerUploadToOssUtil;
  15. import com.fdkankan.manage.entity.CameraVersion;
  16. import com.fdkankan.manage.mapper.ICameraVersionMapper;
  17. import com.fdkankan.manage.service.ICameraVersionService;
  18. import com.fdkankan.manage.vo.request.CameraVersionParam;
  19. import lombok.extern.slf4j.Slf4j;
  20. import org.apache.commons.lang3.StringUtils;
  21. import org.springframework.beans.factory.annotation.Value;
  22. import org.springframework.stereotype.Service;
  23. import org.springframework.web.multipart.MultipartFile;
  24. import javax.annotation.Resource;
  25. import java.io.File;
  26. import java.io.IOException;
  27. import java.util.Base64;
  28. import java.util.List;
  29. /**
  30. * <p>
  31. * 相机版本表 服务实现类
  32. * </p>
  33. *
  34. * @author
  35. * @since 2022-06-15
  36. */
  37. @Service
  38. @Slf4j
  39. public class CameraVersionServiceImpl extends ServiceImpl<ICameraVersionMapper, CameraVersion> implements ICameraVersionService {
  40. public static String DIR_NAME = "camera_version/";
  41. public static String YAMAXUN_S3_PATH = "https://eurs3.4dkankan.com/camera_version/";
  42. @Value("${upload.type}")
  43. private String ossType;
  44. @Value("${oss.prefix.ali}")
  45. private String prefixAli;
  46. @Resource
  47. private MangerUploadToOssUtil mangeUploadToOssUtil;
  48. @Override
  49. public void addAndUpload(MultipartFile file, String version, String description, String minVersion, Integer type) throws IOException {
  50. log.info("run upload");
  51. if (!file.isEmpty()&& file.getSize() <= 0) {
  52. throw new BusinessException(ErrorCode.FILE_NOT_EXIST);
  53. }
  54. // 文件名全名
  55. String fullFileName = 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 = DateUtil.dateStr() + fullFileName;
  66. // 文件保存路径
  67. String filePath = dirPath + DateUtil.dateStr() + fullFileName;
  68. // 写文件到本地
  69. FileUtils.base64ToFileWriter(fileContent, filePath);
  70. log.info("filePath: {}", filePath);
  71. // 上传到阿里云sso
  72. mangeUploadToOssUtil.upload(filePath, DIR_NAME + fileName);
  73. log.info("upload success");
  74. String url = prefixAli + DIR_NAME + fileName;
  75. if("aws".equals(ossType)){
  76. url = YAMAXUN_S3_PATH + fileName;
  77. }
  78. log.info("upload url: {}" + url);
  79. // 添加对象信息
  80. List<CameraVersion> cameraVersions = this.getByVersion(version);
  81. if(cameraVersions != null && cameraVersions.size() >0){
  82. throw new BusinessException(ResultCode.VISION_EXIST.code(),ResultCode.VISION_EXIST.message());
  83. }
  84. CameraVersion versionEntity = new CameraVersion();
  85. versionEntity.setName(fileName);
  86. versionEntity.setFileUrl(url);
  87. versionEntity.setVersion(version);
  88. versionEntity.setDescription(description);
  89. versionEntity.setType(type);
  90. versionEntity.setMinVersion(minVersion);
  91. versionEntity.setStatus("I");
  92. versionEntity.setFileMd5(FileMd5Util.getFileMD5(new File(filePath)));
  93. this.save(versionEntity);
  94. // 删除本地文件
  95. FileUtils.deleteFile(filePath);
  96. }
  97. private List<CameraVersion> getByVersion(String version) {
  98. LambdaQueryWrapper<CameraVersion> wrapper = new LambdaQueryWrapper<>();
  99. wrapper.eq(CameraVersion::getVersion,version);
  100. return this.list(wrapper);
  101. }
  102. @Override
  103. public PageInfo pageList(CameraVersionParam param) {
  104. LambdaQueryWrapper<CameraVersion> queryWrapper = new LambdaQueryWrapper<>();
  105. queryWrapper.eq(CameraVersion::getType,param.getType());
  106. if (StringUtils.isNotBlank(param.getVersion())) {
  107. queryWrapper.and(wrapper -> wrapper
  108. .like(CameraVersion::getName,param.getVersion())
  109. .or()
  110. .like(CameraVersion::getVersion,param.getVersion()));
  111. }
  112. queryWrapper.orderByDesc(CameraVersion::getCreateTime);
  113. Page<CameraVersion> page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), queryWrapper);
  114. return PageInfo.PageInfo(page);
  115. }
  116. @Override
  117. public void updateByParam(CameraVersion param) {
  118. if(param.getId() == null){
  119. throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
  120. }
  121. CameraVersion cameraVersion = this.getById(param.getId());
  122. if(cameraVersion == null){
  123. throw new BusinessException(ErrorCode.NOT_RECORD);
  124. }
  125. if(StringUtils.isNotBlank(param.getStatus()) && !param.getStatus().equals(cameraVersion.getStatus())){
  126. if(StringUtils.isBlank(param.getStatus()) || param.getType() == null){
  127. throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
  128. }
  129. if (!ValidationUtils.validateRecStatus(param.getStatus())) {
  130. throw new BusinessException(ErrorCode.ERROR_MSG);
  131. }
  132. // 仅有有一台相机是活动状态
  133. // 查找所以活动状态相机
  134. if(param.getStatus().equals("A")){
  135. LambdaUpdateWrapper<CameraVersion> updateWrapper = new LambdaUpdateWrapper<>();
  136. updateWrapper.set(CameraVersion::getStatus,"I")
  137. .eq(CameraVersion::getStatus,"A")
  138. .eq(CameraVersion::getType,param.getType());
  139. this.update(updateWrapper);
  140. }
  141. cameraVersion.setStatus(param.getStatus());
  142. this.updateById(param);
  143. return;
  144. }
  145. this.saveOrUpdate(param);
  146. }
  147. }