Переглянути джерело

ADD:添加温湿度验证数据

zoie 4 місяців тому
батько
коміт
bfdf2ad393
3 змінених файлів з 293 додано та 16 видалено
  1. 82 0
      Nats/NatsServer/NatsServer.go
  2. 209 15
      controllers/TaskData.go
  3. 2 1
      routers/TaskData.go

+ 82 - 0
Nats/NatsServer/NatsServer.go

@@ -151,3 +151,85 @@ func Device_Class_List(T_task_id string) (list []Device.DeviceClassList, err err
 	return t_R.Data, nil
 
 }
+func Add_DeviceClassList(T_task_id, T_sn, T_id string) (err error) {
+	logs.Debug("Nats =>", lib.FuncName(), T_task_id, T_sn, T_id)
+	type T_R struct {
+		T_task_id string `xml:"T_task_id"` // 任务主键id
+		T_sn      string `xml:"T_sn"`
+		T_id      string `xml:"T_id"`
+	}
+
+	t_R := T_R{
+		T_task_id: T_task_id,
+		T_sn:      T_sn,
+		T_id:      T_id,
+	}
+
+	b, err := msgpack.Marshal(&t_R)
+	if err != nil {
+		return err
+	}
+
+	msg, err := lib.Nats.Request("ColdVerify_Server_Add_DeviceClassList", b, 3*time.Second)
+	if err != nil {
+		return err
+	}
+
+	type T_Res struct {
+		Code int16  `xml:"Code"`
+		Msg  string `xml:"Msg"`
+	}
+	var t_Res T_Res
+
+	err = msgpack.Unmarshal(msg.Data, &t_Res)
+	if err != nil {
+		return err
+	}
+	if t_Res.Code != 200 {
+		return errors.New(t_Res.Msg)
+	}
+
+	return nil
+
+}
+func Edit_DeviceClassList(T_task_id, T_sn, T_id string) (err error) {
+	logs.Debug("Nats =>", lib.FuncName(), T_task_id, T_sn, T_id)
+	type T_R struct {
+		T_task_id string `xml:"T_task_id"` // 任务主键id
+		T_sn      string `xml:"T_sn"`
+		T_id      string `xml:"T_id"`
+	}
+
+	t_R := T_R{
+		T_task_id: T_task_id,
+		T_sn:      T_sn,
+		T_id:      T_id,
+	}
+
+	b, err := msgpack.Marshal(&t_R)
+	if err != nil {
+		return err
+	}
+
+	msg, err := lib.Nats.Request("ColdVerify_Server_Edit_DeviceClassList", b, 3*time.Second)
+	if err != nil {
+		return err
+	}
+
+	type T_Res struct {
+		Code int16  `xml:"Code"`
+		Msg  string `xml:"Msg"`
+	}
+	var t_Res T_Res
+
+	err = msgpack.Unmarshal(msg.Data, &t_Res)
+	if err != nil {
+		return err
+	}
+	if t_Res.Code != 200 {
+		return errors.New(t_Res.Msg)
+	}
+
+	return nil
+
+}

+ 209 - 15
controllers/TaskData.go

@@ -91,7 +91,7 @@ func (c *TaskDataController) TaskData_List() {
 	c.ServeJSON()
 	return
 }
-func (c *TaskDataController) TaskData_Pdf() {
+func (c *TaskDataController) TaskData_Temperature_Pdf() {
 
 	Time_start := c.GetString("Time_start")
 	Time_end := c.GetString("Time_end")
@@ -171,7 +171,7 @@ func (c *TaskDataController) TaskData_Pdf() {
 	pdf.Text(name)
 
 	y += 20
-	title := Task_r.T_name + "验证数据"
+	title := Task_r.T_name + "温度验证数据"
 	textw, _ = pdf.MeasureTextWidth(title)
 	pdf.SetX((595 / 2) - (textw / 2))
 	pdf.SetY(y)
@@ -246,7 +246,179 @@ func (c *TaskDataController) TaskData_Pdf() {
 		y += 20
 	}
 	filename := time.Now().Format("20060102150405") + ".pdf"
-	timeStr := "ofile/" + filename
+	timeStr := "ofile/温度" + filename
+
+	err = pdf.WritePdf(timeStr)
+	if err != nil {
+		c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
+		c.ServeJSON()
+		return
+	}
+
+	defer func() {
+		//删除目录
+		os.Remove(timeStr)
+	}()
+
+	c.Ctx.Output.Download(timeStr)
+
+}
+func (c *TaskDataController) TaskData_Humidity_Pdf() {
+
+	Time_start := c.GetString("Time_start")
+	Time_end := c.GetString("Time_end")
+	T_snid := c.GetString("T_snid")
+
+	T_task_id := c.GetString("T_task_id")
+	Task_r, err := NatsServer.Read_Task(T_task_id)
+	if err != nil {
+		c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
+		c.ServeJSON()
+		return
+	}
+	if Task_r.T_collection_state == 2 {
+		c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
+		c.ServeJSON()
+		return
+	}
+	user, err := NatsServer.Read_User(Task_r.T_uuid)
+	var T_snid_list []string
+	if len(T_snid) > 0 {
+		T_snid_list = strings.Split(strings.Trim(T_snid, "|"), "|")
+	}
+
+	dataMap := make(map[string]string)
+	timeMap := make(map[string]bool)
+	//classList := Task.Read_TaskData_ById_ClassList(Task_r.T_task_id)
+	//for _, i2 := range classList {
+	//	T_snid_list = append(T_snid_list, fmt.Sprintf("%s,%s", i2.T_sn, i2.T_id))
+	//}
+	for _, v := range T_snid_list {
+		sn_id := strings.Split(v, ",")
+		if len(sn_id) == 2 {
+			List, _ := Task.Read_TaskData_ById_List(Task_r.T_task_id, sn_id[0], sn_id[1], Time_start, Time_end, 0, 9999)
+
+			for _, data := range List {
+				k := fmt.Sprintf("%s,%s", data.T_time, data.T_id)
+				dataMap[k] = fmt.Sprintf("%.1f", data.T_rh)
+				if _, ok := timeMap[data.T_time]; !ok {
+					timeMap[data.T_time] = true
+				}
+			}
+		}
+	}
+
+	var timeList []string
+	for k, _ := range timeMap {
+		timeList = append(timeList, k)
+	}
+	sort.Slice(timeList, func(i, j int) bool {
+		return timeList[i] < timeList[j]
+	})
+
+	pdf := &gopdf.GoPdf{}
+	pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
+
+	err = pdf.AddTTFFont("wts", "static/fonts/MiSans-Medium.ttf")
+	if err != nil {
+		return
+	}
+	err = pdf.SetFont("wts", "", 15)
+	if err != nil {
+		return
+	}
+
+	pdf.SetGrayFill(0.5)
+
+	pdf.SetMargins(0, 20, 0, 20)
+	pdf.AddPage()
+	imgH, _ := gopdf.ImageHolderByPath("./ofile/logo.jpg")
+	err = pdf.ImageByHolder(imgH, 10, 10, &gopdf.Rect{W: 93, H: 32})
+	name := user.T_name
+
+	var y float64 = 40
+	textw, _ := pdf.MeasureTextWidth(name)
+	pdf.SetX((595 / 2) - (textw / 2))
+	pdf.SetY(y)
+	pdf.Text(name)
+
+	y += 20
+	title := Task_r.T_name + "湿度验证数据"
+	textw, _ = pdf.MeasureTextWidth(title)
+	pdf.SetX((595 / 2) - (textw / 2))
+	pdf.SetY(y)
+	pdf.Text(title)
+
+	if len(timeList) > 0 {
+		y += 20
+		timeStr := fmt.Sprintf("%s - %s", timeList[0], timeList[len(timeList)-1])
+		textw, _ = pdf.MeasureTextWidth(timeStr)
+		pdf.SetX((595 / 2) - (textw / 2))
+		pdf.SetY(y)
+		pdf.Text(timeStr)
+	}
+
+	err = pdf.SetFont("wts", "", 10)
+	if err != nil {
+		return
+	}
+
+	y += 20
+	var x float64 = 10
+	var w float64 = 120
+
+	chunks := chunkBy(T_snid_list, 15)
+	for _, list := range chunks {
+		x = 10
+		w = 120
+		lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
+		x += w
+		w = 30
+		for _, v2 := range list {
+			sn_id2 := strings.Split(v2, ",")
+			id2 := sn_id2[1]
+			lib.RectFillColor(pdf, id2, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
+			x += w
+		}
+		y += 20
+
+		for _, t := range timeList {
+			x = 10
+			var textH float64 = 20 // if text height is 25px.
+			pdf.SetNewY(y, textH)
+			y = pdf.GetY()
+			if y == 20 {
+				w = 120
+				lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
+				x += w
+				w = 30
+				for _, v2 := range list {
+					sn_id2 := strings.Split(v2, ",")
+					id2 := sn_id2[1]
+					lib.RectFillColor(pdf, id2, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
+					x += w
+				}
+				y += 20
+				x = 10
+			}
+			w = 120
+			lib.RectFillColor(pdf, t, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
+			x += w
+			w = 30
+			for _, v := range list {
+				sn_id := strings.Split(v, ",")
+				id := sn_id[1]
+				t_t := dataMap[fmt.Sprintf("%s,%s", t, id)]
+				lib.RectFillColor(pdf, t_t, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
+				x += w
+			}
+			y += 20
+
+		}
+		y += 20
+	}
+	filename := time.Now().Format("20060102150405") + ".pdf"
+	timeStr := "ofile/湿度" + filename
 
 	err = pdf.WritePdf(timeStr)
 	if err != nil {
@@ -326,18 +498,12 @@ func (c *TaskDataController) TaskDataClass_Edit() {
 		c.ServeJSON()
 		return
 	}
-	//id, err := Task.Read_TaskData_ByT_sn(Task_r.T_task_id, T_sn)
-	//if err != nil && !errors.Is(err, orm.ErrNoRows) {
-	//	c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
-	//	c.ServeJSON()
-	//	return
-	//}
-	//if len(id) > 0 && id != T_id {
-	//	c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("sn[%s]已被编号[%s]关联,请修改后重试!", T_sn, id)}
-	//	c.ServeJSON()
-	//	return
-	//}
-
+	err = NatsServer.Edit_DeviceClassList(T_task_id, T_sn, T_id)
+	if err != nil {
+		c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
+		c.ServeJSON()
+		return
+	}
 	err = Task.Update_TaskData_ByT_sn(Task_r.T_task_id, T_sn, T_id)
 	if err != nil {
 		c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
@@ -446,6 +612,15 @@ func (c *TaskDataController) TaskData_AddS() {
 			c.ServeJSON()
 			return
 		}
+
+		// 同步设备到线上
+		err = NatsServer.Add_DeviceClassList(T_task_id, T_sn, T_id)
+		if err != nil {
+			c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
+			c.ServeJSON()
+			return
+		}
+
 	}
 
 	for _, v := range T_Data_list {
@@ -621,6 +796,16 @@ func (c *TaskDataController) TaskData_Add() {
 	T_t, _ := c.GetFloat("T_t")
 	T_rh, _ := c.GetFloat("T_rh")
 	T_time := c.GetString("T_time")
+	if len(T_sn) == 0 {
+		c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn 不能为空!"}
+		c.ServeJSON()
+		return
+	}
+	if len(T_id) == 0 {
+		c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id 不能为空!"}
+		c.ServeJSON()
+		return
+	}
 
 	T_task_id := c.GetString("T_task_id")
 	Task_r, err := NatsServer.Read_Task(T_task_id)
@@ -652,6 +837,15 @@ func (c *TaskDataController) TaskData_Add() {
 		c.ServeJSON()
 		return
 	}
+	// 同步设备到线上
+	if T_t == 0 && T_rh == 0 {
+		err = NatsServer.Add_DeviceClassList(T_task_id, T_sn, T_id)
+		if err != nil {
+			c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
+			c.ServeJSON()
+			return
+		}
+	}
 	is := Task.Add_TaskData(Task_r.T_task_id, T_sn, T_id, fmt.Sprintf("%.2f", T_t), fmt.Sprintf("%.2f", T_rh), T_time)
 	if !is {
 		c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}

+ 2 - 1
routers/TaskData.go

@@ -12,7 +12,8 @@ func init() {
 	beego.Router("/TaskData/TaskDataClass_Edit", &controllers.TaskDataController{}, "*:TaskDataClass_Edit")
 	beego.Router("/TaskData/TaskDataClass_Del", &controllers.TaskDataController{}, "*:TaskDataClass_Del")
 	beego.Router("/TaskData/List", &controllers.TaskDataController{}, "*:TaskData_List")
-	beego.Router("/TaskData/pdf", &controllers.TaskDataController{}, "*:TaskData_Pdf")
+	beego.Router("/TaskData/Temperature_Pdf", &controllers.TaskDataController{}, "*:TaskData_Temperature_Pdf")
+	beego.Router("/TaskData/Humidity_Pdf", &controllers.TaskDataController{}, "*:TaskData_Humidity_Pdf")
 	beego.Router("/TaskData/Add", &controllers.TaskDataController{}, "*:TaskData_Add")               //
 	beego.Router("/TaskData/AddS", &controllers.TaskDataController{}, "*:TaskData_AddS")             //
 	beego.Router("/TaskData/Up", &controllers.TaskDataController{}, "*:TaskData_Up")                 //