card.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/admin/main/card/model"
  5. "go-common/library/ecode"
  6. "go-common/library/log"
  7. "go-common/library/sync/errgroup"
  8. )
  9. // CardsByGid get cards by gid.
  10. func (s *Service) CardsByGid(c context.Context, gid int64) ([]*model.Card, error) {
  11. return s.dao.CardsByGid(c, gid)
  12. }
  13. // UpdateCardState update card state.
  14. func (s *Service) UpdateCardState(c context.Context, req *model.ArgState) error {
  15. return s.dao.UpdateCardState(c, req.ID, req.State)
  16. }
  17. // DeleteCard delete card.
  18. func (s *Service) DeleteCard(c context.Context, id int64) error {
  19. return s.dao.DeleteCard(c, id)
  20. }
  21. // UpdateGroupState update group state.
  22. func (s *Service) UpdateGroupState(c context.Context, req *model.ArgState) error {
  23. return s.dao.UpdateGroupState(c, req.ID, req.State)
  24. }
  25. // DeleteGroup delete group.
  26. func (s *Service) DeleteGroup(c context.Context, id int64) error {
  27. return s.dao.DeleteGroup(c, id)
  28. }
  29. // GroupList group list.
  30. func (s *Service) GroupList(c context.Context, req *model.ArgQueryGroup) (res []*model.CardGroup, err error) {
  31. if res, err = s.dao.Groups(c, req); err != nil {
  32. return
  33. }
  34. if len(res) <= 0 {
  35. return
  36. }
  37. var cs []*model.Card
  38. if cs, err = s.dao.Cards(c); err != nil {
  39. return
  40. }
  41. tmp := make(map[int64][]*model.Card, len(res))
  42. for _, v := range cs {
  43. if len(tmp[v.GroupID]) <= 0 {
  44. tmp[v.GroupID] = []*model.Card{}
  45. }
  46. tmp[v.GroupID] = append(tmp[v.GroupID], v)
  47. }
  48. for _, v := range res {
  49. v.Cards = tmp[v.ID]
  50. }
  51. return
  52. }
  53. // CardOrderChange card order change.
  54. func (s *Service) CardOrderChange(c context.Context, req *model.ArgIds) (err error) {
  55. var cs []*model.Card
  56. if cs, err = s.dao.CardsByIds(c, req.Ids); err != nil {
  57. return
  58. }
  59. if len(req.Ids) != len(cs) {
  60. err = ecode.CardIDNotFoundErr
  61. return
  62. }
  63. orders := make(map[int]*model.Card, len(cs))
  64. for i, v := range cs {
  65. orders[i] = v
  66. }
  67. us := []*model.Card{}
  68. for i, v := range req.Ids {
  69. if orders[i].ID != v {
  70. us = append(us, &model.Card{ID: v, OrderNum: orders[i].OrderNum})
  71. }
  72. }
  73. if len(us) > 0 {
  74. err = s.dao.BatchUpdateCardOrder(c, us)
  75. }
  76. return
  77. }
  78. // GroupOrderChange group order change.
  79. func (s *Service) GroupOrderChange(c context.Context, req *model.ArgIds) (err error) {
  80. var cs []*model.CardGroup
  81. if cs, err = s.dao.GroupsByIds(c, req.Ids); err != nil {
  82. return
  83. }
  84. if len(req.Ids) != len(cs) {
  85. err = ecode.CardIDNotFoundErr
  86. return
  87. }
  88. orders := make(map[int]*model.CardGroup, len(cs))
  89. for i, v := range cs {
  90. orders[i] = v
  91. }
  92. us := []*model.CardGroup{}
  93. for i, v := range req.Ids {
  94. if orders[i].ID != v {
  95. us = append(us, &model.CardGroup{ID: v, OrderNum: orders[i].OrderNum})
  96. }
  97. }
  98. if len(us) > 0 {
  99. err = s.dao.BatchUpdateCardGroupOrder(c, us)
  100. }
  101. return
  102. }
  103. // AddCard add card.
  104. func (s *Service) AddCard(c context.Context, req *model.AddCard) (err error) {
  105. var exist *model.Card
  106. if exist, err = s.dao.CardByName(req.Name); err != nil || exist != nil {
  107. return ecode.CardNameExistErr
  108. }
  109. var g errgroup.Group
  110. g.Go(func() (err error) {
  111. if req.CardURL, err = s.dao.Upload(c, "", req.CardFileType, req.CardBody, s.c.Bfs); err != nil {
  112. log.Error("d.Upload iconURL(%+v) error(%v)", req, err)
  113. }
  114. return
  115. })
  116. g.Go(func() (err error) {
  117. if req.BigCradURL, err = s.dao.Upload(c, "", req.BigCardFileType, req.BigCardBody, s.c.Bfs); err != nil {
  118. log.Error("d.Upload bigCardURL(%+v) error(%v)", req, err)
  119. }
  120. return
  121. })
  122. if err = g.Wait(); err != nil {
  123. return
  124. }
  125. if req.CardURL == "" || req.BigCradURL == "" {
  126. err = ecode.CardFileUploadFaildErr
  127. return
  128. }
  129. var order int64
  130. if order, err = s.dao.MaxCardOrder(); err != nil {
  131. return
  132. }
  133. order++
  134. req.OrderNum = order
  135. err = s.dao.AddCard(req)
  136. return
  137. }
  138. // UpdateCard update card.
  139. func (s *Service) UpdateCard(c context.Context, req *model.UpdateCard) (err error) {
  140. var g errgroup.Group
  141. g.Go(func() (err error) {
  142. if req.CardFileType != "" {
  143. if req.CardURL, err = s.dao.Upload(c, "", req.CardFileType, req.CardBody, s.c.Bfs); err != nil {
  144. log.Error("d.Upload iconURL(%+v) error(%v)", req, err)
  145. }
  146. }
  147. return
  148. })
  149. g.Go(func() (err error) {
  150. if req.BigCardFileType != "" {
  151. if req.BigCradURL, err = s.dao.Upload(c, "", req.BigCardFileType, req.BigCardBody, s.c.Bfs); err != nil {
  152. log.Error("d.Upload bigCardURL(%+v) error(%v)", req, err)
  153. }
  154. }
  155. return
  156. })
  157. if err = g.Wait(); err != nil {
  158. return
  159. }
  160. err = s.dao.UpdateCard(req)
  161. return
  162. }
  163. // AddGroup add group.
  164. func (s *Service) AddGroup(c context.Context, req *model.AddGroup) (err error) {
  165. var exist *model.CardGroup
  166. if exist, err = s.dao.GroupByName(req.Name); err != nil {
  167. log.Error("s.dao.GroupByName(%+v) error(%v)", req, err)
  168. return
  169. }
  170. if exist != nil {
  171. return ecode.CardGroupNameExistErr
  172. }
  173. var order int64
  174. if order, err = s.dao.MaxGroupOrder(); err != nil {
  175. return
  176. }
  177. order++
  178. req.OrderNum = order
  179. return s.dao.AddGroup(c, req)
  180. }
  181. // UpdateGroup update group.
  182. func (s *Service) UpdateGroup(c context.Context, req *model.UpdateGroup) error {
  183. return s.dao.UpdateGroup(c, req)
  184. }