TaskData.go 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626
  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. // DataRemoveDuplicates 数据去重
  1850. func (c *TaskDataController) DataRemoveDuplicates() {
  1851. T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1852. T_task_id := c.GetString("T_task_id")
  1853. Task_r, err := NatsServer.Read_Task(T_task_id)
  1854. if err != nil {
  1855. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  1856. c.ServeJSON()
  1857. return
  1858. }
  1859. if Task_r.T_collection_state == 2 {
  1860. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  1861. c.ServeJSON()
  1862. return
  1863. }
  1864. // 删除重复数据
  1865. var count int64
  1866. count, err = Task.DeleteDeduplicate(T_task_id)
  1867. if err != nil {
  1868. c.Data["json"] = lib.JSONS{Code: 202, Msg: "导入失败"}
  1869. c.ServeJSON()
  1870. return
  1871. }
  1872. System.Add_UserLogs_T(T_uuid, "数据去重", fmt.Sprintf("数据平滑(%s)[%d]条", T_task_id, count), nil)
  1873. c.Data["json"] = lib.JSONS{200, "操作成功处理" + fmt.Sprint(count) + "条数据", nil}
  1874. c.ServeJSON()
  1875. }
  1876. // 数据偏移到区间内
  1877. func (c *TaskDataController) DataSkewingInterval() {
  1878. T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1879. //if !b_ {
  1880. // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1881. // c.ServeJSON()
  1882. // return
  1883. //}
  1884. StartTime := c.GetString("StartTime")
  1885. _, ok := lib.TimeStrToTime(StartTime)
  1886. if !ok {
  1887. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  1888. c.ServeJSON()
  1889. return
  1890. }
  1891. EndTime := c.GetString("EndTime")
  1892. _, ok = lib.TimeStrToTime(EndTime)
  1893. if !ok {
  1894. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  1895. c.ServeJSON()
  1896. return
  1897. }
  1898. TemperatureMin, _ := c.GetFloat("TemperatureMin") // 温度
  1899. TemperatureMax, _ := c.GetFloat("TemperatureMax")
  1900. Tmin, Tmax := TemperatureMin, TemperatureMax
  1901. SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|")
  1902. T_task_id := c.GetString("T_task_id")
  1903. Task_r, err := NatsServer.Read_Task(T_task_id)
  1904. if err != nil {
  1905. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  1906. c.ServeJSON()
  1907. return
  1908. }
  1909. if Task_r.T_collection_state == 2 {
  1910. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  1911. c.ServeJSON()
  1912. return
  1913. }
  1914. errMsg := []string{}
  1915. for _, v := range SN_List {
  1916. sn_id := strings.Split(v, ",")
  1917. if len(sn_id) != 2 {
  1918. continue
  1919. }
  1920. sn, id := sn_id[0], sn_id[1]
  1921. min, max := Task.Read_TaskData_T_Min_Max(Task_r.T_task_id, sn, id, StartTime, EndTime)
  1922. if min < Tmin && max > Tmax {
  1923. errMsg = append(errMsg, fmt.Sprintf("【%s】最低温度[%.2f]低于给定的最低温度[%.2f],并且最高温度[%.2f]高于给定的最高温度[%.2f],无法偏移!", id, min, Tmin, max, Tmax))
  1924. continue
  1925. }
  1926. if min > Tmin && max < Tmax {
  1927. continue
  1928. //errMsg = append(errMsg, fmt.Sprintf("【%s】最低温度[%.2f]高于给定最低温度[%.2f],并且最高温度[%.2f]低于给定最高温度[%.2f],无须偏移!", id, min, Tmin, max, Tmax))
  1929. }
  1930. // 判断是否能向上偏移
  1931. if min < Tmin && max < Tmax && (Tmin-min+max) > Tmax {
  1932. errMsg = append(errMsg, fmt.Sprintf("【%s】偏移后最高温度[%.2f]将高于给定的最高温度[%.2f],无法偏移!", id, Tmin-min+max, Tmax))
  1933. continue
  1934. }
  1935. if max > Tmax && min > Tmin && (min-(max-Tmax)) < Tmin {
  1936. errMsg = append(errMsg, fmt.Sprintf("【%s】偏移后最低温度[%.2f]将低于给定的最低温度[%.2f],无法偏移!", id, min-(max-Tmax), Tmin))
  1937. continue
  1938. }
  1939. // 向上偏移
  1940. if min < Tmin && max < Tmax && (Tmin-min+max) <= Tmax {
  1941. Task.UpdateTaskDataTemperatureAndHumidity(Task_r.T_task_id, sn, id, StartTime, EndTime, Tmin-min, 0)
  1942. continue
  1943. }
  1944. // 向下偏移
  1945. if max > Tmax && min > Tmin && (min-(max-Tmax)) >= Tmin {
  1946. Task.UpdateTaskDataTemperatureAndHumidity(Task_r.T_task_id, sn, id, StartTime, EndTime, -(max - Tmax), 0)
  1947. continue
  1948. }
  1949. }
  1950. System.Add_UserLogs_T(T_uuid, "数据偏移到给定范围", fmt.Sprintf("数据偏移到给定范围(%s)[%s|%s],温度[%f-%f]", T_task_id, StartTime, EndTime, TemperatureMin, TemperatureMax), SN_List)
  1951. if len(errMsg) > 0 {
  1952. c.Data["json"] = lib.JSONS{202, "", errMsg}
  1953. c.ServeJSON()
  1954. return
  1955. }
  1956. //反馈成功
  1957. c.Data["json"] = lib.JSONS{200, "调整区间偏移值成功!", nil}
  1958. c.ServeJSON()
  1959. return
  1960. }
  1961. // 等比缩放
  1962. func (c *TaskDataController) DataGeometricScale() {
  1963. T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1964. //if !b_ {
  1965. // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1966. // c.ServeJSON()
  1967. // return
  1968. //}
  1969. StartTime := c.GetString("StartTime")
  1970. _, ok := lib.TimeStrToTime(StartTime)
  1971. if !ok {
  1972. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  1973. c.ServeJSON()
  1974. return
  1975. }
  1976. EndTime := c.GetString("EndTime")
  1977. _, ok = lib.TimeStrToTime(EndTime)
  1978. if !ok {
  1979. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  1980. c.ServeJSON()
  1981. return
  1982. }
  1983. Temperature, _ := c.GetFloat("Temperature") // 温度比列
  1984. Humidity, _ := c.GetFloat("Humidity") // 适度比列
  1985. SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|")
  1986. T_task_id := c.GetString("T_task_id")
  1987. Task_r, err := NatsServer.Read_Task(T_task_id)
  1988. if err != nil {
  1989. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  1990. c.ServeJSON()
  1991. return
  1992. }
  1993. if Task_r.T_collection_state == 2 {
  1994. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  1995. c.ServeJSON()
  1996. return
  1997. }
  1998. errMsg := []string{}
  1999. for _, v := range SN_List {
  2000. sn_id := strings.Split(v, ",")
  2001. if len(sn_id) != 2 {
  2002. continue
  2003. }
  2004. sn, id := sn_id[0], sn_id[1]
  2005. Task.UpdateTaskDataTemperatureAndHumidityByGeometric(Task_r.T_task_id, sn, id, StartTime, EndTime, Temperature, Humidity)
  2006. }
  2007. System.Add_UserLogs_T(T_uuid, "数据等比缩放", fmt.Sprintf("数据等比缩放(%s)[%s|%s],温度[%f-%f]", T_task_id, StartTime, EndTime, Temperature, Humidity), SN_List)
  2008. if len(errMsg) > 0 {
  2009. c.Data["json"] = lib.JSONS{202, "", errMsg}
  2010. c.ServeJSON()
  2011. return
  2012. }
  2013. //反馈成功
  2014. c.Data["json"] = lib.JSONS{200, "数据等比缩放成功!", nil}
  2015. c.ServeJSON()
  2016. return
  2017. }
  2018. // 存档列表
  2019. func (c *TaskDataController) TaskDataCopy_List() {
  2020. var r_jsons lib.R_JSONS
  2021. page, _ := c.GetInt("page")
  2022. if page < 1 {
  2023. page = 1
  2024. }
  2025. page_z, _ := c.GetInt("page_z")
  2026. if page_z < 1 {
  2027. page_z = conf.Page_size
  2028. }
  2029. T_task_id := c.GetString("T_task_id")
  2030. Task_r, err := NatsServer.Read_Task(T_task_id)
  2031. if err != nil {
  2032. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  2033. c.ServeJSON()
  2034. return
  2035. }
  2036. if Task_r.T_collection_state == 2 {
  2037. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  2038. c.ServeJSON()
  2039. return
  2040. }
  2041. var cnt int64
  2042. List, cnt := Task.Read_TaskCopy_List(Task_r.T_task_id, page, page_z)
  2043. page_size := math.Ceil(float64(cnt) / float64(page_z))
  2044. r_jsons.List = List
  2045. r_jsons.Page = page
  2046. r_jsons.Page_size = int(page_size)
  2047. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  2048. r_jsons.Num = int(cnt)
  2049. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  2050. c.ServeJSON()
  2051. return
  2052. }
  2053. // 添加存档
  2054. func (c *TaskDataController) TaskDataCopy_Add() {
  2055. T_task_id := c.GetString("T_task_id")
  2056. Task_r, err := NatsServer.Read_Task(T_task_id)
  2057. if err != nil {
  2058. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  2059. c.ServeJSON()
  2060. return
  2061. }
  2062. if Task_r.T_collection_state == 2 {
  2063. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  2064. c.ServeJSON()
  2065. return
  2066. }
  2067. var_ := Task.TaskCopy{
  2068. T_task_id: T_task_id,
  2069. T_time: time.Now().Format("2006-01-02 15:04:05"),
  2070. T_State: 1,
  2071. }
  2072. T_copy_id, is := Task.Add_TaskCopy(var_)
  2073. if !is {
  2074. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  2075. c.ServeJSON()
  2076. return
  2077. }
  2078. // 创建数据表
  2079. Task.CREATE_TaskDataCopy(conf.Local_AliasName, T_copy_id)
  2080. // 将数据表存档
  2081. Task.TaskData_Arhiving(T_task_id, T_copy_id)
  2082. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_copy_id}
  2083. c.ServeJSON()
  2084. return
  2085. }
  2086. // 删除存档
  2087. func (c *TaskDataController) TaskDataCopy_Del() {
  2088. T_copy_id := c.GetString("T_copy_id")
  2089. r, err := Task.Read_TaskCopy(T_copy_id)
  2090. if err != nil {
  2091. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_copy_id 错误!"}
  2092. c.ServeJSON()
  2093. return
  2094. }
  2095. is := Task.Delete_TaskCopy(r)
  2096. if !is {
  2097. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  2098. c.ServeJSON()
  2099. return
  2100. }
  2101. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_copy_id}
  2102. c.ServeJSON()
  2103. return
  2104. }
  2105. // 存档恢复
  2106. func (c *TaskDataController) TaskDataCopy_Recover() {
  2107. T_task_id := c.GetString("T_task_id")
  2108. T_copy_id := c.GetString("T_copy_id")
  2109. Task_r, err := NatsServer.Read_Task(T_task_id)
  2110. if err != nil {
  2111. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  2112. c.ServeJSON()
  2113. return
  2114. }
  2115. if Task_r.T_collection_state == 2 {
  2116. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  2117. c.ServeJSON()
  2118. return
  2119. }
  2120. _, err = Task.Read_TaskCopy(T_copy_id)
  2121. if err != nil {
  2122. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_copy_id 错误!"}
  2123. c.ServeJSON()
  2124. return
  2125. }
  2126. is := Task.TaskData_Recover(T_task_id, T_copy_id)
  2127. if !is {
  2128. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  2129. c.ServeJSON()
  2130. return
  2131. }
  2132. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_copy_id}
  2133. c.ServeJSON()
  2134. return
  2135. }
  2136. // 查询图片生成状态
  2137. func (c *TaskDataController) TaskData_JPGState() {
  2138. T_task_id := c.GetString("T_task_id")
  2139. jpg, is := Task.Redis_TaskDataJPG_Get(T_task_id)
  2140. if !is {
  2141. c.Data["json"] = lib.JSONS{Code: 202, Msg: "暂无图片正在生成"}
  2142. c.ServeJSON()
  2143. return
  2144. }
  2145. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: jpg}
  2146. c.ServeJSON()
  2147. return
  2148. }
  2149. func (c *TaskDataController) TaskData_JPG() {
  2150. StartTime := c.GetString("StartTime")
  2151. if len(StartTime) > 0 {
  2152. _, ok := lib.TimeStrToTime(StartTime)
  2153. if !ok {
  2154. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  2155. c.ServeJSON()
  2156. return
  2157. }
  2158. }
  2159. EndTime := c.GetString("EndTime")
  2160. if len(EndTime) > 0 {
  2161. _, ok := lib.TimeStrToTime(EndTime)
  2162. if !ok {
  2163. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  2164. c.ServeJSON()
  2165. return
  2166. }
  2167. }
  2168. TemperatureMin, _ := c.GetFloat("TemperatureMin") // 最低温度
  2169. TemperatureMax, _ := c.GetFloat("TemperatureMax") // 最高温度
  2170. T_task_id := c.GetString("T_task_id")
  2171. SN_List := c.GetString("SN_List")
  2172. Task_r, err := NatsServer.Read_Task(T_task_id)
  2173. if err != nil {
  2174. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  2175. c.ServeJSON()
  2176. return
  2177. }
  2178. if Task_r.T_collection_state == 2 {
  2179. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  2180. c.ServeJSON()
  2181. return
  2182. }
  2183. Task.Redis_TaskDataJPG_Del(T_task_id)
  2184. // 生成图片
  2185. go TaskDataJPG(StartTime, EndTime, T_task_id, SN_List, TemperatureMin, TemperatureMax)
  2186. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  2187. c.ServeJSON()
  2188. return
  2189. }
  2190. // 存档生成图片
  2191. func TaskDataJPG(StartTime, EndTime, T_task_id, snList string, TemperatureMin, TemperatureMax float64) {
  2192. Task.Redis_TaskDataJPG_Set(T_task_id, Task.TaskDataJPG{
  2193. State: 1,
  2194. Msg: "图片生成中",
  2195. Url: "",
  2196. })
  2197. msg := ""
  2198. state := 2
  2199. url := ""
  2200. SN_List := strings.Split(strings.Trim(snList, "|"), "|")
  2201. if len(snList) == 0 {
  2202. List := Task.Read_TaskData_ById_ClassList(T_task_id)
  2203. for _, v := range List {
  2204. SN_List = append(SN_List, fmt.Sprintf("%s,%s", v.T_sn, v.T_id))
  2205. }
  2206. }
  2207. id_List := []string{}
  2208. sn_List := []string{}
  2209. if len(snList) > 0 {
  2210. for _, v := range SN_List {
  2211. sn_id := strings.Split(v, ",")
  2212. if len(sn_id) != 2 {
  2213. return
  2214. }
  2215. id_List = append(id_List, fmt.Sprintf("'%s'", sn_id[1]))
  2216. sn_List = append(sn_List, fmt.Sprintf("'%s'", sn_id[0]))
  2217. }
  2218. }
  2219. ymin, ymax, minTime, maxTime := Task.Read_TaskData_T_Min_Max_Time_Min_Max(T_task_id, sn_List, id_List, StartTime, EndTime)
  2220. xmin, xmax := float64(minTime.Unix()), float64(maxTime.Unix())
  2221. // 创建一个新的绘图
  2222. p := plot.New()
  2223. // 设置绘图标题和标签
  2224. p.Title.Text = "温度折线图"
  2225. //p.Legend.ThumbnailWidth = 5 * vg.Inch
  2226. p.X.Label.Text = "时间"
  2227. p.Y.Label.Text = "温度"
  2228. // 添加最高,最低标准线 用红色虚线标识
  2229. p.Add(lib.HorizontalLine(xmin, xmax, TemperatureMin))
  2230. p.Add(lib.HorizontalLine(xmin, xmax, TemperatureMax))
  2231. var chData = make(chan int, 10)
  2232. var jobGroup sync.WaitGroup
  2233. // 创建温度线
  2234. for i := 0; i < len(SN_List); i++ {
  2235. chData <- 1
  2236. jobGroup.Add(1)
  2237. go func(index int) {
  2238. //go func(index int, wg *sync.WaitGroup, p *plot.Plot) {
  2239. defer func() {
  2240. <-chData // 完成时chan取出1个
  2241. jobGroup.Done() // 完成时将等待组值减1
  2242. }()
  2243. sn_id := strings.Split(SN_List[index], ",")
  2244. if len(sn_id) != 2 {
  2245. return
  2246. }
  2247. sn, id := sn_id[0], sn_id[1]
  2248. list, _ := Task.Read_TaskData_ById_List_AES(T_task_id, sn, id, StartTime, EndTime, 0, 9999)
  2249. if len(list) == 0 {
  2250. return
  2251. }
  2252. pts := make(plotter.XYs, len(list))
  2253. for j, d := range list {
  2254. t, _ := lib.TimeStrToTime(d.T_time)
  2255. pts[j].X = float64(t.Unix())
  2256. pts[j].Y = float64(d.T_t)
  2257. }
  2258. line, err := plotter.NewLine(pts)
  2259. if err != nil {
  2260. return
  2261. }
  2262. line.Color = lib.RandomColor(index)
  2263. p.Add(line)
  2264. }(i)
  2265. }
  2266. jobGroup.Wait()
  2267. if ymax < 8 {
  2268. ymax = 8
  2269. }
  2270. if ymin > 0 {
  2271. ymin = 0
  2272. }
  2273. p.Y.Min, p.Y.Max = ymin, ymax
  2274. p.X.Min, p.X.Max = xmin, xmax
  2275. p.Y.Tick.Marker = lib.CommaTicks{}
  2276. //p.X.Tick.Marker = plot.TimeTicks{Format: "2006-01-02 15:04:05"}
  2277. p.X.Tick.Marker = lib.TimeTicks{}
  2278. p.X.Tick.Label.Rotation = math.Pi / 5
  2279. p.X.Tick.Label.YAlign = draw.YCenter
  2280. p.X.Tick.Label.XAlign = draw.XRight
  2281. filename := "jpg" + time.Now().Format("20060102150405")
  2282. // 保存文件
  2283. if err := p.Save(10*vg.Inch, 4*vg.Inch, "ofile/"+filename+".jpg"); err != nil {
  2284. state = 3
  2285. msg = "图片生成失败"
  2286. logs.Error(lib.FuncName(), "生成图片失败", err)
  2287. }
  2288. if !lib.Pload_qiniu("ofile/"+filename+".jpg", "ofile/"+filename+".jpg") {
  2289. state = 3
  2290. msg = "图片上传七牛云失败"
  2291. logs.Error(lib.FuncName(), "上传七牛云失败")
  2292. }
  2293. //删除目录
  2294. os.Remove("ofile/" + filename + ".jpg")
  2295. if len(msg) == 0 {
  2296. msg = "图片生成成功"
  2297. url = "https://bzdcoldverifyoss.baozhida.cn/" + "ofile/" + filename + ".jpg"
  2298. }
  2299. Task.Redis_TaskDataJPG_Set(T_task_id, Task.TaskDataJPG{
  2300. State: state,
  2301. Msg: msg,
  2302. Url: url,
  2303. })
  2304. }
  2305. func (c *TaskDataController) Certificate_List() {
  2306. var r_jsons lib.R_JSONS
  2307. T_task_id := c.GetString("T_task_id")
  2308. T_sn := c.GetString("T_sn")
  2309. page, _ := c.GetInt("page")
  2310. if page < 1 {
  2311. page = 1
  2312. }
  2313. page_z, _ := c.GetInt("page_z")
  2314. if page_z < 1 {
  2315. page_z = conf.Page_size
  2316. }
  2317. Task_r, err := NatsServer.Read_Task(T_task_id)
  2318. if err != nil {
  2319. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  2320. c.ServeJSON()
  2321. return
  2322. }
  2323. if Task_r.T_collection_state == 2 {
  2324. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  2325. c.ServeJSON()
  2326. return
  2327. }
  2328. List, cnt := Certificate.Read_Certificate_List(T_task_id, T_sn, page, page_z)
  2329. r_jsons.List = List
  2330. r_jsons.Page = page
  2331. r_jsons.Page_size = page_z
  2332. r_jsons.Num = int(cnt)
  2333. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  2334. c.ServeJSON()
  2335. return
  2336. }