formular.go 6.4 KB

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