person.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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) Deduplicate() (deduplicated People) {
  43. personMap := map[int]*Person{}
  44. for idx, person := range people {
  45. personMap[person.EmpGUID] = &people[idx]
  46. }
  47. for _, person := range personMap {
  48. deduplicated = append(deduplicated, *person)
  49. }
  50. return
  51. }
  52. func (people People) GetEmpGUIDs() (empGUIDs []int) {
  53. for _, person := range people {
  54. empGUIDs = append(empGUIDs, person.EmpGUID)
  55. }
  56. return
  57. }
  58. func (person *Person) PrepareFormulars() *Person {
  59. var err error
  60. person.formulars, err = FormularSQL.Select(Formular{EmpGUID: person.EmpGUID})
  61. if err != nil && err != sql.ErrNoRows {
  62. panic(err)
  63. }
  64. person.formularsReady = true
  65. return person
  66. }
  67. func (person Person) GetFormulars() Formulars {
  68. if !person.formularsReady {
  69. panic("person is not ready for method GetFormulars: it's required to call PrepareFormulars method, first")
  70. }
  71. return person.formulars
  72. }