Task.go 25 KB

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