Task.go 22 KB

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