200 lines
5.2 KiB
Go
200 lines
5.2 KiB
Go
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
|
//go:build plus
|
|
|
|
package models
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
|
"github.com/iwind/TeaGo/dbs"
|
|
)
|
|
|
|
// FindUserPriceInfo 查找用户计费方式和周期等账单信息
|
|
func (this *UserDAO) FindUserPriceInfo(tx *dbs.Tx, userId int64) (priceType userconfigs.PriceType, pricePeriod userconfigs.PricePeriod, err error) {
|
|
if userId <= 0 {
|
|
return
|
|
}
|
|
|
|
one, err := this.Query(tx).
|
|
Pk(userId).
|
|
Result("priceType", "pricePeriod").
|
|
Find()
|
|
if err != nil || one == nil {
|
|
return "", "", err
|
|
}
|
|
|
|
var user = one.(*User)
|
|
priceType = user.PriceType
|
|
pricePeriod = user.PricePeriod
|
|
|
|
if len(priceType) > 0 && !userconfigs.IsValidPriceType(priceType) {
|
|
priceType = ""
|
|
}
|
|
|
|
if len(pricePeriod) > 0 && !userconfigs.IsValidPricePeriod(pricePeriod) {
|
|
pricePeriod = ""
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// UpdateUserPriceType 修改用户计费方式
|
|
func (this *UserDAO) UpdateUserPriceType(tx *dbs.Tx, userId int64, priceType userconfigs.PriceType) error {
|
|
if !userconfigs.IsValidPriceType(priceType) {
|
|
return errors.New("invalid price type '" + priceType + "'")
|
|
}
|
|
return this.Query(tx).
|
|
Pk(userId).
|
|
Set("priceType", priceType).
|
|
UpdateQuickly()
|
|
}
|
|
|
|
// UpdateUserPricePeriod 修改用户计费周期
|
|
func (this *UserDAO) UpdateUserPricePeriod(tx *dbs.Tx, userId int64, pricePeriod userconfigs.PricePeriod) error {
|
|
if !userconfigs.IsValidPricePeriod(pricePeriod) {
|
|
return errors.New("invalid price period '" + pricePeriod + "'")
|
|
}
|
|
return this.Query(tx).
|
|
Pk(userId).
|
|
Set("pricePeriod", pricePeriod).
|
|
UpdateQuickly()
|
|
}
|
|
|
|
// FindUserBandwidthAlgoForFee 获取用户计费用的带宽算法
|
|
func (this *UserDAO) FindUserBandwidthAlgoForFee(tx *dbs.Tx, userId int64, priceConfig *userconfigs.UserPriceConfig) (bandwidthAlgo string, err error) {
|
|
bandwidthAlgo, err = this.Query(tx).
|
|
Pk(userId).
|
|
Result("bandwidthAlgo").
|
|
FindStringCol("")
|
|
if len(bandwidthAlgo) > 0 {
|
|
return
|
|
}
|
|
|
|
if priceConfig == nil {
|
|
priceConfig, err = SharedSysSettingDAO.ReadUserPriceConfig(tx)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if priceConfig == nil {
|
|
return systemconfigs.BandwidthAlgoSecondly, nil
|
|
}
|
|
}
|
|
|
|
if priceConfig != nil &&
|
|
priceConfig.DefaultBandwidthPriceConfig != nil &&
|
|
len(priceConfig.DefaultBandwidthPriceConfig.BandwidthAlgo) > 0 {
|
|
return priceConfig.DefaultBandwidthPriceConfig.BandwidthAlgo, nil
|
|
}
|
|
|
|
return systemconfigs.BandwidthAlgoSecondly, nil
|
|
}
|
|
|
|
// CheckUserServersEnabled 判断用户是否可用服务功能
|
|
func (this *UserDAO) CheckUserServersEnabled(tx *dbs.Tx, userId int64) (isEnabled bool, err error) {
|
|
if userId <= 0 {
|
|
return true, nil
|
|
}
|
|
|
|
// 是否已删除、未启用、已拒绝
|
|
one, err := this.Query(tx).
|
|
Result("id", "isRejected", "state", "isOn").
|
|
Pk(userId).
|
|
Find()
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if one == nil {
|
|
return false, nil
|
|
}
|
|
var user = one.(*User)
|
|
if user.State != UserStateEnabled || !user.IsOn || user.IsRejected {
|
|
return false, nil
|
|
}
|
|
|
|
// 是否有逾期的账单
|
|
priceConfig, err := SharedSysSettingDAO.ReadUserPriceConfig(tx)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if priceConfig != nil &&
|
|
priceConfig.IsOn &&
|
|
priceConfig.UnpaidBillPolicy.IsOn &&
|
|
priceConfig.UnpaidBillPolicy.DisableServers &&
|
|
(priceConfig.UnpaidBillPolicy.MinDailyBillDays > 0 ||
|
|
priceConfig.UnpaidBillPolicy.MinMonthlyBillDays > 0) {
|
|
countUnpaidBills, err := SharedUserBillDAO.CountAllUserBills(tx, 0, userId, "", true, priceConfig.UnpaidBillPolicy.MinDailyBillDays, priceConfig.UnpaidBillPolicy.MinMonthlyBillDays)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if countUnpaidBills > 0 {
|
|
return false, nil
|
|
}
|
|
}
|
|
|
|
return true, nil
|
|
}
|
|
|
|
// FindUserNotificationConfig 查找通知设置
|
|
func (this *UserDAO) FindUserNotificationConfig(tx *dbs.Tx, userId int64) (*userconfigs.UserNotificationConfig, error) {
|
|
notificationData, err := this.Query(tx).
|
|
Pk(userId).
|
|
Result("notification").
|
|
FindJSONCol()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var config = userconfigs.NewUserNotificationConfig()
|
|
if IsNull(notificationData) {
|
|
return config, nil
|
|
}
|
|
|
|
err = json.Unmarshal(notificationData, config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return config, nil
|
|
}
|
|
|
|
// NotifyEmail 进行邮件通知
|
|
func (this *UserDAO) NotifyEmail(tx *dbs.Tx, userId int64, notificationType userconfigs.UserNotificationType, subject string, body string) error {
|
|
// 当前用户是否有Email设置
|
|
one, err := this.Query(tx).
|
|
Pk(userId).
|
|
Result("email", "verifiedEmail", "notification").
|
|
Find()
|
|
if err != nil || one == nil {
|
|
return err
|
|
}
|
|
var user = one.(*User)
|
|
var email = user.Email
|
|
var verifiedEmail = user.VerifiedEmail
|
|
if len(email) == 0 {
|
|
email = verifiedEmail
|
|
}
|
|
if len(email) == 0 {
|
|
return nil
|
|
}
|
|
|
|
// 检查通知配置
|
|
var notificationData = user.Notification
|
|
var config = userconfigs.NewUserNotificationConfig()
|
|
if !IsNull(notificationData) {
|
|
err = json.Unmarshal(notificationData, config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if !config.Support(notificationType, userconfigs.UserNotificationMediaEmail) {
|
|
return nil
|
|
}
|
|
|
|
// 创建邮件
|
|
_, err = SharedUserEmailNotificationDAO.CreateNotification(tx, email, subject, body)
|
|
return err
|
|
}
|