| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- package services
- import (
- "errors"
- "think-go/models"
- "github.com/beego/beego/v2/client/orm"
- )
- type CommunityListService struct{}
- // CommunityInfo represents community list information
- type CommunityInfo struct {
- Id int `json:"id"`
- ParentId string `json:"parent_id"`
- UserId string `json:"user_id"`
- Username string `json:"username"`
- PromoteName string `json:"promote_name"`
- Sales string `json:"sales"`
- TeamSales string `json:"team_sales"`
- TodayTeamSales string `json:"today_team_sales"`
- UserBv string `json:"user_bv"`
- SubStatus string `json:"sub_status"`
- CTime int64 `json:"c_time"`
- IsYes int `json:"is_yes"`
- }
- // GetCommunityList retrieves community list information by id
- func (s *CommunityListService) GetCommunityList(id int) (*CommunityInfo, error) {
- if id <= 0 {
- return nil, errors.New("invalid id")
- }
- record, err := models.GetCommunityListById(id)
- if err != nil {
- if err == orm.ErrNoRows {
- return nil, errors.New("community list record not found")
- }
- return nil, err
- }
- return s.toCommunityInfo(record), nil
- }
- // GetCommunityListByUserId retrieves community list by user_id
- func (s *CommunityListService) GetCommunityListByUserId(userId string) (*CommunityInfo, error) {
- if userId == "" {
- return nil, errors.New("user_id is required")
- }
- o := orm.NewOrm()
- record := &models.CommunityList{UserId: userId}
- err := o.Read(record, "UserId")
- if err != nil {
- if err == orm.ErrNoRows {
- return nil, errors.New("community list record not found")
- }
- return nil, err
- }
- return s.toCommunityInfo(record), nil
- }
- // CreateCommunityList creates a new community list record
- func (s *CommunityListService) CreateCommunityList(info *CommunityInfo) error {
- if info.UserId == "" {
- return errors.New("user_id is required")
- }
- // Check if record already exists
- o := orm.NewOrm()
- record := &models.CommunityList{UserId: info.UserId}
- err := o.Read(record, "UserId")
- if err == nil {
- return errors.New("community list record already exists")
- }
- record = &models.CommunityList{
- ParentId: info.ParentId,
- UserId: info.UserId,
- Username: info.Username,
- PromoteName: info.PromoteName,
- Sales: info.Sales,
- TeamSales: info.TeamSales,
- TodayTeamSales: info.TodayTeamSales,
- UserBv: info.UserBv,
- SubStatus: info.SubStatus,
- CTime: info.CTime,
- IsYes: info.IsYes,
- }
- _, err = models.AddCommunityList(record)
- return err
- }
- // UpdateCommunityList updates an existing community list record
- func (s *CommunityListService) UpdateCommunityList(info *CommunityInfo) error {
- if info.Id <= 0 {
- return errors.New("invalid id")
- }
- record, err := models.GetCommunityListById(info.Id)
- if err != nil {
- if err == orm.ErrNoRows {
- return errors.New("community list record not found")
- }
- return err
- }
- // Update fields
- record.ParentId = info.ParentId
- record.UserId = info.UserId
- record.Username = info.Username
- record.PromoteName = info.PromoteName
- record.Sales = info.Sales
- record.TeamSales = info.TeamSales
- record.TodayTeamSales = info.TodayTeamSales
- record.UserBv = info.UserBv
- record.SubStatus = info.SubStatus
- record.CTime = info.CTime
- record.IsYes = info.IsYes
- return models.UpdateCommunityListById(record)
- }
- // GetAllCommunityLists retrieves all community list records with pagination
- func (s *CommunityListService) GetAllCommunityLists(offset, limit int64) ([]CommunityInfo, int64, error) {
- query := make(map[string]string)
- fields := []string{"Id", "ParentId", "UserId", "Username", "PromoteName", "Sales", "TeamSales", "TodayTeamSales", "UserBv", "SubStatus", "CTime", "IsYes"}
- sortby := []string{"Id"}
- order := []string{"desc"}
- ml, err := models.GetAllCommunityList(query, fields, sortby, order, offset, limit)
- if err != nil {
- return nil, 0, err
- }
- result := make([]CommunityInfo, 0, len(ml))
- for _, item := range ml {
- if record, ok := item.(models.CommunityList); ok {
- result = append(result, CommunityInfo{
- Id: record.CommunityId,
- ParentId: record.ParentId,
- UserId: record.UserId,
- Username: record.Username,
- PromoteName: record.PromoteName,
- Sales: record.Sales,
- TeamSales: record.TeamSales,
- TodayTeamSales: record.TodayTeamSales,
- UserBv: record.UserBv,
- SubStatus: record.SubStatus,
- CTime: record.CTime,
- IsYes: record.IsYes,
- })
- }
- }
- // Get total count
- o := orm.NewOrm()
- count, err := o.QueryTable(new(models.CommunityList)).Count()
- if err != nil {
- return result, 0, err
- }
- return result, count, nil
- }
- // DeleteCommunityList deletes a community list record by id
- func (s *CommunityListService) DeleteCommunityList(id int) error {
- if id <= 0 {
- return errors.New("invalid id")
- }
- record, err := models.GetCommunityListById(id)
- if err != nil {
- if err == orm.ErrNoRows {
- return errors.New("community list record not found")
- }
- return err
- }
- return models.DeleteCommunityList(record.CommunityId)
- }
- // toCommunityInfo converts model to service info
- func (s *CommunityListService) toCommunityInfo(record *models.CommunityList) *CommunityInfo {
- return &CommunityInfo{
- Id: record.CommunityId,
- ParentId: record.ParentId,
- UserId: record.UserId,
- Username: record.Username,
- PromoteName: record.PromoteName,
- Sales: record.Sales,
- TeamSales: record.TeamSales,
- TodayTeamSales: record.TodayTeamSales,
- UserBv: record.UserBv,
- SubStatus: record.SubStatus,
- CTime: record.CTime,
- IsYes: record.IsYes,
- }
- }
|