version.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package task
  2. import (
  3. "dsbqj-admin/model/mongo/version"
  4. "dsbqj-admin/pkg/util"
  5. "sync"
  6. "github.com/kamva/mgm/v3"
  7. "go.mongodb.org/mongo-driver/bson"
  8. )
  9. var VersionTask *Versions
  10. type Versions struct {
  11. sync.Mutex
  12. VChannels util.SafeMap[string, *ChannelVersions]
  13. }
  14. type ChannelVersions struct {
  15. VList util.SafeArray[*version.Version]
  16. VMap util.SafeMap[string, *version.Version]
  17. }
  18. func VersionInit() *Versions {
  19. VersionTask = new(Versions)
  20. return VersionTask
  21. }
  22. func (this *Versions) Exec() {
  23. var versions = make([]*version.Version, 0)
  24. err := mgm.Coll(&version.Version{}).SimpleFind(&versions, bson.M{})
  25. if err == nil {
  26. this.VChannels.Clear()
  27. for _, v := range versions {
  28. if _, ok := this.VChannels.Get(v.Channel); !ok {
  29. this.VChannels.Set(v.Channel, new(ChannelVersions))
  30. }
  31. channelVersion, _ := this.VChannels.Get(v.Channel)
  32. channelVersion.VList.Push(v)
  33. channelVersion.VMap.Set(v.Version, v)
  34. }
  35. }
  36. }
  37. func (this *Versions) Check(version string, channel string) *version.Version {
  38. channelVersions, ok := this.VChannels.Get(channel)
  39. if !ok { // 遍历列表
  40. return nil
  41. }
  42. data, ok := channelVersions.VMap.Get(version)
  43. if !ok {
  44. for _, v := range channelVersions.VList.View() {
  45. if v.Default && v.Version == version && v.Channel == channel {
  46. data = v
  47. break
  48. }
  49. }
  50. }
  51. return data
  52. }
  53. func (this *Versions) Servers(channel string) []*version.Version {
  54. var res = make([]*version.Version, 0)
  55. if channelVersions, ok := this.VChannels.Get(channel); ok {
  56. for _, v := range channelVersions.VList.View() {
  57. if v.Channel == channel {
  58. res = append(res, v)
  59. }
  60. }
  61. }
  62. return res
  63. }