TaskData.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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. var cnt int64
  159. List, cnt := Task.Read_TaskData_ById_List(Task_r.T_task_id, T_sn, T_id, Time_start, Time_end, page, page_z)
  160. page_size := math.Ceil(float64(cnt) / float64(page_z))
  161. r_jsons.List = List
  162. r_jsons.Page = page
  163. r_jsons.Page_size = int(page_size)
  164. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  165. r_jsons.Num = int(cnt)
  166. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  167. c.ServeJSON()
  168. return
  169. }
  170. // 列表 -
  171. func (c *TaskDataController) UserTaskData_List() {
  172. // 验证登录 User_is, User_r
  173. User_r, User_is := Account.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  174. if !User_is {
  175. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  176. c.ServeJSON()
  177. return
  178. }
  179. var r_jsons lib.R_JSONS
  180. page, _ := c.GetInt("page")
  181. if page < 1 {
  182. page = 1
  183. }
  184. page_z, _ := c.GetInt("page_z")
  185. if page_z < 1 {
  186. page_z = conf.Page_size
  187. }
  188. Time_start := c.GetString("Time_start")
  189. Time_end := c.GetString("Time_end")
  190. T_sn := c.GetString("T_sn")
  191. T_id, err := c.GetInt("T_id")
  192. if err != nil {
  193. T_id = -1
  194. }
  195. T_task_id := c.GetString("T_task_id")
  196. Task_r, is := Task.Read_Task(T_task_id)
  197. if !is {
  198. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  199. c.ServeJSON()
  200. return
  201. }
  202. if Task_r.T_delivery_state == 2 {
  203. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  204. c.ServeJSON()
  205. return
  206. }
  207. if Task_r.T_uuid != User_r.T_uuid {
  208. c.Data["json"] = lib.JSONS{Code: 200, Msg: "无权访问!"}
  209. c.ServeJSON()
  210. return
  211. }
  212. var cnt int64
  213. List, cnt := Task.Read_TaskData_ById_List(Task_r.T_task_id, T_sn, T_id, Time_start, Time_end, page, page_z)
  214. page_size := math.Ceil(float64(cnt) / float64(page_z))
  215. r_jsons.List = List
  216. r_jsons.Page = page
  217. r_jsons.Page_size = int(page_size)
  218. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  219. r_jsons.Num = int(cnt)
  220. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  221. c.ServeJSON()
  222. return
  223. }
  224. // 列表 -
  225. func (c *TaskDataController) TaskDataClass_List() {
  226. // 验证登录 User_is, User_r
  227. _, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  228. if !User_is {
  229. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  230. c.ServeJSON()
  231. return
  232. }
  233. T_task_id := c.GetString("T_task_id")
  234. Task_r, is := Task.Read_Task(T_task_id)
  235. if !is {
  236. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  237. c.ServeJSON()
  238. return
  239. }
  240. if Task_r.T_delivery_state == 2 {
  241. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  242. c.ServeJSON()
  243. return
  244. }
  245. List := Task.Read_TaskData_ById_ClassList(Task_r.T_task_id)
  246. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: List}
  247. c.ServeJSON()
  248. return
  249. }
  250. // 添加-
  251. func (c *TaskDataController) TaskData_AddS() {
  252. // 验证登录 User_is, User_r
  253. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  254. if !User_is {
  255. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  256. c.ServeJSON()
  257. return
  258. }
  259. T_task_id := c.GetString("T_task_id")
  260. Task_r, is := Task.Read_Task(T_task_id)
  261. if !is {
  262. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  263. c.ServeJSON()
  264. return
  265. }
  266. //T_sn|T_id|T_t|T_rh|T_time?
  267. T_Data := c.GetString("T_Data")
  268. if len(T_Data) < 5 {
  269. c.Data["json"] = lib.JSONS{Code: 202, Msg: "err T_Data!"}
  270. c.ServeJSON()
  271. return
  272. }
  273. T_Data_list := strings.Split(T_Data, "?")
  274. println(len(T_Data_list), "len(T_Data_list)")
  275. var T_Data_list_x = 0
  276. for _, v := range T_Data_list {
  277. // 2022-08-09 14:25:00|27.7|55.2
  278. if len(v) < 5 {
  279. println(v, "len(v) < 5")
  280. continue
  281. }
  282. v_list := strings.Split(v, "|")
  283. is = Task.Add_TaskData(Task_r.T_task_id, v_list[0], v_list[1], v_list[2], v_list[3], v_list[4])
  284. if is {
  285. T_Data_list_x += 1
  286. }
  287. }
  288. 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)
  289. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: strconv.Itoa(T_Data_list_x)}
  290. c.ServeJSON()
  291. return
  292. }
  293. // 添加-
  294. func (c *TaskDataController) TaskData_Add() {
  295. // 验证登录 User_is, User_r
  296. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  297. if !User_is {
  298. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  299. c.ServeJSON()
  300. return
  301. }
  302. T_sn := c.GetString("T_sn")
  303. T_id, _ := c.GetInt("T_id")
  304. T_t, _ := c.GetFloat("T_t")
  305. T_rh, _ := c.GetFloat("T_rh")
  306. T_time := c.GetString("T_time")
  307. T_task_id := c.GetString("T_task_id")
  308. Task_r, is := Task.Read_Task(T_task_id)
  309. if !is {
  310. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  311. c.ServeJSON()
  312. return
  313. }
  314. 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)
  315. if !is {
  316. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  317. c.ServeJSON()
  318. return
  319. }
  320. 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)
  321. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  322. c.ServeJSON()
  323. return
  324. }
  325. // 修改-
  326. func (c *TaskDataController) TaskData_Up() {
  327. // 验证登录 User_is, User_r
  328. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  329. if !User_is {
  330. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  331. c.ServeJSON()
  332. return
  333. }
  334. Id, err := c.GetInt("Id")
  335. if err != nil {
  336. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  337. c.ServeJSON()
  338. return
  339. }
  340. T_t, err := c.GetFloat("T_t")
  341. if err != nil {
  342. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_t 错误!"}
  343. c.ServeJSON()
  344. return
  345. }
  346. T_rh, err := c.GetFloat("T_rh")
  347. if err != nil {
  348. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_rh 错误!"}
  349. c.ServeJSON()
  350. return
  351. }
  352. T_time := c.GetString("T_time")
  353. T_task_id := c.GetString("T_task_id")
  354. Task_r, is := Task.Read_Task(T_task_id)
  355. if !is {
  356. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  357. c.ServeJSON()
  358. return
  359. }
  360. is = Task.Up_TaskData(Task_r.T_task_id, strconv.Itoa(Id), fmt.Sprintf("%.2f", T_t), fmt.Sprintf("%.2f", T_rh), T_time)
  361. if !is {
  362. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  363. c.ServeJSON()
  364. return
  365. }
  366. 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)
  367. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  368. c.ServeJSON()
  369. return
  370. }
  371. // 删除-
  372. func (c *TaskDataController) TaskData_Del() {
  373. // 验证登录 User_is, User_r
  374. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  375. if !User_is {
  376. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  377. c.ServeJSON()
  378. return
  379. }
  380. Id, err := c.GetInt("Id")
  381. if err != nil {
  382. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  383. c.ServeJSON()
  384. return
  385. }
  386. T_task_id := c.GetString("T_task_id")
  387. Task_r, is := Task.Read_Task(T_task_id)
  388. if !is {
  389. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  390. c.ServeJSON()
  391. return
  392. }
  393. is = Task.Del_TaskData(Task_r.T_task_id, strconv.Itoa(Id))
  394. if !is {
  395. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  396. c.ServeJSON()
  397. return
  398. }
  399. System.Add_UserLogs(user_r.T_uuid, "任务数据", "删除"+Task_r.T_name, Task_r.T_task_id+"|"+strconv.Itoa(Id))
  400. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  401. c.ServeJSON()
  402. return
  403. }
  404. // 删除-
  405. func (c *TaskDataController) TaskData_Del_t_id() {
  406. // 验证登录 User_is, User_r
  407. user_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  408. if !User_is {
  409. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  410. c.ServeJSON()
  411. return
  412. }
  413. Id, err := c.GetInt("Id")
  414. if err != nil {
  415. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  416. c.ServeJSON()
  417. return
  418. }
  419. T_task_id := c.GetString("T_task_id")
  420. Task_r, is := Task.Read_Task(T_task_id)
  421. if !is {
  422. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  423. c.ServeJSON()
  424. return
  425. }
  426. is = Task.Del_TaskData_t_id(Task_r.T_task_id, strconv.Itoa(Id))
  427. if !is {
  428. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  429. c.ServeJSON()
  430. return
  431. }
  432. System.Add_UserLogs(user_r.T_uuid, "任务数据", "删除"+Task_r.T_name, Task_r.T_task_id+"|"+strconv.Itoa(Id))
  433. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  434. c.ServeJSON()
  435. return
  436. }
  437. // 列表 - 接口
  438. func (c *TaskDataController) Export_Data_Excel() {
  439. // 验证登录 User_is, User_r
  440. _, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  441. if !User_is {
  442. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  443. c.ServeJSON()
  444. return
  445. }
  446. Time_start := c.GetString("Time_start")
  447. Time_end := c.GetString("Time_end")
  448. T_sn_str := c.GetString("T_sn_list") //865901058809339,865901058815849,865901058818991,865901058810568
  449. //T_id, err := c.GetInt("T_id")
  450. //if err != nil {
  451. // T_id = -1
  452. //
  453. //}
  454. T_task_id := c.GetString("T_task_id")
  455. Task_r, is := Task.Read_Task(T_task_id)
  456. if !is {
  457. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  458. c.ServeJSON()
  459. return
  460. }
  461. T_sn_list := strings.Split(T_sn_str, ",")
  462. DeviceSensor_data_list := []Task.TaskData_{}
  463. for _, v := range T_sn_list {
  464. DeviceSensor_data, _ := Task.Read_TaskData_ById_List(Task_r.T_task_id, v, -1, Time_start, Time_end, 1, 9999)
  465. DeviceSensor_data_list = append(DeviceSensor_data_list, DeviceSensor_data...)
  466. }
  467. f := excelize.NewFile() // 设置单元格的值
  468. // 这里设置表头
  469. f.SetCellValue("Sheet1", "A1", "编号")
  470. f.SetCellValue("Sheet1", "B1", "SN")
  471. f.SetCellValue("Sheet1", "C1", "温度℃")
  472. f.SetCellValue("Sheet1", "D1", "湿度%")
  473. f.SetCellValue("Sheet1", "E1", "记录时间")
  474. // 设置列宽
  475. f.SetColWidth("Sheet1", "A", "A", 7)
  476. f.SetColWidth("Sheet1", "B", "B", 20)
  477. f.SetColWidth("Sheet1", "C", "C", 10)
  478. f.SetColWidth("Sheet1", "D", "D", 10)
  479. f.SetColWidth("Sheet1", "E", "E", 22)
  480. line := 1
  481. // 循环写入数据
  482. for _, v := range DeviceSensor_data_list {
  483. line++
  484. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), v.T_id)
  485. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_sn)
  486. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), strconv.FormatFloat(float64(v.T_t), 'f', 1, 64))
  487. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), strconv.FormatFloat(float64(v.T_rh), 'f', 1, 64))
  488. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_time)
  489. }
  490. fmt.Println("DeviceSensor_data:", len(DeviceSensor_data_list))
  491. lib.Create_Dir("./ofile")
  492. timeStr := time.Now().Format("20060102150405")
  493. // 保存文件
  494. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  495. logs.Error(lib.FuncName(), err)
  496. }
  497. if !lib.Pload_qiniu("ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx") {
  498. c.Data["json"] = lib.JSONS{Code: 203, Msg: "oss!"}
  499. c.ServeJSON()
  500. return
  501. }
  502. //删除目录
  503. //err = os.Remove("ofile/" + timeStr + ".xlsx")
  504. //if err != nil {
  505. // logs.Error(lib.FuncName(),err)
  506. //}
  507. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: "https://bzdcoldverifyoss.baozhida.cn/" + "ofile/" + timeStr + ".xlsx"}
  508. c.ServeJSON()
  509. return
  510. }
  511. // 列表 - 接口
  512. func (c *TaskDataController) Export_Data_PDF() {
  513. // 验证登录 User_is, User_r
  514. _, User_is := Account.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  515. if !User_is {
  516. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  517. c.ServeJSON()
  518. return
  519. }
  520. Time_start := c.GetString("Time_start")
  521. Time_end := c.GetString("Time_end")
  522. T_sn_str := c.GetString("T_sn_list") //865901058809339,865901058815849,865901058818991,865901058810568
  523. //T_id, err := c.GetInt("T_id")
  524. //if err != nil {
  525. // T_id = -1
  526. //
  527. //}
  528. T_task_id := c.GetString("T_task_id")
  529. Task_r, is := Task.Read_Task(T_task_id)
  530. if !is {
  531. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  532. c.ServeJSON()
  533. return
  534. }
  535. T_sn_list := strings.Split(T_sn_str, ",")
  536. DeviceSensor_data_list := []Task.TaskData_{}
  537. for _, v := range T_sn_list {
  538. DeviceSensor_data, _ := Task.Read_TaskData_ById_List(Task_r.T_task_id, v, -1, Time_start, Time_end, 1, 9999)
  539. DeviceSensor_data_list = append(DeviceSensor_data_list, DeviceSensor_data...)
  540. }
  541. //------
  542. var err error
  543. pdf := &gopdf.GoPdf{}
  544. pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
  545. //err = GetFont(pdf, "LiberationSerif-Regular.ttf")
  546. //if err != nil {
  547. // log.Fatalln(err)
  548. //}
  549. //err = pdf.SetFont("Ubuntu-L", "", 14)
  550. //if err != nil {
  551. // log.Fatalln(err)
  552. //}
  553. err = pdf.AddTTFFont("simsun", "static/fonts/三极行楷简体-粗.ttf")
  554. if err != nil {
  555. c.Data["json"] = lib.JSONS{Code: 204, Msg: "ok!", Data: err}
  556. c.ServeJSON()
  557. return
  558. }
  559. err = pdf.SetFont("simsun", "", 24)
  560. if err != nil {
  561. c.Data["json"] = lib.JSONS{Code: 205, Msg: "ok!", Data: err}
  562. c.ServeJSON()
  563. return
  564. }
  565. pdf.SetGrayFill(0.5)
  566. pdf.SetMargins(0, 20, 0, 20)
  567. pdf.AddPage()
  568. //use path
  569. //pdf.Image("logo.png", 100, 50, &gopdf.Rect{W: 50, H: 50})
  570. textw, _ := pdf.MeasureTextWidth(Task_r.T_name)
  571. pdf.SetX((595 / 2) - (textw / 2))
  572. pdf.SetY(40)
  573. pdf.Text(Task_r.T_name)
  574. // 线
  575. pdf.SetLineWidth(2)
  576. pdf.SetLineType("dashed")
  577. pdf.Line(10, 60, 585, 60)
  578. err = pdf.AddTTFFont("wts", "static/fonts/MiSans-Medium.ttf")
  579. if err != nil {
  580. c.Data["json"] = lib.JSONS{Code: 206, Msg: "ok!", Data: err}
  581. c.ServeJSON()
  582. return
  583. }
  584. err = pdf.SetFont("wts", "", 12)
  585. if err != nil {
  586. c.Data["json"] = lib.JSONS{Code: 207, Msg: "ok!", Data: err}
  587. c.ServeJSON()
  588. return
  589. }
  590. t_ := Time_start + " / " + Time_end
  591. if len(Time_start) == 0 {
  592. t_ = "所有数据"
  593. }
  594. //fmt.Sprintf(" %.1f ", v.T_t)
  595. lib.RectFillColor(pdf, "提前数据时间段["+t_+"]", 14, 22, 80, 550, 40, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  596. lib.RectFillColor(pdf, "编号", 12, 22, 120, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  597. lib.RectFillColor(pdf, "SN", 12, 92, 120, 150, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  598. lib.RectFillColor(pdf, "温度℃", 12, 242, 120, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  599. lib.RectFillColor(pdf, "湿度%", 12, 342, 120, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  600. //lib.RectFillColor(pdf, "温度范围", 12, 272, 120, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  601. //lib.RectFillColor(pdf, "湿度范围", 12, 362, 120, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  602. lib.RectFillColor(pdf, "记录时间", 12, 442, 120, 130, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  603. var y float64 = 140
  604. err = pdf.SetFont("wts", "", 10)
  605. if err != nil {
  606. c.Data["json"] = lib.JSONS{Code: 207, Msg: "ok!", Data: err}
  607. c.ServeJSON()
  608. return
  609. }
  610. for _, v := range DeviceSensor_data_list {
  611. //text := fmt.Sprintf(" %d ", i+1)
  612. var textH float64 = 25 // if text height is 25px.
  613. pdf.SetNewY(y, textH)
  614. y = pdf.GetY()
  615. //pdf.SetX(x) // must after pdf.SetNewY() called.
  616. //err = pdf.Text(text)
  617. //if err != nil {
  618. // log.Fatalln(err)
  619. //}
  620. T_t := fmt.Sprintf(" %.1f ", v.T_t)
  621. T_rh := fmt.Sprintf(" %.1f ", v.T_rh)
  622. T_time := fmt.Sprintf("%s", v.T_time)
  623. lib.RectFillColor(pdf, strconv.Itoa(v.T_id), 10, 22, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  624. lib.RectFillColor(pdf, v.T_sn, 10, 92, y, 150, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  625. lib.RectFillColor(pdf, T_t, 10, 242, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  626. lib.RectFillColor(pdf, T_rh, 10, 342, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  627. lib.RectFillColor(pdf, T_time, 10, 442, y, 130, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  628. y += 20
  629. }
  630. timeStr := "ofile/" + time.Now().Format("20060102150405") + ".pdf"
  631. err = pdf.WritePdf(timeStr)
  632. if err != nil {
  633. c.Data["json"] = lib.JSONS{Code: 207, Msg: "ok!", Data: err}
  634. c.ServeJSON()
  635. return
  636. }
  637. if !lib.Pload_qiniu(timeStr, timeStr) {
  638. c.Data["json"] = lib.JSONS{Code: 203, Msg: "oss!"}
  639. c.ServeJSON()
  640. return
  641. }
  642. //删除目录
  643. err = os.Remove(timeStr)
  644. if err != nil {
  645. logs.Error(lib.FuncName(), err)
  646. }
  647. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: "https://bzdcoldverifyoss.baozhida.cn/" + timeStr}
  648. c.ServeJSON()
  649. return
  650. }
  651. // 列表 - 接口
  652. func (c *TaskDataController) Check() {
  653. // 验证登录 User_is, User_r
  654. _, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  655. if !User_is {
  656. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  657. c.ServeJSON()
  658. return
  659. }
  660. T_task_id := c.GetString("T_task_id")
  661. Task_r, is := Task.Read_Task(T_task_id)
  662. if !is {
  663. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  664. c.ServeJSON()
  665. return
  666. }
  667. type R_JSONS struct {
  668. T_sn string // SN
  669. T_id int // 编号
  670. T_unm int // 数据量
  671. T_time_interval int64 // 时间间隔
  672. Time_start string // 开始
  673. Time_end string // 结束
  674. Result int // 200 OK, 201 可以补 , 202 不能补
  675. Result_str string // 提示内容
  676. Result_Time_start string // 2022-8-05 21:01
  677. Result_Time_defect int64 // 缺失时间间隔
  678. }
  679. var r_jsons []R_JSONS
  680. List := Task.Read_TaskData_ById_ClassList(Task_r.T_task_id)
  681. for _, v := range List {
  682. var r_json R_JSONS
  683. r_json.T_sn = v.T_sn
  684. r_json.Result = 200
  685. DeviceSensor_data := Task.Read_TaskData_ById_List_(Task_r.T_task_id, v.T_sn)
  686. r_json.T_unm = len(DeviceSensor_data)
  687. if r_json.T_unm > 2 {
  688. r_json.T_id = DeviceSensor_data[0].T_id
  689. r_json.Time_end = DeviceSensor_data[len(DeviceSensor_data)-1].T_time
  690. r_json.Time_start = DeviceSensor_data[0].T_time
  691. formatTime_a, _ := time.Parse("2006-1-2 15:04", DeviceSensor_data[0].T_time)
  692. formatTime_b, _ := time.Parse("2006-1-2 15:04", DeviceSensor_data[1].T_time)
  693. formatTime_x := formatTime_b.Unix() - formatTime_a.Unix()
  694. r_json.T_time_interval = formatTime_x
  695. println("formatTime_x:", v.T_id, v.T_sn, formatTime_x)
  696. if formatTime_x > 60*30 || formatTime_x < 60 {
  697. r_json.Result = 202
  698. r_json.Result_str = "数据异常(数据的第一个时间与第二个时间相差 " + strconv.FormatInt(formatTime_x, 10) + "秒),必须大于60秒,小于30分钟"
  699. r_jsons = append(r_jsons, r_json)
  700. continue
  701. }
  702. for data_i := 2; data_i < len(DeviceSensor_data); data_i++ {
  703. formatTime_a = formatTime_b
  704. formatTime_b, _ = time.Parse("2006-1-2 15:04", DeviceSensor_data[data_i].T_time)
  705. formatTime_ := formatTime_b.Unix() - formatTime_a.Unix()
  706. println("formatTime_x-:", formatTime_)
  707. if formatTime_ < formatTime_x {
  708. r_json.Result = 202
  709. r_json.Result_str = "开始时间:" + DeviceSensor_data[data_i-1].T_time + " 离下一条时间间隔:" + strconv.FormatInt(formatTime_, 10) + "秒,间隔小于时间间隔,不能自动补充"
  710. break
  711. }
  712. if formatTime_x != formatTime_ {
  713. if formatTime_ > 60*30 {
  714. r_json.Result = 202
  715. r_json.Result_str = "开始时间:" + DeviceSensor_data[data_i-1].T_time + " 离下一条时间间隔:" + strconv.FormatInt(formatTime_, 10) + "秒,相差时间大于30分钟,不能自动补充"
  716. break
  717. }
  718. r_json.Result = 201
  719. r_json.Result_str = "开始时间:" + DeviceSensor_data[data_i-1].T_time + " 离下一条时间间隔:" + strconv.FormatInt(formatTime_, 10) + "秒"
  720. r_json.Result_Time_start = DeviceSensor_data[data_i-1].T_time
  721. r_json.Result_Time_defect = formatTime_
  722. println(r_json.Result_str)
  723. break
  724. }
  725. }
  726. } else {
  727. r_json.Result = 202
  728. r_json.Result_str = "数据量太少 必须大于 2条以上!"
  729. }
  730. r_jsons = append(r_jsons, r_json)
  731. }
  732. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  733. c.ServeJSON()
  734. return
  735. }