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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.ruoyi.require.service.impl;
 
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.common.exception.base.BaseException;
import com.ruoyi.common.utils.QueryWrappers;
import com.ruoyi.require.dto.SupplierManagementDto;
import com.ruoyi.require.mapper.SupplierManagementAttachmentMapper;
import com.ruoyi.require.mapper.SupplierManagementMapper;
import com.ruoyi.require.pojo.SupplierManagement;
import com.ruoyi.require.pojo.SupplierManagementAttachment;
import com.ruoyi.require.service.SupplierManagementService;
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 jakarta.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
 
/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2024-11-15 02:46:45
 */
@Service
public class SupplierManagementServiceImpl extends ServiceImpl<SupplierManagementMapper, SupplierManagement> implements SupplierManagementService {
 
    @Autowired
    private SupplierManagementMapper supplierManagementMapper;
 
    @Autowired
    private SupplierManagementAttachmentMapper supplierManagementAttachmentMapper;
 
    @Value("${file.path}")
    private String filePath;
 
    @Value("${wordUrl}")
    private String wordUrl;
 
    public SupplierManagementServiceImpl(SupplierManagementMapper supplierManagementMapper) {
        this.supplierManagementMapper = supplierManagementMapper;
    }
 
    @Override
    public List<SupplierManagement> selectSupplierManagement(SupplierManagement supplierManagement) {
        return this.list();
    }
 
    @Override
    public void exportSupplierManagement(Integer parentId, HttpServletResponse response) {
        List<SupplierManagement> data = supplierManagementMapper.selectSupplierManagementAll(parentId);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        try {
            String fileName = URLEncoder.encode("供应商管理", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            // 新建ExcelWriter
            ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build();
            WriteSheet writeSheet = EasyExcel.writerSheet(0, "供应商管理").head(SupplierManagementDto.class).build();
            excelWriter.write(data, writeSheet);
            // 关闭流
            excelWriter.finish();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new RuntimeException("导出失败");
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("导出失败");
        }
    }
 
    @Override
    public IPage<SupplierManagement> selectQualifiedSupplierManagement(Page page, SupplierManagement supplierManagement) {
        return baseMapper.selectQualifiedSupplierManagement(page, QueryWrappers.queryWrappers(supplierManagement));
    }
 
    @Override
    public List<SupplierManagement> selectQualifiedSupplierManagementById(Integer supplierManagementId) {
        return this.list(new QueryWrapper<SupplierManagement>().lambda().eq(SupplierManagement::getSupplierManagementId , supplierManagementId));
    }
 
    @Override
    public List<SupplierManagement> selectSupplierManagementByParentId(Integer parentId) {
        return supplierManagementMapper.selectSupplierManagement(parentId);
    }
 
    @Override
    public List<SupplierManagement> selectSupplierManagementAll() {
        return supplierManagementMapper.selectList(null);
    }
 
    @Override
    public List<SupplierManagementAttachment> selectAttachments(Integer supplierManagementId) {
        return supplierManagementAttachmentMapper.selectList(new QueryWrapper<SupplierManagementAttachment>().lambda()
                .eq(SupplierManagementAttachment::getSupplierManagementId, supplierManagementId)
                .orderByAsc(SupplierManagementAttachment::getSort, SupplierManagementAttachment::getId));
    }
 
    @Override
    public SupplierManagementAttachment uploadAttachment(Integer supplierManagementId, MultipartFile file) {
        if (supplierManagementMapper.selectById(supplierManagementId) == null) {
            throw new BaseException("供应商不存在");
        }
        if (file == null || file.isEmpty()) {
            throw new BaseException("附件不能为空");
        }
        // 系统全局上传上限为200MB,这里提前给出友好提示
        if (file.getSize() > 200L * 1024 * 1024) {
            throw new BaseException("附件不能超过200MB");
        }
        String originalName = file.getOriginalFilename();
        if (originalName == null || originalName.trim().isEmpty()) {
            originalName = "未命名文件";
        }
        // 个别浏览器可能带上路径前缀,只保留文件名本身
        originalName = originalName.replaceAll(".*[/\\\\]", "");
        try {
            // 图片放图片目录(/img 静态路径),其余任意格式放 word 目录(/word 静态路径)
            boolean image = originalName.matches("(?i).+\\.(jpg|jpeg|png|gif)$");
            File directory = new File(image ? filePath : wordUrl);
            if (!directory.exists()) {
                directory.mkdirs();
            }
            String fileUrl = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyMMddHHmmssSSS")) + "-" + originalName;
            file.transferTo(new File(directory, fileUrl));
            SupplierManagementAttachment attachment = new SupplierManagementAttachment();
            attachment.setSupplierManagementId(supplierManagementId);
            attachment.setFileName(originalName);
            attachment.setFileUrl(fileUrl);
            attachment.setSort(selectAttachments(supplierManagementId).size());
            supplierManagementAttachmentMapper.insert(attachment);
            return attachment;
        } catch (IOException e) {
            throw new BaseException("附件上传失败");
        }
    }
 
    @Override
    public boolean deleteAttachment(Long id) {
        return supplierManagementAttachmentMapper.deleteById(id) > 0;
    }
}