taskController.go 9.6 KB

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