浏览代码

社区图谱

huangchundi 2 天之前
父节点
当前提交
45718254ef
共有 4 个文件被更改,包括 595 次插入0 次删除
  1. 229 0
      controllers/api/CommunityListController.go
  2. 201 0
      controllers/services/CommunityListService.go
  3. 160 0
      models/community_list.go
  4. 5 0
      routers/router.go

+ 229 - 0
controllers/api/CommunityListController.go

@@ -0,0 +1,229 @@
+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",
+	})
+}

+ 201 - 0
controllers/services/CommunityListService.go

@@ -0,0 +1,201 @@
+package services
+
+import (
+	"errors"
+	"think-go/models"
+
+	"github.com/beego/beego/v2/client/orm"
+)
+
+type CommunityListService struct{}
+
+// CommunityInfo represents community list information
+type CommunityInfo struct {
+	Id             int    `json:"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
+}
+
+// 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
+}
+
+// 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.Id <= 0 {
+		return errors.New("invalid id")
+	}
+
+	record, err := models.GetCommunityListById(info.Id)
+	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{
+				Id:             record.Id,
+				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.Id)
+}
+
+// toCommunityInfo converts model to service info
+func (s *CommunityListService) toCommunityInfo(record *models.CommunityList) *CommunityInfo {
+	return &CommunityInfo{
+		Id:             record.Id,
+		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,
+	}
+}

+ 160 - 0
models/community_list.go

@@ -0,0 +1,160 @@
+package models
+
+import (
+	"errors"
+	"fmt"
+	"reflect"
+	"strings"
+
+	"github.com/beego/beego/v2/client/orm"
+)
+
+type CommunityList struct {
+	Id             int    `orm:"column(community_id);auto" description:"社区图谱"`
+	ParentId       string `orm:"column(parent_id);size(225);null" description:"上级id"`
+	UserId         string `orm:"column(user_id);size(225);null"`
+	Username       string `orm:"column(username);size(255);null"`
+	PromoteName    string `orm:"column(promote_name);size(255);null" description:"级别"`
+	Sales          string `orm:"column(sales);size(255);null" description:"入仓金额"`
+	TeamSales      string `orm:"column(team_sales);size(255);null" description:"社区业绩"`
+	TodayTeamSales string `orm:"column(today_team_sales);size(255);null" description:"今天业绩"`
+	UserBv         string `orm:"column(user_bv);size(255);null"`
+	SubStatus      string `orm:"column(sub_status);size(255);null"`
+	CTime          int64  `orm:"column(c_time);null"`
+	IsYes          int    `orm:"column(is_yes);null" description:"默认0:待抓取"`
+}
+
+func (t *CommunityList) TableName() string {
+	return "community_list"
+}
+
+func init() {
+	orm.RegisterModel(new(CommunityList))
+}
+
+// AddCommunityList insert a new CommunityList into database and returns
+// last inserted Id on success.
+func AddCommunityList(m *CommunityList) (id int64, err error) {
+	o := orm.NewOrm()
+	id, err = o.Insert(m)
+	return
+}
+
+// GetCommunityListById retrieves CommunityList by Id. Returns error if
+// Id doesn't exist
+func GetCommunityListById(id int) (v *CommunityList, err error) {
+	o := orm.NewOrm()
+	v = &CommunityList{Id: id}
+	if err = o.Read(v); err == nil {
+		return v, nil
+	}
+	return nil, err
+}
+
+// GetAllCommunityList retrieves all CommunityList matches certain condition. Returns empty list if
+// no records exist
+func GetAllCommunityList(query map[string]string, fields []string, sortby []string, order []string,
+	offset int64, limit int64) (ml []interface{}, err error) {
+	o := orm.NewOrm()
+	qs := o.QueryTable(new(CommunityList))
+	// query k=v
+	for k, v := range query {
+		// rewrite dot-notation to Object__Attribute
+		k = strings.Replace(k, ".", "__", -1)
+		if strings.Contains(k, "isnull") {
+			qs = qs.Filter(k, (v == "true" || v == "1"))
+		} else {
+			qs = qs.Filter(k, v)
+		}
+	}
+	// order by:
+	var sortFields []string
+	if len(sortby) != 0 {
+		if len(sortby) == len(order) {
+			// 1) for each sort field, there is an associated order
+			for i, v := range sortby {
+				orderby := ""
+				if order[i] == "desc" {
+					orderby = "-" + v
+				} else if order[i] == "asc" {
+					orderby = v
+				} else {
+					return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
+				}
+				sortFields = append(sortFields, orderby)
+			}
+			qs = qs.OrderBy(sortFields...)
+		} else if len(sortby) != len(order) && len(order) == 1 {
+			// 2) there is exactly one order, all the sorted fields will be sorted by this order
+			for _, v := range sortby {
+				orderby := ""
+				if order[0] == "desc" {
+					orderby = "-" + v
+				} else if order[0] == "asc" {
+					orderby = v
+				} else {
+					return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
+				}
+				sortFields = append(sortFields, orderby)
+			}
+		} else if len(sortby) != len(order) && len(order) != 1 {
+			return nil, errors.New("Error: 'sortby', 'order' sizes mismatch or 'order' size is not 1")
+		}
+	} else {
+		if len(order) != 0 {
+			return nil, errors.New("Error: unused 'order' fields")
+		}
+	}
+
+	var l []CommunityList
+	qs = qs.OrderBy(sortFields...)
+	if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil {
+		if len(fields) == 0 {
+			for _, v := range l {
+				ml = append(ml, v)
+			}
+		} else {
+			// trim unused fields
+			for _, v := range l {
+				m := make(map[string]interface{})
+				val := reflect.ValueOf(v)
+				for _, fname := range fields {
+					m[fname] = val.FieldByName(fname).Interface()
+				}
+				ml = append(ml, m)
+			}
+		}
+		return ml, nil
+	}
+	return nil, err
+}
+
+// UpdateCommunityList updates CommunityList by Id and returns error if
+// the record to be updated doesn't exist
+func UpdateCommunityListById(m *CommunityList) (err error) {
+	o := orm.NewOrm()
+	v := CommunityList{Id: m.Id}
+	// ascertain id exists in the database
+	if err = o.Read(&v); err == nil {
+		var num int64
+		if num, err = o.Update(m); err == nil {
+			fmt.Println("Number of records updated in database:", num)
+		}
+	}
+	return
+}
+
+// DeleteCommunityList deletes CommunityList by Id and returns error if
+// the record to be deleted doesn't exist
+func DeleteCommunityList(id int) (err error) {
+	o := orm.NewOrm()
+	v := CommunityList{Id: id}
+	// ascertain id exists in the database
+	if err = o.Read(&v); err == nil {
+		var num int64
+		if num, err = o.Delete(&CommunityList{Id: id}); err == nil {
+			fmt.Println("Number of records deleted in database:", num)
+		}
+	}
+	return
+}

+ 5 - 0
routers/router.go

@@ -29,6 +29,11 @@ func init() {
 		beego.NSRouter("/invite/getInviteList", &api.InviteListDirController{}, "post:GetExternalInviteList"),
 		beego.NSRouter("/invite/GetExternalInviteList002", &api.InviteListDirController{}, "post:GetExternalInviteList002"),
 		beego.NSRouter("/inter/apidatainert", &api.InterceptController{}, "post:Apidatainert"),
+		beego.NSRouter("/community/list", &api.CommunityListController{}, "get:GetCommunityList"),
+		beego.NSRouter("/community/create", &api.CommunityListController{}, "post:CreateCommunityList"),
+		beego.NSRouter("/community/update", &api.CommunityListController{}, "post:UpdateCommunityList"),
+		beego.NSRouter("/community/listAll", &api.CommunityListController{}, "get:GetAllCommunityLists"),
+		beego.NSRouter("/community/delete", &api.CommunityListController{}, "post:DeleteCommunityList"),
 	)
 	beego.AddNamespace(nsAdmin)
 	beego.AddNamespace(nsApi)