taskController.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * @Author: lilig
  3. * @Date: 2023-10-10 15:22:58
  4. * @LastEditors: lilifor
  5. * @LastEditTime: 2023-10-10 15:22:58
  6. * @FilePath: \SCD\controllers\TaskController.go
  7. * @Description:
  8. *
  9. * Copyright (c) 2023 by lilig/jujutong, All Rights Reserved.
  10. */
  11. package controllers
  12. import (
  13. "encoding/json"
  14. "fmt"
  15. "scd_check_tools/models/bo"
  16. "scd_check_tools/tools"
  17. "strings"
  18. "time"
  19. )
  20. //检测任务服务
  21. type TaskController struct {
  22. BaseController
  23. }
  24. func init() {
  25. }
  26. // @Summary 创建新的检测任务
  27. // @Description 创建新的检测任务
  28. // @Tags 检测任务服务接口
  29. // @Accept x-www-form-urlencoded
  30. // @Produce json
  31. // @Param id formData int false "任务ID。指定id值大于0时为编辑操作;否则为新增操作"
  32. // @Param name formData string true "任务名称"
  33. // @Param station_id formData int true "变电站ID"
  34. // @Param memo formData string false "任务说明"
  35. // @Param report_id formData int true "报告模板ID"
  36. // @Param scd_id formData int true "SCD文件ID"
  37. // @Param modelids formData string true "模型ID。多个模型ID之间逗号分隔"
  38. // @Success 200 {object} ResultOK 成功
  39. // @Failure 500 {object} ResultError 失败
  40. // @router /task/save [post]
  41. func (c *TaskController) SaveTaskInfo() {
  42. id, _ := c.GetInt("id")
  43. obj := new(bo.TaskMgr)
  44. obj.SetUserInfo(c.GetCurrentUserInfo())
  45. obj.Model = bo.T_data_task{Id: id}
  46. obj.Model.Name = c.GetString("name")
  47. obj.Model.StationId, _ = c.GetInt("station_id")
  48. obj.Model.Memo = c.GetString("memo")
  49. obj.Model.ReportId, _ = c.GetInt("report_id")
  50. obj.Model.ScdId, _ = c.GetInt64("scd_id")
  51. obj.Model.State = 0
  52. if obj.Model.Id == 0 {
  53. obj.Model.Code = fmt.Sprintf("T-%d-%s", obj.Model.StationId, time.Now().Format("20060102150405"))
  54. } else {
  55. obj.Model.Code = c.GetString("code")
  56. }
  57. modelids := c.GetString("modelids")
  58. if obj.Model.Name == "" {
  59. c.Data["json"] = c.ResultError("检测任务名称不能为空")
  60. c.ServeJSON()
  61. return
  62. }
  63. if obj.Model.ReportId == 0 {
  64. c.Data["json"] = c.ResultError("检测报告模型不能为空")
  65. c.ServeJSON()
  66. return
  67. }
  68. if obj.Model.StationId == 0 {
  69. c.Data["json"] = c.ResultError("检测变电站不能为空")
  70. c.ServeJSON()
  71. return
  72. }
  73. if obj.Model.ScdId == 0 {
  74. c.Data["json"] = c.ResultError("检测的SCD文件不能为空")
  75. c.ServeJSON()
  76. return
  77. }
  78. if modelids == "" || modelids == "[]" {
  79. c.Data["json"] = c.ResultError("检测任务的模型不能为空")
  80. c.ServeJSON()
  81. return
  82. }
  83. area_models := []string{}
  84. err1 := json.Unmarshal([]byte(`["`+strings.ReplaceAll(modelids, ",", "\",\"")+`"]`), &area_models)
  85. if err1 != nil {
  86. c.Data["json"] = c.ResultError(err1.Error())
  87. c.ServeJSON()
  88. return
  89. }
  90. taskid, err := obj.Save()
  91. if err != nil {
  92. c.Data["json"] = c.ResultError(err.Error())
  93. c.ServeJSON()
  94. return
  95. }
  96. modelObj := new(bo.TaskModelMgr)
  97. modelObj.SetUserInfo(c.GetCurrentUserInfo())
  98. modelObj.Model.TaskId = taskid
  99. err = modelObj.Save(area_models)
  100. if err != nil {
  101. obj.Delete()
  102. c.Data["json"] = c.ResultError(err.Error())
  103. c.ServeJSON()
  104. return
  105. }
  106. c.Data["json"] = c.ResultOK("", 0)
  107. c.ServeJSON()
  108. }
  109. // @Summary 删除指定的检测任务
  110. // @Description 删除指定的检测任务
  111. // @Tags 检测任务服务接口
  112. // @Accept x-www-form-urlencoded
  113. // @Produce json
  114. // @Param id formData int true "检测任务ID"
  115. // @Success 200 {object} ResultOK 成功
  116. // @Failure 500 {object} ResultError 失败
  117. // @router /task/delete [post]
  118. func (c *TaskController) DeleteTask() {
  119. id, _ := c.GetInt("id")
  120. if id == 0 {
  121. c.Data["json"] = c.ResultError("任务编号不能为空!")
  122. c.ServeJSON()
  123. return
  124. }
  125. obj := new(bo.TaskMgr)
  126. obj.SetUserInfo(c.GetCurrentUserInfo())
  127. obj.Model = bo.T_data_task{}
  128. obj.Model.Id = id
  129. err := obj.Delete()
  130. if err != nil {
  131. c.Data["json"] = c.ResultError(err.Error())
  132. c.ServeJSON()
  133. return
  134. }
  135. c.Data["json"] = c.ResultOK("", 0)
  136. c.ServeJSON()
  137. }
  138. // @Summary 开始进行检测
  139. // @Description 开始进行检测
  140. // @Tags 检测任务服务接口
  141. // @Accept x-www-form-urlencoded
  142. // @Produce json
  143. // @Param id formData int true "检测任务ID"
  144. // @Success 200 {object} ResultOK 成功
  145. // @Failure 500 {object} ResultError 失败
  146. // @router /task/start [post]
  147. func (c *TaskController) StartTask() {
  148. id, _ := c.GetInt("id")
  149. if id == 0 {
  150. c.Data["json"] = c.ResultError("任务编号不能为空!")
  151. c.ServeJSON()
  152. return
  153. }
  154. obj := new(bo.TaskMgr)
  155. obj.SetUserInfo(c.GetCurrentUserInfo())
  156. obj.Model = bo.T_data_task{}
  157. obj.Model.Id = id
  158. err := obj.SetActive(1)
  159. if err != nil {
  160. c.Data["json"] = c.ResultError(err.Error())
  161. c.ServeJSON()
  162. return
  163. }
  164. c.Data["json"] = c.ResultOK("", 0)
  165. c.ServeJSON()
  166. }
  167. // @Summary 取消或者终止检测
  168. // @Description 取消或者终止检测
  169. // @Tags 检测任务服务接口
  170. // @Accept x-www-form-urlencoded
  171. // @Produce json
  172. // @Param id formData int true "检测任务ID"
  173. // @Success 200 {object} ResultOK 成功
  174. // @Failure 500 {object} ResultError 失败
  175. // @router /task/stop [post]
  176. func (c *TaskController) SetTaskActiveStop() {
  177. id, _ := c.GetInt("id")
  178. if id == 0 {
  179. c.Data["json"] = c.ResultError("任务编号不能为空!")
  180. c.ServeJSON()
  181. return
  182. }
  183. obj := new(bo.TaskMgr)
  184. obj.SetUserInfo(c.GetCurrentUserInfo())
  185. obj.Model = bo.T_data_task{}
  186. obj.Model.Id = id
  187. err := obj.SetActive(3)
  188. if err != nil {
  189. c.Data["json"] = c.ResultError(err.Error())
  190. c.ServeJSON()
  191. return
  192. }
  193. c.Data["json"] = c.ResultOK("", 0)
  194. c.ServeJSON()
  195. }
  196. // @Summary 任务重新检测
  197. // @Description 任务重新检测
  198. // @Tags 检测任务服务接口
  199. // @Accept x-www-form-urlencoded
  200. // @Produce json
  201. // @Param id formData int true "检测任务ID"
  202. // @Success 200 {object} ResultOK 成功
  203. // @Failure 500 {object} ResultError 失败
  204. // @router /task/reset [post]
  205. func (c *TaskController) ReSetTaskActive() {
  206. id, _ := c.GetInt("id")
  207. if id == 0 {
  208. c.Data["json"] = c.ResultError("任务编号不能为空!")
  209. c.ServeJSON()
  210. return
  211. }
  212. obj := new(bo.TaskMgr)
  213. obj.SetUserInfo(c.GetCurrentUserInfo())
  214. obj.Model = bo.T_data_task{}
  215. obj.Model.Id = id
  216. err := obj.SetActive(0)
  217. if err != nil {
  218. c.Data["json"] = c.ResultError(err.Error())
  219. c.ServeJSON()
  220. return
  221. }
  222. c.Data["json"] = c.ResultOK("", 0)
  223. c.ServeJSON()
  224. }
  225. // @Summary 查询检测任务
  226. // @Description 查询检测任务。支持任务名称、检测状态等过滤条件
  227. // @Tags 检测任务服务接口
  228. // @Accept x-www-form-urlencoded
  229. // @Produce json
  230. // @Param pageno query int true "当前页码。默认为1"
  231. // @Param pagesize query int true "每页显示数据数。默认为20"
  232. // @Param id query int false "任务ID"
  233. // @Param station_id query int false "变电站ID"
  234. // @Param name query string false "名称"
  235. // @Param start_time query string false "检测开始日期。格式:yyyy-mm-dd"
  236. // @Param end_time query string false "检测结束日期。格式:yyyy-mm-dd"
  237. // @Param state query int false "检测状态。空:全部 0:未检查 1:检测中 2:检测结束 3:异常中断"
  238. // @Success 200 {object} ResultOK 成功
  239. // @Failure 500 {object} ResultError 失败
  240. // @router /task/list [get]
  241. func (c *TaskController) GetTaskList() {
  242. obj := new(bo.TaskMgr)
  243. obj.SetUserInfo(c.GetCurrentUserInfo())
  244. obj.Model = bo.T_data_task{}
  245. obj.Model.Id, _ = c.GetInt("id")
  246. obj.Model.StationId, _ = c.GetInt("station_id")
  247. obj.Model.Name = c.GetString("name")
  248. obj.Model.State, _ = c.GetInt("state", -1)
  249. obj.Model.StartTime = c.GetString("start_time")
  250. obj.Model.EndTime = c.GetString("end_time")
  251. pi, _ := c.GetInt("pagesize", 20)
  252. po, _ := c.GetInt("pageno", 0)
  253. if po == 0 {
  254. po, _ = c.GetInt("pageindex", 0)
  255. }
  256. if po == 0 {
  257. po = 1
  258. }
  259. lst, cnt, err := obj.List(po, pi)
  260. if err != nil {
  261. c.Data["json"] = c.ResultError(err.Error())
  262. c.ServeJSON()
  263. return
  264. }
  265. c.Data["json"] = c.ResultOK(lst, cnt)
  266. c.ServeJSON()
  267. }
  268. // @Summary 获取指定检测任务的详细信息
  269. // @Description 获取指定检测任务的详细信息
  270. // @Tags 检测任务服务接口
  271. // @Accept x-www-form-urlencoded
  272. // @Produce json
  273. // @Param id query int true "任务ID"
  274. // @Success 200 {object} ResultOK 成功
  275. // @Failure 500 {object} ResultError 失败
  276. // @router /task/info [get]
  277. func (c *TaskController) GetTaskInfo() {
  278. obj := new(bo.TaskMgr)
  279. obj.SetUserInfo(c.GetCurrentUserInfo())
  280. obj.Model = bo.T_data_task{}
  281. obj.Model.Id, _ = c.GetInt("id")
  282. lst, err := obj.One()
  283. if err != nil {
  284. c.Data["json"] = c.ResultError(err.Error())
  285. c.ServeJSON()
  286. return
  287. }
  288. scdmgr := new(bo.ScdMgr)
  289. scdinfo, err := scdmgr.One(tools.IsEmpty(lst.ScdId))
  290. if err != nil {
  291. c.Data["json"] = c.ResultError(err.Error())
  292. c.ServeJSON()
  293. return
  294. }
  295. models, _, err := obj.GetModels()
  296. if err != nil {
  297. c.Data["json"] = c.ResultError(err.Error())
  298. c.ServeJSON()
  299. return
  300. }
  301. rep := new(bo.TaskReportMgr)
  302. report, _ := rep.One(lst.Id)
  303. result := map[string]interface{}{}
  304. result["id"] = lst.Id
  305. result["code"] = lst.Code
  306. result["cr"] = lst.Cr
  307. result["ct"] = lst.Ct
  308. result["end_time"] = lst.EndTime
  309. result["memo"] = lst.Memo
  310. result["name"] = lst.Name
  311. result["report_id"] = lst.ReportId //报告模板ID
  312. result["scd_id"] = lst.ScdId
  313. result["scd_info"] = scdinfo
  314. result["start_time"] = lst.StartTime
  315. result["models"] = models
  316. result["report_info"] = report //生成的报告信息
  317. c.Data["json"] = c.ResultOK(result, 1)
  318. c.ServeJSON()
  319. }
  320. // @Summary 获取指定检测任务步骤的实时检测状态
  321. // @Description 获取指定检测任务步骤的实时检测状态。该接口需要周期性调用,建议每2秒调用一次。
  322. // @Tags 检测任务服务接口
  323. // @Accept x-www-form-urlencoded
  324. // @Produce json
  325. // @Param id query int true "任务ID"
  326. // @Success 200 {object} ResultOK 成功
  327. // @Failure 500 {object} ResultError 失败
  328. // @router /task/check/step_info [get]
  329. func (c *TaskController) GetTaskMonitorInfo() {
  330. obj := new(bo.TaskMgr)
  331. obj.SetUserInfo(c.GetCurrentUserInfo())
  332. obj.Model = bo.T_data_task{}
  333. obj.Model.Id, _ = c.GetInt("id")
  334. lst, err := obj.One()
  335. if err != nil {
  336. c.Data["json"] = c.ResultError(err.Error())
  337. c.ServeJSON()
  338. return
  339. }
  340. scdmgr := new(bo.ScdMgr)
  341. _, err = scdmgr.One(tools.IsEmpty(lst.ScdId))
  342. if err != nil {
  343. c.Data["json"] = c.ResultError("任务还未开始检测")
  344. c.ServeJSON()
  345. return
  346. }
  347. info, cnt, err := obj.GetCheckStepInfo()
  348. if err != nil {
  349. c.Data["json"] = c.ResultError(err.Error())
  350. c.ServeJSON()
  351. return
  352. }
  353. c.Data["json"] = c.ResultOK(info, cnt)
  354. c.ServeJSON()
  355. }