taskController.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 pageno query int true "当前页码。默认为1"
  202. // @Param pagesize query int true "每页显示数据数。默认为20"
  203. // @Param id query int false "任务ID"
  204. // @Param station_id query int false "变电站ID"
  205. // @Param name query string false "名称"
  206. // @Param start_time query string false "检测开始日期。格式:yyyy-mm-dd"
  207. // @Param end_time query string false "检测结束日期。格式:yyyy-mm-dd"
  208. // @Param state query int false "检测状态。空:全部 0:未检查 1:检测中 2:检测结束 3:异常中断"
  209. // @Success 200 {object} ResultOK 成功
  210. // @Failure 500 {object} ResultError 失败
  211. // @router /task/list [get]
  212. func (c *TaskController) GetTaskList() {
  213. obj := new(bo.TaskMgr)
  214. obj.SetUserInfo(c.GetCurrentUserInfo())
  215. obj.Model = bo.T_data_task{}
  216. obj.Model.Id, _ = c.GetInt("id")
  217. obj.Model.StationId, _ = c.GetInt("station_id")
  218. obj.Model.Name = c.GetString("name")
  219. obj.Model.State, _ = c.GetInt("state", -1)
  220. obj.Model.StartTime = c.GetString("start_time")
  221. obj.Model.EndTime = c.GetString("end_time")
  222. pi, _ := c.GetInt("pagesize", 20)
  223. po, _ := c.GetInt("pageno", 0)
  224. if po == 0 {
  225. po, _ = c.GetInt("pageindex", 0)
  226. }
  227. if po == 0 {
  228. po = 1
  229. }
  230. lst, cnt, err := obj.List(po, pi)
  231. if err != nil {
  232. c.Data["json"] = c.ResultError(err.Error())
  233. c.ServeJSON()
  234. return
  235. }
  236. c.Data["json"] = c.ResultOK(lst, cnt)
  237. c.ServeJSON()
  238. }
  239. // @Summary 获取指定检测任务的详细信息
  240. // @Description 获取指定检测任务的详细信息
  241. // @Tags 检测任务服务接口
  242. // @Accept x-www-form-urlencoded
  243. // @Produce json
  244. // @Param id query int true "任务ID"
  245. // @Success 200 {object} ResultOK 成功
  246. // @Failure 500 {object} ResultError 失败
  247. // @router /task/info [get]
  248. func (c *TaskController) GetTaskInfo() {
  249. obj := new(bo.TaskMgr)
  250. obj.SetUserInfo(c.GetCurrentUserInfo())
  251. obj.Model = bo.T_data_task{}
  252. obj.Model.Id, _ = c.GetInt("id")
  253. lst, err := obj.One()
  254. if err != nil {
  255. c.Data["json"] = c.ResultError(err.Error())
  256. c.ServeJSON()
  257. return
  258. }
  259. scdmgr := new(bo.ScdMgr)
  260. scdinfo, err := scdmgr.One(tools.IsEmpty(lst.ScdId))
  261. if err != nil {
  262. c.Data["json"] = c.ResultError(err.Error())
  263. c.ServeJSON()
  264. return
  265. }
  266. models, _, err := obj.GetModels()
  267. if err != nil {
  268. c.Data["json"] = c.ResultError(err.Error())
  269. c.ServeJSON()
  270. return
  271. }
  272. rep := new(bo.TaskReportMgr)
  273. report, _ := rep.One(lst.Id)
  274. result := map[string]interface{}{}
  275. result["id"] = lst.Id
  276. result["code"] = lst.Code
  277. result["cr"] = lst.Cr
  278. result["ct"] = lst.Ct
  279. result["end_time"] = lst.EndTime
  280. result["memo"] = lst.Memo
  281. result["name"] = lst.Name
  282. result["report_id"] = lst.ReportId //报告模板ID
  283. result["scd_id"] = lst.ScdId
  284. result["scd_info"] = scdinfo
  285. result["start_time"] = lst.StartTime
  286. result["models"] = models
  287. result["report_info"] = report //生成的报告信息
  288. c.Data["json"] = c.ResultOK(result, 1)
  289. c.ServeJSON()
  290. }
  291. // @Summary 获取指定检测任务步骤的实时检测状态
  292. // @Description 获取指定检测任务步骤的实时检测状态。该接口需要周期性调用,建议每2秒调用一次。
  293. // @Tags 检测任务服务接口
  294. // @Accept x-www-form-urlencoded
  295. // @Produce json
  296. // @Param id query int true "任务ID"
  297. // @Success 200 {object} ResultOK 成功
  298. // @Failure 500 {object} ResultError 失败
  299. // @router /task/check/step_info [get]
  300. func (c *TaskController) GetTaskMonitorInfo() {
  301. obj := new(bo.TaskMgr)
  302. obj.SetUserInfo(c.GetCurrentUserInfo())
  303. obj.Model = bo.T_data_task{}
  304. obj.Model.Id, _ = c.GetInt("id")
  305. lst, err := obj.One()
  306. if err != nil {
  307. c.Data["json"] = c.ResultError(err.Error())
  308. c.ServeJSON()
  309. return
  310. }
  311. scdmgr := new(bo.ScdMgr)
  312. _, err = scdmgr.One(tools.IsEmpty(lst.ScdId))
  313. if err != nil {
  314. c.Data["json"] = c.ResultError("任务还未开始检测")
  315. c.ServeJSON()
  316. return
  317. }
  318. info, cnt, err := obj.GetCheckStepInfo()
  319. if err != nil {
  320. c.Data["json"] = c.ResultError(err.Error())
  321. c.ServeJSON()
  322. return
  323. }
  324. c.Data["json"] = c.ResultOK(info, cnt)
  325. c.ServeJSON()
  326. }