person.go 3.2 KB

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