timesheet_row.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //go:generate reform --gofmt=false
  2. //go:generate easyjson -all timesheet_row.go
  3. package asuModels
  4. import (
  5. "database/sql"
  6. "github.com/xaionaro-go/extime"
  7. "reflect"
  8. )
  9. //reform:table_view
  10. type TimesheetRow struct {
  11. Id int `reform:"Id"`
  12. ADate extime.Date `reform:"ADate"`
  13. ODate extime.Date `reform:"ODate"`
  14. MonthYear extime.Date `reform:"MonthYear"`
  15. EmpGUID int `reform:"EmpGuid"`
  16. StfCardId int `reform:"StaffCardId"`
  17. TimeUseCode string `reform:"TimeUseCode"`
  18. Days int `reform:"Days"`
  19. Hours *int `reform:"Hours"`
  20. Author int `reform:"Author"`
  21. ParentId int `reform:"ParentId"`
  22. TableId *int `reform:"TableId"`
  23. TableState int `reform:"TableState"`
  24. State int `reform:"State"`
  25. AstDate int `reform:"AstDate"`
  26. SID int `reform:"SID"`
  27. Nullified *int `reform:"Nullified"`
  28. ParentState int `reform:"ParentState"`
  29. BlockerId *int `reform:"BlockerId"`
  30. OrgInn *string `reform:"OrgInn"`
  31. DivInn *string `reform:"DivInn"`
  32. TabN *int `reform:"TabN"`
  33. ScGUID *int `reform:"scGUID"`
  34. Description string `reform:"Description"`
  35. TimeUseClass int `reform:"TimeUseClass"`
  36. RequiredHours int `reform:"RequiredHours"`
  37. person Person `reform:"-"`
  38. personReady bool `reform:"-"`
  39. formular Formular `reform:"-"`
  40. formularReady bool `reform:"-"`
  41. }
  42. func (t *TimesheetRow) PreparePerson() *TimesheetRow {
  43. var err error
  44. t.person, err = PersonSQL.First(Person{EmpGUID: t.EmpGUID})
  45. if err != nil && err != sql.ErrNoRows {
  46. panic(err)
  47. return t
  48. }
  49. t.personReady = true
  50. return t
  51. }
  52. func (t TimesheetRow) IsPersonReady() bool {
  53. return t.personReady
  54. }
  55. func (t TimesheetRow) GetPerson() Person {
  56. if !t.personReady {
  57. panic("formular is not ready for method GetPerson: it's required to call PreparePerson method, first")
  58. }
  59. return t.person
  60. }
  61. func (t *TimesheetRow) PrepareFormular() *TimesheetRow {
  62. var err error
  63. t.formular, err = FormularSQL.First(Formular{StfCardId: t.StfCardId})
  64. if err != nil && err != sql.ErrNoRows {
  65. panic(err)
  66. return t
  67. }
  68. t.formularReady = true
  69. return t
  70. }
  71. func (t TimesheetRow) IsFormularReady() bool {
  72. return t.formularReady
  73. }
  74. func (t TimesheetRow) GetFormular() Formular {
  75. if !t.formularReady {
  76. panic("formular is not ready for method GetFormular: it's required to call PrepareFormular method, first")
  77. }
  78. return t.formular
  79. }
  80. func (timesheetRow TimesheetRow) Flush() {
  81. timesheetRow.formular = Formular{}
  82. timesheetRow.formularReady = false
  83. timesheetRow.person = Person{}
  84. timesheetRow.personReady = false
  85. }
  86. func (timesheetRow TimesheetRow) IsEqualsTo(compareTo TimesheetRow) bool {
  87. timesheetRow.Flush()
  88. compareTo.Flush()
  89. return reflect.DeepEqual(timesheetRow, compareTo)
  90. }
  91. type TimesheetRows []TimesheetRow
  92. func (timesheetRows TimesheetRows) GetEmpGUIDs() (empGUIDs []int) {
  93. empGUIDMap := map[int]bool{}
  94. for _, timesheetRow := range timesheetRows {
  95. empGUIDMap[timesheetRow.EmpGUID] = true
  96. }
  97. for empGUID, _ := range empGUIDMap {
  98. empGUIDs = append(empGUIDs, empGUID)
  99. }
  100. return
  101. }
  102. func (timesheetRows TimesheetRows) PrepareFormulars(activeOnly bool) TimesheetRows {
  103. ids := timesheetRows.GetEmpGUIDs()
  104. scope := FormularSQL.Scope()
  105. if activeOnly {
  106. scope = scope.ActiveOnly()
  107. }
  108. formulars, err := scope.Select("EmpGUID IN (?)", ids)
  109. if err != nil && err != sql.ErrNoRows {
  110. panic(err)
  111. }
  112. for range formulars {
  113. panic("Not implemented, yet")
  114. }
  115. for idx, _ := range timesheetRows {
  116. timesheetRows[idx].formularReady = true
  117. }
  118. return timesheetRows
  119. }
  120. func (timesheetRows TimesheetRows) GetFormulars() (formulars Formulars) {
  121. for _, timesheetRow := range timesheetRows {
  122. formulars = append(formulars, timesheetRow.GetFormular())
  123. }
  124. return
  125. }