package controllers import ( "ColdVerify_local/Nats" "ColdVerify_local/Nats/NatsServer" "ColdVerify_local/conf" "ColdVerify_local/lib" "ColdVerify_local/logs" "ColdVerify_local/models/Certificate" "ColdVerify_local/models/System" "ColdVerify_local/models/Task" "errors" "fmt" "github.com/beego/beego/v2/client/orm" beego "github.com/beego/beego/v2/server/web" "github.com/signintech/gopdf" "github.com/vmihailenco/msgpack/v5" "github.com/xuri/excelize/v2" "gonum.org/v1/plot" "gonum.org/v1/plot/plotter" "gonum.org/v1/plot/vg" "gonum.org/v1/plot/vg/draw" "math" "os" "sort" "strconv" "strings" "sync" "time" ) type TaskDataController struct { beego.Controller } // 列表 - func (c *TaskDataController) TaskData_List() { var r_jsons lib.R_JSONS page, _ := c.GetInt("page") if page < 1 { page = 1 } page_z, _ := c.GetInt("page_z") if page_z < 1 { page_z = conf.Page_size } Time_start := c.GetString("Time_start") Time_end := c.GetString("Time_end") T_sn := c.GetString("T_sn") T_id := c.GetString("T_id") 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 } dcList, err := NatsServer.Device_Class_List(T_task_id) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "获取设备列表失败!"} c.ServeJSON() return } // 保存布局编号和校准证书对应关系 var deviceCertificateMap = make(map[string]string) // t_id, T_Certificate_sn for _, v := range dcList { deviceCertificateMap[v.T_id] = v.T_Certificate_sn } var cnt int64 List, cnt := Task.Read_TaskData_ById_List(Task_r.T_task_id, T_sn, T_id, Time_start, Time_end, page, page_z) for i := 0; i < len(List); i++ { List[i].T_Certificate_sn = deviceCertificateMap[List[i].T_id] } page_size := math.Ceil(float64(cnt) / float64(page_z)) r_jsons.List = List r_jsons.Page = page r_jsons.Page_size = int(page_size) r_jsons.Pages = lib.Func_page(int64(page), int64(page_size)) r_jsons.Num = int(cnt) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } func (c *TaskDataController) TaskData_Temperature_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_t) 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 { 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 { c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()} c.ServeJSON() return } defer func() { //删除目录 os.Remove(timeStr) }() c.Ctx.Output.Download(timeStr) } func chunkBy[T any](list []T, size int) [][]T { var chunks [][]T for size < len(list) { list, chunks = list[size:], append(chunks, list[0:size:size]) } return append(chunks, list) } // 列表 - func (c *TaskDataController) TaskDataClass_List() { page, _ := c.GetInt("page") if page < 1 { page = 1 } page_z, _ := c.GetInt("page_z") if page_z < 1 { page_z = conf.Page_size } 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 } List := Task.Read_TaskData_ById_ClassList(Task_r.T_task_id) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: List} c.ServeJSON() return } func (c *TaskDataController) TaskDataClass_Edit() { T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey")) T_task_id := c.GetString("T_task_id") T_sn := c.GetString("T_sn") T_id := c.GetString("T_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 } sn, err := Task.Read_TaskData_ByT_id(Task_r.T_task_id, T_id) if err != nil && !errors.Is(err, orm.ErrNoRows) { c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"} c.ServeJSON() return } if len(sn) > 0 && sn != T_sn { c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("编号[%s]已被sn[%s]关联,请修改后重试!", T_id, sn)} 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: "修改失败!"} c.ServeJSON() return } System.Add_UserLogs_T(T_uuid, "本地版-任务分裂", "修改分类数据"+Task_r.T_name, Task_r.T_task_id+"|"+T_sn+"|"+T_id) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } func (c *TaskDataController) TaskDataClass_Del() { T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey")) T_task_id := c.GetString("T_task_id") T_sn := c.GetString("T_sn") 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 } err = Task.Delete_TaskData_ByT_sn(Task_r.T_task_id, T_sn) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"} c.ServeJSON() return } System.Add_UserLogs_T(T_uuid, "本地版-任务分类", "删除分类数据"+Task_r.T_name, Task_r.T_task_id+"|"+T_sn) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } // 添加- func (c *TaskDataController) TaskData_AddS() { // 获取登录用户的uuid T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey")) 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 } //T_sn|T_id|T_t|T_rh|T_time? T_Data := c.GetString("T_Data") if len(T_Data) < 5 { c.Data["json"] = lib.JSONS{Code: 201, Msg: "err T_Data!"} c.ServeJSON() return } T_Data_list := strings.Split(T_Data, "?") println(len(T_Data_list), "len(T_Data_list)") var T_Data_list_x = 0 snMaps := make(map[string]string) for _, v := range T_Data_list { // 132|132|23.9|72.1|2023-04-30 07:03:00 if len(v) < 5 { println(v, "len(v) < 5") continue } v_list := strings.Split(v, "|") id, sn := strings.Trim(v_list[1], " "), strings.Trim(v_list[0], " ") sn1, ok := snMaps[id] if !ok { snMaps[id] = sn } else if sn1 != sn { c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("编号[%s]已被sn[%s]关联,请修改后重试!", id, sn1)} c.ServeJSON() return } } for T_id, T_sn := range snMaps { sn, err := Task.Read_TaskData_ByT_id(Task_r.T_task_id, T_id) if err != nil && !errors.Is(err, orm.ErrNoRows) { continue } if len(sn) > 0 && sn != T_sn { c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("编号[%s]已被sn[%s]关联,请修改后重试!", T_id, sn)} 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.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 { // 132|132|23.9|72.1|2023-04-30 07:03:00 if len(v) < 5 { println(v, "len(v) < 5") continue } v_list := strings.Split(v, "|") t, _ := lib.ReplaceSeconds(v_list[4]) is := Task.Add_TaskData(Task_r.T_task_id, v_list[0], v_list[1], v_list[2], v_list[3], t) if is { T_Data_list_x += 1 } } System.Add_UserLogs_T(T_uuid, "本地版-任务数据", "添加数据"+Task_r.T_name, Task_r.T_task_id+"结果:"+string(len(T_Data_list))+"/"+string(T_Data_list_x)+"|=> "+T_Data) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: strconv.Itoa(T_Data_list_x)} c.ServeJSON() return } func (c *TaskDataController) TaskData_AddS_Excel() { // 获取登录用户的uuid T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey")) 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 } // 获取上传的文件 file, _, err := c.GetFile("file") if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "文件上传失败!"} c.ServeJSON() return } // 解析 Excel 文件 xlFile, err := excelize.OpenReader(file) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "文件解析失败!"} c.ServeJSON() return } rows, err := xlFile.GetRows(xlFile.GetSheetName(0)) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "获取工作表失败!"} c.ServeJSON() return } var T_Data_list_x int snMaps := make(map[string]string) var valueStrings []string for k, val := range rows { // 跳过第一行 if k == 0 { continue } if len(val) < 5 { println(val, "len(v) < 5") continue } fmt.Println(val) id, sn := val[1], val[0] sn1, ok := snMaps[id] if !ok { snMaps[id] = sn } else if sn1 != sn { c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("编号[%s]已被sn[%s]关联,请修改后重试!", id, sn1)} c.ServeJSON() return } t, _ := lib.ReplaceSeconds(val[4]) valueStrings = append(valueStrings, fmt.Sprintf("('%s','%s',%v,%v,'%s')", val[0], val[1], val[2], val[3], t)) //T_Data_list_x += 1 } for T_id, T_sn := range snMaps { sn, err := Task.Read_TaskData_ByT_id(Task_r.T_task_id, T_id) if err != nil && !errors.Is(err, orm.ErrNoRows) { continue } if len(sn) > 0 && sn != T_sn { c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("编号[%s]已被sn[%s]关联,请修改后重试!", T_id, sn)} 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 = Task.DeleteDeduplicate(T_task_id) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "导入失败"} c.ServeJSON() return } // 创建唯一索引 err = Task.Create_Unique_Index(T_task_id) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "导入失败"} c.ServeJSON() return } pageSize := 10000 // 每页的条数 totalPages := len(valueStrings) / pageSize // 总页数 // 遍历每一页 for page := 1; page <= totalPages+1; page++ { // 计算当前页的起始索引和结束索引 startIndex := (page - 1) * pageSize if startIndex > len(valueStrings) { startIndex = len(valueStrings) } endIndex := page * pageSize // 边界判断,如果结束索引超过列表长度,则将结束索引设置为列表最后一个元素的索引加一 if endIndex > len(valueStrings) { endIndex = len(valueStrings) } fmt.Printf("startIndex %d endIndex %d\n", startIndex, endIndex) // 获取当前页的数据 currentList := valueStrings[startIndex:endIndex] err = Task.Adds_TaskData(T_task_id, currentList) if err == nil { T_Data_list_x += len(currentList) } } //err = Task.Adds_TaskData(T_task_id, valueStrings) // 删除唯一索引 Task.Delete_Unique_Index(T_task_id) System.Add_UserLogs_T(T_uuid, "本地版-任务数据", "添加数据"+Task_r.T_name, Task_r.T_task_id+"结果:"+strconv.Itoa(T_Data_list_x)) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: strconv.Itoa(T_Data_list_x)} c.ServeJSON() return } // 添加- func (c *TaskDataController) TaskData_Add() { // 获取登录用户的uuid T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey")) T_sn := c.GetString("T_sn") T_id := c.GetString("T_id") 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) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"} c.ServeJSON() return } sn, err := Task.Read_TaskData_ByT_id(Task_r.T_task_id, T_id) if err != nil && !errors.Is(err, orm.ErrNoRows) { c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"} c.ServeJSON() return } if len(sn) > 0 && sn != T_sn { c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("编号[%s]已被sn[%s]关联,请修改后重试!", T_id, sn)} 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 } // 同步设备到线上 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: "添加失败!"} c.ServeJSON() return } System.Add_UserLogs_T(T_uuid, "本地版-任务数据", "添加数据"+Task_r.T_name, Task_r.T_task_id+"|"+T_sn+"|"+T_id+"|"+fmt.Sprintf("%.2f", T_t)+"|"+fmt.Sprintf("%.2f", T_rh)+T_time) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } // 修改- func (c *TaskDataController) TaskData_Up() { // 获取登录用户的uuid T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey")) Id, err := c.GetInt("Id") if err != nil { c.Data["json"] = lib.JSONS{Code: 201, Msg: "Id 错误!"} c.ServeJSON() return } T_t, err := c.GetFloat("T_t") if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_t 错误!"} c.ServeJSON() return } T_rh, err := c.GetFloat("T_rh") if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_rh 错误!"} c.ServeJSON() return } T_time := c.GetString("T_time") 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 } is := Task.Up_TaskData(Task_r.T_task_id, strconv.Itoa(Id), fmt.Sprintf("%.2f", T_t), fmt.Sprintf("%.2f", T_rh), T_time) if !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"} c.ServeJSON() return } System.Add_UserLogs_T(T_uuid, "本地版-任务数据", "修改数据"+Task_r.T_name, Task_r.T_task_id+"|"+strconv.Itoa(Id)+"|"+fmt.Sprintf("%.2f", T_t)+"|"+fmt.Sprintf("%.2f", T_rh)+T_time) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } // 删除- func (c *TaskDataController) TaskData_Del() { // 获取登录用户的uuid T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey")) Id, err := c.GetInt("Id") if err != nil { c.Data["json"] = lib.JSONS{Code: 201, Msg: "Id 错误!"} c.ServeJSON() return } 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 } is := Task.Del_TaskData(Task_r.T_task_id, strconv.Itoa(Id)) if !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"} c.ServeJSON() return } System.Add_UserLogs_T(T_uuid, "本地版-任务数据", "删除"+Task_r.T_name, Task_r.T_task_id+"|"+strconv.Itoa(Id)) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } // 删除- func (c *TaskDataController) TaskData_Del_t_id() { // 获取登录用户的uuid T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey")) Id := c.GetString("Id") 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 } is := Task.Del_TaskData_t_id(Task_r.T_task_id, Id) if !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"} c.ServeJSON() return } System.Add_UserLogs_T(T_uuid, "本地版-任务数据", "删除"+Task_r.T_name, Task_r.T_task_id+"|"+Id) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } // 列表 - 接口 func (c *TaskDataController) Export_Data_Excel() { Time_start := c.GetString("Time_start") Time_end := c.GetString("Time_end") T_sn_str := c.GetString("T_sn_list") //865901058809339,865901058815849,865901058818991,865901058810568 //T_id, err := c.GetInt("T_id") //if err != nil { // T_id = -1 // //} 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 } T_sn_list := strings.Split(T_sn_str, ",") DeviceSensor_data_list := []Task.TaskData_{} for _, v := range T_sn_list { DeviceSensor_data, _ := Task.Read_TaskData_ById_List(Task_r.T_task_id, v, "", Time_start, Time_end, 1, 9999) DeviceSensor_data_list = append(DeviceSensor_data_list, DeviceSensor_data...) } f := excelize.NewFile() // 设置单元格的值 // 这里设置表头 f.SetCellValue("Sheet1", "A1", "编号") f.SetCellValue("Sheet1", "B1", "SN") f.SetCellValue("Sheet1", "C1", "温度℃") f.SetCellValue("Sheet1", "D1", "湿度%") f.SetCellValue("Sheet1", "E1", "记录时间") // 设置列宽 f.SetColWidth("Sheet1", "A", "A", 7) f.SetColWidth("Sheet1", "B", "B", 20) f.SetColWidth("Sheet1", "C", "C", 10) f.SetColWidth("Sheet1", "D", "D", 10) f.SetColWidth("Sheet1", "E", "E", 22) line := 1 // 循环写入数据 for _, v := range DeviceSensor_data_list { line++ f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), v.T_id) f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_sn) f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), strconv.FormatFloat(float64(v.T_t), 'f', 1, 64)) f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), strconv.FormatFloat(float64(v.T_rh), 'f', 1, 64)) f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_time) } fmt.Println("DeviceSensor_data:", len(DeviceSensor_data_list)) lib.Create_Dir("./ofile") timeStr := time.Now().Format("20060102150405") // 保存文件 if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil { fmt.Println(err) } if !lib.Pload_qiniu("ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx") { c.Data["json"] = lib.JSONS{Code: 203, Msg: "oss!"} c.ServeJSON() return } //删除目录 err = os.Remove("ofile/" + timeStr + ".xlsx") if err != nil { fmt.Println(err) } c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: "https://bzdcoldverifyoss.baozhida.cn/" + "ofile/" + timeStr + ".xlsx"} c.ServeJSON() return } func (c *TaskDataController) Check() { T_task_id := c.GetString("T_task_id") Task_r, err := NatsServer.Read_Task(T_task_id) if Task_r.T_collection_state == 2 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"} c.ServeJSON() return } if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"} c.ServeJSON() return } type R_JSONS struct { T_sn string // SN T_id string // 编号 T_unm int // 数据量 T_time_interval int64 // 时间间隔 Time_start string // 开始 Time_end string // 结束 Result int // 200 OK, 201 可以补 , 202 不能补 Result_str string // 提示内容 Result_Time_start string // 2022-8-05 21:01 Result_Time_defect int64 // 缺失时间间隔 } var r_jsons []R_JSONS List := Task.Read_TaskData_ById_ClassList(Task_r.T_task_id) for _, v := range List { var r_json R_JSONS r_json.T_sn = v.T_sn r_json.T_id = v.T_id r_json.Result = 200 DeviceSensor_data := Task.Read_TaskData_ById_List_(Task_r.T_task_id, v.T_sn) r_json.T_unm = len(DeviceSensor_data) if r_json.T_unm > 2 { r_json.T_id = DeviceSensor_data[0].T_id r_json.Time_end = DeviceSensor_data[len(DeviceSensor_data)-1].T_time r_json.Time_start = DeviceSensor_data[0].T_time formatTime_a, _ := time.Parse("2006-1-2 15:04", DeviceSensor_data[0].T_time) formatTime_b, _ := time.Parse("2006-1-2 15:04", DeviceSensor_data[1].T_time) formatTime_x := formatTime_b.Unix() - formatTime_a.Unix() r_json.T_time_interval = formatTime_x println("formatTime_x:", v.T_id, v.T_sn, formatTime_x) if formatTime_x > 60*30 || formatTime_x < 60 { r_json.Result = 202 r_json.Result_str = "数据异常(数据的第一个时间与第二个时间相差 " + strconv.FormatInt(formatTime_x, 10) + "秒),必须大于60秒,小于30分钟" r_jsons = append(r_jsons, r_json) continue } for data_i := 2; data_i < len(DeviceSensor_data); data_i++ { formatTime_a = formatTime_b formatTime_b, _ = time.Parse("2006-1-2 15:04", DeviceSensor_data[data_i].T_time) formatTime_ := formatTime_b.Unix() - formatTime_a.Unix() println("formatTime_x-:", formatTime_) if formatTime_ < formatTime_x { r_json.Result = 202 r_json.Result_str = "开始时间:" + DeviceSensor_data[data_i-1].T_time + " 离下一条时间间隔:" + strconv.FormatInt(formatTime_, 10) + "秒,间隔小于时间间隔,不能自动补充" break } if formatTime_x != formatTime_ { if formatTime_ > 60*30 { r_json.Result = 202 r_json.Result_str = "开始时间:" + DeviceSensor_data[data_i-1].T_time + " 离下一条时间间隔:" + strconv.FormatInt(formatTime_, 10) + "秒,相差时间大于30分钟,不能自动补充" break } r_json.Result = 201 r_json.Result_str = "开始时间:" + DeviceSensor_data[data_i-1].T_time + " 离下一条时间间隔:" + strconv.FormatInt(formatTime_, 10) + "秒" r_json.Result_Time_start = DeviceSensor_data[data_i-1].T_time r_json.Result_Time_defect = formatTime_ println(r_json.Result_str) break } } } else { r_json.Result = 202 r_json.Result_str = "数据量太少 必须大于 2条以上!" } r_jsons = append(r_jsons, r_json) } c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } func (c *TaskDataController) Check_Asyn() { T_task_id := c.GetString("T_task_id") Task_r, err := NatsServer.Read_Task(T_task_id) if Task_r.T_collection_state == 2 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"} c.ServeJSON() return } if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"} c.ServeJSON() return } type R_JSONS struct { T_sn string // SN T_id string // 编号 T_unm int // 数据量 T_time_interval int64 // 时间间隔 Time_start string // 开始 Time_end string // 结束 Result int // 200 OK, 201 可以补 , 202 不能补 Result_str string // 提示内容 Result_Time_start string // 2022-8-05 21:01 Result_Time_defect int64 // 缺失时间间隔 } List := Task.Read_TaskData_ById_ClassList(Task_r.T_task_id) r_jsons := make([]R_JSONS, len(List)) var limitMaxNum = 10 var chData = make(chan int, limitMaxNum) var jobGroup sync.WaitGroup var tasknum = len(List) for i := 0; i < tasknum; i++ { chData <- 1 jobGroup.Add(1) go func(index int, wg *sync.WaitGroup, res *[]R_JSONS) { defer wg.Done() var r_json R_JSONS r_json.T_sn = List[index].T_sn r_json.T_id = List[index].T_id r_json.Result = 200 DeviceSensor_data := Task.Read_TaskData_ById_List_(Task_r.T_task_id, List[index].T_sn) r_json.T_unm = len(DeviceSensor_data) if r_json.T_unm > 2 { r_json.T_id = DeviceSensor_data[0].T_id r_json.Time_end = DeviceSensor_data[len(DeviceSensor_data)-1].T_time r_json.Time_start = DeviceSensor_data[0].T_time formatTime_a, _ := time.Parse("2006-1-2 15:04", DeviceSensor_data[0].T_time) formatTime_b, _ := time.Parse("2006-1-2 15:04", DeviceSensor_data[1].T_time) formatTime_x := formatTime_b.Unix() - formatTime_a.Unix() r_json.T_time_interval = formatTime_x println("formatTime_x:", List[index].T_id, List[index].T_sn, formatTime_x) if formatTime_x > 60*30 || formatTime_x < 60 { r_json.Result = 202 r_json.Result_str = "数据异常(数据的第一个时间与第二个时间相差 " + strconv.FormatInt(formatTime_x, 10) + "秒),必须大于60秒,小于30分钟" *res = append(*res, r_json) <-chData return } for data_i := 2; data_i < len(DeviceSensor_data); data_i++ { formatTime_a = formatTime_b formatTime_b, _ = time.Parse("2006-1-2 15:04", DeviceSensor_data[data_i].T_time) formatTime_ := formatTime_b.Unix() - formatTime_a.Unix() if formatTime_ < formatTime_x { println("formatTime_x-:", formatTime_) r_json.Result = 202 r_json.Result_str = "开始时间:" + DeviceSensor_data[data_i-1].T_time + " 离下一条时间间隔:" + strconv.FormatInt(formatTime_, 10) + "秒,间隔小于时间间隔,不能自动补充" break } if formatTime_x != formatTime_ { if formatTime_ > 60*30 { println("formatTime_x-:", formatTime_) r_json.Result = 202 r_json.Result_str = "开始时间:" + DeviceSensor_data[data_i-1].T_time + " 离下一条时间间隔:" + strconv.FormatInt(formatTime_, 10) + "秒,相差时间大于30分钟,不能自动补充" break } r_json.Result = 202 r_json.Result_str = "开始时间:" + DeviceSensor_data[data_i-1].T_time + " 离下一条时间间隔:" + strconv.FormatInt(formatTime_, 10) + "秒" r_json.Result_Time_start = DeviceSensor_data[data_i-1].T_time r_json.Result_Time_defect = formatTime_ println(r_json.Result_str) break } } } else { r_json.Result = 202 r_json.Result_str = "数据量太少 必须大于 2条以上!" } *res = append(*res, r_json) <-chData }(i, &jobGroup, &r_jsons) } //使用Wait等待所有任务执行完毕 jobGroup.Wait() jsonsMap := map[string]R_JSONS{} resp := make([]R_JSONS, 0) for _, json := range r_jsons { jsonsMap[json.T_id] = json } for _, v := range List { resp = append(resp, jsonsMap[v.T_id]) } c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: resp} c.ServeJSON() return } // 打包数据本地数据 func (c *TaskDataController) TaskData_Import_TaskData() { // 获取登录用户的uuid T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey")) T_task_id := c.GetString("T_task_id") _, 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 err = lib.Create_Dir(conf.Sql_Temp_Dir); err != nil { logs.Println("创建sql临时文件失败") } sql_file := fmt.Sprintf("%sZ_TaskData_%s.sql", conf.Sql_Temp_Dir, T_task_id) org, err := Task.Dump_TaskData(T_task_id, conf.MysqlServer2_Username, conf.MysqlServer2_Password, conf.MysqlServer2_UrlPort, conf.MysqlServer2_Database, sql_file) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "z_task_data_" + T_task_id + "线上数据导出失败!"} c.ServeJSON() return } System.Add_UserLogs_T(T_uuid, "本地版-任务数据-打包本地数据", "导出线上数据Z_TaskData_"+T_task_id, org) i := 0 flag := false Task.CREATE_TaskData(conf.Local_AliasName, T_task_id) for i < 10 { Task.Truncate_TaskData(conf.Local_AliasName, T_task_id) org, err = Task.Insert_TaskData(conf.MysqlServer_Username, conf.MysqlServer_Password, conf.MysqlServer_UrlPort, conf.MysqlServer_Database, sql_file) if err != nil { logs.Println("任务数据-打包本地数据", "线下导入数据Z_TaskData_"+T_task_id, err.Error()) } else { if Task.Check_TaskData_Num(T_task_id) { System.Add_UserLogs_T(T_uuid, "本地版-任务数据-打包本地数据", "线下导入数据Z_TaskData_"+T_task_id, org) flag = true break } } i++ } // 重试10次后仍然没有成功导入数据 if !flag { c.Data["json"] = lib.JSONS{Code: 202, Msg: "z_task_data_" + T_task_id + "线下导入数据失败!"} c.ServeJSON() return } //删除导出的sql文件 _ = os.Remove(sql_file) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() } // 更新线上数据 func (c *TaskDataController) TaskData_Up_TaskData() { // 获取登录用户的uuid T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey")) 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 err = lib.Create_Dir(conf.Sql_Temp_Dir); err != nil { logs.Println("创建sql临时文件失败") } sql_file := fmt.Sprintf("%sZ_TaskData_%s.sql", conf.Sql_Temp_Dir, T_task_id) org, err := Task.Dump_TaskData(T_task_id, conf.MysqlServer_Username, conf.MysqlServer_Password, conf.MysqlServer_UrlPort, conf.MysqlServer_Database, sql_file) if err != nil { logs.Println(T_uuid, "任务数据-更新线上数据", "导出线下数据Z_TaskData_"+T_task_id, err.Error()) c.Data["json"] = lib.JSONS{Code: 202, Msg: "z_task_data_" + T_task_id + "线下数据导出失败!"} c.ServeJSON() return } System.Add_UserLogs_T(T_uuid, "本地版-任务数据-更新线上数据", "线上导入数据Z_TaskData_"+T_task_id, org) i := 0 flag := false for i < 10 { Task.Truncate_TaskData(conf.Server_AliasName, T_task_id) org, err = Task.Insert_TaskData(conf.MysqlServer2_Username, conf.MysqlServer2_Password, conf.MysqlServer2_UrlPort, conf.MysqlServer2_Database, sql_file) if err != nil { logs.Println(T_uuid, "任务数据-更新线上数据", "线上导入数据Z_TaskData_"+T_task_id, err.Error()) } else { if Task.Check_TaskData_Num(T_task_id) { System.Add_UserLogs_T(T_uuid, "本地版-任务数据-更新线上数据", "线上导入数据Z_TaskData_"+T_task_id, org) flag = true break } } i++ } // 重试10次后仍然没有成功导入数据 if !flag { c.Data["json"] = lib.JSONS{Code: 202, Msg: "z_task_data_" + T_task_id + "线上导入数据失败!"} c.ServeJSON() return } //删除导出的sql文件 _ = os.Remove(sql_file) // 线上数据更新后 将当前任务 交付审核 标志 为 1 Task_r.T_delivery_state = 1 err = NatsServer.Update_Task(Task_r) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"} c.ServeJSON() return } // 向线上用户日志表写入数据 System.Add_UserLogs_T(T_uuid, "本地版-任务", "修改", Task_r) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() } // 更新线上数据后台执行 func (c *TaskDataController) TaskData_Up_TaskData_Back() { // 获取登录用户的uuid T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey")) 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_delivery_state == 2 { c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } // 采集中 Task_r.T_delivery_state = 2 err = NatsServer.Update_Task(Task_r) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "提交失败!"} c.ServeJSON() return } data := Nats.Up_TaskData_Back{ T_uuid: T_uuid, Task: Task_r, } b, _ := msgpack.Marshal(&data) _ = lib.Nats.Publish("ColdVerify_Local_Up_TaskData", b) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } // CopyFromPosition 数据拷贝 func (c *TaskDataController) CopyFromPosition() { T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey")) //if !b_ { // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"} // c.ServeJSON() // return //} StartTime := c.GetString("StartTime") startTime, ok := lib.TimeStrToTime(StartTime) if !ok { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"} c.ServeJSON() return } EndTime := c.GetString("EndTime") endTime, ok := lib.TimeStrToTime(EndTime) if !ok { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"} c.ServeJSON() return } CopyTime := c.GetString("CopyTime") copyTime, ok := lib.TimeStrToTime(CopyTime) if !ok { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"} c.ServeJSON() return } CopyEndTime := copyTime.Add(endTime.Sub(startTime)).Format("2006-01-02 15:04:05") // 时间间隔 分钟 T_saveT, _ := c.GetInt("T_saveT") SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|") 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 } for _, v := range SN_List { sn_id := strings.Split(v, ",") if len(sn_id) != 2 { continue } sn, id_str := sn_id[0], sn_id[1] List, _ := Task.Read_TaskData_ById_List_AES(Task_r.T_task_id, sn, id_str, StartTime, EndTime, 0, 9999) Task.DeleteTaskDataByTimeRange(Task_r.T_task_id, sn, id_str, CopyTime, CopyEndTime) ct := copyTime go func(TaskDataList []Task.TaskData_, task_id string, T_saveT int) { for _, taskData := range TaskDataList { taskData.T_time = ct.Format("2006-01-02 15:04:05") Task.InsertTaskData(task_id, taskData) ct = ct.Add(time.Second * time.Duration(T_saveT)) } }(List, Task_r.T_task_id, T_saveT) } System.Add_UserLogs_T(T_uuid, "数据复制", fmt.Sprintf("数据复制(%s)[%s|%s],复制时间:%s,,时间间隔:%d", T_task_id, StartTime, EndTime, CopyTime, T_saveT), SN_List) c.Data["json"] = lib.JSONS{200, "数据复制已提交后台处理!", nil} c.ServeJSON() return } // RepairSensorData 数据补漏 func (c *TaskDataController) RepairSensorData() { T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey")) //if !b_ { // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"} // c.ServeJSON() // return //} StartTime := c.GetString("StartTime") _, ok := lib.TimeStrToTime(StartTime) if !ok { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"} c.ServeJSON() return } EndTime := c.GetString("EndTime") _, ok = lib.TimeStrToTime(EndTime) if !ok { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"} c.ServeJSON() return } // 时间间隔 秒 saveTime, _ := c.GetInt("T_saveT") SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|") 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 } num := 0 for _, v := range SN_List { sn_id := strings.Split(v, ",") if len(sn_id) != 2 { continue } sn, id_str := sn_id[0], sn_id[1] list, _ := Task.Read_TaskData_ById_List_AES(Task_r.T_task_id, sn, id_str, StartTime, EndTime, 0, 9999) for i := 0; i < len(list)-1; i++ { current := list[i].T_time next := list[i+1].T_time c, _ := time.Parse("2006-01-02 15:04:05", current) n, _ := time.Parse("2006-01-02 15:04:05", next) interval := n.Unix() - c.Unix() //logs.Debug("时间间隔:", interval, "保存时间:", saveTime) fmt.Println("当前:", current, "下一个:", next) if int(interval) > saveTime { ttInterval := list[i+1].T_t - list[i].T_t ttt := list[i].T_t // 温度临时变量 trhInterval := list[i+1].T_rh - list[i].T_rh trht := list[i].T_rh //湿度临时变量 count := (int(interval) - saveTime) / saveTime if int(interval)%saveTime != 0 { count++ } num += count for k := 0; k < count; k++ { t := c.Add(time.Second * time.Duration(saveTime)).Format("2006-01-02 15:04:05") //时间临时变量 ttt += ttInterval / float32(count) trht += trhInterval / float32(count) Task.InsertTaskData(Task_r.T_task_id, Task.TaskData_{ T_sn: list[i].T_sn, T_id: list[i].T_id, T_t: ttt, T_rh: trht, T_time: t, }) c = c.Add(time.Second * time.Duration(saveTime)) } } } } System.Add_UserLogs_T(T_uuid, "数据补漏", fmt.Sprintf("数据补漏(%s)[%s|%s],时间间隔:%d", T_task_id, StartTime, EndTime, saveTime), SN_List) c.Data["json"] = lib.JSONS{200, fmt.Sprintf("补漏完成!共补漏%d条数据", num), nil} c.ServeJSON() return } // DataSensorDataTrend 数据趋势 func (c *TaskDataController) DataSensorDataTrend() { T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey")) //if !b_ { // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"} // c.ServeJSON() // return //} StartTime := c.GetString("StartTime") _, ok := lib.TimeStrToTime(StartTime) if !ok { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"} c.ServeJSON() return } EndTime := c.GetString("EndTime") _, ok = lib.TimeStrToTime(EndTime) if !ok { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"} c.ServeJSON() return } // 时间间隔 秒 saveTime, _ := c.GetInt("T_saveT") SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|") 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 } for _, v := range SN_List { sn_id := strings.Split(v, ",") if len(sn_id) != 2 { continue } sn, id_str := sn_id[0], sn_id[1] list, _ := Task.Read_TaskData_ById_List_AES(Task_r.T_task_id, sn, id_str, StartTime, EndTime, 0, 9999) if len(list) == 0 { continue } Task.DeleteTaskDataByTimeRange(Task_r.T_task_id, sn, id_str, StartTime, EndTime) first := list[0] last := list[len(list)-1] current, _ := time.Parse("2006-01-02 15:04:05", first.T_time) next, _ := time.Parse("2006-01-02 15:04:05", last.T_time) interval := next.Sub(current).Seconds() / float64(saveTime) ttInterval := (last.T_t - first.T_t) / float32(interval) trhInterval := (last.T_rh - first.T_rh) / float32(interval) tt := first.T_t ttrh := first.T_rh for current.Unix() < next.Unix() { tt += ttInterval ttrh += trhInterval ttime := current.Format("2006-01-02 15:04:05") Task.InsertTaskData(Task_r.T_task_id, Task.TaskData_{ //加保存时间 T_sn: first.T_sn, T_id: id_str, T_t: tt, T_rh: ttrh, T_time: ttime, }) current = current.Add(time.Second * time.Duration(saveTime)) } } System.Add_UserLogs_T(T_uuid, "数据趋势", fmt.Sprintf("数据趋势(%s)[%s|%s],时间间隔:%d", T_task_id, StartTime, EndTime, saveTime), SN_List) c.Data["json"] = lib.JSONS{200, "设置趋势操作成功", nil} c.ServeJSON() return } // UpdateRand 数据随机 func (c *TaskDataController) UpdateRand() { T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey")) //if !b_ { // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"} // c.ServeJSON() // return //} StartTime := c.GetString("StartTime") _, ok := lib.TimeStrToTime(StartTime) if !ok { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"} c.ServeJSON() return } EndTime := c.GetString("EndTime") _, ok = lib.TimeStrToTime(EndTime) if !ok { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"} c.ServeJSON() return } TemperatureMin, _ := c.GetInt("TemperatureMin") // 温度 TemperatureMax, _ := c.GetInt("TemperatureMax") HumidityMax, _ := c.GetInt("HumidityMax") // 湿度 HumidityMin, _ := c.GetInt("HumidityMin") SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|") 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 } for _, v := range SN_List { sn_id := strings.Split(v, ",") if len(sn_id) != 2 { continue } sn, id := sn_id[0], sn_id[1] Task.UpdateTaskDataTemperatureAndHumidityRandom(Task_r.T_task_id, sn, id, StartTime, EndTime, TemperatureMax, TemperatureMin, HumidityMax, HumidityMin) } System.Add_UserLogs_T(T_uuid, "数据随机", fmt.Sprintf("数据随机(%s)[%s|%s],温度[%d-%d],湿度[%d-%d]", T_task_id, StartTime, EndTime, TemperatureMin, TemperatureMax, HumidityMax, HumidityMin), SN_List) //反馈成功 c.Data["json"] = lib.JSONS{200, "调整随机偏移值成功!", nil} c.ServeJSON() return } // UpdateFix 更新固定值 func (c *TaskDataController) UpdateFix() { T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey")) //if !b_ { // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"} // c.ServeJSON() // return //} StartTime := c.GetString("StartTime") _, ok := lib.TimeStrToTime(StartTime) if !ok { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"} c.ServeJSON() return } EndTime := c.GetString("EndTime") _, ok = lib.TimeStrToTime(EndTime) if !ok { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"} c.ServeJSON() return } FixTemperature := c.GetString("FixTemperature") FixHumidity := c.GetString("FixHumidity") //2.得到数据进行统一设置访问修改 humidity, _ := strconv.ParseFloat(FixHumidity, 64) temperature, _ := strconv.ParseFloat(FixTemperature, 64) SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|") 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 } //3.循环更新数据 for _, v := range SN_List { sn_id := strings.Split(v, ",") if len(sn_id) != 2 { continue } sn, id := sn_id[0], sn_id[1] Task.UpdateTaskDataTemperatureAndHumidity(Task_r.T_task_id, sn, id, StartTime, EndTime, temperature, humidity) } System.Add_UserLogs_T(T_uuid, "更新固定值", fmt.Sprintf("更新固定值(%s)[%s|%s],温度[%f],湿度[%f]", T_task_id, StartTime, EndTime, temperature, humidity), SN_List) //4.反馈成功 c.Data["json"] = lib.JSONS{200, "调整固定偏移值成功!", nil} c.ServeJSON() } // DataSensorDataSmooth 数据平滑 func (c *TaskDataController) DataSensorDataSmooth() { T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey")) //if !b_ { // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"} // c.ServeJSON() // return //} StartTime := c.GetString("StartTime") _, ok := lib.TimeStrToTime(StartTime) if !ok { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"} c.ServeJSON() return } EndTime := c.GetString("EndTime") _, ok = lib.TimeStrToTime(EndTime) if !ok { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"} c.ServeJSON() return } tRange, _ := c.GetFloat("tRange") hRange, _ := c.GetFloat("hRange") SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|") 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 } var count int for _, v := range SN_List { sn_id := strings.Split(v, ",") if len(sn_id) != 2 { continue } sn := sn_id[0] id_str := sn_id[1] list, _ := Task.Read_TaskData_ById_List_AES(Task_r.T_task_id, sn, id_str, StartTime, EndTime, 0, 9999) for i := 1; i < len(list); i++ { n := list[i-1] old := list[i] newO := list[i] //变化差 var tInterval = old.T_t - n.T_t var hInterval = old.T_rh - n.T_rh fmt.Println("温度:", n.T_t, "温度next:", old.T_t, "差值:", n.T_t-old.T_t) if tRange != 0 { if tInterval > float32(tRange) { newO.T_t = n.T_t + float32(tRange) } else if tInterval < -float32(tRange) { newO.T_t = n.T_t - float32(tRange) } } if hRange != 0 { if hInterval > float32(hRange) { newO.T_rh = n.T_rh + float32(hRange) } else if hInterval < -float32(hRange) { newO.T_rh = n.T_rh - float32(hRange) } } if old.T_rh != newO.T_rh || old.T_t != newO.T_t { fmt.Println("原始数据:", old, "新数据:", newO) list[i] = newO Task.UpdateTaskData(Task_r.T_task_id, sn, id_str, old, newO) count++ } } } System.Add_UserLogs_T(T_uuid, "数据平滑", fmt.Sprintf("数据平滑(%s)[%s|%s],温度[%f],湿度[%f]", T_task_id, StartTime, EndTime, tRange, hRange), SN_List) c.Data["json"] = lib.JSONS{200, "操作成功处理" + fmt.Sprint(count) + "条数据", nil} c.ServeJSON() } // CopyFromPosition 数据拷贝平均值 func (c *TaskDataController) CopyFromPositionAverageSN() { T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey")) //if !b_ { // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"} // c.ServeJSON() // return //} StartTime := c.GetString("StartTime") startTime, ok := lib.TimeStrToTime(StartTime) if !ok { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"} c.ServeJSON() return } EndTime := c.GetString("EndTime") endTime, ok := lib.TimeStrToTime(EndTime) if !ok { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"} c.ServeJSON() return } CopyTime := c.GetString("CopyTime") CopySN := c.GetString("CopySN") CopyID := c.GetString("CopyID") copyTime, ok := lib.TimeStrToTime(CopyTime) // 时间间隔 s T_saveT, _ := c.GetInt("T_saveT") if !ok { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"} c.ServeJSON() return } CopyEndTime := copyTime.Add(endTime.Sub(startTime)).Format("2006-01-02 15:04:05") SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|") if len(SN_List) != 2 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "SN_List Err!"} c.ServeJSON() return } 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 } sn_id1 := strings.Split(SN_List[0], ",") if len(sn_id1) != 2 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "SN_List Err!"} c.ServeJSON() return } sn1, id_str1 := sn_id1[0], sn_id1[1] sn_id2 := strings.Split(SN_List[1], ",") if len(sn_id2) != 2 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "SN_List Err!"} c.ServeJSON() return } sn2, id_str2 := sn_id2[0], sn_id2[1] List1, _ := Task.Read_TaskData_ById_List_AES(Task_r.T_task_id, sn1, id_str1, StartTime, EndTime, 0, 9999) List2, _ := Task.Read_TaskData_ById_List_AES(Task_r.T_task_id, sn2, id_str2, StartTime, EndTime, 0, 9999) num := len(List1) if len(List2) < len(List1) { num = len(List2) } var list []Task.TaskData_ ct := copyTime for i := 0; i < num; i++ { if List1[i].T_time != List2[i].T_time { c.Data["json"] = lib.JSONS{202, fmt.Sprintf("%s【%s】、%s【%s】时间不一致", List1[i].T_id, List1[i].T_time, List2[i].T_id, List2[i].T_time), nil} c.ServeJSON() return //ct = ct.Add(time.Second * time.Duration(T_saveT)) //continue } T_t := (List1[i].T_t + List2[i].T_t) / 2 T_rh := (List1[i].T_rh + List2[i].T_rh) / 2 list = append(list, Task.TaskData_{ T_sn: CopySN, T_id: CopyID, T_t: T_t, T_rh: T_rh, T_time: ct.Format("2006-01-02 15:04:05"), }) ct = ct.Add(time.Second * time.Duration(T_saveT)) } Task.DeleteTaskDataByTimeRange(Task_r.T_task_id, CopySN, CopyID, CopyTime, CopyEndTime) go func(TaskDataList []Task.TaskData_, task_id string) { for _, taskData := range TaskDataList { Task.InsertTaskData(task_id, taskData) } }(list, Task_r.T_task_id) System.Add_UserLogs_T(T_uuid, "数据复制平均值", fmt.Sprintf("数据复制(%s)[%s|%s],复制时间:%s,,时间间隔:%d", T_task_id, StartTime, EndTime, CopyTime, T_saveT), SN_List) c.Data["json"] = lib.JSONS{200, "数据复制已提交后台处理!", nil} c.ServeJSON() return } // CopyFromPosition 数据拷贝 func (c *TaskDataController) CopyFromPositionSN() { T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey")) //if !b_ { // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"} // c.ServeJSON() // return //} StartTime := c.GetString("StartTime") startTime, ok := lib.TimeStrToTime(StartTime) if !ok { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"} c.ServeJSON() return } EndTime := c.GetString("EndTime") endTime, ok := lib.TimeStrToTime(EndTime) if !ok { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"} c.ServeJSON() return } CopyTime := c.GetString("CopyTime") copyTime, ok := lib.TimeStrToTime(CopyTime) CopySN := c.GetString("CopySN") CopyID := c.GetString("CopyID") if !ok { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"} c.ServeJSON() return } CopyEndTime := copyTime.Add(endTime.Sub(startTime)).Format("2006-01-02 15:04:05") // 时间间隔 分钟 T_saveT, _ := c.GetInt("T_saveT") SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|") if len(SN_List) != 1 { c.Data["json"] = lib.JSONS{202, "SN_List Err!", nil} c.ServeJSON() return } 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 } sn_id := strings.Split(SN_List[0], ",") if len(sn_id) != 2 { c.Data["json"] = lib.JSONS{202, "SN_List Err!", nil} c.ServeJSON() return } sn, id_str := sn_id[0], sn_id[1] List, _ := Task.Read_TaskData_ById_List_AES(Task_r.T_task_id, sn, id_str, StartTime, EndTime, 0, 9999) Task.DeleteTaskDataByTimeRange(Task_r.T_task_id, CopySN, CopyID, CopyTime, CopyEndTime) ct := copyTime go func(TaskDataList []Task.TaskData_, task_id string, T_saveT int) { for _, taskData := range TaskDataList { taskData.T_time = ct.Format("2006-01-02 15:04:05") taskData.T_sn = CopySN taskData.T_id = CopyID Task.InsertTaskData(task_id, taskData) ct = ct.Add(time.Second * time.Duration(T_saveT)) } }(List, Task_r.T_task_id, T_saveT) System.Add_UserLogs_T(T_uuid, "数据复制", fmt.Sprintf("数据复制(%s)[%s|%s],复制时间:%s,,时间间隔:%d", T_task_id, StartTime, EndTime, CopyTime, T_saveT), SN_List) c.Data["json"] = lib.JSONS{200, "数据复制已提交后台处理!", nil} c.ServeJSON() return } // DataRemoveDuplicates 数据去重 func (c *TaskDataController) DataRemoveDuplicates() { T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey")) 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 } // 删除重复数据 var count int64 count, err = Task.DeleteDeduplicate(T_task_id) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "导入失败"} c.ServeJSON() return } System.Add_UserLogs_T(T_uuid, "数据去重", fmt.Sprintf("数据平滑(%s)[%d]条", T_task_id, count), nil) c.Data["json"] = lib.JSONS{200, "操作成功处理" + fmt.Sprint(count) + "条数据", nil} c.ServeJSON() } // 数据偏移到区间内 func (c *TaskDataController) DataSkewingInterval() { T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey")) //if !b_ { // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"} // c.ServeJSON() // return //} StartTime := c.GetString("StartTime") _, ok := lib.TimeStrToTime(StartTime) if !ok { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"} c.ServeJSON() return } EndTime := c.GetString("EndTime") _, ok = lib.TimeStrToTime(EndTime) if !ok { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"} c.ServeJSON() return } TemperatureMin, _ := c.GetFloat("TemperatureMin") // 温度 TemperatureMax, _ := c.GetFloat("TemperatureMax") Tmin, Tmax := TemperatureMin, TemperatureMax SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|") 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 } errMsg := []string{} for _, v := range SN_List { sn_id := strings.Split(v, ",") if len(sn_id) != 2 { continue } sn, id := sn_id[0], sn_id[1] min, max := Task.Read_TaskData_T_Min_Max(Task_r.T_task_id, sn, id, StartTime, EndTime) if min < Tmin && max > Tmax { errMsg = append(errMsg, fmt.Sprintf("【%s】最低温度[%.2f]低于给定的最低温度[%.2f],并且最高温度[%.2f]高于给定的最高温度[%.2f],无法偏移!", id, min, Tmin, max, Tmax)) continue } if min > Tmin && max < Tmax { continue //errMsg = append(errMsg, fmt.Sprintf("【%s】最低温度[%.2f]高于给定最低温度[%.2f],并且最高温度[%.2f]低于给定最高温度[%.2f],无须偏移!", id, min, Tmin, max, Tmax)) } // 判断是否能向上偏移 if min < Tmin && max < Tmax && (Tmin-min+max) > Tmax { errMsg = append(errMsg, fmt.Sprintf("【%s】偏移后最高温度[%.2f]将高于给定的最高温度[%.2f],无法偏移!", id, Tmin-min+max, Tmax)) continue } if max > Tmax && min > Tmin && (min-(max-Tmax)) < Tmin { errMsg = append(errMsg, fmt.Sprintf("【%s】偏移后最低温度[%.2f]将低于给定的最低温度[%.2f],无法偏移!", id, min-(max-Tmax), Tmin)) continue } // 向上偏移 if min < Tmin && max < Tmax && (Tmin-min+max) <= Tmax { Task.UpdateTaskDataTemperatureAndHumidity(Task_r.T_task_id, sn, id, StartTime, EndTime, Tmin-min, 0) continue } // 向下偏移 if max > Tmax && min > Tmin && (min-(max-Tmax)) >= Tmin { Task.UpdateTaskDataTemperatureAndHumidity(Task_r.T_task_id, sn, id, StartTime, EndTime, -(max - Tmax), 0) continue } } System.Add_UserLogs_T(T_uuid, "数据偏移到给定范围", fmt.Sprintf("数据偏移到给定范围(%s)[%s|%s],温度[%f-%f]", T_task_id, StartTime, EndTime, TemperatureMin, TemperatureMax), SN_List) if len(errMsg) > 0 { c.Data["json"] = lib.JSONS{202, "", errMsg} c.ServeJSON() return } //反馈成功 c.Data["json"] = lib.JSONS{200, "调整区间偏移值成功!", nil} c.ServeJSON() return } // 等比缩放 func (c *TaskDataController) DataGeometricScale() { T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey")) //if !b_ { // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"} // c.ServeJSON() // return //} StartTime := c.GetString("StartTime") _, ok := lib.TimeStrToTime(StartTime) if !ok { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"} c.ServeJSON() return } EndTime := c.GetString("EndTime") _, ok = lib.TimeStrToTime(EndTime) if !ok { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"} c.ServeJSON() return } Temperature, _ := c.GetFloat("Temperature") // 温度比列 Humidity, _ := c.GetFloat("Humidity") // 适度比列 SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|") 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 } errMsg := []string{} for _, v := range SN_List { sn_id := strings.Split(v, ",") if len(sn_id) != 2 { continue } sn, id := sn_id[0], sn_id[1] Task.UpdateTaskDataTemperatureAndHumidityByGeometric(Task_r.T_task_id, sn, id, StartTime, EndTime, Temperature, Humidity) } System.Add_UserLogs_T(T_uuid, "数据等比缩放", fmt.Sprintf("数据等比缩放(%s)[%s|%s],温度[%f-%f]", T_task_id, StartTime, EndTime, Temperature, Humidity), SN_List) if len(errMsg) > 0 { c.Data["json"] = lib.JSONS{202, "", errMsg} c.ServeJSON() return } //反馈成功 c.Data["json"] = lib.JSONS{200, "数据等比缩放成功!", nil} c.ServeJSON() return } // DataBatchDelete 批量删除 func (c *TaskDataController) DataBatchDelete() { T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey")) //if !b_ { // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"} // c.ServeJSON() // return //} StartTime := c.GetString("StartTime") _, ok := lib.TimeStrToTime(StartTime) if !ok { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"} c.ServeJSON() return } EndTime := c.GetString("EndTime") _, ok = lib.TimeStrToTime(EndTime) if !ok { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"} c.ServeJSON() return } SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|") 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 } for _, v := range SN_List { sn_id := strings.Split(v, ",") if len(sn_id) != 2 { continue } sn, id_str := sn_id[0], sn_id[1] Task.DeleteTaskDataByTimeRange(Task_r.T_task_id, sn, id_str, StartTime, EndTime) } System.Add_UserLogs_T(T_uuid, "批量删除", fmt.Sprintf("批量删除(%s)[%s|%s]", T_task_id, StartTime, EndTime), SN_List) c.Data["json"] = lib.JSONS{200, "批量删除操作成功", nil} c.ServeJSON() return } // 存档列表 func (c *TaskDataController) TaskDataCopy_List() { var r_jsons lib.R_JSONS page, _ := c.GetInt("page") if page < 1 { page = 1 } page_z, _ := c.GetInt("page_z") if page_z < 1 { page_z = conf.Page_size } 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 } var cnt int64 List, cnt := Task.Read_TaskCopy_List(Task_r.T_task_id, page, page_z) page_size := math.Ceil(float64(cnt) / float64(page_z)) r_jsons.List = List r_jsons.Page = page r_jsons.Page_size = int(page_size) r_jsons.Pages = lib.Func_page(int64(page), int64(page_size)) r_jsons.Num = int(cnt) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } // 添加存档 func (c *TaskDataController) TaskDataCopy_Add() { 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 } var_ := Task.TaskCopy{ T_task_id: T_task_id, T_time: time.Now().Format("2006-01-02 15:04:05"), T_State: 1, } T_copy_id, is := Task.Add_TaskCopy(var_) if !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"} c.ServeJSON() return } // 创建数据表 Task.CREATE_TaskDataCopy(conf.Local_AliasName, T_copy_id) // 将数据表存档 Task.TaskData_Arhiving(T_task_id, T_copy_id) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_copy_id} c.ServeJSON() return } // 删除存档 func (c *TaskDataController) TaskDataCopy_Del() { T_copy_id := c.GetString("T_copy_id") r, err := Task.Read_TaskCopy(T_copy_id) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_copy_id 错误!"} c.ServeJSON() return } is := Task.Delete_TaskCopy(r) if !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"} c.ServeJSON() return } c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_copy_id} c.ServeJSON() return } // 存档恢复 func (c *TaskDataController) TaskDataCopy_Recover() { T_task_id := c.GetString("T_task_id") T_copy_id := c.GetString("T_copy_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 } _, err = Task.Read_TaskCopy(T_copy_id) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_copy_id 错误!"} c.ServeJSON() return } is := Task.TaskData_Recover(T_task_id, T_copy_id) if !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"} c.ServeJSON() return } c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_copy_id} c.ServeJSON() return } // 查询图片生成状态 func (c *TaskDataController) TaskData_JPGState() { T_task_id := c.GetString("T_task_id") jpg, is := Task.Redis_TaskDataJPG_Get(T_task_id) if !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "暂无图片正在生成"} c.ServeJSON() return } c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: jpg} c.ServeJSON() return } func (c *TaskDataController) TaskData_JPG() { StartTime := c.GetString("StartTime") if len(StartTime) > 0 { _, ok := lib.TimeStrToTime(StartTime) if !ok { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"} c.ServeJSON() return } } EndTime := c.GetString("EndTime") if len(EndTime) > 0 { _, ok := lib.TimeStrToTime(EndTime) if !ok { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"} c.ServeJSON() return } } TemperatureMin, _ := c.GetFloat("TemperatureMin") // 最低温度 TemperatureMax, _ := c.GetFloat("TemperatureMax") // 最高温度 T_task_id := c.GetString("T_task_id") SN_List := c.GetString("SN_List") 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 } Task.Redis_TaskDataJPG_Del(T_task_id) // 生成图片 go TaskDataJPG(StartTime, EndTime, T_task_id, SN_List, TemperatureMin, TemperatureMax) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } // 存档生成图片 func TaskDataJPG(StartTime, EndTime, T_task_id, snList string, TemperatureMin, TemperatureMax float64) { Task.Redis_TaskDataJPG_Set(T_task_id, Task.TaskDataJPG{ State: 1, Msg: "图片生成中", Url: "", }) msg := "" state := 2 url := "" SN_List := strings.Split(strings.Trim(snList, "|"), "|") if len(snList) == 0 { List := Task.Read_TaskData_ById_ClassList(T_task_id) for _, v := range List { SN_List = append(SN_List, fmt.Sprintf("%s,%s", v.T_sn, v.T_id)) } } id_List := []string{} sn_List := []string{} if len(snList) > 0 { for _, v := range SN_List { sn_id := strings.Split(v, ",") if len(sn_id) != 2 { return } id_List = append(id_List, fmt.Sprintf("'%s'", sn_id[1])) sn_List = append(sn_List, fmt.Sprintf("'%s'", sn_id[0])) } } ymin, ymax, minTime, maxTime := Task.Read_TaskData_T_Min_Max_Time_Min_Max(T_task_id, sn_List, id_List, StartTime, EndTime) xmin, xmax := float64(minTime.Unix()), float64(maxTime.Unix()) // 创建一个新的绘图 p := plot.New() // 设置绘图标题和标签 p.Title.Text = "温度折线图" //p.Legend.ThumbnailWidth = 5 * vg.Inch p.X.Label.Text = "时间" p.Y.Label.Text = "温度" // 添加最高,最低标准线 用红色虚线标识 p.Add(lib.HorizontalLine(xmin, xmax, TemperatureMin)) p.Add(lib.HorizontalLine(xmin, xmax, TemperatureMax)) var chData = make(chan int, 10) var jobGroup sync.WaitGroup // 创建温度线 for i := 0; i < len(SN_List); i++ { chData <- 1 jobGroup.Add(1) go func(index int) { //go func(index int, wg *sync.WaitGroup, p *plot.Plot) { defer func() { <-chData // 完成时chan取出1个 jobGroup.Done() // 完成时将等待组值减1 }() sn_id := strings.Split(SN_List[index], ",") if len(sn_id) != 2 { return } sn, id := sn_id[0], sn_id[1] list, _ := Task.Read_TaskData_ById_List_AES(T_task_id, sn, id, StartTime, EndTime, 0, 9999) if len(list) == 0 { return } pts := make(plotter.XYs, len(list)) for j, d := range list { t, _ := lib.TimeStrToTime(d.T_time) pts[j].X = float64(t.Unix()) pts[j].Y = float64(d.T_t) } line, err := plotter.NewLine(pts) if err != nil { return } line.Color = lib.RandomColor(index) p.Add(line) }(i) } jobGroup.Wait() if ymax < 8 { ymax = 8 } if ymin > 0 { ymin = 0 } p.Y.Min, p.Y.Max = ymin, ymax p.X.Min, p.X.Max = xmin, xmax p.Y.Tick.Marker = lib.CommaTicks{} //p.X.Tick.Marker = plot.TimeTicks{Format: "2006-01-02 15:04:05"} p.X.Tick.Marker = lib.TimeTicks{} p.X.Tick.Label.Rotation = math.Pi / 5 p.X.Tick.Label.YAlign = draw.YCenter p.X.Tick.Label.XAlign = draw.XRight filename := "jpg" + time.Now().Format("20060102150405") // 保存文件 if err := p.Save(10*vg.Inch, 4*vg.Inch, "ofile/"+filename+".jpg"); err != nil { state = 3 msg = "图片生成失败" logs.Error(lib.FuncName(), "生成图片失败", err) } if !lib.Pload_qiniu("ofile/"+filename+".jpg", "ofile/"+filename+".jpg") { state = 3 msg = "图片上传七牛云失败" logs.Error(lib.FuncName(), "上传七牛云失败") } //删除目录 os.Remove("ofile/" + filename + ".jpg") if len(msg) == 0 { msg = "图片生成成功" url = "https://bzdcoldverifyoss.baozhida.cn/" + "ofile/" + filename + ".jpg" } Task.Redis_TaskDataJPG_Set(T_task_id, Task.TaskDataJPG{ State: state, Msg: msg, Url: url, }) } func (c *TaskDataController) Certificate_List() { var r_jsons lib.R_JSONS T_task_id := c.GetString("T_task_id") T_sn := c.GetString("T_sn") page, _ := c.GetInt("page") if page < 1 { page = 1 } page_z, _ := c.GetInt("page_z") if page_z < 1 { page_z = conf.Page_size } 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 } List, cnt := Certificate.Read_Certificate_List(T_task_id, T_sn, page, page_z) r_jsons.List = List r_jsons.Page = page r_jsons.Page_size = page_z r_jsons.Num = int(cnt) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return }