Browse Source

update:优化运单pdf

zoie 6 months ago
parent
commit
01c658a8a4
4 changed files with 96 additions and 57 deletions
  1. 45 41
      app/admin/controller/waybill.go
  2. 3 1
      app/admin/model/waybill.go
  3. 46 12
      app/admin/service/waybill.go
  4. 2 3
      common/lib/lib.go

+ 45 - 41
app/admin/controller/waybill.go

@@ -1654,7 +1654,6 @@ func PDF(s service.Waybill, waybill model.Waybill, company model.SysDept, humidi
 				minHumidityTime = data.T_time
 			}
 			totalHumidity += data.T_rh
-
 		}
 
 		// 平均温度
@@ -1681,6 +1680,7 @@ func PDF(s service.Waybill, waybill model.Waybill, company model.SysDept, humidi
 
 	pdf.SetMargins(0, 20, 0, 20)
 	pdf.AddPage()
+	var fileName2 string
 	if len(company.Logo) > 0 {
 		imgH, errImg := lib.GetImage(company.Logo)
 		if errImg != nil {
@@ -1711,7 +1711,7 @@ func PDF(s service.Waybill, waybill model.Waybill, company model.SysDept, humidi
 			err = pdf.Image(fileName, 10, 0, &gopdf.Rect{W: W, H: H})
 			if err != nil {
 				if err.Error() == "16-bit depth not supported" {
-					fileName2 := "./ofile/" + time.Now().Format("20060102150405") + ".png"
+					fileName2 = "./ofile/" + time.Now().Format("20060102150405") + ".png"
 					f2, _ := os.Create(fileName2)
 					defer f.Close()
 					defer func() {
@@ -1829,7 +1829,7 @@ func PDF(s service.Waybill, waybill model.Waybill, company model.SysDept, humidi
 	y += 15
 	pdf.SetXY(10, y)
 	T_remark_temp := []rune(waybill.Remark)
-	if len(waybill.Remark) > 35 {
+	if len(T_remark_temp) > 35 {
 		pdf.Text(fmt.Sprintf("备注:%s", string(T_remark_temp[0:35])))
 		pdf.SetXY(10, y+15)
 		pdf.Text(fmt.Sprintf("%s", string(T_remark_temp[35:])))
@@ -1883,18 +1883,12 @@ func PDF(s service.Waybill, waybill model.Waybill, company model.SysDept, humidi
 	y += 10
 	pdf.SetFont("wts", "", 10)
 
-	var x float64 = 19
-	var w float64 = 112
+	var x float64 = 12
+	var w float64 = 110
 	lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
 	x = x + w
-	w = 37
-	lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
-	x = x + w
-	lib.RectFillColor(pdf, "T2", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
-
-	x = x + w
-	w = 112
-	lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
+	w = 100
+	lib.RectFillColor(pdf, "名称", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
 	x = x + w
 	w = 37
 	lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
@@ -1902,49 +1896,50 @@ func PDF(s service.Waybill, waybill model.Waybill, company model.SysDept, humidi
 	lib.RectFillColor(pdf, "T2", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
 
 	x = x + w
-	w = 112
+	w = 110
 	lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
 	x = x + w
+	w = 100
+	lib.RectFillColor(pdf, "名称", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
+	x = x + w
 	w = 37
 	lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
 	x = x + w
 	lib.RectFillColor(pdf, "T2", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
 
+
 	for i, v := range Pdf_data {
-		if i%3 == 0 {
+		if i%2 == 0 {
 			y += 20
 			var textH float64 = 25 // if text height is 25px.
 			pdf.SetNewY(y, textH)
 			y = pdf.GetY()
-			x, w = 19, 112
+			x, w = 12, 110
 			lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
 			x = x + w
-			w = 37
-			lib.RectFillColor(pdf, lib.Float32_to_string(&v.T_id1), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
-			x = x + w
-			lib.RectFillColor(pdf, lib.Float32_to_string(&v.T_id2), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
-		}
-		if i%3 == 1 {
-			x = x + w
-			w = 112
-			lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
+			w = 100
+			lib.RectFillColor(pdf, v.T_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
 			x = x + w
 			w = 37
-			lib.RectFillColor(pdf, lib.Float32_to_string(&v.T_id1), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
+			lib.RectFillColor(pdf, lib.Float32_to_string_forPDF(v.T_id1), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
 			x = x + w
-			lib.RectFillColor(pdf, lib.Float32_to_string(&v.T_id2), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
+			lib.RectFillColor(pdf, lib.Float32_to_string_forPDF(v.T_id2), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
 		}
-		if i%3 == 2 {
+		if i%2 == 1 {
 			x = x + w
-			w = 112
+			w = 110
 			lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
 			x = x + w
+			w = 100
+			lib.RectFillColor(pdf, v.T_name,12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
+			x = x + w
 			w = 37
-			lib.RectFillColor(pdf, lib.Float32_to_string(&v.T_id1), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
+			lib.RectFillColor(pdf, lib.Float32_to_string_forPDF(v.T_id1), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
 			x = x + w
-			lib.RectFillColor(pdf, lib.Float32_to_string(&v.T_id2), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
+			lib.RectFillColor(pdf, lib.Float32_to_string_forPDF(v.T_id2), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
 		}
 
+
 	}
 	filename = "运单" + waybill.WaybillNo + "温度记录" + time.Now().Format("20060102150405") + ".pdf"
 	filePath = "ofile/" + filename
@@ -1955,6 +1950,8 @@ func PDF(s service.Waybill, waybill model.Waybill, company model.SysDept, humidi
 	}
 	defer func() {
 		os.Remove(tempFilepath)
+		os.Remove(fileName2)
+
 	}()
 	return filename, filePath, nil
 }
@@ -2562,25 +2559,29 @@ func DeviceDataTemperatureJPG3(startTime, endTime string, waybillPDF []service.W
 	})
 	series := make([]chart.Series, 0)
 	// 创建温度线
-	xValues := make([]time.Time, len(deviceDataPdfList))
+	xValues1 := make([]time.Time, len(deviceDataPdfList))
+	xValues2 := make([]time.Time, 0)
 	yValues1 := make([]float64, len(deviceDataPdfList))
 
 	yValues2 := make([]float64, len(deviceDataPdfList))
 	for i := 0; i < len(deviceDataPdfList); i++ {
 		t, _ := lib.TimeStrToTime(deviceDataPdfList[i].T_time)
-		xValues[i] = t
-		yValues1[i] = float64(deviceDataPdfList[i].T_id1)
+		xValues1[i] = t
+		yValues1[i] = float64(*deviceDataPdfList[i].T_id1)
 
-		yValues2[i] = float64(deviceDataPdfList[i].T_id2)
+		if deviceDataPdfList[i].T_id2 != nil {
+			xValues2 = append(xValues2, t)
+			yValues2[i] = float64(*deviceDataPdfList[i].T_id2)
+		}
 	}
 	series = append(series, chart.TimeSeries{
 		Name:    "T1",
-		XValues: xValues,
+		XValues: xValues1,
 		YValues: yValues1,
 	})
 	series = append(series, chart.TimeSeries{
 		Name:    "T2",
-		XValues: xValues,
+		XValues: xValues2,
 		YValues: yValues2,
 	})
 	if ymax < TemperatureMax {
@@ -2939,15 +2940,18 @@ func DeviceDataHumidityJPG3(startTime, endTime string, waybillPDF []service.Wayb
 	xValues1 := make([]time.Time, len(deviceDataPdfList))
 	yValues1 := make([]float64, len(deviceDataPdfList))
 
-	xValues2 := make([]time.Time, len(deviceDataPdfList))
+	xValues2 := make([]time.Time, 0)
 	yValues2 := make([]float64, len(deviceDataPdfList))
 	for i := 0; i < len(deviceDataPdfList); i++ {
 		t, _ := lib.TimeStrToTime(deviceDataPdfList[i].T_time)
 		xValues1[i] = t
-		yValues1[i] = float64(deviceDataPdfList[i].T_id1)
+		yValues1[i] = float64(*deviceDataPdfList[i].T_id1)
+
+		if deviceDataPdfList[i].T_id2 != nil {
+			xValues2 = append(xValues2, t)
+			yValues2[i] = float64(*deviceDataPdfList[i].T_id2)
+		}
 
-		xValues2[i] = t
-		yValues2[i] = float64(deviceDataPdfList[i].T_id2)
 	}
 	series = append(series, chart.TimeSeries{
 		Name:    "T1",

+ 3 - 1
app/admin/model/waybill.go

@@ -59,7 +59,7 @@ type Waybill struct {
 	TemperatureInterval     string      `json:"temperatureInterval"  gorm:"size:128"`     // 温度要求
 	DeliveryCondition       string      `json:"deliveryCondition"  gorm:"size:128"`       // 配送要求
 	Quantity                int         `json:"quantity"  gorm:"size:128"`                // 药品数量
-	Remark                  string      `json:"remark"  gorm:"size:4"`                    // 运输备注
+	Remark                  string      `json:"remark"  gorm:"size:text"`                 // 运输备注
 	CustomerId              int         `json:"customerId" gorm:"size:4"`                 // 下单客户id
 	CustomerName            string      `json:"customerName" gorm:"size:128"`             // 下单客户名称
 	OrderTime               model2.Time `json:"orderTime"  gorm:"size:128"`               // 下单时间
@@ -75,6 +75,8 @@ type Waybill struct {
 	Freight   float64     `json:"freight"  gorm:"size:9"` //运费
 	PrintUser SysUserOmit `json:"printUser" gorm:"->;foreignkey:PrintUserId;references:Id"`
 
+	CoolerBox CoolerBoxOmit `json:"coolerBox" gorm:"->;foreignkey:CoolerBoxId;references:Id"`
+
 	Dept SysDept `json:"dept" gorm:"->"` // 部门
 	model2.ControlBy
 	model2.ModelTime

+ 46 - 12
app/admin/service/waybill.go

@@ -91,6 +91,7 @@ func (e *Waybill) GetPage(c *dto.WaybillGetPageReq, list *[]model.Waybill, count
 			actions.Permission(data.TableName(), p),
 		).
 		Preload("PrintUser").
+		Preload("CoolerBox").
 		Find(list).Limit(-1).Offset(-1).
 		Count(count).Error
 	if err != nil {
@@ -117,6 +118,19 @@ func (e *Waybill) GetAppletPage(c *dto.WaybillGetAppletPageReq, list *[]model.Wa
 		e.Log.Errorf("db error: %s", err)
 		return global.GetFailedErr
 	}
+
+	for i := 0; i < len(*list); i++ {
+		if (*list)[i].CoolerBoxId == 0 {
+			continue
+		}
+		var coolerBox model.CoolerBoxOmit
+		err = e.Orm.First(&coolerBox, (*list)[i].CoolerBoxId).Error
+		if err != nil {
+			e.Log.Errorf("查询保温箱信息失败: %s", err)
+		}
+		(*list)[i].CoolerBox = coolerBox
+	}
+
 	return nil
 }
 
@@ -166,6 +180,7 @@ func (e *Waybill) GetCustomerPage(c *dto.WaybillGetCustomerPageReq, list *[]mode
 			cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
 			actions.Permission(data.TableName(), p),
 		).
+		Preload("CoolerBox").
 		Find(list).Limit(-1).Offset(-1).
 		Count(count).Error
 	if err != nil {
@@ -2013,7 +2028,7 @@ func (e *Waybill) GetAllData(c *dto.WaybillGetByWaybillNoReq) ([]nats_server.Dev
 				for j := 0; j < len(list); j++ {
 					if v.T_id == list[j].T_id {
 						if list[j].T_time != taskList[i].StartTime.String() {
-							firstData := list[j]
+							firstData := list[len(list)-1-j]
 							firstData.T_time = taskList[i].StartTime.String()
 							firstMap[v.T_id] = firstData
 							count += 1
@@ -2025,7 +2040,7 @@ func (e *Waybill) GetAllData(c *dto.WaybillGetByWaybillNoReq) ([]nats_server.Dev
 					for k := len(list) - 1; k >= 0; k-- {
 						if v.T_id == list[k].T_id {
 							if taskList[i].Id == lastWaybillTask.Id && list[k].T_time != taskList[i].EndTime.String() && !time.Time(taskList[i].EndTime).IsZero() {
-								lastData := list[k]
+								lastData := list[len(list)-1-k]
 								lastData.T_time = taskList[i].EndTime.String()
 								lastMap[v.T_id] = lastData
 								count += 1
@@ -2103,6 +2118,7 @@ func (e *Waybill) GetTwoDeviceSensorData(c *dto.WaybillGetByWaybillNoReq) ([]nat
 	err = e.Orm.Model(&data).
 		Where("waybill_no = ?", c.WaybillNo).
 		Order("id desc").
+		Preload("Warehouse").Preload("Car").Preload("CoolerBox").
 		Find(&taskList).Error
 	if err != nil {
 		e.Log.Errorf("db error: %s", err)
@@ -2153,7 +2169,7 @@ func (e *Waybill) GetTwoDeviceSensorData(c *dto.WaybillGetByWaybillNoReq) ([]nat
 					if v.T_id == list[j].T_id {
 						if list[j].T_time != taskList[i].StartTime.String() {
 							if i < len(taskList)-2 && list[j].T_time != taskList[i+1].EndTime.String() {
-								firstData := list[j]
+								firstData := list[len(list)-1-j]
 								firstData.T_time = taskList[i].StartTime.String()
 								firstMap[v.T_id] = firstData
 								count += 1
@@ -2166,7 +2182,7 @@ func (e *Waybill) GetTwoDeviceSensorData(c *dto.WaybillGetByWaybillNoReq) ([]nat
 					for k := len(list) - 1; k >= 0; k-- {
 						if v.T_id == list[k].T_id {
 							if taskList[i].Id == lastWaybillTask.Id && list[k].T_time != taskList[i].EndTime.String() && !time.Time(taskList[i].EndTime).IsZero() {
-								lastData := list[k]
+								lastData := list[len(list)-1-k]
 								lastData.T_time = taskList[i].EndTime.String()
 								lastMap[v.T_id] = lastData
 								count += 1
@@ -2184,7 +2200,19 @@ func (e *Waybill) GetTwoDeviceSensorData(c *dto.WaybillGetByWaybillNoReq) ([]nat
 		for _, v := range lastMap {
 			list = append(list, v)
 		}
-		deviceDataPdfList := DeviceSensorDataListToDeviceDataPdfList(list, t_id_list[0], t_id_list[1])
+
+		name := ""
+		if taskList[i].WarehouseId > 0 {
+			name = taskList[i].Warehouse.Name
+		}
+		if taskList[i].CarId > 0 {
+			name = taskList[i].Car.CarNo
+		}
+		if taskList[i].CoolerBoxId > 0 {
+			name = taskList[i].CoolerBox.Name
+		}
+
+		deviceDataPdfList := DeviceSensorDataListToDeviceDataPdfList(name, list, t_id_list[0], t_id_list[1])
 		deviceDataPdf = append(deviceDataPdf, deviceDataPdfList...)
 		dataList = append(dataList, list...)
 		waybillPDF = append(waybillPDF, WaybillPDF{
@@ -2218,26 +2246,32 @@ type WaybillPDF struct {
 }
 
 type DeviceDataPdf struct {
-	T_time string  // sn
-	T_id1  float32 // 传感器id1温度
-	T_id2  float32 // 传感器id2温度
+	T_name string   // 设备名称
+	T_time string   // 时间
+	T_id1  *float32 // 传感器id1温度
+	T_id2  *float32 // 传感器id2温度
 }
 
-func DeviceSensorDataListToDeviceDataPdfList(list []nats_server.DeviceData_R, T_id1 int, T_id2 int) []DeviceDataPdf {
+func DeviceSensorDataListToDeviceDataPdfList(T_name string, list []nats_server.DeviceData_R, T_id1 int, T_id2 int) []DeviceDataPdf {
 	// 创建一个 map 用于存储 DeviceData_Pdf 结果
 	resultMap := make(map[string]DeviceDataPdf)
 
 	// 遍历第一个列表,将数据存入 resultMap
 	for _, item := range list {
+		var pdf DeviceDataPdf
 		key := item.T_time
 		if _, ok := resultMap[key]; !ok {
-			resultMap[key] = DeviceDataPdf{T_time: item.T_time}
+			pdf.T_time = item.T_time
+			pdf.T_name = T_name
+			resultMap[key] = pdf
 		}
 		data := resultMap[key]
 		if item.T_id == T_id1 {
-			data.T_id1 = item.T_t
+			t1 := item.T_t
+			data.T_id1 = &t1
 		} else if item.T_id == T_id2 {
-			data.T_id2 = item.T_t
+			t2 := item.T_t
+			data.T_id2 = &t2
 		}
 		resultMap[key] = data
 	}

+ 2 - 3
common/lib/lib.go

@@ -330,10 +330,9 @@ func To_string(value interface{}) string {
 	return key
 }
 
-func Float32_to_string(value *float32) string {
-	var key string
+func Float32_to_string_forPDF(value *float32) string {
 	if value == nil {
-		return key
+		return "-"
 	}
 	return fmt.Sprintf("%.1f", *value)
 }