timesheet_row.go 4.1 KB

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