egg.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package http
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. eggModel "go-common/app/admin/main/feed/model/egg"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. "github.com/jinzhu/gorm"
  10. )
  11. func addEgg(c *bm.Context) {
  12. var (
  13. err error
  14. p []eggModel.Plat
  15. )
  16. res := map[string]interface{}{}
  17. param := new(eggModel.Obj)
  18. if err = c.Bind(param); err != nil {
  19. return
  20. }
  21. uid, name := managerInfo(c)
  22. e := &eggModel.Egg{
  23. Stime: param.Stime,
  24. Etime: param.Etime,
  25. ShowCount: param.ShowCount,
  26. UID: uid,
  27. Publish: eggModel.NotPublish,
  28. Person: name,
  29. Delete: eggModel.NotDelete,
  30. }
  31. if err = json.Unmarshal([]byte(param.Plat), &p); err != nil {
  32. res["message"] = "参数有误:" + err.Error()
  33. c.JSONMap(res, ecode.RequestErr)
  34. return
  35. }
  36. if err = checkEgg(param.Query, e); err != nil {
  37. res["message"] = err.Error()
  38. c.JSONMap(res, ecode.RequestErr)
  39. return
  40. }
  41. if err = eggSvc.AddEgg(e, p, param.Query); err != nil {
  42. res["message"] = "添加失败:" + err.Error()
  43. c.JSONMap(res, ecode.RequestErr)
  44. return
  45. }
  46. c.JSON(nil, nil)
  47. }
  48. //delEgg delete egg
  49. func delEgg(c *bm.Context) {
  50. var (
  51. err error
  52. egg *eggModel.Egg
  53. )
  54. res := map[string]interface{}{}
  55. param := &struct {
  56. ID uint `form:"id" validate:"required"`
  57. }{}
  58. if err = c.Bind(param); err != nil {
  59. return
  60. }
  61. if egg, err = eggSvc.EggWithID(param.ID); err != nil {
  62. if err == gorm.ErrRecordNotFound {
  63. res["message"] = "找不到数据:" + err.Error()
  64. c.JSONMap(res, ecode.RequestErr)
  65. return
  66. }
  67. res["message"] = "删除失败:" + err.Error()
  68. c.JSONMap(res, ecode.RequestErr)
  69. return
  70. }
  71. if egg.Publish == eggModel.Publish {
  72. res["message"] = "已发布彩蛋不能删除"
  73. c.JSONMap(res, ecode.RequestErr)
  74. return
  75. }
  76. uid, name := managerInfo(c)
  77. if err = eggSvc.DelEgg(param.ID, name, uid); err != nil {
  78. res["message"] = "删除失败:" + err.Error()
  79. c.JSONMap(res, ecode.RequestErr)
  80. return
  81. }
  82. c.JSON(nil, nil)
  83. }
  84. //pubEgg publish egg
  85. func pubEgg(c *bm.Context) {
  86. var (
  87. err error
  88. )
  89. res := map[string]interface{}{}
  90. param := &struct {
  91. ID uint `form:"id" validate:"required"`
  92. Publish uint8 `form:"publish"`
  93. }{}
  94. if err = c.Bind(param); err != nil {
  95. return
  96. }
  97. uid, name := managerInfo(c)
  98. if err = eggSvc.PubEgg(param.ID, param.Publish, name, uid); err != nil {
  99. res["message"] = "发布失败:" + err.Error()
  100. c.JSONMap(res, ecode.RequestErr)
  101. return
  102. }
  103. c.JSON(nil, nil)
  104. }
  105. //updateEgg update egg
  106. func updateEgg(c *bm.Context) {
  107. var (
  108. err error
  109. p []eggModel.Plat
  110. )
  111. res := map[string]interface{}{}
  112. param := new(eggModel.ObjUpdate)
  113. if err = c.Bind(param); err != nil {
  114. return
  115. }
  116. uid, name := managerInfo(c)
  117. e := &eggModel.Egg{
  118. ID: param.ID,
  119. Stime: param.Stime,
  120. Etime: param.Etime,
  121. ShowCount: param.ShowCount,
  122. UID: uid,
  123. Publish: eggModel.NotPublish,
  124. Person: name,
  125. Delete: eggModel.NotDelete,
  126. }
  127. if err = json.Unmarshal([]byte(param.Plat), &p); err != nil {
  128. res["message"] = "json解析失败:" + err.Error()
  129. c.JSONMap(res, ecode.RequestErr)
  130. return
  131. }
  132. if err = checkEgg(param.Query, e); err != nil {
  133. res["message"] = err.Error()
  134. c.JSONMap(res, ecode.RequestErr)
  135. return
  136. }
  137. if err = eggSvc.UpdateEgg(e, p, param.Query); err != nil {
  138. res["message"] = "更新失败:" + err.Error()
  139. c.JSONMap(res, ecode.RequestErr)
  140. return
  141. }
  142. c.JSON(nil, nil)
  143. }
  144. //checkEgg check egg error
  145. func checkEgg(w []string, e *eggModel.Egg) (err error) {
  146. var (
  147. flag bool
  148. eW string
  149. )
  150. if len(w) > 20 {
  151. err = fmt.Errorf("搜索词不能大于20个")
  152. return
  153. }
  154. if e.ShowCount > 10 {
  155. err = fmt.Errorf("彩蛋展示次数不能大于10")
  156. return
  157. }
  158. if flag, eW, err = eggSvc.IsWdExist(w, e.Stime, e.Etime, e.ID); err != nil {
  159. log.Error("eggSrv.checkEgg IsWdExist error(%v)", err)
  160. return
  161. }
  162. if flag {
  163. err = fmt.Errorf("搜索词 (%v) 已有彩蛋,请勿重复添加", eW)
  164. return
  165. }
  166. return
  167. }
  168. //indexEgg get egg list
  169. func indexEgg(c *bm.Context) {
  170. var (
  171. err error
  172. eggs *eggModel.IndexPager
  173. )
  174. res := map[string]interface{}{}
  175. param := &eggModel.IndexParam{}
  176. if err = c.Bind(param); err != nil {
  177. return
  178. }
  179. if eggs, err = eggSvc.IndexEgg(param); err != nil {
  180. res["message"] = "查询失败:" + err.Error()
  181. c.JSONMap(res, ecode.RequestErr)
  182. return
  183. }
  184. c.JSON(eggs, nil)
  185. }
  186. //searchEgg search api for search
  187. func searchEgg(c *bm.Context) {
  188. var (
  189. err error
  190. eggs []eggModel.SearchEgg
  191. )
  192. res := map[string]interface{}{}
  193. if eggs, err = eggSvc.SearchEgg(); err != nil {
  194. res["message"] = "搜索查询失败:" + err.Error()
  195. c.JSONMap(res, ecode.RequestErr)
  196. return
  197. }
  198. c.JSON(eggs, nil)
  199. }
  200. //SearchEggWeb search api for web
  201. func SearchEggWeb(c *bm.Context) {
  202. var (
  203. err error
  204. eggs []eggModel.SearchEggWeb
  205. )
  206. res := map[string]interface{}{}
  207. if eggs, err = eggSvc.SearchEggWeb(); err != nil {
  208. res["message"] = "Web搜索查询失败:" + err.Error()
  209. c.JSONMap(res, ecode.RequestErr)
  210. return
  211. }
  212. c.JSON(eggs, nil)
  213. }