CommunityListService.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. package services
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "net/url"
  10. "strconv"
  11. "think-go/models"
  12. "github.com/beego/beego/v2/client/orm"
  13. )
  14. type CommunityListService struct{}
  15. // CommunityInfo represents community list information
  16. type CommunityInfo struct {
  17. CommunityId int `json:"community_id"`
  18. ParentId string `json:"parent_id"`
  19. UserId string `json:"user_id"`
  20. Username string `json:"username"`
  21. PromoteName string `json:"promote_name"`
  22. Sales string `json:"sales"`
  23. TeamSales string `json:"team_sales"`
  24. TodayTeamSales string `json:"today_team_sales"`
  25. UserBv string `json:"user_bv"`
  26. SubStatus string `json:"sub_status"`
  27. CTime int64 `json:"c_time"`
  28. IsYes int `json:"is_yes"`
  29. }
  30. // GetCommunityList retrieves community list information by id
  31. func (s *CommunityListService) GetCommunityList(id int) (*CommunityInfo, error) {
  32. if id <= 0 {
  33. return nil, errors.New("invalid id")
  34. }
  35. record, err := models.GetCommunityListById(id)
  36. if err != nil {
  37. if err == orm.ErrNoRows {
  38. return nil, errors.New("community list record not found")
  39. }
  40. return nil, err
  41. }
  42. return s.toCommunityInfo(record), nil
  43. }
  44. type ExternalCommunityResponse struct {
  45. Code int `json:"code"`
  46. Data struct {
  47. InviteAmount int `json:"invite_amount"`
  48. List []struct {
  49. CTime int64 `json:"c_time"`
  50. UserID string `json:"userid"`
  51. Username string `json:"username"`
  52. PromoteName string `json:"promote_name"`
  53. Sales string `json:"sales"`
  54. TeamSales string `json:"team_sales"`
  55. TodayTeamSales string `json:"today_team_sales"`
  56. UserBV string `json:"user_bv"`
  57. SubStatus int `json:"sub_status"`
  58. } `json:"list"`
  59. Count int `json:"count"`
  60. } `json:"data"`
  61. }
  62. // GetCommunityListByUserId retrieves community list by user_id
  63. func (s *CommunityListService) GetCommunityListByUserId(userId string) (*CommunityInfo, error) {
  64. if userId == "" {
  65. return nil, errors.New("user_id is required")
  66. }
  67. o := orm.NewOrm()
  68. record := &models.CommunityList{UserId: userId}
  69. err := o.Read(record, "UserId")
  70. if err != nil {
  71. if err == orm.ErrNoRows {
  72. return nil, errors.New("community list record not found")
  73. }
  74. return nil, err
  75. }
  76. return s.toCommunityInfo(record), nil
  77. }
  78. func (s *CommunityListService) GoRequestNetCommunityInfo(userId string) {
  79. svc := &CommunityListService{}
  80. // 2. 准备外部接口的请求参数(x-www-form-urlencoded)
  81. data := url.Values{}
  82. data.Set("limit", strconv.Itoa(1000))
  83. data.Set("page", strconv.Itoa(1))
  84. data.Set("userid", userId)
  85. // 3. 构造 HTTP 请求
  86. externalURL := "https://app-api.aiceanglobal1.com/api/v1/reward/inviteList" // 替换为真实地址
  87. req, err := http.NewRequest("POST", externalURL, bytes.NewBufferString(data.Encode()))
  88. if err != nil {
  89. return
  90. }
  91. // 4. 设置 Headers
  92. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  93. // 假设 Authorization 是 Bearer token,从配置或环境变量获取
  94. req.Header.Set("Authorization", "Bearer 9409e48b4f3cfabc4d44b88db19516ca")
  95. // 外部接口需要的 userid header(可能和 body 中的 userid 不同,按需设置)
  96. req.Header.Set("userid", "04a0d628189644a1b918f14cde664610")
  97. // 5. 发送请求
  98. client := &http.Client{}
  99. resp, err := client.Do(req)
  100. if err != nil {
  101. fmt.Println("errrr:", err)
  102. return
  103. }
  104. defer resp.Body.Close()
  105. // 6. 读取响应体
  106. body, err := ioutil.ReadAll(resp.Body)
  107. if err != nil {
  108. return
  109. }
  110. // 7. 解析 JSON
  111. var externalResp ExternalCommunityResponse
  112. if err := json.Unmarshal(body, &externalResp); err != nil {
  113. return
  114. }
  115. // 遍历列表
  116. for _, item := range externalResp.Data.List {
  117. // 取 count
  118. info := &CommunityInfo{
  119. ParentId: userId,
  120. UserId: item.UserID,
  121. Username: item.Username,
  122. PromoteName: item.PromoteName,
  123. Sales: item.Sales,
  124. TeamSales: item.TeamSales,
  125. TodayTeamSales: item.TodayTeamSales,
  126. UserBv: item.UserBV,
  127. SubStatus: strconv.Itoa(item.SubStatus),
  128. CTime: item.CTime,
  129. IsYes: 0,
  130. }
  131. err := svc.CreateCommunityList(info)
  132. if err != nil {
  133. return
  134. }
  135. }
  136. }
  137. func (s *CommunityListService) GetCommunityListByIsYes() ([]CommunityInfo, error) {
  138. query := make(map[string]string)
  139. query["is_yes"] = strconv.Itoa(0)
  140. fields := []string{"CommunityId", "ParentId", "UserId", "Username", "PromoteName", "Sales", "TeamSales", "TodayTeamSales", "UserBv", "SubStatus", "CTime", "IsYes"}
  141. sortby := []string{"CommunityId"}
  142. order := []string{"desc"}
  143. ml, err := models.GetAllCommunityList(query, fields, sortby, order, 0, 100)
  144. if err != nil {
  145. return nil, err
  146. }
  147. //fmt.Printf("ml------:", ml)
  148. result := make([]CommunityInfo, 0, len(ml))
  149. for _, item := range ml {
  150. // 断言 item 是 map[string]interface{}
  151. m, ok := item.(map[string]interface{})
  152. if !ok {
  153. // 类型不匹配,跳过或记录错误
  154. continue
  155. }
  156. // 从 map 中安全提取字段(注意字段名与 JSON 标签一致)
  157. info := CommunityInfo{
  158. CommunityId: getInt(m, "CommunityId"), // 根据实际字段名调整
  159. ParentId: getString(m, "ParentId"),
  160. UserId: getString(m, "UserId"),
  161. Username: getString(m, "Username"),
  162. PromoteName: getString(m, "PromoteName"),
  163. Sales: getString(m, "Sales"),
  164. TeamSales: getString(m, "TeamSales"),
  165. TodayTeamSales: getString(m, "TodayTeamSales"),
  166. UserBv: getString(m, "UserBv"),
  167. SubStatus: getString(m, "SubStatus"),
  168. CTime: getInt64(m, "CTime"),
  169. IsYes: getInt(m, "IsYes"),
  170. }
  171. result = append(result, info)
  172. }
  173. return result, err
  174. }
  175. // CreateCommunityList creates a new community list record
  176. func (s *CommunityListService) CreateCommunityList(info *CommunityInfo) error {
  177. if info.UserId == "" {
  178. return errors.New("user_id is required")
  179. }
  180. // Check if record already exists
  181. o := orm.NewOrm()
  182. record := &models.CommunityList{UserId: info.UserId}
  183. err := o.Read(record, "UserId")
  184. if err == nil {
  185. return errors.New("community list record already exists")
  186. }
  187. record = &models.CommunityList{
  188. ParentId: info.ParentId,
  189. UserId: info.UserId,
  190. Username: info.Username,
  191. PromoteName: info.PromoteName,
  192. Sales: info.Sales,
  193. TeamSales: info.TeamSales,
  194. TodayTeamSales: info.TodayTeamSales,
  195. UserBv: info.UserBv,
  196. SubStatus: info.SubStatus,
  197. CTime: info.CTime,
  198. IsYes: info.IsYes,
  199. }
  200. _, err = models.AddCommunityList(record)
  201. return err
  202. }
  203. // UpdateCommunityList updates an existing community list record
  204. func (s *CommunityListService) UpdateCommunityList(info *CommunityInfo) error {
  205. if info.CommunityId <= 0 {
  206. return errors.New("invalid id")
  207. }
  208. record, err := models.GetCommunityListById(info.CommunityId)
  209. if err != nil {
  210. if err == orm.ErrNoRows {
  211. return errors.New("community list record not found")
  212. }
  213. return err
  214. }
  215. // Update fields
  216. record.ParentId = info.ParentId
  217. record.UserId = info.UserId
  218. record.Username = info.Username
  219. record.PromoteName = info.PromoteName
  220. record.Sales = info.Sales
  221. record.TeamSales = info.TeamSales
  222. record.TodayTeamSales = info.TodayTeamSales
  223. record.UserBv = info.UserBv
  224. record.SubStatus = info.SubStatus
  225. record.CTime = info.CTime
  226. record.IsYes = info.IsYes
  227. return models.UpdateCommunityListById(record)
  228. }
  229. // GetAllCommunityLists retrieves all community list records with pagination
  230. func (s *CommunityListService) GetAllCommunityLists(offset, limit int64) ([]CommunityInfo, int64, error) {
  231. query := make(map[string]string)
  232. fields := []string{"Id", "ParentId", "UserId", "Username", "PromoteName", "Sales", "TeamSales", "TodayTeamSales", "UserBv", "SubStatus", "CTime", "IsYes"}
  233. sortby := []string{"Id"}
  234. order := []string{"desc"}
  235. ml, err := models.GetAllCommunityList(query, fields, sortby, order, offset, limit)
  236. if err != nil {
  237. return nil, 0, err
  238. }
  239. result := make([]CommunityInfo, 0, len(ml))
  240. for _, item := range ml {
  241. if record, ok := item.(models.CommunityList); ok {
  242. result = append(result, CommunityInfo{
  243. CommunityId: record.CommunityId,
  244. ParentId: record.ParentId,
  245. UserId: record.UserId,
  246. Username: record.Username,
  247. PromoteName: record.PromoteName,
  248. Sales: record.Sales,
  249. TeamSales: record.TeamSales,
  250. TodayTeamSales: record.TodayTeamSales,
  251. UserBv: record.UserBv,
  252. SubStatus: record.SubStatus,
  253. CTime: record.CTime,
  254. IsYes: record.IsYes,
  255. })
  256. }
  257. }
  258. // Get total count
  259. o := orm.NewOrm()
  260. count, err := o.QueryTable(new(models.CommunityList)).Count()
  261. if err != nil {
  262. return result, 0, err
  263. }
  264. return result, count, nil
  265. }
  266. // DeleteCommunityList deletes a community list record by id
  267. func (s *CommunityListService) DeleteCommunityList(id int) error {
  268. if id <= 0 {
  269. return errors.New("invalid id")
  270. }
  271. record, err := models.GetCommunityListById(id)
  272. if err != nil {
  273. if err == orm.ErrNoRows {
  274. return errors.New("community list record not found")
  275. }
  276. return err
  277. }
  278. return models.DeleteCommunityList(record.CommunityId)
  279. }
  280. // toCommunityInfo converts model to service info
  281. func (s *CommunityListService) toCommunityInfo(record *models.CommunityList) *CommunityInfo {
  282. return &CommunityInfo{
  283. CommunityId: record.CommunityId,
  284. ParentId: record.ParentId,
  285. UserId: record.UserId,
  286. Username: record.Username,
  287. PromoteName: record.PromoteName,
  288. Sales: record.Sales,
  289. TeamSales: record.TeamSales,
  290. TodayTeamSales: record.TodayTeamSales,
  291. UserBv: record.UserBv,
  292. SubStatus: record.SubStatus,
  293. CTime: record.CTime,
  294. IsYes: record.IsYes,
  295. }
  296. }