package com.whyc.service;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
import com.whyc.dto.FileOpreate;
|
import com.whyc.dto.Response;
|
import com.whyc.dto.XmlFileOpreate;
|
import com.whyc.mapper.*;
|
import com.whyc.pojo.*;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.io.File;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
@Service
|
public class StationInfoService {
|
@Autowired(required = false)
|
private StationInfoMapper mapper;
|
|
@Autowired(required = false)
|
private FileInfoMapper infoMapper;
|
|
@Autowired(required = false)
|
private BattgroupInfoMapper groupInfoMapper;
|
|
@Autowired(required = false)
|
private BattGroupDataMapper dataMapper;
|
|
@Autowired(required = false)
|
private FileParamMapper paramMapper;
|
//添加台站
|
public Response addStation(String stationName1, String stationName2, String stationName3) {
|
String stationName="";
|
boolean bl=true;
|
String msg="";
|
StationInfo s=new StationInfo();
|
if(stationName1!=null&&!stationName1.isEmpty()){
|
s.setStationName1(stationName1);
|
stationName=stationName+stationName1;
|
}
|
if(stationName2!=null&&!stationName2.isEmpty()){
|
s.setStationName2(stationName2);
|
stationName=stationName+"-"+stationName2;
|
}else{
|
s.setStationName2("-");
|
}
|
if(stationName3!=null&&!stationName3.isEmpty()){
|
s.setStationName3(stationName3);
|
stationName=stationName+"-"+stationName3;
|
}else{
|
s.setStationName3("-");
|
}
|
//查询stationName是否存在
|
QueryWrapper wrapper=new QueryWrapper();
|
wrapper.eq("station_name",stationName);
|
int count=mapper.selectCount(wrapper);
|
if(count>0){
|
bl=false;
|
msg="台站存在,添加失败";
|
}else{
|
s.setStationId("0");
|
s.setStationName(stationName);
|
int flag=mapper.insert(s);
|
bl=flag>0;
|
msg="台站添加成功";
|
}
|
return new Response().set(1,bl,msg);
|
}
|
//台站下添加文件
|
@Transactional
|
public Response addFileInStation(String stationName,String filePath,int fileId) {
|
File file=new File(filePath);
|
boolean flag=true;
|
//1.查询出最大的台站id(只有机房下添加了文件才会生成机房id)和最大文件id
|
int stationIdMax=mapper.selectMaxId();
|
int fileIdMax=infoMapper.selectMaxId();
|
List list=new ArrayList();
|
if(file.exists()){
|
if(file.isFile()){
|
if(!filePath.contains(".xml")){
|
return new Response().set(1,false,"台站下添加文件失败,文件不是指定的xml文件");
|
}
|
//2.解析文件并补全文件id
|
FileInfo fileInfo=XmlFileOpreate.readXml(filePath);
|
FileParam fParam=new FileParam();
|
if(fileId!=0){
|
QueryWrapper wrapper=new QueryWrapper();
|
wrapper.eq("file_id",fileId);
|
wrapper.last("limit 1");
|
fParam=paramMapper.selectOne(wrapper);
|
fileInfo.setFileParam(fParam);
|
}
|
fileInfo.setFileUrl(file.getPath());
|
fileInfo.setFileName(file.getName());
|
//4.将单文件的数据存入数据库
|
FileInfo returnFileInfo=insertFileInDataBase(stationName,stationIdMax,fileIdMax,fileInfo);
|
list.add(returnFileInfo);
|
}else{
|
//3.如果是文件夹:获取文件夹下所有的文件
|
List<File> allFile= FileOpreate.getAllFile(filePath);
|
//4.将文件路径下的所有文件解析并存入数据库
|
list=insertAllFileInDataBase(stationName,stationIdMax,fileIdMax,allFile,fileId);
|
}
|
}else{
|
flag=false;
|
}
|
return new Response().setII(1,flag,list,flag?"台站下添加文件成功":"台站下添加文件失败");
|
}
|
//4.将文件路径下的所有文件解析并存入数据库
|
private List insertAllFileInDataBase(String stationName, int stationIdMax, int fileIdMax, List<File> allFile,int fileId) {
|
List list=new ArrayList();
|
FileParam fParam=new FileParam();
|
if(allFile!=null&&allFile.size()>0){
|
for (int i=0;i< allFile.size();i++){
|
File file=allFile.get(i);
|
if(!file.getName().contains(".xml")){
|
continue;
|
}
|
//2.解析文件并补全文件id
|
FileInfo fileInfo=XmlFileOpreate.readXml(file.getPath());
|
fileInfo.setFileUrl(file.getPath());
|
fileInfo.setFileName(file.getName());
|
if(fileId==0){
|
if(i==0){
|
fParam=fileInfo.getFileParam();//取第一个文件的参数
|
}else {
|
fileInfo.setFileParam(fParam);//将所有文件的参数设置成第一个文件的参数
|
}
|
}else{
|
QueryWrapper wrapper=new QueryWrapper();
|
wrapper.eq("file_id",fileId);
|
wrapper.last("limit 1");
|
fParam=paramMapper.selectOne(wrapper);
|
fileInfo.setFileParam(fParam);
|
}
|
//4.将单文件的数据存入数据库
|
FileInfo returnFileInfo=insertFileInDataBase(stationName,stationIdMax,fileIdMax+i,fileInfo);
|
list.add(returnFileInfo);
|
}
|
}
|
return list;
|
}
|
|
//4.将单文件的数据存入数据库
|
private FileInfo insertFileInDataBase(String stationName,int stationIdMax,int fileIdMax, FileInfo fileInfo) {
|
int count=0;
|
Integer stationId=0;
|
//查询最大的电池组号
|
int battGroupId=groupInfoMapper.selectMaxId();
|
if(fileInfo!=null){
|
//查询站点的机房是否存在stationId
|
QueryWrapper wrapper=new QueryWrapper();
|
stationId=mapper.selectIdByName(stationName);
|
if(stationId!=0){
|
fileInfo.setStationId(String.valueOf(stationId));
|
}else{
|
fileInfo.setStationId(String.valueOf(stationIdMax+1));
|
stationId=stationIdMax+1;
|
}
|
fileInfo.setFileId(String.valueOf(fileIdMax+1));
|
//判断文件已经添加过则不再重复添加
|
int flag=judgeFile(stationId,fileInfo.getFileUrl());
|
if(flag>0){
|
return new FileInfo();
|
}else{
|
count=infoMapper.insert(fileInfo);
|
}
|
//将解析出来的文件信息插入数据库
|
FileParam fparam=fileInfo.getFileParam();
|
//去除groupinfo
|
List<BattgroupInfo> battgroupInfoList=fileInfo.getBattInfoList();
|
if(count>0&&fparam!=null){
|
fparam.setFileId(String.valueOf(fileIdMax+1));
|
paramMapper.insert(fparam);
|
//将电池组信息插入数据库
|
if(battgroupInfoList.size()>0){
|
for(int i=0;i<battgroupInfoList.size();i++){
|
BattgroupInfo battgroupInfo=battgroupInfoList.get(i);
|
battgroupInfo.setBattgroupId(String.valueOf(battGroupId+1+i));
|
battgroupInfo.setStationId(String.valueOf(stationId));
|
battgroupInfo.setFileId(String.valueOf(fileIdMax+1));
|
battgroupInfo.setBattgroupNum(fparam.getGroupNum());
|
int groupCount=groupInfoMapper.insert(battgroupInfo);
|
|
List<BattgroupData> battgroupDataList=battgroupInfo.getBattDataList();
|
//插入放电数据
|
if(groupCount>0&&battgroupDataList.size()>0){
|
for (int j=0;j<battgroupDataList.size();j++){
|
BattgroupData battgroupData=battgroupDataList.get(j);
|
battgroupData.setBattgroupId(String.valueOf(battGroupId+1+i));
|
}
|
}
|
dataMapper.insertBatchSomeColumn(battgroupDataList);
|
}
|
}
|
}
|
}
|
if(count>0){
|
//操作成功给台站赋予stationId
|
UpdateWrapper uwrapper=new UpdateWrapper();
|
uwrapper.set("station_id",stationId);
|
uwrapper.eq("station_name",stationName);
|
mapper.update(null,uwrapper);
|
}
|
return fileInfo;
|
}
|
//判断文件已经添加过则不再重复添加
|
public int judgeFile(int stationId,String fileUrl){
|
int flag=0;
|
QueryWrapper wrapper=new QueryWrapper();
|
wrapper.eq("station_id",stationId);
|
wrapper.eq("file_url",fileUrl);
|
FileInfo fileInfo=infoMapper.selectOne(wrapper);
|
if(fileInfo!=null){
|
flag=1;
|
}
|
return flag;
|
}
|
//编辑台站
|
public Response updateStation(String stationName1, String stationName2, String stationName3, String updateName) {
|
String stationName="";
|
int stationFlag=0;
|
if(stationName1!=null&&!stationName1.isEmpty()){
|
stationName=stationName+stationName1;
|
stationFlag=1;
|
}
|
if(stationName2!=null&&!stationName2.isEmpty()){
|
stationName=stationName+"-"+stationName2;
|
stationFlag=2;
|
}
|
if(stationName3!=null&&!stationName3.isEmpty()){
|
stationName=stationName+"-"+stationName3;
|
stationFlag=3;
|
}
|
int flag=0;
|
if(stationFlag==1){
|
//修改StationName1
|
flag=mapper.updateStationName1(stationName1,updateName);
|
}
|
if(stationFlag==2){
|
//修改StationName2
|
flag=mapper.updateStationName2(stationName1,stationName2,updateName);
|
}
|
if(stationFlag==3){
|
//修改StationName3
|
flag=mapper.updateStationName3(stationName1,stationName2,stationName3,updateName);
|
}
|
return new Response().set(1,flag>0,"修改操作");
|
}
|
//删除台站
|
@Transactional
|
public Response deleteStation(String stationName1, String stationName2, String stationName3) {
|
int stationFlag=0;
|
if(stationName1!=null&&!stationName1.isEmpty()){
|
stationFlag=1;
|
}
|
if(stationName2!=null&&!stationName2.isEmpty()){
|
stationFlag=2;
|
}
|
if(stationName3!=null&&!stationName3.isEmpty()){
|
stationFlag=3;
|
}
|
List<StationInfo> idStations=new ArrayList<>();
|
if(stationFlag==1){
|
//找出根节点是stationName1的所有机房id
|
idStations=mapper.selectIdByName1(stationName1);
|
}
|
if(stationFlag==2){
|
//找出根节点是stationName1,stationName2的所有机房id
|
idStations=mapper.selectIdByName2(stationName1,stationName2);
|
}
|
if(stationFlag==3){
|
//找出根节点是stationName1,stationName2,stationName3的所有机房id
|
idStations=mapper.selectIdByName3(stationName1,stationName2,stationName3);
|
}
|
if(idStations!=null&&idStations.size()>0){
|
for (StationInfo stationInfo:idStations) {
|
String stationName=stationInfo.getStationName();
|
Integer stationId=Integer.valueOf(stationInfo.getStationId());
|
//3.删除所有文件数据
|
//dataMapper.deleteDataAndInfoByStationId(stationId);
|
deleteDataAndInfoByStationId(stationId);
|
|
//4.删除所有文件参数
|
//paramMapper.deleteParamByStationId(stationId);
|
deleteParamByStationId(stationId);
|
//最终删除基站
|
UpdateWrapper wrapper=new UpdateWrapper();
|
wrapper.eq("station_id",stationId);
|
wrapper.eq("station_name",stationName);
|
mapper.delete(wrapper);
|
}
|
return new Response().set(1,true,"删除成功");
|
}else{
|
return new Response().set(1,false,"机房不存在");
|
}
|
}
|
//移除台站下文件(单个文件)
|
@Transactional
|
public Response delFileFromStation(String stationName, String filePath) {
|
//1.先查出机房的id
|
Integer stationId=mapper.selectIdByName(stationName);
|
if(stationId==null){
|
return new Response().set(1,false,"机房不存在");
|
}else if(stationId==0){
|
return new Response().set(1,false,"机房下不存在文件");
|
}else {
|
//2.查出文件对应的文件id
|
Integer fileId=infoMapper.selectFileId(stationId,filePath);
|
if(fileId==null||fileId==0){
|
return new Response().set(1,false,"文件不存在");
|
}else{
|
//3.删除fileid对应的数据(stationId,fileId)
|
//dataMapper.deleteDataAndInfo(stationId,fileId);
|
//3根据(stationId,fileId)删除groupinfo和groupdata数据
|
deleteDataAndInfo(stationId,fileId);
|
|
//4.删除fileid对应的参数
|
paramMapper.deleteParamByFileId(fileId);
|
|
//5.删除机房下文件信息(stationId,fileId)
|
infoMapper.deleteFileInStation(stationId,fileId);
|
return new Response().set(1,true,"删除成功");
|
}
|
}
|
}
|
//3根据(stationId,fileId)删除groupinfo和groupdata数据
|
public void deleteDataAndInfo(int stationId,int fileId){
|
//先查询出对应的电池组id然后删除
|
QueryWrapper wrapper=new QueryWrapper();
|
wrapper.eq("station_id",stationId);
|
wrapper.eq("file_id",fileId);
|
List<BattgroupInfo> list=groupInfoMapper.selectList(wrapper);
|
if(list!=null&&list.size()>0){
|
for (BattgroupInfo ginfo:list) {
|
UpdateWrapper dataWrapper=new UpdateWrapper();
|
dataWrapper.eq("battGroup_id",ginfo.getBattgroupId());
|
dataMapper.delete(dataWrapper);
|
}
|
}
|
//再删除groupinfo
|
UpdateWrapper infoWrapper=new UpdateWrapper();
|
infoWrapper.eq("station_id",stationId);
|
infoWrapper.eq("file_id",fileId);
|
groupInfoMapper.delete(infoWrapper);
|
}
|
|
//删除所有文件数据
|
public void deleteDataAndInfoByStationId(int stationId){
|
//先查询出对应的电池组id然后删除
|
QueryWrapper wrapper=new QueryWrapper();
|
wrapper.eq("station_id",stationId);
|
List<BattgroupInfo> list=groupInfoMapper.selectList(wrapper);
|
if(list!=null&&list.size()>0){
|
for (BattgroupInfo ginfo:list) {
|
UpdateWrapper dataWrapper=new UpdateWrapper();
|
dataWrapper.eq("battGroup_id",ginfo.getBattgroupId());
|
dataMapper.delete(dataWrapper);
|
//再删除groupinfo
|
UpdateWrapper infoWrapper=new UpdateWrapper();
|
infoWrapper.eq("file_id",ginfo.getFileId());
|
groupInfoMapper.delete(infoWrapper);
|
}
|
}
|
}
|
//3删除所有文件参数
|
public void deleteParamByStationId(int stationId){
|
//先查询出对应的电池组id然后删除
|
QueryWrapper wrapper=new QueryWrapper();
|
wrapper.eq("station_id",stationId);
|
List<FileInfo> list=infoMapper.selectList(wrapper);
|
if(list!=null&&list.size()>0){
|
for (FileInfo info:list) {
|
UpdateWrapper paramWrapper=new UpdateWrapper();
|
paramWrapper.eq("file_id",info.getFileId());
|
paramMapper.delete(paramWrapper);
|
|
//再删除fileinfo
|
UpdateWrapper infoWrapper=new UpdateWrapper();
|
infoWrapper.eq("file_id",info.getFileId());
|
infoMapper.delete(infoWrapper);
|
}
|
}
|
}
|
|
public StationInfo getByFileId(String fileId) {
|
return mapper.getByFileId(fileId);
|
}
|
}
|