package com.fdkankan.fusion.service.impl; import cn.hutool.core.io.FileUtil; 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.fdkankan.fusion.common.FilePath; import com.fdkankan.fusion.common.PageInfo; import com.fdkankan.fusion.common.ResultCode; import com.fdkankan.fusion.common.util.DateUtils; import com.fdkankan.fusion.common.util.FileMd5Util; import com.fdkankan.fusion.common.util.ShellUtil; import com.fdkankan.fusion.entity.CameraVersionApp; import com.fdkankan.fusion.entity.TmUser; import com.fdkankan.fusion.entity.User; import com.fdkankan.fusion.exception.BusinessException; import com.fdkankan.fusion.mapper.ICameraVersionAppMapper; import com.fdkankan.fusion.request.CameraVersionParam; import com.fdkankan.fusion.response.CameraVersionVo; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.fdkankan.fusion.service.ICameraVersionAppService; import com.fdkankan.fusion.service.ITmUserService; import com.fdkankan.fusion.service.IUserService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.util.ArrayList; import java.util.List; /** *

* 相机版本表 服务实现类 *

* * @author * @since 2024-07-24 */ @Service @Slf4j public class CameraVersionAppServiceImpl extends ServiceImpl implements ICameraVersionAppService { public static String DIR_NAME = "camera_version_app/"; @Value("${upload.query-path}") private String ossUrlPrefix; @Autowired IUserService userService; @Autowired ITmUserService tmUserService; @Override public void addAndUpload(MultipartFile file, String version, String description, String minVersion, Integer type,String userName) { if(StringUtils.isBlank(version)){ throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS); } log.info("run upload"); if (!file.isEmpty()&& file.getSize() <= 0) { throw new BusinessException(ResultCode.UPLOAD_ERROR); } try { // 文件名全名 String fullFileName = file.getOriginalFilename(); String resourcePath = FilePath.LOCAL_BASE_PATH ; log.info("resourcePath: {}", resourcePath); // 创建目录 String dirPath = resourcePath + DIR_NAME; FileUtil.mkdir(dirPath); // 拼接唯一文件名 String fileName = DateUtils.dateStr() + fullFileName; // 文件保存路径 String filePath = dirPath + DateUtils.dateStr() + fullFileName; // 写文件到本地 File file1 = new File(filePath); file.transferTo(file1); // 添加对象信息 // switch (type){ // case 1: type = 1;break; // case 2: type = 9;break; // case 3: type = 10;break; // default: throw new BusinessException(ResultCode.CAMERA_TYPE_ERROR); // } List CameraVersionApps = this.getByVersion(version,type); if(CameraVersionApps != null && CameraVersionApps.size() >0){ throw new BusinessException(ResultCode.CAMERA_VERSION_EXIT); } log.info("filePath: {}", filePath); // 上传到阿里云sso ShellUtil.yunUpload(filePath,DIR_NAME + fileName); log.info("upload success"); String url = ossUrlPrefix + DIR_NAME + fileName; log.info("upload url: {}" + url); User user = userService.getByUserName(userName); CameraVersionApp versionEntity = new CameraVersionApp(); versionEntity.setName(fileName); versionEntity.setFileUrl(url); versionEntity.setVersion(version); versionEntity.setDescription(description); versionEntity.setType(type); versionEntity.setMinVersion(minVersion); versionEntity.setStatus("I"); versionEntity.setFileMd5(FileMd5Util.getFileMD5(new File(filePath))); versionEntity.setSysUserId(user.getId()); this.save(versionEntity); // 删除本地文件 FileUtil.del(filePath); }catch (Exception e){ log.info("upload-error:{}",e); }finally { } } private List getByVersion(String version,Integer type) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(CameraVersionApp::getVersion,version); wrapper.eq(CameraVersionApp::getType,type); return this.list(wrapper); } @Override public Page pageList(CameraVersionParam param) { Integer type = param.getType(); // switch (type){ // case 1: type = 1;break; //看看 // case 2: type = 9;break; //看见 // case 3: type = 10;break; //深时 // default: throw new BusinessException(ResultCode.CAMERA_TYPE_ERROR); // } LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(CameraVersionApp::getType,type); if (StringUtils.isNotBlank(param.getVersion())) { queryWrapper.like(CameraVersionApp::getVersion,param.getVersion()); } queryWrapper.orderByDesc(CameraVersionApp::getCreateTime); Page page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), queryWrapper); return page; } @Override public void updateByParam(CameraVersionApp param) { if(param.getId() == null){ throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS); } CameraVersionApp cameraVersionApp = this.getById(param.getId()); if(cameraVersionApp == null){ throw new BusinessException(ResultCode.CAMERA_VERSION_NOTEXIT); } if(StringUtils.isNotBlank(param.getStatus()) && !param.getStatus().equals(cameraVersionApp.getStatus())){ if(StringUtils.isBlank(param.getStatus()) || param.getType() == null){ throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS); } if (!validateRecStatus(param.getStatus())) { throw new BusinessException(ResultCode.CAMERA_VERSION_STATUS_ERROR); } // 仅有有一台相机是活动状态 // 查找所以活动状态相机 if(param.getStatus().equals("A")){ LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper<>(); updateWrapper.set(CameraVersionApp::getStatus,"I") .eq(CameraVersionApp::getStatus,"A") .eq(CameraVersionApp::getType,param.getType()); this.update(updateWrapper); } this.updateById(param); return; } this.saveOrUpdate(param); } public static boolean validateRecStatus(String str) { if (StringUtils.isEmpty(str)) { return Boolean.FALSE; } else { return !"A".equals(str) && !"I".equals(str) ? Boolean.FALSE : Boolean.TRUE; } } }