unit.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package specialAPIModels
  2. import (
  3. I "devel.mephi.ru/dyokunev/wwwvoip/app/iface"
  4. "fmt"
  5. "github.com/revel/revel"
  6. "voip/app/models"
  7. )
  8. type Unit struct {
  9. common
  10. Code *string `json:",omitempty"`
  11. Name string `json:",omitempty"`
  12. Fullname *string `json:",omitempty"`
  13. Shortname *string `json:",omitempty"`
  14. ParentId *int `json:"parent_id,omitempty"`
  15. }
  16. type Units []Unit
  17. func (units *Units) IConvert(apiHost string, affiliateUnit *models.Unit) (I.Slice, error) {
  18. var convertedUnits models.Units
  19. unitCodeBySpecialAPIId := map[int]string{}
  20. unitParentSpecialAPIIdBySpecialAPIId := map[int]int{}
  21. unitCodeBySpecialAPIId[0] = "99 999 10"
  22. unitCodeMinIsBusy := map[string]bool{}
  23. unitCodeMinNext := 1
  24. unitCodeMaj := affiliateUnit.Code.Maj
  25. parentIsZeroCount := 0
  26. for _, unit := range *units {
  27. if unit.ParentId == nil {
  28. continue
  29. }
  30. if *unit.ParentId == 0 {
  31. if unit.Shortname != nil {
  32. if *unit.Shortname != "" {
  33. affiliateUnit.Shortname = *unit.Shortname
  34. break
  35. }
  36. }
  37. }
  38. }
  39. for _, unit := range *units {
  40. revel.TRACE.Printf("unit == %v", unit)
  41. if unit.ParentId == nil {
  42. continue
  43. }
  44. if unit.Code != nil {
  45. if len(*unit.Code) > 2 || len(*unit.Code) == 0 {
  46. revel.ERROR.Printf("len(unit.Code<\"%v\">) > 2: %v", unit.Code, unit)
  47. unit.Code = nil
  48. }
  49. }
  50. if unit.Code == nil {
  51. nextHundredIfRequired := func() {
  52. if unitCodeMinNext <= 99 {
  53. return
  54. }
  55. if unitCodeMaj == 99 {
  56. revel.ERROR.Panicf("unit code variants exhausted: unitCodeMinNext > 99 && unitCodeMaj == 99")
  57. }
  58. revel.WARN.Printf("unit code variants (Min part) exhausted: unitCodeMinNext > 99 && unitCodeMaj != 99")
  59. unitCodeMinNext = 0
  60. unitCodeMaj = 99
  61. unitCodeMinIsBusy = map[string]bool{}
  62. }
  63. unitCode := ""
  64. unit.Code = &unitCode
  65. if *unit.ParentId == 0 {
  66. *unit.Code = "00"
  67. } else {
  68. nextHundredIfRequired()
  69. for unitCodeMinIsBusy[fmt.Sprintf("%02d", unitCodeMinNext)] {
  70. unitCodeMinNext++
  71. nextHundredIfRequired()
  72. }
  73. *unit.Code = fmt.Sprintf("%02d", unitCodeMinNext)
  74. unitCodeMinNext++
  75. }
  76. }
  77. if len(*unit.Code) < 2 {
  78. revel.ERROR.Printf("len(unit.Code<\"%v\">) < 2: %v", unit.Code, unit)
  79. *unit.Code = fmt.Sprintf("%02s", *unit.Code)
  80. }
  81. /*if (len(*unit.Code) > 2) {
  82. revel.ERROR.Printf("len(unit.Code<\"%v\">) > 2: %v", unit.Code, unit)
  83. *unit.Code = (*unit.Code)[:2]
  84. }*/
  85. unitCodeMinIsBusy[*unit.Code] = true
  86. convertedUnit := models.Unit{
  87. CodeStr: fmt.Sprintf("%02v %03v %02v", unitCodeMaj, affiliateUnit.Code.Mid, *unit.Code),
  88. Name: unit.Name,
  89. }
  90. if unit.Fullname == nil {
  91. convertedUnit.Fullname = ""
  92. } else {
  93. convertedUnit.Fullname = *unit.Fullname
  94. }
  95. if unit.Shortname == nil {
  96. convertedUnit.Shortname = affiliateUnit.Shortname + "_" + *unit.Code
  97. } else {
  98. if *unit.Shortname == "" {
  99. convertedUnit.Shortname = affiliateUnit.Shortname + "_" + *unit.Code
  100. } else {
  101. convertedUnit.Shortname = *unit.Shortname
  102. }
  103. }
  104. convertedUnit.SetUnitCode(convertedUnit.CodeStr)
  105. convertedUnit.SourceAPIHost = &apiHost
  106. {
  107. unitId := unit.Id
  108. convertedUnit.SpecialAPIId = &unitId
  109. convertedUnit.ExtId = -(convertedUnit.Code.Mid*100000 + unitId)
  110. }
  111. revel.TRACE.Printf("convertedUnit == %v", convertedUnit)
  112. convertedUnits = append(convertedUnits, convertedUnit)
  113. unitParentSpecialAPIIdBySpecialAPIId[unit.Id] = *unit.ParentId
  114. unitCodeBySpecialAPIId[unit.Id] = convertedUnit.CodeStr
  115. if *unit.ParentId == 0 {
  116. parentIsZeroCount++
  117. }
  118. }
  119. if parentIsZeroCount != 1 {
  120. return nil, fmt.Errorf("One affiliate unit tried to insert insert not one affiliate unit into voip.mephi.ru: parentIsZeroCount == %v (should be == 1)", parentIsZeroCount)
  121. }
  122. for idx, convertedUnit := range convertedUnits {
  123. convertedUnits[idx].ParentsCodeStr = unitCodeBySpecialAPIId[unitParentSpecialAPIIdBySpecialAPIId[*convertedUnit.SpecialAPIId]]
  124. if convertedUnits[idx].ParentsCodeStr == "" {
  125. revel.ERROR.Printf("parent of unit %v doesn't exist", convertedUnits[idx])
  126. convertedUnits[idx].ParentsCodeStr = affiliateUnit.CodeStr
  127. }
  128. }
  129. return convertedUnits, nil
  130. }