1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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;
        }
    }
 
}