operCollectArc.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. func listCollectArcOper(c *bm.Context) {
  12. var (
  13. err error
  14. ops []*operation.Operation
  15. total int
  16. )
  17. v := new(struct {
  18. Type int8 `form:"type"`
  19. Platform int8 `form:"platform"`
  20. Pn int `form:"pn" validate:"min=1"`
  21. Ps int `form:"ps" validate:"min=1"`
  22. })
  23. if err = c.Bind(v); err != nil {
  24. return
  25. }
  26. if v.Pn < 1 {
  27. v.Pn = 1
  28. }
  29. if v.Ps < 20 {
  30. v.Ps = 20
  31. }
  32. if _, ok := typesMap[v.Type]; !ok {
  33. data := map[string]interface{}{
  34. "code": ecode.RequestErr,
  35. "message": "forbid request with wrong type enum value",
  36. }
  37. c.Render(http.StatusOK, render.MapJSON(data))
  38. return
  39. }
  40. if _, ok := platformMap[v.Platform]; !ok {
  41. data := map[string]interface{}{
  42. "code": ecode.RequestErr,
  43. "message": "forbid request with wrong platform enum value",
  44. }
  45. c.Render(http.StatusOK, render.MapJSON(data))
  46. return
  47. }
  48. now := time.Now().Format("2006-01-02 15:04:05")
  49. where := " dtime = '0000-00-00 00:00:00' AND type = 'collect_arc' AND platform = ?"
  50. if v.Platform == 100 {
  51. where = " dtime = '0000-00-00 00:00:00' AND type = 'collect_arc' "
  52. }
  53. if v.Type == 0 {
  54. where += " AND (stime < ? AND etime > ?) "
  55. } else if v.Type == 1 {
  56. where += " AND (stime > ? OR etime < ?) "
  57. } else if v.Type == 2 {
  58. where += " AND 1=1 "
  59. }
  60. if v.Type == 2 {
  61. if v.Platform == 100 {
  62. if err = svc.DB.Debug().Model(&operation.Operation{}).Order("rank ASC").Offset((v.Pn-1)*v.Ps).Limit(v.Ps).Find(&ops, where).Error; err != nil {
  63. c.JSON(nil, err)
  64. return
  65. }
  66. svc.DB.Debug().Model(&operation.Operation{}).Where(where).Count(&total)
  67. } else {
  68. if err = svc.DB.Debug().Model(&operation.Operation{}).Order("rank ASC").Offset((v.Pn-1)*v.Ps).Limit(v.Ps).Find(&ops, where, v.Platform).Error; err != nil {
  69. c.JSON(nil, err)
  70. return
  71. }
  72. svc.DB.Debug().Model(&operation.Operation{}).Where(where, v.Platform).Count(&total)
  73. }
  74. } else {
  75. if v.Platform == 100 {
  76. if err = svc.DB.Debug().Model(&operation.Operation{}).Order("rank ASC").Offset((v.Pn-1)*v.Ps).Limit(v.Ps).Find(&ops, where, now, now).Error; err != nil {
  77. c.JSON(nil, err)
  78. return
  79. }
  80. svc.DB.Debug().Model(&operation.Operation{}).Where(where, now, now).Count(&total)
  81. } else {
  82. if err = svc.DB.Debug().Model(&operation.Operation{}).Order("rank ASC").Offset((v.Pn-1)*v.Ps).Limit(v.Ps).Find(&ops, where, v.Platform, now, now).Error; err != nil {
  83. c.JSON(nil, err)
  84. return
  85. }
  86. svc.DB.Debug().Model(&operation.Operation{}).Where(where, v.Platform, now, now).Count(&total)
  87. }
  88. }
  89. var opsView []*operation.ViewOperation
  90. layout := "2006-01-02 15:04:05"
  91. for _, v := range ops {
  92. var (
  93. status string
  94. timeNow = time.Now()
  95. stime, _ = time.Parse(time.RFC3339, v.Stime)
  96. etime, _ = time.Parse(time.RFC3339, v.Etime)
  97. ctime, _ = time.Parse(time.RFC3339, v.Ctime)
  98. mtime, _ = time.Parse(time.RFC3339, v.Mtime)
  99. dtime, _ = time.Parse(time.RFC3339, v.Dtime)
  100. )
  101. if time.Now().Before(etime) && timeNow.After(stime) {
  102. status = "显示"
  103. } else {
  104. status = "隐藏"
  105. }
  106. opsView = append(opsView, &operation.ViewOperation{
  107. ID: v.ID,
  108. Type: v.Type,
  109. Ads: v.Ads,
  110. Platform: v.Platform,
  111. Rank: v.Rank,
  112. Pic: v.Pic,
  113. Link: v.Link,
  114. Content: v.Content,
  115. Username: v.Username,
  116. Remark: v.Remark,
  117. Note: v.Note,
  118. AppPic: v.AppPic,
  119. Stime: stime.Format(layout),
  120. Etime: etime.Format(layout),
  121. Ctime: ctime.Format(layout),
  122. Dtime: dtime.Format(layout),
  123. Mtime: mtime.Format(layout),
  124. Status: status,
  125. })
  126. }
  127. c.Render(http.StatusOK, render.MapJSON(map[string]interface{}{
  128. "code": 0,
  129. "message": "0",
  130. "data": opsView,
  131. "pager": map[string]int{
  132. "page": v.Pn,
  133. "pagesize": v.Ps,
  134. "total": total,
  135. },
  136. }))
  137. }
  138. func addCollectArcOper(c *bm.Context) {
  139. var (
  140. err error
  141. )
  142. username, _ := c.Get("username")
  143. uname, ok := username.(string)
  144. if !ok || len(uname) == 0 {
  145. c.JSON(nil, ecode.RequestErr)
  146. return
  147. }
  148. v := new(struct {
  149. Ads int8 `form:"ads" `
  150. Rank int8 `form:"rank"`
  151. Pic string `form:"pic" validate:"required"`
  152. Link string `form:"link" validate:"required"`
  153. Content string `form:"content" validate:"required"`
  154. Remark string `form:"remark"`
  155. Note string `form:"note"`
  156. AppPic string `form:"app_pic" validate:"required"`
  157. Platform int8 `form:"platform"`
  158. Stime string `form:"stime" validate:"required"`
  159. Etime string `form:"etime" validate:"required"`
  160. })
  161. if err = c.Bind(v); err != nil {
  162. return
  163. }
  164. m := &operation.Operation{
  165. Type: "collect_arc",
  166. Ads: v.Ads,
  167. Rank: v.Rank,
  168. Pic: v.Pic,
  169. Link: v.Link,
  170. Content: v.Content,
  171. Username: uname,
  172. Remark: v.Remark,
  173. Note: v.Note,
  174. AppPic: v.AppPic,
  175. Platform: v.Platform,
  176. Ctime: time.Now().Format("2006-01-02 15:04:05"),
  177. Stime: v.Stime,
  178. Etime: v.Etime,
  179. }
  180. db := svc.DB.Debug().Model(&operation.Operation{}).Create(m)
  181. if err = db.Error; err != nil {
  182. log.Error("creativeSvc.Operation error(%v)", err)
  183. c.JSON(nil, ecode.RequestErr)
  184. return
  185. }
  186. c.JSON(map[string]interface{}{
  187. "id": db.Value.(*operation.Operation).ID,
  188. }, nil)
  189. }