CameraVersionAppServiceImpl.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package com.fdkankan.fusion.service.impl;
  2. import cn.hutool.core.io.FileUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.fdkankan.fusion.common.FilePath;
  7. import com.fdkankan.fusion.common.PageInfo;
  8. import com.fdkankan.fusion.common.ResultCode;
  9. import com.fdkankan.fusion.common.util.DateUtils;
  10. import com.fdkankan.fusion.common.util.FileMd5Util;
  11. import com.fdkankan.fusion.common.util.ShellUtil;
  12. import com.fdkankan.fusion.entity.CameraVersionApp;
  13. import com.fdkankan.fusion.entity.TmUser;
  14. import com.fdkankan.fusion.entity.User;
  15. import com.fdkankan.fusion.exception.BusinessException;
  16. import com.fdkankan.fusion.mapper.ICameraVersionAppMapper;
  17. import com.fdkankan.fusion.request.CameraVersionParam;
  18. import com.fdkankan.fusion.response.CameraVersionVo;
  19. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  20. import com.fdkankan.fusion.service.ICameraVersionAppService;
  21. import com.fdkankan.fusion.service.ITmUserService;
  22. import com.fdkankan.fusion.service.IUserService;
  23. import lombok.extern.slf4j.Slf4j;
  24. import org.apache.commons.lang3.StringUtils;
  25. import org.springframework.beans.BeanUtils;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.beans.factory.annotation.Value;
  28. import org.springframework.stereotype.Service;
  29. import org.springframework.web.multipart.MultipartFile;
  30. import java.io.File;
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. /**
  34. * <p>
  35. * 相机版本表 服务实现类
  36. * </p>
  37. *
  38. * @author
  39. * @since 2024-07-24
  40. */
  41. @Service
  42. @Slf4j
  43. public class CameraVersionAppServiceImpl extends ServiceImpl<ICameraVersionAppMapper, CameraVersionApp> implements ICameraVersionAppService {
  44. public static String DIR_NAME = "camera_version_app/";
  45. @Value("${upload.query-path}")
  46. private String ossUrlPrefix;
  47. @Autowired
  48. IUserService userService;
  49. @Autowired
  50. ITmUserService tmUserService;
  51. @Override
  52. public void addAndUpload(MultipartFile file, String version, String description, String minVersion, Integer type,String userName) {
  53. if(StringUtils.isBlank(version)){
  54. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  55. }
  56. log.info("run upload");
  57. if (!file.isEmpty()&& file.getSize() <= 0) {
  58. throw new BusinessException(ResultCode.UPLOAD_ERROR);
  59. }
  60. try {
  61. // 文件名全名
  62. String fullFileName = file.getOriginalFilename();
  63. String resourcePath = FilePath.LOCAL_BASE_PATH ;
  64. log.info("resourcePath: {}", resourcePath);
  65. // 创建目录
  66. String dirPath = resourcePath + DIR_NAME;
  67. FileUtil.mkdir(dirPath);
  68. // 拼接唯一文件名
  69. String fileName = DateUtils.dateStr() + fullFileName;
  70. // 文件保存路径
  71. String filePath = dirPath + DateUtils.dateStr() + fullFileName;
  72. // 写文件到本地
  73. File file1 = new File(filePath);
  74. file.transferTo(file1);
  75. // 添加对象信息
  76. // switch (type){
  77. // case 1: type = 1;break;
  78. // case 2: type = 9;break;
  79. // case 3: type = 10;break;
  80. // default: throw new BusinessException(ResultCode.CAMERA_TYPE_ERROR);
  81. // }
  82. List<CameraVersionApp> CameraVersionApps = this.getByVersion(version,type);
  83. if(CameraVersionApps != null && CameraVersionApps.size() >0){
  84. throw new BusinessException(ResultCode.CAMERA_VERSION_EXIT);
  85. }
  86. log.info("filePath: {}", filePath);
  87. // 上传到阿里云sso
  88. ShellUtil.yunUpload(filePath,DIR_NAME + fileName);
  89. log.info("upload success");
  90. String url = ossUrlPrefix + DIR_NAME + fileName;
  91. log.info("upload url: {}" + url);
  92. User user = userService.getByUserName(userName);
  93. CameraVersionApp versionEntity = new CameraVersionApp();
  94. versionEntity.setName(fileName);
  95. versionEntity.setFileUrl(url);
  96. versionEntity.setVersion(version);
  97. versionEntity.setDescription(description);
  98. versionEntity.setType(type);
  99. versionEntity.setMinVersion(minVersion);
  100. versionEntity.setStatus("I");
  101. versionEntity.setFileMd5(FileMd5Util.getFileMD5(new File(filePath)));
  102. versionEntity.setSysUserId(user.getId());
  103. this.save(versionEntity);
  104. // 删除本地文件
  105. FileUtil.del(filePath);
  106. }catch (Exception e){
  107. log.info("upload-error:{}",e);
  108. }finally {
  109. }
  110. }
  111. private List<CameraVersionApp> getByVersion(String version,Integer type) {
  112. LambdaQueryWrapper<CameraVersionApp> wrapper = new LambdaQueryWrapper<>();
  113. wrapper.eq(CameraVersionApp::getVersion,version);
  114. wrapper.eq(CameraVersionApp::getType,type);
  115. return this.list(wrapper);
  116. }
  117. @Override
  118. public Page<CameraVersionApp> pageList(CameraVersionParam param) {
  119. Integer type = param.getType();
  120. // switch (type){
  121. // case 1: type = 1;break; //看看
  122. // case 2: type = 9;break; //看见
  123. // case 3: type = 10;break; //深时
  124. // default: throw new BusinessException(ResultCode.CAMERA_TYPE_ERROR);
  125. // }
  126. LambdaQueryWrapper<CameraVersionApp> queryWrapper = new LambdaQueryWrapper<>();
  127. queryWrapper.eq(CameraVersionApp::getType,type);
  128. if (StringUtils.isNotBlank(param.getVersion())) {
  129. queryWrapper.like(CameraVersionApp::getVersion,param.getVersion());
  130. }
  131. queryWrapper.orderByDesc(CameraVersionApp::getCreateTime);
  132. Page<CameraVersionApp> page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), queryWrapper);
  133. return page;
  134. }
  135. @Override
  136. public void updateByParam(CameraVersionApp param) {
  137. if(param.getId() == null){
  138. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  139. }
  140. CameraVersionApp cameraVersionApp = this.getById(param.getId());
  141. if(cameraVersionApp == null){
  142. throw new BusinessException(ResultCode.CAMERA_VERSION_NOTEXIT);
  143. }
  144. if(StringUtils.isNotBlank(param.getStatus()) && !param.getStatus().equals(cameraVersionApp.getStatus())){
  145. if(StringUtils.isBlank(param.getStatus()) || param.getType() == null){
  146. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  147. }
  148. if (!validateRecStatus(param.getStatus())) {
  149. throw new BusinessException(ResultCode.CAMERA_VERSION_STATUS_ERROR);
  150. }
  151. // 仅有有一台相机是活动状态
  152. // 查找所以活动状态相机
  153. if(param.getStatus().equals("A")){
  154. LambdaUpdateWrapper<CameraVersionApp> updateWrapper = new LambdaUpdateWrapper<>();
  155. updateWrapper.set(CameraVersionApp::getStatus,"I")
  156. .eq(CameraVersionApp::getStatus,"A")
  157. .eq(CameraVersionApp::getType,param.getType());
  158. this.update(updateWrapper);
  159. }
  160. this.updateById(param);
  161. return;
  162. }
  163. this.saveOrUpdate(param);
  164. }
  165. public static boolean validateRecStatus(String str) {
  166. if (StringUtils.isEmpty(str)) {
  167. return Boolean.FALSE;
  168. } else {
  169. return !"A".equals(str) && !"I".equals(str) ? Boolean.FALSE : Boolean.TRUE;
  170. }
  171. }
  172. }