whycxzp
2023-02-25 ade751704a36681e5eb0e55c3db8458896a0135c
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
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.whyc.mapper.Station3DMapper;
import com.whyc.pojo.Station3D;
import com.whyc.util.ActionUtil;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Decoder;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
@Service
public class Station3DService {
 
    @Resource
    private Station3DMapper mapper;
 
    public void add(Station3D station3D) {
        mapper.insert(station3D);
    }
 
    public void addBatch(List<Station3D> station3DList) {
        mapper.insertBatchSomeColumn(station3DList);
    }
 
    public void delete(int stationId) {
        mapper.deleteById(stationId);
    }
 
    public void update(Station3D station3D) {
        mapper.updateById(station3D);
    }
 
    public Station3D getById(int id) {
        return mapper.selectById(id);
    }
 
    public List<Station3D> getByDeviceId(int deviceId) {
        QueryWrapper<Station3D> wrapper = Wrappers.query();
        wrapper.eq("deviceId",deviceId);
 
        return mapper.selectList(wrapper);
    }
 
    public List<Station3D> getAll() {
        return mapper.selectList(null);
    }
 
    public List<Station3D> getAllStations() {
        QueryWrapper<Station3D> wrapper = Wrappers.query();
        wrapper.select("distinct deviceId,stationName,pictureName,pictureUrl");
        return mapper.selectList(wrapper);
    }
 
    public void uploadPicture(String stationName, String fileData, String pictureName, HttpServletRequest request) {
 
        fileData = fileData.replaceAll(" ", "+");
        fileData = fileData.split(",")[1];
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] dataBytes = new byte[0];
        String pictureUrl="";
        try {
            dataBytes = decoder.decodeBuffer(fileData);
            /*=========图片保存===========*/
            /*//获取到tomcat webapp绝对路径
            //String realPath = ActionUtil.getApplication().getRealPath("/");
            String realPath = request.getServletContext().getRealPath("/");
            String[] split = realPath.split("\\\\");
            String projectName = split[split.length - 1];
 
            String webAppPath = realPath.substring(0, realPath.lastIndexOf(projectName));*/
 
            ApplicationHome home = new ApplicationHome(getClass());
            File jarFile = home.getDir();
 
            //存储3D站点图片的文件夹
            String fileDirName = jarFile.getParentFile().toString()+File.separator+"fg_photo_3DStation"+File.separator;
 
            //图片的Url
            /*StringBuffer requestUrl = ActionUtil.getRequest().getRequestURL();
            String httpName = requestUrl.substring(0, requestUrl.indexOf(projectName));
            pictureUrl = httpName+projectName + "_photo_3DStation/"+pictureName+".jpg";*/
 
            File file = new File(fileDirName);
            //不存在则创建该文件夹
            if (!file.exists()) {
                file.mkdirs();
            }
 
            //文件全路径
            String fileName = fileDirName + File.separator + pictureName + ".jpg";
            FileOutputStream fot = new FileOutputStream(fileName);
            fot.write(dataBytes);
            fot.flush();
            fot.close();
 
        } catch (IOException e) {
            e.printStackTrace();
        }
 
        /*=========数据库表更新===========*/
        UpdateWrapper<Station3D> wrapper = Wrappers.update();
        wrapper.set("pictureUrl",pictureUrl).eq("stationName",stationName);
        mapper.update(null,wrapper);
    }
 
    public void updateBatch(List<Station3D> station3DList) {
        mapper.updateBatch(station3DList);
    }
}