whycxzp
2025-06-12 a0a525d59f1cb3167a54af9309c8dab162e89005
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
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whyc.dto.Response;
import com.whyc.mapper.DeviceSpareMapper;
import com.whyc.pojo.web_site.DeviceSpare;
import com.whyc.util.CommonUtil;
import com.whyc.util.ThreadLocalUtil;
import org.apache.commons.lang.StringUtils;
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.Date;
import java.util.List;
 
@Service
public class DeviceSpareService {
 
    @Resource
    private DeviceSpareMapper mapper;
 
    public Response<PageInfo<DeviceSpare>> getPage(Integer pageNum, Integer pageSize, String type, String name) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<DeviceSpare> query = Wrappers.query();
        query.eq(StringUtils.isNotBlank(type), "type", type);
        query.eq(StringUtils.isNotBlank(name),  "name", name);
        List<DeviceSpare> deviceSpares = mapper.selectList(query);
        return new Response<PageInfo<DeviceSpare>>().set(1, new PageInfo<>(deviceSpares));
    }
 
    public Response<List<DeviceSpare>> getList(String name) {
        QueryWrapper<DeviceSpare> query = Wrappers.query();
        query.eq(StringUtils.isNotBlank(name), "name", name);
        return new Response<List<DeviceSpare>>().set(1, mapper.selectList(query));
    }
 
    public Response add(DeviceSpare spare, List<MultipartFile> file) throws IOException {
        //对file进行处理,保存到文件夹中
        //对存储路径进行定义
        Date now = new Date();
        String timeFormat = ThreadLocalUtil.format(ThreadLocalUtil.TIME_YYYY_MM_DD_HH_MM_SS_UNION, now);
        String dirMonth = ThreadLocalUtil.format(ThreadLocalUtil.TIME_YYYY_MM, now);
        String fileDirPath = CommonUtil.getRootFile() + "deviceSpare" + File.separator + dirMonth;
        File fileDir = new File(fileDirPath);
        //如果文件夹不存在则创建
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
        StringBuilder pictureUrlSb = new StringBuilder();
        if (file != null && file.size() > 0) {
            for (int i = 0; i < file.size(); i++) {
                MultipartFile multipartFile = file.get(i);
                String fileName = multipartFile.getOriginalFilename();
                //将fileName中可能存在的,去掉
                fileName = fileName.replace(",","");
                String filePath = fileDirPath + File.separator + timeFormat+"_"+fileName;
 
                multipartFile.transferTo(new File(filePath));
                String split = "pis_file"+File.separator+"deviceSpare";
                String picUrl = File.separator + filePath.substring(filePath.indexOf(split));
                if(i == file.size()-1) {
                    pictureUrlSb.append(picUrl);
                }else {
                    pictureUrlSb.append(picUrl).append(",");
                }
            }
        }
        spare.setPictureUrl(pictureUrlSb.toString());
        mapper.insert(spare);
        return new Response().setII(1,"增加完成");
    }
 
    public Response update(DeviceSpare spare) {
        mapper.updateById(spare);
        return new Response().setII(1,"修改完成");
    }
 
    public Response delete(Integer id) {
        mapper.deleteById(id);
        return new Response().setII(1,"删除完成");
    }
}