144 lines
3.7 KiB
Go
144 lines
3.7 KiB
Go
package models
|
|
|
|
import (
|
|
_ "github.com/go-sql-driver/mysql"
|
|
"github.com/iwind/TeaGo/Tea"
|
|
"github.com/iwind/TeaGo/dbs"
|
|
"github.com/iwind/TeaGo/types"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
HTTPDNSCustomRuleStateEnabled = 1
|
|
HTTPDNSCustomRuleStateDisabled = 0
|
|
)
|
|
|
|
type HTTPDNSCustomRuleDAO dbs.DAO
|
|
|
|
func NewHTTPDNSCustomRuleDAO() *HTTPDNSCustomRuleDAO {
|
|
return dbs.NewDAO(&HTTPDNSCustomRuleDAO{
|
|
DAOObject: dbs.DAOObject{
|
|
DB: Tea.Env,
|
|
Table: "edgeHTTPDNSCustomRules",
|
|
Model: new(HTTPDNSCustomRule),
|
|
PkName: "id",
|
|
},
|
|
}).(*HTTPDNSCustomRuleDAO)
|
|
}
|
|
|
|
var SharedHTTPDNSCustomRuleDAO *HTTPDNSCustomRuleDAO
|
|
|
|
func init() {
|
|
dbs.OnReady(func() {
|
|
SharedHTTPDNSCustomRuleDAO = NewHTTPDNSCustomRuleDAO()
|
|
})
|
|
}
|
|
|
|
func (this *HTTPDNSCustomRuleDAO) CreateRule(tx *dbs.Tx, rule *HTTPDNSCustomRule) (int64, error) {
|
|
var op = NewHTTPDNSCustomRuleOperator()
|
|
op.AppId = rule.AppId
|
|
op.DomainId = rule.DomainId
|
|
op.RuleName = rule.RuleName
|
|
op.LineScope = rule.LineScope
|
|
op.LineCarrier = rule.LineCarrier
|
|
op.LineRegion = rule.LineRegion
|
|
op.LineProvince = rule.LineProvince
|
|
op.LineContinent = rule.LineContinent
|
|
op.LineCountry = rule.LineCountry
|
|
op.TTL = rule.TTL
|
|
op.IsOn = rule.IsOn
|
|
op.Priority = rule.Priority
|
|
op.UpdatedAt = time.Now().Unix()
|
|
op.State = HTTPDNSCustomRuleStateEnabled
|
|
err := this.Save(tx, op)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return types.Int64(op.Id), nil
|
|
}
|
|
|
|
func (this *HTTPDNSCustomRuleDAO) UpdateRule(tx *dbs.Tx, rule *HTTPDNSCustomRule) error {
|
|
var op = NewHTTPDNSCustomRuleOperator()
|
|
op.Id = rule.Id
|
|
op.RuleName = rule.RuleName
|
|
op.LineScope = rule.LineScope
|
|
op.LineCarrier = rule.LineCarrier
|
|
op.LineRegion = rule.LineRegion
|
|
op.LineProvince = rule.LineProvince
|
|
op.LineContinent = rule.LineContinent
|
|
op.LineCountry = rule.LineCountry
|
|
op.TTL = rule.TTL
|
|
op.IsOn = rule.IsOn
|
|
op.Priority = rule.Priority
|
|
op.UpdatedAt = time.Now().Unix()
|
|
return this.Save(tx, op)
|
|
}
|
|
|
|
func (this *HTTPDNSCustomRuleDAO) DisableRule(tx *dbs.Tx, ruleId int64) error {
|
|
_, err := this.Query(tx).
|
|
Pk(ruleId).
|
|
Set("state", HTTPDNSCustomRuleStateDisabled).
|
|
Update()
|
|
return err
|
|
}
|
|
|
|
func (this *HTTPDNSCustomRuleDAO) DisableRulesWithAppId(tx *dbs.Tx, appDbId int64) error {
|
|
_, err := this.Query(tx).
|
|
Attr("appId", appDbId).
|
|
State(HTTPDNSCustomRuleStateEnabled).
|
|
Set("state", HTTPDNSCustomRuleStateDisabled).
|
|
Set("updatedAt", time.Now().Unix()).
|
|
Update()
|
|
return err
|
|
}
|
|
|
|
func (this *HTTPDNSCustomRuleDAO) UpdateRuleStatus(tx *dbs.Tx, ruleId int64, isOn bool) error {
|
|
_, err := this.Query(tx).
|
|
Pk(ruleId).
|
|
State(HTTPDNSCustomRuleStateEnabled).
|
|
Set("isOn", isOn).
|
|
Set("updatedAt", time.Now().Unix()).
|
|
Update()
|
|
return err
|
|
}
|
|
|
|
func (this *HTTPDNSCustomRuleDAO) FindEnabledRule(tx *dbs.Tx, ruleId int64) (*HTTPDNSCustomRule, error) {
|
|
one, err := this.Query(tx).
|
|
Pk(ruleId).
|
|
State(HTTPDNSCustomRuleStateEnabled).
|
|
Find()
|
|
if one == nil {
|
|
return nil, err
|
|
}
|
|
return one.(*HTTPDNSCustomRule), nil
|
|
}
|
|
|
|
func (this *HTTPDNSCustomRuleDAO) ListEnabledRulesWithDomainId(tx *dbs.Tx, domainId int64) (result []*HTTPDNSCustomRule, err error) {
|
|
_, err = this.Query(tx).
|
|
State(HTTPDNSCustomRuleStateEnabled).
|
|
Attr("domainId", domainId).
|
|
Asc("priority").
|
|
AscPk().
|
|
Slice(&result).
|
|
FindAll()
|
|
return
|
|
}
|
|
|
|
func (this *HTTPDNSCustomRuleDAO) ListEnabledRulesWithAppId(tx *dbs.Tx, appDbId int64) (result []*HTTPDNSCustomRule, err error) {
|
|
_, err = this.Query(tx).
|
|
State(HTTPDNSCustomRuleStateEnabled).
|
|
Attr("appId", appDbId).
|
|
Asc("priority").
|
|
AscPk().
|
|
Slice(&result).
|
|
FindAll()
|
|
return
|
|
}
|
|
|
|
func (this *HTTPDNSCustomRuleDAO) CountEnabledRulesWithDomainId(tx *dbs.Tx, domainId int64) (int64, error) {
|
|
return this.Query(tx).
|
|
State(HTTPDNSCustomRuleStateEnabled).
|
|
Attr("domainId", domainId).
|
|
Count()
|
|
}
|