1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728 |
- 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")
- err := c.TaskData_Pdf(Time_start, Time_end, T_task_id, T_snid, Task.Temperature)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
- c.ServeJSON()
- return
- }
- }
- 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")
- err := c.TaskData_Pdf(Time_start, Time_end, T_task_id, T_snid, Task.Humidity)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
- c.ServeJSON()
- return
- }
- }
- func (c *TaskDataController) TaskData_Pdf(Time_start, Time_end, T_task_id, T_snid, T_type string) (err error) {
- Time_start_b := Time_start
- Time_end_b := Time_end
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- return errors.New("T_task_id 错误!")
- }
- if Task_r.T_collection_state == 2 {
- return errors.New("数据采集中,请稍后!")
- }
- 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, "|"), "|")
- }
- 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", "", 10)
- if err != nil {
- return
- }
- dataMap := make(map[string]string)
- timeMap := make(map[string]bool)
- idWidthMap := make(map[string]float64)
- dataListMap := make(map[string][]Task.TaskData_)
- T_snid_list1 := []string{}
- T_snid_list2 := []string{}
- 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)
- if len(List) == 0 {
- continue
- }
- idw, _ := pdf.MeasureTextWidth(sn_id[1])
- if !lib.IsNumeric(sn_id[1]) {
- //IsNotNumeric = true
- T_snid_list1 = append(T_snid_list1, v)
- if len(Task_r.T_BindDeviceDataStartTime) > 0 {
- Time_start = Task_r.T_BindDeviceDataStartTime
- }
- if len(Task_r.T_BindDeviceDataEndTime) > 0 {
- Time_end = Task_r.T_BindDeviceDataEndTime
- }
- } else {
- T_snid_list2 = append(T_snid_list2, v)
- }
- if idw > 20 {
- idWidthMap[sn_id[1]] = idw + 12
- } else {
- idWidthMap[sn_id[1]] = 30.3
- }
- sort.Slice(List, func(i, j int) bool {
- return List[i].T_time < List[j].T_time
- })
- dataListMap[sn_id[1]] = List
- for _, data := range List {
- k := fmt.Sprintf("%s,%s", data.T_time, data.T_id)
- if T_type == Task.Temperature {
- dataMap[k] = fmt.Sprintf("%.1f", data.T_t)
- }
- if T_type == Task.Humidity {
- dataMap[k] = fmt.Sprintf("%.1f", data.T_rh)
- }
- if _, ok := timeMap[data.T_time]; !ok {
- timeMap[data.T_time] = true
- }
- }
- }
- Time_start = Time_start_b
- Time_end = Time_end_b
- }
- var timeList []string
- for k, _ := range timeMap {
- timeList = append(timeList, k)
- }
- sort.Slice(timeList, func(i, j int) bool {
- return timeList[i] < timeList[j]
- })
- 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
- T_type_name := ""
- if T_type == Task.Temperature {
- T_type_name = "温度"
- }
- if T_type == Task.Humidity {
- T_type_name = "湿度"
- }
- title := Task_r.T_name + T_type_name + "验证数据"
- textw, _ = pdf.MeasureTextWidth(title)
- pdf.SetX((595 / 2) - (textw / 2))
- pdf.SetY(y)
- pdf.Text(title)
- err = pdf.SetFont("wts", "", 12)
- if err != nil {
- return
- }
- 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
- sort.Slice(T_snid_list1, func(i, j int) bool {
- return extractSecondElement(T_snid_list1[i]) < extractSecondElement(T_snid_list1[j])
- })
- for _, v := range T_snid_list1 {
- sn_id := strings.Split(v, ",")
- id := sn_id[1]
- dataList2, _ := dataListMap[id]
- err = pdf.SetFont("wts", "", 15)
- if err != nil {
- return
- }
- var textH float64 = 20 // if text height is 25px.
- y += 20
- pdf.SetNewY(y, textH)
- y = pdf.GetY()
- if y > 790 {
- pdf.AddPage()
- y = 20
- }
- textw, _ = pdf.MeasureTextWidth(id)
- pdf.SetX((595 / 2) - (textw / 2))
- pdf.SetY(y)
- pdf.Text(id)
- y += 10
- err = pdf.SetFont("wts", "", 10)
- if err != nil {
- return
- }
- pdf.SetNewY(y, textH)
- y = pdf.GetY()
- if y < 790 {
- x = 10
- for i := 0; i < 4; i++ {
- w = 113.8
- lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x += w
- w = 30
- lib.RectFillColor(pdf, T_type_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x += w
- }
- y += 20
- } else {
- pdf.AddPage()
- y = 20
- x = 10
- for i := 0; i < 4; i++ {
- w = 113.8
- lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x += w
- w = 30
- lib.RectFillColor(pdf, T_type_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x += w
- }
- y += 20
- }
- for i := 0; i < len(dataList2); i++ {
- temp := i % 4
- x = 10 + 143.8*float64(temp)
- w = 113.8
- lib.RectFillColor(pdf, dataList2[i].T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x += w
- w = 30
- if T_type == Task.Temperature {
- lib.RectFillColor(pdf, fmt.Sprintf("%.1f", dataList2[i].T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- if T_type == Task.Humidity {
- lib.RectFillColor(pdf, fmt.Sprintf("%.1f", dataList2[i].T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- if temp == 3 {
- y += 20
- pdf.SetNewY(y, textH)
- y = pdf.GetY()
- // 每页添加表头
- if y == 20 {
- x = 10
- for k := 0; k < 4; k++ {
- w = 113.8
- lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x += w
- w = 30
- lib.RectFillColor(pdf, T_type_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x += w
- }
- y += 20
- }
- }
- }
- y += 25
- }
- y += 20
- sort.Slice(T_snid_list2, func(i, j int) bool {
- return extractSecondElement(T_snid_list2[i]) < extractSecondElement(T_snid_list2[j])
- })
- chunks := splitData(T_snid_list2, 454.5, idWidthMap)
- for _, list := range chunks {
- x = 10
- w = 120.7
- lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x += w
- w = 30.3
- for _, v2 := range list {
- sn_id2 := strings.Split(v2, ",")
- id2 := sn_id2[1]
- w = idWidthMap[id2]
- 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.7
- lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x += w
- w = 30.3
- for _, v2 := range list {
- sn_id2 := strings.Split(v2, ",")
- id2 := sn_id2[1]
- w = idWidthMap[id2]
- lib.RectFillColor(pdf, id2, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x += w
- }
- y += 20
- x = 10
- }
- w = 120.7
- lib.RectFillColor(pdf, t, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x += w
- w = 30.3
- for _, v := range list {
- sn_id := strings.Split(v, ",")
- id := sn_id[1]
- data := dataMap[fmt.Sprintf("%s,%s", t, id)]
- w = idWidthMap[id]
- lib.RectFillColor(pdf, data, 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/" + T_type_name + filename
- err = pdf.WritePdf(timeStr)
- if err != nil {
- return
- }
- defer func() {
- //删除目录
- os.Remove(timeStr)
- }()
- c.Ctx.Output.Download(timeStr)
- return nil
- }
- // Function to extract the second element after splitting by comma
- func extractSecondElement(s string) string {
- parts := strings.Split(s, ",")
- if len(parts) >= 2 {
- return parts[1]
- }
- return s // return original string if splitting doesn't give expected parts
- }
- 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 splitData(data []string, threshold float64, idWidthMap map[string]float64) [][]string {
- var result [][]string
- var currentBatch []string
- var currentSum float64
- for _, item := range data {
- sn_id := strings.Split(item, ",")
- if len(sn_id) == 2 {
- wd := idWidthMap[sn_id[1]]
- if currentSum+wd > threshold+0.1 {
- // 当前批次超过阈值,切分
- result = append(result, currentBatch)
- // 重置当前批次和当前总和
- currentBatch = []string{}
- currentSum = 0
- }
- currentBatch = append(currentBatch, item)
- currentSum += wd
- }
- }
- // 添加最后一批
- if len(currentBatch) > 0 {
- result = append(result, currentBatch)
- }
- return result
- }
- // 列表 -
- 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
- Task.UpdateAUTO_INCREMENT(conf.Local_AliasName,Task_r.T_task_id)
- 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
- }
- Task.UpdateAUTO_INCREMENT(conf.Local_AliasName,Task_r.T_task_id)
- // 获取上传的文件
- 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
- }
- Task.UpdateAUTO_INCREMENT(conf.Local_AliasName,Task_r.T_task_id)
- 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
- }
- exist := Task.CheckTableExist(conf.Local_AliasName, T_task_id)
- if !exist {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "本地数据不存在!"}
- 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
- }
|