TaskData.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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. "fmt"
  12. beego "github.com/beego/beego/v2/server/web"
  13. "github.com/signintech/gopdf"
  14. "github.com/vmihailenco/msgpack/v5"
  15. "github.com/xuri/excelize/v2"
  16. "math"
  17. "os"
  18. "strconv"
  19. "strings"
  20. "time"
  21. )
  22. type TaskDataController struct {
  23. beego.Controller
  24. }
  25. // 获取-
  26. func (c *TaskDataController) Extract_TaskData() {
  27. // 验证登录 User_is, User_r
  28. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  29. if !User_is {
  30. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  31. c.ServeJSON()
  32. return
  33. }
  34. Time_start := c.GetString("Time_start")
  35. Time_end := c.GetString("Time_end")
  36. T_task_id := c.GetString("T_task_id")
  37. Task_r, is := Task.Read_Task(T_task_id)
  38. if !is {
  39. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  40. c.ServeJSON()
  41. return
  42. }
  43. DeviceClass_List := Device.Read_DeviceClassList_List_id(Task_r.T_class)
  44. if len(DeviceClass_List) == 0 {
  45. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_Class_id 分类设备列表 为空!"}
  46. c.ServeJSON()
  47. return
  48. }
  49. logs.Println("----导入 :", DeviceClass_List)
  50. // 清空表
  51. Task.Truncate_TaskData(Task_r.T_task_id)
  52. // 插入 数据
  53. for _, v := range DeviceClass_List {
  54. logs.Println("---- :", v.T_sn, Task_r.T_task_id, Time_start, Time_end)
  55. Task.Import_TaskData(v.T_sn, v.T_id, Task_r.T_task_id, Time_start, Time_end)
  56. }
  57. // 提取数据后 将 当前任务 数据采集 标志 为 1
  58. Task_r.T_collection_state = 1
  59. if !Task.Update_Task(Task_r, "T_collection_state") {
  60. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  61. c.ServeJSON()
  62. return
  63. }
  64. System.Add_UserLogs_T(user_r.T_uuid, "任务", "修改", Task_r)
  65. System.Add_UserLogs(user_r.T_uuid, "提取数据", "提取数据"+Task_r.T_name, Task_r.T_task_id+"|"+Time_start+"|"+Time_end)
  66. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  67. c.ServeJSON()
  68. return
  69. }
  70. // 后台导出数据
  71. func (c *TaskDataController) Extract_TaskData_Back() {
  72. // 验证登录 User_is, User_r
  73. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  74. if !User_is {
  75. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  76. c.ServeJSON()
  77. return
  78. }
  79. Time_start := c.GetString("Time_start")
  80. Time_end := c.GetString("Time_end")
  81. T_task_id := c.GetString("T_task_id")
  82. Task_r, is := Task.Read_Task(T_task_id)
  83. if !is {
  84. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  85. c.ServeJSON()
  86. return
  87. }
  88. // 采集中
  89. if Task_r.T_collection_state == 2 {
  90. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  91. c.ServeJSON()
  92. return
  93. }
  94. DeviceClass_List := Device.Read_DeviceClassList_List_id(Task_r.T_class)
  95. if len(DeviceClass_List) == 0 {
  96. c.Data["json"] = lib.JSONS{Code: 202, Msg: "设备列表为空,请先添加设备!"}
  97. c.ServeJSON()
  98. return
  99. }
  100. // 更新状态为采集中
  101. Task_r.T_collection_state = 2
  102. if !Task.Update_Task(Task_r, "T_collection_state") {
  103. c.Data["json"] = lib.JSONS{Code: 202, Msg: "导出数据失败!"}
  104. c.ServeJSON()
  105. return
  106. }
  107. data := Nats.Extract_TaskData_Back{
  108. T_uuid: user_r.T_uuid,
  109. Time_start: Time_start,
  110. Time_end: Time_end,
  111. DeviceClassList: DeviceClass_List,
  112. Task: Task_r,
  113. }
  114. // 后台执行打包数据
  115. b, _ := msgpack.Marshal(&data)
  116. _ = lib.Nats.Publish("ColdVerify_Server_Extract_TaskData_Back", b)
  117. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  118. c.ServeJSON()
  119. return
  120. }
  121. // 列表 -
  122. func (c *TaskDataController) TaskData_List() {
  123. // 验证登录 User_is, User_r
  124. _, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  125. if !User_is {
  126. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  127. c.ServeJSON()
  128. return
  129. }
  130. var r_jsons lib.R_JSONS
  131. page, _ := c.GetInt("page")
  132. if page < 1 {
  133. page = 1
  134. }
  135. page_z, _ := c.GetInt("page_z")
  136. if page_z < 1 {
  137. page_z = conf.Page_size
  138. }
  139. Time_start := c.GetString("Time_start")
  140. Time_end := c.GetString("Time_end")
  141. T_sn := c.GetString("T_sn")
  142. T_id, err := c.GetInt("T_id")
  143. if err != nil {
  144. T_id = -1
  145. }
  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, err := c.GetInt("T_id")
  202. if err != nil {
  203. T_id = -1
  204. }
  205. T_task_id := c.GetString("T_task_id")
  206. Task_r, is := Task.Read_Task(T_task_id)
  207. if !is {
  208. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  209. c.ServeJSON()
  210. return
  211. }
  212. if Task_r.T_delivery_state == 2 {
  213. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  214. c.ServeJSON()
  215. return
  216. }
  217. if Task_r.T_uuid != User_r.T_uuid {
  218. c.Data["json"] = lib.JSONS{Code: 200, Msg: "无权访问!"}
  219. c.ServeJSON()
  220. return
  221. }
  222. // 查询设备列表
  223. dcList, _ := Device.Read_DeviceClassList_OrderList(Task_r.T_class, T_sn, "", page, 9999)
  224. // 保存布局编号和校准证书对应关系
  225. var deviceCertificateMap = make(map[string]string) // t_id, T_Certificate_sn
  226. for _, v := range dcList {
  227. deviceCertificateMap[v.T_id] = v.T_Certificate_sn
  228. }
  229. var cnt int64
  230. List, cnt := Task.Read_TaskData_ById_List(Task_r.T_task_id, T_sn, T_id, Time_start, Time_end, page, page_z)
  231. for i := 0; i < len(List); i++ {
  232. List[i].T_Certificate_sn = deviceCertificateMap[List[i].T_id]
  233. }
  234. page_size := math.Ceil(float64(cnt) / float64(page_z))
  235. r_jsons.List = List
  236. r_jsons.Page = page
  237. r_jsons.Page_size = int(page_size)
  238. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  239. r_jsons.Num = int(cnt)
  240. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  241. c.ServeJSON()
  242. return
  243. }
  244. // 列表 -
  245. func (c *TaskDataController) TaskDataClass_List() {
  246. // 验证登录 User_is, User_r
  247. _, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  248. if !User_is {
  249. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  250. c.ServeJSON()
  251. return
  252. }
  253. T_task_id := c.GetString("T_task_id")
  254. Task_r, is := Task.Read_Task(T_task_id)
  255. if !is {
  256. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  257. c.ServeJSON()
  258. return
  259. }
  260. if Task_r.T_delivery_state == 2 {
  261. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  262. c.ServeJSON()
  263. return
  264. }
  265. List := Task.Read_TaskData_ById_ClassList(Task_r.T_task_id)
  266. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: List}
  267. c.ServeJSON()
  268. return
  269. }
  270. // 添加-
  271. func (c *TaskDataController) TaskData_AddS() {
  272. // 验证登录 User_is, User_r
  273. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  274. if !User_is {
  275. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  276. c.ServeJSON()
  277. return
  278. }
  279. T_task_id := c.GetString("T_task_id")
  280. Task_r, is := Task.Read_Task(T_task_id)
  281. if !is {
  282. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  283. c.ServeJSON()
  284. return
  285. }
  286. //T_sn|T_id|T_t|T_rh|T_time?
  287. T_Data := c.GetString("T_Data")
  288. if len(T_Data) < 5 {
  289. c.Data["json"] = lib.JSONS{Code: 202, Msg: "err T_Data!"}
  290. c.ServeJSON()
  291. return
  292. }
  293. T_Data_list := strings.Split(T_Data, "?")
  294. println(len(T_Data_list), "len(T_Data_list)")
  295. var T_Data_list_x = 0
  296. for _, v := range T_Data_list {
  297. // 2022-08-09 14:25:00|27.7|55.2
  298. if len(v) < 5 {
  299. println(v, "len(v) < 5")
  300. continue
  301. }
  302. v_list := strings.Split(v, "|")
  303. is = Task.Add_TaskData(Task_r.T_task_id, v_list[0], v_list[1], v_list[2], v_list[3], v_list[4])
  304. if is {
  305. T_Data_list_x += 1
  306. }
  307. }
  308. 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)
  309. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: strconv.Itoa(T_Data_list_x)}
  310. c.ServeJSON()
  311. return
  312. }
  313. // 添加-
  314. func (c *TaskDataController) TaskData_Add() {
  315. // 验证登录 User_is, User_r
  316. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  317. if !User_is {
  318. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  319. c.ServeJSON()
  320. return
  321. }
  322. T_sn := c.GetString("T_sn")
  323. T_id, _ := c.GetInt("T_id")
  324. T_t, _ := c.GetFloat("T_t")
  325. T_rh, _ := c.GetFloat("T_rh")
  326. T_time := c.GetString("T_time")
  327. T_task_id := c.GetString("T_task_id")
  328. Task_r, is := Task.Read_Task(T_task_id)
  329. if !is {
  330. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  331. c.ServeJSON()
  332. return
  333. }
  334. 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)
  335. if !is {
  336. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  337. c.ServeJSON()
  338. return
  339. }
  340. 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)
  341. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  342. c.ServeJSON()
  343. return
  344. }
  345. // 修改-
  346. func (c *TaskDataController) TaskData_Up() {
  347. // 验证登录 User_is, User_r
  348. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  349. if !User_is {
  350. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  351. c.ServeJSON()
  352. return
  353. }
  354. Id, err := c.GetInt("Id")
  355. if err != nil {
  356. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  357. c.ServeJSON()
  358. return
  359. }
  360. T_t, err := c.GetFloat("T_t")
  361. if err != nil {
  362. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_t 错误!"}
  363. c.ServeJSON()
  364. return
  365. }
  366. T_rh, err := c.GetFloat("T_rh")
  367. if err != nil {
  368. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_rh 错误!"}
  369. c.ServeJSON()
  370. return
  371. }
  372. T_time := c.GetString("T_time")
  373. T_task_id := c.GetString("T_task_id")
  374. Task_r, is := Task.Read_Task(T_task_id)
  375. if !is {
  376. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  377. c.ServeJSON()
  378. return
  379. }
  380. is = Task.Up_TaskData(Task_r.T_task_id, strconv.Itoa(Id), fmt.Sprintf("%.2f", T_t), fmt.Sprintf("%.2f", T_rh), T_time)
  381. if !is {
  382. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  383. c.ServeJSON()
  384. return
  385. }
  386. 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)
  387. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  388. c.ServeJSON()
  389. return
  390. }
  391. // 删除-
  392. func (c *TaskDataController) TaskData_Del() {
  393. // 验证登录 User_is, User_r
  394. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  395. if !User_is {
  396. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  397. c.ServeJSON()
  398. return
  399. }
  400. Id, err := c.GetInt("Id")
  401. if err != nil {
  402. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  403. c.ServeJSON()
  404. return
  405. }
  406. T_task_id := c.GetString("T_task_id")
  407. Task_r, is := Task.Read_Task(T_task_id)
  408. if !is {
  409. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  410. c.ServeJSON()
  411. return
  412. }
  413. is = Task.Del_TaskData(Task_r.T_task_id, strconv.Itoa(Id))
  414. if !is {
  415. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  416. c.ServeJSON()
  417. return
  418. }
  419. System.Add_UserLogs(user_r.T_uuid, "任务数据", "删除"+Task_r.T_name, Task_r.T_task_id+"|"+strconv.Itoa(Id))
  420. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  421. c.ServeJSON()
  422. return
  423. }
  424. // 删除-
  425. func (c *TaskDataController) TaskData_Del_t_id() {
  426. // 验证登录 User_is, User_r
  427. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  428. if !User_is {
  429. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  430. c.ServeJSON()
  431. return
  432. }
  433. Id, err := c.GetInt("Id")
  434. if err != nil {
  435. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  436. c.ServeJSON()
  437. return
  438. }
  439. T_task_id := c.GetString("T_task_id")
  440. Task_r, is := Task.Read_Task(T_task_id)
  441. if !is {
  442. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  443. c.ServeJSON()
  444. return
  445. }
  446. is = Task.Del_TaskData_t_id(Task_r.T_task_id, strconv.Itoa(Id))
  447. if !is {
  448. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  449. c.ServeJSON()
  450. return
  451. }
  452. System.Add_UserLogs(user_r.T_uuid, "任务数据", "删除"+Task_r.T_name, Task_r.T_task_id+"|"+strconv.Itoa(Id))
  453. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  454. c.ServeJSON()
  455. return
  456. }
  457. // 列表 - 接口
  458. func (c *TaskDataController) Export_Data_Excel() {
  459. // 验证登录 User_is, User_r
  460. _, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  461. if !User_is {
  462. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  463. c.ServeJSON()
  464. return
  465. }
  466. Time_start := c.GetString("Time_start")
  467. Time_end := c.GetString("Time_end")
  468. T_sn_str := c.GetString("T_sn_list") //865901058809339,865901058815849,865901058818991,865901058810568
  469. //T_id, err := c.GetInt("T_id")
  470. //if err != nil {
  471. // T_id = -1
  472. //
  473. //}
  474. T_task_id := c.GetString("T_task_id")
  475. Task_r, is := Task.Read_Task(T_task_id)
  476. if !is {
  477. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  478. c.ServeJSON()
  479. return
  480. }
  481. T_sn_list := strings.Split(T_sn_str, ",")
  482. DeviceSensor_data_list := []Task.TaskData_{}
  483. for _, v := range T_sn_list {
  484. DeviceSensor_data, _ := Task.Read_TaskData_ById_List(Task_r.T_task_id, v, -1, Time_start, Time_end, 1, 9999)
  485. DeviceSensor_data_list = append(DeviceSensor_data_list, DeviceSensor_data...)
  486. }
  487. f := excelize.NewFile() // 设置单元格的值
  488. // 这里设置表头
  489. f.SetCellValue("Sheet1", "A1", "编号")
  490. f.SetCellValue("Sheet1", "B1", "SN")
  491. f.SetCellValue("Sheet1", "C1", "温度℃")
  492. f.SetCellValue("Sheet1", "D1", "湿度%")
  493. f.SetCellValue("Sheet1", "E1", "记录时间")
  494. // 设置列宽
  495. f.SetColWidth("Sheet1", "A", "A", 7)
  496. f.SetColWidth("Sheet1", "B", "B", 20)
  497. f.SetColWidth("Sheet1", "C", "C", 10)
  498. f.SetColWidth("Sheet1", "D", "D", 10)
  499. f.SetColWidth("Sheet1", "E", "E", 22)
  500. line := 1
  501. // 循环写入数据
  502. for _, v := range DeviceSensor_data_list {
  503. line++
  504. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), v.T_id)
  505. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_sn)
  506. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), strconv.FormatFloat(float64(v.T_t), 'f', 1, 64))
  507. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), strconv.FormatFloat(float64(v.T_rh), 'f', 1, 64))
  508. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_time)
  509. }
  510. fmt.Println("DeviceSensor_data:", len(DeviceSensor_data_list))
  511. lib.Create_Dir("./ofile")
  512. timeStr := time.Now().Format("20060102150405")
  513. // 保存文件
  514. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  515. logs.Error(lib.FuncName(), err)
  516. }
  517. if !lib.Pload_qiniu("ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx") {
  518. c.Data["json"] = lib.JSONS{Code: 203, Msg: "oss!"}
  519. c.ServeJSON()
  520. return
  521. }
  522. //删除目录
  523. //err = os.Remove("ofile/" + timeStr + ".xlsx")
  524. //if err != nil {
  525. // logs.Error(lib.FuncName(),err)
  526. //}
  527. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: "https://bzdcoldverifyoss.baozhida.cn/" + "ofile/" + timeStr + ".xlsx"}
  528. c.ServeJSON()
  529. return
  530. }
  531. // 列表 - 接口
  532. func (c *TaskDataController) Export_Data_PDF() {
  533. // 验证登录 User_is, User_r
  534. _, User_is := Account.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  535. if !User_is {
  536. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  537. c.ServeJSON()
  538. return
  539. }
  540. Time_start := c.GetString("Time_start")
  541. Time_end := c.GetString("Time_end")
  542. T_sn_str := c.GetString("T_sn_list") //865901058809339,865901058815849,865901058818991,865901058810568
  543. //T_id, err := c.GetInt("T_id")
  544. //if err != nil {
  545. // T_id = -1
  546. //
  547. //}
  548. T_task_id := c.GetString("T_task_id")
  549. Task_r, is := Task.Read_Task(T_task_id)
  550. if !is {
  551. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  552. c.ServeJSON()
  553. return
  554. }
  555. dcList, _ := Device.Read_DeviceClassList_OrderList(Task_r.T_class, "", "", 0, 9999)
  556. // 查询设备列表
  557. // 保存布局编号和校准证书对应关系
  558. var deviceCertificateMap = make(map[string]string) // t_id, T_Certificate_sn
  559. for _, v := range dcList {
  560. deviceCertificateMap[v.T_id] = v.T_Certificate_sn
  561. }
  562. T_sn_list := strings.Split(T_sn_str, ",")
  563. DeviceSensor_data_list := []Task.TaskData_{}
  564. for _, v := range T_sn_list {
  565. DeviceSensor_data, _ := Task.Read_TaskData_ById_List(Task_r.T_task_id, v, -1, Time_start, Time_end, 1, 9999)
  566. DeviceSensor_data_list = append(DeviceSensor_data_list, DeviceSensor_data...)
  567. for i := 0; i < len(DeviceSensor_data_list); i++ {
  568. DeviceSensor_data_list[i].T_Certificate_sn = deviceCertificateMap[DeviceSensor_data_list[i].T_id]
  569. }
  570. }
  571. //------
  572. var err error
  573. pdf := &gopdf.GoPdf{}
  574. pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
  575. //err = GetFont(pdf, "LiberationSerif-Regular.ttf")
  576. //if err != nil {
  577. // log.Fatalln(err)
  578. //}
  579. //err = pdf.SetFont("Ubuntu-L", "", 14)
  580. //if err != nil {
  581. // log.Fatalln(err)
  582. //}
  583. err = pdf.AddTTFFont("simsun", "static/fonts/三极行楷简体-粗.ttf")
  584. if err != nil {
  585. c.Data["json"] = lib.JSONS{Code: 204, Msg: "ok!", Data: err}
  586. c.ServeJSON()
  587. return
  588. }
  589. err = pdf.SetFont("simsun", "", 24)
  590. if err != nil {
  591. c.Data["json"] = lib.JSONS{Code: 205, Msg: "ok!", Data: err}
  592. c.ServeJSON()
  593. return
  594. }
  595. pdf.SetGrayFill(0.5)
  596. pdf.SetMargins(0, 20, 0, 20)
  597. pdf.AddPage()
  598. //use path
  599. //pdf.Image("logo.png", 100, 50, &gopdf.Rect{W: 50, H: 50})
  600. textw, _ := pdf.MeasureTextWidth(Task_r.T_name)
  601. pdf.SetX((595 / 2) - (textw / 2))
  602. pdf.SetY(40)
  603. pdf.Text(Task_r.T_name)
  604. // 线
  605. pdf.SetLineWidth(2)
  606. pdf.SetLineType("dashed")
  607. pdf.Line(10, 60, 585, 60)
  608. err = pdf.AddTTFFont("wts", "static/fonts/MiSans-Medium.ttf")
  609. if err != nil {
  610. c.Data["json"] = lib.JSONS{Code: 206, Msg: "ok!", Data: err}
  611. c.ServeJSON()
  612. return
  613. }
  614. err = pdf.SetFont("wts", "", 12)
  615. if err != nil {
  616. c.Data["json"] = lib.JSONS{Code: 207, Msg: "ok!", Data: err}
  617. c.ServeJSON()
  618. return
  619. }
  620. t_ := Time_start + " / " + Time_end
  621. if len(Time_start) == 0 {
  622. t_ = "所有数据"
  623. }
  624. //fmt.Sprintf(" %.1f ", v.T_t)
  625. lib.RectFillColor(pdf, "提取数据时间段["+t_+"]", 14, 22, 80, 550, 40, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  626. lib.RectFillColor(pdf, "布局编号", 12, 22, 120, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  627. lib.RectFillColor(pdf, "证书编号", 12, 92, 120, 150, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  628. lib.RectFillColor(pdf, "温度℃", 12, 242, 120, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  629. lib.RectFillColor(pdf, "湿度%", 12, 342, 120, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  630. //lib.RectFillColor(pdf, "温度范围", 12, 272, 120, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  631. //lib.RectFillColor(pdf, "湿度范围", 12, 362, 120, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  632. lib.RectFillColor(pdf, "记录时间", 12, 442, 120, 130, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  633. var y float64 = 140
  634. err = pdf.SetFont("wts", "", 10)
  635. if err != nil {
  636. c.Data["json"] = lib.JSONS{Code: 207, Msg: "ok!", Data: err}
  637. c.ServeJSON()
  638. return
  639. }
  640. for _, v := range DeviceSensor_data_list {
  641. //text := fmt.Sprintf(" %d ", i+1)
  642. var textH float64 = 25 // if text height is 25px.
  643. pdf.SetNewY(y, textH)
  644. y = pdf.GetY()
  645. //pdf.SetX(x) // must after pdf.SetNewY() called.
  646. //err = pdf.Text(text)
  647. //if err != nil {
  648. // log.Fatalln(err)
  649. //}
  650. T_t := fmt.Sprintf(" %.1f ", v.T_t)
  651. T_rh := fmt.Sprintf(" %.1f ", v.T_rh)
  652. T_time := fmt.Sprintf("%s", v.T_time)
  653. lib.RectFillColor(pdf, v.T_id, 10, 22, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  654. lib.RectFillColor(pdf, v.T_Certificate_sn, 10, 92, y, 150, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  655. lib.RectFillColor(pdf, T_t, 10, 242, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  656. lib.RectFillColor(pdf, T_rh, 10, 342, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  657. lib.RectFillColor(pdf, T_time, 10, 442, y, 130, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  658. y += 20
  659. }
  660. timeStr := "ofile/" + time.Now().Format("20060102150405") + ".pdf"
  661. err = pdf.WritePdf(timeStr)
  662. if err != nil {
  663. c.Data["json"] = lib.JSONS{Code: 207, Msg: "ok!", Data: err}
  664. c.ServeJSON()
  665. return
  666. }
  667. if !lib.Pload_qiniu(timeStr, timeStr) {
  668. c.Data["json"] = lib.JSONS{Code: 203, Msg: "oss!"}
  669. c.ServeJSON()
  670. return
  671. }
  672. //删除目录
  673. err = os.Remove(timeStr)
  674. if err != nil {
  675. logs.Error(lib.FuncName(), err)
  676. }
  677. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: "https://bzdcoldverifyoss.baozhida.cn/" + timeStr}
  678. c.ServeJSON()
  679. return
  680. }
  681. // 列表 - 接口
  682. func (c *TaskDataController) Check() {
  683. // 验证登录 User_is, User_r
  684. _, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  685. if !User_is {
  686. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  687. c.ServeJSON()
  688. return
  689. }
  690. T_task_id := c.GetString("T_task_id")
  691. Task_r, is := Task.Read_Task(T_task_id)
  692. if !is {
  693. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  694. c.ServeJSON()
  695. return
  696. }
  697. type R_JSONS struct {
  698. T_sn string // SN
  699. T_id string // 编号
  700. T_unm int // 数据量
  701. T_time_interval int64 // 时间间隔
  702. Time_start string // 开始
  703. Time_end string // 结束
  704. Result int // 200 OK, 201 可以补 , 202 不能补
  705. Result_str string // 提示内容
  706. Result_Time_start string // 2022-8-05 21:01
  707. Result_Time_defect int64 // 缺失时间间隔
  708. }
  709. var r_jsons []R_JSONS
  710. List := Task.Read_TaskData_ById_ClassList(Task_r.T_task_id)
  711. for _, v := range List {
  712. var r_json R_JSONS
  713. r_json.T_sn = v.T_sn
  714. r_json.Result = 200
  715. DeviceSensor_data := Task.Read_TaskData_ById_List_(Task_r.T_task_id, v.T_sn)
  716. r_json.T_unm = len(DeviceSensor_data)
  717. if r_json.T_unm > 2 {
  718. r_json.T_id = DeviceSensor_data[0].T_id
  719. r_json.Time_end = DeviceSensor_data[len(DeviceSensor_data)-1].T_time
  720. r_json.Time_start = DeviceSensor_data[0].T_time
  721. formatTime_a, _ := time.Parse("2006-1-2 15:04", DeviceSensor_data[0].T_time)
  722. formatTime_b, _ := time.Parse("2006-1-2 15:04", DeviceSensor_data[1].T_time)
  723. formatTime_x := formatTime_b.Unix() - formatTime_a.Unix()
  724. r_json.T_time_interval = formatTime_x
  725. println("formatTime_x:", v.T_id, v.T_sn, formatTime_x)
  726. if formatTime_x > 60*30 || formatTime_x < 60 {
  727. r_json.Result = 202
  728. r_json.Result_str = "数据异常(数据的第一个时间与第二个时间相差 " + strconv.FormatInt(formatTime_x, 10) + "秒),必须大于60秒,小于30分钟"
  729. r_jsons = append(r_jsons, r_json)
  730. continue
  731. }
  732. for data_i := 2; data_i < len(DeviceSensor_data); data_i++ {
  733. formatTime_a = formatTime_b
  734. formatTime_b, _ = time.Parse("2006-1-2 15:04", DeviceSensor_data[data_i].T_time)
  735. formatTime_ := formatTime_b.Unix() - formatTime_a.Unix()
  736. println("formatTime_x-:", formatTime_)
  737. if formatTime_ < formatTime_x {
  738. r_json.Result = 202
  739. r_json.Result_str = "开始时间:" + DeviceSensor_data[data_i-1].T_time + " 离下一条时间间隔:" + strconv.FormatInt(formatTime_, 10) + "秒,间隔小于时间间隔,不能自动补充"
  740. break
  741. }
  742. if formatTime_x != formatTime_ {
  743. if formatTime_ > 60*30 {
  744. r_json.Result = 202
  745. r_json.Result_str = "开始时间:" + DeviceSensor_data[data_i-1].T_time + " 离下一条时间间隔:" + strconv.FormatInt(formatTime_, 10) + "秒,相差时间大于30分钟,不能自动补充"
  746. break
  747. }
  748. r_json.Result = 201
  749. r_json.Result_str = "开始时间:" + DeviceSensor_data[data_i-1].T_time + " 离下一条时间间隔:" + strconv.FormatInt(formatTime_, 10) + "秒"
  750. r_json.Result_Time_start = DeviceSensor_data[data_i-1].T_time
  751. r_json.Result_Time_defect = formatTime_
  752. println(r_json.Result_str)
  753. break
  754. }
  755. }
  756. } else {
  757. r_json.Result = 202
  758. r_json.Result_str = "数据量太少 必须大于 2条以上!"
  759. }
  760. r_jsons = append(r_jsons, r_json)
  761. }
  762. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  763. c.ServeJSON()
  764. return
  765. }