TaskData.go 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724
  1. package controllers
  2. import (
  3. "ColdVerify_local/Nats"
  4. "ColdVerify_local/Nats/NatsServer"
  5. "ColdVerify_local/conf"
  6. "ColdVerify_local/lib"
  7. "ColdVerify_local/logs"
  8. "ColdVerify_local/models/Certificate"
  9. "ColdVerify_local/models/System"
  10. "ColdVerify_local/models/Task"
  11. "errors"
  12. "fmt"
  13. "github.com/beego/beego/v2/client/orm"
  14. beego "github.com/beego/beego/v2/server/web"
  15. "github.com/signintech/gopdf"
  16. "github.com/vmihailenco/msgpack/v5"
  17. "github.com/xuri/excelize/v2"
  18. "gonum.org/v1/plot"
  19. "gonum.org/v1/plot/plotter"
  20. "gonum.org/v1/plot/vg"
  21. "gonum.org/v1/plot/vg/draw"
  22. "math"
  23. "os"
  24. "sort"
  25. "strconv"
  26. "strings"
  27. "sync"
  28. "time"
  29. )
  30. type TaskDataController struct {
  31. beego.Controller
  32. }
  33. // 列表 -
  34. func (c *TaskDataController) TaskData_List() {
  35. var r_jsons lib.R_JSONS
  36. page, _ := c.GetInt("page")
  37. if page < 1 {
  38. page = 1
  39. }
  40. page_z, _ := c.GetInt("page_z")
  41. if page_z < 1 {
  42. page_z = conf.Page_size
  43. }
  44. Time_start := c.GetString("Time_start")
  45. Time_end := c.GetString("Time_end")
  46. T_sn := c.GetString("T_sn")
  47. T_id := c.GetString("T_id")
  48. T_task_id := c.GetString("T_task_id")
  49. Task_r, err := NatsServer.Read_Task(T_task_id)
  50. if err != nil {
  51. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  52. c.ServeJSON()
  53. return
  54. }
  55. if Task_r.T_collection_state == 2 {
  56. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  57. c.ServeJSON()
  58. return
  59. }
  60. dcList, err := NatsServer.Device_Class_List(T_task_id)
  61. if err != nil {
  62. c.Data["json"] = lib.JSONS{Code: 202, Msg: "获取设备列表失败!"}
  63. c.ServeJSON()
  64. return
  65. }
  66. // 保存布局编号和校准证书对应关系
  67. var deviceCertificateMap = make(map[string]string) // t_id, T_Certificate_sn
  68. for _, v := range dcList {
  69. deviceCertificateMap[v.T_id] = v.T_Certificate_sn
  70. }
  71. var cnt int64
  72. List, cnt := Task.Read_TaskData_ById_List(Task_r.T_task_id, T_sn, T_id, Time_start, Time_end, page, page_z)
  73. for i := 0; i < len(List); i++ {
  74. List[i].T_Certificate_sn = deviceCertificateMap[List[i].T_id]
  75. }
  76. page_size := math.Ceil(float64(cnt) / float64(page_z))
  77. r_jsons.List = List
  78. r_jsons.Page = page
  79. r_jsons.Page_size = int(page_size)
  80. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  81. r_jsons.Num = int(cnt)
  82. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  83. c.ServeJSON()
  84. return
  85. }
  86. func (c *TaskDataController) TaskData_Temperature_Pdf() {
  87. Time_start := c.GetString("Time_start")
  88. Time_end := c.GetString("Time_end")
  89. T_snid := c.GetString("T_snid")
  90. T_task_id := c.GetString("T_task_id")
  91. err := c.TaskData_Pdf(Time_start, Time_end, T_task_id, T_snid, Task.Temperature)
  92. if err != nil {
  93. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  94. c.ServeJSON()
  95. return
  96. }
  97. }
  98. func (c *TaskDataController) TaskData_Humidity_Pdf() {
  99. Time_start := c.GetString("Time_start")
  100. Time_end := c.GetString("Time_end")
  101. T_snid := c.GetString("T_snid")
  102. T_task_id := c.GetString("T_task_id")
  103. err := c.TaskData_Pdf(Time_start, Time_end, T_task_id, T_snid, Task.Humidity)
  104. if err != nil {
  105. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  106. c.ServeJSON()
  107. return
  108. }
  109. }
  110. func (c *TaskDataController) TaskData_Pdf(Time_start, Time_end, T_task_id, T_snid, T_type string) (err error) {
  111. Time_start_b := Time_start
  112. Time_end_b := Time_end
  113. Task_r, err := NatsServer.Read_Task(T_task_id)
  114. if err != nil {
  115. return errors.New("T_task_id 错误!")
  116. }
  117. if Task_r.T_collection_state == 2 {
  118. return errors.New("数据采集中,请稍后!")
  119. }
  120. user, err := NatsServer.Read_User(Task_r.T_uuid)
  121. var T_snid_list []string
  122. if len(T_snid) > 0 {
  123. T_snid_list = strings.Split(strings.Trim(T_snid, "|"), "|")
  124. }
  125. pdf := &gopdf.GoPdf{}
  126. pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
  127. err = pdf.AddTTFFont("wts", "static/fonts/MiSans-Medium.ttf")
  128. if err != nil {
  129. return
  130. }
  131. err = pdf.SetFont("wts", "", 10)
  132. if err != nil {
  133. return
  134. }
  135. dataMap := make(map[string]string)
  136. timeMap := make(map[string]bool)
  137. idWidthMap := make(map[string]float64)
  138. dataListMap := make(map[string][]Task.TaskData_)
  139. T_snid_list1 := []string{}
  140. T_snid_list2 := []string{}
  141. for _, v := range T_snid_list {
  142. sn_id := strings.Split(v, ",")
  143. if len(sn_id) == 2 {
  144. List, _ := Task.Read_TaskData_ById_List(Task_r.T_task_id, sn_id[0], sn_id[1], Time_start, Time_end, 0, 9999)
  145. if len(List) == 0 {
  146. continue
  147. }
  148. idw, _ := pdf.MeasureTextWidth(sn_id[1])
  149. if !lib.IsNumeric(sn_id[1]) {
  150. //IsNotNumeric = true
  151. T_snid_list1 = append(T_snid_list1, v)
  152. if len(Task_r.T_BindDeviceDataStartTime) > 0 {
  153. Time_start = Task_r.T_BindDeviceDataStartTime
  154. }
  155. if len(Task_r.T_BindDeviceDataEndTime) > 0 {
  156. Time_end = Task_r.T_BindDeviceDataEndTime
  157. }
  158. } else {
  159. T_snid_list2 = append(T_snid_list2, v)
  160. }
  161. if idw > 20 {
  162. idWidthMap[sn_id[1]] = idw + 12
  163. } else {
  164. idWidthMap[sn_id[1]] = 30.3
  165. }
  166. sort.Slice(List, func(i, j int) bool {
  167. return List[i].T_time < List[j].T_time
  168. })
  169. dataListMap[sn_id[1]] = List
  170. for _, data := range List {
  171. k := fmt.Sprintf("%s,%s", data.T_time, data.T_id)
  172. if T_type == Task.Temperature {
  173. dataMap[k] = fmt.Sprintf("%.1f", data.T_t)
  174. }
  175. if T_type == Task.Humidity {
  176. dataMap[k] = fmt.Sprintf("%.1f", data.T_rh)
  177. }
  178. if _, ok := timeMap[data.T_time]; !ok {
  179. timeMap[data.T_time] = true
  180. }
  181. }
  182. }
  183. Time_start = Time_start_b
  184. Time_end = Time_end_b
  185. }
  186. var timeList []string
  187. for k, _ := range timeMap {
  188. timeList = append(timeList, k)
  189. }
  190. sort.Slice(timeList, func(i, j int) bool {
  191. return timeList[i] < timeList[j]
  192. })
  193. err = pdf.SetFont("wts", "", 15)
  194. if err != nil {
  195. return
  196. }
  197. pdf.SetGrayFill(0.5)
  198. pdf.SetMargins(0, 20, 0, 20)
  199. pdf.AddPage()
  200. imgH, _ := gopdf.ImageHolderByPath("./ofile/logo.jpg")
  201. err = pdf.ImageByHolder(imgH, 10, 10, &gopdf.Rect{W: 93, H: 32})
  202. name := user.T_name
  203. var y float64 = 40
  204. textw, _ := pdf.MeasureTextWidth(name)
  205. pdf.SetX((595 / 2) - (textw / 2))
  206. pdf.SetY(y)
  207. pdf.Text(name)
  208. y += 20
  209. T_type_name := ""
  210. if T_type == Task.Temperature {
  211. T_type_name = "温度"
  212. }
  213. if T_type == Task.Humidity {
  214. T_type_name = "湿度"
  215. }
  216. title := Task_r.T_name + T_type_name + "验证数据"
  217. textw, _ = pdf.MeasureTextWidth(title)
  218. pdf.SetX((595 / 2) - (textw / 2))
  219. pdf.SetY(y)
  220. pdf.Text(title)
  221. err = pdf.SetFont("wts", "", 12)
  222. if err != nil {
  223. return
  224. }
  225. if len(timeList) > 0 {
  226. y += 20
  227. timeStr := fmt.Sprintf("%s - %s", timeList[0], timeList[len(timeList)-1])
  228. textw, _ = pdf.MeasureTextWidth(timeStr)
  229. pdf.SetX((595 / 2) - (textw / 2))
  230. pdf.SetY(y)
  231. pdf.Text(timeStr)
  232. }
  233. err = pdf.SetFont("wts", "", 10)
  234. if err != nil {
  235. return
  236. }
  237. y += 20
  238. var x float64 = 10
  239. var w float64 = 120
  240. sort.Slice(T_snid_list1, func(i, j int) bool {
  241. return extractSecondElement(T_snid_list1[i]) < extractSecondElement(T_snid_list1[j])
  242. })
  243. for _, v := range T_snid_list1 {
  244. sn_id := strings.Split(v, ",")
  245. id := sn_id[1]
  246. dataList2, _ := dataListMap[id]
  247. err = pdf.SetFont("wts", "", 15)
  248. if err != nil {
  249. return
  250. }
  251. var textH float64 = 20 // if text height is 25px.
  252. y += 20
  253. pdf.SetNewY(y, textH)
  254. y = pdf.GetY()
  255. if y > 790 {
  256. pdf.AddPage()
  257. y = 20
  258. }
  259. textw, _ = pdf.MeasureTextWidth(id)
  260. pdf.SetX((595 / 2) - (textw / 2))
  261. pdf.SetY(y)
  262. pdf.Text(id)
  263. y += 10
  264. err = pdf.SetFont("wts", "", 10)
  265. if err != nil {
  266. return
  267. }
  268. pdf.SetNewY(y, textH)
  269. y = pdf.GetY()
  270. if y < 790 {
  271. x = 10
  272. for i := 0; i < 4; i++ {
  273. w = 113.8
  274. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  275. x += w
  276. w = 30
  277. lib.RectFillColor(pdf, T_type_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  278. x += w
  279. }
  280. y += 20
  281. } else {
  282. pdf.AddPage()
  283. y = 20
  284. x = 10
  285. for i := 0; i < 4; i++ {
  286. w = 113.8
  287. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  288. x += w
  289. w = 30
  290. lib.RectFillColor(pdf, T_type_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  291. x += w
  292. }
  293. y += 20
  294. }
  295. for i := 0; i < len(dataList2); i++ {
  296. temp := i % 4
  297. x = 10 + 143.8*float64(temp)
  298. w = 113.8
  299. lib.RectFillColor(pdf, dataList2[i].T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  300. x += w
  301. w = 30
  302. if T_type == Task.Temperature {
  303. lib.RectFillColor(pdf, fmt.Sprintf("%.1f", dataList2[i].T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  304. }
  305. if T_type == Task.Humidity {
  306. lib.RectFillColor(pdf, fmt.Sprintf("%.1f", dataList2[i].T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  307. }
  308. if temp == 3 {
  309. y += 20
  310. pdf.SetNewY(y, textH)
  311. y = pdf.GetY()
  312. // 每页添加表头
  313. if y == 20 {
  314. x = 10
  315. for k := 0; k < 4; k++ {
  316. w = 113.8
  317. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  318. x += w
  319. w = 30
  320. lib.RectFillColor(pdf, T_type_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  321. x += w
  322. }
  323. y += 20
  324. }
  325. }
  326. }
  327. y += 25
  328. }
  329. y += 20
  330. sort.Slice(T_snid_list2, func(i, j int) bool {
  331. return extractSecondElement(T_snid_list2[i]) < extractSecondElement(T_snid_list2[j])
  332. })
  333. chunks := splitData(T_snid_list2, 454.5, idWidthMap)
  334. for _, list := range chunks {
  335. x = 10
  336. w = 120.7
  337. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  338. x += w
  339. w = 30.3
  340. for _, v2 := range list {
  341. sn_id2 := strings.Split(v2, ",")
  342. id2 := sn_id2[1]
  343. w = idWidthMap[id2]
  344. lib.RectFillColor(pdf, id2, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  345. x += w
  346. }
  347. y += 20
  348. for _, t := range timeList {
  349. x = 10
  350. var textH float64 = 20 // if text height is 25px.
  351. pdf.SetNewY(y, textH)
  352. y = pdf.GetY()
  353. if y == 20 {
  354. w = 120.7
  355. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  356. x += w
  357. w = 30.3
  358. for _, v2 := range list {
  359. sn_id2 := strings.Split(v2, ",")
  360. id2 := sn_id2[1]
  361. w = idWidthMap[id2]
  362. lib.RectFillColor(pdf, id2, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  363. x += w
  364. }
  365. y += 20
  366. x = 10
  367. }
  368. w = 120.7
  369. lib.RectFillColor(pdf, t, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  370. x += w
  371. w = 30.3
  372. for _, v := range list {
  373. sn_id := strings.Split(v, ",")
  374. id := sn_id[1]
  375. data := dataMap[fmt.Sprintf("%s,%s", t, id)]
  376. w = idWidthMap[id]
  377. lib.RectFillColor(pdf, data, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  378. x += w
  379. }
  380. y += 20
  381. }
  382. y += 20
  383. }
  384. filename := time.Now().Format("20060102150405") + ".pdf"
  385. timeStr := "ofile/" + T_type_name + filename
  386. err = pdf.WritePdf(timeStr)
  387. if err != nil {
  388. return
  389. }
  390. defer func() {
  391. //删除目录
  392. os.Remove(timeStr)
  393. }()
  394. c.Ctx.Output.Download(timeStr)
  395. return nil
  396. }
  397. // Function to extract the second element after splitting by comma
  398. func extractSecondElement(s string) string {
  399. parts := strings.Split(s, ",")
  400. if len(parts) >= 2 {
  401. return parts[1]
  402. }
  403. return s // return original string if splitting doesn't give expected parts
  404. }
  405. func chunkBy[T any](list []T, size int) [][]T {
  406. var chunks [][]T
  407. for size < len(list) {
  408. list, chunks = list[size:], append(chunks, list[0:size:size])
  409. }
  410. return append(chunks, list)
  411. }
  412. func splitData(data []string, threshold float64, idWidthMap map[string]float64) [][]string {
  413. var result [][]string
  414. var currentBatch []string
  415. var currentSum float64
  416. for _, item := range data {
  417. sn_id := strings.Split(item, ",")
  418. if len(sn_id) == 2 {
  419. wd := idWidthMap[sn_id[1]]
  420. if currentSum+wd > threshold+0.1 {
  421. // 当前批次超过阈值,切分
  422. result = append(result, currentBatch)
  423. // 重置当前批次和当前总和
  424. currentBatch = []string{}
  425. currentSum = 0
  426. }
  427. currentBatch = append(currentBatch, item)
  428. currentSum += wd
  429. }
  430. }
  431. // 添加最后一批
  432. if len(currentBatch) > 0 {
  433. result = append(result, currentBatch)
  434. }
  435. return result
  436. }
  437. // 列表 -
  438. func (c *TaskDataController) TaskDataClass_List() {
  439. page, _ := c.GetInt("page")
  440. if page < 1 {
  441. page = 1
  442. }
  443. page_z, _ := c.GetInt("page_z")
  444. if page_z < 1 {
  445. page_z = conf.Page_size
  446. }
  447. T_task_id := c.GetString("T_task_id")
  448. Task_r, err := NatsServer.Read_Task(T_task_id)
  449. if err != nil {
  450. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  451. c.ServeJSON()
  452. return
  453. }
  454. if Task_r.T_collection_state == 2 {
  455. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  456. c.ServeJSON()
  457. return
  458. }
  459. List := Task.Read_TaskData_ById_ClassList(Task_r.T_task_id)
  460. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: List}
  461. c.ServeJSON()
  462. return
  463. }
  464. func (c *TaskDataController) TaskDataClass_Edit() {
  465. T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  466. T_task_id := c.GetString("T_task_id")
  467. T_sn := c.GetString("T_sn")
  468. T_id := c.GetString("T_id")
  469. Task_r, err := NatsServer.Read_Task(T_task_id)
  470. if err != nil {
  471. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  472. c.ServeJSON()
  473. return
  474. }
  475. sn, err := Task.Read_TaskData_ByT_id(Task_r.T_task_id, T_id)
  476. if err != nil && !errors.Is(err, orm.ErrNoRows) {
  477. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  478. c.ServeJSON()
  479. return
  480. }
  481. if len(sn) > 0 && sn != T_sn {
  482. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("编号[%s]已被sn[%s]关联,请修改后重试!", T_id, sn)}
  483. c.ServeJSON()
  484. return
  485. }
  486. err = NatsServer.Edit_DeviceClassList(T_task_id, T_sn, T_id)
  487. if err != nil {
  488. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  489. c.ServeJSON()
  490. return
  491. }
  492. err = Task.Update_TaskData_ByT_sn(Task_r.T_task_id, T_sn, T_id)
  493. if err != nil {
  494. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  495. c.ServeJSON()
  496. return
  497. }
  498. System.Add_UserLogs_T(T_uuid, "本地版-任务分裂", "修改分类数据"+Task_r.T_name, Task_r.T_task_id+"|"+T_sn+"|"+T_id)
  499. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  500. c.ServeJSON()
  501. return
  502. }
  503. func (c *TaskDataController) TaskDataClass_Del() {
  504. T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  505. T_task_id := c.GetString("T_task_id")
  506. T_sn := c.GetString("T_sn")
  507. Task_r, err := NatsServer.Read_Task(T_task_id)
  508. if err != nil {
  509. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  510. c.ServeJSON()
  511. return
  512. }
  513. err = Task.Delete_TaskData_ByT_sn(Task_r.T_task_id, T_sn)
  514. if err != nil {
  515. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  516. c.ServeJSON()
  517. return
  518. }
  519. System.Add_UserLogs_T(T_uuid, "本地版-任务分类", "删除分类数据"+Task_r.T_name, Task_r.T_task_id+"|"+T_sn)
  520. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  521. c.ServeJSON()
  522. return
  523. }
  524. // 添加-
  525. func (c *TaskDataController) TaskData_AddS() {
  526. // 获取登录用户的uuid
  527. T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  528. T_task_id := c.GetString("T_task_id")
  529. Task_r, err := NatsServer.Read_Task(T_task_id)
  530. if err != nil {
  531. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  532. c.ServeJSON()
  533. return
  534. }
  535. //T_sn|T_id|T_t|T_rh|T_time?
  536. T_Data := c.GetString("T_Data")
  537. if len(T_Data) < 5 {
  538. c.Data["json"] = lib.JSONS{Code: 201, Msg: "err T_Data!"}
  539. c.ServeJSON()
  540. return
  541. }
  542. T_Data_list := strings.Split(T_Data, "?")
  543. println(len(T_Data_list), "len(T_Data_list)")
  544. var T_Data_list_x = 0
  545. snMaps := make(map[string]string)
  546. for _, v := range T_Data_list {
  547. // 132|132|23.9|72.1|2023-04-30 07:03:00
  548. if len(v) < 5 {
  549. println(v, "len(v) < 5")
  550. continue
  551. }
  552. v_list := strings.Split(v, "|")
  553. id, sn := strings.Trim(v_list[1], " "), strings.Trim(v_list[0], " ")
  554. sn1, ok := snMaps[id]
  555. if !ok {
  556. snMaps[id] = sn
  557. } else if sn1 != sn {
  558. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("编号[%s]已被sn[%s]关联,请修改后重试!", id, sn1)}
  559. c.ServeJSON()
  560. return
  561. }
  562. }
  563. for T_id, T_sn := range snMaps {
  564. sn, err := Task.Read_TaskData_ByT_id(Task_r.T_task_id, T_id)
  565. if err != nil && !errors.Is(err, orm.ErrNoRows) {
  566. continue
  567. }
  568. if len(sn) > 0 && sn != T_sn {
  569. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("编号[%s]已被sn[%s]关联,请修改后重试!", T_id, sn)}
  570. c.ServeJSON()
  571. return
  572. }
  573. id, err := Task.Read_TaskData_ByT_sn(Task_r.T_task_id, T_sn)
  574. if err != nil && !errors.Is(err, orm.ErrNoRows) {
  575. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  576. c.ServeJSON()
  577. return
  578. }
  579. if len(id) > 0 && id != T_id {
  580. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("sn[%s]已被编号[%s]关联,请修改后重试!", T_sn, id)}
  581. c.ServeJSON()
  582. return
  583. }
  584. // 同步设备到线上
  585. err = NatsServer.Add_DeviceClassList(T_task_id, T_sn, T_id)
  586. if err != nil {
  587. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  588. c.ServeJSON()
  589. return
  590. }
  591. }
  592. for _, v := range T_Data_list {
  593. // 132|132|23.9|72.1|2023-04-30 07:03:00
  594. if len(v) < 5 {
  595. println(v, "len(v) < 5")
  596. continue
  597. }
  598. v_list := strings.Split(v, "|")
  599. t, _ := lib.ReplaceSeconds(v_list[4])
  600. is := Task.Add_TaskData(Task_r.T_task_id, v_list[0], v_list[1], v_list[2], v_list[3], t)
  601. if is {
  602. T_Data_list_x += 1
  603. }
  604. }
  605. 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)
  606. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: strconv.Itoa(T_Data_list_x)}
  607. c.ServeJSON()
  608. return
  609. }
  610. func (c *TaskDataController) TaskData_AddS_Excel() {
  611. // 获取登录用户的uuid
  612. T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  613. T_task_id := c.GetString("T_task_id")
  614. Task_r, err := NatsServer.Read_Task(T_task_id)
  615. if err != nil {
  616. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  617. c.ServeJSON()
  618. return
  619. }
  620. // 获取上传的文件
  621. file, _, err := c.GetFile("file")
  622. if err != nil {
  623. c.Data["json"] = lib.JSONS{Code: 202, Msg: "文件上传失败!"}
  624. c.ServeJSON()
  625. return
  626. }
  627. // 解析 Excel 文件
  628. xlFile, err := excelize.OpenReader(file)
  629. if err != nil {
  630. c.Data["json"] = lib.JSONS{Code: 202, Msg: "文件解析失败!"}
  631. c.ServeJSON()
  632. return
  633. }
  634. rows, err := xlFile.GetRows(xlFile.GetSheetName(0))
  635. if err != nil {
  636. c.Data["json"] = lib.JSONS{Code: 202, Msg: "获取工作表失败!"}
  637. c.ServeJSON()
  638. return
  639. }
  640. var T_Data_list_x int
  641. snMaps := make(map[string]string)
  642. var valueStrings []string
  643. for k, val := range rows {
  644. // 跳过第一行
  645. if k == 0 {
  646. continue
  647. }
  648. if len(val) < 5 {
  649. println(val, "len(v) < 5")
  650. continue
  651. }
  652. fmt.Println(val)
  653. id, sn := val[1], val[0]
  654. sn1, ok := snMaps[id]
  655. if !ok {
  656. snMaps[id] = sn
  657. } else if sn1 != sn {
  658. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("编号[%s]已被sn[%s]关联,请修改后重试!", id, sn1)}
  659. c.ServeJSON()
  660. return
  661. }
  662. t, _ := lib.ReplaceSeconds(val[4])
  663. valueStrings = append(valueStrings, fmt.Sprintf("('%s','%s',%v,%v,'%s')", val[0], val[1], val[2], val[3], t))
  664. //T_Data_list_x += 1
  665. }
  666. for T_id, T_sn := range snMaps {
  667. sn, err := Task.Read_TaskData_ByT_id(Task_r.T_task_id, T_id)
  668. if err != nil && !errors.Is(err, orm.ErrNoRows) {
  669. continue
  670. }
  671. if len(sn) > 0 && sn != T_sn {
  672. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("编号[%s]已被sn[%s]关联,请修改后重试!", T_id, sn)}
  673. c.ServeJSON()
  674. return
  675. }
  676. id, err := Task.Read_TaskData_ByT_sn(Task_r.T_task_id, T_sn)
  677. if err != nil && !errors.Is(err, orm.ErrNoRows) {
  678. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  679. c.ServeJSON()
  680. return
  681. }
  682. if len(id) > 0 && id != T_id {
  683. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("sn[%s]已被编号[%s]关联,请修改后重试!", T_sn, id)}
  684. c.ServeJSON()
  685. return
  686. }
  687. }
  688. // 删除重复数据
  689. _, err = Task.DeleteDeduplicate(T_task_id)
  690. if err != nil {
  691. c.Data["json"] = lib.JSONS{Code: 202, Msg: "导入失败"}
  692. c.ServeJSON()
  693. return
  694. }
  695. // 创建唯一索引
  696. err = Task.Create_Unique_Index(T_task_id)
  697. if err != nil {
  698. c.Data["json"] = lib.JSONS{Code: 202, Msg: "导入失败"}
  699. c.ServeJSON()
  700. return
  701. }
  702. pageSize := 10000 // 每页的条数
  703. totalPages := len(valueStrings) / pageSize // 总页数
  704. // 遍历每一页
  705. for page := 1; page <= totalPages+1; page++ {
  706. // 计算当前页的起始索引和结束索引
  707. startIndex := (page - 1) * pageSize
  708. if startIndex > len(valueStrings) {
  709. startIndex = len(valueStrings)
  710. }
  711. endIndex := page * pageSize
  712. // 边界判断,如果结束索引超过列表长度,则将结束索引设置为列表最后一个元素的索引加一
  713. if endIndex > len(valueStrings) {
  714. endIndex = len(valueStrings)
  715. }
  716. fmt.Printf("startIndex %d endIndex %d\n", startIndex, endIndex)
  717. // 获取当前页的数据
  718. currentList := valueStrings[startIndex:endIndex]
  719. err = Task.Adds_TaskData(T_task_id, currentList)
  720. if err == nil {
  721. T_Data_list_x += len(currentList)
  722. }
  723. }
  724. //err = Task.Adds_TaskData(T_task_id, valueStrings)
  725. // 删除唯一索引
  726. Task.Delete_Unique_Index(T_task_id)
  727. System.Add_UserLogs_T(T_uuid, "本地版-任务数据", "添加数据"+Task_r.T_name, Task_r.T_task_id+"结果:"+strconv.Itoa(T_Data_list_x))
  728. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: strconv.Itoa(T_Data_list_x)}
  729. c.ServeJSON()
  730. return
  731. }
  732. // 添加-
  733. func (c *TaskDataController) TaskData_Add() {
  734. // 获取登录用户的uuid
  735. T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  736. T_sn := c.GetString("T_sn")
  737. T_id := c.GetString("T_id")
  738. T_t, _ := c.GetFloat("T_t")
  739. T_rh, _ := c.GetFloat("T_rh")
  740. T_time := c.GetString("T_time")
  741. if len(T_sn) == 0 {
  742. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn 不能为空!"}
  743. c.ServeJSON()
  744. return
  745. }
  746. if len(T_id) == 0 {
  747. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id 不能为空!"}
  748. c.ServeJSON()
  749. return
  750. }
  751. T_task_id := c.GetString("T_task_id")
  752. Task_r, err := NatsServer.Read_Task(T_task_id)
  753. if err != nil {
  754. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  755. c.ServeJSON()
  756. return
  757. }
  758. sn, err := Task.Read_TaskData_ByT_id(Task_r.T_task_id, T_id)
  759. if err != nil && !errors.Is(err, orm.ErrNoRows) {
  760. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  761. c.ServeJSON()
  762. return
  763. }
  764. if len(sn) > 0 && sn != T_sn {
  765. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("编号[%s]已被sn[%s]关联,请修改后重试!", T_id, sn)}
  766. c.ServeJSON()
  767. return
  768. }
  769. id, err := Task.Read_TaskData_ByT_sn(Task_r.T_task_id, T_sn)
  770. if err != nil && !errors.Is(err, orm.ErrNoRows) {
  771. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  772. c.ServeJSON()
  773. return
  774. }
  775. if len(id) > 0 && id != T_id {
  776. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("sn[%s]已被编号[%s]关联,请修改后重试!", T_sn, id)}
  777. c.ServeJSON()
  778. return
  779. }
  780. // 同步设备到线上
  781. if T_t == 0 && T_rh == 0 {
  782. err = NatsServer.Add_DeviceClassList(T_task_id, T_sn, T_id)
  783. if err != nil {
  784. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  785. c.ServeJSON()
  786. return
  787. }
  788. }
  789. 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)
  790. if !is {
  791. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  792. c.ServeJSON()
  793. return
  794. }
  795. 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)
  796. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  797. c.ServeJSON()
  798. return
  799. }
  800. // 修改-
  801. func (c *TaskDataController) TaskData_Up() {
  802. // 获取登录用户的uuid
  803. T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  804. Id, err := c.GetInt("Id")
  805. if err != nil {
  806. c.Data["json"] = lib.JSONS{Code: 201, Msg: "Id 错误!"}
  807. c.ServeJSON()
  808. return
  809. }
  810. T_t, err := c.GetFloat("T_t")
  811. if err != nil {
  812. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_t 错误!"}
  813. c.ServeJSON()
  814. return
  815. }
  816. T_rh, err := c.GetFloat("T_rh")
  817. if err != nil {
  818. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_rh 错误!"}
  819. c.ServeJSON()
  820. return
  821. }
  822. T_time := c.GetString("T_time")
  823. T_task_id := c.GetString("T_task_id")
  824. Task_r, err := NatsServer.Read_Task(T_task_id)
  825. if err != nil {
  826. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  827. c.ServeJSON()
  828. return
  829. }
  830. is := Task.Up_TaskData(Task_r.T_task_id, strconv.Itoa(Id), fmt.Sprintf("%.2f", T_t), fmt.Sprintf("%.2f", T_rh), T_time)
  831. if !is {
  832. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  833. c.ServeJSON()
  834. return
  835. }
  836. 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)
  837. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  838. c.ServeJSON()
  839. return
  840. }
  841. // 删除-
  842. func (c *TaskDataController) TaskData_Del() {
  843. // 获取登录用户的uuid
  844. T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  845. Id, err := c.GetInt("Id")
  846. if err != nil {
  847. c.Data["json"] = lib.JSONS{Code: 201, Msg: "Id 错误!"}
  848. c.ServeJSON()
  849. return
  850. }
  851. T_task_id := c.GetString("T_task_id")
  852. Task_r, err := NatsServer.Read_Task(T_task_id)
  853. if err != nil {
  854. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  855. c.ServeJSON()
  856. return
  857. }
  858. is := Task.Del_TaskData(Task_r.T_task_id, strconv.Itoa(Id))
  859. if !is {
  860. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  861. c.ServeJSON()
  862. return
  863. }
  864. System.Add_UserLogs_T(T_uuid, "本地版-任务数据", "删除"+Task_r.T_name, Task_r.T_task_id+"|"+strconv.Itoa(Id))
  865. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  866. c.ServeJSON()
  867. return
  868. }
  869. // 删除-
  870. func (c *TaskDataController) TaskData_Del_t_id() {
  871. // 获取登录用户的uuid
  872. T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  873. Id := c.GetString("Id")
  874. T_task_id := c.GetString("T_task_id")
  875. Task_r, err := NatsServer.Read_Task(T_task_id)
  876. if err != nil {
  877. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  878. c.ServeJSON()
  879. return
  880. }
  881. is := Task.Del_TaskData_t_id(Task_r.T_task_id, Id)
  882. if !is {
  883. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  884. c.ServeJSON()
  885. return
  886. }
  887. System.Add_UserLogs_T(T_uuid, "本地版-任务数据", "删除"+Task_r.T_name, Task_r.T_task_id+"|"+Id)
  888. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  889. c.ServeJSON()
  890. return
  891. }
  892. // 列表 - 接口
  893. func (c *TaskDataController) Export_Data_Excel() {
  894. Time_start := c.GetString("Time_start")
  895. Time_end := c.GetString("Time_end")
  896. T_sn_str := c.GetString("T_sn_list") //865901058809339,865901058815849,865901058818991,865901058810568
  897. //T_id, err := c.GetInt("T_id")
  898. //if err != nil {
  899. // T_id = -1
  900. //
  901. //}
  902. T_task_id := c.GetString("T_task_id")
  903. Task_r, err := NatsServer.Read_Task(T_task_id)
  904. if err != nil {
  905. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  906. c.ServeJSON()
  907. return
  908. }
  909. if Task_r.T_collection_state == 2 {
  910. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  911. c.ServeJSON()
  912. return
  913. }
  914. T_sn_list := strings.Split(T_sn_str, ",")
  915. DeviceSensor_data_list := []Task.TaskData_{}
  916. for _, v := range T_sn_list {
  917. DeviceSensor_data, _ := Task.Read_TaskData_ById_List(Task_r.T_task_id, v, "", Time_start, Time_end, 1, 9999)
  918. DeviceSensor_data_list = append(DeviceSensor_data_list, DeviceSensor_data...)
  919. }
  920. f := excelize.NewFile() // 设置单元格的值
  921. // 这里设置表头
  922. f.SetCellValue("Sheet1", "A1", "编号")
  923. f.SetCellValue("Sheet1", "B1", "SN")
  924. f.SetCellValue("Sheet1", "C1", "温度℃")
  925. f.SetCellValue("Sheet1", "D1", "湿度%")
  926. f.SetCellValue("Sheet1", "E1", "记录时间")
  927. // 设置列宽
  928. f.SetColWidth("Sheet1", "A", "A", 7)
  929. f.SetColWidth("Sheet1", "B", "B", 20)
  930. f.SetColWidth("Sheet1", "C", "C", 10)
  931. f.SetColWidth("Sheet1", "D", "D", 10)
  932. f.SetColWidth("Sheet1", "E", "E", 22)
  933. line := 1
  934. // 循环写入数据
  935. for _, v := range DeviceSensor_data_list {
  936. line++
  937. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), v.T_id)
  938. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_sn)
  939. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), strconv.FormatFloat(float64(v.T_t), 'f', 1, 64))
  940. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), strconv.FormatFloat(float64(v.T_rh), 'f', 1, 64))
  941. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_time)
  942. }
  943. fmt.Println("DeviceSensor_data:", len(DeviceSensor_data_list))
  944. lib.Create_Dir("./ofile")
  945. timeStr := time.Now().Format("20060102150405")
  946. // 保存文件
  947. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  948. fmt.Println(err)
  949. }
  950. if !lib.Pload_qiniu("ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx") {
  951. c.Data["json"] = lib.JSONS{Code: 203, Msg: "oss!"}
  952. c.ServeJSON()
  953. return
  954. }
  955. //删除目录
  956. err = os.Remove("ofile/" + timeStr + ".xlsx")
  957. if err != nil {
  958. fmt.Println(err)
  959. }
  960. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: "https://bzdcoldverifyoss.baozhida.cn/" + "ofile/" + timeStr + ".xlsx"}
  961. c.ServeJSON()
  962. return
  963. }
  964. func (c *TaskDataController) Check() {
  965. T_task_id := c.GetString("T_task_id")
  966. Task_r, err := NatsServer.Read_Task(T_task_id)
  967. if Task_r.T_collection_state == 2 {
  968. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  969. c.ServeJSON()
  970. return
  971. }
  972. if err != nil {
  973. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  974. c.ServeJSON()
  975. return
  976. }
  977. type R_JSONS struct {
  978. T_sn string // SN
  979. T_id string // 编号
  980. T_unm int // 数据量
  981. T_time_interval int64 // 时间间隔
  982. Time_start string // 开始
  983. Time_end string // 结束
  984. Result int // 200 OK, 201 可以补 , 202 不能补
  985. Result_str string // 提示内容
  986. Result_Time_start string // 2022-8-05 21:01
  987. Result_Time_defect int64 // 缺失时间间隔
  988. }
  989. var r_jsons []R_JSONS
  990. List := Task.Read_TaskData_ById_ClassList(Task_r.T_task_id)
  991. for _, v := range List {
  992. var r_json R_JSONS
  993. r_json.T_sn = v.T_sn
  994. r_json.T_id = v.T_id
  995. r_json.Result = 200
  996. DeviceSensor_data := Task.Read_TaskData_ById_List_(Task_r.T_task_id, v.T_sn)
  997. r_json.T_unm = len(DeviceSensor_data)
  998. if r_json.T_unm > 2 {
  999. r_json.T_id = DeviceSensor_data[0].T_id
  1000. r_json.Time_end = DeviceSensor_data[len(DeviceSensor_data)-1].T_time
  1001. r_json.Time_start = DeviceSensor_data[0].T_time
  1002. formatTime_a, _ := time.Parse("2006-1-2 15:04", DeviceSensor_data[0].T_time)
  1003. formatTime_b, _ := time.Parse("2006-1-2 15:04", DeviceSensor_data[1].T_time)
  1004. formatTime_x := formatTime_b.Unix() - formatTime_a.Unix()
  1005. r_json.T_time_interval = formatTime_x
  1006. println("formatTime_x:", v.T_id, v.T_sn, formatTime_x)
  1007. if formatTime_x > 60*30 || formatTime_x < 60 {
  1008. r_json.Result = 202
  1009. r_json.Result_str = "数据异常(数据的第一个时间与第二个时间相差 " + strconv.FormatInt(formatTime_x, 10) + "秒),必须大于60秒,小于30分钟"
  1010. r_jsons = append(r_jsons, r_json)
  1011. continue
  1012. }
  1013. for data_i := 2; data_i < len(DeviceSensor_data); data_i++ {
  1014. formatTime_a = formatTime_b
  1015. formatTime_b, _ = time.Parse("2006-1-2 15:04", DeviceSensor_data[data_i].T_time)
  1016. formatTime_ := formatTime_b.Unix() - formatTime_a.Unix()
  1017. println("formatTime_x-:", formatTime_)
  1018. if formatTime_ < formatTime_x {
  1019. r_json.Result = 202
  1020. r_json.Result_str = "开始时间:" + DeviceSensor_data[data_i-1].T_time + " 离下一条时间间隔:" + strconv.FormatInt(formatTime_, 10) + "秒,间隔小于时间间隔,不能自动补充"
  1021. break
  1022. }
  1023. if formatTime_x != formatTime_ {
  1024. if formatTime_ > 60*30 {
  1025. r_json.Result = 202
  1026. r_json.Result_str = "开始时间:" + DeviceSensor_data[data_i-1].T_time + " 离下一条时间间隔:" + strconv.FormatInt(formatTime_, 10) + "秒,相差时间大于30分钟,不能自动补充"
  1027. break
  1028. }
  1029. r_json.Result = 201
  1030. r_json.Result_str = "开始时间:" + DeviceSensor_data[data_i-1].T_time + " 离下一条时间间隔:" + strconv.FormatInt(formatTime_, 10) + "秒"
  1031. r_json.Result_Time_start = DeviceSensor_data[data_i-1].T_time
  1032. r_json.Result_Time_defect = formatTime_
  1033. println(r_json.Result_str)
  1034. break
  1035. }
  1036. }
  1037. } else {
  1038. r_json.Result = 202
  1039. r_json.Result_str = "数据量太少 必须大于 2条以上!"
  1040. }
  1041. r_jsons = append(r_jsons, r_json)
  1042. }
  1043. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1044. c.ServeJSON()
  1045. return
  1046. }
  1047. func (c *TaskDataController) Check_Asyn() {
  1048. T_task_id := c.GetString("T_task_id")
  1049. Task_r, err := NatsServer.Read_Task(T_task_id)
  1050. if Task_r.T_collection_state == 2 {
  1051. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  1052. c.ServeJSON()
  1053. return
  1054. }
  1055. if err != nil {
  1056. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  1057. c.ServeJSON()
  1058. return
  1059. }
  1060. type R_JSONS struct {
  1061. T_sn string // SN
  1062. T_id string // 编号
  1063. T_unm int // 数据量
  1064. T_time_interval int64 // 时间间隔
  1065. Time_start string // 开始
  1066. Time_end string // 结束
  1067. Result int // 200 OK, 201 可以补 , 202 不能补
  1068. Result_str string // 提示内容
  1069. Result_Time_start string // 2022-8-05 21:01
  1070. Result_Time_defect int64 // 缺失时间间隔
  1071. }
  1072. List := Task.Read_TaskData_ById_ClassList(Task_r.T_task_id)
  1073. r_jsons := make([]R_JSONS, len(List))
  1074. var limitMaxNum = 10
  1075. var chData = make(chan int, limitMaxNum)
  1076. var jobGroup sync.WaitGroup
  1077. var tasknum = len(List)
  1078. for i := 0; i < tasknum; i++ {
  1079. chData <- 1
  1080. jobGroup.Add(1)
  1081. go func(index int, wg *sync.WaitGroup, res *[]R_JSONS) {
  1082. defer wg.Done()
  1083. var r_json R_JSONS
  1084. r_json.T_sn = List[index].T_sn
  1085. r_json.T_id = List[index].T_id
  1086. r_json.Result = 200
  1087. DeviceSensor_data := Task.Read_TaskData_ById_List_(Task_r.T_task_id, List[index].T_sn)
  1088. r_json.T_unm = len(DeviceSensor_data)
  1089. if r_json.T_unm > 2 {
  1090. r_json.T_id = DeviceSensor_data[0].T_id
  1091. r_json.Time_end = DeviceSensor_data[len(DeviceSensor_data)-1].T_time
  1092. r_json.Time_start = DeviceSensor_data[0].T_time
  1093. formatTime_a, _ := time.Parse("2006-1-2 15:04", DeviceSensor_data[0].T_time)
  1094. formatTime_b, _ := time.Parse("2006-1-2 15:04", DeviceSensor_data[1].T_time)
  1095. formatTime_x := formatTime_b.Unix() - formatTime_a.Unix()
  1096. r_json.T_time_interval = formatTime_x
  1097. println("formatTime_x:", List[index].T_id, List[index].T_sn, formatTime_x)
  1098. if formatTime_x > 60*30 || formatTime_x < 60 {
  1099. r_json.Result = 202
  1100. r_json.Result_str = "数据异常(数据的第一个时间与第二个时间相差 " + strconv.FormatInt(formatTime_x, 10) + "秒),必须大于60秒,小于30分钟"
  1101. *res = append(*res, r_json)
  1102. <-chData
  1103. return
  1104. }
  1105. for data_i := 2; data_i < len(DeviceSensor_data); data_i++ {
  1106. formatTime_a = formatTime_b
  1107. formatTime_b, _ = time.Parse("2006-1-2 15:04", DeviceSensor_data[data_i].T_time)
  1108. formatTime_ := formatTime_b.Unix() - formatTime_a.Unix()
  1109. if formatTime_ < formatTime_x {
  1110. println("formatTime_x-:", formatTime_)
  1111. r_json.Result = 202
  1112. r_json.Result_str = "开始时间:" + DeviceSensor_data[data_i-1].T_time + " 离下一条时间间隔:" + strconv.FormatInt(formatTime_, 10) + "秒,间隔小于时间间隔,不能自动补充"
  1113. break
  1114. }
  1115. if formatTime_x != formatTime_ {
  1116. if formatTime_ > 60*30 {
  1117. println("formatTime_x-:", formatTime_)
  1118. r_json.Result = 202
  1119. r_json.Result_str = "开始时间:" + DeviceSensor_data[data_i-1].T_time + " 离下一条时间间隔:" + strconv.FormatInt(formatTime_, 10) + "秒,相差时间大于30分钟,不能自动补充"
  1120. break
  1121. }
  1122. r_json.Result = 202
  1123. r_json.Result_str = "开始时间:" + DeviceSensor_data[data_i-1].T_time + " 离下一条时间间隔:" + strconv.FormatInt(formatTime_, 10) + "秒"
  1124. r_json.Result_Time_start = DeviceSensor_data[data_i-1].T_time
  1125. r_json.Result_Time_defect = formatTime_
  1126. println(r_json.Result_str)
  1127. break
  1128. }
  1129. }
  1130. } else {
  1131. r_json.Result = 202
  1132. r_json.Result_str = "数据量太少 必须大于 2条以上!"
  1133. }
  1134. *res = append(*res, r_json)
  1135. <-chData
  1136. }(i, &jobGroup, &r_jsons)
  1137. }
  1138. //使用Wait等待所有任务执行完毕
  1139. jobGroup.Wait()
  1140. jsonsMap := map[string]R_JSONS{}
  1141. resp := make([]R_JSONS, 0)
  1142. for _, json := range r_jsons {
  1143. jsonsMap[json.T_id] = json
  1144. }
  1145. for _, v := range List {
  1146. resp = append(resp, jsonsMap[v.T_id])
  1147. }
  1148. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: resp}
  1149. c.ServeJSON()
  1150. return
  1151. }
  1152. // 打包数据本地数据
  1153. func (c *TaskDataController) TaskData_Import_TaskData() {
  1154. // 获取登录用户的uuid
  1155. T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1156. T_task_id := c.GetString("T_task_id")
  1157. _, err := NatsServer.Read_Task(T_task_id)
  1158. if err != nil {
  1159. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  1160. c.ServeJSON()
  1161. return
  1162. }
  1163. if err = lib.Create_Dir(conf.Sql_Temp_Dir); err != nil {
  1164. logs.Println("创建sql临时文件失败")
  1165. }
  1166. sql_file := fmt.Sprintf("%sZ_TaskData_%s.sql", conf.Sql_Temp_Dir, T_task_id)
  1167. org, err := Task.Dump_TaskData(T_task_id, conf.MysqlServer2_Username, conf.MysqlServer2_Password, conf.MysqlServer2_UrlPort, conf.MysqlServer2_Database, sql_file)
  1168. if err != nil {
  1169. c.Data["json"] = lib.JSONS{Code: 202, Msg: "z_task_data_" + T_task_id + "线上数据导出失败!"}
  1170. c.ServeJSON()
  1171. return
  1172. }
  1173. System.Add_UserLogs_T(T_uuid, "本地版-任务数据-打包本地数据", "导出线上数据Z_TaskData_"+T_task_id, org)
  1174. i := 0
  1175. flag := false
  1176. Task.CREATE_TaskData(conf.Local_AliasName, T_task_id)
  1177. for i < 10 {
  1178. Task.Truncate_TaskData(conf.Local_AliasName, T_task_id)
  1179. org, err = Task.Insert_TaskData(conf.MysqlServer_Username, conf.MysqlServer_Password, conf.MysqlServer_UrlPort, conf.MysqlServer_Database, sql_file)
  1180. if err != nil {
  1181. logs.Println("任务数据-打包本地数据", "线下导入数据Z_TaskData_"+T_task_id, err.Error())
  1182. } else {
  1183. if Task.Check_TaskData_Num(T_task_id) {
  1184. System.Add_UserLogs_T(T_uuid, "本地版-任务数据-打包本地数据", "线下导入数据Z_TaskData_"+T_task_id, org)
  1185. flag = true
  1186. break
  1187. }
  1188. }
  1189. i++
  1190. }
  1191. // 重试10次后仍然没有成功导入数据
  1192. if !flag {
  1193. c.Data["json"] = lib.JSONS{Code: 202, Msg: "z_task_data_" + T_task_id + "线下导入数据失败!"}
  1194. c.ServeJSON()
  1195. return
  1196. }
  1197. //删除导出的sql文件
  1198. _ = os.Remove(sql_file)
  1199. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1200. c.ServeJSON()
  1201. }
  1202. // 更新线上数据
  1203. func (c *TaskDataController) TaskData_Up_TaskData() {
  1204. // 获取登录用户的uuid
  1205. T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1206. T_task_id := c.GetString("T_task_id")
  1207. Task_r, err := NatsServer.Read_Task(T_task_id)
  1208. if err != nil {
  1209. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  1210. c.ServeJSON()
  1211. return
  1212. }
  1213. if err = lib.Create_Dir(conf.Sql_Temp_Dir); err != nil {
  1214. logs.Println("创建sql临时文件失败")
  1215. }
  1216. sql_file := fmt.Sprintf("%sZ_TaskData_%s.sql", conf.Sql_Temp_Dir, T_task_id)
  1217. org, err := Task.Dump_TaskData(T_task_id, conf.MysqlServer_Username, conf.MysqlServer_Password, conf.MysqlServer_UrlPort, conf.MysqlServer_Database, sql_file)
  1218. if err != nil {
  1219. logs.Println(T_uuid, "任务数据-更新线上数据", "导出线下数据Z_TaskData_"+T_task_id, err.Error())
  1220. c.Data["json"] = lib.JSONS{Code: 202, Msg: "z_task_data_" + T_task_id + "线下数据导出失败!"}
  1221. c.ServeJSON()
  1222. return
  1223. }
  1224. System.Add_UserLogs_T(T_uuid, "本地版-任务数据-更新线上数据", "线上导入数据Z_TaskData_"+T_task_id, org)
  1225. i := 0
  1226. flag := false
  1227. for i < 10 {
  1228. Task.Truncate_TaskData(conf.Server_AliasName, T_task_id)
  1229. org, err = Task.Insert_TaskData(conf.MysqlServer2_Username, conf.MysqlServer2_Password, conf.MysqlServer2_UrlPort, conf.MysqlServer2_Database, sql_file)
  1230. if err != nil {
  1231. logs.Println(T_uuid, "任务数据-更新线上数据", "线上导入数据Z_TaskData_"+T_task_id, err.Error())
  1232. } else {
  1233. if Task.Check_TaskData_Num(T_task_id) {
  1234. System.Add_UserLogs_T(T_uuid, "本地版-任务数据-更新线上数据", "线上导入数据Z_TaskData_"+T_task_id, org)
  1235. flag = true
  1236. break
  1237. }
  1238. }
  1239. i++
  1240. }
  1241. // 重试10次后仍然没有成功导入数据
  1242. if !flag {
  1243. c.Data["json"] = lib.JSONS{Code: 202, Msg: "z_task_data_" + T_task_id + "线上导入数据失败!"}
  1244. c.ServeJSON()
  1245. return
  1246. }
  1247. //删除导出的sql文件
  1248. _ = os.Remove(sql_file)
  1249. // 线上数据更新后 将当前任务 交付审核 标志 为 1
  1250. Task_r.T_delivery_state = 1
  1251. err = NatsServer.Update_Task(Task_r)
  1252. if err != nil {
  1253. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  1254. c.ServeJSON()
  1255. return
  1256. }
  1257. // 向线上用户日志表写入数据
  1258. System.Add_UserLogs_T(T_uuid, "本地版-任务", "修改", Task_r)
  1259. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1260. c.ServeJSON()
  1261. }
  1262. // 更新线上数据后台执行
  1263. func (c *TaskDataController) TaskData_Up_TaskData_Back() {
  1264. // 获取登录用户的uuid
  1265. T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1266. T_task_id := c.GetString("T_task_id")
  1267. Task_r, err := NatsServer.Read_Task(T_task_id)
  1268. if err != nil {
  1269. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  1270. c.ServeJSON()
  1271. return
  1272. }
  1273. if Task_r.T_delivery_state == 2 {
  1274. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1275. c.ServeJSON()
  1276. return
  1277. }
  1278. exist := Task.CheckTableExist(conf.Local_AliasName, T_task_id)
  1279. if !exist {
  1280. c.Data["json"] = lib.JSONS{Code: 202, Msg: "本地数据不存在!"}
  1281. c.ServeJSON()
  1282. return
  1283. }
  1284. // 采集中
  1285. Task_r.T_delivery_state = 2
  1286. err = NatsServer.Update_Task(Task_r)
  1287. if err != nil {
  1288. c.Data["json"] = lib.JSONS{Code: 202, Msg: "提交失败!"}
  1289. c.ServeJSON()
  1290. return
  1291. }
  1292. data := Nats.Up_TaskData_Back{
  1293. T_uuid: T_uuid,
  1294. Task: Task_r,
  1295. }
  1296. b, _ := msgpack.Marshal(&data)
  1297. _ = lib.Nats.Publish("ColdVerify_Local_Up_TaskData", b)
  1298. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1299. c.ServeJSON()
  1300. return
  1301. }
  1302. // CopyFromPosition 数据拷贝
  1303. func (c *TaskDataController) CopyFromPosition() {
  1304. T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1305. //if !b_ {
  1306. // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1307. // c.ServeJSON()
  1308. // return
  1309. //}
  1310. StartTime := c.GetString("StartTime")
  1311. startTime, ok := lib.TimeStrToTime(StartTime)
  1312. if !ok {
  1313. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  1314. c.ServeJSON()
  1315. return
  1316. }
  1317. EndTime := c.GetString("EndTime")
  1318. endTime, ok := lib.TimeStrToTime(EndTime)
  1319. if !ok {
  1320. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  1321. c.ServeJSON()
  1322. return
  1323. }
  1324. CopyTime := c.GetString("CopyTime")
  1325. copyTime, ok := lib.TimeStrToTime(CopyTime)
  1326. if !ok {
  1327. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  1328. c.ServeJSON()
  1329. return
  1330. }
  1331. CopyEndTime := copyTime.Add(endTime.Sub(startTime)).Format("2006-01-02 15:04:05")
  1332. // 时间间隔 分钟
  1333. T_saveT, _ := c.GetInt("T_saveT")
  1334. SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|")
  1335. T_task_id := c.GetString("T_task_id")
  1336. Task_r, err := NatsServer.Read_Task(T_task_id)
  1337. if err != nil {
  1338. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  1339. c.ServeJSON()
  1340. return
  1341. }
  1342. if Task_r.T_collection_state == 2 {
  1343. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  1344. c.ServeJSON()
  1345. return
  1346. }
  1347. for _, v := range SN_List {
  1348. sn_id := strings.Split(v, ",")
  1349. if len(sn_id) != 2 {
  1350. continue
  1351. }
  1352. sn, id_str := sn_id[0], sn_id[1]
  1353. List, _ := Task.Read_TaskData_ById_List_AES(Task_r.T_task_id, sn, id_str, StartTime, EndTime, 0, 9999)
  1354. Task.DeleteTaskDataByTimeRange(Task_r.T_task_id, sn, id_str, CopyTime, CopyEndTime)
  1355. ct := copyTime
  1356. go func(TaskDataList []Task.TaskData_, task_id string, T_saveT int) {
  1357. for _, taskData := range TaskDataList {
  1358. taskData.T_time = ct.Format("2006-01-02 15:04:05")
  1359. Task.InsertTaskData(task_id, taskData)
  1360. ct = ct.Add(time.Second * time.Duration(T_saveT))
  1361. }
  1362. }(List, Task_r.T_task_id, T_saveT)
  1363. }
  1364. System.Add_UserLogs_T(T_uuid, "数据复制", fmt.Sprintf("数据复制(%s)[%s|%s],复制时间:%s,,时间间隔:%d", T_task_id, StartTime, EndTime, CopyTime, T_saveT), SN_List)
  1365. c.Data["json"] = lib.JSONS{200, "数据复制已提交后台处理!", nil}
  1366. c.ServeJSON()
  1367. return
  1368. }
  1369. // RepairSensorData 数据补漏
  1370. func (c *TaskDataController) RepairSensorData() {
  1371. T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1372. //if !b_ {
  1373. // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1374. // c.ServeJSON()
  1375. // return
  1376. //}
  1377. StartTime := c.GetString("StartTime")
  1378. _, ok := lib.TimeStrToTime(StartTime)
  1379. if !ok {
  1380. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  1381. c.ServeJSON()
  1382. return
  1383. }
  1384. EndTime := c.GetString("EndTime")
  1385. _, ok = lib.TimeStrToTime(EndTime)
  1386. if !ok {
  1387. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  1388. c.ServeJSON()
  1389. return
  1390. }
  1391. // 时间间隔 秒
  1392. saveTime, _ := c.GetInt("T_saveT")
  1393. SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|")
  1394. T_task_id := c.GetString("T_task_id")
  1395. Task_r, err := NatsServer.Read_Task(T_task_id)
  1396. if err != nil {
  1397. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  1398. c.ServeJSON()
  1399. return
  1400. }
  1401. if Task_r.T_collection_state == 2 {
  1402. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  1403. c.ServeJSON()
  1404. return
  1405. }
  1406. num := 0
  1407. for _, v := range SN_List {
  1408. sn_id := strings.Split(v, ",")
  1409. if len(sn_id) != 2 {
  1410. continue
  1411. }
  1412. sn, id_str := sn_id[0], sn_id[1]
  1413. list, _ := Task.Read_TaskData_ById_List_AES(Task_r.T_task_id, sn, id_str, StartTime, EndTime, 0, 9999)
  1414. for i := 0; i < len(list)-1; i++ {
  1415. current := list[i].T_time
  1416. next := list[i+1].T_time
  1417. c, _ := time.Parse("2006-01-02 15:04:05", current)
  1418. n, _ := time.Parse("2006-01-02 15:04:05", next)
  1419. interval := n.Unix() - c.Unix()
  1420. //logs.Debug("时间间隔:", interval, "保存时间:", saveTime)
  1421. fmt.Println("当前:", current, "下一个:", next)
  1422. if int(interval) > saveTime {
  1423. ttInterval := list[i+1].T_t - list[i].T_t
  1424. ttt := list[i].T_t // 温度临时变量
  1425. trhInterval := list[i+1].T_rh - list[i].T_rh
  1426. trht := list[i].T_rh //湿度临时变量
  1427. count := (int(interval) - saveTime) / saveTime
  1428. if int(interval)%saveTime != 0 {
  1429. count++
  1430. }
  1431. num += count
  1432. for k := 0; k < count; k++ {
  1433. t := c.Add(time.Second * time.Duration(saveTime)).Format("2006-01-02 15:04:05") //时间临时变量
  1434. ttt += ttInterval / float32(count)
  1435. trht += trhInterval / float32(count)
  1436. Task.InsertTaskData(Task_r.T_task_id, Task.TaskData_{
  1437. T_sn: list[i].T_sn,
  1438. T_id: list[i].T_id,
  1439. T_t: ttt,
  1440. T_rh: trht,
  1441. T_time: t,
  1442. })
  1443. c = c.Add(time.Second * time.Duration(saveTime))
  1444. }
  1445. }
  1446. }
  1447. }
  1448. System.Add_UserLogs_T(T_uuid, "数据补漏", fmt.Sprintf("数据补漏(%s)[%s|%s],时间间隔:%d", T_task_id, StartTime, EndTime, saveTime), SN_List)
  1449. c.Data["json"] = lib.JSONS{200, fmt.Sprintf("补漏完成!共补漏%d条数据", num), nil}
  1450. c.ServeJSON()
  1451. return
  1452. }
  1453. // DataSensorDataTrend 数据趋势
  1454. func (c *TaskDataController) DataSensorDataTrend() {
  1455. T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1456. //if !b_ {
  1457. // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1458. // c.ServeJSON()
  1459. // return
  1460. //}
  1461. StartTime := c.GetString("StartTime")
  1462. _, ok := lib.TimeStrToTime(StartTime)
  1463. if !ok {
  1464. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  1465. c.ServeJSON()
  1466. return
  1467. }
  1468. EndTime := c.GetString("EndTime")
  1469. _, ok = lib.TimeStrToTime(EndTime)
  1470. if !ok {
  1471. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  1472. c.ServeJSON()
  1473. return
  1474. }
  1475. // 时间间隔 秒
  1476. saveTime, _ := c.GetInt("T_saveT")
  1477. SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|")
  1478. T_task_id := c.GetString("T_task_id")
  1479. Task_r, err := NatsServer.Read_Task(T_task_id)
  1480. if err != nil {
  1481. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  1482. c.ServeJSON()
  1483. return
  1484. }
  1485. if Task_r.T_collection_state == 2 {
  1486. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  1487. c.ServeJSON()
  1488. return
  1489. }
  1490. for _, v := range SN_List {
  1491. sn_id := strings.Split(v, ",")
  1492. if len(sn_id) != 2 {
  1493. continue
  1494. }
  1495. sn, id_str := sn_id[0], sn_id[1]
  1496. list, _ := Task.Read_TaskData_ById_List_AES(Task_r.T_task_id, sn, id_str, StartTime, EndTime, 0, 9999)
  1497. if len(list) == 0 {
  1498. continue
  1499. }
  1500. Task.DeleteTaskDataByTimeRange(Task_r.T_task_id, sn, id_str, StartTime, EndTime)
  1501. first := list[0]
  1502. last := list[len(list)-1]
  1503. current, _ := time.Parse("2006-01-02 15:04:05", first.T_time)
  1504. next, _ := time.Parse("2006-01-02 15:04:05", last.T_time)
  1505. interval := next.Sub(current).Seconds() / float64(saveTime)
  1506. ttInterval := (last.T_t - first.T_t) / float32(interval)
  1507. trhInterval := (last.T_rh - first.T_rh) / float32(interval)
  1508. tt := first.T_t
  1509. ttrh := first.T_rh
  1510. for current.Unix() < next.Unix() {
  1511. tt += ttInterval
  1512. ttrh += trhInterval
  1513. ttime := current.Format("2006-01-02 15:04:05")
  1514. Task.InsertTaskData(Task_r.T_task_id, Task.TaskData_{
  1515. //加保存时间
  1516. T_sn: first.T_sn,
  1517. T_id: id_str,
  1518. T_t: tt,
  1519. T_rh: ttrh,
  1520. T_time: ttime,
  1521. })
  1522. current = current.Add(time.Second * time.Duration(saveTime))
  1523. }
  1524. }
  1525. System.Add_UserLogs_T(T_uuid, "数据趋势", fmt.Sprintf("数据趋势(%s)[%s|%s],时间间隔:%d", T_task_id, StartTime, EndTime, saveTime), SN_List)
  1526. c.Data["json"] = lib.JSONS{200, "设置趋势操作成功", nil}
  1527. c.ServeJSON()
  1528. return
  1529. }
  1530. // UpdateRand 数据随机
  1531. func (c *TaskDataController) UpdateRand() {
  1532. T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1533. //if !b_ {
  1534. // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1535. // c.ServeJSON()
  1536. // return
  1537. //}
  1538. StartTime := c.GetString("StartTime")
  1539. _, ok := lib.TimeStrToTime(StartTime)
  1540. if !ok {
  1541. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  1542. c.ServeJSON()
  1543. return
  1544. }
  1545. EndTime := c.GetString("EndTime")
  1546. _, ok = lib.TimeStrToTime(EndTime)
  1547. if !ok {
  1548. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  1549. c.ServeJSON()
  1550. return
  1551. }
  1552. TemperatureMin, _ := c.GetInt("TemperatureMin") // 温度
  1553. TemperatureMax, _ := c.GetInt("TemperatureMax")
  1554. HumidityMax, _ := c.GetInt("HumidityMax") // 湿度
  1555. HumidityMin, _ := c.GetInt("HumidityMin")
  1556. SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|")
  1557. T_task_id := c.GetString("T_task_id")
  1558. Task_r, err := NatsServer.Read_Task(T_task_id)
  1559. if err != nil {
  1560. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  1561. c.ServeJSON()
  1562. return
  1563. }
  1564. if Task_r.T_collection_state == 2 {
  1565. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  1566. c.ServeJSON()
  1567. return
  1568. }
  1569. for _, v := range SN_List {
  1570. sn_id := strings.Split(v, ",")
  1571. if len(sn_id) != 2 {
  1572. continue
  1573. }
  1574. sn, id := sn_id[0], sn_id[1]
  1575. Task.UpdateTaskDataTemperatureAndHumidityRandom(Task_r.T_task_id, sn, id, StartTime, EndTime, TemperatureMax, TemperatureMin, HumidityMax, HumidityMin)
  1576. }
  1577. 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)
  1578. //反馈成功
  1579. c.Data["json"] = lib.JSONS{200, "调整随机偏移值成功!", nil}
  1580. c.ServeJSON()
  1581. return
  1582. }
  1583. // UpdateFix 更新固定值
  1584. func (c *TaskDataController) UpdateFix() {
  1585. T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1586. //if !b_ {
  1587. // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1588. // c.ServeJSON()
  1589. // return
  1590. //}
  1591. StartTime := c.GetString("StartTime")
  1592. _, ok := lib.TimeStrToTime(StartTime)
  1593. if !ok {
  1594. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  1595. c.ServeJSON()
  1596. return
  1597. }
  1598. EndTime := c.GetString("EndTime")
  1599. _, ok = lib.TimeStrToTime(EndTime)
  1600. if !ok {
  1601. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  1602. c.ServeJSON()
  1603. return
  1604. }
  1605. FixTemperature := c.GetString("FixTemperature")
  1606. FixHumidity := c.GetString("FixHumidity")
  1607. //2.得到数据进行统一设置访问修改
  1608. humidity, _ := strconv.ParseFloat(FixHumidity, 64)
  1609. temperature, _ := strconv.ParseFloat(FixTemperature, 64)
  1610. SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|")
  1611. T_task_id := c.GetString("T_task_id")
  1612. Task_r, err := NatsServer.Read_Task(T_task_id)
  1613. if err != nil {
  1614. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  1615. c.ServeJSON()
  1616. return
  1617. }
  1618. if Task_r.T_collection_state == 2 {
  1619. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  1620. c.ServeJSON()
  1621. return
  1622. }
  1623. //3.循环更新数据
  1624. for _, v := range SN_List {
  1625. sn_id := strings.Split(v, ",")
  1626. if len(sn_id) != 2 {
  1627. continue
  1628. }
  1629. sn, id := sn_id[0], sn_id[1]
  1630. Task.UpdateTaskDataTemperatureAndHumidity(Task_r.T_task_id, sn, id, StartTime, EndTime, temperature, humidity)
  1631. }
  1632. System.Add_UserLogs_T(T_uuid, "更新固定值", fmt.Sprintf("更新固定值(%s)[%s|%s],温度[%f],湿度[%f]", T_task_id, StartTime, EndTime, temperature, humidity), SN_List)
  1633. //4.反馈成功
  1634. c.Data["json"] = lib.JSONS{200, "调整固定偏移值成功!", nil}
  1635. c.ServeJSON()
  1636. }
  1637. // DataSensorDataSmooth 数据平滑
  1638. func (c *TaskDataController) DataSensorDataSmooth() {
  1639. T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1640. //if !b_ {
  1641. // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1642. // c.ServeJSON()
  1643. // return
  1644. //}
  1645. StartTime := c.GetString("StartTime")
  1646. _, ok := lib.TimeStrToTime(StartTime)
  1647. if !ok {
  1648. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  1649. c.ServeJSON()
  1650. return
  1651. }
  1652. EndTime := c.GetString("EndTime")
  1653. _, ok = lib.TimeStrToTime(EndTime)
  1654. if !ok {
  1655. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  1656. c.ServeJSON()
  1657. return
  1658. }
  1659. tRange, _ := c.GetFloat("tRange")
  1660. hRange, _ := c.GetFloat("hRange")
  1661. SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|")
  1662. T_task_id := c.GetString("T_task_id")
  1663. Task_r, err := NatsServer.Read_Task(T_task_id)
  1664. if err != nil {
  1665. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  1666. c.ServeJSON()
  1667. return
  1668. }
  1669. if Task_r.T_collection_state == 2 {
  1670. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  1671. c.ServeJSON()
  1672. return
  1673. }
  1674. var count int
  1675. for _, v := range SN_List {
  1676. sn_id := strings.Split(v, ",")
  1677. if len(sn_id) != 2 {
  1678. continue
  1679. }
  1680. sn := sn_id[0]
  1681. id_str := sn_id[1]
  1682. list, _ := Task.Read_TaskData_ById_List_AES(Task_r.T_task_id, sn, id_str, StartTime, EndTime, 0, 9999)
  1683. for i := 1; i < len(list); i++ {
  1684. n := list[i-1]
  1685. old := list[i]
  1686. newO := list[i]
  1687. //变化差
  1688. var tInterval = old.T_t - n.T_t
  1689. var hInterval = old.T_rh - n.T_rh
  1690. fmt.Println("温度:", n.T_t, "温度next:", old.T_t, "差值:", n.T_t-old.T_t)
  1691. if tRange != 0 {
  1692. if tInterval > float32(tRange) {
  1693. newO.T_t = n.T_t + float32(tRange)
  1694. } else if tInterval < -float32(tRange) {
  1695. newO.T_t = n.T_t - float32(tRange)
  1696. }
  1697. }
  1698. if hRange != 0 {
  1699. if hInterval > float32(hRange) {
  1700. newO.T_rh = n.T_rh + float32(hRange)
  1701. } else if hInterval < -float32(hRange) {
  1702. newO.T_rh = n.T_rh - float32(hRange)
  1703. }
  1704. }
  1705. if old.T_rh != newO.T_rh || old.T_t != newO.T_t {
  1706. fmt.Println("原始数据:", old, "新数据:", newO)
  1707. list[i] = newO
  1708. Task.UpdateTaskData(Task_r.T_task_id, sn, id_str, old, newO)
  1709. count++
  1710. }
  1711. }
  1712. }
  1713. System.Add_UserLogs_T(T_uuid, "数据平滑", fmt.Sprintf("数据平滑(%s)[%s|%s],温度[%f],湿度[%f]", T_task_id, StartTime, EndTime, tRange, hRange), SN_List)
  1714. c.Data["json"] = lib.JSONS{200, "操作成功处理" + fmt.Sprint(count) + "条数据", nil}
  1715. c.ServeJSON()
  1716. }
  1717. // CopyFromPosition 数据拷贝平均值
  1718. func (c *TaskDataController) CopyFromPositionAverageSN() {
  1719. T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1720. //if !b_ {
  1721. // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1722. // c.ServeJSON()
  1723. // return
  1724. //}
  1725. StartTime := c.GetString("StartTime")
  1726. startTime, ok := lib.TimeStrToTime(StartTime)
  1727. if !ok {
  1728. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  1729. c.ServeJSON()
  1730. return
  1731. }
  1732. EndTime := c.GetString("EndTime")
  1733. endTime, ok := lib.TimeStrToTime(EndTime)
  1734. if !ok {
  1735. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  1736. c.ServeJSON()
  1737. return
  1738. }
  1739. CopyTime := c.GetString("CopyTime")
  1740. CopySN := c.GetString("CopySN")
  1741. CopyID := c.GetString("CopyID")
  1742. copyTime, ok := lib.TimeStrToTime(CopyTime)
  1743. // 时间间隔 s
  1744. T_saveT, _ := c.GetInt("T_saveT")
  1745. if !ok {
  1746. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  1747. c.ServeJSON()
  1748. return
  1749. }
  1750. CopyEndTime := copyTime.Add(endTime.Sub(startTime)).Format("2006-01-02 15:04:05")
  1751. SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|")
  1752. if len(SN_List) != 2 {
  1753. c.Data["json"] = lib.JSONS{Code: 202, Msg: "SN_List Err!"}
  1754. c.ServeJSON()
  1755. return
  1756. }
  1757. T_task_id := c.GetString("T_task_id")
  1758. Task_r, err := NatsServer.Read_Task(T_task_id)
  1759. if err != nil {
  1760. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  1761. c.ServeJSON()
  1762. return
  1763. }
  1764. if Task_r.T_collection_state == 2 {
  1765. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  1766. c.ServeJSON()
  1767. return
  1768. }
  1769. sn_id1 := strings.Split(SN_List[0], ",")
  1770. if len(sn_id1) != 2 {
  1771. c.Data["json"] = lib.JSONS{Code: 202, Msg: "SN_List Err!"}
  1772. c.ServeJSON()
  1773. return
  1774. }
  1775. sn1, id_str1 := sn_id1[0], sn_id1[1]
  1776. sn_id2 := strings.Split(SN_List[1], ",")
  1777. if len(sn_id2) != 2 {
  1778. c.Data["json"] = lib.JSONS{Code: 202, Msg: "SN_List Err!"}
  1779. c.ServeJSON()
  1780. return
  1781. }
  1782. sn2, id_str2 := sn_id2[0], sn_id2[1]
  1783. List1, _ := Task.Read_TaskData_ById_List_AES(Task_r.T_task_id, sn1, id_str1, StartTime, EndTime, 0, 9999)
  1784. List2, _ := Task.Read_TaskData_ById_List_AES(Task_r.T_task_id, sn2, id_str2, StartTime, EndTime, 0, 9999)
  1785. num := len(List1)
  1786. if len(List2) < len(List1) {
  1787. num = len(List2)
  1788. }
  1789. var list []Task.TaskData_
  1790. ct := copyTime
  1791. for i := 0; i < num; i++ {
  1792. if List1[i].T_time != List2[i].T_time {
  1793. 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}
  1794. c.ServeJSON()
  1795. return
  1796. //ct = ct.Add(time.Second * time.Duration(T_saveT))
  1797. //continue
  1798. }
  1799. T_t := (List1[i].T_t + List2[i].T_t) / 2
  1800. T_rh := (List1[i].T_rh + List2[i].T_rh) / 2
  1801. list = append(list, Task.TaskData_{
  1802. T_sn: CopySN,
  1803. T_id: CopyID,
  1804. T_t: T_t,
  1805. T_rh: T_rh,
  1806. T_time: ct.Format("2006-01-02 15:04:05"),
  1807. })
  1808. ct = ct.Add(time.Second * time.Duration(T_saveT))
  1809. }
  1810. Task.DeleteTaskDataByTimeRange(Task_r.T_task_id, CopySN, CopyID, CopyTime, CopyEndTime)
  1811. go func(TaskDataList []Task.TaskData_, task_id string) {
  1812. for _, taskData := range TaskDataList {
  1813. Task.InsertTaskData(task_id, taskData)
  1814. }
  1815. }(list, Task_r.T_task_id)
  1816. System.Add_UserLogs_T(T_uuid, "数据复制平均值", fmt.Sprintf("数据复制(%s)[%s|%s],复制时间:%s,,时间间隔:%d", T_task_id, StartTime, EndTime, CopyTime, T_saveT), SN_List)
  1817. c.Data["json"] = lib.JSONS{200, "数据复制已提交后台处理!", nil}
  1818. c.ServeJSON()
  1819. return
  1820. }
  1821. // CopyFromPosition 数据拷贝
  1822. func (c *TaskDataController) CopyFromPositionSN() {
  1823. T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1824. //if !b_ {
  1825. // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1826. // c.ServeJSON()
  1827. // return
  1828. //}
  1829. StartTime := c.GetString("StartTime")
  1830. startTime, ok := lib.TimeStrToTime(StartTime)
  1831. if !ok {
  1832. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  1833. c.ServeJSON()
  1834. return
  1835. }
  1836. EndTime := c.GetString("EndTime")
  1837. endTime, ok := lib.TimeStrToTime(EndTime)
  1838. if !ok {
  1839. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  1840. c.ServeJSON()
  1841. return
  1842. }
  1843. CopyTime := c.GetString("CopyTime")
  1844. copyTime, ok := lib.TimeStrToTime(CopyTime)
  1845. CopySN := c.GetString("CopySN")
  1846. CopyID := c.GetString("CopyID")
  1847. if !ok {
  1848. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  1849. c.ServeJSON()
  1850. return
  1851. }
  1852. CopyEndTime := copyTime.Add(endTime.Sub(startTime)).Format("2006-01-02 15:04:05")
  1853. // 时间间隔 分钟
  1854. T_saveT, _ := c.GetInt("T_saveT")
  1855. SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|")
  1856. if len(SN_List) != 1 {
  1857. c.Data["json"] = lib.JSONS{202, "SN_List Err!", nil}
  1858. c.ServeJSON()
  1859. return
  1860. }
  1861. T_task_id := c.GetString("T_task_id")
  1862. Task_r, err := NatsServer.Read_Task(T_task_id)
  1863. if err != nil {
  1864. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  1865. c.ServeJSON()
  1866. return
  1867. }
  1868. if Task_r.T_collection_state == 2 {
  1869. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  1870. c.ServeJSON()
  1871. return
  1872. }
  1873. sn_id := strings.Split(SN_List[0], ",")
  1874. if len(sn_id) != 2 {
  1875. c.Data["json"] = lib.JSONS{202, "SN_List Err!", nil}
  1876. c.ServeJSON()
  1877. return
  1878. }
  1879. sn, id_str := sn_id[0], sn_id[1]
  1880. List, _ := Task.Read_TaskData_ById_List_AES(Task_r.T_task_id, sn, id_str, StartTime, EndTime, 0, 9999)
  1881. Task.DeleteTaskDataByTimeRange(Task_r.T_task_id, CopySN, CopyID, CopyTime, CopyEndTime)
  1882. ct := copyTime
  1883. go func(TaskDataList []Task.TaskData_, task_id string, T_saveT int) {
  1884. for _, taskData := range TaskDataList {
  1885. taskData.T_time = ct.Format("2006-01-02 15:04:05")
  1886. taskData.T_sn = CopySN
  1887. taskData.T_id = CopyID
  1888. Task.InsertTaskData(task_id, taskData)
  1889. ct = ct.Add(time.Second * time.Duration(T_saveT))
  1890. }
  1891. }(List, Task_r.T_task_id, T_saveT)
  1892. System.Add_UserLogs_T(T_uuid, "数据复制", fmt.Sprintf("数据复制(%s)[%s|%s],复制时间:%s,,时间间隔:%d", T_task_id, StartTime, EndTime, CopyTime, T_saveT), SN_List)
  1893. c.Data["json"] = lib.JSONS{200, "数据复制已提交后台处理!", nil}
  1894. c.ServeJSON()
  1895. return
  1896. }
  1897. // DataRemoveDuplicates 数据去重
  1898. func (c *TaskDataController) DataRemoveDuplicates() {
  1899. T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1900. T_task_id := c.GetString("T_task_id")
  1901. Task_r, err := NatsServer.Read_Task(T_task_id)
  1902. if err != nil {
  1903. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  1904. c.ServeJSON()
  1905. return
  1906. }
  1907. if Task_r.T_collection_state == 2 {
  1908. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  1909. c.ServeJSON()
  1910. return
  1911. }
  1912. // 删除重复数据
  1913. var count int64
  1914. count, err = Task.DeleteDeduplicate(T_task_id)
  1915. if err != nil {
  1916. c.Data["json"] = lib.JSONS{Code: 202, Msg: "导入失败"}
  1917. c.ServeJSON()
  1918. return
  1919. }
  1920. System.Add_UserLogs_T(T_uuid, "数据去重", fmt.Sprintf("数据平滑(%s)[%d]条", T_task_id, count), nil)
  1921. c.Data["json"] = lib.JSONS{200, "操作成功处理" + fmt.Sprint(count) + "条数据", nil}
  1922. c.ServeJSON()
  1923. }
  1924. // 数据偏移到区间内
  1925. func (c *TaskDataController) DataSkewingInterval() {
  1926. T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1927. //if !b_ {
  1928. // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1929. // c.ServeJSON()
  1930. // return
  1931. //}
  1932. StartTime := c.GetString("StartTime")
  1933. _, ok := lib.TimeStrToTime(StartTime)
  1934. if !ok {
  1935. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  1936. c.ServeJSON()
  1937. return
  1938. }
  1939. EndTime := c.GetString("EndTime")
  1940. _, ok = lib.TimeStrToTime(EndTime)
  1941. if !ok {
  1942. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  1943. c.ServeJSON()
  1944. return
  1945. }
  1946. TemperatureMin, _ := c.GetFloat("TemperatureMin") // 温度
  1947. TemperatureMax, _ := c.GetFloat("TemperatureMax")
  1948. Tmin, Tmax := TemperatureMin, TemperatureMax
  1949. SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|")
  1950. T_task_id := c.GetString("T_task_id")
  1951. Task_r, err := NatsServer.Read_Task(T_task_id)
  1952. if err != nil {
  1953. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  1954. c.ServeJSON()
  1955. return
  1956. }
  1957. if Task_r.T_collection_state == 2 {
  1958. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  1959. c.ServeJSON()
  1960. return
  1961. }
  1962. errMsg := []string{}
  1963. for _, v := range SN_List {
  1964. sn_id := strings.Split(v, ",")
  1965. if len(sn_id) != 2 {
  1966. continue
  1967. }
  1968. sn, id := sn_id[0], sn_id[1]
  1969. min, max := Task.Read_TaskData_T_Min_Max(Task_r.T_task_id, sn, id, StartTime, EndTime)
  1970. if min < Tmin && max > Tmax {
  1971. errMsg = append(errMsg, fmt.Sprintf("【%s】最低温度[%.2f]低于给定的最低温度[%.2f],并且最高温度[%.2f]高于给定的最高温度[%.2f],无法偏移!", id, min, Tmin, max, Tmax))
  1972. continue
  1973. }
  1974. if min > Tmin && max < Tmax {
  1975. continue
  1976. //errMsg = append(errMsg, fmt.Sprintf("【%s】最低温度[%.2f]高于给定最低温度[%.2f],并且最高温度[%.2f]低于给定最高温度[%.2f],无须偏移!", id, min, Tmin, max, Tmax))
  1977. }
  1978. // 判断是否能向上偏移
  1979. if min < Tmin && max < Tmax && (Tmin-min+max) > Tmax {
  1980. errMsg = append(errMsg, fmt.Sprintf("【%s】偏移后最高温度[%.2f]将高于给定的最高温度[%.2f],无法偏移!", id, Tmin-min+max, Tmax))
  1981. continue
  1982. }
  1983. if max > Tmax && min > Tmin && (min-(max-Tmax)) < Tmin {
  1984. errMsg = append(errMsg, fmt.Sprintf("【%s】偏移后最低温度[%.2f]将低于给定的最低温度[%.2f],无法偏移!", id, min-(max-Tmax), Tmin))
  1985. continue
  1986. }
  1987. // 向上偏移
  1988. if min < Tmin && max < Tmax && (Tmin-min+max) <= Tmax {
  1989. Task.UpdateTaskDataTemperatureAndHumidity(Task_r.T_task_id, sn, id, StartTime, EndTime, Tmin-min, 0)
  1990. continue
  1991. }
  1992. // 向下偏移
  1993. if max > Tmax && min > Tmin && (min-(max-Tmax)) >= Tmin {
  1994. Task.UpdateTaskDataTemperatureAndHumidity(Task_r.T_task_id, sn, id, StartTime, EndTime, -(max - Tmax), 0)
  1995. continue
  1996. }
  1997. }
  1998. System.Add_UserLogs_T(T_uuid, "数据偏移到给定范围", fmt.Sprintf("数据偏移到给定范围(%s)[%s|%s],温度[%f-%f]", T_task_id, StartTime, EndTime, TemperatureMin, TemperatureMax), SN_List)
  1999. if len(errMsg) > 0 {
  2000. c.Data["json"] = lib.JSONS{202, "", errMsg}
  2001. c.ServeJSON()
  2002. return
  2003. }
  2004. //反馈成功
  2005. c.Data["json"] = lib.JSONS{200, "调整区间偏移值成功!", nil}
  2006. c.ServeJSON()
  2007. return
  2008. }
  2009. // 等比缩放
  2010. func (c *TaskDataController) DataGeometricScale() {
  2011. T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  2012. //if !b_ {
  2013. // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  2014. // c.ServeJSON()
  2015. // return
  2016. //}
  2017. StartTime := c.GetString("StartTime")
  2018. _, ok := lib.TimeStrToTime(StartTime)
  2019. if !ok {
  2020. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  2021. c.ServeJSON()
  2022. return
  2023. }
  2024. EndTime := c.GetString("EndTime")
  2025. _, ok = lib.TimeStrToTime(EndTime)
  2026. if !ok {
  2027. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  2028. c.ServeJSON()
  2029. return
  2030. }
  2031. Temperature, _ := c.GetFloat("Temperature") // 温度比列
  2032. Humidity, _ := c.GetFloat("Humidity") // 适度比列
  2033. SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|")
  2034. T_task_id := c.GetString("T_task_id")
  2035. Task_r, err := NatsServer.Read_Task(T_task_id)
  2036. if err != nil {
  2037. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  2038. c.ServeJSON()
  2039. return
  2040. }
  2041. if Task_r.T_collection_state == 2 {
  2042. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  2043. c.ServeJSON()
  2044. return
  2045. }
  2046. errMsg := []string{}
  2047. for _, v := range SN_List {
  2048. sn_id := strings.Split(v, ",")
  2049. if len(sn_id) != 2 {
  2050. continue
  2051. }
  2052. sn, id := sn_id[0], sn_id[1]
  2053. Task.UpdateTaskDataTemperatureAndHumidityByGeometric(Task_r.T_task_id, sn, id, StartTime, EndTime, Temperature, Humidity)
  2054. }
  2055. System.Add_UserLogs_T(T_uuid, "数据等比缩放", fmt.Sprintf("数据等比缩放(%s)[%s|%s],温度[%f-%f]", T_task_id, StartTime, EndTime, Temperature, Humidity), SN_List)
  2056. if len(errMsg) > 0 {
  2057. c.Data["json"] = lib.JSONS{202, "", errMsg}
  2058. c.ServeJSON()
  2059. return
  2060. }
  2061. //反馈成功
  2062. c.Data["json"] = lib.JSONS{200, "数据等比缩放成功!", nil}
  2063. c.ServeJSON()
  2064. return
  2065. }
  2066. // DataBatchDelete 批量删除
  2067. func (c *TaskDataController) DataBatchDelete() {
  2068. T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  2069. //if !b_ {
  2070. // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  2071. // c.ServeJSON()
  2072. // return
  2073. //}
  2074. StartTime := c.GetString("StartTime")
  2075. _, ok := lib.TimeStrToTime(StartTime)
  2076. if !ok {
  2077. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  2078. c.ServeJSON()
  2079. return
  2080. }
  2081. EndTime := c.GetString("EndTime")
  2082. _, ok = lib.TimeStrToTime(EndTime)
  2083. if !ok {
  2084. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  2085. c.ServeJSON()
  2086. return
  2087. }
  2088. SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|")
  2089. T_task_id := c.GetString("T_task_id")
  2090. Task_r, err := NatsServer.Read_Task(T_task_id)
  2091. if err != nil {
  2092. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  2093. c.ServeJSON()
  2094. return
  2095. }
  2096. if Task_r.T_collection_state == 2 {
  2097. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  2098. c.ServeJSON()
  2099. return
  2100. }
  2101. for _, v := range SN_List {
  2102. sn_id := strings.Split(v, ",")
  2103. if len(sn_id) != 2 {
  2104. continue
  2105. }
  2106. sn, id_str := sn_id[0], sn_id[1]
  2107. Task.DeleteTaskDataByTimeRange(Task_r.T_task_id, sn, id_str, StartTime, EndTime)
  2108. }
  2109. System.Add_UserLogs_T(T_uuid, "批量删除", fmt.Sprintf("批量删除(%s)[%s|%s]", T_task_id, StartTime, EndTime), SN_List)
  2110. c.Data["json"] = lib.JSONS{200, "批量删除操作成功", nil}
  2111. c.ServeJSON()
  2112. return
  2113. }
  2114. // 存档列表
  2115. func (c *TaskDataController) TaskDataCopy_List() {
  2116. var r_jsons lib.R_JSONS
  2117. page, _ := c.GetInt("page")
  2118. if page < 1 {
  2119. page = 1
  2120. }
  2121. page_z, _ := c.GetInt("page_z")
  2122. if page_z < 1 {
  2123. page_z = conf.Page_size
  2124. }
  2125. T_task_id := c.GetString("T_task_id")
  2126. Task_r, err := NatsServer.Read_Task(T_task_id)
  2127. if err != nil {
  2128. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  2129. c.ServeJSON()
  2130. return
  2131. }
  2132. if Task_r.T_collection_state == 2 {
  2133. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  2134. c.ServeJSON()
  2135. return
  2136. }
  2137. var cnt int64
  2138. List, cnt := Task.Read_TaskCopy_List(Task_r.T_task_id, page, page_z)
  2139. page_size := math.Ceil(float64(cnt) / float64(page_z))
  2140. r_jsons.List = List
  2141. r_jsons.Page = page
  2142. r_jsons.Page_size = int(page_size)
  2143. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  2144. r_jsons.Num = int(cnt)
  2145. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  2146. c.ServeJSON()
  2147. return
  2148. }
  2149. // 添加存档
  2150. func (c *TaskDataController) TaskDataCopy_Add() {
  2151. T_task_id := c.GetString("T_task_id")
  2152. Task_r, err := NatsServer.Read_Task(T_task_id)
  2153. if err != nil {
  2154. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  2155. c.ServeJSON()
  2156. return
  2157. }
  2158. if Task_r.T_collection_state == 2 {
  2159. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  2160. c.ServeJSON()
  2161. return
  2162. }
  2163. var_ := Task.TaskCopy{
  2164. T_task_id: T_task_id,
  2165. T_time: time.Now().Format("2006-01-02 15:04:05"),
  2166. T_State: 1,
  2167. }
  2168. T_copy_id, is := Task.Add_TaskCopy(var_)
  2169. if !is {
  2170. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  2171. c.ServeJSON()
  2172. return
  2173. }
  2174. // 创建数据表
  2175. Task.CREATE_TaskDataCopy(conf.Local_AliasName, T_copy_id)
  2176. // 将数据表存档
  2177. Task.TaskData_Arhiving(T_task_id, T_copy_id)
  2178. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_copy_id}
  2179. c.ServeJSON()
  2180. return
  2181. }
  2182. // 删除存档
  2183. func (c *TaskDataController) TaskDataCopy_Del() {
  2184. T_copy_id := c.GetString("T_copy_id")
  2185. r, err := Task.Read_TaskCopy(T_copy_id)
  2186. if err != nil {
  2187. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_copy_id 错误!"}
  2188. c.ServeJSON()
  2189. return
  2190. }
  2191. is := Task.Delete_TaskCopy(r)
  2192. if !is {
  2193. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  2194. c.ServeJSON()
  2195. return
  2196. }
  2197. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_copy_id}
  2198. c.ServeJSON()
  2199. return
  2200. }
  2201. // 存档恢复
  2202. func (c *TaskDataController) TaskDataCopy_Recover() {
  2203. T_task_id := c.GetString("T_task_id")
  2204. T_copy_id := c.GetString("T_copy_id")
  2205. Task_r, err := NatsServer.Read_Task(T_task_id)
  2206. if err != nil {
  2207. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  2208. c.ServeJSON()
  2209. return
  2210. }
  2211. if Task_r.T_collection_state == 2 {
  2212. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  2213. c.ServeJSON()
  2214. return
  2215. }
  2216. _, err = Task.Read_TaskCopy(T_copy_id)
  2217. if err != nil {
  2218. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_copy_id 错误!"}
  2219. c.ServeJSON()
  2220. return
  2221. }
  2222. is := Task.TaskData_Recover(T_task_id, T_copy_id)
  2223. if !is {
  2224. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  2225. c.ServeJSON()
  2226. return
  2227. }
  2228. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_copy_id}
  2229. c.ServeJSON()
  2230. return
  2231. }
  2232. // 查询图片生成状态
  2233. func (c *TaskDataController) TaskData_JPGState() {
  2234. T_task_id := c.GetString("T_task_id")
  2235. jpg, is := Task.Redis_TaskDataJPG_Get(T_task_id)
  2236. if !is {
  2237. c.Data["json"] = lib.JSONS{Code: 202, Msg: "暂无图片正在生成"}
  2238. c.ServeJSON()
  2239. return
  2240. }
  2241. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: jpg}
  2242. c.ServeJSON()
  2243. return
  2244. }
  2245. func (c *TaskDataController) TaskData_JPG() {
  2246. StartTime := c.GetString("StartTime")
  2247. if len(StartTime) > 0 {
  2248. _, ok := lib.TimeStrToTime(StartTime)
  2249. if !ok {
  2250. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  2251. c.ServeJSON()
  2252. return
  2253. }
  2254. }
  2255. EndTime := c.GetString("EndTime")
  2256. if len(EndTime) > 0 {
  2257. _, ok := lib.TimeStrToTime(EndTime)
  2258. if !ok {
  2259. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  2260. c.ServeJSON()
  2261. return
  2262. }
  2263. }
  2264. TemperatureMin, _ := c.GetFloat("TemperatureMin") // 最低温度
  2265. TemperatureMax, _ := c.GetFloat("TemperatureMax") // 最高温度
  2266. T_task_id := c.GetString("T_task_id")
  2267. SN_List := c.GetString("SN_List")
  2268. Task_r, err := NatsServer.Read_Task(T_task_id)
  2269. if err != nil {
  2270. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  2271. c.ServeJSON()
  2272. return
  2273. }
  2274. if Task_r.T_collection_state == 2 {
  2275. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  2276. c.ServeJSON()
  2277. return
  2278. }
  2279. Task.Redis_TaskDataJPG_Del(T_task_id)
  2280. // 生成图片
  2281. go TaskDataJPG(StartTime, EndTime, T_task_id, SN_List, TemperatureMin, TemperatureMax)
  2282. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  2283. c.ServeJSON()
  2284. return
  2285. }
  2286. // 存档生成图片
  2287. func TaskDataJPG(StartTime, EndTime, T_task_id, snList string, TemperatureMin, TemperatureMax float64) {
  2288. Task.Redis_TaskDataJPG_Set(T_task_id, Task.TaskDataJPG{
  2289. State: 1,
  2290. Msg: "图片生成中",
  2291. Url: "",
  2292. })
  2293. msg := ""
  2294. state := 2
  2295. url := ""
  2296. SN_List := strings.Split(strings.Trim(snList, "|"), "|")
  2297. if len(snList) == 0 {
  2298. List := Task.Read_TaskData_ById_ClassList(T_task_id)
  2299. for _, v := range List {
  2300. SN_List = append(SN_List, fmt.Sprintf("%s,%s", v.T_sn, v.T_id))
  2301. }
  2302. }
  2303. id_List := []string{}
  2304. sn_List := []string{}
  2305. if len(snList) > 0 {
  2306. for _, v := range SN_List {
  2307. sn_id := strings.Split(v, ",")
  2308. if len(sn_id) != 2 {
  2309. return
  2310. }
  2311. id_List = append(id_List, fmt.Sprintf("'%s'", sn_id[1]))
  2312. sn_List = append(sn_List, fmt.Sprintf("'%s'", sn_id[0]))
  2313. }
  2314. }
  2315. ymin, ymax, minTime, maxTime := Task.Read_TaskData_T_Min_Max_Time_Min_Max(T_task_id, sn_List, id_List, StartTime, EndTime)
  2316. xmin, xmax := float64(minTime.Unix()), float64(maxTime.Unix())
  2317. // 创建一个新的绘图
  2318. p := plot.New()
  2319. // 设置绘图标题和标签
  2320. p.Title.Text = "温度折线图"
  2321. //p.Legend.ThumbnailWidth = 5 * vg.Inch
  2322. p.X.Label.Text = "时间"
  2323. p.Y.Label.Text = "温度"
  2324. // 添加最高,最低标准线 用红色虚线标识
  2325. p.Add(lib.HorizontalLine(xmin, xmax, TemperatureMin))
  2326. p.Add(lib.HorizontalLine(xmin, xmax, TemperatureMax))
  2327. var chData = make(chan int, 10)
  2328. var jobGroup sync.WaitGroup
  2329. // 创建温度线
  2330. for i := 0; i < len(SN_List); i++ {
  2331. chData <- 1
  2332. jobGroup.Add(1)
  2333. go func(index int) {
  2334. //go func(index int, wg *sync.WaitGroup, p *plot.Plot) {
  2335. defer func() {
  2336. <-chData // 完成时chan取出1个
  2337. jobGroup.Done() // 完成时将等待组值减1
  2338. }()
  2339. sn_id := strings.Split(SN_List[index], ",")
  2340. if len(sn_id) != 2 {
  2341. return
  2342. }
  2343. sn, id := sn_id[0], sn_id[1]
  2344. list, _ := Task.Read_TaskData_ById_List_AES(T_task_id, sn, id, StartTime, EndTime, 0, 9999)
  2345. if len(list) == 0 {
  2346. return
  2347. }
  2348. pts := make(plotter.XYs, len(list))
  2349. for j, d := range list {
  2350. t, _ := lib.TimeStrToTime(d.T_time)
  2351. pts[j].X = float64(t.Unix())
  2352. pts[j].Y = float64(d.T_t)
  2353. }
  2354. line, err := plotter.NewLine(pts)
  2355. if err != nil {
  2356. return
  2357. }
  2358. line.Color = lib.RandomColor(index)
  2359. p.Add(line)
  2360. }(i)
  2361. }
  2362. jobGroup.Wait()
  2363. if ymax < 8 {
  2364. ymax = 8
  2365. }
  2366. if ymin > 0 {
  2367. ymin = 0
  2368. }
  2369. p.Y.Min, p.Y.Max = ymin, ymax
  2370. p.X.Min, p.X.Max = xmin, xmax
  2371. p.Y.Tick.Marker = lib.CommaTicks{}
  2372. //p.X.Tick.Marker = plot.TimeTicks{Format: "2006-01-02 15:04:05"}
  2373. p.X.Tick.Marker = lib.TimeTicks{}
  2374. p.X.Tick.Label.Rotation = math.Pi / 5
  2375. p.X.Tick.Label.YAlign = draw.YCenter
  2376. p.X.Tick.Label.XAlign = draw.XRight
  2377. filename := "jpg" + time.Now().Format("20060102150405")
  2378. // 保存文件
  2379. if err := p.Save(10*vg.Inch, 4*vg.Inch, "ofile/"+filename+".jpg"); err != nil {
  2380. state = 3
  2381. msg = "图片生成失败"
  2382. logs.Error(lib.FuncName(), "生成图片失败", err)
  2383. }
  2384. if !lib.Pload_qiniu("ofile/"+filename+".jpg", "ofile/"+filename+".jpg") {
  2385. state = 3
  2386. msg = "图片上传七牛云失败"
  2387. logs.Error(lib.FuncName(), "上传七牛云失败")
  2388. }
  2389. //删除目录
  2390. os.Remove("ofile/" + filename + ".jpg")
  2391. if len(msg) == 0 {
  2392. msg = "图片生成成功"
  2393. url = "https://bzdcoldverifyoss.baozhida.cn/" + "ofile/" + filename + ".jpg"
  2394. }
  2395. Task.Redis_TaskDataJPG_Set(T_task_id, Task.TaskDataJPG{
  2396. State: state,
  2397. Msg: msg,
  2398. Url: url,
  2399. })
  2400. }
  2401. func (c *TaskDataController) Certificate_List() {
  2402. var r_jsons lib.R_JSONS
  2403. T_task_id := c.GetString("T_task_id")
  2404. T_sn := c.GetString("T_sn")
  2405. page, _ := c.GetInt("page")
  2406. if page < 1 {
  2407. page = 1
  2408. }
  2409. page_z, _ := c.GetInt("page_z")
  2410. if page_z < 1 {
  2411. page_z = conf.Page_size
  2412. }
  2413. Task_r, err := NatsServer.Read_Task(T_task_id)
  2414. if err != nil {
  2415. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  2416. c.ServeJSON()
  2417. return
  2418. }
  2419. if Task_r.T_collection_state == 2 {
  2420. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  2421. c.ServeJSON()
  2422. return
  2423. }
  2424. List, cnt := Certificate.Read_Certificate_List(T_task_id, T_sn, page, page_z)
  2425. r_jsons.List = List
  2426. r_jsons.Page = page
  2427. r_jsons.Page_size = page_z
  2428. r_jsons.Num = int(cnt)
  2429. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  2430. c.ServeJSON()
  2431. return
  2432. }