Browse Source

Moaar auxiliary methods

Dmitry Yu Okunev 5 years ago
parent
commit
e3f6fd8a35
3 changed files with 80 additions and 2 deletions
  1. 49 0
      formular.go
  2. 10 0
      person.go
  3. 21 2
      unit.go

+ 49 - 0
formular.go

@@ -141,6 +141,55 @@ func (f Formular) IsActive() bool {
 
 type Formulars []Formular
 
+func (formulars Formulars) ToMap() map[int]*Formular {
+	formularMap := map[int]*Formular{}
+
+	for idx, formular := range formulars {
+		formularMap[formular.StfCardId] = &formulars[idx]
+	}
+
+	return formularMap
+}
+
+func (formulars Formulars) GetStfCardIds() (stfCardIds []int) {
+	for _, formular := range formulars {
+		stfCardIds = append(stfCardIds, formular.StfCardId)
+	}
+
+	return
+}
+
+func (formulars Formulars) GetEmpGUIDs() (empGUIDs []int) {
+	empGUIDMap := map[int]bool{}
+
+	for _, formular := range formulars {
+		empGUIDMap[formular.EmpGUID] = true
+	}
+	for empGUID, _ := range empGUIDMap {
+		empGUIDs = append(empGUIDs, empGUID)
+	}
+
+	return
+}
+
+func (formulars Formulars) PreparePeople() Formulars {
+	ids := formulars.GetEmpGUIDs()
+
+	people, err := PersonSQL.Select("EmpGUID IN (?)", ids)
+	if err != nil && err != sql.ErrNoRows {
+		panic(err)
+	}
+
+	personMap := People(people).ToMap()
+
+	for idx, formular := range formulars {
+		formulars[idx].person = *personMap[formular.EmpGUID]
+		formulars[idx].personReady = true
+	}
+
+	return formulars
+}
+
 func (formulars Formulars) GetPeople() (people People) {
 	for _, formular := range formulars {
 		people = append(people, formular.GetPerson())

+ 10 - 0
person.go

@@ -47,6 +47,16 @@ func (u Person) View_readTag(fieldName string, parent interface{}, args []interf
 
 type People []Person
 
+func (people People) ToMap() map[int]*Person {
+	personMap := map[int]*Person{}
+
+	for idx, person := range people {
+		personMap[person.EmpGUID] = &people[idx]
+	}
+
+	return personMap
+}
+
 func (people People) Deduplicate() (deduplicated People) {
 	personMap := map[int]*Person{}
 

+ 21 - 2
unit.go

@@ -99,13 +99,19 @@ func (u Unit) View_readTag(fieldName string, parent interface{}, args []interfac
 
 type Units []Unit
 
-func (units Units) calculateTree(strict bool) {
+func (units Units) ToMap() map[int]*Unit {
 	unitMap := map[int]*Unit{}
 
 	for idx, unit := range units {
 		unitMap[unit.Id] = &units[idx]
 	}
 
+	return unitMap
+}
+
+func (units Units) calculateTree(strict bool) {
+	unitMap := units.ToMap()
+
 	for idx, _ := range units {
 		unit := &units[idx]
 
@@ -178,8 +184,21 @@ func (u Unit) GetFormulars() Formulars {
 }
 
 func (units Units) PrepareFormulars() Units {
+	ids := units.GetUnitIds()
+
+	formulars, err := FormularSQL.Select("OrgDiv IN (?)", ids)
+	if err != nil && err != sql.ErrNoRows {
+		panic(err)
+	}
+
+	unitsMap := units.ToMap()
+
+	for _, formular := range formulars {
+		(*unitsMap[formular.UnitId]).formulars = append((*unitsMap[formular.UnitId]).formulars, formular)
+	}
+
 	for idx, _ := range units {
-		units[idx].PrepareFormulars()
+		units[idx].formularsReady = true
 	}
 
 	return units