subcontact.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package models
  15. import (
  16. "yunion.io/x/pkg/tristate"
  17. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  18. )
  19. type SSubContactManager struct {
  20. db.SStandaloneResourceBaseManager
  21. }
  22. // +onecloud:swagger-gen-ignore
  23. type SSubContact struct {
  24. db.SStandaloneResourceBase
  25. // id of receiver user
  26. ReceiverID string `width:"128" nullable:"false" index:"true"`
  27. Type string `width:"16" nullable:"false" index:"true"`
  28. Contact string `width:"128" nullable:"false"`
  29. ParentContactType string `width:"16" nullable:"false"`
  30. Enabled tristate.TriState `default:"false"`
  31. Verified tristate.TriState `default:"false"`
  32. VerifiedNote string `width:"1024"`
  33. }
  34. var SubContactManager *SSubContactManager
  35. var (
  36. vTrue = true
  37. vFalse = false
  38. pTrue = &vTrue
  39. pFalse = &vFalse
  40. )
  41. func init() {
  42. SubContactManager = &SSubContactManager{
  43. SStandaloneResourceBaseManager: db.NewStandaloneResourceBaseManager(
  44. SSubContact{},
  45. "subcontacts_tbl",
  46. "subcontact",
  47. "subcontacts",
  48. ),
  49. }
  50. SubContactManager.SetVirtualObject(SubContactManager)
  51. }
  52. func (scm *SSubContactManager) fetchMapByReceiverID(receiverID string) (map[string]*SSubContact, error) {
  53. q := scm.Query().Equals("receiver_id", receiverID)
  54. scontacts := make([]SSubContact, 0, 3)
  55. err := db.FetchModelObjects(scm, q, &scontacts)
  56. if err != nil {
  57. return nil, err
  58. }
  59. ret := make(map[string]*SSubContact, len(scontacts))
  60. for i := range scontacts {
  61. ret[scontacts[i].Type] = &scontacts[i]
  62. }
  63. return ret, nil
  64. }
  65. func (sc *SSubContact) Enable() error {
  66. return sc.Update(nil, pTrue, nil)
  67. }
  68. func (sc *SSubContact) Disable() error {
  69. return sc.Update(nil, pFalse, nil)
  70. }
  71. func (sc *SSubContact) Verify() error {
  72. return sc.Update(nil, nil, pTrue)
  73. }
  74. func (sc *SSubContact) Disverify() error {
  75. return sc.Update(nil, nil, pFalse)
  76. }
  77. func (sc *SSubContact) Update(contact *string, enabled *bool, verified *bool) error {
  78. _, err := db.Update(sc, func() error {
  79. if contact != nil {
  80. sc.Contact = *contact
  81. }
  82. if enabled != nil {
  83. sc.Enabled = tristate.NewFromBool(*enabled)
  84. }
  85. if verified != nil {
  86. sc.Verified = tristate.NewFromBool(*verified)
  87. }
  88. return nil
  89. })
  90. return err
  91. }