CommunityListController.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package api
  2. import (
  3. "strconv"
  4. "think-go/controllers/services"
  5. "think-go/utils"
  6. beego "github.com/beego/beego/v2/server/web"
  7. )
  8. type CommunityListController struct {
  9. beego.Controller
  10. }
  11. var communityListService = &services.CommunityListService{}
  12. // GetCommunityList 获取社区列表信息
  13. // @router /api/community/list [get]
  14. func (c *CommunityListController) GetCommunityList() {
  15. // 获取参数
  16. idStr := c.Ctx.Input.Query("id")
  17. userId, _ := utils.GetRequestString(&c.Controller, "user_id")
  18. if idStr != "" {
  19. // 根据 ID 查询
  20. id, err := strconv.Atoi(idStr)
  21. if err != nil {
  22. utils.JSON(&c.Controller, 400, "invalid id parameter", nil)
  23. return
  24. }
  25. record, err := communityListService.GetCommunityList(id)
  26. if err != nil {
  27. utils.JSON(&c.Controller, 500, err.Error(), nil)
  28. return
  29. }
  30. utils.JSON(&c.Controller, 200, "success", record)
  31. return
  32. }
  33. if userId != "" {
  34. // 根据用户 ID 查询
  35. record, err := communityListService.GetCommunityListByUserId(userId)
  36. if err != nil {
  37. utils.JSON(&c.Controller, 500, err.Error(), nil)
  38. return
  39. }
  40. utils.JSON(&c.Controller, 200, "success", record)
  41. return
  42. }
  43. utils.JSON(&c.Controller, 400, "please provide id or user_id parameter", nil)
  44. }
  45. // CreateCommunityList 创建社区列表记录
  46. // @router /api/community/create [post]
  47. func (c *CommunityListController) CreateCommunityList() {
  48. parentId, _ := utils.GetRequestString(&c.Controller, "parent_id")
  49. userId, _ := utils.GetRequestString(&c.Controller, "user_id")
  50. username, _ := utils.GetRequestString(&c.Controller, "username")
  51. promoteName, _ := utils.GetRequestString(&c.Controller, "promote_name")
  52. sales, _ := utils.GetRequestString(&c.Controller, "sales")
  53. teamSales, _ := utils.GetRequestString(&c.Controller, "team_sales")
  54. todayTeamSales, _ := utils.GetRequestString(&c.Controller, "today_team_sales")
  55. userBv, _ := utils.GetRequestString(&c.Controller, "user_bv")
  56. subStatus, _ := utils.GetRequestString(&c.Controller, "sub_status")
  57. cTimeStr := c.Ctx.Input.Query("c_time")
  58. cTime := int64(0)
  59. if cTimeStr != "" {
  60. if ct, err := strconv.ParseInt(cTimeStr, 10, 64); err == nil {
  61. cTime = ct
  62. }
  63. }
  64. isYesStr := c.Ctx.Input.Query("is_yes")
  65. isYes := 0
  66. if isYesStr != "" {
  67. if iy, err := strconv.Atoi(isYesStr); err == nil {
  68. isYes = iy
  69. }
  70. }
  71. info := &services.CommunityInfo{
  72. ParentId: parentId,
  73. UserId: userId,
  74. Username: username,
  75. PromoteName: promoteName,
  76. Sales: sales,
  77. TeamSales: teamSales,
  78. TodayTeamSales: todayTeamSales,
  79. UserBv: userBv,
  80. SubStatus: subStatus,
  81. CTime: cTime,
  82. IsYes: isYes,
  83. }
  84. err := communityListService.CreateCommunityList(info)
  85. if err != nil {
  86. utils.JSON(&c.Controller, 500, err.Error(), nil)
  87. return
  88. }
  89. utils.JSON(&c.Controller, 200, "create success", map[string]interface{}{
  90. "message": "community list record created successfully",
  91. })
  92. }
  93. // UpdateCommunityList 更新社区列表记录
  94. // @router /api/community/update [post]
  95. func (c *CommunityListController) UpdateCommunityList() {
  96. idStr, _ := utils.GetRequestString(&c.Controller, "id")
  97. id, err := strconv.Atoi(idStr)
  98. if err != nil || id <= 0 {
  99. utils.JSON(&c.Controller, 400, "invalid or missing id parameter", nil)
  100. return
  101. }
  102. parentId, _ := utils.GetRequestString(&c.Controller, "parent_id")
  103. userId, _ := utils.GetRequestString(&c.Controller, "user_id")
  104. username, _ := utils.GetRequestString(&c.Controller, "username")
  105. promoteName, _ := utils.GetRequestString(&c.Controller, "promote_name")
  106. sales, _ := utils.GetRequestString(&c.Controller, "sales")
  107. teamSales, _ := utils.GetRequestString(&c.Controller, "team_sales")
  108. todayTeamSales, _ := utils.GetRequestString(&c.Controller, "today_team_sales")
  109. userBv, _ := utils.GetRequestString(&c.Controller, "user_bv")
  110. subStatus, _ := utils.GetRequestString(&c.Controller, "sub_status")
  111. cTimeStr := c.Ctx.Input.Query("c_time")
  112. cTime := int64(0)
  113. if cTimeStr != "" {
  114. if ct, err := strconv.ParseInt(cTimeStr, 10, 64); err == nil {
  115. cTime = ct
  116. }
  117. }
  118. isYesStr := c.Ctx.Input.Query("is_yes")
  119. isYes := 0
  120. if isYesStr != "" {
  121. if iy, err := strconv.Atoi(isYesStr); err == nil {
  122. isYes = iy
  123. }
  124. }
  125. info := &services.CommunityInfo{
  126. Id: id,
  127. ParentId: parentId,
  128. UserId: userId,
  129. Username: username,
  130. PromoteName: promoteName,
  131. Sales: sales,
  132. TeamSales: teamSales,
  133. TodayTeamSales: todayTeamSales,
  134. UserBv: userBv,
  135. SubStatus: subStatus,
  136. CTime: cTime,
  137. IsYes: isYes,
  138. }
  139. err = communityListService.UpdateCommunityList(info)
  140. if err != nil {
  141. utils.JSON(&c.Controller, 500, err.Error(), nil)
  142. return
  143. }
  144. utils.JSON(&c.Controller, 200, "update success", map[string]interface{}{
  145. "message": "community list record updated successfully",
  146. })
  147. }
  148. // GetAllCommunityLists 获取所有社区列表记录(分页)
  149. // @router /api/community/listAll [get]
  150. func (c *CommunityListController) GetAllCommunityLists() {
  151. // 获取分页参数
  152. pageStr := c.Ctx.Input.Query("page")
  153. pageSizeStr := c.Ctx.Input.Query("pageSize")
  154. page := 1
  155. pageSize := 10
  156. if pageStr != "" {
  157. if p, err := strconv.Atoi(pageStr); err == nil && p > 0 {
  158. page = p
  159. }
  160. }
  161. if pageSizeStr != "" {
  162. if ps, err := strconv.Atoi(pageSizeStr); err == nil && ps > 0 {
  163. pageSize = ps
  164. }
  165. }
  166. offset := int64((page - 1) * pageSize)
  167. limit := int64(pageSize)
  168. records, total, err := communityListService.GetAllCommunityLists(offset, limit)
  169. if err != nil {
  170. utils.JSON(&c.Controller, 500, err.Error(), nil)
  171. return
  172. }
  173. utils.JSON(&c.Controller, 200, "success", map[string]interface{}{
  174. "list": records,
  175. "total": total,
  176. "page": page,
  177. "pageSize": pageSize,
  178. })
  179. }
  180. // DeleteCommunityList 删除社区列表记录
  181. // @router /api/community/delete [post]
  182. func (c *CommunityListController) DeleteCommunityList() {
  183. idStr, _ := utils.GetRequestString(&c.Controller, "id")
  184. id, err := strconv.Atoi(idStr)
  185. if err != nil || id <= 0 {
  186. utils.JSON(&c.Controller, 400, "invalid or missing id parameter", nil)
  187. return
  188. }
  189. err = communityListService.DeleteCommunityList(id)
  190. if err != nil {
  191. utils.JSON(&c.Controller, 500, err.Error(), nil)
  192. return
  193. }
  194. utils.JSON(&c.Controller, 200, "success", map[string]interface{}{
  195. "message": "community list record deleted successfully",
  196. })
  197. }