actions.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "dashoo.cn/base_common/utils"
  6. "dashoo.cn/mcs_common/business/actions"
  7. )
  8. // 报警项目接口说明
  9. type ActionsController struct {
  10. BaseController
  11. }
  12. type ActionModel struct {
  13. Name string `json:"name"`
  14. Item string `json:"item"`
  15. SendTo string `json:"sendto"`
  16. Wdid string `json:"wdid"`
  17. }
  18. // @Title 报警项目列表
  19. // @Description 报警项目列表
  20. // @Success 200 {object} business.device.DeviceChannels
  21. // @router /list [get]
  22. func (this *ActionsController) List() {
  23. var list []actions.Actions
  24. svc := actions.GetActionsService(utils.DBE)
  25. fmt.Println("-----lisasdasfdft-------")
  26. page := this.GetPageInfoForm()
  27. keyword := this.GetString("keyword123")
  28. where := " CreateUserId='" + utils.ToStr(this.User.Id) + "' "
  29. if keyword != "" {
  30. where = where + " and AName='" + keyword + "'"
  31. }
  32. println("message:", where)
  33. total := svc.GetPagingEntitiesWithOrder(page.CurrentPage, page.Size, "CreateOn", false, &list, where)
  34. var datainfo DataInfo
  35. fmt.Println("-----list-------", list)
  36. datainfo.Items = list
  37. datainfo.CurrentItemCount = total
  38. this.Data["json"] = &datainfo
  39. this.ServeJSON()
  40. }
  41. // @Title 创建报警项目
  42. // @Description 创建报警项目
  43. // @Param body body business.device.DeviceChannels "报警项目信息"
  44. // @Success 200 {object} controllers.Request
  45. // @router / [post]
  46. func (this *ActionsController) AddPost() {
  47. var model ActionModel
  48. var jsonblob = this.Ctx.Input.RequestBody
  49. json.Unmarshal(jsonblob, &model)
  50. var errinfo ErrorInfo
  51. var status Status
  52. var action actions.Actions
  53. u, p := this.GetuAndp()
  54. action.AName = model.Name
  55. action.AItem, _ = utils.StrTo(model.Item).Int()
  56. if action.AItem == 1 {
  57. action.ItemName = "电子邮件"
  58. action.SPara1 = model.SendTo
  59. } else if action.AItem == 2 {
  60. action.ItemName = "网址推送"
  61. action.SPara2 = model.SendTo
  62. } else if action.AItem == 5 {
  63. action.ItemName = "短信"
  64. action.SPara3 = model.SendTo
  65. } else if action.AItem == 6 {
  66. action.ItemName = "语音"
  67. action.SPara6 = model.SendTo
  68. } else if action.AItem == 8 {
  69. action.ItemName = "网页消息"
  70. action.SPara5 = this.User.Id
  71. }
  72. strUrl := utils.Cfg.MustValue("server", "apiurl") + "/actions/?u=" + u + "&p=" + p
  73. json.Unmarshal(Apipost(strUrl, "POST", action), &status)
  74. if status.Status != 0 {
  75. errinfo.Message = utils.AlertProcess("添加失败!" + status.Message)
  76. errinfo.Code = -1
  77. this.Data["json"] = &errinfo
  78. this.ServeJSON()
  79. } else {
  80. svc := actions.GetActionsService(utils.DBE)
  81. action.AccCode = this.GetAccode()
  82. action.Wdid = status.Id
  83. action.Id, _ = utils.StrTo(status.Id).Int()
  84. action.CreateUserId, _ = utils.StrTo(this.User.Id).Int()
  85. action.CreateBy = this.User.Realname
  86. _, err := svc.InsertEntity(&action) //插入数据库
  87. if err == nil {
  88. errinfo.Message = utils.AlertProcess("添加成功!")
  89. errinfo.Code = 0
  90. this.Data["json"] = &errinfo
  91. this.ServeJSON()
  92. } else {
  93. errinfo.Message = utils.AlertProcess("添加失败!" + err.Error())
  94. errinfo.Code = -2
  95. this.Data["json"] = &errinfo
  96. this.ServeJSON()
  97. }
  98. }
  99. }
  100. // @Title 编辑报警项目
  101. // @Description 编辑报警项目
  102. // @Param id path string true "需要修改的报警项目编号"
  103. // @Param body body business.device.DeviceChannels "报警项目信息"
  104. // @Success 200 {object} ErrorInfo
  105. // @router /:id [put]
  106. func (this *ActionsController) EditPost() {
  107. var model ActionModel
  108. var jsonblob = this.Ctx.Input.RequestBody
  109. json.Unmarshal(jsonblob, &model)
  110. id := this.Ctx.Input.Param(":id")
  111. var action actions.Actions
  112. var actionempty actions.Actions
  113. var status Status
  114. var errinfo ErrorInfo
  115. u, p := this.GetuAndp()
  116. action.AName = model.Name
  117. action.AItem, _ = utils.StrTo(model.Item).Int()
  118. if action.AItem == 1 {
  119. action.ItemName = "电子邮件"
  120. action.SPara1 = model.SendTo
  121. } else if action.AItem == 2 {
  122. action.ItemName = "网址推送"
  123. action.SPara2 = model.SendTo
  124. } else if action.AItem == 5 {
  125. action.ItemName = "短信"
  126. action.SPara3 = model.SendTo
  127. } else if action.AItem == 6 {
  128. action.ItemName = "语音"
  129. action.SPara6 = model.SendTo
  130. } else if action.AItem == 8 {
  131. action.ItemName = "网页消息"
  132. action.SPara5 = this.User.Id
  133. }
  134. strUrl := utils.Cfg.MustValue("server", "apiurl") + "/actions/" + model.Wdid + "?u=" + u + "&p=" + p
  135. json.Unmarshal(Apipost(strUrl, "PUT", &action), &status)
  136. if status.Status != 0 {
  137. errinfo.Message = utils.AlertProcess("保存失败!" + status.Message)
  138. errinfo.Code = -1
  139. this.Data["json"] = &errinfo
  140. this.ServeJSON()
  141. } else {
  142. svc := actions.GetActionsService(utils.DBE)
  143. var cols []string = []string{"AName", "AItem", "ItemName", "SPara1", "SPara2", "SPara3", "SPara4", "SPara5", "SPara6"}
  144. svc.UpdateEntityAndBackupByCols(id, &action, &actionempty, cols, utils.ToStr(this.User.Id), this.User.Realname)
  145. errinfo.Message = utils.AlertProcess("保存成功!")
  146. errinfo.Code = 0
  147. this.Data["json"] = &errinfo
  148. this.ServeJSON()
  149. }
  150. }
  151. // @Title 删除报警项目
  152. // @Description 删除报警项目
  153. // @Param id path string true "需要删除的报警项目编号"
  154. // @Success 200 {object} ErrorInfo
  155. // @Failure 403 :id 为空
  156. // @router /:id [delete]
  157. func (this *ActionsController) Delete() {
  158. id := this.Ctx.Input.Param(":id")
  159. wdid := this.GetString("wdid")
  160. var errinfo ErrorInfo
  161. svc := actions.GetActionsService(utils.DBE)
  162. if svc.IsUsed_TriggerAbnormal(id) {
  163. errinfo.Message = "删除失败!此设置已在【异常提醒设置】中使用"
  164. errinfo.Code = -2
  165. this.Data["json"] = &errinfo
  166. this.ServeJSON()
  167. } else {
  168. var entity actions.Actions
  169. var status Status
  170. u, p := this.GetuAndp()
  171. strUrl := utils.Cfg.MustValue("server", "apiurl") + "/actions/" + wdid + "?u=" + u + "&p=" + p
  172. json.Unmarshal(Apipost(strUrl, "DELETE", &entity), &status)
  173. if status.Status == 0 {
  174. svc.DeleteEntityById(id, entity)
  175. errinfo.Message = "删除成功!"
  176. errinfo.Code = 0
  177. this.Data["json"] = &errinfo
  178. this.ServeJSON()
  179. } else if status.Status == -1 {
  180. errinfo.Message = "删除失败!无删除权限!"
  181. errinfo.Code = -1
  182. this.Data["json"] = &errinfo
  183. this.ServeJSON()
  184. } else if status.Status == -3 {
  185. errinfo.Message = "删除失败!此项目设置已在设备【" + status.Message + "】中使用"
  186. errinfo.Code = -3
  187. this.Data["json"] = &errinfo
  188. this.ServeJSON()
  189. }
  190. }
  191. }