164 lines
3.9 KiB
Go
164 lines
3.9 KiB
Go
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
|
//go:build plus
|
|
|
|
package userPlans
|
|
|
|
import (
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
"github.com/iwind/TeaGo/maps"
|
|
)
|
|
|
|
type IndexAction struct {
|
|
actionutils.ParentAction
|
|
}
|
|
|
|
func (this *IndexAction) Init() {
|
|
this.Nav("", "", "")
|
|
}
|
|
|
|
func (this *IndexAction) RunGet(params struct {
|
|
Type string
|
|
UserId int64
|
|
PlanId int64
|
|
PeriodUnit string
|
|
}) {
|
|
if len(params.Type) == 0 {
|
|
params.Type = "available"
|
|
}
|
|
|
|
this.Data["userId"] = params.UserId
|
|
this.Data["planId"] = params.PlanId
|
|
this.Data["periodUnit"] = params.PeriodUnit
|
|
this.Data["type"] = params.Type
|
|
|
|
var expireDays int32 = 0
|
|
var isExpired = false
|
|
switch params.Type {
|
|
case "expiring7":
|
|
expireDays = 7
|
|
case "expiring30":
|
|
expireDays = 30
|
|
case "expired":
|
|
isExpired = true
|
|
}
|
|
|
|
// 统计数据
|
|
countAvailableResp, err := this.RPC().NSUserPlanRPC().CountNSUserPlans(this.AdminContext(), &pb.CountNSUserPlansRequest{})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
this.Data["countAvailable"] = countAvailableResp.Count
|
|
|
|
countExpiring7Resp, err := this.RPC().NSUserPlanRPC().CountNSUserPlans(this.AdminContext(), &pb.CountNSUserPlansRequest{
|
|
ExpireDays: 7,
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
this.Data["countExpiring7"] = countExpiring7Resp.Count
|
|
|
|
countExpiring30Resp, err := this.RPC().NSUserPlanRPC().CountNSUserPlans(this.AdminContext(), &pb.CountNSUserPlansRequest{
|
|
ExpireDays: 30,
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
this.Data["countExpiring30"] = countExpiring30Resp.Count
|
|
|
|
countExpiredResp, err := this.RPC().NSUserPlanRPC().CountNSUserPlans(this.AdminContext(), &pb.CountNSUserPlansRequest{
|
|
IsExpired: true,
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
this.Data["countExpired"] = countExpiredResp.Count
|
|
|
|
// 所有套餐
|
|
plansResp, err := this.RPC().NSPlanRPC().FindAllNSPlans(this.AdminContext(), &pb.FindAllNSPlansRequest{})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
var planMaps = []maps.Map{}
|
|
for _, plan := range plansResp.NsPlans {
|
|
planMaps = append(planMaps, maps.Map{
|
|
"id": plan.Id,
|
|
"name": plan.Name,
|
|
})
|
|
}
|
|
this.Data["plans"] = planMaps
|
|
|
|
// 用户套餐数量
|
|
countResp, err := this.RPC().NSUserPlanRPC().CountNSUserPlans(this.AdminContext(), &pb.CountNSUserPlansRequest{
|
|
UserId: params.UserId,
|
|
NsPlanId: params.PlanId,
|
|
PeriodUnit: params.PeriodUnit,
|
|
IsExpired: isExpired,
|
|
ExpireDays: expireDays,
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
var count = countResp.Count
|
|
var page = this.NewPage(count)
|
|
this.Data["page"] = page.AsHTML()
|
|
|
|
userPlansResp, err := this.RPC().NSUserPlanRPC().ListNSUserPlans(this.AdminContext(), &pb.ListNSUserPlansRequest{
|
|
UserId: params.UserId,
|
|
NsPlanId: params.PlanId,
|
|
PeriodUnit: params.PeriodUnit,
|
|
IsExpired: isExpired,
|
|
ExpireDays: expireDays,
|
|
Offset: page.Offset,
|
|
Size: page.Size,
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
var userPlanMaps = []maps.Map{}
|
|
for _, userPlan := range userPlansResp.NsUserPlans {
|
|
// plan
|
|
var planMap maps.Map
|
|
if userPlan.NsPlan != nil {
|
|
planMap = maps.Map{
|
|
"id": userPlan.NsPlan.Id,
|
|
"name": userPlan.NsPlan.Name,
|
|
}
|
|
}
|
|
|
|
// user
|
|
var userMap maps.Map
|
|
if userPlan.User != nil {
|
|
userMap = maps.Map{
|
|
"id": userPlan.User.Id,
|
|
"username": userPlan.User.Username,
|
|
"fullname": userPlan.User.Fullname,
|
|
}
|
|
}
|
|
|
|
// 错误的结束日期跳过,防止下面格式化的时候出错
|
|
if len(userPlan.DayTo) < 8 {
|
|
continue
|
|
}
|
|
|
|
userPlanMaps = append(userPlanMaps, maps.Map{
|
|
"id": userPlan.Id,
|
|
"dayFrom": userPlan.DayFrom,
|
|
"dayTo": userPlan.DayTo[:4] + "-" + userPlan.DayTo[4:6] + "-" + userPlan.DayTo[6:],
|
|
"periodUnit": userPlan.PeriodUnit,
|
|
"plan": planMap,
|
|
"user": userMap,
|
|
})
|
|
}
|
|
this.Data["userPlans"] = userPlanMaps
|
|
|
|
this.Show()
|
|
}
|