package com.fdkankan.manage.service.impl;
import cn.dev33.satoken.stp.StpUtil;
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.fyun.face.FYunFileServiceInterface;
import com.fdkankan.manage.common.FilePath;
import com.fdkankan.manage.common.ResultCode;
import com.fdkankan.manage.exception.BusinessException;
import com.fdkankan.manage.common.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.manage.common.ResultCode;
import com.fdkankan.manage.entity.SpaceSdk;
import com.fdkankan.manage.entity.SysUser;
import com.fdkankan.manage.service.ISysUserService;
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 com.fdkankan.manage.vo.response.CameraVersionVo;
import com.fdkankan.manage.vo.response.SpaceSdkVo;
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 javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
/**
*
* 相机版本表 服务实现类
*
*
* @author
* @since 2022-06-15
*/
@Service
@Slf4j
public class CameraVersionServiceImpl extends ServiceImpl implements ICameraVersionService {
public static String DIR_NAME = "camera_version/";
@Value("${fyun.host:https://4dkk.4dage.com/}")
private String ossUrlPrefix;
@Autowired
private FYunFileServiceInterface fYunFileServiceInterface;
@Autowired
ISysUserService sysUserService;
@Override
public void addAndUpload(MultipartFile file, String version, String description, String minVersion, Integer type) throws IOException {
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_FILE_ERROR);
}
// 文件名全名
String fullFileName = file.getOriginalFilename();
String resourcePath = FilePath.cameraVersionLocalPath;
log.info("resourcePath: {}", resourcePath);
// 创建目录
String dirPath = resourcePath + DIR_NAME;
FileUtils.createDir(dirPath);
// 拼接唯一文件名
String fileName = DateUtil.dateStr() + fullFileName;
// 文件保存路径
String filePath = dirPath + DateUtil.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 cameraVersions = this.getByVersion(version,type);
if(cameraVersions != null && cameraVersions.size() >0){
throw new BusinessException(ResultCode.VISION_EXIST.code(),ResultCode.VISION_EXIST.message());
}
log.info("filePath: {}", filePath);
// 上传到阿里云sso
fYunFileServiceInterface.uploadFile(filePath, DIR_NAME + fileName);
log.info("upload success");
String url = ossUrlPrefix + DIR_NAME + fileName;
log.info("upload url: {}" + url);
CameraVersion versionEntity = new CameraVersion();
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(Long.valueOf(StpUtil.getLoginId().toString()));
this.save(versionEntity);
// 删除本地文件
FileUtils.deleteFile(filePath);
}
private List getByVersion(String version,Integer type) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(CameraVersion::getVersion,version);
wrapper.eq(CameraVersion::getType,type);
return this.list(wrapper);
}
@Override
public PageInfo 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(CameraVersion::getType,type);
if (StringUtils.isNotBlank(param.getVersion())) {
queryWrapper.like(CameraVersion::getVersion,param.getVersion());
}
queryWrapper.orderByDesc(CameraVersion::getCreateTime);
Page page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), queryWrapper);
List voList = new ArrayList<>();
for (CameraVersion record : page.getRecords()) {
CameraVersionVo vo = new CameraVersionVo();
BeanUtils.copyProperties(record,vo);
if(record.getSysUserId() !=null){
SysUser user = sysUserService.getById(record.getSysUserId());
if(user != null){
vo.setCreateName(user.getNickName());
}
}
voList.add(vo);
}
Page voPage = new Page<>(param.getPageNum(),param.getPageSize());
voPage.setRecords(voList);
voPage.setTotal(page.getTotal());
return PageInfo.PageInfo(voPage);
}
@Override
public void updateByParam(CameraVersion param) {
if(param.getId() == null){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
CameraVersion cameraVersion = this.getById(param.getId());
if(cameraVersion == null){
throw new BusinessException(ResultCode.CAMERA_VERSION_NOT_EXIST);
}
if(StringUtils.isNotBlank(param.getStatus()) && !param.getStatus().equals(cameraVersion.getStatus())){
if(StringUtils.isBlank(param.getStatus()) || param.getType() == null){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
if (!ValidationUtils.validateRecStatus(param.getStatus())) {
throw new BusinessException(ResultCode.CAMERA_VERSION_STATUS_ERROR);
}
// 仅有有一台相机是活动状态
// 查找所以活动状态相机
if(param.getStatus().equals("A")){
LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.set(CameraVersion::getStatus,"I")
.eq(CameraVersion::getStatus,"A")
.eq(CameraVersion::getType,param.getType());
this.update(updateWrapper);
}
this.updateById(param);
return;
}
this.saveOrUpdate(param);
}
}