twin-api/app/common/model/pkg.go

74 lines
1.5 KiB
Go

package model
import (
"fmt"
"strings"
gsUtils "git.u8t.cn/open/go-server/utils"
"github.com/spf13/cast"
)
type Pkg struct {
Id int64 `json:"id"`
ExpireTime int64 `json:"expire_time"`
CreateTime int64 `json:"create_time"`
UpdateTime int64 `json:"update_time"`
Status int64 `json:"status"`
Name string `json:"name"`
Version string `json:"version"`
Link string `json:"link"`
}
const (
PkgNormal = 1
PkgDelete = 2
)
func (p *Pkg) Format() map[string]any {
res := make(map[string]any)
res["id"] = p.Id
res["name"] = p.Name
res["version"] = p.Version
res["link"] = p.Link
res["status"] = p.Status
res["expire"] = gsUtils.TimeToDateTime(p.ExpireTime)
res["create_date"] = gsUtils.TimeToDateTime(p.CreateTime)
res["update_date"] = gsUtils.TimeToDateTime(p.UpdateTime)
return res
}
func (p *Pkg) FormatAdmin(userCount int64) map[string]any {
res := p.Format()
res["user_count"] = userCount
return res
}
func ParseExpire(expire string) (int64, error) {
expire = strings.TrimSpace(expire)
if expire == "" {
return 0, fmt.Errorf("expire is required")
}
if isUnixTimestamp(expire) {
return cast.ToInt64(expire), nil
}
if ts := gsUtils.DateTimeToTime(expire); ts > 0 {
return ts, nil
}
if ts := gsUtils.DateToTime(expire); ts > 0 {
return ts, nil
}
return 0, fmt.Errorf("invalid expire format, supports timestamp or date")
}
func isUnixTimestamp(s string) bool {
if len(s) < 10 {
return false
}
for _, c := range s {
if c < '0' || c > '9' {
return false
}
}
return cast.ToInt64(s) > 1e9
}