person.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //go:generate reform --gofmt=false
  2. package asuModels
  3. import (
  4. "fmt"
  5. "html/template"
  6. )
  7. //reform:face
  8. type Person struct {
  9. EmpGUID int `reform:"EmpGUID" view:"readonly"`
  10. Lastname string `reform:"SurName" view:"readonly"`
  11. Firstname string `reform:"FirstName" view:"readonly"`
  12. Patronymic *string `reform:"MiddleName" view:"readonly"`
  13. Sex *int `reform:"Sex" view:"readonly"`
  14. Id int `reform:"_Id" view:"readonly"`
  15. BirthDay *string `reform:"BirthDay" view:"readonly"`
  16. Citizenship *int `reform:"Citizenship" view:"readonly"`
  17. CitizenType *string `reform:"CitizenType" view:"readonly"`
  18. Phone *string `reform:"Phone" view:"readonly"`
  19. Phone2 *string `reform:"Phone2" view:"readonly"`
  20. Phone3 *string `reform:"Phone3" view:"readonly"`
  21. formulars Formulars `reform:"-"`
  22. formularsReady bool `reform:"-"`
  23. }
  24. // TODO: Remove this "VIEW" from this model
  25. func (u Person) View_readTag(fieldName string, parent interface{}, args []interface{}) template.HTML {
  26. if u.EmpGUID == 0 {
  27. return template.HTML(fmt.Sprintf(`Не назначено`))
  28. }
  29. description := u.Lastname + " " + u.Firstname
  30. if u.Patronymic != nil {
  31. description += " " + *u.Patronymic
  32. }
  33. if u.BirthDay != nil {
  34. if len(*u.BirthDay) > len("1988-03-06") {
  35. description += " (" + (*u.BirthDay)[:len("1988-03-06")] + ")"
  36. }
  37. }
  38. return template.HTML(fmt.Sprintf(`<a href="/asu/people/%v?fullscreen=true">%v</a>`, u.EmpGUID, description))
  39. }
  40. type People []Person
  41. func (people People) Deduplicate() (deduplicated People) {
  42. personMap := map[int]*Person{}
  43. for idx, person := range people {
  44. personMap[person.EmpGUID] = &people[idx]
  45. }
  46. for _, person := range personMap {
  47. deduplicated = append(deduplicated, *person)
  48. }
  49. return
  50. }
  51. func (people People) GetEmpGUIDs() (empGUIDs []int) {
  52. for _, person := range people {
  53. empGUIDs = append(empGUIDs, person.EmpGUID)
  54. }
  55. return
  56. }
  57. func (person *Person) PrepareFormulars() *Person {
  58. var err error
  59. person.formulars, err = FormularSQL.Select(Formular{EmpGUID: person.EmpGUID})
  60. if err != nil {
  61. panic(err)
  62. }
  63. person.formularsReady = true
  64. return person
  65. }
  66. func (person Person) GetFormulars() Formulars {
  67. if !person.formularsReady {
  68. panic("person is not ready for method GetFormulars: it's required to call PrepareFormulars method, first")
  69. }
  70. return person.formulars
  71. }