unit.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. //go:generate reform --gofmt=false
  2. package asuModels
  3. import (
  4. "database/sql"
  5. "fmt"
  6. "github.com/xaionaro-go/extime"
  7. "html/template"
  8. "strings"
  9. "reflect"
  10. )
  11. //reform:unit
  12. type Unit struct {
  13. Id int `reform:"id" view:"readonly"`
  14. OrgInn string `reform:"OrgInn" view:"readonly"`
  15. Name *string `reform:"Name" view:"readonly"`
  16. ParentId *int `reform:"ParentId" view:"readonly"`
  17. LevelIdx int `reform:"LevelIdx" view:"readonly"`
  18. IsHidden int `reform:"IsHidden" view:"readonly"`
  19. OKVED *string `reform:"OKVED" view:"readonly"`
  20. BriefName *string `reform:"BriefName" view:"readonly"`
  21. CreateOrderNo *string `reform:"CreateOrderNo" view:"readonly"`
  22. CreateOrderDate *extime.Time `reform:"CreateOrderDate" view:"readonly"`
  23. CloseOrderNo *string `reform:"CloseOrderNo" view:"readonly"`
  24. CloseOrderDate *extime.Time `reform:"CloseOrderDate" view:"readonly"`
  25. ChiefName *string `reform:"ChiefName" view:"readonly"`
  26. PersNumber *int `reform:"PersNumber" view:"readonly"`
  27. Code string `reform:"Code" view:"readonly"`
  28. SortNumber *int `reform:"SortNumber" view:"readonly"`
  29. ChiefTabN *string `reform:"ChiefTabN" view:"readonly"`
  30. children []*Unit `reform:"-"`
  31. childrenReady bool `reform:"-"`
  32. formulars Formulars `reform:"-"`
  33. formularsReady bool `reform:"-"`
  34. isActive bool `reform:"-"`
  35. isActiveReady bool `reform:"-"`
  36. }
  37. func (u *Unit) AfterFind() error {
  38. if u.Name != nil {
  39. *u.Name = strings.Trim(*u.Name, " ")
  40. }
  41. if u.BriefName != nil {
  42. *u.BriefName = strings.Trim(*u.BriefName, " ")
  43. }
  44. return nil
  45. }
  46. func (sql Unit) ActiveOnly() *UnitScope {
  47. return sql.Scope().ActiveOnly()
  48. }
  49. func (sql *UnitScope) ActiveOnly() *UnitScope {
  50. cond := "((CreateOrderDate IS NULL OR CreateOrderDate < NOW()) AND (CloseOrderDate IS NULL OR CloseOrderDate+24*3600 > NOW()))"
  51. return sql.Where(cond+" OR (Id IN (SELECT ParentId FROM unit WHERE "+cond+"))")
  52. }
  53. func (sql Unit) RealOnly() *UnitScope {
  54. return sql.Scope().RealOnly()
  55. }
  56. func (sql *UnitScope) RealOnly() *UnitScope {
  57. return sql.Where("LENGTH(Code) = 6")
  58. }
  59. func (sql Unit) UnhiddenOnly() *UnitScope {
  60. return sql.Scope().UnhiddenOnly()
  61. }
  62. func (sql *UnitScope) UnhiddenOnly() *UnitScope {
  63. return sql.Where("IsHidden=1")
  64. }
  65. func (sql Unit) NonEmptyOnly() *UnitScope {
  66. return sql.Scope().UnhiddenOnly()
  67. }
  68. func (sql *UnitScope) NonEmptyOnly() *UnitScope {
  69. return sql.Where("Id IN (SELECT * FROM formular WHERE (StartDate IS NULL OR StartDate < NOW()) AND (EndDate IS NULL OR EndDate+24*3600 > NOW()))")
  70. }
  71. // TODO: Remove this "VIEW" from this model
  72. func (u Unit) View_readTag(fieldName string, parent interface{}, args []interface{}) template.HTML {
  73. if u.Id == 0 {
  74. return template.HTML(fmt.Sprintf(`Не назначено`))
  75. }
  76. name := ""
  77. if u.BriefName != nil {
  78. name = *u.BriefName
  79. }
  80. if u.Name != nil {
  81. name += " (" + *u.Name + ")"
  82. }
  83. return template.HTML(fmt.Sprintf(`<a href="/asu/unitsByID/%v?fullscreen=true">%v — %v</a>`, u.Id, u.Code, name))
  84. }
  85. type Units []Unit
  86. func (units Units) calculateTree(strict bool) {
  87. unitMap := map[int]*Unit{}
  88. for idx, unit := range units {
  89. unitMap[unit.Id] = &units[idx]
  90. }
  91. for idx, _ := range units {
  92. unit := &units[idx]
  93. if unit.ParentId == nil {
  94. continue
  95. }
  96. parent := unitMap[*unit.ParentId]
  97. if parent == nil {
  98. if strict {
  99. panic(fmt.Sprintf("Not full units list. len(units) == %v; *unit.ParentId == %v", units, *unit.ParentId))
  100. }
  101. continue
  102. }
  103. parent.children = append(parent.children, unit)
  104. }
  105. for idx, _ := range units {
  106. units[idx].childrenReady = true
  107. }
  108. }
  109. func (units Units) CalculateTree() Units {
  110. units.calculateTree(true)
  111. return units
  112. }
  113. func (unit *Unit) DoRecursive(f func(*Unit, interface{}), arg interface{}) {
  114. f(unit, arg)
  115. for _, child := range unit.GetChildrenPtrs() {
  116. child.DoRecursive(f, arg)
  117. }
  118. }
  119. func (units Units) DoRecursive(f func(*Unit, interface{}), arg interface{}) {
  120. for _, unit := range units {
  121. unit.DoRecursive(f, arg)
  122. }
  123. }
  124. func (u Unit) GetChildrenPtrs() []*Unit {
  125. if !u.childrenReady {
  126. panic("unit is not ready for method GetChildrenPtrs: it's required to call CalculateTree method, first")
  127. }
  128. return u.children
  129. }
  130. func (u *Unit) PrepareFormulars() *Unit {
  131. var err error
  132. u.formulars, err = FormularSQL.Select(Formular{UnitId: u.Id})
  133. if err != nil && err != sql.ErrNoRows {
  134. panic(err)
  135. }
  136. u.formularsReady = true
  137. return u
  138. }
  139. func (u Unit) GetFormularsPtr() *Formulars {
  140. return &u.formulars
  141. }
  142. func (u Unit) GetFormulars() Formulars {
  143. if !u.formularsReady {
  144. panic("unit is not ready for method GetFormulars: it's required to call PrepareFormulars method, first")
  145. }
  146. return u.formulars
  147. }
  148. func (units Units) PrepareFormulars() Units {
  149. for idx, _ := range units {
  150. units[idx].PrepareFormulars()
  151. }
  152. return units
  153. }
  154. func (units Units) GetFormulars() (formulars Formulars) {
  155. for _, unit := range units {
  156. formulars = append(formulars, unit.GetFormulars()...)
  157. }
  158. return
  159. }
  160. func (u *Unit) PrepareIsActive() *Unit {
  161. now := extime.Now()
  162. startIsGood := false
  163. endIsGood := false
  164. if u.CreateOrderDate != nil {
  165. startIsGood = u.CreateOrderDate.UnixNano() <= now.UnixNano()
  166. }
  167. if u.CloseOrderDate == nil {
  168. endIsGood = true
  169. } else {
  170. endIsGood = u.CloseOrderDate.UnixNano()+24*3600*1000*1000*1000 >= now.UnixNano()
  171. }
  172. u.isActive = startIsGood && endIsGood
  173. return u
  174. }
  175. func (u Unit) IsActive() bool {
  176. if !u.isActiveReady {
  177. panic("unit is not ready for method IsActive: it's required to call PrepareIsActive method, first")
  178. }
  179. return u.isActive
  180. }
  181. func (u *Unit) PrepareChildrenTree() *Unit {
  182. if u.childrenReady {
  183. return u
  184. }
  185. children, err := UnitSQL.Select(Unit{ParentId: &u.Id})
  186. if err == sql.ErrNoRows {
  187. u.childrenReady = true
  188. return u
  189. }
  190. if err != nil {
  191. panic(err)
  192. }
  193. for idx, _ := range children {
  194. child := &children[idx]
  195. child.PrepareChildrenTree()
  196. u.children = append(u.children, child)
  197. }
  198. u.childrenReady = true
  199. return u
  200. }
  201. func (units Units) PrepareChildrenTree() Units {
  202. for idx, _ := range units {
  203. units[idx].PrepareChildrenTree()
  204. }
  205. return units
  206. }
  207. func GetUnitsHeadedBy(empGUID int) (units Units) {
  208. var formulars Formulars
  209. trueValue := true
  210. formulars, err := FormularSQL.ActiveOnly().Select(Formular{EmpGUID: empGUID, IsHead: &trueValue})
  211. if err != nil {
  212. panic(err)
  213. }
  214. for _, formular := range formulars {
  215. if !formular.PrepareIsActive().IsActive() {
  216. continue
  217. }
  218. formular.PrepareUnit()
  219. unit := formular.GetUnit()
  220. unit.PrepareChildrenTree()
  221. units = append(units, unit)
  222. }
  223. return
  224. }
  225. func (units Units) GetUnitIds() (unitIds []int) {
  226. for _, unit := range units {
  227. unitIds = append(unitIds, unit.Id)
  228. }
  229. return
  230. }
  231. func (unit Unit) Flush() {
  232. unit.children = []*Unit{}
  233. unit.childrenReady = false
  234. unit.formulars = Formulars{}
  235. unit.formularsReady = false
  236. unit.isActive = false
  237. unit.isActiveReady = false
  238. }
  239. func (unit Unit) IsEqualsTo(compareTo Unit) bool {
  240. unit.Flush()
  241. compareTo.Flush()
  242. return reflect.DeepEqual(unit, compareTo)
  243. }