Task.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. package controllers
  2. import (
  3. "ColdVerify_server/Nats/NatsServer"
  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. "gonum.org/v1/plot"
  14. "gonum.org/v1/plot/plotter"
  15. "gonum.org/v1/plot/vg"
  16. "gonum.org/v1/plot/vg/draw"
  17. "image/color"
  18. "math"
  19. "os"
  20. "sync"
  21. "time"
  22. )
  23. type TaskController struct {
  24. beego.Controller
  25. }
  26. // 列表 -
  27. func (c *TaskController) List() {
  28. // 验证登录 User_is, User_r
  29. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  30. if !User_is {
  31. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  32. c.ServeJSON()
  33. return
  34. }
  35. var r_jsons lib.R_JSONS
  36. page, _ := c.GetInt("page")
  37. if page < 1 {
  38. page = 1
  39. }
  40. page_z, _ := c.GetInt("page_z")
  41. if page_z < 1 {
  42. page_z = conf.Page_size
  43. }
  44. T_name := c.GetString("T_name")
  45. T_uuid := c.GetString("T_uuid")
  46. UserMap := Account.UserListToMap(Account.Read_User_List_ALL_1())
  47. AdminMap := Account.AdminListToMap(Account.Read_Admin_List_ALL_1())
  48. var T_admin string
  49. if User_r.T_power > 2 {
  50. T_admin = User_r.T_uuid
  51. }
  52. var cnt int
  53. List, cnt := Task.Read_Task_List(T_uuid, T_admin, T_name, UserMap, AdminMap, page, page_z)
  54. page_size := math.Ceil(float64(cnt) / float64(page_z))
  55. r_jsons.List = List
  56. r_jsons.Page = page
  57. r_jsons.Page_size = int(page_size)
  58. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  59. r_jsons.Num = cnt
  60. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  61. c.ServeJSON()
  62. return
  63. }
  64. // 列表 -
  65. func (c *TaskController) UserTaskList() {
  66. // 验证登录 User_is, User_r
  67. User_r, User_is := Account.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  68. if !User_is {
  69. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  70. c.ServeJSON()
  71. return
  72. }
  73. var r_jsons lib.R_JSONS
  74. page, _ := c.GetInt("page")
  75. if page < 1 {
  76. page = 1
  77. }
  78. page_z, _ := c.GetInt("page_z")
  79. if page_z < 1 {
  80. page_z = conf.Page_size
  81. }
  82. T_name := c.GetString("T_name")
  83. UserMap := Account.UserListToMap(Account.Read_User_List_ALL_1())
  84. AdminMap := Account.AdminListToMap(Account.Read_Admin_List_ALL_1())
  85. var cnt int
  86. List, cnt := Task.Read_UserTask_List(User_r.T_uuid, T_name, UserMap, AdminMap, page, page_z)
  87. page_size := math.Ceil(float64(cnt) / float64(page_z))
  88. r_jsons.List = List
  89. r_jsons.Page = page
  90. r_jsons.Page_size = int(page_size)
  91. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  92. r_jsons.Num = cnt
  93. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  94. c.ServeJSON()
  95. return
  96. }
  97. // 获取-
  98. func (c *TaskController) Get() {
  99. // 验证登录 User_is, User_r
  100. _, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  101. if !User_is {
  102. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  103. c.ServeJSON()
  104. return
  105. }
  106. T_task_id := c.GetString("T_task_id")
  107. r, is := Task.Read_Task(T_task_id)
  108. if !is {
  109. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  110. c.ServeJSON()
  111. return
  112. }
  113. // 添加浏览量
  114. _ = Task.Add_Task_Visit(r)
  115. r.T_Visit += 1
  116. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Task.TaskToTask_R(r)}
  117. c.ServeJSON()
  118. return
  119. }
  120. // 添加-
  121. func (c *TaskController) Add() {
  122. // 验证登录 User_is, User_r
  123. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  124. if !User_is {
  125. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  126. c.ServeJSON()
  127. return
  128. }
  129. dc := Device.DeviceClass{
  130. T_uuid: User_r.T_uuid,
  131. T_State: 1,
  132. }
  133. T_class_id, is := Device.Add_DeviceClass(dc)
  134. if !is {
  135. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加分类失败!"}
  136. c.ServeJSON()
  137. return
  138. }
  139. System.Add_UserLogs_T(User_r.T_uuid, "分类管理", "添加", dc)
  140. T_name := c.GetString("T_name")
  141. T_uuid := c.GetString("T_uuid") // 用户uuid
  142. T_VerifyTemplate_class := c.GetString("T_VerifyTemplate_class")
  143. T_VerifyTemplate_id := c.GetString("T_VerifyTemplate_id")
  144. T_deadline := c.GetString("T_deadline")
  145. T_scheme := c.GetString("T_scheme")
  146. T_collection := c.GetString("T_collection")
  147. T_reporting := c.GetString("T_reporting")
  148. T_delivery := c.GetString("T_delivery")
  149. var_ := Task.Task{
  150. T_class: int(T_class_id),
  151. T_uuid: T_uuid,
  152. T_name: T_name,
  153. T_VerifyTemplate_class: T_VerifyTemplate_class,
  154. T_VerifyTemplate_id: T_VerifyTemplate_id,
  155. T_deadline: T_deadline,
  156. T_scheme: T_scheme,
  157. T_collection: T_collection,
  158. T_reporting: T_reporting,
  159. T_delivery: T_delivery,
  160. T_Show: 1,
  161. T_State: 1,
  162. }
  163. T_task_id, is := Task.Add_Task(var_)
  164. if !is {
  165. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  166. c.ServeJSON()
  167. return
  168. }
  169. NatsServer.Create_Local_Table(T_task_id)
  170. // 添加任务操作日志
  171. Task.Add_TaskLogs_T(User_r.T_uuid, T_task_id, "任务管理", "添加", var_)
  172. System.Add_UserLogs_T(User_r.T_uuid, "任务管理", "添加", var_)
  173. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_task_id}
  174. c.ServeJSON()
  175. return
  176. }
  177. // 添加-
  178. func (c *TaskController) AddData_Tool() {
  179. T_uuid := "3e84dda9-9eec-42b9-9350-0894262fc8a1" // 用户uuid
  180. T_name := c.GetString("T_name")
  181. T_task_id := c.GetString("T_task_id")
  182. r, _ := Task.Read_Task(T_task_id)
  183. if r.T_collection_state == 2 {
  184. c.Data["json"] = lib.JSONS{Code: 200, Msg: "数据采集中..."}
  185. c.ServeJSON()
  186. return
  187. }
  188. if r.Id > 0 {
  189. // 同步1.0数据
  190. NatsServer.Sync1_TaskData(T_task_id)
  191. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_task_id}
  192. c.ServeJSON()
  193. return
  194. }
  195. dc := Device.DeviceClass{
  196. T_uuid: T_uuid,
  197. T_State: 1,
  198. }
  199. T_class_id, is := Device.Add_DeviceClass(dc)
  200. if !is {
  201. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加分类失败!"}
  202. c.ServeJSON()
  203. return
  204. }
  205. var_ := Task.Task{
  206. T_task_id: T_task_id,
  207. T_class: int(T_class_id),
  208. T_uuid: T_uuid,
  209. T_name: T_name,
  210. T_Show: 1,
  211. T_State: 1,
  212. T_collection_state: 2,
  213. }
  214. _, is = Task.Add_Task_Tool(var_)
  215. if !is {
  216. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  217. c.ServeJSON()
  218. return
  219. }
  220. // 创建本地表
  221. NatsServer.Create_Local_Table(T_task_id)
  222. // 同步1.0数据
  223. NatsServer.Sync1_TaskData(T_task_id)
  224. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_task_id}
  225. c.ServeJSON()
  226. return
  227. }
  228. // 修改采集状态-
  229. func (c *TaskController) UpCollectionState() {
  230. // 验证登录 User_is, User_r
  231. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  232. if !User_is {
  233. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  234. c.ServeJSON()
  235. return
  236. }
  237. T_collection_state, _ := c.GetInt("T_collection_state")
  238. T_task_id := c.GetString("T_task_id")
  239. r, is := Task.Read_Task(T_task_id)
  240. if !is {
  241. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  242. c.ServeJSON()
  243. return
  244. }
  245. r.T_collection_state = T_collection_state
  246. if !Task.Update_Task(r, "T_collection_state") {
  247. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  248. c.ServeJSON()
  249. return
  250. }
  251. // 添加任务操作日志
  252. Task.Add_TaskLogs_T(User_r.T_uuid, T_task_id, "任务管理", "修改采集状态", r)
  253. System.Add_UserLogs_T(User_r.T_uuid, "任务管理", "修改采集状态", r)
  254. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  255. c.ServeJSON()
  256. return
  257. }
  258. func (c *TaskController) UpDeliveryState() {
  259. // 验证登录 User_is, User_r
  260. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  261. if !User_is {
  262. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  263. c.ServeJSON()
  264. return
  265. }
  266. T_delivery_state, _ := c.GetInt("T_delivery_state")
  267. T_task_id := c.GetString("T_task_id")
  268. r, is := Task.Read_Task(T_task_id)
  269. if !is {
  270. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  271. c.ServeJSON()
  272. return
  273. }
  274. r.T_delivery_state = T_delivery_state
  275. if !Task.Update_Task(r, "T_delivery_state") {
  276. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  277. c.ServeJSON()
  278. return
  279. }
  280. // 添加任务操作日志
  281. Task.Add_TaskLogs_T(User_r.T_uuid, T_task_id, "任务管理", "修改交付审核状态", r)
  282. System.Add_UserLogs_T(User_r.T_uuid, "任务管理", "修改交付审核状态", r)
  283. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  284. c.ServeJSON()
  285. return
  286. }
  287. // 修改-
  288. func (c *TaskController) Up() {
  289. // 验证登录 User_is, User_r
  290. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  291. if !User_is {
  292. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  293. c.ServeJSON()
  294. return
  295. }
  296. T_name := c.GetString("T_name")
  297. T_Show, T_Show_err := c.GetInt("T_Show")
  298. T_VerifyTemplate_class := c.GetString("T_VerifyTemplate_class")
  299. T_VerifyTemplate_id := c.GetString("T_VerifyTemplate_id")
  300. T_deadline := c.GetString("T_deadline")
  301. T_scheme := c.GetString("T_scheme")
  302. T_collection := c.GetString("T_collection")
  303. T_collection_state, _ := c.GetInt("T_collection_state")
  304. T_reporting := c.GetString("T_reporting")
  305. T_delivery := c.GetString("T_delivery")
  306. T_doc1 := c.GetString("T_doc1")
  307. T_pdf1 := c.GetString("T_pdf1")
  308. T_doc2 := c.GetString("T_doc2")
  309. T_pdf2 := c.GetString("T_pdf2")
  310. T_doc3 := c.GetString("T_doc3")
  311. T_pdf3 := c.GetString("T_pdf3")
  312. T_VerifyDeviceDataStartTime := c.GetString("T_VerifyDeviceDataStartTime") // 验证设备数据开始时间
  313. T_VerifyDeviceDataEndTime := c.GetString("T_VerifyDeviceDataEndTime") // 验证设备数据开始时间
  314. T_BindDeviceDataStartTime := c.GetString("T_BindDeviceDataStartTime") // 绑定设备数据开始时间
  315. T_BindDeviceDataEndTime := c.GetString("T_BindDeviceDataEndTime") // 绑定设备数据结束时间
  316. T_task_id := c.GetString("T_task_id")
  317. r, is := Task.Read_Task(T_task_id)
  318. if !is {
  319. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  320. c.ServeJSON()
  321. return
  322. }
  323. // .......
  324. clos := make([]string, 0)
  325. if len(T_name) > 0 {
  326. r.T_name = T_name
  327. clos = append(clos, "T_name")
  328. }
  329. if T_Show_err == nil {
  330. r.T_Show = T_Show
  331. clos = append(clos, "T_Show")
  332. }
  333. if len(T_VerifyTemplate_class) > 0 {
  334. r.T_VerifyTemplate_class = T_VerifyTemplate_class
  335. clos = append(clos, "T_VerifyTemplate_class")
  336. }
  337. if len(T_VerifyTemplate_id) > 0 {
  338. r.T_VerifyTemplate_id = T_VerifyTemplate_id
  339. clos = append(clos, "T_VerifyTemplate_id")
  340. }
  341. if len(T_deadline) > 0 {
  342. r.T_deadline = T_deadline
  343. clos = append(clos, "T_deadline")
  344. }
  345. if len(T_scheme) > 0 {
  346. r.T_scheme = T_scheme
  347. clos = append(clos, "T_scheme")
  348. }
  349. if len(T_collection) > 0 {
  350. r.T_collection = T_collection
  351. clos = append(clos, "T_collection")
  352. }
  353. if len(T_reporting) > 0 {
  354. r.T_reporting = T_reporting
  355. clos = append(clos, "T_reporting")
  356. }
  357. if len(T_delivery) > 0 {
  358. r.T_delivery = T_delivery
  359. clos = append(clos, "T_delivery")
  360. }
  361. if T_collection_state == 4 {
  362. r.T_collection_state = T_collection_state
  363. clos = append(clos, "T_collection_state")
  364. }
  365. if len(T_doc1) > 0 {
  366. r.T_doc1 = T_doc1
  367. clos = append(clos, "T_doc1")
  368. }
  369. // 验证报告内容T_pdf1 ,上传后将 当前任务 实施方案 标志 为 1
  370. if len(T_pdf1) > 0 {
  371. r.T_pdf1 = T_pdf1
  372. clos = append(clos, "T_pdf1")
  373. r.T_scheme_state = 1
  374. clos = append(clos, "T_scheme_state")
  375. }
  376. if len(T_doc2) > 0 {
  377. r.T_doc2 = T_doc2
  378. clos = append(clos, "T_doc2")
  379. }
  380. // 验证报告内容T_pdf2 ,上传后将 当前任务 报告编写 标志 为 1
  381. if len(T_pdf2) > 0 {
  382. r.T_pdf2 = T_pdf2
  383. clos = append(clos, "T_pdf2")
  384. r.T_reporting_state = 1
  385. clos = append(clos, "T_reporting_state")
  386. }
  387. if len(T_doc3) > 0 {
  388. r.T_doc3 = T_doc3
  389. clos = append(clos, "T_doc3")
  390. }
  391. if len(T_pdf3) > 0 {
  392. r.T_pdf3 = T_pdf3
  393. clos = append(clos, "T_pdf3")
  394. }
  395. if len(T_VerifyDeviceDataStartTime) > 0 {
  396. r.T_VerifyDeviceDataStartTime = T_VerifyDeviceDataStartTime
  397. clos = append(clos, "T_VerifyDeviceDataStartTime")
  398. }
  399. if len(T_VerifyDeviceDataEndTime) > 0 {
  400. r.T_VerifyDeviceDataEndTime = T_VerifyDeviceDataEndTime
  401. clos = append(clos, "T_VerifyDeviceDataEndTime")
  402. }
  403. if len(T_BindDeviceDataStartTime) > 0 {
  404. r.T_BindDeviceDataStartTime = T_BindDeviceDataStartTime
  405. clos = append(clos, "T_BindDeviceDataStartTime")
  406. }
  407. if len(T_BindDeviceDataEndTime) > 0 {
  408. r.T_BindDeviceDataEndTime = T_BindDeviceDataEndTime
  409. clos = append(clos, "T_BindDeviceDataEndTime")
  410. }
  411. // .......
  412. // "T_name", "T_Show", "T_VerifyTemplate_id", "T_deadline",
  413. // "T_collection", "T_reporting", "T_delivery",
  414. // "T_collection_state", "T_reporting_state", "T_delivery_state",
  415. // "T_doc1", "T_pdf1", "T_doc2", "T_pdf2", "T_doc3", "T_pdf3"
  416. if !Task.Update_Task(r, clos...) {
  417. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  418. c.ServeJSON()
  419. return
  420. }
  421. // 添加任务操作日志
  422. Task.Add_TaskLogs_T(User_r.T_uuid, T_task_id, "任务管理", "修改", r)
  423. System.Add_UserLogs_T(User_r.T_uuid, "任务管理", "修改", r)
  424. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  425. c.ServeJSON()
  426. return
  427. }
  428. // 删除-
  429. func (c *TaskController) Del() {
  430. // 验证登录 User_is, User_r
  431. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  432. if !User_is {
  433. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  434. c.ServeJSON()
  435. return
  436. }
  437. T_task_id := c.GetString("T_task_id")
  438. if r, is := Task.Read_Task(T_task_id); is {
  439. if !Task.Delete_Task(r) {
  440. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  441. c.ServeJSON()
  442. return
  443. }
  444. // 添加任务操作日志
  445. Task.Add_TaskLogs_T(User_r.T_uuid, T_task_id, "任务管理", "删除", r)
  446. System.Add_UserLogs_T(User_r.T_uuid, "任务管理", "删除", r)
  447. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  448. c.ServeJSON()
  449. return
  450. }
  451. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  452. c.ServeJSON()
  453. return
  454. }
  455. // 列表 -
  456. func (c *TaskController) Logs_List() {
  457. // 验证登录 User_is, User_r
  458. _, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  459. if !User_is {
  460. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  461. c.ServeJSON()
  462. return
  463. }
  464. var r_jsons lib.R_JSONS
  465. page, _ := c.GetInt("page")
  466. if page < 1 {
  467. page = 1
  468. }
  469. page_z, _ := c.GetInt("page_z")
  470. if page_z < 1 {
  471. page_z = conf.Page_size
  472. }
  473. T_task_id := c.GetString("T_task_id")
  474. AdminMap := Account.AdminListToMap(Account.Read_Admin_List_ALL_1())
  475. var cnt int
  476. List, cnt := Task.Read_TaskLogs_List(T_task_id, AdminMap, page, page_z)
  477. page_size := math.Ceil(float64(cnt) / float64(page_z))
  478. r_jsons.List = List
  479. r_jsons.Page = page
  480. r_jsons.Page_size = int(page_size)
  481. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  482. r_jsons.Num = cnt
  483. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  484. c.ServeJSON()
  485. return
  486. }
  487. // 查询图片生成状态
  488. func (c *TaskController) DeviceData_JPGState() {
  489. T_task_id := c.GetString("T_task_id")
  490. T_remark := c.GetString("T_remark")
  491. jpg, is := Device.Redis_DeviceDataJPG_Get(T_task_id + T_remark)
  492. if !is {
  493. c.Data["json"] = lib.JSONS{Code: 202, Msg: "暂无图片正在生成"}
  494. c.ServeJSON()
  495. return
  496. }
  497. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: jpg}
  498. c.ServeJSON()
  499. return
  500. }
  501. func (c *TaskController) DeviceData_JPG() {
  502. StartTime := c.GetString("StartTime")
  503. if len(StartTime) > 0 {
  504. _, ok := lib.TimeStrToTime(StartTime)
  505. if !ok {
  506. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  507. c.ServeJSON()
  508. return
  509. }
  510. }
  511. EndTime := c.GetString("EndTime")
  512. if len(EndTime) > 0 {
  513. _, ok := lib.TimeStrToTime(EndTime)
  514. if !ok {
  515. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  516. c.ServeJSON()
  517. return
  518. }
  519. }
  520. T_remark := c.GetString("T_remark")
  521. TemperatureMin, _ := c.GetFloat("TemperatureMin") // 最低温度
  522. TemperatureMax, _ := c.GetFloat("TemperatureMax") // 最高温度
  523. if TemperatureMin == 0 {
  524. TemperatureMin = 2
  525. }
  526. if TemperatureMax == 0 {
  527. TemperatureMax = 8
  528. }
  529. T_task_id := c.GetString("T_task_id")
  530. Task_r, is := Task.Read_Task(T_task_id)
  531. if !is {
  532. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  533. c.ServeJSON()
  534. return
  535. }
  536. if Task_r.T_collection_state == 2 {
  537. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  538. c.ServeJSON()
  539. return
  540. }
  541. deviceClassList, _ := Device.Read_DeviceClassList_OrderList(Task_r.T_class, "", "", T_remark, 0, 9999)
  542. if !is {
  543. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class 错误!"}
  544. c.ServeJSON()
  545. return
  546. }
  547. Device.Redis_DeviceDataJPG_Del(T_task_id + T_remark)
  548. // 生成图片
  549. go DeviceDataJPG(StartTime, EndTime, T_task_id, T_remark, deviceClassList, TemperatureMin, TemperatureMax)
  550. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  551. c.ServeJSON()
  552. return
  553. }
  554. // 存档生成图片
  555. func DeviceDataJPG(StartTime, EndTime, T_task_id, T_remark string, deviceList []Device.DeviceClassList, TemperatureMin, TemperatureMax float64) {
  556. Device.Redis_DeviceDataJPG_Set(T_task_id+T_remark, Device.DeviceDataJPG{
  557. State: 1,
  558. Msg: "图片生成中",
  559. Url: "",
  560. })
  561. msg := ""
  562. state := 2
  563. url := ""
  564. if TemperatureMin == 0 {
  565. TemperatureMin = 2
  566. }
  567. if TemperatureMax == 0 {
  568. TemperatureMax = 8
  569. }
  570. var ymin, ymax float64
  571. var xminT, xmaxT time.Time
  572. if len(deviceList) > 0 {
  573. ymin, ymax, xminT, xmaxT = Device.Read_DeviceData_T_Min_Max_Time_Min_Max(deviceList[0].T_sn, StartTime, EndTime)
  574. }
  575. // 创建一个新的绘图
  576. p := plot.New()
  577. // 设置绘图标题和标签
  578. p.Title.Text = "温度折线图"
  579. //p.Legend.ThumbnailWidth = 5 * vg.Inch
  580. p.X.Label.Text = "时间"
  581. p.Y.Label.Text = "温度"
  582. var chData = make(chan int, 10)
  583. var jobGroup sync.WaitGroup
  584. var device = make([]Device.DeviceCount, len(deviceList))
  585. // 创建温度线
  586. for i := 0; i < len(deviceList); i++ {
  587. chData <- 1
  588. jobGroup.Add(1)
  589. go func(index int) {
  590. //go func(index int, wg *sync.WaitGroup, p *plot.Plot) {
  591. defer func() {
  592. <-chData // 完成时chan取出1个
  593. jobGroup.Done() // 完成时将等待组值减1
  594. }()
  595. sn, id := deviceList[index].T_sn, deviceList[index].T_id
  596. ymin_, ymax_, minTime_, maxTime_ := Device.Read_DeviceData_T_Min_Max_Time_Min_Max(sn, StartTime, EndTime)
  597. if ymin > ymin_ {
  598. ymin = ymin_
  599. }
  600. if ymax < ymax_ {
  601. ymax = ymax_
  602. }
  603. if xminT.After(minTime_) && !minTime_.IsZero() {
  604. xminT = minTime_
  605. }
  606. if xmaxT.Before(maxTime_) && !maxTime_.IsZero() {
  607. xmaxT = maxTime_
  608. }
  609. r_maps, r_maps_num := Device.Read_DeviceSensorData_ById_List(sn, StartTime, EndTime, 0, 9999)
  610. device[index] = Device.DeviceCount{
  611. T_id: id,
  612. Num: r_maps_num,
  613. }
  614. if r_maps_num == 0 {
  615. return
  616. }
  617. pts := make(plotter.XYs, len(r_maps))
  618. for j, d := range r_maps {
  619. t, _ := lib.TimeStrToTime(d.T_time)
  620. pts[j].X = float64(t.Unix())
  621. pts[j].Y = float64(d.T_t)
  622. }
  623. line, err := plotter.NewLine(pts)
  624. if err != nil {
  625. return
  626. }
  627. line.Color = randomColor(index)
  628. p.Add(line)
  629. }(i)
  630. }
  631. jobGroup.Wait()
  632. xmin, xmax := float64(xminT.Unix()), float64(xmaxT.Unix())
  633. // 添加最高,最低标准线 用红色虚线标识
  634. p.Add(horizontalLine(xmin, xmax, TemperatureMin))
  635. p.Add(horizontalLine(xmin, xmax, TemperatureMax))
  636. if ymax < 8 {
  637. ymax = 8
  638. }
  639. if ymin > 0 {
  640. ymin = 0
  641. }
  642. p.Y.Min, p.Y.Max = ymin, ymax
  643. p.X.Min, p.X.Max = xmin, xmax
  644. p.Y.Tick.Marker = commaTicks{}
  645. //p.X.Tick.Marker = plot.TimeTicks{Format: "2006-01-02 15:04:05"}
  646. p.X.Tick.Marker = timeTicks{}
  647. p.X.Tick.Label.Rotation = math.Pi / 5
  648. p.X.Tick.Label.YAlign = draw.YCenter
  649. p.X.Tick.Label.XAlign = draw.XRight
  650. filename := "jpg" + time.Now().Format("20060102150405")
  651. // 保存文件
  652. if err := p.Save(10*vg.Inch, 4*vg.Inch, "ofile/"+filename+".jpg"); err != nil {
  653. Device.Redis_DeviceDataJPG_Set(T_task_id+T_remark, Device.DeviceDataJPG{
  654. State: 3,
  655. Msg: "图片生成失败",
  656. Url: url,
  657. })
  658. logs.Error(lib.FuncName(), "生成图片失败", err)
  659. return
  660. }
  661. if !lib.Pload_qiniu("ofile/"+filename+".jpg", "ofile/"+filename+".jpg") {
  662. Device.Redis_DeviceDataJPG_Set(T_task_id+T_remark, Device.DeviceDataJPG{
  663. State: 3,
  664. Msg: "图片上传七牛云失败",
  665. Url: url,
  666. })
  667. logs.Error(lib.FuncName(), "上传七牛云失败")
  668. return
  669. }
  670. //删除目录
  671. os.Remove("ofile/" + filename + ".jpg")
  672. msg = "图片生成成功"
  673. url = "https://bzdcoldverifyoss.baozhida.cn/" + "ofile/" + filename + ".jpg"
  674. Device.Redis_DeviceDataJPG_Set(T_task_id+T_remark, Device.DeviceDataJPG{
  675. State: state,
  676. Msg: msg,
  677. Url: url,
  678. Device: device,
  679. })
  680. return
  681. }
  682. func horizontalLine(xmin, xmax, y float64) *plotter.Line {
  683. pts := make(plotter.XYs, 2)
  684. pts[0].X = xmin
  685. pts[0].Y = y
  686. pts[1].X = xmax
  687. pts[1].Y = y
  688. line, err := plotter.NewLine(pts)
  689. if err != nil {
  690. panic(err)
  691. }
  692. line.LineStyle.Dashes = []vg.Length{vg.Points(8), vg.Points(5), vg.Points(1), vg.Points(5)}
  693. line.Color = color.RGBA{R: 255, A: 255}
  694. return line
  695. }
  696. type timeTicks struct{}
  697. func (timeTicks) Ticks(min, max float64) []plot.Tick {
  698. tks := plot.TimeTicks{}.Ticks(min, max)
  699. for i, t := range tks {
  700. //if t.Label == "" { // Skip minor ticks, they are fine.
  701. // continue
  702. //}
  703. tks[i].Label = time.Unix(int64(t.Value), 0).Format("2006-01-02 15:04:05")
  704. }
  705. return tks
  706. }
  707. type commaTicks struct{}
  708. // Ticks computes the default tick marks, but inserts commas
  709. // into the labels for the major tick marks.
  710. func (commaTicks) Ticks(min, max float64) []plot.Tick {
  711. tks := plot.DefaultTicks{}.Ticks(min, max)
  712. for i, t := range tks {
  713. //if t.Label == "" { // Skip minor ticks, they are fine.
  714. // continue
  715. //}
  716. tks[i].Label = fmt.Sprintf("%.0f", t.Value)
  717. }
  718. return tks
  719. }
  720. // 生成随机颜色的辅助函数
  721. func randomColor(i int) color.RGBA {
  722. var colors []color.RGBA
  723. colors = append(colors,
  724. color.RGBA{R: 52, G: 152, B: 219, A: 255},
  725. color.RGBA{R: 230, G: 126, B: 34, A: 255},
  726. color.RGBA{R: 142, G: 68, B: 173, A: 255},
  727. color.RGBA{R: 211, G: 84, B: 0, A: 255},
  728. color.RGBA{R: 231, G: 76, B: 60, A: 255},
  729. color.RGBA{R: 26, G: 188, B: 156, A: 255},
  730. color.RGBA{R: 243, G: 156, B: 18, A: 255},
  731. color.RGBA{R: 22, G: 160, B: 133, A: 255},
  732. color.RGBA{R: 46, G: 204, B: 113, A: 255},
  733. color.RGBA{R: 39, G: 174, B: 96, A: 255},
  734. color.RGBA{R: 41, G: 128, B: 185, A: 255},
  735. color.RGBA{R: 155, G: 89, B: 182, A: 255},
  736. color.RGBA{R: 192, G: 57, B: 43, A: 255},
  737. color.RGBA{R: 241, G: 196, B: 15, A: 255},
  738. )
  739. return colors[i%len(colors)]
  740. }