package com.ruoyi.basic.service.impl;
|
|
import cn.hutool.core.lang.UUID;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.ruoyi.basic.dto.WorkShopDto;
|
import com.ruoyi.basic.mapper.WorkShopFileMapper;
|
import com.ruoyi.basic.mapper.WorkShopMapper;
|
import com.ruoyi.basic.pojo.WorkShop;
|
import com.ruoyi.basic.pojo.WorkShopFile;
|
import com.ruoyi.basic.service.WorkShopService;
|
import com.ruoyi.common.utils.QueryWrappers;
|
import com.ruoyi.common.utils.SecurityUtils;
|
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.time.LocalDateTime;
|
import java.time.format.DateTimeFormatter;
|
|
/**
|
* @description 针对表【work_shop(车间名称)】的数据库操作Service实现
|
* @createDate 2024-03-12 16:17:55
|
*/
|
@Service
|
public class WorkShopServiceImpl extends ServiceImpl<WorkShopMapper, WorkShop> implements WorkShopService {
|
|
@Resource
|
private WorkShopMapper workShopMapper;
|
@Resource
|
private WorkShopFileMapper workShopFileMapper;
|
|
@Value("${wordUrl}")
|
private String wordUrl;
|
|
@Value("${file.path}")
|
private String imgUrl;
|
|
|
@Override
|
public IPage<WorkShopDto> selectWorkShop(Page page, WorkShopDto workShopDto) {
|
return workShopMapper.selectPage(page, QueryWrappers.queryWrappers(workShopDto));
|
}
|
|
@Override
|
public int addWorkShop(WorkShopDto workShopDto) {
|
if (workShopDto.getId() == null) {
|
return workShopMapper.insert(workShopDto);
|
} else {
|
return workShopMapper.updateById(workShopDto);
|
}
|
}
|
|
@Override
|
public int delWorkShop(Integer id) {
|
return workShopMapper.deleteById(id);
|
}
|
|
@Override
|
public int uploadFile(Integer id, MultipartFile file) {
|
String username = SecurityUtils.getUsername();
|
String urlString;
|
String pathName;
|
String path;
|
String filename = file.getOriginalFilename();
|
String contentType = file.getContentType();
|
WorkShopFile workShopFile = new WorkShopFile();
|
workShopFile.setWorkShopId(id);
|
workShopFile.setFileName(filename);
|
if (contentType != null && contentType.startsWith("image/")) {
|
// 是图片
|
path = imgUrl;
|
workShopFile.setType(1);
|
} else {
|
// 是文件
|
path = wordUrl;
|
workShopFile.setType(2);
|
}
|
try {
|
// 1. 解析绝对路径(优先使用配置的绝对路径,否则基于项目根目录)
|
String basePath = imgUrl.startsWith(File.separator) ? imgUrl : System.getProperty("user.dir") + File.separator + imgUrl;
|
File realpath = new File(basePath);
|
|
// 2. 创建目录(记录日志,检查是否成功)
|
if (!realpath.exists()) {
|
if (!realpath.mkdirs()) {
|
System.err.println("目录创建失败:{}");
|
return 0;
|
}
|
}
|
|
// 3. 生成唯一文件名
|
String uuid = UUID.randomUUID().toString().replace("-", "");
|
String suffix = filename.substring(filename.lastIndexOf("."));
|
pathName = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + "_" + uuid + suffix;
|
File targetFile = new File(realpath, pathName);
|
|
// 4. 保存文件
|
file.transferTo(targetFile);
|
workShopFile.setFileUrl(pathName);
|
workShopFile.setName(username);
|
return workShopFileMapper.insert(workShopFile);
|
} catch (Exception e) {
|
e.printStackTrace();
|
System.err.println("附件上传错误");
|
return 0;
|
}
|
}
|
|
}
|