Преглед изворни кода

add:导出pdf添加报告专用章

zoie пре 3 месеци
родитељ
комит
d6d260e998
3 измењених фајлова са 147 додато и 52 уклоњено
  1. 145 50
      controllers/TaskData.go
  2. 2 2
      lib/Qiniu.go
  3. BIN
      static/commonSeal.jpg

+ 145 - 50
controllers/TaskData.go

@@ -15,6 +15,7 @@ import (
 	"github.com/signintech/gopdf"
 	"github.com/vmihailenco/msgpack/v5"
 	"github.com/xuri/excelize/v2"
+	"log"
 	"math"
 	"os"
 	"sort"
@@ -991,25 +992,13 @@ func (c *TaskDataController) TaskData_Pdf(Time_start, Time_end, T_task_id, T_sn,
 	dataList, _ := Task.Read_TaskData_ById_List(Task_r.T_task_id, T_sn, T_id, Time_start, Time_end, 0, 9999)
 	dataListMap := make(map[string][]Task.TaskData_)
 	dcListMap := make(map[string]string)
+	dataMap := make(map[string]string)
+	timeMap := make(map[string]bool)
 	for _, data := range dataList {
 		dataListMap[data.T_id] = append(dataListMap[data.T_id], data)
 		if _, ok := dcListMap[data.T_id]; !ok {
 			dcListMap[data.T_id] = data.T_sn
 		}
-	}
-	for id, sn := range dcListMap {
-		dcList = append(dcList, Device.DeviceClassList{T_id: id, T_sn: sn})
-	}
-	for id, list := range dataListMap {
-		sort.Slice(list, func(i, j int) bool {
-			return list[i].T_time < list[j].T_time
-		})
-		dataListMap[id] = list
-	}
-
-	dataMap := make(map[string]string)
-	timeMap := make(map[string]bool)
-	for _, data := range dataList {
 		k := fmt.Sprintf("%s,%s", data.T_time, data.T_id)
 		if T_type == Task.Temperature {
 			dataMap[k] = fmt.Sprintf("%.1f", data.T_t)
@@ -1021,6 +1010,15 @@ func (c *TaskDataController) TaskData_Pdf(Time_start, Time_end, T_task_id, T_sn,
 			timeMap[data.T_time] = true
 		}
 	}
+	for id, sn := range dcListMap {
+		dcList = append(dcList, Device.DeviceClassList{T_id: id, T_sn: sn})
+	}
+	for id, list := range dataListMap {
+		sort.Slice(list, func(i, j int) bool {
+			return list[i].T_time < list[j].T_time
+		})
+		dataListMap[id] = list
+	}
 
 	var timeList []string
 	for k, _ := range timeMap {
@@ -1042,7 +1040,7 @@ func (c *TaskDataController) TaskData_Pdf(Time_start, Time_end, T_task_id, T_sn,
 		pdf.Text(timeStr)
 	}
 
-	y += 20
+	//y += 20
 	var x float64 = 10
 	var w float64 = 120
 
@@ -1066,7 +1064,28 @@ func (c *TaskDataController) TaskData_Pdf(Time_start, Time_end, T_task_id, T_sn,
 	sort.Slice(dcList1, func(i, j int) bool {
 		return dcList1[i].T_id < dcList1[j].T_id
 	})
-	for _, dc := range dcList1 {
+	dcList2 := listSubtract(dcList, dcList1)
+	// 获取探头时间
+	var timeList2 []string
+	timeMap2 := make(map[string]struct{})
+	for _, list := range dcList2 {
+		dataList2, ok := dataListMap[list.T_id]
+		if ok {
+			for _, data := range dataList2 {
+				if _, ok = timeMap2[data.T_time]; !ok {
+					timeMap2[data.T_time] = struct{}{}
+				}
+			}
+		}
+	}
+	for k, _ := range timeMap2 {
+		timeList2 = append(timeList2, k)
+	}
+	sort.Slice(timeList2, func(i, j int) bool {
+		return timeList2[i] < timeList2[j]
+	})
+	var temp int
+	for index, dc := range dcList1 {
 
 		dataList2, _ := dataListMap[dc.T_id]
 
@@ -1074,13 +1093,17 @@ func (c *TaskDataController) TaskData_Pdf(Time_start, Time_end, T_task_id, T_sn,
 		if err != nil {
 			return
 		}
+		y += 30
+
 		var textH float64 = 20 // if text height is 25px.
-		y += 20
-		pdf.SetNewY(y, textH)
-		y = pdf.GetY()
 		if y > 790 {
+			addStampImage(pdf, 455, y-150)
 			pdf.AddPage()
 			y = 30
+		} else {
+			if index > 0 {
+				y += 10
+			}
 		}
 
 		textw, _ = pdf.MeasureTextWidth(dc.T_id)
@@ -1093,8 +1116,7 @@ func (c *TaskDataController) TaskData_Pdf(Time_start, Time_end, T_task_id, T_sn,
 		if err != nil {
 			return
 		}
-		pdf.SetNewY(y, textH)
-		y = pdf.GetY()
+
 		if y < 790 {
 			x = 10
 			for i := 0; i < 4; i++ {
@@ -1107,6 +1129,7 @@ func (c *TaskDataController) TaskData_Pdf(Time_start, Time_end, T_task_id, T_sn,
 			}
 			y += 20
 		} else {
+			addStampImage(pdf, 455, y-150)
 			pdf.AddPage()
 			y = 20
 			x = 10
@@ -1120,8 +1143,39 @@ func (c *TaskDataController) TaskData_Pdf(Time_start, Time_end, T_task_id, T_sn,
 			}
 			y += 20
 		}
+
 		for i := 0; i < len(dataList2); i++ {
-			temp := i % 4
+			if y > 790 {
+				addStampImage(pdf, 455, y-130)
+				pdf.AddPage()
+				y = pdf.GetY()
+				x = 10
+				for k := 0; k < 4; k++ {
+					w = 113.8
+					lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
+					x += w
+					w = 30
+					lib.RectFillColor(pdf, T_type_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
+					x += w
+				}
+				y += 20
+			}
+
+			temp = i % 4
+			// 每页添加表头
+			if y == 20 && temp == 0 {
+				x = 10
+				for k := 0; k < 4; k++ {
+					w = 113.8
+					lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
+					x += w
+					w = 30
+					lib.RectFillColor(pdf, T_type_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
+					x += w
+				}
+				y += 20
+			}
+
 			x = 10 + 143.8*float64(temp)
 			w = 113.8
 
@@ -1134,40 +1188,37 @@ func (c *TaskDataController) TaskData_Pdf(Time_start, Time_end, T_task_id, T_sn,
 			if T_type == Task.Humidity {
 				lib.RectFillColor(pdf, fmt.Sprintf("%.1f", dataList2[i].T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
 			}
+			//if y > 760 {
+			//	fmt.Println("=====y:", y)
+			//}
+			//if y > 790 {
+			//	addStampImage(pdf, 455, y-130)
+			//}
 			if temp == 3 {
 				y += 20
 				pdf.SetNewY(y, textH)
 				y = pdf.GetY()
-				// 每页添加表头
-				if y == 20 {
-					x = 10
-					for k := 0; k < 4; k++ {
-						w = 113.8
-						lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
-						x += w
-						w = 30
-						lib.RectFillColor(pdf, T_type_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
-						x += w
-					}
-					y += 20
-				}
 			}
+
 		}
-		y += 25
 
 	}
 
-	//chunks := chunkBy(dcList, 15)
-	y += 20
-	dcList2 := listSubtract(dcList, dcList1)
+	if len(dcList2) > 0 {
+		if temp != 3 {
+			y += 20
+		}
+		y += 40
+	}
 	sort.Slice(dcList2, func(i, j int) bool {
 		return dcList2[i].T_id < dcList2[j].T_id
 	})
 	chunks := splitData(dcList2, 454.5, idWidthMap)
-	for _, list := range chunks {
-		var textH float64 = 20 // if text height is 25px.
-		pdf.SetNewY(y, textH)
-		y = pdf.GetY()
+	for i, list := range chunks {
+
+		if i > 0 {
+			y += 40
+		}
 		if y < 790 {
 			x = 10
 			w = 120.7
@@ -1181,15 +1232,34 @@ func (c *TaskDataController) TaskData_Pdf(Time_start, Time_end, T_task_id, T_sn,
 			}
 			y += 20
 		} else {
+			addStampImage(pdf, 455, y-150)
 			pdf.AddPage()
 			y = 20
 		}
 
-		for _, t := range timeList {
+		var StampImageX float64
+		for j, t := range timeList2 {
+			if j > 0 {
+				y += 20
+			}
 			x = 10
-			textH = 20 // if text height is 25px.
-			pdf.SetNewY(y, textH)
-			y = pdf.GetY()
+			if y > 790 {
+				addStampImage(pdf, StampImageX-130, y-130)
+				pdf.AddPage()
+				y = pdf.GetY()
+				x = 10
+				w = 120.7
+				lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
+				x += w
+				w = 30.3
+				for _, v2 := range list {
+					w = idWidthMap[v2.T_id]
+					lib.RectFillColor(pdf, v2.T_id, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
+					x += w
+				}
+				y += 20
+				x = 10
+			}
 			if y == 20 {
 				w = 120.7
 				lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
@@ -1213,10 +1283,29 @@ func (c *TaskDataController) TaskData_Pdf(Time_start, Time_end, T_task_id, T_sn,
 				lib.RectFillColor(pdf, t_t, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
 				x += w
 			}
-			y += 20
+			StampImageX = x
+		}
+	}
 
+	if y <= 790 {
+		if y < 150 {
+			y = 20
+		} else {
+			y = y - 145
 		}
-		y += 20
+		x = x - 135
+		if len(dcList2) == 0 {
+			x = 455
+		}
+		addStampImage(pdf, x, y)
+	} else if y > 790 && len(dcList2) == 0 {
+		if y < 150 {
+			y = 20
+		} else {
+			y = y - 145
+		}
+		addStampImage(pdf, 455, y)
+
 	}
 	filename := time.Now().Format("20060102150405") + ".pdf"
 	timeStr := "ofile/" + T_type_name + filename
@@ -1248,7 +1337,13 @@ func (c *TaskDataController) TaskData_Pdf(Time_start, Time_end, T_task_id, T_sn,
 	return nil
 
 }
-
+func addStampImage(pdf *gopdf.GoPdf, x, y float64) {
+	imagePath := "./static/commonSeal.jpg"
+	err := pdf.Image(imagePath, x, y, &gopdf.Rect{W: 123, H: 128.6})
+	if err != nil {
+		log.Print(err.Error())
+	}
+}
 func listSubtract(sliceA, sliceB []Device.DeviceClassList) []Device.DeviceClassList {
 	// 创建一个集合用于存储 sliceB 的元素
 	elementsToRemove := make(map[string]Device.DeviceClassList)

+ 2 - 2
lib/Qiniu.go

@@ -86,8 +86,8 @@ func UploadToken(T_suffix string) string {
 		//{"key":"` + conf.Oss + `/$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}
 		ForceSaveKey: true,
 		SaveKey:      "UpImage/" + Tokey + "." + T_suffix,
-		//FsizeLimit:   1024 * 1024 * 1,
-		MimeLimit: "image/*;application/pdf",
+		FsizeLimit:   1024 * 1024 * 30,
+		MimeLimit:    "image/*;application/pdf",
 	}
 
 	upToken := putPolicy.UploadToken(Qiniu)

BIN
static/commonSeal.jpg