index.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. package http
  2. import (
  3. "go-common/app/admin/main/creative/model/operation"
  4. "go-common/library/ecode"
  5. "go-common/library/log"
  6. bm "go-common/library/net/http/blademaster"
  7. "go-common/library/net/http/blademaster/render"
  8. "net/http"
  9. "time"
  10. )
  11. var (
  12. typesMap = map[int8]int8{
  13. 0: 0, // 显示
  14. 1: 1, // 隐藏
  15. 2: 2, // 全部
  16. }
  17. platformMap = map[int8]int8{
  18. 0: 0, // web+app
  19. 1: 1, // app
  20. 2: 2, // web
  21. 100: 100, // 全平台
  22. }
  23. )
  24. func viewNotice(c *bm.Context) {
  25. var (
  26. err error
  27. info = &operation.Operation{}
  28. )
  29. v := new(struct {
  30. ID int64 `form:"id"`
  31. })
  32. if err = c.Bind(v); err != nil {
  33. return
  34. }
  35. if v.ID == 0 {
  36. c.JSON(nil, ecode.RequestErr)
  37. return
  38. }
  39. if err = svc.DB.Debug().Model(&operation.Operation{}).First(info, v.ID).Error; err != nil {
  40. c.JSON(nil, err)
  41. return
  42. }
  43. if info.ID == 0 {
  44. c.JSON(nil, ecode.NothingFound)
  45. } else {
  46. layout := "2006-01-02 15:04:05"
  47. ftime, _ := time.Parse(time.RFC3339, info.Stime)
  48. info.Stime = ftime.Format(layout)
  49. ftime, _ = time.Parse(time.RFC3339, info.Etime)
  50. info.Etime = ftime.Format(layout)
  51. ftime, _ = time.Parse(time.RFC3339, info.Ctime)
  52. info.Ctime = ftime.Format(layout)
  53. ftime, _ = time.Parse(time.RFC3339, info.Mtime)
  54. info.Mtime = ftime.Format(layout)
  55. ftime, _ = time.Parse(time.RFC3339, info.Dtime)
  56. info.Dtime = ftime.Format(layout)
  57. c.JSON(info, nil)
  58. }
  59. }
  60. func listNotice(c *bm.Context) {
  61. var (
  62. err error
  63. ops []*operation.Operation
  64. total int
  65. )
  66. v := new(struct {
  67. Type int8 `form:"type"`
  68. Platform int8 `form:"platform"`
  69. Page int `form:"page" validate:"min=1"`
  70. PageSize int `form:"pagesize" validate:"min=1"`
  71. })
  72. if err = c.Bind(v); err != nil {
  73. return
  74. }
  75. if v.Page < 1 {
  76. v.Page = 1
  77. }
  78. if v.PageSize < 20 {
  79. v.PageSize = 20
  80. }
  81. if _, ok := typesMap[v.Type]; !ok {
  82. data := map[string]interface{}{
  83. "code": ecode.RequestErr,
  84. "message": "forbid request with wrong type enum value",
  85. }
  86. c.Render(http.StatusOK, render.MapJSON(data))
  87. return
  88. }
  89. if _, ok := platformMap[v.Platform]; !ok {
  90. data := map[string]interface{}{
  91. "code": ecode.RequestErr,
  92. "message": "forbid request with wrong platform enum value",
  93. }
  94. c.Render(http.StatusOK, render.MapJSON(data))
  95. return
  96. }
  97. now := time.Now().Format("2006-01-02 15:04:05")
  98. where := " dtime = '0000-00-00 00:00:00' AND type = 'play' AND platform = ?"
  99. if v.Platform == 100 {
  100. where = " dtime = '0000-00-00 00:00:00' AND type = 'play' "
  101. }
  102. if v.Type == 0 {
  103. where += " AND (stime < ? AND etime > ?) "
  104. } else if v.Type == 1 {
  105. where += " AND (stime > ? OR etime < ?) "
  106. } else if v.Type == 2 {
  107. where += " AND 1=1 "
  108. }
  109. if v.Type == 2 {
  110. if v.Platform == 100 {
  111. if err = svc.DB.Debug().Model(&operation.Operation{}).Order("rank ASC").Offset((v.Page-1)*v.PageSize).Limit(v.PageSize).Find(&ops, where).Error; err != nil {
  112. c.JSON(nil, err)
  113. return
  114. }
  115. svc.DB.Debug().Model(&operation.Operation{}).Where(where).Count(&total)
  116. } else {
  117. if err = svc.DB.Debug().Model(&operation.Operation{}).Order("rank ASC").Offset((v.Page-1)*v.PageSize).Limit(v.PageSize).Find(&ops, where, v.Platform).Error; err != nil {
  118. c.JSON(nil, err)
  119. return
  120. }
  121. svc.DB.Debug().Model(&operation.Operation{}).Where(where, v.Platform).Count(&total)
  122. }
  123. } else {
  124. if v.Platform == 100 {
  125. if err = svc.DB.Debug().Model(&operation.Operation{}).Order("rank ASC").Offset((v.Page-1)*v.PageSize).Limit(v.PageSize).Find(&ops, where, now, now).Error; err != nil {
  126. c.JSON(nil, err)
  127. return
  128. }
  129. svc.DB.Debug().Model(&operation.Operation{}).Where(where, now, now).Count(&total)
  130. } else {
  131. if err = svc.DB.Debug().Model(&operation.Operation{}).Order("rank ASC").Offset((v.Page-1)*v.PageSize).Limit(v.PageSize).Find(&ops, where, v.Platform, now, now).Error; err != nil {
  132. c.JSON(nil, err)
  133. return
  134. }
  135. svc.DB.Debug().Model(&operation.Operation{}).Where(where, v.Platform, now, now).Count(&total)
  136. }
  137. }
  138. var opsView []*operation.ViewOperation
  139. for _, v := range ops {
  140. var (
  141. status string
  142. timeNow = time.Now()
  143. stime, _ = time.Parse(time.RFC3339, v.Stime)
  144. etime, _ = time.Parse(time.RFC3339, v.Etime)
  145. )
  146. if time.Now().Before(etime) && timeNow.After(stime) {
  147. status = "显示"
  148. } else {
  149. status = "隐藏"
  150. }
  151. opsView = append(opsView, &operation.ViewOperation{
  152. ID: v.ID,
  153. Type: v.Type,
  154. Ads: v.Ads,
  155. Platform: v.Platform,
  156. Rank: v.Rank,
  157. Pic: v.Pic,
  158. Link: v.Link,
  159. Content: v.Content,
  160. Username: v.Username,
  161. Remark: v.Remark,
  162. Note: v.Note,
  163. AppPic: v.AppPic,
  164. Stime: v.Stime,
  165. Etime: v.Etime,
  166. Ctime: v.Ctime,
  167. Mtime: v.Mtime,
  168. Dtime: v.Dtime,
  169. Status: status,
  170. })
  171. }
  172. c.Render(http.StatusOK, render.MapJSON(map[string]interface{}{
  173. "code": 0,
  174. "message": "0",
  175. "data": opsView,
  176. "pager": map[string]int{
  177. "page": v.Page,
  178. "pagesize": v.PageSize,
  179. "total": total,
  180. },
  181. }))
  182. }
  183. func addNotice(c *bm.Context) {
  184. var (
  185. err error
  186. )
  187. v := new(struct {
  188. Ads int8 `form:"ads" `
  189. Rank int8 `form:"rank"`
  190. Pic string `form:"pic" validate:"required"`
  191. Link string `form:"link" validate:"required"`
  192. Content string `form:"content" validate:"required"`
  193. Username string `form:"username" validate:"required"`
  194. Remark string `form:"remark"`
  195. Note string `form:"note"`
  196. AppPic string `form:"app_pic" validate:"required"`
  197. Platform int8 `form:"platform"`
  198. Stime string `form:"stime" validate:"required"`
  199. Etime string `form:"etime" validate:"required"`
  200. })
  201. if err = c.Bind(v); err != nil {
  202. return
  203. }
  204. m := &operation.Operation{
  205. Type: "play",
  206. Ads: v.Ads,
  207. Rank: v.Rank,
  208. Pic: v.Pic,
  209. Link: v.Link,
  210. Content: v.Content,
  211. Username: v.Username,
  212. Remark: v.Remark,
  213. Note: v.Note,
  214. AppPic: v.AppPic,
  215. Platform: v.Platform,
  216. Ctime: time.Now().Format("2006-01-02 15:04:05"),
  217. Stime: v.Stime,
  218. Etime: v.Etime,
  219. }
  220. db := svc.DB.Debug().Model(&operation.Operation{}).Create(m)
  221. if err = db.Error; err != nil {
  222. log.Error("creativeSvc.Operation error(%v)", err)
  223. c.JSON(nil, ecode.RequestErr)
  224. return
  225. }
  226. c.JSON(map[string]interface{}{
  227. "id": db.Value.(*operation.Operation).ID,
  228. }, nil)
  229. }
  230. func upNotice(c *bm.Context) {
  231. var (
  232. op = &operation.Operation{}
  233. err error
  234. )
  235. v := new(struct {
  236. ID int64 `form:"id"`
  237. Ads int8 `form:"ads"`
  238. Platform int8 `form:"platform"`
  239. Rank int8 `form:"rank"`
  240. Pic string `form:"pic"`
  241. Link string `form:"link"`
  242. Content string `form:"content"`
  243. Username string `form:"username"`
  244. Remark string `form:"remark"`
  245. Note string `form:"note"`
  246. AppPic string `form:"app_pic"`
  247. Stime string `form:"stime"`
  248. Etime string `form:"etime"`
  249. })
  250. if err = c.Bind(v); err != nil {
  251. return
  252. }
  253. if v.ID == 0 {
  254. c.JSON(nil, ecode.RequestErr)
  255. return
  256. }
  257. if err = svc.DB.Debug().Model(&operation.Operation{}).Find(op, v.ID).Error; err != nil {
  258. c.JSON(nil, err)
  259. return
  260. }
  261. if err = svc.DB.Debug().Model(&operation.Operation{ID: v.ID}).Update(map[string]interface {
  262. }{
  263. "id": v.ID,
  264. "ads": v.Ads,
  265. "rank": v.Rank,
  266. "pic": v.Pic,
  267. "link": v.Link,
  268. "content": v.Content,
  269. "username": v.Username,
  270. "remark": v.Remark,
  271. "note": v.Note,
  272. "app_pic": v.AppPic,
  273. "platform": v.Platform,
  274. "mtime": time.Now().Format("2006-01-02 15:04:05"),
  275. "stime": v.Stime,
  276. "etime": v.Etime,
  277. }).Error; err != nil {
  278. log.Error("svc.save error(%v)", err)
  279. }
  280. c.JSON(nil, err)
  281. }
  282. func delNotice(c *bm.Context) {
  283. var (
  284. op = &operation.Operation{}
  285. err error
  286. )
  287. if err = c.Bind(op); err != nil {
  288. return
  289. }
  290. if op.ID == 0 {
  291. c.JSON(nil, ecode.RequestErr)
  292. return
  293. }
  294. if err = svc.DB.Debug().Model(&operation.Operation{}).Find(op, op.ID).Error; err != nil {
  295. c.JSON(nil, err)
  296. return
  297. }
  298. if err = svc.DB.Debug().Model(&operation.Operation{ID: op.ID}).Update("dtime", time.Now().Format("2006-01-02 15:04:05")).Error; err != nil {
  299. log.Error("svc.del Notice error(%v)", err)
  300. }
  301. c.JSON(nil, err)
  302. }