formular.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package asuModels
  2. //go:generate reform --gofmt=false
  3. import (
  4. "database/sql"
  5. "fmt"
  6. "github.com/revel/revel"
  7. "github.com/xaionaro-go/extime"
  8. "html/template"
  9. "strings"
  10. )
  11. //reform:formular
  12. type Formular struct {
  13. EmpGUID int `reform:"EmpGUID" view:"readonly"`
  14. StfCardId int `reform:"StfCardId" view:"readonly"`
  15. PersonnelNumber int `reform:"TabN" view:"readonly"`
  16. FormularType string `reform:"Tip_formular" view:"readonly"`
  17. StartDate *extime.Date `reform:"StartDate" view:"readonly"`
  18. EndDate *extime.Date `reform:"EndDate" view:"readonly"`
  19. AppointmentName string `reform:"Name" view:"readonly"`
  20. AppointmentShortName *string `reform:"Prof_BriefName" view:"readonly"`
  21. WorkStart *extime.Date `reform:"Start_work" view:"readonly"`
  22. Category string `reform:"Category" view:"readonly"`
  23. UnitName string `reform:"Name_podr" view:"readonly"`
  24. UnitShortName *string `reform:"Podr_BriefName" view:"readonly"`
  25. UnitCode string `reform:"Code" view:"readonly"`
  26. ChiefName *string `reform:"ChiefName" view:"readonly"`
  27. Rate *float64 `reform:"RateAmount" view:"readonly"`
  28. IsHead *bool `reform:"IsHead" view:"readonly"`
  29. UnitId int `reform:"OrgDiv" view:"readonly"`
  30. PersNumber *int `reform:"PersNumber" view:"readonly"`
  31. unit Unit `reform:"-"`
  32. unitReady bool `reform:"-"`
  33. person Person `reform:"-"`
  34. personReady bool `reform:"-"`
  35. isActive bool `reform:"-"`
  36. isActiveReady bool `reform:"-"`
  37. //FormularTypeCode int `reform:"Cod_Tip_formular"`
  38. //CategoryCode int `reform:"Cod_Catgory"`
  39. //WorkEnd *string `reform:"End_work"`
  40. //Profession int `reform:"Profession"`
  41. //Lastname string `reform:"SurName"`
  42. //Firstname string `reform:"FirstName"`
  43. //Patronymic string `reform:"MiddleName"`
  44. //OrgInn string `reform:"OrgInn"`
  45. //DivInn string `reform:"DivInn"`
  46. //Sex int `reform:"Sex"`
  47. //WorkState int `reform:"WorkState"`
  48. //ProfKvalLevel string `reform:"ProfKvalLevel"`
  49. //ProfKvalGroup int `reform:"ProfKvalGroup"`
  50. //EmpCardId string `reform:"EmpCardId"`
  51. //VacDays *int `reform:"VacDays"`
  52. //ExtraVacDays *int `reform:"ExtraVacDays"`
  53. }
  54. func (f *Formular) AfterFind() error {
  55. if f.UnitShortName != nil {
  56. *f.UnitShortName = strings.Trim(*f.UnitShortName, " ")
  57. }
  58. f.UnitName = strings.Trim(f.UnitName, " ")
  59. return nil
  60. }
  61. func (sql Formular) ActiveOnly() *FormularScope {
  62. return sql.Scope().ActiveOnly()
  63. }
  64. func (sql *FormularScope) ActiveOnly() *FormularScope {
  65. return sql.Where("(StartDate IS NULL OR StartDate < NOW()) AND (EndDate IS NULL OR EndDate+24*3600 > NOW())")
  66. }
  67. // TODO: Remove this "VIEW" from this model
  68. func (f Formular) View_readTag(fieldName string, parent interface{}, args []interface{}) template.HTML {
  69. unitName := f.UnitName
  70. if f.UnitShortName != nil {
  71. unitName = *f.UnitShortName + " (" + unitName + ")"
  72. }
  73. return template.HTML(fmt.Sprintf(`<a href="/asu/formulars/%v?fullscreen=true">%v — %v: [%v] %v — %v</a>`, f.StfCardId, f.StartDate, f.EndDate, f.UnitCode, f.AppointmentName, unitName))
  74. }
  75. func (f *Formular) PrepareUnit() *Formular {
  76. var err error
  77. f.unit, err = UnitSQL.First(Unit{Id: f.UnitId})
  78. if err != nil && err != sql.ErrNoRows {
  79. revel.ERROR.Printf("Got error: %v", err.Error())
  80. return f
  81. }
  82. f.unitReady = true
  83. return f
  84. }
  85. func (f Formular) IsUnitReady() bool {
  86. return f.unitReady
  87. }
  88. func (f Formular) GetUnit() Unit {
  89. if !f.unitReady {
  90. panic("formular is not ready for method GetUnit: it's required to call PrepareUnit method, first")
  91. }
  92. return f.unit
  93. }
  94. func (f *Formular) PreparePerson() *Formular {
  95. var err error
  96. f.person, err = PersonSQL.First(Person{EmpGUID: f.EmpGUID})
  97. if err != nil && err != sql.ErrNoRows {
  98. revel.ERROR.Printf("Got error: %v", err.Error())
  99. return f
  100. }
  101. f.personReady = true
  102. return f
  103. }
  104. func (f Formular) IsPersonReady() bool {
  105. return f.personReady
  106. }
  107. func (f Formular) GetPerson() Person {
  108. if !f.personReady {
  109. panic("formular is not ready for method GetPerson: it's required to call PreparePerson method, first")
  110. }
  111. return f.person
  112. }
  113. func (f *Formular) PrepareIsActive() *Formular {
  114. now := extime.Now()
  115. startIsGood := false
  116. endIsGood := false
  117. if f.StartDate != nil {
  118. startIsGood = f.StartDate.UnixNano() <= now.UnixNano()
  119. }
  120. if f.EndDate == nil {
  121. endIsGood = true
  122. } else {
  123. endIsGood = f.EndDate.UnixNano()+24*3600*1000*1000*1000 >= now.UnixNano()
  124. }
  125. f.isActive = startIsGood && endIsGood
  126. f.isActiveReady = true
  127. return f
  128. }
  129. func (f Formular) IsActive() bool {
  130. if !f.isActiveReady {
  131. panic("formular is not ready for method IsActive: it's required to call PrepareIsActive method, first")
  132. }
  133. return f.isActive
  134. }
  135. type Formulars []Formular
  136. func (formulars Formulars) ToMap() map[int]*Formular {
  137. formularMap := map[int]*Formular{}
  138. for idx, formular := range formulars {
  139. formularMap[formular.StfCardId] = &formulars[idx]
  140. }
  141. return formularMap
  142. }
  143. func (formulars Formulars) GetStfCardIds() (stfCardIds []int) {
  144. for _, formular := range formulars {
  145. stfCardIds = append(stfCardIds, formular.StfCardId)
  146. }
  147. return
  148. }
  149. func (formulars Formulars) GetEmpGUIDs() (empGUIDs []int) {
  150. empGUIDMap := map[int]bool{}
  151. for _, formular := range formulars {
  152. empGUIDMap[formular.EmpGUID] = true
  153. }
  154. for empGUID := range empGUIDMap {
  155. empGUIDs = append(empGUIDs, empGUID)
  156. }
  157. return
  158. }
  159. func (formulars Formulars) PreparePeople() Formulars {
  160. ids := formulars.GetEmpGUIDs()
  161. if len(ids) == 0 {
  162. return formulars
  163. }
  164. people, err := PersonSQL.Select("EmpGUID IN (?)", ids)
  165. if err != nil && err != sql.ErrNoRows {
  166. panic(err)
  167. }
  168. personMap := People(people).ToMap()
  169. for idx, formular := range formulars {
  170. formulars[idx].person = *personMap[formular.EmpGUID]
  171. formulars[idx].personReady = true
  172. }
  173. return formulars
  174. }
  175. func (formulars Formulars) GetPeople() (people People) {
  176. for _, formular := range formulars {
  177. people = append(people, formular.GetPerson())
  178. }
  179. return
  180. }