Commit 44454d4e by landerliang@163.com

自动签章上传,先判断当前车牌号下是否不为空,为空则不进行报告下载,节省资源

parent 010404dc
...@@ -137,13 +137,14 @@ public class ReportService { ...@@ -137,13 +137,14 @@ public class ReportService {
reportDetailsReqVo.setCarNum(testReportVo.getVLPN()); reportDetailsReqVo.setCarNum(testReportVo.getVLPN());
reportDetailsReqVo.setReportNum(testReportVo.getInspectionNum()); reportDetailsReqVo.setReportNum(testReportVo.getInspectionNum());
//下载报告pdf 并写入数据库 //下载报告pdf
reportPdfVo = new ReportPdfVo();
String path = carReportUtil.authDownloadReport(account, password, reportDetailsReqVo); String path = carReportUtil.authDownloadReport(account, password, reportDetailsReqVo);
//如果下载成功 //如果下载成功
if(StrUtil.isNotBlank(path)){ if(StrUtil.isNotBlank(path)){
// 写入数据库
reportPdfVo = new ReportPdfVo();
reportPdfVo.setPath(path); reportPdfVo.setPath(path);
reportPdfVo.setVehicleId(testReportVo.getVehicleID()); reportPdfVo.setVehicleId(testReportVo.getVehicleID());
reportPdfVo.setUniqueString(testReportVo.getUniqueString()); reportPdfVo.setUniqueString(testReportVo.getUniqueString());
...@@ -337,7 +338,6 @@ public class ReportService { ...@@ -337,7 +338,6 @@ public class ReportService {
AssertUtil.isNotNull(deptVo,"列表获取失败,未找到当前账号所属部门"); AssertUtil.isNotNull(deptVo,"列表获取失败,未找到当前账号所属部门");
String reportPath = ""; String reportPath = "";
try { try {
//下载检测报告 //下载检测报告
reportPath = carReportUtil.downloadReport(deptVo.getEpAccount(), deptVo.getEpPassword(), detailsReqVo); reportPath = carReportUtil.downloadReport(deptVo.getEpAccount(), deptVo.getEpPassword(), detailsReqVo);
}catch (Exception e) { }catch (Exception e) {
......
...@@ -766,146 +766,152 @@ public class CarReportUtil { ...@@ -766,146 +766,152 @@ public class CarReportUtil {
WebDriver driver = null; WebDriver driver = null;
try { try {
//把检测报告编号作为该报告文件的文件名 //判断是否已拍照有文件再进行下载
String fileName = detailsReqVo.getReportNum(); if(!FileUtil.isDirEmpty(FileUtil.file(uploadFilePath + detailsReqVo.getCarNum()))){
//如果已经下载有检测报告则直接return
String reportHtmlPath = getReportPath(fileName,".html"); //把检测报告编号作为该报告文件的文件名
String fileName = detailsReqVo.getReportNum();
//如果已经下载有检测报告则直接return
String reportHtmlPath = getReportPath(fileName,".html");
/*if(FileUtil.isNotEmpty(FileUtil.file(fileName))){ /*if(FileUtil.isNotEmpty(FileUtil.file(fileName))){
return getReportPath(fileName,".pdf"); return getReportPath(fileName,".pdf");
}*/ }*/
//登录环保 //登录环保
driver = loginEp(username, password,BrowserVersion.INTERNET_EXPLORER); driver = loginEp(username, password,BrowserVersion.INTERNET_EXPLORER);
Thread.sleep(1000); Thread.sleep(1000);
//抓取检测报告超链接 //抓取检测报告超链接
WebElement inspecReportBtn = driver.findElement(new By.ById("mainMenuTree_3_a")); WebElement inspecReportBtn = driver.findElement(new By.ById("mainMenuTree_3_a"));
inspecReportBtn.click(); inspecReportBtn.click();
//延迟加载html //延迟加载html
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS) driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS)
.setScriptTimeout(60,TimeUnit.SECONDS).pageLoadTimeout(120,TimeUnit.SECONDS); .setScriptTimeout(60,TimeUnit.SECONDS).pageLoadTimeout(120,TimeUnit.SECONDS);
Thread.sleep(3000); Thread.sleep(3000);
//点击完检测报告的超链接后,抓取frame窗口的元素 //点击完检测报告的超链接后,抓取frame窗口的元素
WebElement homeFrame = driver.findElement(new By.ByXPath("//*[@id=\"tabs\"]/div[2]/div[2]/div/iframe")); WebElement homeFrame = driver.findElement(new By.ByXPath("//*[@id=\"tabs\"]/div[2]/div[2]/div/iframe"));
//切换到iframe页面 //切换到iframe页面
driver = driver.switchTo().frame(homeFrame); driver = driver.switchTo().frame(homeFrame);
//延迟加载html //延迟加载html
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS) driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS)
.setScriptTimeout(60,TimeUnit.SECONDS).pageLoadTimeout(120,TimeUnit.SECONDS); .setScriptTimeout(60,TimeUnit.SECONDS).pageLoadTimeout(120,TimeUnit.SECONDS);
Thread.sleep(2000); Thread.sleep(2000);
WebElement element = driver.findElement(new By.ByXPath("//*[@id=\"table_Condition\"]/tbody/tr[2]/td[3]/input")); WebElement element = driver.findElement(new By.ByXPath("//*[@id=\"table_Condition\"]/tbody/tr[2]/td[3]/input"));
element.click(); element.click();
//获取车牌号输入框 //获取车牌号输入框
WebElement carNumInput = driver.findElement(new By.ByXPath("//*[@id=\"conWindow\"]/div[1]/div/table/tbody/tr[1]/td[2]/input")); WebElement carNumInput = driver.findElement(new By.ByXPath("//*[@id=\"conWindow\"]/div[1]/div/table/tbody/tr[1]/td[2]/input"));
carNumInput.sendKeys(detailsReqVo.getCarNum()); carNumInput.sendKeys(detailsReqVo.getCarNum());
//点击检索按钮 //点击检索按钮
driver.findElement(new By.ById("btnVin")).click(); driver.findElement(new By.ById("btnVin")).click();
//延迟加载html //延迟加载html
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS) driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS)
.setScriptTimeout(60,TimeUnit.SECONDS).pageLoadTimeout(120,TimeUnit.SECONDS); .setScriptTimeout(60,TimeUnit.SECONDS).pageLoadTimeout(120,TimeUnit.SECONDS);
Thread.sleep(2000); Thread.sleep(2000);
//抓取检索后的列表 //抓取检索后的列表
List<WebElement> trElements = driver.findElements(new By.ByXPath("//*[contains(@id,'datagrid-row-')]")); List<WebElement> trElements = driver.findElements(new By.ByXPath("//*[contains(@id,'datagrid-row-')]"));
AssertUtil.isTrue(trElements.size()>0,"获取该检测报告失败!"); AssertUtil.isTrue(trElements.size()>0,"获取该检测报告失败!");
//拿出对应的那条记录(根据检测编码) //拿出对应的那条记录(根据检测编码)
WebElement trElement =null; WebElement trElement =null;
for (WebElement tr: trElements){ for (WebElement tr: trElements){
if(tr.getText().contains(detailsReqVo.getCarNum())){ if(tr.getText().contains(detailsReqVo.getCarNum())){
trElement = tr; trElement = tr;
}
} }
}
//点击检索出来的列表 //点击检索出来的列表
trElement.click(); trElement.click();
//延迟加载html //延迟加载html
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS) driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS)
.setScriptTimeout(60, TimeUnit.SECONDS).pageLoadTimeout(120, TimeUnit.SECONDS); .setScriptTimeout(60, TimeUnit.SECONDS).pageLoadTimeout(120, TimeUnit.SECONDS);
Thread.sleep(3000); Thread.sleep(3000);
//抓取检测报告frame //抓取检测报告frame
WebElement reportFrame = driver.findElement(new By.ById("iframe_divReport")); WebElement reportFrame = driver.findElement(new By.ById("iframe_divReport"));
//切换到检测报告frame //切换到检测报告frame
driver = driver.switchTo().frame(reportFrame); driver = driver.switchTo().frame(reportFrame);
Thread.sleep(2000); Thread.sleep(2000);
//抓取打印预览按钮进行点击
//WebElement printView = reportDriver.findElement(new By.ById("A1"));
WebElement printView =driver.findElement(new By.ById("btnPlaySelectAll"));
printView.click();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS)
.setScriptTimeout(20, TimeUnit.SECONDS).pageLoadTimeout(120, TimeUnit.SECONDS);
boolean isError = driver.getPageSource().contains("无检测方法结果数据不允许打印操作");
AssertUtil.isNotTrue(isError,"无检测方法结果数据不允许打印操作");
Thread.sleep(3000);
//手动在授权人签字栏加上当天日期
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd");
String todayStr = simpleDateFormat.format(DateUtil.date());
//替换掉css样式的链接以及去掉多余的元素
Document parse = Jsoup.parse(driver.getPageSource());
parse.select("a").remove();
String reportHtml = parse.html();
reportHtml = reportHtml.replace("src=\"../..", "src=\"" + host).replace("src=\"/businesscom", "src=\"" + host + "businesscom")
.replace("黑体","SimSun").replace("<br> \n" +
" <br>","<br style=\"page-break-after:always;\"> \n" +
" <br style=\"page-break-after:always;\">")
.replace("<br> \n" +
" <font color=\"#FF00FF\"> 打印控件未安装!点击这里 ,安装后请刷新页面或重新进入。 </font> \n" +
" <br> \n" +
" <br> \n" +
" <font color=\"#FF00FF\"> (如果此前正常,仅因浏览器升级或重安装而出问题,需重新执行以上安装) </font>", " ")
.replace("<div>","<div style=\"margin-left:-460px\">")
.replace("<td style=\"width:13.25%;\"> 授权签字人 </td> \n" +
" <td colspan=\"4\" style=\"width:30%;border-left:none;border-right:none;\"></td>",
"<td style=\"width:13.25%;\"> 授权签字人 </td> \n" +
" <td colspan=\"4\" style=\"width:30%;border-left:none;border-right:none;padding-left:100px\">" + todayStr + "</td>");
Document parse1 = Jsoup.parse(reportHtml);
String imgSrc = parse1.getElementById("qrImg").attr("src");
Object[] cookies = driver.manage().getCookies().toArray();
byte[] respBytes = HttpUtil.createGet(imgSrc).header("Cookie", cookies[0].toString() + ";" + cookies[1].toString() + ";" + cookies[2].toString()).execute().bodyBytes();
FileUtil.writeBytes(respBytes, getReportPath(fileName,".png"));
//抓取打印预览按钮进行点击
//WebElement printView = reportDriver.findElement(new By.ById("A1"));
WebElement printView =driver.findElement(new By.ById("btnPlaySelectAll"));
printView.click();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS)
.setScriptTimeout(20, TimeUnit.SECONDS).pageLoadTimeout(120, TimeUnit.SECONDS);
boolean isError = driver.getPageSource().contains("无检测方法结果数据不允许打印操作");
AssertUtil.isNotTrue(isError,"无检测方法结果数据不允许打印操作");
Thread.sleep(3000);
//手动在授权人签字栏加上当天日期 //将二维码转换成base64流 踩坑: 不转base64的话,转PDF后,图片显示不出来
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd"); BASE64Encoder encoder = new BASE64Encoder();
String todayStr = simpleDateFormat.format(DateUtil.date()); String base64Qrcode = encoder.encode(respBytes);
//将base64流的二维码加到页面上
//替换掉css样式的链接以及去掉多余的元素 parse1.getElementById("qrImg").attr("src","data:image/png;base64," + base64Qrcode);
Document parse = Jsoup.parse(driver.getPageSource());
parse.select("a").remove();
String reportHtml = parse.html();
reportHtml = reportHtml.replace("src=\"../..", "src=\"" + host).replace("src=\"/businesscom", "src=\"" + host + "businesscom")
.replace("黑体","SimSun").replace("<br> \n" +
" <br>","<br style=\"page-break-after:always;\"> \n" +
" <br style=\"page-break-after:always;\">")
.replace("<br> \n" +
" <font color=\"#FF00FF\"> 打印控件未安装!点击这里 ,安装后请刷新页面或重新进入。 </font> \n" +
" <br> \n" +
" <br> \n" +
" <font color=\"#FF00FF\"> (如果此前正常,仅因浏览器升级或重安装而出问题,需重新执行以上安装) </font>", " ")
.replace("<div>","<div style=\"margin-left:-460px\">")
.replace("<td style=\"width:13.25%;\"> 授权签字人 </td> \n" +
" <td colspan=\"4\" style=\"width:30%;border-left:none;border-right:none;\"></td>",
"<td style=\"width:13.25%;\"> 授权签字人 </td> \n" +
" <td colspan=\"4\" style=\"width:30%;border-left:none;border-right:none;padding-left:100px\">" + todayStr + "</td>");
Document parse1 = Jsoup.parse(reportHtml);
String imgSrc = parse1.getElementById("qrImg").attr("src");
Object[] cookies = driver.manage().getCookies().toArray();
byte[] respBytes = HttpUtil.createGet(imgSrc).header("Cookie", cookies[0].toString() + ";" + cookies[1].toString() + ";" + cookies[2].toString()).execute().bodyBytes();
FileUtil.writeBytes(respBytes, getReportPath(fileName,".png"));
//将二维码转换成base64流 踩坑: 不转base64的话,转PDF后,图片显示不出来
BASE64Encoder encoder = new BASE64Encoder();
String base64Qrcode = encoder.encode(respBytes);
//将base64流的二维码加到页面上 FileUtil.writeBytes(parse1.html().getBytes("UTF-8"), reportHtmlPath);
parse1.getElementById("qrImg").attr("src","data:image/png;base64," + base64Qrcode);
FileUtil.writeBytes(parse1.html().getBytes("UTF-8"), reportHtmlPath); //将保存到本地的html转成pdf
PdfUtil.convert(reportHtmlPath,getReportPath(fileName,".pdf"));
//离开当前检测报告页返回列表页面
//driver.switchTo().parentFrame();
//将保存到本地的html转成pdf log.info(">> 检测报告下载完成!");
PdfUtil.convert(reportHtmlPath,getReportPath(fileName,".pdf"));
//离开当前检测报告页返回列表页面 if(driver != null){
//driver.switchTo().parentFrame(); driver.quit();
}
log.info(">> 检测报告下载完成!"); return getReportPath(fileName, ".pdf");
if(driver != null){
driver.quit();
} }
return getReportPath(fileName, ".pdf");
} catch (InterruptedException | UnsupportedEncodingException | UnhandledAlertException e) { } catch (InterruptedException | UnsupportedEncodingException | UnhandledAlertException e) {
e.printStackTrace(); e.printStackTrace();
} }
...@@ -1115,7 +1121,7 @@ public class CarReportUtil { ...@@ -1115,7 +1121,7 @@ public class CarReportUtil {
/** /**
* 获取最新条没上传文件的检测报告 * 获取最新条没上传文件的检测报告
* @param stationCode * @param stationCode
* @param cookies * @param cookies
* @return * @return
...@@ -1140,7 +1146,7 @@ public class CarReportUtil { ...@@ -1140,7 +1146,7 @@ public class CarReportUtil {
MyContext.PHOTO_TABLE_NAME,MyContext.PHOTO_WHERE,fldsVo.getFld(),conditionVoList,null,5); MyContext.PHOTO_TABLE_NAME,MyContext.PHOTO_WHERE,fldsVo.getFld(),conditionVoList,null,5);
JSONObject body = JSONUtil.createObj(); JSONObject body = JSONUtil.createObj();
body.put("data",queryDataVo); body.put("data",queryDataVo);
body.put("rows",5); body.put("rows",10);
body.put("page",1); body.put("page",1);
body.put("sort","DetectEndTime"); body.put("sort","DetectEndTime");
body.put("order","DESC"); body.put("order","DESC");
......
...@@ -18,6 +18,16 @@ public class LoginCacheTest { ...@@ -18,6 +18,16 @@ public class LoginCacheTest {
@Resource(name = "userDetailsService") @Resource(name = "userDetailsService")
private UserDetailsServiceImpl userDetailsService; private UserDetailsServiceImpl userDetailsService;
@Test
public void testIsDirEmptry(){
String dirPath = "C:\\Users\\Lander.LAPTOP-6VMQUJS1\\Desktop\\pdf";
boolean dirEmpty = FileUtil.isDirEmpty(FileUtil.file(dirPath));
System.out.println("当前文件夹是否为空:" + dirEmpty);
if(!dirEmpty){
System.out.println("不为空");
}
}
@Test @Test
public void testCreateFileDirect(){ public void testCreateFileDirect(){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment