TaskData.go 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  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. pdf.AddPage()
  974. y = 30
  975. } else {
  976. if index > 0 {
  977. y += 10
  978. }
  979. }
  980. textw, _ = pdf.MeasureTextWidth(dc.T_id)
  981. pdf.SetX((595 / 2) - (textw / 2))
  982. pdf.SetY(y)
  983. pdf.Text(dc.T_id)
  984. y += 10
  985. err = pdf.SetFont("wts", "", 10)
  986. if err != nil {
  987. return
  988. }
  989. if y < 790 {
  990. x = 10
  991. for i := 0; i < 4; i++ {
  992. w = 113.8
  993. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  994. x += w
  995. w = 30
  996. lib.RectFillColor(pdf, T_type_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  997. x += w
  998. }
  999. y += 20
  1000. } else {
  1001. addStampImage(pdf, 455, y-150)
  1002. pdf.AddPage()
  1003. y = 20
  1004. x = 10
  1005. for i := 0; i < 4; i++ {
  1006. w = 113.8
  1007. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1008. x += w
  1009. w = 30
  1010. lib.RectFillColor(pdf, T_type_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1011. x += w
  1012. }
  1013. y += 20
  1014. }
  1015. for i := 0; i < len(dataList2); i++ {
  1016. if y > 790 {
  1017. addStampImage(pdf, 455, y-130)
  1018. pdf.AddPage()
  1019. y = pdf.GetY()
  1020. x = 10
  1021. for k := 0; k < 4; k++ {
  1022. w = 113.8
  1023. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1024. x += w
  1025. w = 30
  1026. lib.RectFillColor(pdf, T_type_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1027. x += w
  1028. }
  1029. y += 20
  1030. }
  1031. temp = i % 4
  1032. // 每页添加表头
  1033. if y == 20 && temp == 0 {
  1034. x = 10
  1035. for k := 0; k < 4; k++ {
  1036. w = 113.8
  1037. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1038. x += w
  1039. w = 30
  1040. lib.RectFillColor(pdf, T_type_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1041. x += w
  1042. }
  1043. y += 20
  1044. }
  1045. x = 10 + 143.8*float64(temp)
  1046. w = 113.8
  1047. lib.RectFillColor(pdf, dataList2[i].T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1048. x += w
  1049. w = 30
  1050. if T_type == Task.Temperature {
  1051. lib.RectFillColor(pdf, fmt.Sprintf("%.1f", dataList2[i].T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1052. }
  1053. if T_type == Task.Humidity {
  1054. lib.RectFillColor(pdf, fmt.Sprintf("%.1f", dataList2[i].T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1055. }
  1056. //if y > 760 {
  1057. // fmt.Println("=====y:", y)
  1058. //}
  1059. //if y > 790 {
  1060. // addStampImage(pdf, 455, y-130)
  1061. //}
  1062. if temp == 3 {
  1063. y += 20
  1064. pdf.SetNewY(y, textH)
  1065. y = pdf.GetY()
  1066. }
  1067. }
  1068. }
  1069. if len(dcList2) > 0 {
  1070. if temp != 3 {
  1071. y += 20
  1072. }
  1073. y += 40
  1074. }
  1075. sort.Slice(dcList2, func(i, j int) bool {
  1076. return dcList2[i].T_id < dcList2[j].T_id
  1077. })
  1078. chunks := splitData(dcList2, 454.5, idWidthMap)
  1079. for i, list := range chunks {
  1080. if i > 0 {
  1081. y += 40
  1082. }
  1083. if y < 790 {
  1084. x = 10
  1085. w = 120.7
  1086. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1087. x += w
  1088. w = 30.3
  1089. for _, v2 := range list {
  1090. w = idWidthMap[v2.T_id]
  1091. lib.RectFillColor(pdf, v2.T_id, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1092. x += w
  1093. }
  1094. y += 20
  1095. } else {
  1096. addStampImage(pdf, 455, y-150)
  1097. pdf.AddPage()
  1098. y = 20
  1099. }
  1100. StampImageX := x
  1101. for j, t := range timeList2 {
  1102. if j > 0 {
  1103. y += 20
  1104. }
  1105. if y >= 790 {
  1106. addStampImage(pdf, StampImageX-130, y-130)
  1107. pdf.AddPage()
  1108. y = pdf.GetY()
  1109. x = 10
  1110. w = 120.7
  1111. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1112. x += w
  1113. w = 30.3
  1114. for _, v2 := range list {
  1115. w = idWidthMap[v2.T_id]
  1116. lib.RectFillColor(pdf, v2.T_id, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1117. x += w
  1118. }
  1119. y += 20
  1120. x = 10
  1121. }
  1122. x = 10
  1123. if y == 20 {
  1124. w = 120.7
  1125. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1126. x += w
  1127. w = 30.3
  1128. for _, v2 := range list {
  1129. w = idWidthMap[v2.T_id]
  1130. lib.RectFillColor(pdf, v2.T_id, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1131. x += w
  1132. }
  1133. y += 20
  1134. x = 10
  1135. }
  1136. w = 120.7
  1137. lib.RectFillColor(pdf, t, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1138. x += w
  1139. w = 30.3
  1140. for _, v := range list {
  1141. t_t := dataMap[fmt.Sprintf("%s,%s", t, v.T_id)]
  1142. w = idWidthMap[v.T_id]
  1143. lib.RectFillColor(pdf, t_t, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1144. x += w
  1145. }
  1146. StampImageX = x
  1147. }
  1148. }
  1149. if y <= 790 {
  1150. if y < 150 {
  1151. y = 20
  1152. } else {
  1153. y = y - 145
  1154. }
  1155. x = x - 135
  1156. if len(dcList2) == 0 {
  1157. x = 455
  1158. }
  1159. addStampImage(pdf, x, y)
  1160. } else if y > 790 && len(dcList2) == 0 {
  1161. if y < 150 {
  1162. y = 20
  1163. } else {
  1164. y = y - 145
  1165. }
  1166. addStampImage(pdf, 455, y)
  1167. }
  1168. filename := time.Now().Format("20060102150405") + ".pdf"
  1169. timeStr := "ofile/" + T_type_name + filename
  1170. err = pdf.WritePdf(timeStr)
  1171. if err != nil {
  1172. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  1173. c.ServeJSON()
  1174. return
  1175. }
  1176. // 上传 OSS
  1177. //url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr, timeStr)
  1178. //url, is := NatsServer.Qiniu_UploadFile("/Users/zoie/work/bzd_project/ColdVerify_server/"+timeStr, timeStr)
  1179. //if !is {
  1180. // err = errors.New("oss!")
  1181. // return
  1182. //}
  1183. defer func() {
  1184. //删除目录
  1185. os.Remove(timeStr)
  1186. }()
  1187. c.Ctx.Output.Download(timeStr)
  1188. //c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1189. //c.ServeJSON()
  1190. //return
  1191. return nil
  1192. }
  1193. func addStampImage(pdf *gopdf.GoPdf, x, y float64) {
  1194. imagePath := "./static/commonSeal.jpg"
  1195. if x < 10 {
  1196. x = 10
  1197. }
  1198. if y < 20 {
  1199. y = 20
  1200. }
  1201. err := pdf.Image(imagePath, x, y, &gopdf.Rect{W: 123, H: 128.6})
  1202. if err != nil {
  1203. log.Print(err.Error())
  1204. }
  1205. }
  1206. func listSubtract(sliceA, sliceB []Device.DeviceClassList) []Device.DeviceClassList {
  1207. // 创建一个集合用于存储 sliceB 的元素
  1208. elementsToRemove := make(map[string]Device.DeviceClassList)
  1209. for _, item := range sliceB {
  1210. elementsToRemove[item.T_id] = item
  1211. }
  1212. // 构建新切片,包含 sliceA 中不在 elementsToRemove 中的元素
  1213. var result []Device.DeviceClassList
  1214. for _, item := range sliceA {
  1215. if _, found := elementsToRemove[item.T_id]; !found {
  1216. result = append(result, item)
  1217. }
  1218. }
  1219. return result
  1220. }
  1221. func chunkBy1[T any](list []T, size int) [][]T {
  1222. var chunks [][]T
  1223. for size < len(list) {
  1224. list, chunks = list[size:], append(chunks, list[0:size:size])
  1225. }
  1226. return append(chunks, list)
  1227. }
  1228. func splitData(data []Device.DeviceClassList, threshold float64, idWidthMap map[string]float64) [][]Device.DeviceClassList {
  1229. var result [][]Device.DeviceClassList
  1230. var currentBatch []Device.DeviceClassList
  1231. var currentSum float64
  1232. for _, item := range data {
  1233. wd := idWidthMap[item.T_id]
  1234. if currentSum+wd > threshold+0.1 {
  1235. // 当前批次超过阈值,切分
  1236. result = append(result, currentBatch)
  1237. // 重置当前批次和当前总和
  1238. currentBatch = []Device.DeviceClassList{}
  1239. currentSum = 0
  1240. }
  1241. currentBatch = append(currentBatch, item)
  1242. currentSum += wd
  1243. }
  1244. // 添加最后一批
  1245. if len(currentBatch) > 0 {
  1246. result = append(result, currentBatch)
  1247. }
  1248. return result
  1249. }