Browse Source

ADD:优化数据展示导出excel、pdf 排版

zoie 11 months ago
parent
commit
43c7cf2e49
3 changed files with 37 additions and 6 deletions
  1. 15 5
      controllers/Data.go
  2. 1 1
      models/Device/DeviceData.go
  3. 21 0
      models/Device/DeviceSensor.go

+ 15 - 5
controllers/Data.go

@@ -4,12 +4,12 @@ import (
 	"Cold_Api/Nats/NatsServer"
 	"Cold_Api/conf"
 	"Cold_Api/controllers/lib"
-	"github.com/beego/beego/v2/core/logs"
 	"Cold_Api/models/Account"
 	"Cold_Api/models/Device"
 	"encoding/base64"
 	"errors"
 	"fmt"
+	"github.com/beego/beego/v2/core/logs"
 	beego "github.com/beego/beego/v2/server/web"
 	"github.com/signintech/gopdf"
 	"github.com/xuri/excelize/v2"
@@ -329,8 +329,13 @@ func (c *DataController) Device_Sensor_Data_Excel() {
 	}
 	var DeviceSensor_data []Device.DeviceData_R
 
-	DeviceSensor_data, _ = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, 0, 9999)
-
+	//DeviceSensor_data, _ = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, 0, 9999)
+	dsList := Device.Read_DeviceSensorList_By_T_snid(T_snid)
+	for _, v := range dsList {
+		snid := fmt.Sprintf("%s,%d", v.T_sn, v.T_id)
+		data, _ := Device.Read_DeviceData_By_T_snid_List(snid, Time_start, Time_end, 0, 9999)
+		DeviceSensor_data = append(DeviceSensor_data, data...)
+	}
 	f := excelize.NewFile() // 设置单元格的值
 	// 这里设置表头
 	f.SetCellValue("Sheet1", "A1", "编号")
@@ -622,8 +627,13 @@ func (c *DataController) Device_Sensor_Data_PDF() {
 	}
 	var DeviceSensor_data []Device.DeviceData_R
 
-	DeviceSensor_data, _ = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, 0, 9999)
-
+	//DeviceSensor_data, _ = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, 0, 9999)
+	dsList := Device.Read_DeviceSensorList_By_T_snid(T_snid)
+	for _, v := range dsList {
+		snid := fmt.Sprintf("%s,%d", v.T_sn, v.T_id)
+		data, _ := Device.Read_DeviceData_By_T_snid_List(snid, Time_start, Time_end, 0, 9999)
+		DeviceSensor_data = append(DeviceSensor_data, data...)
+	}
 	var err error
 	pdf := &gopdf.GoPdf{}
 	pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4

+ 1 - 1
models/Device/DeviceData.go

@@ -448,7 +448,7 @@ func Read_DeviceData_ByIds(SN string, ids []int, Time_start_ string, Time_end_ s
 	}
 	//fmt.Println("maps_z;",maps_z[0][0])
 	//t_tl,t_tu,t_rhl,t_rhu,
-	sql = "SELECT t_id,t_sp,t_t,t_rh,t_site,DATE_FORMAT(t_time,'%Y-%m-%d %H:%i:%s') AS t_time,t_time AS t_time1 FROM z_device_data_" + SN + " WHERE " + sql_time + " t_id in (" + lib.IntListToDotStr(ids) + ") ORDER BY t_time1,t_id DESC "
+	sql = "SELECT t_id,t_sp,t_t,t_rh,t_site,DATE_FORMAT(t_time,'%Y-%m-%d %H:%i:%s') AS t_time,t_time AS t_time1 FROM z_device_data_" + SN + " WHERE " + sql_time + " t_id in (" + lib.IntListToDotStr(ids) + ") ORDER BY t_time1 DESC,t_id ASC "
 	if page_z != 9999 {
 		sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
 	}

+ 21 - 0
models/Device/DeviceSensor.go

@@ -1737,3 +1737,24 @@ func Read_DeviceSensorList_BySN(T_sn string) (DeviceSensor_r []DeviceSensor_R, c
 	}
 	return DeviceSensor_r, cnt
 }
+
+func Read_DeviceSensorList_By_T_snid(T_snid string) []DeviceSensor {
+	T_snid_list := strings.Split(strings.Trim(T_snid, "|"), "|")
+	var maps []DeviceSensor
+	ids := []int{}
+	for _, v := range T_snid_list {
+		sn_id := strings.Split(v, ",")
+
+		if len(sn_id) == 2 {
+			ds, _ := Read_DeviceSensor_ByTsn_Tid(sn_id[0], lib.To_int(sn_id[1]))
+			ids = append(ids, ds.Id)
+		}
+	}
+	o := orm.NewOrm()
+	cond := orm.NewCondition()
+	cond1 := cond.And("id__in", ids)
+	qs := o.QueryTable(new(DeviceSensor))
+	qs.SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&maps)
+
+	return maps
+}