|
| 1 | +package models |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + _ "github.com/go-sql-driver/mysql" |
| 6 | + "github.com/iwind/TeaGo/Tea" |
| 7 | + "github.com/iwind/TeaGo/dbs" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +type AuthorityKeyDAO dbs.DAO |
| 12 | + |
| 13 | +func NewAuthorityKeyDAO() *AuthorityKeyDAO { |
| 14 | + return dbs.NewDAO(&AuthorityKeyDAO{ |
| 15 | + DAOObject: dbs.DAOObject{ |
| 16 | + DB: Tea.Env, |
| 17 | + Table: "edgeAuthorityKeys", |
| 18 | + Model: new(AuthorityKey), |
| 19 | + PkName: "id", |
| 20 | + }, |
| 21 | + }).(*AuthorityKeyDAO) |
| 22 | +} |
| 23 | + |
| 24 | +var SharedAuthorityKeyDAO *AuthorityKeyDAO |
| 25 | + |
| 26 | +func init() { |
| 27 | + dbs.OnReady(func() { |
| 28 | + SharedAuthorityKeyDAO = NewAuthorityKeyDAO() |
| 29 | + }) |
| 30 | +} |
| 31 | + |
| 32 | +// UpdateKey 设置Key |
| 33 | +func (this *AuthorityKeyDAO) UpdateKey(tx *dbs.Tx, value string, dayFrom string, dayTo string, hostname string, macAddresses []string) error { |
| 34 | + one, err := this.Query(tx). |
| 35 | + AscPk(). |
| 36 | + Find() |
| 37 | + if err != nil { |
| 38 | + return err |
| 39 | + } |
| 40 | + op := NewAuthorityKeyOperator() |
| 41 | + if one != nil { |
| 42 | + op.Id = one.(*AuthorityKey).Id |
| 43 | + } |
| 44 | + op.Value = value |
| 45 | + op.DayFrom = dayFrom |
| 46 | + op.DayTo = dayTo |
| 47 | + op.Hostname = hostname |
| 48 | + |
| 49 | + if len(macAddresses) == 0 { |
| 50 | + macAddresses = []string{} |
| 51 | + } |
| 52 | + macAddressesJSON, err := json.Marshal(macAddresses) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + op.MacAddresses = macAddressesJSON |
| 58 | + op.UpdatedAt = time.Now().Unix() |
| 59 | + |
| 60 | + return this.Save(tx, op) |
| 61 | +} |
| 62 | + |
| 63 | +// ReadKey 读取Key |
| 64 | +func (this *AuthorityKeyDAO) ReadKey(tx *dbs.Tx) (key *AuthorityKey, err error) { |
| 65 | + one, err := this.Query(tx). |
| 66 | + AscPk(). |
| 67 | + Find() |
| 68 | + if err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + if one == nil { |
| 72 | + return nil, nil |
| 73 | + } |
| 74 | + return one.(*AuthorityKey), nil |
| 75 | +} |
| 76 | + |
| 77 | +// ResetKey 重置Key |
| 78 | +func (this *AuthorityKeyDAO) ResetKey(tx *dbs.Tx) error { |
| 79 | + _, err := this.Query(tx). |
| 80 | + Delete() |
| 81 | + return err |
| 82 | +} |
0 commit comments