TaskData.go 38 KB

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