TaskData.go 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  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. func addStampImage(pdf *gopdf.GoPdf, x, y float64) {
  1200. imagePath := "./static/commonSeal.jpg"
  1201. if x < 10 {
  1202. x = 10
  1203. }
  1204. if y < 20 {
  1205. y = 20
  1206. }
  1207. err := pdf.Image(imagePath, x, y, &gopdf.Rect{W: 123, H: 128.6})
  1208. if err != nil {
  1209. log.Print(err.Error())
  1210. }
  1211. }
  1212. func listSubtract(sliceA, sliceB []Device.DeviceClassList) []Device.DeviceClassList {
  1213. // 创建一个集合用于存储 sliceB 的元素
  1214. elementsToRemove := make(map[string]Device.DeviceClassList)
  1215. for _, item := range sliceB {
  1216. elementsToRemove[item.T_id] = item
  1217. }
  1218. // 构建新切片,包含 sliceA 中不在 elementsToRemove 中的元素
  1219. var result []Device.DeviceClassList
  1220. for _, item := range sliceA {
  1221. if _, found := elementsToRemove[item.T_id]; !found {
  1222. result = append(result, item)
  1223. }
  1224. }
  1225. return result
  1226. }
  1227. func chunkBy1[T any](list []T, size int) [][]T {
  1228. var chunks [][]T
  1229. for size < len(list) {
  1230. list, chunks = list[size:], append(chunks, list[0:size:size])
  1231. }
  1232. return append(chunks, list)
  1233. }
  1234. func splitData(data []Device.DeviceClassList, threshold float64, idWidthMap map[string]float64) [][]Device.DeviceClassList {
  1235. var result [][]Device.DeviceClassList
  1236. var currentBatch []Device.DeviceClassList
  1237. var currentSum float64
  1238. for _, item := range data {
  1239. wd := idWidthMap[item.T_id]
  1240. if currentSum+wd > threshold+0.1 {
  1241. // 当前批次超过阈值,切分
  1242. result = append(result, currentBatch)
  1243. // 重置当前批次和当前总和
  1244. currentBatch = []Device.DeviceClassList{}
  1245. currentSum = 0
  1246. }
  1247. currentBatch = append(currentBatch, item)
  1248. currentSum += wd
  1249. }
  1250. // 添加最后一批
  1251. if len(currentBatch) > 0 {
  1252. result = append(result, currentBatch)
  1253. }
  1254. return result
  1255. }
  1256. func AddWatermark(task Task.Task, pdf *gopdf.GoPdf) {
  1257. // 设置透明度 (通过绘制透明背景实现)
  1258. // 使用浅色字体来模拟透明度
  1259. //originalX := pdf.GetX()
  1260. //originalY := pdf.GetY()
  1261. //grayColor := color.RGBA{R: 200, G: 200, B: 200, A: 255} // 浅灰色,接近透明效果
  1262. //pdf.SetTextColor(grayColor.R, grayColor.G, grayColor.B) // 设置字体颜色
  1263. //pdf.SetFontSize(50)
  1264. //
  1265. //// 设置水印的重复布局
  1266. //text := "水印"
  1267. //xOffset := 180.0 // X 坐标的偏移量
  1268. //yOffset := 180.0 // Y 坐标的偏移量
  1269. //angle := 45.0 // 旋转角度(度)
  1270. //
  1271. //// 循环绘制水印
  1272. //for y := 0.0; y < 780; y += yOffset { // 842 是 A4 页面高度的像素数 (72 DPI)
  1273. // for x := -100.0; x < 595; x += xOffset { // 595 是 A4 页面宽度的像素数 (72 DPI)
  1274. // // 保存当前的绘制状态
  1275. // pdf.SetX(x)
  1276. // pdf.SetY(y)
  1277. // pdf.Rotate(angle, x+100, y+50) // 旋转水印
  1278. // pdf.Cell(nil, text)
  1279. // pdf.RotateReset() // 恢复旋转状态
  1280. // }
  1281. //}
  1282. //
  1283. //grayColor2 := color.RGBA{R: 0, G: 0, B: 0, A: 0} // 浅灰色,接近透明效果
  1284. //pdf.SetTextColor(grayColor2.R, grayColor2.G, grayColor2.B) // 设置字体颜色
  1285. //pdf.SetFontSize(10)
  1286. //pdf.SetX(originalX)
  1287. //pdf.SetY(originalY)
  1288. }