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, } }