person.go 3.5 KB

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