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.github.pagehelper.Page;
|
import com.github.pagehelper.PageHelper;
|
import com.github.pagehelper.PageInfo;
|
import com.whyc.dto.*;
|
import com.whyc.mapper.*;
|
import com.whyc.pojo.*;
|
import com.whyc.util.MathUtil;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import java.io.File;
|
import java.lang.reflect.Field;
|
import java.text.ParseException;
|
import java.util.*;
|
|
@Service
|
public class FileParamService {
|
@Autowired(required = false)
|
private FileParamMapper mapper;
|
|
@Autowired(required = false)
|
private BattGroupDataMapper dataMapper;
|
|
@Autowired(required = false)
|
private FileParamMapper paramMapper;
|
|
@Autowired(required = false)
|
private FileInfoMapper infoMapper;
|
|
@Autowired(required = false)
|
private TestParamMapper testParamMapper;
|
|
@Autowired(required = false)
|
private BattgroupInfoMapper groupInfoMapper;
|
|
//解析xml文件(传参一个文件)
|
public Response getXmlValue(String filePath) {
|
File file=new File(filePath);
|
if(file.exists()) {
|
if (file.isFile()) {
|
FileInfo fileInfo = XmlFileOpreate.readXml(filePath);
|
fileInfo.setFileUrl(filePath);
|
fileInfo.setFileId("0");
|
String fileName = filePath.substring(filePath.lastIndexOf("\\")+1);
|
fileInfo.setFileName(fileName);
|
fileInfo.setFileNameTmp(fileName+" tmp1.~xml");
|
return new Response().setII(1,fileInfo!=null,fileInfo,"返回解析数据");
|
}else{
|
return new Response().set(1,false,"文件路径不正确");
|
}
|
}else{
|
return new Response().set(1,false,"文件路径不正确");
|
}
|
}
|
//解析xml文件(传参一个文件夹)
|
public Response getXmlValueByPath(String filePath) {
|
File file=new File(filePath);
|
List list=new ArrayList();
|
if(file.exists()){
|
if(file.isFile()){
|
FileInfo fileInfo=XmlFileOpreate.readXml(filePath);
|
fileInfo.setFileUrl(filePath);
|
list.add(fileInfo);
|
}else{
|
//3.如果是文件夹:获取文件夹下所有的文件
|
List<File> allFile= FileOpreate.getAllFile(filePath);
|
if(allFile!=null&&allFile.size()>0){
|
for (File f:allFile){
|
if(!f.getName().contains(".xml")){
|
continue;
|
}
|
FileInfo fileInfo=XmlFileOpreate.readXml(f.getPath());
|
fileInfo.setFileUrl(f.getPath());
|
list.add(fileInfo);
|
}
|
}
|
}
|
return new Response().setII(1,list.size()>0,list,"返回解析数据");
|
}else{
|
return new Response().set(1,false,"文件路径不正确");
|
}
|
}
|
|
//通过修改属性窗口值来修改文件值
|
public Response updateXmlByFileParam(FileParam fileParam,String filePath) {
|
//修改文件对应的数据库数据
|
//paramMapper.updateParamByFileUrl(fileParam,filePath);
|
updateParamByFileUrl(fileParam,filePath);
|
|
Map<String,String> map=new HashMap<>();
|
Class paramClass=fileParam.getClass();
|
// 获取所有的属性数组
|
Field[] fields = paramClass.getDeclaredFields();
|
for (Field field:fields) {
|
field.setAccessible(true);
|
try {
|
String paramName=field.getName();
|
//获取属性值
|
Object obj=field.get(fileParam);
|
if(obj==null){
|
obj="null";
|
}
|
String paramValue=obj.toString();
|
if(field.getType().toString().equals("class java.util.Date")){
|
if(!paramValue.equals("null")){
|
paramValue= ActionUtil.sdfwithALL.format(ActionUtil.df.parse(paramValue));
|
}
|
}
|
String xmlName=FileParamToXml.getNameByType(paramName);
|
if(xmlName!=null&&!xmlName.isEmpty()){
|
map.put(xmlName,paramValue);
|
}
|
} catch (IllegalAccessException | ParseException e) {
|
e.printStackTrace();
|
}
|
}
|
boolean bl=false;
|
if(map.size()>0){
|
bl=XmlFileOpreate.writeXml(map,filePath);
|
}
|
return new Response().setII(1,bl,map,"修改文件");
|
}
|
//修改文件对应的数据库数据
|
public void updateParamByFileUrl(FileParam fileParam,String filePath){
|
//根据文件路径查出file_id
|
QueryWrapper wrapper=new QueryWrapper();
|
wrapper.eq("file_url",filePath);
|
List<FileInfo> list=infoMapper.selectList(wrapper);
|
if(list!=null&&list.size()>0){
|
for (FileInfo info:list){
|
//修改参数信息
|
paramMapper.updateParam(fileParam,info.getFileId());
|
}
|
}
|
|
}
|
|
//通过修改属性窗口值来修改文件值
|
public Response updateXmlByParamMap(Map<String,String> map,String filePath) {
|
boolean bl=false;
|
if(map.size()>0){
|
bl=XmlFileOpreate.writeXml(map,filePath);
|
}
|
return new Response().setII(1,bl,map,"修改文件");
|
}
|
//查询数据中存在的电池标称电压类型
|
public Response getMonVolStd() {
|
List<Integer> list=mapper.getMonVolStd();
|
return new Response().setII(1,list.size()>0,list,"返回所有的标称电压类型");
|
}
|
//按照筛选条件查询数据库信息(内阻评价:内阻越大越差),然后再根据内阻手动分页
|
public Response getDataByCondition(Date testTime1, Date testTime2, String battVol,int pageCurr,int pageSize,int flag) {
|
//PageHelper.startPage(pageCurr,pageSize);
|
List<FileParam> list=mapper.getDataByCondition(testTime1,testTime2,Float.valueOf(battVol));
|
List flagList=new ArrayList();//存放评价结果
|
QueryWrapper testParamWrapper=new QueryWrapper();
|
testParamWrapper.eq("enabled",1);
|
TestParam testParam=testParamMapper.selectOne(testParamWrapper);
|
float std=0f;
|
int resFlag=0;//整组内阻标识,1差,2良,3优
|
if(list!=null&&list.size()>0){
|
for (FileParam fparam:list) {
|
List<BattgroupInfo> binfInfoList=fparam.getBattInfoList();
|
std=fparam.getBattRes();
|
if(binfInfoList!=null&&binfInfoList.size()>0){
|
for (BattgroupInfo binfo:binfInfoList) {
|
List<BattgroupData> binfDataList=binfo.getBattDataList();
|
float maxStd=0f;
|
List listBv=new ArrayList();
|
List listBr=new ArrayList();
|
List listBs=new ArrayList();
|
List listCr=new ArrayList();
|
if(binfDataList!=null&&binfDataList.size()>0) {
|
for (BattgroupData bData : binfDataList) {
|
listBv.add(Float.valueOf(bData.getBv()));
|
listBr.add(Float.valueOf(bData.getBr()));
|
listBs.add(Float.valueOf(bData.getBs()));
|
listCr.add(Float.valueOf(bData.getCr()));
|
if( Float.parseFloat(bData.getBr())>=maxStd){
|
maxStd= Float.parseFloat(bData.getBr());
|
}
|
}
|
binfo.setSdBasebv(MathUtil.getStandardDeviation(listBv));
|
binfo.setSdBasebr(MathUtil.getStandardDeviation(listBr));
|
binfo.setSdBasebs(MathUtil.getStandardDeviation(listBs));
|
binfo.setSdBasecr(MathUtil.getStandardDeviation(listCr));
|
resFlag=MathUtil.getflag(maxStd,std,testParam);
|
binfo.setBrJudge(MathUtil.getJudge(resFlag));
|
}
|
break;
|
}
|
}
|
if(flag==0){//为0是全部
|
flagList.add(fparam);
|
}else{
|
if(flag==resFlag){
|
flagList.add(fparam);
|
}
|
}
|
}
|
}
|
//创建Page类
|
Page page = new Page(pageCurr, pageSize);
|
//为Page类中的total属性赋值
|
int total = flagList.size();
|
page.setTotal(total);
|
//计算当前需要显示的数据下标起始值
|
int startIndex = (pageCurr - 1) * pageSize;
|
int endIndex = Math.min(startIndex + pageSize,total);
|
//从链表中截取需要显示的子链表,并加入到Page
|
page.addAll(flagList.subList(startIndex,endIndex));
|
//以Page创建PageInfo
|
PageInfo pageInfo = new PageInfo<>(page);
|
return new Response().setII(1,flagList.size()>0,pageInfo,"筛选数据库信息");
|
}
|
|
public FileParam getByFileId(String fileId) {
|
QueryWrapper<FileParam> query = Wrappers.query();
|
query.eq("file_id",fileId).last(" limit 1");
|
return mapper.selectOne(query);
|
}
|
//删除基站下数据
|
public Response deleteDataById(int stationId, int fileId) {
|
//删除所有文件数据(指定机房id和文件id)
|
//dataMapper.deleteDataAndInfo(stationId,fileId);
|
deleteDataAndInfo(stationId,fileId);
|
//删除所有文件参数
|
paramMapper.deleteParamByFileId(fileId);
|
//移除机房下挂在的文件
|
infoMapper.deleteFileInStation(stationId,fileId);
|
return new Response().set(1,"删除成功!");
|
}
|
//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);
|
}
|
//根据fileId获取参数信息
|
public Response getParamByFileId(int fileId) {
|
QueryWrapper wrapper=new QueryWrapper();
|
wrapper.eq("file_id",fileId);
|
wrapper.last("limit 1");
|
FileParam fileParam=mapper.selectOne(wrapper);
|
return new Response().setII(1,fileParam!=null,fileParam,"返回数据");
|
}
|
|
//根据fileId通过修改属性窗口值来修改数据库属性
|
public Response updateFileParamByFileId(FileParam fileParam, String fileId) {
|
int flag=paramMapper.updateParam(fileParam,fileId);
|
return new Response().set(1,flag>0,"修改数据");
|
}
|
}
|