TaskData.go 40 KB

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