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.PageHelper;
|
import com.github.pagehelper.PageInfo;
|
import com.whyc.dto.BattInfAndEnduranceDTO;
|
import com.whyc.dto.Response;
|
import com.whyc.mapper.BattInfMapper;
|
import com.whyc.pojo.Battinf;
|
import com.whyc.util.ActionUtil;
|
import org.springframework.boot.system.ApplicationHome;
|
import org.springframework.core.env.Environment;
|
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.io.InputStream;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.HashMap;
|
import java.util.List;
|
|
import static com.whyc.util.ActionUtil.createFilefolderIFNotExist;
|
|
@Service
|
public class BattInfService {
|
@Resource
|
private BattInfMapper battInfMapper;
|
@Resource
|
private Environment environment;
|
|
public int searchMaxBattgroupId_zj() {
|
Integer id = battInfMapper.getMaxBattGroupId_zj();
|
if (id == null) {
|
id = 0;
|
}
|
return id + 1;
|
}
|
|
public String searchMaxId_zj() {
|
Integer id = battInfMapper.getMaxStationId_zj();
|
id = id == null ? id = 0 : id;
|
return (id + 1) + "";
|
}
|
|
public int searchMaxDevId(Integer fbsDeviceId){
|
Integer devId = battInfMapper.getMaxDevId(fbsDeviceId);
|
if(devId == null){
|
int length = String.valueOf(fbsDeviceId).length();
|
switch (length){
|
case 3:
|
devId = fbsDeviceId*1000000;break;
|
case 4:
|
devId = fbsDeviceId*100000;break;
|
case 5:
|
devId = fbsDeviceId*10000;break;
|
case 6:
|
devId = fbsDeviceId*1000;break;
|
/*case 9100:
|
case 9110:
|
case 9120:
|
case 9605:
|
devId = fbsDeviceId*100000;break;
|
case 61850:
|
case 71850:
|
devId = fbsDeviceId*10000;break;
|
default:
|
devId= fbsDeviceId*100000;break;*/
|
}
|
//设备从未添加,则末位从10开始
|
devId+=9;
|
}
|
devId+=1;
|
|
return devId;
|
}
|
//从数据库读取最大的电源设备id+1作为新增设备的id,设备id为8位数
|
public int getPowerDeviceId(Integer powerDeviceType) {
|
Integer devId = battInfMapper.getMaxPowerDeviceId(powerDeviceType);
|
if(devId==null||devId==0){
|
/*switch (powerDeviceType){
|
//电源系统
|
case 110:
|
//绝缘装置
|
case 111:
|
//充电机
|
case 112:
|
//空调系统
|
case 113:
|
//视频系统
|
case 114:
|
devId = powerDeviceType*1000000;break;
|
}*/
|
devId = powerDeviceType*1000000;
|
}
|
devId+=1;
|
|
return devId;
|
|
}
|
|
|
public Response searchAllStationName1 (Integer uId){
|
List<String> list = battInfMapper.getStationName1ByUserId(uId);
|
return new Response().set(1,list,"查询成功");
|
}
|
|
public Response searchAllStationName2 (Integer uId,String stationName1){
|
List<String> list = battInfMapper.getStationName2ByUseridAndSt1(uId,stationName1);
|
return new Response().set(1,list,"查询成功");
|
}
|
|
public Response searchAllStationName5 (Integer uId,String stationName1,String stationName2){
|
List<String> list = battInfMapper.getStationName5ByUseridAndSt1AndSt2(stationName1,stationName2,uId);
|
return new Response().set(1,list,"查询成功");
|
}
|
|
public Response searchAllStationName(Integer uId,String stationName1,String stationName2,String stationName5){
|
List<Battinf> list = battInfMapper.getAllStationName(uId,stationName1,stationName2,stationName5);
|
return new Response().set(1,list,"查询成功");
|
}
|
|
|
public Response searchBattGroupByStationName (Integer uId,String stationName1,String stationName){
|
List<Battinf> list = battInfMapper.getBattgroupListByStationNameAndStationName1(uId,stationName1,stationName);
|
return new Response().set(1,list,"查询成功");
|
}
|
|
public Response searchStationNameInGroup(Integer uId,String stationName1){
|
List<String> list = battInfMapper.getStationNameInGroup(uId,stationName1);
|
return new Response().set(1,list,"查询成功");
|
}
|
|
|
public Response add(Battinf battinf,Integer userId){
|
int battGroupId = searchMaxBattgroupId_zj();//BattinfGroupFactory.searchMaxBattgroupId();
|
battinf.setBattGroupId(battGroupId);
|
String stationId = searchMaxId_zj();//BattinfGroupFactory.searchStationId();
|
battinf.setStationId(stationId);
|
int dev_id = searchMaxDevId(battinf.getFBSDeviceId().intValue());
|
Battinf bst3 = battInfMapper.judgeBattStationName3(battinf);
|
if (bst3!=null){
|
battinf.setStationId(bst3.getStationId());
|
Battinf bst = battInfMapper.judgeBattStationName(battinf.getStationName(),battinf.getFBSDeviceName());
|
if (bst!=null){
|
battinf.setFBSDeviceId(bst.getFBSDeviceId());
|
}else {
|
battinf.setFBSDeviceId((long) dev_id);
|
}
|
}else {
|
battinf.setStationId(stationId);
|
battinf.setFBSDeviceId((long) dev_id);
|
}
|
battInfMapper.insert(battinf);
|
battInfMapper.insertUserBattgroupBaojigroupBattgroup(battinf.getStationId(),battinf.getBattGroupId());
|
battInfMapper.insertUserBattgroupBaojigroupBattgroupSelect(battinf.getStationId(),battinf.getBattGroupId(),userId);
|
run_cmd();
|
return new Response().set(1,battinf,"添加成功");
|
}
|
|
|
//启动创建数据库的jar文件
|
public void run_cmd() {
|
int configType = Integer.parseInt(environment.getProperty("configFile.type"));
|
ApplicationHome applicationHome = new ApplicationHome(getClass());
|
File jarFile = applicationHome.getDir();
|
String path = "";
|
//测试版
|
if (configType == 1) {
|
path = jarFile.getParentFile().toString();
|
} else {
|
//打包版
|
path = jarFile.toString();
|
}
|
//识别服务器的系统类型
|
String OS = System.getProperty("os.name").toLowerCase();
|
String strcmd = "";
|
if (OS.contains("window")) {
|
|
//String batPath = path + File.separator + "cmd" + File.separator + "cmd_BTSE_DB_Builder.cmd";
|
|
String jarPath = path + File.separator + "BTSE_DB_Builder.exe"; //主程序中jar的目录
|
strcmd = ("cmd /c start ") + jarPath.replaceAll(" ", "\" \"");
|
} else {
|
strcmd = "java -jar /app/BTSE_DB_Builder.jar";
|
}
|
//System.out.println(path);
|
//修复绝对路径中存在空格时执行不成功bug
|
Process child = null;
|
InputStream in = null;
|
try {
|
//System.out.println(strcmd);
|
child = Runtime.getRuntime().exec(strcmd);
|
in = child.getInputStream();
|
int c;
|
while ((c = in.read()) != -1) {
|
//System.out.print((char)c);
|
}
|
in.close();
|
try {
|
child.waitFor();
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
//System.out.println("Run Bat OK....");
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
|
public Response searchInfo(int pageNum,int pageSize,Integer userId,String stationName1,String stationName2,String stationName5,String stationName3){
|
PageHelper.startPage(pageNum,pageSize);
|
List<Battinf> list = battInfMapper.searchInform(userId, stationName1, stationName2,stationName5, stationName3);
|
PageInfo<Battinf> pageInfo = new PageInfo<>(list);
|
return new Response().set(1,pageInfo,"查询成功");
|
}
|
|
public void deleteByBattgroupId(Integer battgroupId){
|
QueryWrapper<Battinf> wrapper = new QueryWrapper<Battinf>().eq("battgroupId",battgroupId);
|
battInfMapper.delete(wrapper);
|
}
|
|
public Response update(Battinf battinf){
|
if (battInfMapper.updateById(battinf)>0){
|
run_cmd();
|
return new Response().set(1,"更新成功");
|
}else {
|
return new Response().set(0,"更新失败");
|
}
|
}
|
|
public Response updateList(List<Battinf> battinfs){
|
QueryWrapper<Battinf> queryWrapper = new QueryWrapper<>();
|
queryWrapper.eq("dev_num",battinfs.get(0).getDevNum());
|
List<Battinf> list = battInfMapper.selectList(queryWrapper);
|
for (Battinf batt:list) {
|
batt.setDevNum(0+"");
|
battInfMapper.updateById(batt);
|
}
|
for (Battinf battinf:battinfs) {
|
battInfMapper.updateById(battinf);
|
}
|
run_cmd();
|
return new Response().set(1,"更新成功");
|
}
|
|
public Response updateIp(Battinf battinf){
|
UpdateWrapper<Battinf> updateWrapper = new UpdateWrapper<Battinf>();
|
updateWrapper.eq("FBSDeviceId",battinf.getFBSDeviceId());
|
int affectRows = battInfMapper.update(battinf,updateWrapper);
|
if (affectRows>0){
|
return new Response().set(1,true,"更新成功");
|
}else {
|
return new Response().set(1, false, "更新失败");
|
}
|
}
|
|
public Response findByBattGroupId(int battGroupId) {
|
QueryWrapper<Battinf> queryWrapper = new QueryWrapper<>();
|
queryWrapper.eq("battgroupid", battGroupId);
|
List<Battinf> list = battInfMapper.selectList(queryWrapper);
|
return new Response().set(1, list, "查询成功");
|
}
|
|
//根据电池组id获取设备id历史数据导出报表使用
|
public Battinf getdevIdByBattGroupId(int battGroupId) {
|
QueryWrapper queryWrapper = new QueryWrapper();
|
queryWrapper.eq("battgroupid", battGroupId);
|
queryWrapper.last("limit 1");
|
Battinf binf = battInfMapper.selectOne(queryWrapper);
|
return binf;
|
}
|
|
public Response searchBattProducer() {
|
List<String> list = battInfMapper.getAllBattProducer();
|
return new Response().set(1, list);
|
}
|
|
public Response updatePublicKeyByDevId(String publicKey, Integer deviceId) {
|
if (battInfMapper.updatePublicKeyByDevId(publicKey, deviceId) > 0) {
|
return new Response().set(1, true, "更新成功");
|
} else {
|
return new Response().set(1, false, "更新失败");
|
}
|
}
|
|
|
public void addBatch(List<Battinf> battInfList) {
|
battInfMapper.insertBatchSomeColumn(battInfList);
|
}
|
|
|
public List<Battinf> getStationList(int userId) {
|
return battInfMapper.getStationList(userId);
|
}
|
|
|
public Response searchNotInBattMapByUid(int uId){
|
List<Battinf> list = battInfMapper.searchNotInBattMapByUid(uId);
|
return new Response().set(1,list,"查询成功");
|
}
|
|
public List<Battinf> getBattGroupInfoByFBSDeviceId(int fbsDeviceId) {
|
QueryWrapper<Battinf> queryWrapper = Wrappers.query();
|
queryWrapper.select("BattGroupId","BattGroupName").eq("FBSDeviceId",fbsDeviceId);
|
return battInfMapper.selectList(queryWrapper);
|
}
|
|
public Response findUseYear(int uId){
|
HashMap<String,Integer> map = new HashMap<>();
|
int yearBelowThree = 0;
|
int yearBetweenThreeAndFive = 0;
|
int yearBetweenFiveAndSeven = 0;
|
int yearAboveSeven = 0;
|
List<Integer> list = battInfMapper.findUseYear(uId);
|
//查询3年内,3-5年,5-7年,7年以上,含头不含尾
|
for (int year:list){
|
if(year<3){
|
yearBelowThree+=1;
|
}else if(year<5){
|
yearBetweenThreeAndFive+=1;
|
}else if(year<7){
|
yearBetweenFiveAndSeven+=1;
|
}else{
|
yearAboveSeven+=1;
|
}
|
}
|
map.put("3年内",yearBelowThree);
|
map.put("3-5年",yearBetweenThreeAndFive);
|
map.put("5-7年",yearBetweenFiveAndSeven);
|
map.put("7年以上",yearAboveSeven);
|
return new Response().set(1,map,"查询成功");
|
}
|
|
public Response findBattProducer(int uId){
|
List<Battinf> list = battInfMapper.findBattProducer(uId);
|
return new Response().set(1,list,"查询成功");
|
}
|
|
public Response findBattProducerInfoByProducerName(String battProducer,int uId){
|
List<BattInfAndEnduranceDTO> list = battInfMapper.findBattProducerInfoByProducerName(battProducer,uId);
|
return new Response().set(1,list,"查询成功");
|
}
|
|
/**
|
* yearCode:
|
* 1:3年以内
|
* 2:3-5年
|
* 3:5到7年
|
* 4:7年以上
|
* @param yearCode
|
* @param uId
|
* @return
|
*/
|
public Response findBattProducerInfoByYearCode(int yearCode,int uId){
|
List<BattInfAndEnduranceDTO> list = battInfMapper.findBattProducerInfoByYearCode(yearCode,uId);
|
return new Response().set(1,list,"查询成功");
|
}
|
|
public Response findBattProducerInfoByTime(Date startTime,Date endTime, int uId){
|
List<BattInfAndEnduranceDTO> list = battInfMapper.findBattProducerInfoByTime(startTime,endTime,uId);
|
return new Response().set(1,list,"查询成功");
|
}
|
|
public Response findBattInfoByStationId(String stationId){
|
QueryWrapper<Battinf> queryWrapper = new QueryWrapper<>();
|
queryWrapper.eq("stationId",stationId);
|
List<Battinf> list = battInfMapper.selectList(queryWrapper);
|
return new Response().set(1,list,"查询成功");
|
}
|
//-----------根据蓄电池组查不重复的MonCount(monNum)(单体编号)
|
public Response searchByMonNum(int battGroupId){
|
List<Battinf> list = battInfMapper.searchByMonNum(battGroupId);
|
for (Battinf inf:list) {
|
inf.setMonNum(Integer.parseInt(inf.getSignalName().split("#")[0]));
|
}
|
return new Response().set(1,list,"查询成功");
|
}
|
|
public List<Battinf> getStationList8059(Long uId) {
|
return battInfMapper.getStationList8059(uId);
|
}
|
|
//取出电池组信息构建excel文件的名称
|
public String serchExcelName(int battGroupId) {
|
Battinf binf = battInfMapper.serchExcelName(battGroupId);
|
String excelName = binf.getStationName1() + "-" + binf.getStationName2() + "-" + binf.getStationName5() + "-" + binf.getStationName3() + "-" + ActionUtil.sdfwithOutday.format(binf.getBattProductDate()).replace("_", "年") + "月-" + (int) ((float) binf.getMonCapStd()) + "AH" + "-" + binf.getBattGroupName();
|
return excelName;
|
}
|
|
//取出电池组信息构建excel文件的名称A059
|
public String serchExcelNameA059(int battGroupId, String stateFlag) {
|
Battinf binf = battInfMapper.serchExcelName(battGroupId);
|
//导出格式:内蒙古自治区-鄂尔多斯市-东胜区-中心局-理士400AH1组-2022-04-21-第1次放电
|
String excelName = binf.getStationName1() + "-" + binf.getStationName2() + "-" + binf.getStationName5() + "-" + binf.getStationName3() + "-" +
|
binf.getBattProducer() + binf.getMonCapStd() + "AH" + (binf.getGroupIndexInFBSDevice() + 1) + "组" + "-" + stateFlag;
|
return excelName;
|
}
|
|
|
public Response uploadPicOfStation(List<MultipartFile> file, String stationId) {
|
String fileDirName = "";
|
int configType = Integer.parseInt(environment.getProperty("configFile.type"));
|
ApplicationHome applicationHome = new ApplicationHome(getClass());
|
File jarFile = applicationHome.getDir();
|
//测试版
|
if (configType == 1) {
|
fileDirName = jarFile.getParentFile().toString();
|
} else {
|
//打包版
|
fileDirName = jarFile.toString();
|
}
|
String root=fileDirName+"/fg_photo/stationsrc/"+ stationId + "/";
|
List<String> filePathList = new ArrayList<>();
|
boolean isSuccess = false;
|
try {
|
for (int i = 0; i < file.size() && stationId != null; i++) {
|
String fileFileName = file.get(i).getOriginalFilename();
|
String filePath = root + fileFileName;
|
createFilefolderIFNotExist(filePath);
|
file.get(i).transferTo(new File(filePath));
|
isSuccess = true;
|
filePathList.add(filePath);
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
if (isSuccess) {
|
return new Response().set(1,filePathList,"上传成功");
|
} else {
|
return new Response().set(0,false,"上传失败");
|
}
|
}
|
|
public Response deletePic(String stationId,List<String> fileNames){
|
String fileDirName = "";
|
int configType = Integer.parseInt(environment.getProperty("configFile.type"));
|
ApplicationHome applicationHome = new ApplicationHome(getClass());
|
File jarFile = applicationHome.getDir();
|
//测试版
|
if(configType==1){
|
fileDirName = jarFile.getParentFile().toString();
|
}else{
|
//打包版
|
fileDirName = jarFile.toString();
|
}
|
boolean deleted = false;
|
String root=fileDirName+"/fg_photo/stationsrc/"+ stationId + "/";
|
for (int i = 0; i < fileNames.size(); i++) {
|
File file = new File(root+fileNames.get(i));
|
if (file.exists()&&file.isFile()){
|
deleted = file.delete();
|
}else {
|
continue;
|
}
|
}
|
if(deleted){
|
return new Response().set(1,true,"删除成功");
|
}else {
|
return new Response().set(1,false,"删除失败");
|
}
|
}
|
|
|
public Response getStationPic(String stationId){
|
String fileDirName = "";
|
int configType = Integer.parseInt(environment.getProperty("configFile.type"));
|
ApplicationHome applicationHome = new ApplicationHome(getClass());
|
File jarFile = applicationHome.getDir();
|
//测试版
|
if(configType==1){
|
fileDirName = jarFile.getParentFile().toString();
|
}else{
|
//打包版
|
fileDirName = jarFile.toString();
|
}
|
String root=fileDirName+"/fg_photo/stationsrc/"+ stationId ;
|
File f = new File(root);
|
System.out.println("文件路径"+root);
|
// 路径不存在
|
if(!f.exists()){
|
return new Response().set(1,false,stationId+"站点暂未上传图片");
|
}
|
|
File result[] = f.listFiles();
|
List<String> fileNames = new ArrayList<>();
|
// 循环遍历
|
for(int i = 0; i<result.length; i++){
|
File fs = result[i];
|
if (fs.isFile()) {
|
fileNames.add(fs.getName());
|
}
|
}
|
return new Response().set(1,fileNames,"查询成功");
|
}
|
|
|
public Battinf getBattGroupInfoByBattGroupId(int battGroupId) {
|
QueryWrapper<Battinf> queryWrapper = Wrappers.query();
|
queryWrapper.select("MonVolStd", "MonCount", "cellShowType").eq("BattGroupId", battGroupId);
|
return battInfMapper.selectOne(queryWrapper);
|
}
|
|
public Response updateMonCapWH(Battinf inf) {
|
Float nomCapWH = inf.getNomCapWH();
|
Float nomCapAH = nomCapWH/53.5f;
|
UpdateWrapper<Battinf> update = Wrappers.update();
|
update.set("NomCapWH",nomCapWH).set("NomCapAH",nomCapAH).eq("battGroupId",inf.getBattGroupId());
|
battInfMapper.update(null,update);
|
return new Response().setII(1,"修改完成");
|
}
|
}
|