taskController.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. )
  18. //检测任务服务
  19. type TaskController struct {
  20. BaseController
  21. }
  22. func init() {
  23. }
  24. // @Summary 创建新的检测任务
  25. // @Description 创建新的检测任务
  26. // @Tags 检测任务服务接口
  27. // @Accept x-www-form-urlencoded
  28. // @Produce json
  29. // @Param id formData int false "任务ID。指定id值大于0时为编辑操作;否则为新增操作"
  30. // @Param name formData string true "任务名称"
  31. // @Param station_id formData int true "变电站ID"
  32. // @Param memo formData string false "任务说明"
  33. // @Param report_id formData int true "报告模板ID"
  34. // @Param scd_id formData int true "SCD文件ID"
  35. // @Param modelids formData array true "间隔-模型关系。数据格式:[[间隔id,模型id],[间隔id,模型id],..]多个间隔-模型关系之间逗号分隔"
  36. // @Success 200 {object} ResultOK 成功
  37. // @Failure 500 {object} ResultError 失败
  38. // @router /task/save [post]
  39. func (c *TaskController) SaveTaskInfo() {
  40. id, _ := c.GetInt("id")
  41. obj := new(bo.TaskMgr)
  42. obj.SetUserInfo(c.GetCurrentUserInfo())
  43. obj.Model = bo.T_data_task{Id: id}
  44. obj.Model.Name = c.GetString("name")
  45. obj.Model.StationId, _ = c.GetInt("station_id")
  46. obj.Model.Memo = c.GetString("memo")
  47. obj.Model.ReportId, _ = c.GetInt("report_id")
  48. obj.Model.ScdId, _ = c.GetInt64("scd_id")
  49. obj.Model.State = 0
  50. if obj.Model.Id == 0 {
  51. obj.Model.Code = fmt.Sprintf("T-%d-%s%s", obj.Model.StationId, tools.NowTime())
  52. } else {
  53. obj.Model.Code = c.GetString("code")
  54. }
  55. modelids := c.GetString("modelids")
  56. if obj.Model.Name == "" {
  57. c.Data["json"] = c.ResultError("检测任务名称不能为空")
  58. c.ServeJSON()
  59. return
  60. }
  61. if obj.Model.ReportId == 0 {
  62. c.Data["json"] = c.ResultError("检测报告模型不能为空")
  63. c.ServeJSON()
  64. return
  65. }
  66. if obj.Model.StationId == 0 {
  67. c.Data["json"] = c.ResultError("检测变电站不能为空")
  68. c.ServeJSON()
  69. return
  70. }
  71. if obj.Model.ScdId == 0 {
  72. c.Data["json"] = c.ResultError("检测的SCD文件不能为空")
  73. c.ServeJSON()
  74. return
  75. }
  76. if modelids == "" || modelids == "[]" {
  77. c.Data["json"] = c.ResultError("检测任务的间隔检测模型不能为空")
  78. c.ServeJSON()
  79. return
  80. }
  81. area_models := [][]int{}
  82. err1 := json.Unmarshal([]byte(modelids), &area_models)
  83. if err1 != nil {
  84. c.Data["json"] = c.ResultError(err1.Error())
  85. c.ServeJSON()
  86. return
  87. }
  88. taskid, err := obj.Save()
  89. if err != nil {
  90. c.Data["json"] = c.ResultError(err.Error())
  91. c.ServeJSON()
  92. return
  93. }
  94. modelObj := new(bo.TaskModelMgr)
  95. modelObj.SetUserInfo(c.GetCurrentUserInfo())
  96. modelObj.Model.TaskId = taskid
  97. err = modelObj.Save(area_models)
  98. if err != nil {
  99. obj.Delete()
  100. c.Data["json"] = c.ResultError(err.Error())
  101. c.ServeJSON()
  102. return
  103. }
  104. c.Data["json"] = c.ResultOK("", 0)
  105. c.ServeJSON()
  106. }
  107. // @Summary 删除指定的检测任务
  108. // @Description 删除指定的检测任务
  109. // @Tags 检测任务服务接口
  110. // @Accept x-www-form-urlencoded
  111. // @Produce json
  112. // @Param id formData int true "检测任务ID"
  113. // @Success 200 {object} ResultOK 成功
  114. // @Failure 500 {object} ResultError 失败
  115. // @router /task/delete [post]
  116. func (c *TaskController) SetTaskActiveStart() {
  117. id, _ := c.GetInt("id")
  118. if id == 0 {
  119. c.Data["json"] = c.ResultError("任务编号不能为空!")
  120. c.ServeJSON()
  121. return
  122. }
  123. obj := new(bo.TaskMgr)
  124. obj.SetUserInfo(c.GetCurrentUserInfo())
  125. obj.Model = bo.T_data_task{}
  126. obj.Model.Id = id
  127. err := obj.Delete()
  128. if err != nil {
  129. c.Data["json"] = c.ResultError(err.Error())
  130. c.ServeJSON()
  131. return
  132. }
  133. c.Data["json"] = c.ResultOK("", 0)
  134. c.ServeJSON()
  135. }
  136. // @Summary 开始进行检测
  137. // @Description 开始进行检测
  138. // @Tags 检测任务服务接口
  139. // @Accept x-www-form-urlencoded
  140. // @Produce json
  141. // @Param id formData int true "检测任务ID"
  142. // @Success 200 {object} ResultOK 成功
  143. // @Failure 500 {object} ResultError 失败
  144. // @router /task/start [post]
  145. func (c *TaskController) DeleteTask() {
  146. id, _ := c.GetInt("id")
  147. if id == 0 {
  148. c.Data["json"] = c.ResultError("任务编号不能为空!")
  149. c.ServeJSON()
  150. return
  151. }
  152. obj := new(bo.TaskMgr)
  153. obj.SetUserInfo(c.GetCurrentUserInfo())
  154. obj.Model = bo.T_data_task{}
  155. obj.Model.Id = id
  156. err := obj.SetActive(1)
  157. if err != nil {
  158. c.Data["json"] = c.ResultError(err.Error())
  159. c.ServeJSON()
  160. return
  161. }
  162. c.Data["json"] = c.ResultOK("", 0)
  163. c.ServeJSON()
  164. }
  165. // @Summary 取消或者终止检测
  166. // @Description 取消或者终止检测
  167. // @Tags 检测任务服务接口
  168. // @Accept x-www-form-urlencoded
  169. // @Produce json
  170. // @Param id formData int true "检测任务ID"
  171. // @Success 200 {object} ResultOK 成功
  172. // @Failure 500 {object} ResultError 失败
  173. // @router /task/stop [post]
  174. func (c *TaskController) SetTaskActiveStop() {
  175. id, _ := c.GetInt("id")
  176. if id == 0 {
  177. c.Data["json"] = c.ResultError("任务编号不能为空!")
  178. c.ServeJSON()
  179. return
  180. }
  181. obj := new(bo.TaskMgr)
  182. obj.SetUserInfo(c.GetCurrentUserInfo())
  183. obj.Model = bo.T_data_task{}
  184. obj.Model.Id = id
  185. err := obj.SetActive(3)
  186. if err != nil {
  187. c.Data["json"] = c.ResultError(err.Error())
  188. c.ServeJSON()
  189. return
  190. }
  191. c.Data["json"] = c.ResultOK("", 0)
  192. c.ServeJSON()
  193. }
  194. // @Summary 查询检测任务
  195. // @Description 查询检测任务。支持任务名称、检测状态等过滤条件
  196. // @Tags 检测任务服务接口
  197. // @Accept x-www-form-urlencoded
  198. // @Produce json
  199. // @Param pageno query int true "当前页码。默认为1"
  200. // @Param pagesize query int true "每页显示数据数。默认为20"
  201. // @Param id query int false "任务ID"
  202. // @Param station_id query int false "变电站ID"
  203. // @Param name query string false "名称"
  204. // @Param start_time query string false "检测开始日期。格式:yyyy-mm-dd"
  205. // @Param end_time query string false "检测结束日期。格式:yyyy-mm-dd"
  206. // @Param state query int false "检测状态。空:全部 0:未检查 1:检测中 2:检测结束 3:异常中断"
  207. // @Success 200 {object} ResultOK 成功
  208. // @Failure 500 {object} ResultError 失败
  209. // @router /task/list [get]
  210. func (c *TaskController) GetTaskList() {
  211. obj := new(bo.TaskMgr)
  212. obj.SetUserInfo(c.GetCurrentUserInfo())
  213. obj.Model = bo.T_data_task{}
  214. obj.Model.Id, _ = c.GetInt("id")
  215. obj.Model.StationId, _ = c.GetInt("station_id")
  216. obj.Model.Name = c.GetString("name")
  217. obj.Model.State, _ = c.GetInt("state", -1)
  218. obj.Model.StartTime = c.GetString("start_time")
  219. obj.Model.EndTime = c.GetString("end_time")
  220. pi, _ := c.GetInt("pagesize", 20)
  221. po, _ := c.GetInt("pageno", 0)
  222. if po == 0 {
  223. po, _ = c.GetInt("pageindex", 0)
  224. }
  225. if po == 0 {
  226. po = 1
  227. }
  228. lst, cnt, err := obj.List(po, pi)
  229. if err != nil {
  230. c.Data["json"] = c.ResultError(err.Error())
  231. c.ServeJSON()
  232. return
  233. }
  234. c.Data["json"] = c.ResultOK(lst, cnt)
  235. c.ServeJSON()
  236. }