|
@@ -1,7 +1,14 @@
|
|
|
package services
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
|
+ "bytes"
|
|
|
|
|
+ "encoding/json"
|
|
|
"errors"
|
|
"errors"
|
|
|
|
|
+ "fmt"
|
|
|
|
|
+ "io/ioutil"
|
|
|
|
|
+ "net/http"
|
|
|
|
|
+ "net/url"
|
|
|
|
|
+ "strconv"
|
|
|
"think-go/models"
|
|
"think-go/models"
|
|
|
|
|
|
|
|
"github.com/beego/beego/v2/client/orm"
|
|
"github.com/beego/beego/v2/client/orm"
|
|
@@ -11,7 +18,7 @@ type CommunityListService struct{}
|
|
|
|
|
|
|
|
// CommunityInfo represents community list information
|
|
// CommunityInfo represents community list information
|
|
|
type CommunityInfo struct {
|
|
type CommunityInfo struct {
|
|
|
- Id int `json:"id"`
|
|
|
|
|
|
|
+ CommunityId int `json:"community_id"`
|
|
|
ParentId string `json:"parent_id"`
|
|
ParentId string `json:"parent_id"`
|
|
|
UserId string `json:"user_id"`
|
|
UserId string `json:"user_id"`
|
|
|
Username string `json:"username"`
|
|
Username string `json:"username"`
|
|
@@ -42,6 +49,25 @@ func (s *CommunityListService) GetCommunityList(id int) (*CommunityInfo, error)
|
|
|
return s.toCommunityInfo(record), nil
|
|
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
|
|
// GetCommunityListByUserId retrieves community list by user_id
|
|
|
func (s *CommunityListService) GetCommunityListByUserId(userId string) (*CommunityInfo, error) {
|
|
func (s *CommunityListService) GetCommunityListByUserId(userId string) (*CommunityInfo, error) {
|
|
|
if userId == "" {
|
|
if userId == "" {
|
|
@@ -60,6 +86,121 @@ func (s *CommunityListService) GetCommunityListByUserId(userId string) (*Communi
|
|
|
|
|
|
|
|
return s.toCommunityInfo(record), nil
|
|
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
|
|
// CreateCommunityList creates a new community list record
|
|
|
func (s *CommunityListService) CreateCommunityList(info *CommunityInfo) error {
|
|
func (s *CommunityListService) CreateCommunityList(info *CommunityInfo) error {
|
|
@@ -95,11 +236,11 @@ func (s *CommunityListService) CreateCommunityList(info *CommunityInfo) error {
|
|
|
|
|
|
|
|
// UpdateCommunityList updates an existing community list record
|
|
// UpdateCommunityList updates an existing community list record
|
|
|
func (s *CommunityListService) UpdateCommunityList(info *CommunityInfo) error {
|
|
func (s *CommunityListService) UpdateCommunityList(info *CommunityInfo) error {
|
|
|
- if info.Id <= 0 {
|
|
|
|
|
|
|
+ if info.CommunityId <= 0 {
|
|
|
return errors.New("invalid id")
|
|
return errors.New("invalid id")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- record, err := models.GetCommunityListById(info.Id)
|
|
|
|
|
|
|
+ record, err := models.GetCommunityListById(info.CommunityId)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
if err == orm.ErrNoRows {
|
|
if err == orm.ErrNoRows {
|
|
|
return errors.New("community list record not found")
|
|
return errors.New("community list record not found")
|
|
@@ -139,7 +280,7 @@ func (s *CommunityListService) GetAllCommunityLists(offset, limit int64) ([]Comm
|
|
|
for _, item := range ml {
|
|
for _, item := range ml {
|
|
|
if record, ok := item.(models.CommunityList); ok {
|
|
if record, ok := item.(models.CommunityList); ok {
|
|
|
result = append(result, CommunityInfo{
|
|
result = append(result, CommunityInfo{
|
|
|
- Id: record.CommunityId,
|
|
|
|
|
|
|
+ CommunityId: record.CommunityId,
|
|
|
ParentId: record.ParentId,
|
|
ParentId: record.ParentId,
|
|
|
UserId: record.UserId,
|
|
UserId: record.UserId,
|
|
|
Username: record.Username,
|
|
Username: record.Username,
|
|
@@ -185,7 +326,7 @@ func (s *CommunityListService) DeleteCommunityList(id int) error {
|
|
|
// toCommunityInfo converts model to service info
|
|
// toCommunityInfo converts model to service info
|
|
|
func (s *CommunityListService) toCommunityInfo(record *models.CommunityList) *CommunityInfo {
|
|
func (s *CommunityListService) toCommunityInfo(record *models.CommunityList) *CommunityInfo {
|
|
|
return &CommunityInfo{
|
|
return &CommunityInfo{
|
|
|
- Id: record.CommunityId,
|
|
|
|
|
|
|
+ CommunityId: record.CommunityId,
|
|
|
ParentId: record.ParentId,
|
|
ParentId: record.ParentId,
|
|
|
UserId: record.UserId,
|
|
UserId: record.UserId,
|
|
|
Username: record.Username,
|
|
Username: record.Username,
|