whitelist.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. package http
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "go-common/app/admin/main/creative/model/whitelist"
  6. accapi "go-common/app/service/main/account/api"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/http/blademaster/render"
  11. "net/http"
  12. "time"
  13. )
  14. var maxMid = int64(2147483647)
  15. func viewWhiteList(c *bm.Context) {
  16. var (
  17. err error
  18. info = &whitelist.Whitelist{}
  19. )
  20. if err = c.Bind(info); err != nil {
  21. return
  22. }
  23. if info.ID == 0 {
  24. c.JSON(nil, ecode.RequestErr)
  25. return
  26. }
  27. if err = svc.DB.Debug().Model(&whitelist.Whitelist{}).First(info, info.ID).Error; err != nil {
  28. c.JSON(nil, err)
  29. return
  30. }
  31. if info.ID == 0 {
  32. c.JSON(nil, ecode.NothingFound)
  33. } else {
  34. pfl, _ := svc.ProfileStat(c, info.MID)
  35. if pfl.Profile != nil {
  36. info.Name = pfl.Profile.Name
  37. }
  38. info.Fans = pfl.Follower
  39. info.CurrentLevel = pfl.LevelInfo.Cur
  40. c.JSON(info, nil)
  41. }
  42. }
  43. func listWhiteList(c *bm.Context) {
  44. var (
  45. err error
  46. wls []*whitelist.Whitelist
  47. total int
  48. )
  49. v := new(struct {
  50. MID int64 `form:"mid"`
  51. AdminMID int64 `form:"admin_mid"`
  52. State int8 `form:"state"`
  53. Type int8 `form:"type"`
  54. Page int `form:"pn"`
  55. PageSize int `form:"ps"`
  56. })
  57. if err = c.Bind(v); err != nil {
  58. return
  59. }
  60. if v.Page < 1 {
  61. v.Page = 1
  62. }
  63. if v.PageSize < 20 {
  64. v.PageSize = 20
  65. }
  66. where := " state = ? AND type = ? "
  67. if v.MID != 0 {
  68. where += " AND mid = ?"
  69. if v.AdminMID != 0 {
  70. where += " AND admin_id = ?"
  71. if err = svc.DB.Debug().Model(&whitelist.Whitelist{}).Order("ctime DESC").Offset((v.Page-1)*v.PageSize).Limit(v.PageSize).Find(&wls, where, v.State, v.Type, v.MID, v.AdminMID).Error; err != nil {
  72. c.JSON(nil, err)
  73. }
  74. svc.DB.Debug().Model(&whitelist.Whitelist{}).Where(where, v.State, v.Type, v.MID, v.AdminMID).Count(&total)
  75. } else {
  76. if err = svc.DB.Debug().Model(&whitelist.Whitelist{}).Order("ctime DESC").Offset((v.Page-1)*v.PageSize).Limit(v.PageSize).Find(&wls, where, v.State, v.Type, v.MID).Error; err != nil {
  77. c.JSON(nil, err)
  78. }
  79. svc.DB.Debug().Model(&whitelist.Whitelist{}).Where(where, v.State, v.Type, v.MID).Count(&total)
  80. }
  81. } else {
  82. if v.AdminMID != 0 {
  83. where += " AND admin_id = ?"
  84. if err = svc.DB.Debug().Model(&whitelist.Whitelist{}).Order("ctime DESC").Offset((v.Page-1)*v.PageSize).Limit(v.PageSize).Find(&wls, where, v.State, v.Type, v.AdminMID).Error; err != nil {
  85. c.JSON(nil, err)
  86. }
  87. svc.DB.Debug().Model(&whitelist.Whitelist{}).Where(where, v.State, v.Type, v.AdminMID).Count(&total)
  88. } else {
  89. if err = svc.DB.Debug().Model(&whitelist.Whitelist{}).Order("ctime DESC").Offset((v.Page-1)*v.PageSize).Limit(v.PageSize).Find(&wls, where, v.State, v.Type).Error; err != nil {
  90. c.JSON(nil, err)
  91. }
  92. svc.DB.Debug().Model(&whitelist.Whitelist{}).Where(where, v.State, v.Type).Count(&total)
  93. }
  94. }
  95. wls, err = svc.Cards(c, wls)
  96. if err != nil {
  97. log.Error("svc.Cards error(%v)", err)
  98. c.JSON(nil, err)
  99. return
  100. }
  101. c.Render(http.StatusOK, render.MapJSON(map[string]interface{}{
  102. "code": 0,
  103. "message": "0",
  104. "data": wls,
  105. "pager": map[string]int{
  106. "page": v.Page,
  107. "pagesize": v.PageSize,
  108. "total": total,
  109. },
  110. }))
  111. }
  112. func exportWhiteList(c *bm.Context) {
  113. var (
  114. err error
  115. wls []*whitelist.Whitelist
  116. total int
  117. )
  118. v := new(struct {
  119. MID int64 `form:"mid;" `
  120. AdminMID int64 `form:"admin_mid;" `
  121. State int8 `form:"state"`
  122. Type int8 `form:"type"`
  123. Page int `form:"pn"`
  124. PageSize int `form:"ps"`
  125. })
  126. if err = c.Bind(v); err != nil {
  127. return
  128. }
  129. if v.Page < 1 {
  130. v.Page = 1
  131. }
  132. if v.PageSize < 20 {
  133. v.PageSize = 20
  134. }
  135. where := " state = ? AND type = ? "
  136. if v.MID != 0 {
  137. where += " AND mid = ?"
  138. if v.AdminMID != 0 {
  139. where += " AND admin_id = ?"
  140. if err = svc.DB.Debug().Model(&whitelist.Whitelist{}).Order("ctime DESC").Offset((v.Page-1)*v.PageSize).Limit(v.PageSize).Find(&wls, where, v.State, v.Type, v.MID, v.AdminMID).Error; err != nil {
  141. c.JSON(nil, err)
  142. }
  143. svc.DB.Debug().Model(&whitelist.Whitelist{}).Where(where, v.State, v.Type, v.MID, v.AdminMID).Count(&total)
  144. } else {
  145. if err = svc.DB.Debug().Model(&whitelist.Whitelist{}).Order("ctime DESC").Offset((v.Page-1)*v.PageSize).Limit(v.PageSize).Find(&wls, where, v.State, v.Type, v.MID).Error; err != nil {
  146. c.JSON(nil, err)
  147. }
  148. svc.DB.Debug().Model(&whitelist.Whitelist{}).Where(where, v.State, v.Type, v.MID).Count(&total)
  149. }
  150. } else {
  151. if v.AdminMID != 0 {
  152. where += " AND admin_id = ?"
  153. if err = svc.DB.Debug().Model(&whitelist.Whitelist{}).Order("ctime DESC").Offset((v.Page-1)*v.PageSize).Limit(v.PageSize).Find(&wls, where, v.State, v.Type, v.AdminMID).Error; err != nil {
  154. c.JSON(nil, err)
  155. }
  156. svc.DB.Debug().Model(&whitelist.Whitelist{}).Where(where, v.State, v.Type, v.AdminMID).Count(&total)
  157. } else {
  158. if err = svc.DB.Debug().Model(&whitelist.Whitelist{}).Order("ctime DESC").Offset((v.Page-1)*v.PageSize).Limit(v.PageSize).Find(&wls, where, v.State, v.Type).Error; err != nil {
  159. c.JSON(nil, err)
  160. }
  161. svc.DB.Debug().Model(&whitelist.Whitelist{}).Where(where, v.State, v.Type).Count(&total)
  162. }
  163. }
  164. wls, err = svc.Cards(c, wls)
  165. if err != nil {
  166. log.Error("svc.Cards error(%v)", err)
  167. c.JSON(nil, err)
  168. return
  169. }
  170. fWLS, err := formatWhilteList(wls)
  171. if err != nil {
  172. log.Error("formatWhilteList error(%v)", err)
  173. c.JSON(nil, err)
  174. return
  175. }
  176. c.Render(http.StatusOK, CSV{
  177. Content: FormatCSV(fWLS),
  178. Title: fmt.Sprintf("%s-%d-%d-%s", time.Now().Format("2006-01-02"), v.Type, v.State, "white_list"),
  179. })
  180. }
  181. func addWhiteList(c *bm.Context) {
  182. var (
  183. err error
  184. pfl *accapi.ProfileStatReply
  185. )
  186. v := new(struct {
  187. MID int64 `form:"mid" validate:"required,min=1,gte=1"`
  188. AdminMID int64 `form:"admin_mid" validate:"required,min=1,gte=1"`
  189. Comment string `form:"comment"`
  190. State int8 `form:"state"`
  191. Type int8 `form:"type"`
  192. })
  193. if err = c.Bind(v); err != nil {
  194. return
  195. }
  196. if v.MID > maxMid || v.AdminMID > maxMid {
  197. c.JSON(nil, ecode.RequestErr)
  198. return
  199. }
  200. pfl, err = svc.ProfileStat(c, v.MID)
  201. if err != nil || pfl == nil || pfl.Profile == nil || pfl.Profile.Mid == 0 {
  202. log.Error("svc.Card zero result error(%v)", err)
  203. c.JSON(nil, ecode.UserNotExist)
  204. return
  205. }
  206. m := &whitelist.Whitelist{
  207. State: v.State,
  208. Type: v.Type,
  209. Ctime: time.Now().Format("2006-01-02 15:04:05"),
  210. MID: v.MID,
  211. AdminMID: v.AdminMID,
  212. Comment: v.Comment,
  213. }
  214. db := svc.DB.Debug().Model(&whitelist.Whitelist{}).Create(m)
  215. if err = db.Error; err != nil {
  216. log.Error("creativeSvc.whitelist error(%v)", err)
  217. c.JSON(nil, err)
  218. }
  219. c.JSON(map[string]interface{}{
  220. "id": db.Value.(*whitelist.Whitelist).ID,
  221. }, nil)
  222. }
  223. func addBatchWhiteList(c *bm.Context) {
  224. var (
  225. err error
  226. )
  227. v := new(struct {
  228. Params string `form:"params" validate:"required"`
  229. })
  230. if err = c.Bind(v); err != nil {
  231. return
  232. }
  233. filterJSON := v.Params
  234. if len(filterJSON) == 0 {
  235. c.JSON(nil, ecode.RequestErr)
  236. return
  237. }
  238. type Filter struct {
  239. MID int64 `json:"mid"`
  240. AdminMID int64 `json:"admin_mid"`
  241. Comment string `json:"comment"`
  242. State int8 `json:"state"`
  243. Type int8 `json:"type"`
  244. }
  245. var filtersJSONData []*Filter
  246. if err = json.Unmarshal([]byte(filterJSON), &filtersJSONData); err != nil {
  247. err = ecode.RequestErr
  248. return
  249. }
  250. if len(filtersJSONData) == 0 {
  251. err = ecode.RequestErr
  252. return
  253. }
  254. db := svc.DB.Debug().Model(&whitelist.Whitelist{})
  255. for _, v := range filtersJSONData {
  256. m := &whitelist.Whitelist{
  257. State: v.State,
  258. Type: v.Type,
  259. Ctime: time.Now().Format("2006-01-02 15:04:05"),
  260. MID: v.MID,
  261. AdminMID: v.AdminMID,
  262. Comment: v.Comment,
  263. }
  264. db.Create(m)
  265. if err = db.Error; err != nil {
  266. log.Error("creativeSvc.batchWhitelist error(%v)", err)
  267. c.JSON(nil, err)
  268. return
  269. }
  270. }
  271. c.JSON(nil, err)
  272. }
  273. func upWhiteList(c *bm.Context) {
  274. var (
  275. wl = &whitelist.Whitelist{}
  276. pfl *accapi.ProfileStatReply
  277. err error
  278. )
  279. v := new(struct {
  280. ID int64 `form:"id"`
  281. MID int64 `form:"mid" validate:"required,min=1,gte=1"`
  282. AdminMID int64 `form:"admin_mid" validate:"required,min=1,gte=1"`
  283. Comment string `form:"comment"`
  284. })
  285. if err = c.Bind(v); err != nil {
  286. return
  287. }
  288. if v.ID == 0 || v.MID > maxMid || v.AdminMID > maxMid {
  289. c.JSON(nil, ecode.RequestErr)
  290. return
  291. }
  292. pfl, err = svc.ProfileStat(c, v.MID)
  293. if err != nil || pfl == nil || pfl.Profile == nil || pfl.Profile.Mid == 0 {
  294. log.Error("svc.Card zero result error(%v)", err)
  295. c.JSON(nil, ecode.UserNotExist)
  296. return
  297. }
  298. if err = svc.DB.Debug().Model(&whitelist.Whitelist{}).Find(wl, v.ID).Error; err != nil {
  299. c.JSON(nil, err)
  300. return
  301. }
  302. if err = svc.DB.Debug().Model(&whitelist.Whitelist{ID: v.ID}).Update(map[string]interface {
  303. }{
  304. "id": v.ID,
  305. "mid": v.MID,
  306. "admin_mid": v.AdminMID,
  307. "comment": v.Comment,
  308. "mtime": time.Now().Format("2006-01-02 15:04:05"),
  309. }).Error; err != nil {
  310. log.Error("svc.save error(%v)", err)
  311. }
  312. c.JSON(nil, err)
  313. }
  314. func delWhiteList(c *bm.Context) {
  315. var (
  316. wl = &whitelist.Whitelist{}
  317. err error
  318. )
  319. if err = c.Bind(wl); err != nil {
  320. return
  321. }
  322. if wl.ID == 0 {
  323. c.JSON(nil, ecode.RequestErr)
  324. return
  325. }
  326. if err = svc.DB.Debug().Model(&whitelist.Whitelist{}).Find(wl, wl.ID).Error; err != nil {
  327. c.JSON(nil, err)
  328. return
  329. }
  330. if err = svc.DB.Debug().Model(&whitelist.Whitelist{ID: wl.ID}).Update(map[string]interface {
  331. }{
  332. "id": wl.ID,
  333. "State": 0,
  334. "mtime": time.Now().Format("2006-01-02 15:04:05"),
  335. }).Error; err != nil {
  336. log.Error("svc.del WhiteList error(%v)", err)
  337. }
  338. c.JSON(nil, err)
  339. }