| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- package services
- import (
- "bytes"
- "encoding/json"
- "errors"
- "fmt"
- "io/ioutil"
- "net/http"
- "net/url"
- "strconv"
- "think-go/models"
- "github.com/beego/beego/v2/client/orm"
- )
- type CommunityListService struct{}
- // CommunityInfo represents community list information
- type CommunityInfo struct {
- CommunityId int `json:"community_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
- }
- type ExternalCommunityResponse struct {
- Code int `json:"code"`
- Data struct {
- InviteAmount int `json:"invite_amount"`
- List []struct {
- CTime int64 `json:"c_time"`
- UserID string `json:"userid"`
- 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 int `json:"sub_status"`
- } `json:"list"`
- Count int `json:"count"`
- } `json:"data"`
- }
- // 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
- }
- func (s *CommunityListService) GoRequestNetCommunityInfo(userId string) {
- svc := &CommunityListService{}
- // 2. 准备外部接口的请求参数(x-www-form-urlencoded)
- data := url.Values{}
- data.Set("limit", strconv.Itoa(1000))
- data.Set("page", strconv.Itoa(1))
- data.Set("userid", userId)
- // 3. 构造 HTTP 请求
- externalURL := "https://app-api.aiceanglobal1.com/api/v1/reward/inviteList" // 替换为真实地址
- req, err := http.NewRequest("POST", externalURL, bytes.NewBufferString(data.Encode()))
- if err != nil {
- return
- }
- // 4. 设置 Headers
- req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
- // 假设 Authorization 是 Bearer token,从配置或环境变量获取
- req.Header.Set("Authorization", "Bearer 9409e48b4f3cfabc4d44b88db19516ca")
- // 外部接口需要的 userid header(可能和 body 中的 userid 不同,按需设置)
- req.Header.Set("userid", "04a0d628189644a1b918f14cde664610")
- // 5. 发送请求
- client := &http.Client{}
- resp, err := client.Do(req)
- if err != nil {
- fmt.Println("errrr:", err)
- return
- }
- defer resp.Body.Close()
- // 6. 读取响应体
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- return
- }
- // 7. 解析 JSON
- var externalResp ExternalCommunityResponse
- if err := json.Unmarshal(body, &externalResp); err != nil {
- return
- }
- // 遍历列表
- for _, item := range externalResp.Data.List {
- // 取 count
- info := &CommunityInfo{
- ParentId: userId,
- UserId: item.UserID,
- Username: item.Username,
- PromoteName: item.PromoteName,
- Sales: item.Sales,
- TeamSales: item.TeamSales,
- TodayTeamSales: item.TodayTeamSales,
- UserBv: item.UserBV,
- SubStatus: strconv.Itoa(item.SubStatus),
- CTime: item.CTime,
- IsYes: 0,
- }
- err := svc.CreateCommunityList(info)
- if err != nil {
- return
- }
- }
- }
- func (s *CommunityListService) GetCommunityListByIsYes() ([]CommunityInfo, error) {
- query := make(map[string]string)
- query["is_yes"] = strconv.Itoa(0)
- fields := []string{"CommunityId", "ParentId", "UserId", "Username", "PromoteName", "Sales", "TeamSales", "TodayTeamSales", "UserBv", "SubStatus", "CTime", "IsYes"}
- sortby := []string{"CommunityId"}
- order := []string{"desc"}
- ml, err := models.GetAllCommunityList(query, fields, sortby, order, 0, 100)
- if err != nil {
- return nil, err
- }
- //fmt.Printf("ml------:", ml)
- result := make([]CommunityInfo, 0, len(ml))
- for _, item := range ml {
- // 断言 item 是 map[string]interface{}
- m, ok := item.(map[string]interface{})
- if !ok {
- // 类型不匹配,跳过或记录错误
- continue
- }
- // 从 map 中安全提取字段(注意字段名与 JSON 标签一致)
- info := CommunityInfo{
- CommunityId: getInt(m, "CommunityId"), // 根据实际字段名调整
- ParentId: getString(m, "ParentId"),
- UserId: getString(m, "UserId"),
- Username: getString(m, "Username"),
- PromoteName: getString(m, "PromoteName"),
- Sales: getString(m, "Sales"),
- TeamSales: getString(m, "TeamSales"),
- TodayTeamSales: getString(m, "TodayTeamSales"),
- UserBv: getString(m, "UserBv"),
- SubStatus: getString(m, "SubStatus"),
- CTime: getInt64(m, "CTime"),
- IsYes: getInt(m, "IsYes"),
- }
- result = append(result, info)
- }
- return result, err
- }
- // 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.CommunityId <= 0 {
- return errors.New("invalid id")
- }
- record, err := models.GetCommunityListById(info.CommunityId)
- 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{
- CommunityId: 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{
- CommunityId: 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,
- }
- }
|