| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- package api
- import (
- "strconv"
- "think-go/controllers/services"
- "think-go/utils"
- beego "github.com/beego/beego/v2/server/web"
- )
- type CommunityListController struct {
- beego.Controller
- }
- var communityListService = &services.CommunityListService{}
- // GetCommunityList 获取社区列表信息
- // @router /api/community/list [get]
- func (c *CommunityListController) GetCommunityList() {
- // 获取参数
- idStr := c.Ctx.Input.Query("id")
- userId, _ := utils.GetRequestString(&c.Controller, "user_id")
- if idStr != "" {
- // 根据 ID 查询
- id, err := strconv.Atoi(idStr)
- if err != nil {
- utils.JSON(&c.Controller, 400, "invalid id parameter", nil)
- return
- }
- record, err := communityListService.GetCommunityList(id)
- if err != nil {
- utils.JSON(&c.Controller, 500, err.Error(), nil)
- return
- }
- utils.JSON(&c.Controller, 200, "success", record)
- return
- }
- if userId != "" {
- // 根据用户 ID 查询
- record, err := communityListService.GetCommunityListByUserId(userId)
- if err != nil {
- utils.JSON(&c.Controller, 500, err.Error(), nil)
- return
- }
- utils.JSON(&c.Controller, 200, "success", record)
- return
- }
- utils.JSON(&c.Controller, 400, "please provide id or user_id parameter", nil)
- }
- // CreateCommunityList 创建社区列表记录
- // @router /api/community/create [post]
- func (c *CommunityListController) CreateCommunityList() {
- parentId, _ := utils.GetRequestString(&c.Controller, "parent_id")
- userId, _ := utils.GetRequestString(&c.Controller, "user_id")
- username, _ := utils.GetRequestString(&c.Controller, "username")
- promoteName, _ := utils.GetRequestString(&c.Controller, "promote_name")
- sales, _ := utils.GetRequestString(&c.Controller, "sales")
- teamSales, _ := utils.GetRequestString(&c.Controller, "team_sales")
- todayTeamSales, _ := utils.GetRequestString(&c.Controller, "today_team_sales")
- userBv, _ := utils.GetRequestString(&c.Controller, "user_bv")
- subStatus, _ := utils.GetRequestString(&c.Controller, "sub_status")
- cTimeStr := c.Ctx.Input.Query("c_time")
- cTime := int64(0)
- if cTimeStr != "" {
- if ct, err := strconv.ParseInt(cTimeStr, 10, 64); err == nil {
- cTime = ct
- }
- }
- isYesStr := c.Ctx.Input.Query("is_yes")
- isYes := 0
- if isYesStr != "" {
- if iy, err := strconv.Atoi(isYesStr); err == nil {
- isYes = iy
- }
- }
- info := &services.CommunityInfo{
- ParentId: parentId,
- UserId: userId,
- Username: username,
- PromoteName: promoteName,
- Sales: sales,
- TeamSales: teamSales,
- TodayTeamSales: todayTeamSales,
- UserBv: userBv,
- SubStatus: subStatus,
- CTime: cTime,
- IsYes: isYes,
- }
- err := communityListService.CreateCommunityList(info)
- if err != nil {
- utils.JSON(&c.Controller, 500, err.Error(), nil)
- return
- }
- utils.JSON(&c.Controller, 200, "create success", map[string]interface{}{
- "message": "community list record created successfully",
- })
- }
- // UpdateCommunityList 更新社区列表记录
- // @router /api/community/update [post]
- func (c *CommunityListController) UpdateCommunityList() {
- idStr, _ := utils.GetRequestString(&c.Controller, "id")
- id, err := strconv.Atoi(idStr)
- if err != nil || id <= 0 {
- utils.JSON(&c.Controller, 400, "invalid or missing id parameter", nil)
- return
- }
- parentId, _ := utils.GetRequestString(&c.Controller, "parent_id")
- userId, _ := utils.GetRequestString(&c.Controller, "user_id")
- username, _ := utils.GetRequestString(&c.Controller, "username")
- promoteName, _ := utils.GetRequestString(&c.Controller, "promote_name")
- sales, _ := utils.GetRequestString(&c.Controller, "sales")
- teamSales, _ := utils.GetRequestString(&c.Controller, "team_sales")
- todayTeamSales, _ := utils.GetRequestString(&c.Controller, "today_team_sales")
- userBv, _ := utils.GetRequestString(&c.Controller, "user_bv")
- subStatus, _ := utils.GetRequestString(&c.Controller, "sub_status")
- cTimeStr := c.Ctx.Input.Query("c_time")
- cTime := int64(0)
- if cTimeStr != "" {
- if ct, err := strconv.ParseInt(cTimeStr, 10, 64); err == nil {
- cTime = ct
- }
- }
- isYesStr := c.Ctx.Input.Query("is_yes")
- isYes := 0
- if isYesStr != "" {
- if iy, err := strconv.Atoi(isYesStr); err == nil {
- isYes = iy
- }
- }
- info := &services.CommunityInfo{
- Id: id,
- ParentId: parentId,
- UserId: userId,
- Username: username,
- PromoteName: promoteName,
- Sales: sales,
- TeamSales: teamSales,
- TodayTeamSales: todayTeamSales,
- UserBv: userBv,
- SubStatus: subStatus,
- CTime: cTime,
- IsYes: isYes,
- }
- err = communityListService.UpdateCommunityList(info)
- if err != nil {
- utils.JSON(&c.Controller, 500, err.Error(), nil)
- return
- }
- utils.JSON(&c.Controller, 200, "update success", map[string]interface{}{
- "message": "community list record updated successfully",
- })
- }
- // GetAllCommunityLists 获取所有社区列表记录(分页)
- // @router /api/community/listAll [get]
- func (c *CommunityListController) GetAllCommunityLists() {
- // 获取分页参数
- pageStr := c.Ctx.Input.Query("page")
- pageSizeStr := c.Ctx.Input.Query("pageSize")
- page := 1
- pageSize := 10
- if pageStr != "" {
- if p, err := strconv.Atoi(pageStr); err == nil && p > 0 {
- page = p
- }
- }
- if pageSizeStr != "" {
- if ps, err := strconv.Atoi(pageSizeStr); err == nil && ps > 0 {
- pageSize = ps
- }
- }
- offset := int64((page - 1) * pageSize)
- limit := int64(pageSize)
- records, total, err := communityListService.GetAllCommunityLists(offset, limit)
- if err != nil {
- utils.JSON(&c.Controller, 500, err.Error(), nil)
- return
- }
- utils.JSON(&c.Controller, 200, "success", map[string]interface{}{
- "list": records,
- "total": total,
- "page": page,
- "pageSize": pageSize,
- })
- }
- // DeleteCommunityList 删除社区列表记录
- // @router /api/community/delete [post]
- func (c *CommunityListController) DeleteCommunityList() {
- idStr, _ := utils.GetRequestString(&c.Controller, "id")
- id, err := strconv.Atoi(idStr)
- if err != nil || id <= 0 {
- utils.JSON(&c.Controller, 400, "invalid or missing id parameter", nil)
- return
- }
- err = communityListService.DeleteCommunityList(id)
- if err != nil {
- utils.JSON(&c.Controller, 500, err.Error(), nil)
- return
- }
- utils.JSON(&c.Controller, 200, "success", map[string]interface{}{
- "message": "community list record deleted successfully",
- })
- }
|