|
|
@@ -0,0 +1,144 @@
|
|
|
+package com.fdkankan.manage.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fdkankan.common.constant.ErrorCode;
|
|
|
+import com.fdkankan.common.exception.BusinessException;
|
|
|
+import com.fdkankan.common.response.PageInfo;
|
|
|
+import com.fdkankan.common.util.DateUtil;
|
|
|
+import com.fdkankan.common.util.FileMd5Util;
|
|
|
+import com.fdkankan.common.util.FileUtils;
|
|
|
+import com.fdkankan.common.validation.ValidationUtils;
|
|
|
+import com.fdkankan.fyun.oss.UploadToOssUtil;
|
|
|
+import com.fdkankan.manage.entity.CameraVersion;
|
|
|
+import com.fdkankan.manage.mapper.ICameraVersionMapper;
|
|
|
+import com.fdkankan.manage.service.ICameraVersionService;
|
|
|
+import com.fdkankan.manage.vo.request.CameraVersionParam;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Base64;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 相机版本表 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author
|
|
|
+ * @since 2022-06-15
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class CameraVersionServiceImpl extends ServiceImpl<ICameraVersionMapper, CameraVersion> implements ICameraVersionService {
|
|
|
+
|
|
|
+ public static String DIR_NAME = "camera_version/";
|
|
|
+ public static String YAMAXUN_S3_PATH = "https://eurs3.4dkankan.com/camera_version/";
|
|
|
+
|
|
|
+ @Value("${upload.type}")
|
|
|
+ private String ossType;
|
|
|
+ @Value("${prefix.ali}")
|
|
|
+ private String prefixAli;
|
|
|
+ @Resource
|
|
|
+ private UploadToOssUtil uploadToOssUtil;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void addAndUpload(MultipartFile file, String version, String description, String minVersion, String type) throws IOException {
|
|
|
+ String cameraType = StringUtils.isNotBlank(type) ? type : "1";
|
|
|
+ log.info("run upload");
|
|
|
+ if (!file.isEmpty()&& file.getSize() <= 0) {
|
|
|
+ throw new BusinessException(ErrorCode.FILE_NOT_EXIST);
|
|
|
+ }
|
|
|
+ // 文件名全名
|
|
|
+ String fullFileName = file.getOriginalFilename();
|
|
|
+ // 将文件转字节-> 字符串
|
|
|
+ String fileContent = Base64.getEncoder().encodeToString(file.getBytes());
|
|
|
+ // 获取类路径
|
|
|
+ String resourcePath = FileUtils.getResource();
|
|
|
+ log.info("resourcePath: {}", resourcePath);
|
|
|
+ // 创建目录
|
|
|
+ String dirPath = resourcePath + DIR_NAME;
|
|
|
+ FileUtils.createDir(dirPath);
|
|
|
+ // 拼接唯一文件名
|
|
|
+ String fileName = DateUtil.dateStr() + fullFileName;
|
|
|
+ // 文件保存路径
|
|
|
+ String filePath = dirPath + DateUtil.dateStr() + fullFileName;
|
|
|
+ // 写文件到本地
|
|
|
+ FileUtils.base64ToFileWriter(fileContent, filePath);
|
|
|
+ log.info("filePath: {}", filePath);
|
|
|
+ // 上传到阿里云sso
|
|
|
+ uploadToOssUtil.upload(filePath, DIR_NAME + fileName);
|
|
|
+ log.info("upload success");
|
|
|
+ String url = prefixAli + DIR_NAME + fileName;
|
|
|
+ if("aws".equals(ossType)){
|
|
|
+ url = YAMAXUN_S3_PATH + fileName;
|
|
|
+ }
|
|
|
+ log.info("upload url: {}" + url);
|
|
|
+ // 添加对象信息
|
|
|
+ CameraVersion versionEntity = new CameraVersion();
|
|
|
+ versionEntity.setName(fileName);
|
|
|
+ versionEntity.setFileUrl(url);
|
|
|
+ versionEntity.setVersion(version);
|
|
|
+ versionEntity.setDescription(description);
|
|
|
+ versionEntity.setType(Integer.parseInt(cameraType));
|
|
|
+ versionEntity.setMinVersion(minVersion);
|
|
|
+ versionEntity.setStatus("I");
|
|
|
+ versionEntity.setFileMd5(FileMd5Util.getFileMD5(new File(filePath)));
|
|
|
+ this.save(versionEntity);
|
|
|
+ // 删除本地文件
|
|
|
+ FileUtils.deleteFile(filePath);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PageInfo pageList(CameraVersionParam param) {
|
|
|
+ LambdaQueryWrapper<CameraVersion> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.eq(CameraVersion::getType,param.getType());
|
|
|
+ if (StringUtils.isNotBlank(param.getVersion())) {
|
|
|
+ queryWrapper.and(wrapper -> wrapper
|
|
|
+ .like(CameraVersion::getName,param.getVersion())
|
|
|
+ .or()
|
|
|
+ .like(CameraVersion::getVersion,param.getVersion()));
|
|
|
+ }
|
|
|
+ queryWrapper.orderByDesc(CameraVersion::getCreateTime);
|
|
|
+ Page<CameraVersion> page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), queryWrapper);
|
|
|
+ return PageInfo.PageInfo(page);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void updateByParam(CameraVersion param) {
|
|
|
+ if(param.getId() == null){
|
|
|
+ throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
|
|
|
+ }
|
|
|
+ CameraVersion cameraVersion = this.getById(param.getId());
|
|
|
+ if(cameraVersion == null){
|
|
|
+ throw new BusinessException(ErrorCode.NOT_RECORD);
|
|
|
+ }
|
|
|
+ if(StringUtils.isNotBlank(param.getStatus())){
|
|
|
+ if(StringUtils.isBlank(param.getStatus()) || param.getType() == null){
|
|
|
+ throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
|
|
|
+ }
|
|
|
+ if (!ValidationUtils.validateRecStatus(param.getStatus())) {
|
|
|
+ throw new BusinessException(ErrorCode.ERROR_MSG);
|
|
|
+ }
|
|
|
+ // 仅有有一台相机是活动状态
|
|
|
+ // 查找所以活动状态相机
|
|
|
+ LambdaUpdateWrapper<CameraVersion> updateWrapper = new LambdaUpdateWrapper<>();
|
|
|
+ updateWrapper.set(CameraVersion::getStatus,"I")
|
|
|
+ .eq(CameraVersion::getStatus,"A")
|
|
|
+ .eq(CameraVersion::getType,param.getType());
|
|
|
+ this.update(updateWrapper);
|
|
|
+
|
|
|
+ cameraVersion.setStatus(param.getStatus());
|
|
|
+ this.updateById(cameraVersion);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ this.saveOrUpdate(param);
|
|
|
+ }
|
|
|
+}
|