This commit is contained in:
zangshuihua 2022-10-18 17:59:40 +08:00
commit 6c336b6513

View File

@ -0,0 +1,657 @@
package com.ruoyi.web.controller.shate;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.BarChart;
import com.ruoyi.common.utils.chartForWord.CustomXWPFDocument;
import com.ruoyi.common.utils.shate.PageUtils;
import com.ruoyi.system.domain_shate.*;
import com.ruoyi.system.service_shate.IDateUploadService;
import com.ruoyi.system.service_shate.IPlantingSuitabilityService;
import com.ruoyi.system.service_shate.ISeedingSuccessService;
import com.ruoyi.system.service_shate.IVegetationHealthServices;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* @Author: JinSheng Song
* @Date: 2022/7/11 15:46
*/
@RestController
@RequestMapping(value = "/api/Upload", produces = "application/json;charset=UTF-8")
@Api(tags = "文件上传记录")
public class DateUploadController
{
@Resource
IPlantingSuitabilityService plantingSuitabilityService;
@Resource
IVegetationHealthServices vegetationHealthServices;
@Resource
private IDateUploadService service;
@Resource
ISeedingSuccessService seedingSuccessService;
private String FilePathName;
private String ExePathName;
private String FilePaths;
private String ShpPaths;
@RequestMapping(value = "/SelectUpload",method = {RequestMethod.POST})
public AjaxResult SelectUpload(@RequestBody paging paging)
{
// LocalDate tomorrow = LocalDate.of(2022,10,2);
// LocalDate today = LocalDate.now();
//
// if(tomorrow.isAfter(today)==false)
// {
// return AjaxResult.error("软件授权日期已过,请联系管理人员!");
// }
String type1=paging.getType1().replace(" ","");
String type=paging.getType().replace(" ","");
String type2="";
if (paging.getType2()!=null)
{
type2= paging.getType2().toUpperCase();
}
String date=paging.getUploadTime();
String name=paging.getType3();
String dateTime =paging.getDateTime();
if ( name!=null && name!="")
{
if (name.equals("1"))
{
name="M_Heal%";
}
else if(name.equals("2"))
{
name="H_Heal%";
}
}
List<DateUpload> Seeding= service.selectUpload(paging.getZone(),type.toUpperCase(), type1.toUpperCase(),
type2,paging.getYear(),date,name,dateTime);
pageVO vaue=new pageVO();
List<?> value= PageUtils.getListPaging(Seeding,paging.getPageNum(),paging.getPageSize());
vaue.setValue(value);
vaue.setTotal(Seeding.size());
return AjaxResult.success(vaue);
}
@RequestMapping(value = "/delUpload",method = {RequestMethod.POST})
public AjaxResult DelUpload(@RequestBody UplodFile uplodFile)
{
return delUploadFile(uplodFile.getId());
// List<Integer> ids=uplodFile.getId();
// Integer Seeding=0;
// UploadFile file=new UploadFile();
// for (int i=0; i<ids.size();i++)
// {
// DateUpload value= service.selectDel(ids.get(i));
//
// String[] strs2=value.getFileName().split("\\_");
//
//// file.UploadFile1(strs2[1],value.getZone(),value.getDataTime());
//
// Seeding= service.DelUpload(ids.get(i));
// if (Seeding==0){return AjaxResult.error("没有此id单据"+ids.get(i));}
// }
//
// return AjaxResult.success(Seeding);
}
@RequestMapping(value = "/delUpload1",method = {RequestMethod.DELETE})
public AjaxResult DelUpload1(@RequestParam("id") List<Integer> id)
{
List<Integer> ids=id;
Integer Seeding=0;
for (int i=0; i<ids.size();i++)
{
Seeding= service.DelUpload(ids.get(i));
if (Seeding==0){return AjaxResult.error("没有此id单据"+ids.get(i));}
}
return AjaxResult.success(Seeding);
}
@RequestMapping(value = "/InitShp",method = {RequestMethod.POST})
public AjaxResult InitShp(@RequestBody UplodFile uplodFile)
{
List<DateUpload> dateUploads=infr(uplodFile.getDateUploads());
for (int i=0; i<dateUploads.size();i++)
{
Integer Seeding= service.IntoShp(dateUploads.get(i));
}
return AjaxResult.success("成功");
}
@RequestMapping(value = "/InitUpload",method = {RequestMethod.POST})
public AjaxResult InitUpload(@RequestBody UplodFile uplodFile)
{
readXml();
List<DateUpload> dateUploads=infr(uplodFile.getDateUploads());
for (int i=0; i<dateUploads.size();i++)
{
Integer Seeding= service.IntoUpload(dateUploads.get(i));
}
return AjaxResult.success("成功");
}
private List<DateUpload> infr(List<DateUpload> dateUpload)
{
for (int i=0;i<dateUpload.size();i++)
{
String [] typesList=dateUpload.get(i).getType1().split("\\/");
String type1=typesList[1].replace(" ","").toUpperCase();
String type=typesList[0].replace(" ","").toUpperCase();
Date d=new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String dateNowStr = sdf.format(d);
if(type1.equals("EVALUATIONINDICATORS"))
{
String[] strs = dateUpload.get(i).getFileName().split("\\.");
String name=strs[0];
String[] strs2=name.split("\\_");
String[] typeValue=name.split("2");
String type2=typeValue[0];
String name1=strs2[strs2.length-1];
String name2=strs2[strs2.length-2];
dateUpload.get(i).setDataTime(name2);
dateUpload.get(i).setZone(name1);
dateUpload.get(i).setCreatedTime(dateNowStr);
dateUpload.get(i).setType(type);
dateUpload.get(i).setType1(type1);
dateUpload.get(i).setYear(name2.substring(0,4));
dateUpload.get(i).setType2(type2.substring(0,type2.length()-1).toUpperCase());
dateUpload.get(i).setExtent1(dateUpload.get(i).getExtent().toString());
DateUpload uploadValue=service.selectShp(name1,type,"");
MyThread1 demo=new MyThread1(name.replace(".tif",""),
FilePaths+dateUpload.get(i).getData_id(),
ShpPaths+uploadValue.getData_id());
demo.run();
}
else if(type1.equals("BOUNDARIES"))//Boundaries
{
String[] strs = dateUpload.get(i).getFileName().split("\\.");
String name=strs[0];
String[] strs2=name.split("\\_");
String name1=strs2[strs2.length-1];
dateUpload.get(i).setZone(name1);
dateUpload.get(i).setCreatedTime(dateNowStr);
dateUpload.get(i).setYear(dateNowStr.substring(0,4));
dateUpload.get(i).setType(type);
dateUpload.get(i).setType1(type1);
dateUpload.get(i).setExtent1(dateUpload.get(i).getExtent().toString());
}
else if(type1.equals("LANDUSE"))
{
String[] strs = dateUpload.get(i).getFileName().split("\\.");
String name=strs[0];
String[] strs2=name.split("\\_");
String[] typeValue=name.split("2");
String type2=typeValue[0];
String name1=strs2[strs2.length-1];
String name2=strs2[strs2.length-2];
dateUpload.get(i).setDataTime(name2);
dateUpload.get(i).setZone(name1);
dateUpload.get(i).setCreatedTime(dateNowStr);
dateUpload.get(i).setType(type);
dateUpload.get(i).setType1(type1);
dateUpload.get(i).setYear(name2.substring(0,4));
dateUpload.get(i).setType2(type2.substring(0,type2.length()-1).toUpperCase());
dateUpload.get(i).setExtent1(dateUpload.get(i).getExtent().toString());
DateUpload uploadValue=service.selectShp(name1,type,"");
MyThread1 demo=new MyThread1(name.replace(".tif",""),
FilePaths+dateUpload.get(i).getData_id(),
ShpPaths+uploadValue.getData_id());
demo.run();
}
else if(type1.equals("SUITABILITY"))
{
String[] strs = dateUpload.get(i).getFileName().split("\\.");
String name=strs[0];
String[] strs2=name.split("\\_");
String[] typeValue=name.split("2");
String type2=typeValue[0];
String name1=strs2[strs2.length-1];
String name2=strs2[strs2.length-2];
dateUpload.get(i).setDataTime(name2);
dateUpload.get(i).setZone(name1);
dateUpload.get(i).setCreatedTime(dateNowStr);
dateUpload.get(i).setType(type);
dateUpload.get(i).setType1(type1);
dateUpload.get(i).setYear(name2.substring(0,4));
dateUpload.get(i).setType2(type2.substring(0,type2.length()-1).toUpperCase());
dateUpload.get(i).setExtent1(dateUpload.get(i).getExtent().toString());
DateUpload uploadValue=service.selectShp(name1,type,"");
MyThread1 demo=new MyThread1(name.replace(".tif",""),
FilePaths+dateUpload.get(i).getData_id(),
ShpPaths+uploadValue.getData_id());
demo.run();
}
else
{
String[] strs = dateUpload.get(i).getFileName().split("\\.");
String name=strs[0];
String[] strs2=name.split("\\_");
String name1=strs2[strs2.length-1];
String name2=strs2[strs2.length-2];
dateUpload.get(i).setDataTime(name2);
Integer month=Integer.parseInt(name2.substring(4,6));
dateUpload.get(i).setYear(name2.substring(0,4));
dateUpload.get(i).setMonth(month);
dateUpload.get(i).setZone(name1);
dateUpload.get(i).setCreatedTime(dateNowStr);
dateUpload.get(i).setType(type);
dateUpload.get(i).setType1(type1);
dateUpload.get(i).setExtent1(dateUpload.get(i).getExtent().toString());
DateUpload uploadValue=service.selectShp(name1,type,"");
MyThread1 demo=new MyThread1(name.replace(".tif",""),
FilePaths+dateUpload.get(i).getData_id(),
ShpPaths+uploadValue.getData_id());
demo.run();
}
}
return dateUpload;
}
/**
* 调用exe
* @param fileName 文件名称
* @param filePath tif文件路径
* @param shpPath shp文件路径
*/
@RequestMapping(value = "/callExe",method = {RequestMethod.GET})
public void callExe(String fileName,String filePath,String shpPath)
{
BufferedReader bufferedReader=null;
Process proc=null;
try {
// String til = UrlFileUpload.fileUpload(filePath,FilePathName);
//
// String shp = UrlFileUpload.fileUpload(shpPath,FilePathName);
String[] cmd = {ExePathName,
fileName,
filePath+".tif",
shpPath+".zip"};
ProcessBuilder pb = new ProcessBuilder();
pb.command(cmd);
proc=pb.start();
System.out.println("Start calling algorithm");
bufferedReader =new BufferedReader(new InputStreamReader(proc.getErrorStream()));
String aa=null;
String line=null;
while ((line=bufferedReader.readLine())!=null){
aa=aa+line;
}
System.out.println(aa);
} catch (IOException e) {
e.printStackTrace();
System.out.println(e.getMessage());;
}
}
public class MyThread1 extends Thread{
private String FileName;
private String FilePath;
private String ShpPath;
public MyThread1(String fileName,String filePath,String shpPath){
this.FileName = fileName;
this.FilePath = filePath;
this.ShpPath = shpPath;
}
@Override
public void run(){
callExe(FileName,FilePath,ShpPath);
}
}
/**
* 删除
* @return
*/
@RequestMapping(value = "/delUploadFile",method = {RequestMethod.DELETE})
public AjaxResult delUploadFile(List<Integer> id) {
List<Integer> ids = id;
UploadFile file = new UploadFile();
for (int i = 0; i < ids.size(); i++) {
Integer Seeding=0;
DateUpload value = service.selectDel(ids.get(i));
String type1=value.getType1();
String type= value.getType();
if (type1.equals("EVALUATIONINDICATORS")) {
//value.getType2();
String[] typeValue = value.getFileName().split("2");
String type2 = typeValue[0];
type2=type2.substring(0,type2.length()-1);
file.UploadFile1(type2, value.getZone(), value.getDataTime(),service,"");
Seeding= service.DelUpload(ids.get(i));
if (Seeding==0){return AjaxResult.error("没有此id单据"+ids.get(i));}
}
// &&
// (type1.equals("RSIMAGES") || type1.equals("PLANTINGDISTRIBUTION") || type1.equals("VEGETATIONHEALTH"))
else if (type.equals("VEGETATIONHEALTH"))
{
String[] typeValue = value.getFileName().split("_");
String resolution=typeValue[0];
String type2 = "Heal";
file.UploadFile1(type2, value.getZone(), value.getDataTime(),service,resolution);
Seeding= service.DelUpload(ids.get(i));
if (Seeding==0){return AjaxResult.error("没有此id单据"+ids.get(i));}
}
else
{
String[] typeValue = value.getFileName().split("_");
String type2 = typeValue[0];
file.UploadFile1(type2, value.getZone(), value.getDataTime(),service,"");
Seeding= service.DelUpload(ids.get(i));
if (Seeding==0){return AjaxResult.error("没有此id单据"+ids.get(i));}
}
}
return AjaxResult.success("成功!");
}
public void readXml() {
try {
// 创建解析器工厂
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
// 创建一个Document对象
String parent = new File(ResourceUtils.getURL("classpath:").getPath()).getParentFile().getParentFile().getParent();
File file = new File("route.xml");
Document doc =db.parse(file);
NodeList routeList = doc.getElementsByTagName("Rodel");
// 遍历每个book节点
for (int i = 0; i < routeList.getLength(); i++) {
// 索引从零开始
org.w3c.dom.Node book = routeList.item(i);
// 获取book节点所有属性集合
org.w3c.dom.NamedNodeMap attrs = book.getAttributes();
NodeList childNodes = book.getChildNodes();
for (int k = 0; k < childNodes.getLength(); k++) {
// 区分,去掉空格和换行符
if (childNodes.item(k).getNodeType() == Node.ELEMENT_NODE) {
this.ExePathName=childNodes.item(1).getTextContent().trim();
this.FilePathName=childNodes.item(3).getTextContent().trim();
this.FilePaths=childNodes.item(5).getTextContent().trim();
this.ShpPaths=childNodes.item(7).getTextContent().trim();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 导出Word
* @param
*/
@RequestMapping(value = "/exportWord",method = {RequestMethod.GET})
@ResponseBody
public void exportWord(HttpServletResponse response, HttpServletRequest request) throws Exception {
//接受前端传来的数据
String year = "2022";//request.getParameter("year");
String zone = "01";//request.getParameter("zone");
//创建文本对象
CustomXWPFDocument docxDocument = new CustomXWPFDocument();
//创建标题
BarChart.setTitle(docxDocument,"Evaluation report of planting monitoring in ITBA Nature Reserve");
//创建第一个段落的标题
BarChart.createFirstLevelTopic(docxDocument, "1.Background information");
//创建第一个段落的内容
BarChart.createParagraphAndInsertWord(docxDocument, "Date time: "+year);
BarChart.createParagraphAndInsertWord(docxDocument, "Evaluation area: "+zone);
//创建第二段落的标题
BarChart.createFirstLevelTopic(docxDocument, "2.Results of remote sensing monitoring of seeding success rate");
//循环判断有多少个资源
List<SeedingSuccessRate> seedingSuccessRates = seedingSuccessService.sqlSeeding(zone, year);
for (SeedingSuccessRate seedingSuccessRate : seedingSuccessRates) {
//创建第二段落的内容
BarChart.insertPic(docxDocument, "C:\\Users\\xkrs\\Desktop\\pic\\1.png");
BarChart.setPicTitle(docxDocument, "Spatial distribution data of successful seeding regions");
BarChart.insertPic(docxDocument, "C:\\Users\\xkrs\\Desktop\\pic\\2.png");
BarChart.setPicTitle(docxDocument, "Spatial distribution data of seeding success rate");
BarChart.createParagraphAndInsertWord(docxDocument, ""+BarChart.changeDate(seedingSuccessRate.getImageDate())+". The total planting area in "+seedingSuccessRate.getZone()+" area was "+BarChart.getDoubleNumber((seedingSuccessRate.getSeedingSuccessArea()/seedingSuccessRate.getSeedingSuccess())) +" hm², the vegetation survival area was "+seedingSuccessRate.getSeedingSuccessArea()+" hm², and the seeding success rate was "+seedingSuccessRate.getSeedingSuccess()+"%.");
}
//第三段的标题
BarChart.createFirstLevelTopic(docxDocument, "3.Results of remote sensing monitoring of vegetation health");
//获取ProductCode1的集合
List<String> Heal_code1=new ArrayList<>();
List<VegetationHealth> vegetationHealths = vegetationHealthServices.sqlInfo(zone, year);
for (VegetationHealth vegetationHealth : vegetationHealths) {
String productCode1 = vegetationHealth.getProductCode1();
System.out.println(productCode1);
Heal_code1.add(vegetationHealth.getProductCode1());
}
for(int i = 0; i <Heal_code1.size(); i=i+2) {
List<VegetationHealth> vegetationHealths_M = vegetationHealthServices.sqlSeeding(zone, Heal_code1.get((i+1)), year);
for (VegetationHealth vegetationHealth_m : vegetationHealths_M) {
//第三段的内容
//第三段的第一个小标题
BarChart.createSecondLevelTopic(docxDocument, "3."+(i+1)+".Monitoring results of medium resolution data");
//第三段的第一个小标题的内容
BarChart.insertPic(docxDocument, "C:\\Users\\xkrs\\Desktop\\pic\\2.png");
BarChart.setPicTitle(docxDocument, "Spatial distribution data of vegetation planting area");
BarChart.insertPic(docxDocument, "C:\\Users\\xkrs\\Desktop\\pic\\1.png");
BarChart.setPicTitle(docxDocument, "Spatial distribution data of vegetation health status");
//插入柱状图表
String[] xAxisData = new String[]{"Health", "Normal", "Not-Health"};
Double m1 = vegetationHealth_m.getHealthy();
Double m2 = vegetationHealth_m.getNormal();
Double m3 = vegetationHealth_m.getNotHealthy();
Double[] yAxisData = new Double[]{m1, m2, m3};
BarChart.drawTable(docxDocument,xAxisData,yAxisData);
BarChart.setPicTitle(docxDocument, "Statistical data on vegetation health");
BarChart.createParagraphAndInsertWord(docxDocument, ""+BarChart.changeDate(vegetationHealth_m.getImageDate())+". Based on the monitoring results of high resolution satellite images, the area of healthy vegetation growth in "+vegetationHealth_m.getZone()+" region was "+vegetationHealth_m.getHealthy()+" hm², accounting for "+BarChart.getDoubleNumber((vegetationHealth_m.getHealthy()/vegetationHealth_m.getTotalArea())*100)+"%. The area with normal vegetation growth was "+vegetationHealth_m.getNormal()+" hm², accounting for "+BarChart.getDoubleNumber((vegetationHealth_m.getNormal()/vegetationHealth_m.getTotalArea())*100)+"%. The area of unhealthy vegetation growth was "+vegetationHealth_m.getNotHealthy()+" hm², accounting for "+BarChart.getDoubleNumber((vegetationHealth_m.getNotHealthy()/vegetationHealth_m.getTotalArea())*100)+"%.");
}
//第三段的第二个小标题
List<VegetationHealth> vegetationHealths_H = vegetationHealthServices.sqlSeeding(zone, Heal_code1.get(i), year);
int j=i+2;
for (VegetationHealth vegetationHealth_h : vegetationHealths_H) {
//第三段的第二个小标题
BarChart.createSecondLevelTopic(docxDocument, "3."+j+".Monitoring results of high resolution data");
//第三段的第二个小标题的内容
BarChart.insertPic(docxDocument, "C:\\Users\\xkrs\\Desktop\\pic\\1.png");
BarChart.setPicTitle(docxDocument, "Spatial distribution data of vegetation planting area");
BarChart.insertPic(docxDocument, "C:\\Users\\xkrs\\Desktop\\pic\\2.png");
BarChart.setPicTitle(docxDocument, "Spatial distribution data of vegetation health status");
//此处插入图表
String[] xAxisData1 = new String[]{"Health", "Normal", "Not-Health"};
Double h1 = vegetationHealth_h.getHealthy();
Double h2 = vegetationHealth_h.getNormal();
Double h3 = vegetationHealth_h.getNotHealthy();
Double[] yAxisData1 = new Double[]{h1, h2, h3};
BarChart.drawTable(docxDocument,xAxisData1,yAxisData1);
BarChart.setPicTitle(docxDocument, "Statistical data on vegetation health");
BarChart.createParagraphAndInsertWord(docxDocument, ""+BarChart.changeDate(vegetationHealth_h.getImageDate())+". Based on the monitoring results of high resolution satellite images, the area of healthy vegetation growth in "+vegetationHealth_h.getZone()+" region was "+vegetationHealth_h.getHealthy()+" hm², accounting for "+BarChart.getDoubleNumber((vegetationHealth_h.getHealthy()/vegetationHealth_h.getTotalArea())*100)+"%. The area with normal vegetation growth was "+vegetationHealth_h.getNormal()+" hm², accounting for "+BarChart.getDoubleNumber((vegetationHealth_h.getNormal()/vegetationHealth_h.getTotalArea())*100)+"%. The area of unhealthy vegetation growth was "+vegetationHealth_h.getNotHealthy()+" hm², accounting for "+BarChart.getDoubleNumber((vegetationHealth_h.getNotHealthy()/vegetationHealth_h.getTotalArea())*100)+"%.");
}
}
//第四个标题
List<PlantingSuitability> plantingSuitabilities = plantingSuitabilityService.sqlSeeding(zone);
for (PlantingSuitability suitability : plantingSuitabilities) {
BarChart.createFirstLevelTopic(docxDocument,"4.Results of remote sensing monitoring of planting suitability");
//第四个标题的内容
BarChart.insertPic(docxDocument,"C:\\Users\\xkrs\\Desktop\\pic\\1.png");
BarChart.setPicTitle(docxDocument,"Spatial distribution data of planting suitability");
//此处插入图表
String[] xAxisData2 = new String[]{"Health", "Normal", "Not-Health"};
Double s1 = suitability.getVerySuitable();
Double s2 = suitability.getSuitable();
Double s3 = suitability.getNotSuitable();
Double[] yAxisData2 = new Double[]{s1, s2, s3};
BarChart.drawTable(docxDocument,xAxisData2,yAxisData2);
BarChart.setPicTitle(docxDocument,"Spatial distribution data of planting suitability");
BarChart.createParagraphAndInsertWord(docxDocument,"In "+suitability.getZone()+" region, the area of very suitable region was "+suitability.getVerySuitable()+" hm², accounting for "+BarChart.getDoubleNumber((suitability.getVerySuitable()/suitability.getTotalArea())*100)+"%. The area of suitable grade was "+suitability.getSuitable()+" hm², accounting for "+BarChart.getDoubleNumber((suitability.getSuitable()/suitability.getTotalArea())*100)+"%. The area of unsuitable area was "+suitability.getNotSuitable()+" hm², accounting for "+BarChart.getDoubleNumber((suitability.getNotSuitable()/suitability.getTotalArea())*100)+"%.");
}
response.setCharacterEncoding("UTF-8");
response.setContentType("application/msword");
//文件名
String fileName = "Evaluation report of planting monitoring in ITBA Nature Reserve - V2.0.docx";
response.setHeader("Content-Disposition","attachment;fileName="+new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
ServletOutputStream responseOutputStream = response.getOutputStream();
docxDocument.write(responseOutputStream);
responseOutputStream.flush();
responseOutputStream.close();
}
}