package com.whyc.service;
|
|
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
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((Wrapper<Station3D>) ActionUtil.objeNull);
|
}
|
|
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="";
|
FileOutputStream fot =null;
|
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";
|
fot = new FileOutputStream(fileName);
|
fot.write(dataBytes);
|
fot.flush();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}finally {
|
if(fot!=null){
|
try {
|
fot.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
|
/*=========数据库表更新===========*/
|
UpdateWrapper<Station3D> wrapper = Wrappers.update();
|
wrapper.set("pictureUrl",pictureUrl).eq("stationName",stationName);
|
mapper.update((Station3D) ActionUtil.objeNull,wrapper);
|
}
|
|
public void updateBatch(List<Station3D> station3DList) {
|
mapper.updateBatch(station3DList);
|
}
|
}
|