package specialAPIModels import ( I "devel.mephi.ru/dyokunev/wwwvoip/app/iface" "fmt" "github.com/revel/revel" "voip/app/models" ) type Unit struct { common Code *string `json:",omitempty"` Name string `json:",omitempty"` Fullname *string `json:",omitempty"` Shortname *string `json:",omitempty"` ParentId *int `json:"parent_id,omitempty"` } type Units []Unit func (units *Units) IConvert(apiHost string, affiliateUnit *models.Unit) (I.Slice, error) { var convertedUnits models.Units unitCodeBySpecialAPIId := map[int]string{} unitParentSpecialAPIIdBySpecialAPIId := map[int]int{} unitCodeBySpecialAPIId[0] = "99 999 10" unitCodeMinIsBusy := map[string]bool{} unitCodeMinNext := 1 unitCodeMaj := affiliateUnit.Code.Maj parentIsZeroCount := 0 for _, unit := range *units { if unit.ParentId == nil { continue } if *unit.ParentId == 0 { if unit.Shortname != nil { if *unit.Shortname != "" { affiliateUnit.Shortname = *unit.Shortname break } } } } for _, unit := range *units { revel.TRACE.Printf("unit == %v", unit) if unit.ParentId == nil { continue } if unit.Code != nil { if len(*unit.Code) > 2 || len(*unit.Code) == 0 { revel.ERROR.Printf("len(unit.Code<\"%v\">) > 2: %v", unit.Code, unit) unit.Code = nil } } if unit.Code == nil { nextHundredIfRequired := func() { if unitCodeMinNext <= 99 { return } if unitCodeMaj == 99 { revel.ERROR.Panicf("unit code variants exhausted: unitCodeMinNext > 99 && unitCodeMaj == 99") } revel.WARN.Printf("unit code variants (Min part) exhausted: unitCodeMinNext > 99 && unitCodeMaj != 99") unitCodeMinNext = 0 unitCodeMaj = 99 unitCodeMinIsBusy = map[string]bool{} } unitCode := "" unit.Code = &unitCode if *unit.ParentId == 0 { *unit.Code = "00" } else { nextHundredIfRequired() for unitCodeMinIsBusy[fmt.Sprintf("%02d", unitCodeMinNext)] { unitCodeMinNext++ nextHundredIfRequired() } *unit.Code = fmt.Sprintf("%02d", unitCodeMinNext) unitCodeMinNext++ } } if len(*unit.Code) < 2 { revel.ERROR.Printf("len(unit.Code<\"%v\">) < 2: %v", unit.Code, unit) *unit.Code = fmt.Sprintf("%02s", *unit.Code) } /*if (len(*unit.Code) > 2) { revel.ERROR.Printf("len(unit.Code<\"%v\">) > 2: %v", unit.Code, unit) *unit.Code = (*unit.Code)[:2] }*/ unitCodeMinIsBusy[*unit.Code] = true convertedUnit := models.Unit{ CodeStr: fmt.Sprintf("%02v %03v %02v", unitCodeMaj, affiliateUnit.Code.Mid, *unit.Code), Name: unit.Name, } if unit.Fullname == nil { convertedUnit.Fullname = "" } else { convertedUnit.Fullname = *unit.Fullname } if unit.Shortname == nil { convertedUnit.Shortname = affiliateUnit.Shortname + "_" + *unit.Code } else { if *unit.Shortname == "" { convertedUnit.Shortname = affiliateUnit.Shortname + "_" + *unit.Code } else { convertedUnit.Shortname = *unit.Shortname } } convertedUnit.SetUnitCode(convertedUnit.CodeStr) convertedUnit.SourceAPIHost = &apiHost { unitId := unit.Id convertedUnit.SpecialAPIId = &unitId convertedUnit.ExtId = -(convertedUnit.Code.Mid*100000 + unitId) } revel.TRACE.Printf("convertedUnit == %v", convertedUnit) convertedUnits = append(convertedUnits, convertedUnit) unitParentSpecialAPIIdBySpecialAPIId[unit.Id] = *unit.ParentId unitCodeBySpecialAPIId[unit.Id] = convertedUnit.CodeStr if *unit.ParentId == 0 { parentIsZeroCount++ } } if parentIsZeroCount != 1 { 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) } for idx, convertedUnit := range convertedUnits { convertedUnits[idx].ParentsCodeStr = unitCodeBySpecialAPIId[unitParentSpecialAPIIdBySpecialAPIId[*convertedUnit.SpecialAPIId]] if convertedUnits[idx].ParentsCodeStr == "" { revel.ERROR.Printf("parent of unit %v doesn't exist", convertedUnits[idx]) convertedUnits[idx].ParentsCodeStr = affiliateUnit.CodeStr } } return convertedUnits, nil }