123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724 |
- 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
- 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
- }
- 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
- }
|