//go:generate reform --gofmt=false . package asuModels import ( "database/sql" "fmt" "html/template" ) //reform:face type Person struct { EmpGUID int `reform:"EmpGUID" view:"readonly"` Lastname string `reform:"SurName" view:"readonly"` Firstname string `reform:"FirstName" view:"readonly"` Patronymic *string `reform:"MiddleName" view:"readonly"` Sex *int `reform:"Sex" view:"readonly"` Id int `reform:"_Id" view:"readonly"` BirthDay *string `reform:"BirthDay" view:"readonly"` Citizenship *int `reform:"Citizenship" view:"readonly"` CitizenType *string `reform:"CitizenType" view:"readonly"` Phone *string `reform:"Phone" view:"readonly"` Phone2 *string `reform:"Phone2" view:"readonly"` Phone3 *string `reform:"Phone3" view:"readonly"` formulars Formulars `reform:"-"` formularsReady bool `reform:"-"` } // TODO: Remove this "VIEW" from this model func (u Person) View_readTag(fieldName string, parent interface{}, args []interface{}) template.HTML { if u.EmpGUID == 0 { return template.HTML(fmt.Sprintf(`Не назначено`)) } description := u.Lastname + " " + u.Firstname if u.Patronymic != nil { description += " " + *u.Patronymic } if u.BirthDay != nil { if len(*u.BirthDay) > len("1988-03-06") { description += " (" + (*u.BirthDay)[:len("1988-03-06")] + ")" } } return template.HTML(fmt.Sprintf(`%v`, u.EmpGUID, description)) } 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{} for idx, person := range people { personMap[person.EmpGUID] = &people[idx] } for _, person := range personMap { deduplicated = append(deduplicated, *person) } return } func (people People) GetEmpGUIDs() (empGUIDs []int) { for _, person := range people { empGUIDs = append(empGUIDs, person.EmpGUID) } return } func (person *Person) PrepareFormulars() *Person { var err error person.formulars, err = FormularSQL.Select(Formular{EmpGUID: person.EmpGUID}) if err != nil && err != sql.ErrNoRows { panic(err) } person.formularsReady = true return person } func (person Person) IsFormularsReady() bool { return person.formularsReady } func (person Person) GetFormulars() Formulars { if !person.formularsReady { panic("person is not ready for method GetFormulars: it's required to call PrepareFormulars method, first") } return person.formulars } func (person Person) GetFormularsPtr() *Formulars { return &person.formulars } func (people People) PrepareFormulars() People { ids := people.GetEmpGUIDs() if len(ids) == 0 { return people } formulars, err := FormularSQL.Select("EmpGUID IN (?)", ids) if err != nil && err != sql.ErrNoRows { panic(err) } personMap := People(people).ToMap() for _, formular := range formulars { (*personMap[formular.EmpGUID]).formulars = append((*personMap[formular.EmpGUID]).formulars, formular) } for idx := range people { people[idx].formularsReady = true } return people }