CommunityListService.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package services
  2. import (
  3. "errors"
  4. "think-go/models"
  5. "github.com/beego/beego/v2/client/orm"
  6. )
  7. type CommunityListService struct{}
  8. // CommunityInfo represents community list information
  9. type CommunityInfo struct {
  10. Id int `json:"id"`
  11. ParentId string `json:"parent_id"`
  12. UserId string `json:"user_id"`
  13. Username string `json:"username"`
  14. PromoteName string `json:"promote_name"`
  15. Sales string `json:"sales"`
  16. TeamSales string `json:"team_sales"`
  17. TodayTeamSales string `json:"today_team_sales"`
  18. UserBv string `json:"user_bv"`
  19. SubStatus string `json:"sub_status"`
  20. CTime int64 `json:"c_time"`
  21. IsYes int `json:"is_yes"`
  22. }
  23. // GetCommunityList retrieves community list information by id
  24. func (s *CommunityListService) GetCommunityList(id int) (*CommunityInfo, error) {
  25. if id <= 0 {
  26. return nil, errors.New("invalid id")
  27. }
  28. record, err := models.GetCommunityListById(id)
  29. if err != nil {
  30. if err == orm.ErrNoRows {
  31. return nil, errors.New("community list record not found")
  32. }
  33. return nil, err
  34. }
  35. return s.toCommunityInfo(record), nil
  36. }
  37. // GetCommunityListByUserId retrieves community list by user_id
  38. func (s *CommunityListService) GetCommunityListByUserId(userId string) (*CommunityInfo, error) {
  39. if userId == "" {
  40. return nil, errors.New("user_id is required")
  41. }
  42. o := orm.NewOrm()
  43. record := &models.CommunityList{UserId: userId}
  44. err := o.Read(record, "UserId")
  45. if err != nil {
  46. if err == orm.ErrNoRows {
  47. return nil, errors.New("community list record not found")
  48. }
  49. return nil, err
  50. }
  51. return s.toCommunityInfo(record), nil
  52. }
  53. // CreateCommunityList creates a new community list record
  54. func (s *CommunityListService) CreateCommunityList(info *CommunityInfo) error {
  55. if info.UserId == "" {
  56. return errors.New("user_id is required")
  57. }
  58. // Check if record already exists
  59. o := orm.NewOrm()
  60. record := &models.CommunityList{UserId: info.UserId}
  61. err := o.Read(record, "UserId")
  62. if err == nil {
  63. return errors.New("community list record already exists")
  64. }
  65. record = &models.CommunityList{
  66. ParentId: info.ParentId,
  67. UserId: info.UserId,
  68. Username: info.Username,
  69. PromoteName: info.PromoteName,
  70. Sales: info.Sales,
  71. TeamSales: info.TeamSales,
  72. TodayTeamSales: info.TodayTeamSales,
  73. UserBv: info.UserBv,
  74. SubStatus: info.SubStatus,
  75. CTime: info.CTime,
  76. IsYes: info.IsYes,
  77. }
  78. _, err = models.AddCommunityList(record)
  79. return err
  80. }
  81. // UpdateCommunityList updates an existing community list record
  82. func (s *CommunityListService) UpdateCommunityList(info *CommunityInfo) error {
  83. if info.Id <= 0 {
  84. return errors.New("invalid id")
  85. }
  86. record, err := models.GetCommunityListById(info.Id)
  87. if err != nil {
  88. if err == orm.ErrNoRows {
  89. return errors.New("community list record not found")
  90. }
  91. return err
  92. }
  93. // Update fields
  94. record.ParentId = info.ParentId
  95. record.UserId = info.UserId
  96. record.Username = info.Username
  97. record.PromoteName = info.PromoteName
  98. record.Sales = info.Sales
  99. record.TeamSales = info.TeamSales
  100. record.TodayTeamSales = info.TodayTeamSales
  101. record.UserBv = info.UserBv
  102. record.SubStatus = info.SubStatus
  103. record.CTime = info.CTime
  104. record.IsYes = info.IsYes
  105. return models.UpdateCommunityListById(record)
  106. }
  107. // GetAllCommunityLists retrieves all community list records with pagination
  108. func (s *CommunityListService) GetAllCommunityLists(offset, limit int64) ([]CommunityInfo, int64, error) {
  109. query := make(map[string]string)
  110. fields := []string{"Id", "ParentId", "UserId", "Username", "PromoteName", "Sales", "TeamSales", "TodayTeamSales", "UserBv", "SubStatus", "CTime", "IsYes"}
  111. sortby := []string{"Id"}
  112. order := []string{"desc"}
  113. ml, err := models.GetAllCommunityList(query, fields, sortby, order, offset, limit)
  114. if err != nil {
  115. return nil, 0, err
  116. }
  117. result := make([]CommunityInfo, 0, len(ml))
  118. for _, item := range ml {
  119. if record, ok := item.(models.CommunityList); ok {
  120. result = append(result, CommunityInfo{
  121. Id: record.CommunityId,
  122. ParentId: record.ParentId,
  123. UserId: record.UserId,
  124. Username: record.Username,
  125. PromoteName: record.PromoteName,
  126. Sales: record.Sales,
  127. TeamSales: record.TeamSales,
  128. TodayTeamSales: record.TodayTeamSales,
  129. UserBv: record.UserBv,
  130. SubStatus: record.SubStatus,
  131. CTime: record.CTime,
  132. IsYes: record.IsYes,
  133. })
  134. }
  135. }
  136. // Get total count
  137. o := orm.NewOrm()
  138. count, err := o.QueryTable(new(models.CommunityList)).Count()
  139. if err != nil {
  140. return result, 0, err
  141. }
  142. return result, count, nil
  143. }
  144. // DeleteCommunityList deletes a community list record by id
  145. func (s *CommunityListService) DeleteCommunityList(id int) error {
  146. if id <= 0 {
  147. return errors.New("invalid id")
  148. }
  149. record, err := models.GetCommunityListById(id)
  150. if err != nil {
  151. if err == orm.ErrNoRows {
  152. return errors.New("community list record not found")
  153. }
  154. return err
  155. }
  156. return models.DeleteCommunityList(record.CommunityId)
  157. }
  158. // toCommunityInfo converts model to service info
  159. func (s *CommunityListService) toCommunityInfo(record *models.CommunityList) *CommunityInfo {
  160. return &CommunityInfo{
  161. Id: record.CommunityId,
  162. ParentId: record.ParentId,
  163. UserId: record.UserId,
  164. Username: record.Username,
  165. PromoteName: record.PromoteName,
  166. Sales: record.Sales,
  167. TeamSales: record.TeamSales,
  168. TodayTeamSales: record.TodayTeamSales,
  169. UserBv: record.UserBv,
  170. SubStatus: record.SubStatus,
  171. CTime: record.CTime,
  172. IsYes: record.IsYes,
  173. }
  174. }