TaskData.go 74 KB

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