TaskData.go 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  1. package controllers
  2. import (
  3. "ColdVerify_server/Nats"
  4. "ColdVerify_server/conf"
  5. "ColdVerify_server/lib"
  6. "ColdVerify_server/logs"
  7. "ColdVerify_server/models/Account"
  8. "ColdVerify_server/models/Device"
  9. "ColdVerify_server/models/System"
  10. "ColdVerify_server/models/Task"
  11. "errors"
  12. "fmt"
  13. beego "github.com/beego/beego/v2/server/web"
  14. "github.com/signintech/gopdf"
  15. "github.com/vmihailenco/msgpack/v5"
  16. "github.com/xuri/excelize/v2"
  17. "math"
  18. "os"
  19. "sort"
  20. "strconv"
  21. "strings"
  22. "time"
  23. )
  24. type TaskDataController struct {
  25. beego.Controller
  26. }
  27. // 获取-
  28. func (c *TaskDataController) Extract_TaskData() {
  29. // 验证登录 User_is, User_r
  30. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  31. if !User_is {
  32. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  33. c.ServeJSON()
  34. return
  35. }
  36. Time_start := c.GetString("Time_start")
  37. Time_end := c.GetString("Time_end")
  38. T_task_id := c.GetString("T_task_id")
  39. Task_r, is := Task.Read_Task(T_task_id)
  40. if !is {
  41. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  42. c.ServeJSON()
  43. return
  44. }
  45. DeviceClass_List := Device.Read_DeviceClassList_List_id(Task_r.T_class)
  46. if len(DeviceClass_List) == 0 {
  47. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_Class_id 分类设备列表 为空!"}
  48. c.ServeJSON()
  49. return
  50. }
  51. logs.Println("----导入 :", DeviceClass_List)
  52. // 清空表
  53. Task.Truncate_TaskData(Task_r.T_task_id)
  54. // 插入 数据
  55. for _, v := range DeviceClass_List {
  56. logs.Println("---- :", v.T_sn, Task_r.T_task_id, Time_start, Time_end)
  57. Task.Import_TaskData(v.T_sn, v.T_id, Task_r.T_task_id, Time_start, Time_end)
  58. }
  59. // 提取数据后 将 当前任务 数据采集 标志 为 1
  60. Task_r.T_collection_state = 1
  61. if !Task.Update_Task(Task_r, "T_collection_state") {
  62. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  63. c.ServeJSON()
  64. return
  65. }
  66. System.Add_UserLogs_T(user_r.T_uuid, "任务", "修改", Task_r)
  67. System.Add_UserLogs(user_r.T_uuid, "提取数据", "提取数据"+Task_r.T_name, Task_r.T_task_id+"|"+Time_start+"|"+Time_end)
  68. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  69. c.ServeJSON()
  70. return
  71. }
  72. // 后台导出数据
  73. func (c *TaskDataController) Extract_TaskData_Back() {
  74. // 验证登录 User_is, User_r
  75. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  76. if !User_is {
  77. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  78. c.ServeJSON()
  79. return
  80. }
  81. Time_start := c.GetString("Time_start")
  82. Time_end := c.GetString("Time_end")
  83. T_task_id := c.GetString("T_task_id")
  84. Task_r, is := Task.Read_Task(T_task_id)
  85. if !is {
  86. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  87. c.ServeJSON()
  88. return
  89. }
  90. // 采集中
  91. if Task_r.T_collection_state == 2 {
  92. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据正采集中,请勿重复提交!"}
  93. c.ServeJSON()
  94. return
  95. }
  96. DeviceClass_List := Device.Read_DeviceClassList_List_id(Task_r.T_class)
  97. if len(DeviceClass_List) == 0 {
  98. c.Data["json"] = lib.JSONS{Code: 202, Msg: "设备列表为空,请先添加设备!"}
  99. c.ServeJSON()
  100. return
  101. }
  102. // 更新状态为采集中
  103. Task_r.T_collection_state = 2
  104. if !Task.Update_Task(Task_r, "T_collection_state") {
  105. c.Data["json"] = lib.JSONS{Code: 202, Msg: "导出数据失败!"}
  106. c.ServeJSON()
  107. return
  108. }
  109. data := Nats.Extract_TaskData_Back{
  110. T_uuid: user_r.T_uuid,
  111. Time_start: Time_start,
  112. Time_end: Time_end,
  113. DeviceClassList: DeviceClass_List,
  114. Task: Task_r,
  115. }
  116. // 后台执行打包数据
  117. b, _ := msgpack.Marshal(&data)
  118. _ = lib.Nats.Publish("ColdVerify_Server_Extract_TaskData_Back", b)
  119. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  120. c.ServeJSON()
  121. return
  122. }
  123. // 列表 -
  124. func (c *TaskDataController) TaskData_List() {
  125. // 验证登录 User_is, User_r
  126. _, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  127. if !User_is {
  128. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  129. c.ServeJSON()
  130. return
  131. }
  132. var r_jsons lib.R_JSONS
  133. page, _ := c.GetInt("page")
  134. if page < 1 {
  135. page = 1
  136. }
  137. page_z, _ := c.GetInt("page_z")
  138. if page_z < 1 {
  139. page_z = conf.Page_size
  140. }
  141. Time_start := c.GetString("Time_start")
  142. Time_end := c.GetString("Time_end")
  143. T_sn := c.GetString("T_sn")
  144. T_id := c.GetString("T_id")
  145. T_task_id := c.GetString("T_task_id")
  146. Task_r, is := Task.Read_Task(T_task_id)
  147. if !is {
  148. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  149. c.ServeJSON()
  150. return
  151. }
  152. if Task_r.T_delivery_state == 2 {
  153. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  154. c.ServeJSON()
  155. return
  156. }
  157. // 查询设备列表
  158. dcList, _ := Device.Read_DeviceClassList_OrderList(Task_r.T_class, T_sn, "", "", page, 9999)
  159. // 保存布局编号和校准证书对应关系
  160. var deviceCertificateMap = make(map[string]string) // t_id, T_Certificate_sn
  161. for _, v := range dcList {
  162. deviceCertificateMap[v.T_id] = v.T_Certificate_sn
  163. }
  164. var cnt int64
  165. List, cnt := Task.Read_TaskData_ById_List(Task_r.T_task_id, T_sn, T_id, Time_start, Time_end, page, page_z)
  166. for i := 0; i < len(List); i++ {
  167. List[i].T_Certificate_sn = deviceCertificateMap[List[i].T_id]
  168. }
  169. page_size := math.Ceil(float64(cnt) / float64(page_z))
  170. r_jsons.List = List
  171. r_jsons.Page = page
  172. r_jsons.Page_size = int(page_size)
  173. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  174. r_jsons.Num = int(cnt)
  175. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  176. c.ServeJSON()
  177. return
  178. }
  179. // 列表 -
  180. func (c *TaskDataController) UserTaskData_List() {
  181. // 验证登录 User_is, User_r
  182. User_r, User_is := Account.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  183. if !User_is {
  184. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  185. c.ServeJSON()
  186. return
  187. }
  188. var r_jsons lib.R_JSONS
  189. page, _ := c.GetInt("page")
  190. if page < 1 {
  191. page = 1
  192. }
  193. page_z, _ := c.GetInt("page_z")
  194. if page_z < 1 {
  195. page_z = conf.Page_size
  196. }
  197. Time_start := c.GetString("Time_start")
  198. Time_end := c.GetString("Time_end")
  199. T_sn := c.GetString("T_sn")
  200. T_id := c.GetString("T_id")
  201. T_task_id := c.GetString("T_task_id")
  202. Task_r, is := Task.Read_Task(T_task_id)
  203. if !is {
  204. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  205. c.ServeJSON()
  206. return
  207. }
  208. if Task_r.T_delivery_state == 2 {
  209. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  210. c.ServeJSON()
  211. return
  212. }
  213. if Task_r.T_uuid != User_r.T_uuid {
  214. c.Data["json"] = lib.JSONS{Code: 200, Msg: "无权访问!"}
  215. c.ServeJSON()
  216. return
  217. }
  218. if len(Time_start) == 0 {
  219. Time_start = Task_r.T_VerifyDeviceDataStartTime
  220. }
  221. if len(Time_end) == 0 {
  222. Time_end = Task_r.T_VerifyDeviceDataEndTime
  223. }
  224. // 查询设备列表
  225. dcList, _ := Device.Read_DeviceClassList_OrderList(Task_r.T_class, T_sn, "", "", page, 9999)
  226. // 保存布局编号和校准证书对应关系
  227. var deviceCertificateMap = make(map[string]string) // t_id, T_Certificate_sn
  228. for _, v := range dcList {
  229. deviceCertificateMap[v.T_id] = v.T_Certificate_sn
  230. }
  231. var cnt int64
  232. List, cnt := Task.Read_TaskData_ById_List(Task_r.T_task_id, T_sn, T_id, Time_start, Time_end, page, page_z)
  233. for i := 0; i < len(List); i++ {
  234. List[i].T_Certificate_sn = deviceCertificateMap[List[i].T_id]
  235. }
  236. page_size := math.Ceil(float64(cnt) / float64(page_z))
  237. r_jsons.List = List
  238. r_jsons.Page = page
  239. r_jsons.Page_size = int(page_size)
  240. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  241. r_jsons.Num = int(cnt)
  242. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  243. c.ServeJSON()
  244. return
  245. }
  246. // 列表 -
  247. func (c *TaskDataController) TaskDataClass_List() {
  248. // 验证登录 User_is, User_r
  249. _, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  250. if !User_is {
  251. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  252. c.ServeJSON()
  253. return
  254. }
  255. T_task_id := c.GetString("T_task_id")
  256. Task_r, is := Task.Read_Task(T_task_id)
  257. if !is {
  258. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  259. c.ServeJSON()
  260. return
  261. }
  262. if Task_r.T_delivery_state == 2 {
  263. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  264. c.ServeJSON()
  265. return
  266. }
  267. List := Task.Read_TaskData_ById_ClassList(Task_r.T_task_id)
  268. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: List}
  269. c.ServeJSON()
  270. return
  271. }
  272. // 添加-
  273. func (c *TaskDataController) TaskData_AddS() {
  274. // 验证登录 User_is, User_r
  275. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  276. if !User_is {
  277. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  278. c.ServeJSON()
  279. return
  280. }
  281. T_task_id := c.GetString("T_task_id")
  282. Task_r, is := Task.Read_Task(T_task_id)
  283. if !is {
  284. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  285. c.ServeJSON()
  286. return
  287. }
  288. //T_sn|T_id|T_t|T_rh|T_time?
  289. T_Data := c.GetString("T_Data")
  290. if len(T_Data) < 5 {
  291. c.Data["json"] = lib.JSONS{Code: 202, Msg: "err T_Data!"}
  292. c.ServeJSON()
  293. return
  294. }
  295. T_Data_list := strings.Split(T_Data, "?")
  296. println(len(T_Data_list), "len(T_Data_list)")
  297. var T_Data_list_x = 0
  298. for _, v := range T_Data_list {
  299. // 2022-08-09 14:25:00|27.7|55.2
  300. if len(v) < 5 {
  301. println(v, "len(v) < 5")
  302. continue
  303. }
  304. v_list := strings.Split(v, "|")
  305. is = Task.Add_TaskData(Task_r.T_task_id, v_list[0], v_list[1], v_list[2], v_list[3], v_list[4])
  306. if is {
  307. T_Data_list_x += 1
  308. }
  309. }
  310. System.Add_UserLogs(user_r.T_uuid, "任务数据", "添加数据"+Task_r.T_name, Task_r.T_task_id+"结果:"+string(len(T_Data_list))+"/"+string(T_Data_list_x)+"|=> "+T_Data)
  311. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: strconv.Itoa(T_Data_list_x)}
  312. c.ServeJSON()
  313. return
  314. }
  315. // 添加-
  316. func (c *TaskDataController) TaskData_Add() {
  317. // 验证登录 User_is, User_r
  318. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  319. if !User_is {
  320. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  321. c.ServeJSON()
  322. return
  323. }
  324. T_sn := c.GetString("T_sn")
  325. T_id, _ := c.GetInt("T_id")
  326. T_t, _ := c.GetFloat("T_t")
  327. T_rh, _ := c.GetFloat("T_rh")
  328. T_time := c.GetString("T_time")
  329. T_task_id := c.GetString("T_task_id")
  330. Task_r, is := Task.Read_Task(T_task_id)
  331. if !is {
  332. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  333. c.ServeJSON()
  334. return
  335. }
  336. is = Task.Add_TaskData(Task_r.T_task_id, T_sn, strconv.Itoa(T_id), fmt.Sprintf("%.2f", T_t), fmt.Sprintf("%.2f", T_rh), T_time)
  337. if !is {
  338. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  339. c.ServeJSON()
  340. return
  341. }
  342. System.Add_UserLogs(user_r.T_uuid, "任务数据", "添加数据"+Task_r.T_name, Task_r.T_task_id+"|"+T_sn+"|"+strconv.Itoa(T_id)+"|"+fmt.Sprintf("%.2f", T_t)+"|"+fmt.Sprintf("%.2f", T_rh)+T_time)
  343. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  344. c.ServeJSON()
  345. return
  346. }
  347. // 修改-
  348. func (c *TaskDataController) TaskData_Up() {
  349. // 验证登录 User_is, User_r
  350. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  351. if !User_is {
  352. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  353. c.ServeJSON()
  354. return
  355. }
  356. Id, err := c.GetInt("Id")
  357. if err != nil {
  358. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  359. c.ServeJSON()
  360. return
  361. }
  362. T_t, err := c.GetFloat("T_t")
  363. if err != nil {
  364. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_t 错误!"}
  365. c.ServeJSON()
  366. return
  367. }
  368. T_rh, err := c.GetFloat("T_rh")
  369. if err != nil {
  370. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_rh 错误!"}
  371. c.ServeJSON()
  372. return
  373. }
  374. T_time := c.GetString("T_time")
  375. T_task_id := c.GetString("T_task_id")
  376. Task_r, is := Task.Read_Task(T_task_id)
  377. if !is {
  378. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  379. c.ServeJSON()
  380. return
  381. }
  382. is = Task.Up_TaskData(Task_r.T_task_id, strconv.Itoa(Id), fmt.Sprintf("%.2f", T_t), fmt.Sprintf("%.2f", T_rh), T_time)
  383. if !is {
  384. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  385. c.ServeJSON()
  386. return
  387. }
  388. System.Add_UserLogs(user_r.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)
  389. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  390. c.ServeJSON()
  391. return
  392. }
  393. // 删除-
  394. func (c *TaskDataController) TaskData_Del() {
  395. // 验证登录 User_is, User_r
  396. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  397. if !User_is {
  398. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  399. c.ServeJSON()
  400. return
  401. }
  402. Id, err := c.GetInt("Id")
  403. if err != nil {
  404. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  405. c.ServeJSON()
  406. return
  407. }
  408. T_task_id := c.GetString("T_task_id")
  409. Task_r, is := Task.Read_Task(T_task_id)
  410. if !is {
  411. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  412. c.ServeJSON()
  413. return
  414. }
  415. is = Task.Del_TaskData(Task_r.T_task_id, strconv.Itoa(Id))
  416. if !is {
  417. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  418. c.ServeJSON()
  419. return
  420. }
  421. System.Add_UserLogs(user_r.T_uuid, "任务数据", "删除"+Task_r.T_name, Task_r.T_task_id+"|"+strconv.Itoa(Id))
  422. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  423. c.ServeJSON()
  424. return
  425. }
  426. // 删除-
  427. func (c *TaskDataController) TaskData_Del_t_id() {
  428. // 验证登录 User_is, User_r
  429. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  430. if !User_is {
  431. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  432. c.ServeJSON()
  433. return
  434. }
  435. Id, err := c.GetInt("Id")
  436. if err != nil {
  437. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  438. c.ServeJSON()
  439. return
  440. }
  441. T_task_id := c.GetString("T_task_id")
  442. Task_r, is := Task.Read_Task(T_task_id)
  443. if !is {
  444. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  445. c.ServeJSON()
  446. return
  447. }
  448. is = Task.Del_TaskData_t_id(Task_r.T_task_id, strconv.Itoa(Id))
  449. if !is {
  450. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  451. c.ServeJSON()
  452. return
  453. }
  454. System.Add_UserLogs(user_r.T_uuid, "任务数据", "删除"+Task_r.T_name, Task_r.T_task_id+"|"+strconv.Itoa(Id))
  455. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  456. c.ServeJSON()
  457. return
  458. }
  459. // 列表 - 接口
  460. func (c *TaskDataController) Export_Data_Excel() {
  461. // 验证登录 User_is, User_r
  462. _, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  463. if !User_is {
  464. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  465. c.ServeJSON()
  466. return
  467. }
  468. Time_start := c.GetString("Time_start")
  469. Time_end := c.GetString("Time_end")
  470. T_sn_str := c.GetString("T_sn_list") //865901058809339,865901058815849,865901058818991,865901058810568
  471. //T_id, err := c.GetInt("T_id")
  472. //if err != nil {
  473. // T_id = -1
  474. //
  475. //}
  476. T_task_id := c.GetString("T_task_id")
  477. Task_r, is := Task.Read_Task(T_task_id)
  478. if !is {
  479. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  480. c.ServeJSON()
  481. return
  482. }
  483. T_sn_list := strings.Split(T_sn_str, ",")
  484. DeviceSensor_data_list := []Task.TaskData_{}
  485. for _, v := range T_sn_list {
  486. DeviceSensor_data, _ := Task.Read_TaskData_ById_List(Task_r.T_task_id, v, "", Time_start, Time_end, 1, 9999)
  487. DeviceSensor_data_list = append(DeviceSensor_data_list, DeviceSensor_data...)
  488. }
  489. f := excelize.NewFile() // 设置单元格的值
  490. // 这里设置表头
  491. f.SetCellValue("Sheet1", "A1", "编号")
  492. f.SetCellValue("Sheet1", "B1", "SN")
  493. f.SetCellValue("Sheet1", "C1", "温度℃")
  494. f.SetCellValue("Sheet1", "D1", "湿度%")
  495. f.SetCellValue("Sheet1", "E1", "记录时间")
  496. // 设置列宽
  497. f.SetColWidth("Sheet1", "A", "A", 7)
  498. f.SetColWidth("Sheet1", "B", "B", 20)
  499. f.SetColWidth("Sheet1", "C", "C", 10)
  500. f.SetColWidth("Sheet1", "D", "D", 10)
  501. f.SetColWidth("Sheet1", "E", "E", 22)
  502. line := 1
  503. // 循环写入数据
  504. for _, v := range DeviceSensor_data_list {
  505. line++
  506. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), v.T_id)
  507. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_sn)
  508. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), strconv.FormatFloat(float64(v.T_t), 'f', 1, 64))
  509. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), strconv.FormatFloat(float64(v.T_rh), 'f', 1, 64))
  510. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_time)
  511. }
  512. logs.Println("DeviceSensor_data:", len(DeviceSensor_data_list))
  513. lib.Create_Dir("./ofile")
  514. timeStr := time.Now().Format("20060102150405")
  515. // 保存文件
  516. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  517. logs.Error(lib.FuncName(), err)
  518. }
  519. if !lib.Pload_qiniu("ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx") {
  520. c.Data["json"] = lib.JSONS{Code: 203, Msg: "oss!"}
  521. c.ServeJSON()
  522. return
  523. }
  524. //删除目录
  525. //err = os.Remove("ofile/" + timeStr + ".xlsx")
  526. //if err != nil {
  527. // logs.Error(lib.FuncName(),err)
  528. //}
  529. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: "https://bzdcoldverifyoss.baozhida.cn/" + "ofile/" + timeStr + ".xlsx"}
  530. c.ServeJSON()
  531. return
  532. }
  533. // 列表 - 接口
  534. func (c *TaskDataController) Export_Data_PDF() {
  535. // 验证登录 User_is, User_r
  536. _, User_is := Account.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  537. if !User_is {
  538. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  539. c.ServeJSON()
  540. return
  541. }
  542. Time_start := c.GetString("Time_start")
  543. Time_end := c.GetString("Time_end")
  544. T_sn_str := c.GetString("T_sn_list") //865901058809339,865901058815849,865901058818991,865901058810568
  545. //T_id, err := c.GetInt("T_id")
  546. //if err != nil {
  547. // T_id = -1
  548. //
  549. //}
  550. T_task_id := c.GetString("T_task_id")
  551. Task_r, is := Task.Read_Task(T_task_id)
  552. if !is {
  553. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  554. c.ServeJSON()
  555. return
  556. }
  557. dcList, _ := Device.Read_DeviceClassList_OrderList(Task_r.T_class, "", "", "", 0, 9999)
  558. // 查询设备列表
  559. // 保存布局编号和校准证书对应关系
  560. var deviceCertificateMap = make(map[string]string) // t_id, T_Certificate_sn
  561. for _, v := range dcList {
  562. deviceCertificateMap[v.T_id] = v.T_Certificate_sn
  563. }
  564. T_sn_list := strings.Split(T_sn_str, ",")
  565. DeviceSensor_data_list := []Task.TaskData_{}
  566. for _, v := range T_sn_list {
  567. DeviceSensor_data, _ := Task.Read_TaskData_ById_List(Task_r.T_task_id, v, "", Time_start, Time_end, 1, 9999)
  568. DeviceSensor_data_list = append(DeviceSensor_data_list, DeviceSensor_data...)
  569. for i := 0; i < len(DeviceSensor_data_list); i++ {
  570. DeviceSensor_data_list[i].T_Certificate_sn = deviceCertificateMap[DeviceSensor_data_list[i].T_id]
  571. }
  572. }
  573. //------
  574. var err error
  575. pdf := &gopdf.GoPdf{}
  576. pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
  577. //err = GetFont(pdf, "LiberationSerif-Regular.ttf")
  578. //if err != nil {
  579. // log.Fatalln(err)
  580. //}
  581. //err = pdf.SetFont("Ubuntu-L", "", 14)
  582. //if err != nil {
  583. // log.Fatalln(err)
  584. //}
  585. err = pdf.AddTTFFont("simsun", "static/fonts/三极行楷简体-粗.ttf")
  586. if err != nil {
  587. c.Data["json"] = lib.JSONS{Code: 204, Msg: "ok!", Data: err}
  588. c.ServeJSON()
  589. return
  590. }
  591. err = pdf.SetFont("simsun", "", 24)
  592. if err != nil {
  593. c.Data["json"] = lib.JSONS{Code: 205, Msg: "ok!", Data: err}
  594. c.ServeJSON()
  595. return
  596. }
  597. pdf.SetGrayFill(0.5)
  598. pdf.SetMargins(0, 20, 0, 20)
  599. pdf.AddPage()
  600. //use path
  601. //pdf.Image("logo.png", 100, 50, &gopdf.Rect{W: 50, H: 50})
  602. textw, _ := pdf.MeasureTextWidth(Task_r.T_name)
  603. pdf.SetX((595 / 2) - (textw / 2))
  604. pdf.SetY(40)
  605. pdf.Text(Task_r.T_name)
  606. // 线
  607. pdf.SetLineWidth(2)
  608. pdf.SetLineType("dashed")
  609. pdf.Line(10, 60, 585, 60)
  610. err = pdf.AddTTFFont("wts", "static/fonts/MiSans-Medium.ttf")
  611. if err != nil {
  612. c.Data["json"] = lib.JSONS{Code: 206, Msg: "ok!", Data: err}
  613. c.ServeJSON()
  614. return
  615. }
  616. err = pdf.SetFont("wts", "", 12)
  617. if err != nil {
  618. c.Data["json"] = lib.JSONS{Code: 207, Msg: "ok!", Data: err}
  619. c.ServeJSON()
  620. return
  621. }
  622. t_ := Time_start + " / " + Time_end
  623. if len(Time_start) == 0 {
  624. t_ = "所有数据"
  625. }
  626. //fmt.Sprintf(" %.1f ", v.T_t)
  627. lib.RectFillColor(pdf, "提取数据时间段["+t_+"]", 14, 22, 80, 550, 40, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  628. lib.RectFillColor(pdf, "布局编号", 12, 22, 120, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  629. lib.RectFillColor(pdf, "证书编号", 12, 92, 120, 150, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  630. lib.RectFillColor(pdf, "温度℃", 12, 242, 120, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  631. lib.RectFillColor(pdf, "湿度%", 12, 342, 120, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  632. //lib.RectFillColor(pdf, "温度范围", 12, 272, 120, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  633. //lib.RectFillColor(pdf, "湿度范围", 12, 362, 120, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  634. lib.RectFillColor(pdf, "记录时间", 12, 442, 120, 130, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  635. var y float64 = 140
  636. err = pdf.SetFont("wts", "", 10)
  637. if err != nil {
  638. c.Data["json"] = lib.JSONS{Code: 207, Msg: "ok!", Data: err}
  639. c.ServeJSON()
  640. return
  641. }
  642. for _, v := range DeviceSensor_data_list {
  643. //text := fmt.Sprintf(" %d ", i+1)
  644. var textH float64 = 25 // if text height is 25px.
  645. pdf.SetNewY(y, textH)
  646. y = pdf.GetY()
  647. //pdf.SetX(x) // must after pdf.SetNewY() called.
  648. //err = pdf.Text(text)
  649. //if err != nil {
  650. // log.Fatalln(err)
  651. //}
  652. T_t := fmt.Sprintf(" %.1f ", v.T_t)
  653. T_rh := fmt.Sprintf(" %.1f ", v.T_rh)
  654. T_time := fmt.Sprintf("%s", v.T_time)
  655. lib.RectFillColor(pdf, v.T_id, 10, 22, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  656. lib.RectFillColor(pdf, v.T_Certificate_sn, 10, 92, y, 150, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  657. lib.RectFillColor(pdf, T_t, 10, 242, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  658. lib.RectFillColor(pdf, T_rh, 10, 342, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  659. lib.RectFillColor(pdf, T_time, 10, 442, y, 130, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  660. y += 20
  661. }
  662. timeStr := "ofile/" + time.Now().Format("20060102150405") + ".pdf"
  663. err = pdf.WritePdf(timeStr)
  664. if err != nil {
  665. c.Data["json"] = lib.JSONS{Code: 207, Msg: "ok!", Data: err}
  666. c.ServeJSON()
  667. return
  668. }
  669. if !lib.Pload_qiniu(timeStr, timeStr) {
  670. c.Data["json"] = lib.JSONS{Code: 203, Msg: "oss!"}
  671. c.ServeJSON()
  672. return
  673. }
  674. //删除目录
  675. err = os.Remove(timeStr)
  676. if err != nil {
  677. logs.Error(lib.FuncName(), err)
  678. }
  679. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: "https://bzdcoldverifyoss.baozhida.cn/" + timeStr}
  680. c.ServeJSON()
  681. return
  682. }
  683. // 列表 - 接口
  684. func (c *TaskDataController) Check() {
  685. // 验证登录 User_is, User_r
  686. _, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  687. if !User_is {
  688. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  689. c.ServeJSON()
  690. return
  691. }
  692. T_task_id := c.GetString("T_task_id")
  693. Task_r, is := Task.Read_Task(T_task_id)
  694. if !is {
  695. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  696. c.ServeJSON()
  697. return
  698. }
  699. type R_JSONS struct {
  700. T_sn string // SN
  701. T_id string // 编号
  702. T_unm int // 数据量
  703. T_time_interval int64 // 时间间隔
  704. Time_start string // 开始
  705. Time_end string // 结束
  706. Result int // 200 OK, 201 可以补 , 202 不能补
  707. Result_str string // 提示内容
  708. Result_Time_start string // 2022-8-05 21:01
  709. Result_Time_defect int64 // 缺失时间间隔
  710. }
  711. var r_jsons []R_JSONS
  712. List := Task.Read_TaskData_ById_ClassList(Task_r.T_task_id)
  713. for _, v := range List {
  714. var r_json R_JSONS
  715. r_json.T_sn = v.T_sn
  716. r_json.Result = 200
  717. DeviceSensor_data := Task.Read_TaskData_ById_List_(Task_r.T_task_id, v.T_sn)
  718. r_json.T_unm = len(DeviceSensor_data)
  719. if r_json.T_unm > 2 {
  720. r_json.T_id = DeviceSensor_data[0].T_id
  721. r_json.Time_end = DeviceSensor_data[len(DeviceSensor_data)-1].T_time
  722. r_json.Time_start = DeviceSensor_data[0].T_time
  723. formatTime_a, _ := time.Parse("2006-1-2 15:04", DeviceSensor_data[0].T_time)
  724. formatTime_b, _ := time.Parse("2006-1-2 15:04", DeviceSensor_data[1].T_time)
  725. formatTime_x := formatTime_b.Unix() - formatTime_a.Unix()
  726. r_json.T_time_interval = formatTime_x
  727. println("formatTime_x:", v.T_id, v.T_sn, formatTime_x)
  728. if formatTime_x > 60*30 || formatTime_x < 60 {
  729. r_json.Result = 202
  730. r_json.Result_str = "数据异常(数据的第一个时间与第二个时间相差 " + strconv.FormatInt(formatTime_x, 10) + "秒),必须大于60秒,小于30分钟"
  731. r_jsons = append(r_jsons, r_json)
  732. continue
  733. }
  734. for data_i := 2; data_i < len(DeviceSensor_data); data_i++ {
  735. formatTime_a = formatTime_b
  736. formatTime_b, _ = time.Parse("2006-1-2 15:04", DeviceSensor_data[data_i].T_time)
  737. formatTime_ := formatTime_b.Unix() - formatTime_a.Unix()
  738. println("formatTime_x-:", formatTime_)
  739. if formatTime_ < formatTime_x {
  740. r_json.Result = 202
  741. r_json.Result_str = "开始时间:" + DeviceSensor_data[data_i-1].T_time + " 离下一条时间间隔:" + strconv.FormatInt(formatTime_, 10) + "秒,间隔小于时间间隔,不能自动补充"
  742. break
  743. }
  744. if formatTime_x != formatTime_ {
  745. if formatTime_ > 60*30 {
  746. r_json.Result = 202
  747. r_json.Result_str = "开始时间:" + DeviceSensor_data[data_i-1].T_time + " 离下一条时间间隔:" + strconv.FormatInt(formatTime_, 10) + "秒,相差时间大于30分钟,不能自动补充"
  748. break
  749. }
  750. r_json.Result = 201
  751. r_json.Result_str = "开始时间:" + DeviceSensor_data[data_i-1].T_time + " 离下一条时间间隔:" + strconv.FormatInt(formatTime_, 10) + "秒"
  752. r_json.Result_Time_start = DeviceSensor_data[data_i-1].T_time
  753. r_json.Result_Time_defect = formatTime_
  754. println(r_json.Result_str)
  755. break
  756. }
  757. }
  758. } else {
  759. r_json.Result = 202
  760. r_json.Result_str = "数据量太少 必须大于 2条以上!"
  761. }
  762. r_jsons = append(r_jsons, r_json)
  763. }
  764. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  765. c.ServeJSON()
  766. return
  767. }
  768. func (c *TaskDataController) TaskData_Temperature_Pdf() {
  769. // 验证登录管理员 User_is, User_r
  770. _, User_Admin_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  771. // 验证登录用户 User_is, User_r
  772. _, User_is := Account.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  773. if !User_Admin_is && !User_is {
  774. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  775. c.ServeJSON()
  776. return
  777. }
  778. Time_start := c.GetString("Time_start")
  779. Time_end := c.GetString("Time_end")
  780. T_sn := c.GetString("T_sn")
  781. T_id := c.GetString("T_id")
  782. T_task_id := c.GetString("T_task_id")
  783. err := c.TaskData_Pdf(Time_start, Time_end, T_task_id, T_sn, T_id, Task.Temperature)
  784. if err != nil {
  785. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  786. c.ServeJSON()
  787. return
  788. }
  789. }
  790. func (c *TaskDataController) TaskData_Humidity_Pdf() {
  791. // 验证登录管理员 User_is, User_r
  792. _, User_Admin_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  793. // 验证登录用户 User_is, User_r
  794. _, User_is := Account.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  795. if !User_Admin_is && !User_is {
  796. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  797. c.ServeJSON()
  798. return
  799. }
  800. Time_start := c.GetString("Time_start")
  801. Time_end := c.GetString("Time_end")
  802. T_sn := c.GetString("T_sn")
  803. T_id := c.GetString("T_id")
  804. T_task_id := c.GetString("T_task_id")
  805. err := c.TaskData_Pdf(Time_start, Time_end, T_task_id, T_sn, T_id, Task.Humidity)
  806. if err != nil {
  807. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  808. c.ServeJSON()
  809. return
  810. }
  811. }
  812. func (c *TaskDataController) TaskData_Pdf(Time_start, Time_end, T_task_id, T_sn, T_id, T_type string) (err error) {
  813. Task_r, is := Task.Read_Task(T_task_id)
  814. if !is {
  815. return errors.New("T_task_id 错误!")
  816. }
  817. user, is := Account.Read_User(Task_r.T_uuid)
  818. if !is {
  819. return errors.New("Task uuid 错误!")
  820. }
  821. if Task_r.T_delivery_state == 2 {
  822. return errors.New("数据采集中,请稍后!")
  823. }
  824. pdf := &gopdf.GoPdf{}
  825. pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
  826. err = pdf.AddTTFFont("wts", "static/fonts/MiSans-Medium.ttf")
  827. if err != nil {
  828. return
  829. }
  830. err = pdf.SetFont("wts", "", 15)
  831. if err != nil {
  832. return
  833. }
  834. pdf.SetGrayFill(0.5)
  835. pdf.SetMargins(0, 20, 0, 20)
  836. pdf.AddPage()
  837. imgH, _ := gopdf.ImageHolderByPath("./static/logo.jpg")
  838. err = pdf.ImageByHolder(imgH, 10, 10, &gopdf.Rect{W: 93, H: 32})
  839. name := user.T_name
  840. var y float64 = 40
  841. textw, _ := pdf.MeasureTextWidth(name)
  842. pdf.SetX((595 / 2) - (textw / 2))
  843. pdf.SetY(y)
  844. pdf.Text(name)
  845. y += 20
  846. T_type_name := ""
  847. if T_type == Task.Temperature {
  848. T_type_name = "温度"
  849. }
  850. if T_type == Task.Humidity {
  851. T_type_name = "湿度"
  852. }
  853. title := Task_r.T_name + T_type_name + "验证数据"
  854. textw, _ = pdf.MeasureTextWidth(title)
  855. pdf.SetX((595 / 2) - (textw / 2))
  856. pdf.SetY(y)
  857. pdf.Text(title)
  858. // 查询设备列表
  859. //dcList, _ := Device.Read_DeviceClassList_OrderList(Task_r.T_class, T_sn, T_id, "", 0, 9999)
  860. dcList := []Device.DeviceClassList{}
  861. dataList, _ := Task.Read_TaskData_ById_List(Task_r.T_task_id, T_sn, T_id, Time_start, Time_end, 0, 9999)
  862. dataListMap := make(map[string][]Task.TaskData_)
  863. dcListMap := make(map[string]string)
  864. for _, data := range dataList {
  865. dataListMap[data.T_id] = append(dataListMap[data.T_id], data)
  866. if _, ok := dcListMap[data.T_id]; !ok {
  867. dcListMap[data.T_id] = data.T_sn
  868. }
  869. }
  870. for id, sn := range dcListMap {
  871. dcList = append(dcList, Device.DeviceClassList{T_id: id, T_sn: sn})
  872. }
  873. for id, list := range dataListMap {
  874. sort.Slice(list, func(i, j int) bool {
  875. return list[i].T_time < list[j].T_time
  876. })
  877. dataListMap[id] = list
  878. }
  879. dataMap := make(map[string]string)
  880. timeMap := make(map[string]bool)
  881. for _, data := range dataList {
  882. k := fmt.Sprintf("%s,%s", data.T_time, data.T_id)
  883. if T_type == Task.Temperature {
  884. dataMap[k] = fmt.Sprintf("%.1f", data.T_t)
  885. }
  886. if T_type == Task.Humidity {
  887. dataMap[k] = fmt.Sprintf("%.1f", data.T_rh)
  888. }
  889. if _, ok := timeMap[data.T_time]; !ok {
  890. timeMap[data.T_time] = true
  891. }
  892. }
  893. var timeList []string
  894. for k, _ := range timeMap {
  895. timeList = append(timeList, k)
  896. }
  897. sort.Slice(timeList, func(i, j int) bool {
  898. return timeList[i] < timeList[j]
  899. })
  900. err = pdf.SetFont("wts", "", 12)
  901. if err != nil {
  902. return
  903. }
  904. if len(timeList) > 0 {
  905. y += 20
  906. timeStr := fmt.Sprintf("%s - %s", timeList[0], timeList[len(timeList)-1])
  907. textw, _ = pdf.MeasureTextWidth(timeStr)
  908. pdf.SetX((595 / 2) - (textw / 2))
  909. pdf.SetY(y)
  910. pdf.Text(timeStr)
  911. }
  912. y += 20
  913. var x float64 = 10
  914. var w float64 = 120
  915. err = pdf.SetFont("wts", "", 10)
  916. if err != nil {
  917. return
  918. }
  919. idWidthMap := make(map[string]float64)
  920. dcList1 := make([]Device.DeviceClassList, 0)
  921. for _, list := range dcList {
  922. idw, _ := pdf.MeasureTextWidth(list.T_id)
  923. if !lib.IsNumeric(list.T_id) {
  924. dcList1 = append(dcList1, list)
  925. }
  926. if idw > 20 {
  927. idWidthMap[list.T_id] = idw + 12
  928. } else {
  929. idWidthMap[list.T_id] = 30.3
  930. }
  931. }
  932. sort.Slice(dcList1, func(i, j int) bool {
  933. return dcList1[i].T_id < dcList1[j].T_id
  934. })
  935. for _, dc := range dcList1 {
  936. dataList2, _ := dataListMap[dc.T_id]
  937. err = pdf.SetFont("wts", "", 15)
  938. if err != nil {
  939. return
  940. }
  941. var textH float64 = 20 // if text height is 25px.
  942. y += 20
  943. pdf.SetNewY(y, textH)
  944. y = pdf.GetY()
  945. if y > 790 {
  946. pdf.AddPage()
  947. y = 30
  948. }
  949. textw, _ = pdf.MeasureTextWidth(dc.T_id)
  950. pdf.SetX((595 / 2) - (textw / 2))
  951. pdf.SetY(y)
  952. pdf.Text(dc.T_id)
  953. y += 10
  954. err = pdf.SetFont("wts", "", 10)
  955. if err != nil {
  956. return
  957. }
  958. pdf.SetNewY(y, textH)
  959. y = pdf.GetY()
  960. if y < 790 {
  961. x = 10
  962. for i := 0; i < 4; i++ {
  963. w = 113.8
  964. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  965. x += w
  966. w = 30
  967. lib.RectFillColor(pdf, T_type_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  968. x += w
  969. }
  970. y += 20
  971. } else {
  972. pdf.AddPage()
  973. y = 20
  974. x = 10
  975. for i := 0; i < 4; i++ {
  976. w = 113.8
  977. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  978. x += w
  979. w = 30
  980. lib.RectFillColor(pdf, T_type_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  981. x += w
  982. }
  983. y += 20
  984. }
  985. for i := 0; i < len(dataList2); i++ {
  986. temp := i % 4
  987. x = 10 + 143.8*float64(temp)
  988. w = 113.8
  989. lib.RectFillColor(pdf, dataList2[i].T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  990. x += w
  991. w = 30
  992. if T_type == Task.Temperature {
  993. lib.RectFillColor(pdf, fmt.Sprintf("%.1f", dataList2[i].T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  994. }
  995. if T_type == Task.Humidity {
  996. lib.RectFillColor(pdf, fmt.Sprintf("%.1f", dataList2[i].T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  997. }
  998. if temp == 3 {
  999. y += 20
  1000. pdf.SetNewY(y, textH)
  1001. y = pdf.GetY()
  1002. // 每页添加表头
  1003. if y == 20 {
  1004. x = 10
  1005. for k := 0; k < 4; k++ {
  1006. w = 113.8
  1007. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1008. x += w
  1009. w = 30
  1010. lib.RectFillColor(pdf, T_type_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1011. x += w
  1012. }
  1013. y += 20
  1014. }
  1015. }
  1016. }
  1017. y += 25
  1018. }
  1019. //chunks := chunkBy(dcList, 15)
  1020. y += 20
  1021. dcList2 := listSubtract(dcList, dcList1)
  1022. sort.Slice(dcList2, func(i, j int) bool {
  1023. return dcList2[i].T_id < dcList2[j].T_id
  1024. })
  1025. chunks := splitData(dcList2, 454.5, idWidthMap)
  1026. for _, list := range chunks {
  1027. var textH float64 = 20 // if text height is 25px.
  1028. pdf.SetNewY(y, textH)
  1029. y = pdf.GetY()
  1030. if y < 790 {
  1031. x = 10
  1032. w = 120.7
  1033. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1034. x += w
  1035. w = 30.3
  1036. for _, v2 := range list {
  1037. w = idWidthMap[v2.T_id]
  1038. lib.RectFillColor(pdf, v2.T_id, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1039. x += w
  1040. }
  1041. y += 20
  1042. } else {
  1043. pdf.AddPage()
  1044. y = 20
  1045. }
  1046. for _, t := range timeList {
  1047. x = 10
  1048. textH = 20 // if text height is 25px.
  1049. pdf.SetNewY(y, textH)
  1050. y = pdf.GetY()
  1051. if y == 20 {
  1052. w = 120.7
  1053. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1054. x += w
  1055. w = 30.3
  1056. for _, v2 := range list {
  1057. w = idWidthMap[v2.T_id]
  1058. lib.RectFillColor(pdf, v2.T_id, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1059. x += w
  1060. }
  1061. y += 20
  1062. x = 10
  1063. }
  1064. w = 120.7
  1065. lib.RectFillColor(pdf, t, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1066. x += w
  1067. w = 30.3
  1068. for _, v := range list {
  1069. t_t := dataMap[fmt.Sprintf("%s,%s", t, v.T_id)]
  1070. w = idWidthMap[v.T_id]
  1071. lib.RectFillColor(pdf, t_t, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1072. x += w
  1073. }
  1074. y += 20
  1075. }
  1076. y += 20
  1077. }
  1078. filename := time.Now().Format("20060102150405") + ".pdf"
  1079. timeStr := "ofile/" + T_type_name + filename
  1080. err = pdf.WritePdf(timeStr)
  1081. if err != nil {
  1082. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  1083. c.ServeJSON()
  1084. return
  1085. }
  1086. // 上传 OSS
  1087. //url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr, timeStr)
  1088. //url, is := NatsServer.Qiniu_UploadFile("/Users/zoie/work/bzd_project/ColdVerify_server/"+timeStr, timeStr)
  1089. //if !is {
  1090. // err = errors.New("oss!")
  1091. // return
  1092. //}
  1093. defer func() {
  1094. //删除目录
  1095. os.Remove(timeStr)
  1096. }()
  1097. c.Ctx.Output.Download(timeStr)
  1098. //c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1099. //c.ServeJSON()
  1100. //return
  1101. return nil
  1102. }
  1103. func listSubtract(sliceA, sliceB []Device.DeviceClassList) []Device.DeviceClassList {
  1104. // 创建一个集合用于存储 sliceB 的元素
  1105. elementsToRemove := make(map[string]Device.DeviceClassList)
  1106. for _, item := range sliceB {
  1107. elementsToRemove[item.T_id] = item
  1108. }
  1109. // 构建新切片,包含 sliceA 中不在 elementsToRemove 中的元素
  1110. var result []Device.DeviceClassList
  1111. for _, item := range sliceA {
  1112. if _, found := elementsToRemove[item.T_id]; !found {
  1113. result = append(result, item)
  1114. }
  1115. }
  1116. return result
  1117. }
  1118. func chunkBy1[T any](list []T, size int) [][]T {
  1119. var chunks [][]T
  1120. for size < len(list) {
  1121. list, chunks = list[size:], append(chunks, list[0:size:size])
  1122. }
  1123. return append(chunks, list)
  1124. }
  1125. func splitData(data []Device.DeviceClassList, threshold float64, idWidthMap map[string]float64) [][]Device.DeviceClassList {
  1126. var result [][]Device.DeviceClassList
  1127. var currentBatch []Device.DeviceClassList
  1128. var currentSum float64
  1129. for _, item := range data {
  1130. wd := idWidthMap[item.T_id]
  1131. if currentSum+wd > threshold+0.1 {
  1132. // 当前批次超过阈值,切分
  1133. result = append(result, currentBatch)
  1134. // 重置当前批次和当前总和
  1135. currentBatch = []Device.DeviceClassList{}
  1136. currentSum = 0
  1137. }
  1138. currentBatch = append(currentBatch, item)
  1139. currentSum += wd
  1140. }
  1141. // 添加最后一批
  1142. if len(currentBatch) > 0 {
  1143. result = append(result, currentBatch)
  1144. }
  1145. return result
  1146. }