card_setup.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package channel
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strconv"
  6. "go-common/app/admin/main/feed/conf"
  7. accdao "go-common/app/admin/main/feed/dao/account"
  8. arcdao "go-common/app/admin/main/feed/dao/archive"
  9. pgcdao "go-common/app/admin/main/feed/dao/pgc"
  10. showdao "go-common/app/admin/main/feed/dao/show"
  11. cardmodel "go-common/app/admin/main/feed/model/channel"
  12. "go-common/app/admin/main/feed/model/common"
  13. "go-common/app/admin/main/feed/util"
  14. "go-common/library/log"
  15. )
  16. // Service is search service
  17. type Service struct {
  18. showDao *showdao.Dao
  19. pgcDao *pgcdao.Dao
  20. accDao *accdao.Dao
  21. arcDao *arcdao.Dao
  22. }
  23. // New new a search service
  24. func New(c *conf.Config) (s *Service) {
  25. var (
  26. pgc *pgcdao.Dao
  27. err error
  28. )
  29. if pgc, err = pgcdao.New(c); err != nil {
  30. log.Error("pgcdao.New error(%v)", err)
  31. return
  32. }
  33. s = &Service{
  34. showDao: showdao.New(c),
  35. pgcDao: pgc,
  36. accDao: accdao.New(c),
  37. arcDao: arcdao.New(c),
  38. }
  39. return
  40. }
  41. //parseConten parse string type id to int type id
  42. func parseConten(content string) (s string, err error) {
  43. type Content struct {
  44. ID string `json:"id"`
  45. Title string `json:"title"`
  46. }
  47. var contents []Content
  48. if err = json.Unmarshal([]byte(content), &contents); err != nil {
  49. return
  50. }
  51. type ContentTrans struct {
  52. ID int64 `json:"id"`
  53. Title string `json:"title"`
  54. }
  55. var cTrans []ContentTrans
  56. for _, v := range contents {
  57. var s int64
  58. if s, err = strconv.ParseInt(v.ID, 10, 64); err != nil {
  59. return "", err
  60. }
  61. v := ContentTrans{
  62. ID: s,
  63. Title: v.Title,
  64. }
  65. cTrans = append(cTrans, v)
  66. }
  67. var b []byte
  68. if b, err = json.Marshal(cTrans); err != nil {
  69. return "", err
  70. }
  71. return string(b), nil
  72. }
  73. //AddCardSetup card set up
  74. func (s *Service) AddCardSetup(card *cardmodel.AddCardSetup, person string, uid int64) (err error) {
  75. var (
  76. flag bool
  77. e error
  78. )
  79. flag, e = isDup(card.Content)
  80. if e != nil {
  81. return e
  82. }
  83. if flag {
  84. return fmt.Errorf("ID重复")
  85. }
  86. card.Person = person
  87. card.UID = uid
  88. if card.Content, err = parseConten(card.Content); err != nil {
  89. return
  90. }
  91. if err = s.showDao.DB.Model(&cardmodel.AddCardSetup{}).
  92. Create(card).Error; err != nil {
  93. log.Error("chanelSvc.AddCardSetup Create error(%v)", err)
  94. return
  95. }
  96. if card.Type == common.CardPgcsRcmd {
  97. if err = util.AddLog(cardmodel.LogBusPgcsRcmd, person, uid, 0, cardmodel.ActAddCsPgcRcmd, card); err != nil {
  98. log.Error("chanelSvc.UpdateCardSetup AddLog error(%v)", err)
  99. return
  100. }
  101. } else if card.Type == common.CardUpRcmdNew {
  102. if err = util.AddLog(cardmodel.LogBusRcmdNew, person, uid, 0, cardmodel.ActAddCsRcmdNew, card); err != nil {
  103. log.Error("chanelSvc.UpdateCardSetup AddLog error(%v)", err)
  104. return
  105. }
  106. }
  107. return
  108. }
  109. //CardSetupList card set up
  110. func (s *Service) CardSetupList(id int, t string, person string, title string, pn int, ps int) (cPager *cardmodel.SetupPager, err error) {
  111. cPager = &cardmodel.SetupPager{
  112. Page: common.Page{
  113. Num: pn,
  114. Size: ps,
  115. },
  116. }
  117. w := map[string]interface{}{
  118. "deleted": cardmodel.NotDelete,
  119. "type": t,
  120. }
  121. query := s.showDao.DB.Model(&cardmodel.Setup{})
  122. if id != 0 {
  123. w["id"] = id
  124. }
  125. if person != "" {
  126. query = query.Where("person like ?", "%"+person+"%")
  127. }
  128. if title != "" {
  129. if t == "up_rcmd_new" {
  130. query = query.Where("long_title like ?", "%"+title+"%")
  131. } else {
  132. query = query.Where("title like ?", "%"+title+"%")
  133. }
  134. }
  135. if err = query.Where(w).Count(&cPager.Page.Total).Error; err != nil {
  136. log.Error("chanelSvc.CardSetupList Index count error(%v)", err)
  137. return
  138. }
  139. cards := []*cardmodel.Setup{}
  140. if err = query.Where(w).Order("`id` DESC").Offset((pn - 1) * ps).Limit(ps).Find(&cards).Error; err != nil {
  141. log.Error("chanelSvc.CardSetupList First error(%v)", err)
  142. return
  143. }
  144. cPager.Item = cards
  145. return
  146. }
  147. //DelCardSetup card set up
  148. func (s *Service) DelCardSetup(id int, t string, person string, uid int64) (err error) {
  149. dbModel := s.showDao.DB.Model(&cardmodel.Setup{})
  150. dbModel = dbModel.Where("id = ?", id).Where("type = ?", t)
  151. if err = dbModel.Update("deleted", cardmodel.Delete).Error; err != nil {
  152. log.Error("chanelSvc.CardSetupList First error(%v)", err)
  153. return
  154. }
  155. if t == common.CardPgcsRcmd {
  156. if err = util.AddLog(cardmodel.LogBusPgcsRcmd, person, uid, int64(id), cardmodel.ActDelCsPgcRcmd, ""); err != nil {
  157. log.Error("chanelSvc.UpdateCardSetup AddLog error(%v)", err)
  158. return
  159. }
  160. } else if t == common.CardUpRcmdNew {
  161. if err = util.AddLog(cardmodel.LogBusRcmdNew, person, uid, int64(id), cardmodel.ActDelCsRcmdNew, ""); err != nil {
  162. log.Error("chanelSvc.UpdateCardSetup AddLog error(%v)", err)
  163. return
  164. }
  165. }
  166. return
  167. }
  168. func isDup(con string) (flag bool, err error) {
  169. type Content struct {
  170. ID string `json:"id"`
  171. }
  172. value := []Content{}
  173. if err := json.Unmarshal([]byte(con), &value); err != nil {
  174. return false, err
  175. }
  176. s := make(map[string]bool)
  177. for _, v := range value {
  178. if s[v.ID] {
  179. return true, nil
  180. }
  181. s[v.ID] = true
  182. }
  183. return false, nil
  184. }
  185. //UpdateCardSetup card set up
  186. func (s *Service) UpdateCardSetup(id int, card *cardmodel.AddCardSetup, person string, uid int64) (err error) {
  187. var (
  188. flag bool
  189. e error
  190. )
  191. flag, e = isDup(card.Content)
  192. if e != nil {
  193. return e
  194. }
  195. if flag {
  196. return fmt.Errorf("ID重复")
  197. }
  198. if card.Content, err = parseConten(card.Content); err != nil {
  199. return
  200. }
  201. dbModel := s.showDao.DB.Model(&cardmodel.Setup{})
  202. dbModel = dbModel.Where("id = ?", id).Where("type = ?", card.Type)
  203. if err = dbModel.Update(card).Error; err != nil {
  204. log.Error("chanelSvc.CardSetupList First error(%v)", err)
  205. return
  206. }
  207. if card.Type == common.CardPgcsRcmd {
  208. if err = util.AddLog(cardmodel.LogBusPgcsRcmd, person, uid, int64(id), cardmodel.ActUpCsPgcRcmd, card); err != nil {
  209. log.Error("chanelSvc.UpdateCardSetup AddLog error(%v)", err)
  210. return
  211. }
  212. } else if card.Type == common.CardUpRcmdNew {
  213. if err = util.AddLog(cardmodel.LogBusRcmdNew, person, uid, int64(id), cardmodel.ActUpCsRcmdNew, card); err != nil {
  214. log.Error("chanelSvc.UpdateCardSetup AddLog error(%v)", err)
  215. return
  216. }
  217. }
  218. return
  219. }