zwxi 9 月之前
父節點
當前提交
3dd9f26e1a
共有 4 個文件被更改,包括 74 次插入12 次删除
  1. 8 0
      models/english_report.go
  2. 9 0
      models/report.go
  3. 9 0
      models/smart_report/smart_report.go
  4. 48 12
      services/smart_report.go

+ 8 - 0
models/english_report.go

@@ -929,3 +929,11 @@ func ModifyEnglishReportImgUrl(reportId int, detailImgUrl string) (err error) {
 	_, err = o.Raw(sql, detailImgUrl, reportId).Exec()
 	return
 }
+
+// UpdatePdfUrlEnglishReportById 清空pdf相关字段
+func UpdatePdfUrlEnglishReportById(reportId int) (err error) {
+	o := orm.NewOrmUsingDB("rddp")
+	sql := `UPDATE english_report SET detail_img_url = '',detail_pdf_url='',modify_time=NOW() WHERE id = ? `
+	_, err = o.Raw(sql, reportId).Exec()
+	return
+}

+ 9 - 0
models/report.go

@@ -1172,4 +1172,13 @@ func ModifyReportImgUrl(reportId int, detailImgUrl string) (err error) {
 	sql := `UPDATE report SET detail_img_url=? WHERE id=? `
 	_, err = o.Raw(sql, detailImgUrl, reportId).Exec()
 	return
+}
+
+
+// UpdatePdfUrlReportById 清空pdf相关字段
+func UpdatePdfUrlReportById(reportId int) (err error) {
+	o := orm.NewOrmUsingDB("rddp")
+	sql := `UPDATE report SET detail_img_url = '',detail_pdf_url='',modify_time=NOW() WHERE id = ? `
+	_, err = o.Raw(sql, reportId).Exec()
+	return
 }

+ 9 - 0
models/smart_report/smart_report.go

@@ -418,3 +418,12 @@ func UpdateSmartReportsStateBySecondIds(oldState, newState int, secondIds []int)
 	_, err = o.Raw(sql, oldState, newState, secondIds).Exec()
 	return
 }
+
+
+// UpdatePdfUrlSmartReportById 清空pdf相关字段
+func UpdatePdfUrlSmartReportById(reportId int) (err error) {
+	o := orm.NewOrmUsingDB("rddp")
+	sql := `UPDATE smart_report SET detail_img_url = '',detail_pdf_url='',modify_time=NOW() WHERE smart_report_id = ? `
+	_, err = o.Raw(sql, reportId).Exec()
+	return
+}

+ 48 - 12
services/smart_report.go

@@ -182,10 +182,13 @@ finally:
 	pyCode = fmt.Sprintf(pyCode, utils.ChromePath, reportUrl, filePath)
 	utils.FileLog.Info("pdf pyCode: \n" + pyCode)
 	cmd := exec.Command("python3", "-c", pyCode)
-	_, err = cmd.CombinedOutput()
-	if err != nil {
+	output, e := cmd.CombinedOutput()
+	if e != nil {
+		err = e
 		utils.FileLog.Info("ReportToPdf failed: , error: \n" + err.Error())
+		utils.FileLog.Info("Output: %s\n", string(output))
 		go alarm_msg.SendAlarmMsg("ReportToPdf failed:"+err.Error(), 3)
+		go alarm_msg.SendAlarmMsg("Output :"+string(output), 3)
 	}
 	defer func() {
 		cmd.Process.Kill()
@@ -193,7 +196,7 @@ finally:
 	return
 }
 
-func ReportToJpeg(reportUrl, filePath string) (err error) {
+func ReportToJpeg(width int, reportUrl, filePath string) (err error) {
 	pyCode := `
 import asyncio
 from pyppeteer import launch, errors
@@ -212,7 +215,7 @@ async def main():
         
         # 设置视口大小
         await page.setViewport({
-            'width': 1920,
+            'width': %d,
             'height': 1080
         })
         
@@ -221,11 +224,15 @@ async def main():
             'waitUntil': 'networkidle0',
             'timeout': 1000000  # 设置超时时间为 100 秒
         })
-        
-        # 截取全页面的屏幕截图
+        # Customizing footer for page numbers starting from page 2
+
+        # 在这里添加两秒的等待
+        await asyncio.sleep(2)
+
         await page.screenshot({
             'path': "%s",
             'fullPage': True,
+			'quality':100
         })
         
     except errors.BrowserError as e:
@@ -250,14 +257,17 @@ finally:
     loop.close()
 `
 
-	pyCode = fmt.Sprintf(pyCode, utils.ChromePath, reportUrl, filePath)
+	pyCode = fmt.Sprintf(pyCode, utils.ChromePath, width, reportUrl, filePath)
 	utils.FileLog.Info("jpeg pyCode: \n" + pyCode)
 	cmd := exec.Command("python3", "-c", pyCode)
 
-	_, err = cmd.CombinedOutput()
-	if err != nil {
+	output, e := cmd.CombinedOutput()
+	if e != nil {
+		err = e
 		utils.FileLog.Info("ReportToJpeg failed: , error: \n" + err.Error())
+		utils.FileLog.Info("Output: %s\n", string(output))
 		go alarm_msg.SendAlarmMsg("ReportToJpeg failed:"+err.Error(), 3)
+		go alarm_msg.SendAlarmMsg("Output :"+string(output), 3)
 	}
 	defer func() {
 		cmd.Process.Kill()
@@ -265,7 +275,7 @@ finally:
 	return
 }
 
-func Report2pdfAndJpeg(reportUrl string, reportId,reportType int) {
+func Report2pdfAndJpeg(reportUrl string, reportId, reportType int) {
 	var err error
 
 	defer func() {
@@ -274,6 +284,28 @@ func Report2pdfAndJpeg(reportUrl string, reportId,reportType int) {
 			utils.FileLog.Info("Report2pdfAndJpeg failed: , error: \n" + err.Error())
 		}
 	}()
+
+	// 先清空字段
+	if reportType == 1 {
+		err = models.UpdatePdfUrlReportById(reportId)
+		if err != nil {
+			utils.FileLog.Info("清空pdf长图字段失败, Err: \n" + err.Error())
+			return
+		}
+	} else if reportType == 2 {
+		err = models.UpdatePdfUrlEnglishReportById(reportId)
+		if err != nil {
+			utils.FileLog.Info("清空pdf长图字段失败, Err: \n" + err.Error())
+			return
+		}
+	} else if reportType == 3 {
+		err = smart_report.UpdatePdfUrlSmartReportById(reportId)
+		if err != nil {
+			utils.FileLog.Info("清空pdf长图字段失败, Err: \n" + err.Error())
+			return
+		}
+	}
+
 	reportCode := utils.MD5(strconv.Itoa(reportId))
 
 	pdfPath := `./static/` + reportCode + ".pdf"
@@ -341,7 +373,11 @@ func Report2pdfAndJpeg(reportUrl string, reportId,reportType int) {
 	}()
 
 	go func() {
-		err := ReportToJpeg(reportUrl, jpegPath)
+		width := 1200
+		if reportType == 3 {
+			width = 800
+		}
+		err := ReportToJpeg(width, reportUrl, jpegPath)
 		if err != nil {
 			utils.FileLog.Info("ReportToJpeg failed: , error: \n" + err.Error())
 		}
@@ -396,4 +432,4 @@ func Report2pdfAndJpeg(reportUrl string, reportId,reportType int) {
 		}
 
 	}()
-}
+}