TaskData.go 78 KB

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