Browse Source

临时提交

boweniac 3 months ago
parent
commit
97848c3592

+ 53 - 0
desc/wechat/dashboard.api

@@ -39,6 +39,51 @@ type (
         Label []string `json:"label"`
         Val []int64 `json:"val"`
     }
+
+    LabelsReq {
+        EndDate *string `json:"end_date"`
+
+        // 租户id
+        OrganizationId  *uint64 `json:"organizationId,optional"`
+    }
+
+    LabelsResp {
+        BaseDataInfo
+
+        Data []LabelsData `json:"data"`
+    }
+
+    LabelsData {
+        Value uint64 `json:"value"`
+        Name string `json:"name"`
+    }
+
+    WxReq {
+        PageInfo
+
+        EndDate *string `json:"end_date"`
+
+        // 租户id
+        OrganizationId  *uint64 `json:"organizationId,optional"`
+    }
+
+    WxResp {
+        BaseDataInfo
+
+        Data []WxList `json:"data"`
+    }
+
+    WxList {
+        BaseListInfo
+
+        Data  []WxData  `json:"data"`
+    }
+
+    WxData {
+        TotalFriend uint64 `json:"total_friend"`
+        TotalGroup uint64 `json:"total_group"`
+        InteractionRate float32 `json:"interaction_rate"`
+    }
 )
 
 @server(
@@ -51,4 +96,12 @@ service Wechat {
     // get charts | 获取图表数据
     @handler getCharts
     post /dashboard/charts (ChartsReq) returns (ChartsResp)
+
+    // get labels | 获取图表数据
+    @handler getLabels
+    post /dashboard/labels (LabelsReq) returns (LabelsResp)
+
+    // get wxs | 获取图表数据
+    @handler getWxs
+    post /dashboard/wx (WxReq) returns (WxResp)
 }

+ 44 - 0
internal/handler/dashboard/get_labels_handler.go

@@ -0,0 +1,44 @@
+package dashboard
+
+import (
+	"net/http"
+
+	"github.com/zeromicro/go-zero/rest/httpx"
+
+	"wechat-api/internal/logic/dashboard"
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+)
+
+// swagger:route post /dashboard/labels dashboard GetLabels
+//
+// get labels | 获取图表数据
+//
+// get labels | 获取图表数据
+//
+// Parameters:
+//  + name: body
+//    require: true
+//    in: body
+//    type: LabelsReq
+//
+// Responses:
+//  200: LabelsResp
+
+func GetLabelsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.LabelsReq
+		if err := httpx.Parse(r, &req, true); err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+			return
+		}
+
+		l := dashboard.NewGetLabelsLogic(r.Context(), svcCtx)
+		resp, err := l.GetLabels(&req)
+		if err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+		} else {
+			httpx.OkJsonCtx(r.Context(), w, resp)
+		}
+	}
+}

+ 44 - 0
internal/handler/dashboard/get_wxs_handler.go

@@ -0,0 +1,44 @@
+package dashboard
+
+import (
+	"net/http"
+
+	"github.com/zeromicro/go-zero/rest/httpx"
+
+	"wechat-api/internal/logic/dashboard"
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+)
+
+// swagger:route post /dashboard/wx dashboard GetWxs
+//
+// get wxs | 获取图表数据
+//
+// get wxs | 获取图表数据
+//
+// Parameters:
+//  + name: body
+//    require: true
+//    in: body
+//    type: WxReq
+//
+// Responses:
+//  200: WxResp
+
+func GetWxsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.WxReq
+		if err := httpx.Parse(r, &req, true); err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+			return
+		}
+
+		l := dashboard.NewGetWxsLogic(r.Context(), svcCtx)
+		resp, err := l.GetWxs(&req)
+		if err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+		} else {
+			httpx.OkJsonCtx(r.Context(), w, resp)
+		}
+	}
+}

+ 10 - 0
internal/handler/routes.go

@@ -1498,6 +1498,16 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
 					Path:    "/dashboard/charts",
 					Handler: dashboard.GetChartsHandler(serverCtx),
 				},
+				{
+					Method:  http.MethodPost,
+					Path:    "/dashboard/labels",
+					Handler: dashboard.GetLabelsHandler(serverCtx),
+				},
+				{
+					Method:  http.MethodPost,
+					Path:    "/dashboard/wx",
+					Handler: dashboard.GetWxsHandler(serverCtx),
+				},
 			}...,
 		),
 		rest.WithJwt(serverCtx.Config.Auth.AccessSecret),

+ 51 - 0
internal/logic/dashboard/get_charts_logic.go

@@ -148,12 +148,20 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 				Val:   append(totalFriend.Val, tf.Value),
 			}
 		}
+		totalFriendLen := len(totalFriendSum)
+		if totalFriendLen > 0 && totalFriendSum[0].Value > 0 {
+			totalFriend.Rate = float32((totalFriendSum[totalFriendLen-1].Value - totalFriendSum[0].Value) / totalFriendSum[0].Value)
+		}
 		for _, tg := range totalGroupSum {
 			totalGroup = types.ChartsUint{
 				Count: tg.Value,
 				Val:   append(totalGroup.Val, tg.Value),
 			}
 		}
+		totalGroupLen := len(totalGroupSum)
+		if totalGroupLen > 0 && totalGroupSum[0].Value > 0 {
+			totalGroup.Rate = float32((totalGroupSum[totalGroupLen-1].Value - totalGroupSum[0].Value) / totalGroupSum[0].Value)
+		}
 		for _, ct := range consumeTokenSum {
 			addtimeLastTwoDigits := ct.Addtime % 100
 			consumeToken = types.ChartsUint{
@@ -168,6 +176,10 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 				Val:   append(activeUser.Val, au.Value),
 			}
 		}
+		activeUserLen := len(activeUserSum)
+		if activeUserLen > 0 && activeUserSum[0].Value > 0 {
+			activeUser.Rate = float32((activeUserSum[activeUserLen-1].Value - activeUserSum[0].Value) / activeUserSum[0].Value)
+		}
 		for _, nu := range newUserSum {
 			addtimeLastTwoDigits := nu.Addtime % 100
 			newUser = types.ChartsInt{
@@ -176,6 +188,10 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 				Label: append(newUser.Label, fmt.Sprintf("%02d", addtimeLastTwoDigits)),
 			}
 		}
+		newUserLen := len(newUserSum)
+		if newUserLen > 0 && newUserSum[0].Value > 0 {
+			newUser.Rate = float32((newUserSum[newUserLen-1].Value - newUserSum[0].Value) / newUserSum[0].Value)
+		}
 	} else {
 		// 从天表统计数据
 		startAddTimeString := startTime.Format("20060102")
@@ -217,18 +233,29 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 				Val:   append(sopRun.Val, sr.Value),
 			}
 		}
+
 		for _, tf := range totalFriendSum {
 			totalFriend = types.ChartsUint{
 				Count: tf.Value,
 				Val:   append(totalFriend.Val, tf.Value),
 			}
 		}
+		totalFriendLen := len(totalFriendSum)
+		if totalFriendLen > 0 && totalFriendSum[0].Value > 0 {
+			totalFriend.Rate = float32((totalFriendSum[totalFriendLen-1].Value - totalFriendSum[0].Value) / totalFriendSum[0].Value)
+		}
+
 		for _, tg := range totalGroupSum {
 			totalGroup = types.ChartsUint{
 				Count: tg.Value,
 				Val:   append(totalGroup.Val, tg.Value),
 			}
 		}
+		totalGroupLen := len(totalGroupSum)
+		if totalGroupLen > 0 && totalGroupSum[0].Value > 0 {
+			totalGroup.Rate = float32((totalGroupSum[totalGroupLen-1].Value - totalGroupSum[0].Value) / totalGroupSum[0].Value)
+		}
+
 		for _, ct := range consumeTokenSum {
 			addtimeLastTwoDigits := ct.Addtime % 100
 			consumeToken = types.ChartsUint{
@@ -243,6 +270,10 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 				Val:   append(activeUser.Val, au.Value),
 			}
 		}
+		activeUserLen := len(activeUserSum)
+		if activeUserLen > 0 && activeUserSum[0].Value > 0 {
+			activeUser.Rate = float32((activeUserSum[activeUserLen-1].Value - activeUserSum[0].Value) / activeUserSum[0].Value)
+		}
 		for _, nu := range newUserSum {
 			addtimeLastTwoDigits := nu.Addtime % 100
 			newUser = types.ChartsInt{
@@ -251,6 +282,10 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 				Label: append(newUser.Label, fmt.Sprintf("%02d", addtimeLastTwoDigits)),
 			}
 		}
+		newUserLen := len(newUserSum)
+		if newUserLen > 0 && newUserSum[0].Value > 0 {
+			newUser.Rate = float32((newUserSum[newUserLen-1].Value - newUserSum[0].Value) / newUserSum[0].Value)
+		}
 
 		if isCurrentDay {
 			// 从小时表统计当天数据
@@ -300,10 +335,18 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 					Count: totalFriendOfDay,
 					Val:   append(totalFriend.Val, totalFriendOfDay),
 				}
+				tfLen := len(totalFriend.Val)
+				if tfLen > 0 && totalFriend.Val[0] > 0 {
+					totalFriend.Rate = float32((totalFriend.Val[tfLen-1] - totalFriend.Val[0]) / totalFriend.Val[0])
+				}
 				totalGroup = types.ChartsUint{
 					Count: totalGroupOfDay,
 					Val:   append(totalGroup.Val, totalGroupOfDay),
 				}
+				tgLen := len(totalGroup.Val)
+				if tgLen > 0 && totalGroup.Val[0] > 0 {
+					totalGroup.Rate = float32((totalGroup.Val[tgLen-1] - totalGroup.Val[0]) / totalGroup.Val[0])
+				}
 				consumeToken = types.ChartsUint{
 					Count: consumeToken.Count + consumeTokenOfDay,
 					Val:   append(consumeToken.Val, consumeTokenOfDay),
@@ -313,11 +356,19 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 					Count: activeUserOfDay,
 					Val:   append(activeUser.Val, activeUserOfDay),
 				}
+				auLen := len(activeUser.Val)
+				if auLen > 0 && activeUser.Val[0] > 0 {
+					activeUser.Rate = float32((activeUser.Val[auLen-1] - activeUser.Val[0]) / activeUser.Val[0])
+				}
 				newUser = types.ChartsInt{
 					Count: newUserOfDay,
 					Val:   append(newUser.Val, newUserOfDay),
 					Label: append(newUser.Label, "今日"),
 				}
+				nuLen := len(newUser.Val)
+				if nuLen > 0 && newUser.Val[0] > 0 {
+					newUser.Rate = float32((newUser.Val[nuLen-1] - newUser.Val[0]) / newUser.Val[0])
+				}
 			}
 		}
 	}

+ 47 - 0
internal/logic/dashboard/get_labels_logic.go

@@ -0,0 +1,47 @@
+package dashboard
+
+import (
+	"context"
+	"wechat-api/ent/labelrelationship"
+	"wechat-api/ent/predicate"
+	"wechat-api/internal/utils/dberrorhandler"
+
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+
+	"github.com/zeromicro/go-zero/core/logx"
+)
+
+type GetLabelsLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewGetLabelsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetLabelsLogic {
+	return &GetLabelsLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx}
+}
+
+func (l *GetLabelsLogic) GetLabels(req *types.LabelsReq) (resp *types.LabelsResp, err error) {
+	// 获取组织id
+	var organizationId uint64 = 0
+	isAdmin := l.ctx.Value("isAdmin").(bool)
+	if isAdmin && req.OrganizationId != nil && *req.OrganizationId != 0 {
+		organizationId = *req.OrganizationId
+	} else {
+		organizationId = l.ctx.Value("organizationId").(uint64)
+	}
+
+	var predicates []predicate.LabelRelationship
+	predicates = append(predicates, labelrelationship.OrganizationIDEQ(organizationId))
+	data, err := l.svcCtx.DB.LabelRelationship.Query().Where(predicates...).Page(l.ctx, req.Page, req.PageSize)
+
+	if err != nil {
+		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+	}
+
+	return
+}

+ 29 - 0
internal/logic/dashboard/get_wxs_logic.go

@@ -0,0 +1,29 @@
+package dashboard
+
+import (
+	"context"
+
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+
+	"github.com/zeromicro/go-zero/core/logx"
+)
+
+type GetWxsLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewGetWxsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetWxsLogic {
+	return &GetWxsLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx}
+}
+
+func (l *GetWxsLogic) GetWxs(req *types.WxReq) (resp *types.WxResp, err error) {
+	// todo: add your logic here and delete this line
+
+	return
+}

+ 43 - 0
internal/types/types.go

@@ -2943,3 +2943,46 @@ type ChartsInt struct {
 	Label []string `json:"label"`
 	Val   []int64  `json:"val"`
 }
+
+// swagger:model LabelsReq
+type LabelsReq struct {
+	EndDate *string `json:"end_date"`
+	// 租户id
+	OrganizationId *uint64 `json:"organizationId,optional"`
+}
+
+// swagger:model LabelsResp
+type LabelsResp struct {
+	BaseDataInfo
+	Data []LabelsData `json:"data"`
+}
+
+type LabelsData struct {
+	Value uint64 `json:"value"`
+	Name  string `json:"name"`
+}
+
+// swagger:model WxReq
+type WxReq struct {
+	PageInfo
+	EndDate *string `json:"end_date"`
+	// 租户id
+	OrganizationId *uint64 `json:"organizationId,optional"`
+}
+
+// swagger:model WxResp
+type WxResp struct {
+	BaseDataInfo
+	Data []WxList `json:"data"`
+}
+
+type WxList struct {
+	BaseListInfo
+	Data []WxData `json:"data"`
+}
+
+type WxData struct {
+	TotalFriend     uint64  `json:"total_friend"`
+	TotalGroup      uint64  `json:"total_group"`
+	InteractionRate float32 `json:"interaction_rate"`
+}