132 lines
5.4 KiB
Go
132 lines
5.4 KiB
Go
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
|
//go:build plus
|
|
|
|
package models
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
|
"github.com/iwind/TeaGo/dbs"
|
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
|
)
|
|
|
|
// CreatePackage 创建高防产品
|
|
func (this *ADPackageDAO) CreatePackage(tx *dbs.Tx, networkId int64, protectionBandwidthSize int32, protectionBandwidthUnit userconfigs.ADPackageSizeUnit, serverBandwidthSize int32, serverBandwidthUnit userconfigs.ADPackageSizeUnit) (int64, error) {
|
|
var op = NewADPackageOperator()
|
|
op.NetworkId = networkId
|
|
op.ProtectionBandwidthSize = protectionBandwidthSize
|
|
op.ProtectionBandwidthUnit = protectionBandwidthUnit
|
|
op.ProtectionBandwidthBits = userconfigs.ADPackageSizeBits(protectionBandwidthSize, protectionBandwidthUnit)
|
|
op.ServerBandwidthSize = serverBandwidthSize
|
|
op.ServerBandwidthUnit = serverBandwidthUnit
|
|
op.ServerBandwidthBits = userconfigs.ADPackageSizeBits(serverBandwidthSize, serverBandwidthUnit)
|
|
|
|
op.IsOn = true
|
|
op.State = ADPackageStateEnabled
|
|
return this.SaveInt64(tx, op)
|
|
}
|
|
|
|
// UpdatePackage 修改高防产品
|
|
func (this *ADPackageDAO) UpdatePackage(tx *dbs.Tx, packageId int64, isOn bool, networkId int64, protectionBandwidthSize int32, protectionBandwidthUnit userconfigs.ADPackageSizeUnit, serverBandwidthSize int32, serverBandwidthUnit userconfigs.ADPackageSizeUnit) error {
|
|
if packageId <= 0 {
|
|
return errors.New("invalid packageId")
|
|
}
|
|
|
|
var op = NewADPackageOperator()
|
|
op.Id = packageId
|
|
op.NetworkId = networkId
|
|
op.ProtectionBandwidthSize = protectionBandwidthSize
|
|
op.ProtectionBandwidthUnit = protectionBandwidthUnit
|
|
op.ProtectionBandwidthBits = userconfigs.ADPackageSizeBits(protectionBandwidthSize, protectionBandwidthUnit)
|
|
op.ServerBandwidthSize = serverBandwidthSize
|
|
op.ServerBandwidthUnit = serverBandwidthUnit
|
|
op.ServerBandwidthBits = userconfigs.ADPackageSizeBits(serverBandwidthSize, serverBandwidthUnit)
|
|
|
|
op.IsOn = isOn
|
|
return this.Save(tx, op)
|
|
}
|
|
|
|
// CountAllPackages 查询高防产品数量
|
|
func (this *ADPackageDAO) CountAllPackages(tx *dbs.Tx, networkId int64) (int64, error) {
|
|
var query = this.Query(tx).
|
|
State(ADPackageStateEnabled)
|
|
|
|
if networkId > 0 {
|
|
query.Attr("networkId", networkId)
|
|
}
|
|
|
|
query.Where("networkId IN (SELECT id FROM " + SharedADNetworkDAO.Table + " WHERE state=1)")
|
|
|
|
return query.Count()
|
|
}
|
|
|
|
// ListPackages 列出单页高防产品
|
|
func (this *ADPackageDAO) ListPackages(tx *dbs.Tx, networkId int64, offset int64, size int64) (result []*ADPackage, err error) {
|
|
var query = this.Query(tx).
|
|
State(ADPackageStateEnabled)
|
|
if networkId > 0 {
|
|
query.Attr("networkId", networkId)
|
|
}
|
|
_, err = query.
|
|
Where("networkId IN (SELECT id FROM " + SharedADNetworkDAO.Table + " WHERE state=1)").
|
|
Asc("networkId").
|
|
Asc("protectionBandwidthSize").
|
|
Asc("serverBandwidthSize").
|
|
AscPk().
|
|
Offset(offset).
|
|
Limit(size).
|
|
Slice(&result).
|
|
FindAll()
|
|
return
|
|
}
|
|
|
|
// CountAllIdlePackages 查询可用的产品数量
|
|
func (this *ADPackageDAO) CountAllIdlePackages(tx *dbs.Tx) (int64, error) {
|
|
return this.Query(tx).
|
|
State(ADPackageStateEnabled).
|
|
Attr("isOn", true).
|
|
Where("networkId IN (SELECT id FROM "+SharedADNetworkDAO.Table+" WHERE state=1 AND isOn=1)").
|
|
Where("id IN (SELECT DISTINCT packageId FROM "+SharedADPackageInstanceDAO.Table+" WHERE state=1 AND isOn=1 AND (userId=0 OR userDayTo IS NULL OR userDayTo<:endDay))"). // 确保没有正在使用
|
|
Param("endDay", timeutil.Format("Ymd")).
|
|
Where("id IN (SELECT DISTINCT packageId FROM " + SharedADPackagePriceDAO.Table + " WHERE price>0 )"). // 已设定价格
|
|
Count()
|
|
}
|
|
|
|
// FindAllIdlePackages 列出所有可用的高防产品
|
|
func (this *ADPackageDAO) FindAllIdlePackages(tx *dbs.Tx) (result []*ADPackage, err error) {
|
|
_, err = this.Query(tx).
|
|
State(ADPackageStateEnabled).
|
|
Attr("isOn", true).
|
|
Where("networkId IN (SELECT id FROM "+SharedADNetworkDAO.Table+" WHERE state=1 AND isOn=1)").
|
|
Where("id IN (SELECT DISTINCT packageId FROM "+SharedADPackageInstanceDAO.Table+" WHERE state=1 AND isOn=1 AND (userId=0 OR userDayTo IS NULL OR userDayTo>:endDay))"). // 确保没有正在使用
|
|
Param("endDay", timeutil.Format("Ymd")).
|
|
Where("id IN (SELECT DISTINCT packageId FROM " + SharedADPackagePriceDAO.Table + " WHERE price>0 )"). // 已设定价格
|
|
Asc("protectionBandwidthSize").
|
|
Asc("serverBandwidthSize").
|
|
AscPk().
|
|
Slice(&result).
|
|
FindAll()
|
|
|
|
return
|
|
}
|
|
|
|
// FindIdlePackage 查找符合条件的高防产品
|
|
func (this *ADPackageDAO) FindIdlePackage(tx *dbs.Tx, networkId int64, protectionBandwidthSize int64, protectionBandwidthUnit string, serverBandwidthSize int64, serverBandwidthUnit string) (p *ADPackage, err error) {
|
|
one, err := this.Query(tx).
|
|
State(ADPackageStateEnabled).
|
|
Attr("isOn", true).
|
|
Attr("networkId", networkId).
|
|
Attr("protectionBandwidthSize", protectionBandwidthSize).
|
|
Attr("protectionBandwidthUnit", protectionBandwidthUnit).
|
|
Attr("serverBandwidthSize", serverBandwidthSize).
|
|
Attr("serverBandwidthUnit", serverBandwidthUnit).
|
|
Where("id IN (SELECT DISTINCT packageId FROM "+SharedADPackageInstanceDAO.Table+" WHERE state=1 AND isOn=1 AND (userId=0 OR userDayTo IS NULL OR userDayTo<:endDay))"). // 确保没有正在使用
|
|
Param("endDay", timeutil.Format("Ymd")).
|
|
Where("id IN (SELECT DISTINCT packageId FROM " + SharedADPackagePriceDAO.Table + " WHERE price>0 )"). // 已设定价格
|
|
Find()
|
|
if err != nil || one == nil {
|
|
return nil, err
|
|
}
|
|
return one.(*ADPackage), nil
|
|
}
|