stars.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package popular
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/feed/model/common"
  6. "go-common/app/admin/main/feed/model/show"
  7. "go-common/app/admin/main/feed/util"
  8. "go-common/library/log"
  9. "github.com/jinzhu/gorm"
  10. )
  11. const (
  12. //ActionAddCPopular .
  13. ActionAddCPopular = "ActAddPopularStars"
  14. //ActionUpCPopular .
  15. ActionUpCPopular = "ActUpPopularStars"
  16. //ActionDelCPopular .
  17. ActionDelCPopular = "ActDelPopularStars"
  18. //ActionRejCPopular .
  19. ActionRejCPopular = "ActRejPopularStars"
  20. //_CardTypeUpRcmdNew 热门新星卡片
  21. _CardTypeUpRcmdNew = "up_rcmd_new"
  22. //ActionAIAddCPopular .
  23. ActionAIAddCPopular = "ActAIAddPopularStars"
  24. //_CardSourceOperate popular stars build by operate
  25. _CardSourceOperate = 0
  26. //_CardSourceAI popular stars build by ai
  27. _CardSourceAI = 1
  28. )
  29. //PopularStarsList channel Popular list
  30. func (s *Service) PopularStarsList(lp *show.PopularStarsLP) (pager *show.PopularStarsPager, err error) {
  31. pager = &show.PopularStarsPager{
  32. Page: common.Page{
  33. Num: lp.Pn,
  34. Size: lp.Ps,
  35. },
  36. }
  37. w := map[string]interface{}{
  38. "deleted": common.NotDeleted,
  39. "type": _CardTypeUpRcmdNew,
  40. }
  41. query := s.showDao.DB.Model(&show.PopularStars{})
  42. if lp.ID > 0 {
  43. w["id"] = lp.ID
  44. }
  45. if lp.Status > 0 {
  46. w["status"] = lp.Status
  47. }
  48. if lp.Source >= 0 {
  49. if lp.Source == 0 {
  50. query = query.Where(map[string]interface{}{"source": _CardSourceOperate})
  51. } else {
  52. w["source"] = lp.Source
  53. }
  54. }
  55. if lp.Person != "" {
  56. query = query.Where("person like ?", "%"+lp.Person+"%")
  57. }
  58. if lp.LongTitle != "" {
  59. query = query.Where("long_title like ?", "%"+lp.LongTitle+"%")
  60. }
  61. if err = query.Where(w).Count(&pager.Page.Total).Error; err != nil {
  62. log.Error("popularSvc.PopularStarsList Index count error(%v)", err)
  63. return
  64. }
  65. Populars := make([]*show.PopularStars, 0)
  66. if err = query.Where(w).Order("`id` DESC").Offset((lp.Pn - 1) * lp.Ps).Limit(lp.Ps).Find(&Populars).Error; err != nil {
  67. log.Error("popularSvc.PopularStarsList First error(%v)", err)
  68. return
  69. }
  70. pager.Item = Populars
  71. return
  72. }
  73. //AddPopularStars add popular stars
  74. func (s *Service) AddPopularStars(c context.Context, param *show.PopularStarsAP, name string, uid int64) (err error) {
  75. var (
  76. popStars *show.PopularStars
  77. )
  78. if popStars, err = s.ValidMid(param.Value); err != nil {
  79. fmt.Println("error")
  80. return
  81. }
  82. if popStars.ID != 0 {
  83. err = fmt.Errorf("up主ID 已存在")
  84. return
  85. }
  86. param.Type = _CardTypeUpRcmdNew
  87. param.Source = _CardSourceOperate
  88. param.Status = common.Pass
  89. if err = s.showDao.PopularStarsAdd(param); err != nil {
  90. return
  91. }
  92. if err = util.AddLogs(common.LogPopularStars, name, uid, 0, ActionAddCPopular, param); err != nil {
  93. log.Error("popularSvc.AddPopularStars AddLog error(%v)", err)
  94. return
  95. }
  96. return
  97. }
  98. //UpdatePopularStars update channel Popular
  99. func (s *Service) UpdatePopularStars(c context.Context, param *show.PopularStarsUP, name string, uid int64) (err error) {
  100. var (
  101. popStars *show.PopularStars
  102. )
  103. if popStars, err = s.ValidMid(param.Value); err != nil {
  104. return
  105. }
  106. if popStars.ID != 0 && popStars.ID != param.ID {
  107. err = fmt.Errorf("up主ID 已存在")
  108. return
  109. }
  110. param.Type = _CardTypeUpRcmdNew
  111. param.Status = common.Pass
  112. if err = s.showDao.PopularStarsUpdate(param); err != nil {
  113. return
  114. }
  115. if err = util.AddLogs(common.LogPopularStars, name, uid, 0, ActionUpCPopular, param); err != nil {
  116. log.Error("popularSvc.UpdatePopularStars AddLog error(%v)", err)
  117. return
  118. }
  119. return
  120. }
  121. //DeletePopularStars delete channel Popular
  122. func (s *Service) DeletePopularStars(id int64, name string, uid int64) (err error) {
  123. if err = s.showDao.PopularStarsDelete(id, _CardTypeUpRcmdNew); err != nil {
  124. return
  125. }
  126. if err = util.AddLogs(common.LogPopularStars, name, uid, id, ActionDelCPopular, id); err != nil {
  127. log.Error("popularSvc.DeletePopularStars AddLog error(%v)", err)
  128. return
  129. }
  130. return
  131. }
  132. //RejectPopularStars reject channel Popular
  133. func (s *Service) RejectPopularStars(id int64, name string, uid int64) (err error) {
  134. if err = s.showDao.PopularStarsReject(id, _CardTypeUpRcmdNew); err != nil {
  135. return
  136. }
  137. if err = util.AddLogs(common.LogPopularStars, name, uid, id, ActionRejCPopular, id); err != nil {
  138. log.Error("popularSvc.DeletePopularStars AddLog error(%v)", err)
  139. return
  140. }
  141. return
  142. }
  143. //ValidMid mid must unique
  144. func (s *Service) ValidMid(mid string) (popStars *show.PopularStars, err error) {
  145. w := map[string]interface{}{
  146. "value": mid,
  147. "deleted": common.NotDeleted,
  148. "type": _CardTypeUpRcmdNew,
  149. }
  150. popStars = &show.PopularStars{}
  151. if err = s.showDao.DB.Model(&show.PopularStars{}).Where(w).Find(&popStars).Error; err != nil {
  152. if err == gorm.ErrRecordNotFound {
  153. err = nil
  154. return
  155. }
  156. log.Error("popularSvc.ValidMid Find error(%v)", err)
  157. return
  158. }
  159. return
  160. }
  161. //AIAddPopularStars add popular stars
  162. func (s *Service) AIAddPopularStars(c context.Context, values []*show.PopularStarsAP) (err error) {
  163. if err = util.AddLogs(common.LogPopularStars, "AI", 0, 0, ActionAIAddCPopular, values); err != nil {
  164. log.Error("popularSvc.AIAddPopularStars AddLog error(%v)", err)
  165. return
  166. }
  167. for _, v := range values {
  168. var popStars *show.PopularStars
  169. if popStars, err = s.ValidMid(v.Value); err != nil {
  170. log.Error("popularSvc.AIAddPopularStars ValidMid value(%v) error(%v)", v, err)
  171. continue
  172. }
  173. if popStars.ID != 0 {
  174. //运营创建的优先级最高
  175. if popStars.Source == _CardSourceOperate {
  176. continue
  177. }
  178. //已通过的优先级较高
  179. if popStars.Status == common.Pass {
  180. continue
  181. }
  182. tmp := &show.PopularStarsUP{
  183. ID: popStars.ID,
  184. Content: v.Content,
  185. LongTitle: v.LongTitle,
  186. }
  187. if err = s.showDao.PopularStarsUpdate(tmp); err != nil {
  188. log.Error("popularSvc.AIAddPopularStars PopularStarsUpdate value(%v) error(%v)", tmp, err)
  189. continue
  190. }
  191. } else {
  192. tmp := &show.PopularStarsAP{
  193. Type: _CardTypeUpRcmdNew,
  194. Source: _CardSourceAI,
  195. Status: common.Verify,
  196. Value: v.Value,
  197. Content: v.Content,
  198. LongTitle: v.LongTitle,
  199. }
  200. if err = s.showDao.PopularStarsAdd(tmp); err != nil {
  201. log.Error("popularSvc.AIAddPopularStars PopularStarsAdd value(%v) error(%v)", tmp, err)
  202. continue
  203. }
  204. }
  205. }
  206. return
  207. }