Task.go 23 KB

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