factor.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package http
  2. import (
  3. "strconv"
  4. "strings"
  5. "time"
  6. "go-common/app/admin/main/spy/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. )
  11. func factors(c *bm.Context) {
  12. var (
  13. data []*model.Factors
  14. err error
  15. )
  16. data, err = spySrv.Factors(c)
  17. if err != nil {
  18. log.Error("spySrv.Factors error(%v)", err)
  19. c.JSON(data, err)
  20. return
  21. }
  22. c.JSON(data, err)
  23. }
  24. func updateFactor(c *bm.Context) {
  25. var (
  26. params = c.Request.Form
  27. idsStr = params.Get("ids")
  28. name = params.Get("name")
  29. fs []*model.Factor
  30. err error
  31. )
  32. if name == "" {
  33. c.JSON(nil, ecode.RequestErr)
  34. return
  35. }
  36. idsArr := strings.Split(idsStr, ",")
  37. for _, idStr := range idsArr {
  38. id, theErr := strconv.ParseInt(idStr, 10, 64)
  39. if theErr != nil {
  40. c.JSON(nil, ecode.RequestErr)
  41. return
  42. }
  43. factorVal, theErr := strconv.ParseFloat(params.Get("id_"+idStr), 32)
  44. if theErr != nil || factorVal < 0 || factorVal > 1 {
  45. c.JSON(nil, ecode.RequestErr)
  46. return
  47. }
  48. fs = append(fs, &model.Factor{ID: id, FactorVal: float32(factorVal)})
  49. }
  50. err = spySrv.UpdateFactor(c, fs, name)
  51. if err != nil {
  52. log.Error("spySrv.UpdateFactor(%v) error(%v)", fs, err)
  53. c.JSON(nil, err)
  54. return
  55. }
  56. c.JSON(nil, nil)
  57. }
  58. func addFactor(c *bm.Context) {
  59. var (
  60. params = c.Request.Form
  61. nickname = params.Get("nick_name")
  62. serviceID int64
  63. eventID int64
  64. groupID int64
  65. riskLevel int64
  66. factorVal float64
  67. categoryID int64
  68. err error
  69. )
  70. if serviceID, err = strconv.ParseInt(params.Get("service_id"), 10, 64); err != nil {
  71. c.JSON(nil, ecode.RequestErr)
  72. return
  73. }
  74. if eventID, err = strconv.ParseInt(params.Get("event_id"), 10, 64); err != nil {
  75. c.JSON(nil, ecode.RequestErr)
  76. return
  77. }
  78. if groupID, err = strconv.ParseInt(params.Get("group_id"), 10, 64); err != nil {
  79. c.JSON(nil, ecode.RequestErr)
  80. return
  81. }
  82. if riskLevel, err = strconv.ParseInt(params.Get("risk_level"), 10, 8); err != nil {
  83. c.JSON(nil, ecode.RequestErr)
  84. return
  85. }
  86. if factorVal, err = strconv.ParseFloat(params.Get("factor_val"), 32); err != nil {
  87. c.JSON(nil, ecode.RequestErr)
  88. return
  89. }
  90. if categoryID, err = strconv.ParseInt(params.Get("category_id"), 10, 8); err != nil {
  91. c.JSON(nil, ecode.RequestErr)
  92. return
  93. }
  94. if factorVal > 1 || factorVal <= 0 {
  95. c.JSON(nil, ecode.RequestErr)
  96. return
  97. }
  98. if riskLevel < 1 || riskLevel > 10 {
  99. c.JSON(nil, ecode.RequestErr)
  100. return
  101. }
  102. err = spySrv.AddFactor(c, &model.Factor{
  103. NickName: nickname,
  104. ServiceID: serviceID,
  105. EventID: eventID,
  106. GroupID: groupID,
  107. RiskLevel: int8(riskLevel),
  108. FactorVal: float32(factorVal),
  109. CTime: time.Now(),
  110. CategoryID: int8(categoryID),
  111. })
  112. if err != nil {
  113. log.Error("spySrv.AddFactor error(%v)", err)
  114. c.JSON(nil, err)
  115. return
  116. }
  117. c.JSON(nil, nil)
  118. }
  119. func addEvent(c *bm.Context) {
  120. var (
  121. params = c.Request.Form
  122. name = params.Get("name")
  123. nickName = params.Get("nick_name")
  124. err error
  125. serviceID int64
  126. status int64
  127. )
  128. if serviceID, err = strconv.ParseInt(params.Get("service_id"), 10, 64); err != nil {
  129. c.JSON(nil, ecode.RequestErr)
  130. return
  131. }
  132. if status, err = strconv.ParseInt(params.Get("status"), 10, 8); err != nil {
  133. c.JSON(nil, ecode.RequestErr)
  134. return
  135. }
  136. err = spySrv.AddEvent(c, &model.Event{
  137. Name: name,
  138. NickName: nickName,
  139. ServiceID: serviceID,
  140. Status: int8(status),
  141. CTime: time.Now(),
  142. MTime: time.Now(),
  143. })
  144. if err != nil {
  145. log.Error("spySrv.AddEvent error(%v)", err)
  146. c.JSON(nil, err)
  147. return
  148. }
  149. c.JSON(nil, nil)
  150. }
  151. func addService(c *bm.Context) {
  152. var (
  153. params = c.Request.Form
  154. name = params.Get("name")
  155. nickName = params.Get("nick_name")
  156. err error
  157. status int64
  158. )
  159. if status, err = strconv.ParseInt(params.Get("status"), 10, 8); err != nil {
  160. c.JSON(nil, ecode.RequestErr)
  161. return
  162. }
  163. err = spySrv.AddService(c, &model.Service{
  164. Name: name,
  165. NickName: nickName,
  166. Status: int8(status),
  167. CTime: time.Now(),
  168. MTime: time.Now(),
  169. })
  170. if err != nil {
  171. log.Error("spySrv.AddService error(%v)", err)
  172. c.JSON(nil, err)
  173. return
  174. }
  175. c.JSON(nil, nil)
  176. }
  177. func addGroup(c *bm.Context) {
  178. var (
  179. params = c.Request.Form
  180. name = params.Get("name")
  181. err error
  182. )
  183. err = spySrv.AddGroup(c, &model.FactorGroup{
  184. Name: name,
  185. CTime: time.Now(),
  186. })
  187. if err != nil {
  188. log.Error("spySrv.AddGroup error(%v)", err)
  189. c.JSON(nil, err)
  190. return
  191. }
  192. c.JSON(nil, nil)
  193. }
  194. func updateEventName(c *bm.Context) {
  195. var (
  196. params = c.Request.Form
  197. nickname string
  198. id int64
  199. err error
  200. )
  201. nickname = params.Get("nickname")
  202. if err != nil || len(nickname) > model.MaxRemarkLen {
  203. c.JSON(nil, ecode.RequestErr)
  204. return
  205. }
  206. id, err = strconv.ParseInt(params.Get("id"), 10, 64)
  207. if err != nil {
  208. c.JSON(nil, ecode.RequestErr)
  209. return
  210. }
  211. err = spySrv.UpdateEventName(c, &model.Event{
  212. ID: id,
  213. NickName: nickname,
  214. })
  215. if err != nil {
  216. log.Error("spySrv.UpdateEventName error(%v)", err)
  217. c.JSON(nil, err)
  218. return
  219. }
  220. c.JSON(nil, nil)
  221. }