unit.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. "reflect"
  9. "strings"
  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. func (unit Unit) IsChildrenReady() bool {
  86. return unit.childrenReady
  87. }
  88. type Units []Unit
  89. func (units Units) ToPersNumberMap() map[int]*Unit {
  90. unitMap := map[int]*Unit{}
  91. for idx, unit := range units {
  92. if unit.PersNumber == nil {
  93. continue
  94. }
  95. unitMap[*unit.PersNumber] = &units[idx]
  96. }
  97. return unitMap
  98. }
  99. func (units Units) ToMap() map[int]*Unit {
  100. unitMap := map[int]*Unit{}
  101. for idx, unit := range units {
  102. unitMap[unit.Id] = &units[idx]
  103. }
  104. return unitMap
  105. }
  106. func (units Units) calculateTree(strict bool) {
  107. unitMap := units.ToMap()
  108. for idx, _ := range units {
  109. unit := &units[idx]
  110. if unit.ParentId == nil {
  111. continue
  112. }
  113. parent := unitMap[*unit.ParentId]
  114. if parent == nil {
  115. if strict {
  116. panic(fmt.Sprintf("Not full units list. len(units) == %v; *unit.ParentId == %v", units, *unit.ParentId))
  117. }
  118. continue
  119. }
  120. parent.children = append(parent.children, unit)
  121. }
  122. for idx, _ := range units {
  123. units[idx].childrenReady = true
  124. }
  125. }
  126. func (units Units) CalculateTree() Units {
  127. units.calculateTree(true)
  128. return units
  129. }
  130. func (unit *Unit) DoRecursive(f func(*Unit, interface{}) bool, arg interface{}) bool {
  131. if !f(unit, arg) {
  132. return false
  133. }
  134. for _, child := range unit.GetChildrenPtrs() {
  135. if !child.DoRecursive(f, arg) {
  136. return false
  137. }
  138. }
  139. return true
  140. }
  141. func (units Units) DoRecursive(f func(*Unit, interface{}) bool, arg interface{}) bool {
  142. for _, unit := range units {
  143. unit.DoRecursive(f, arg)
  144. }
  145. return true
  146. }
  147. func (u Unit) GetChildrenPtrs() []*Unit {
  148. if !u.childrenReady {
  149. panic("unit is not ready for method GetChildrenPtrs: it's required to call CalculateTree method, first")
  150. }
  151. return u.children
  152. }
  153. func (u *Unit) PrepareFormulars() *Unit {
  154. var err error
  155. if u.PersNumber == nil {
  156. u.formulars, err = FormularSQL.Select(Formular{UnitId: u.Id})
  157. } else {
  158. u.formulars, err = FormularSQL.Select(Formular{PersNumber: u.PersNumber})
  159. for idx, _ := range u.formulars {
  160. u.formulars[idx].UnitId = u.Id
  161. }
  162. }
  163. if err != nil && err != sql.ErrNoRows {
  164. panic(err)
  165. }
  166. u.formularsReady = true
  167. return u
  168. }
  169. func (u Unit) GetFormularsPtr() *Formulars {
  170. return &u.formulars
  171. }
  172. func (u Unit) IsFormularsReady() bool {
  173. return u.formularsReady
  174. }
  175. func (u Unit) GetFormulars() Formulars {
  176. if !u.formularsReady {
  177. panic("unit is not ready for method GetFormulars: it's required to call PrepareFormulars method, first")
  178. }
  179. return u.formulars
  180. }
  181. func (units Units) PrepareFormulars(activeOnly bool) Units {
  182. if len(units) == 0 {
  183. return units
  184. }
  185. ids := units.GetUnitIds()
  186. nums := units.GetPersNumbers()
  187. scope := FormularSQL.Scope()
  188. if activeOnly {
  189. scope = scope.ActiveOnly()
  190. }
  191. if len(nums) == 0 && len(ids) == 0 {
  192. return units
  193. }
  194. if len(nums) > 0 {
  195. scope = scope.Where("PersNumber IN (?) OR OrgDiv IN (?)", nums, ids)
  196. } else {
  197. scope = scope.Where("OrgDiv IN (?)", ids)
  198. }
  199. formulars, err := scope.Select()
  200. if err != nil && err != sql.ErrNoRows {
  201. panic(err)
  202. }
  203. unitsMap := units.ToMap()
  204. unitsPMap := units.ToPersNumberMap()
  205. for _, formular := range formulars {
  206. if formular.PersNumber == nil {
  207. (*unitsMap[formular.UnitId]).formulars = append((*unitsMap[formular.UnitId]).formulars, formular)
  208. continue
  209. }
  210. unitPtr := unitsPMap[*formular.PersNumber]
  211. formular.UnitId = (*unitPtr).Id
  212. (*unitPtr).formulars = append((*unitPtr).formulars, formular)
  213. }
  214. for idx, _ := range units {
  215. units[idx].formularsReady = true
  216. }
  217. return units
  218. }
  219. func (units Units) GetFormulars() (formulars Formulars) {
  220. for _, unit := range units {
  221. formulars = append(formulars, unit.GetFormulars()...)
  222. }
  223. return
  224. }
  225. func (u *Unit) PrepareIsActive() *Unit {
  226. now := extime.Now()
  227. startIsGood := false
  228. endIsGood := false
  229. if u.CreateOrderDate != nil {
  230. startIsGood = u.CreateOrderDate.UnixNano() <= now.UnixNano()
  231. }
  232. if u.CloseOrderDate == nil {
  233. endIsGood = true
  234. } else {
  235. endIsGood = u.CloseOrderDate.UnixNano()+24*3600*1000*1000*1000 >= now.UnixNano()
  236. }
  237. u.isActive = startIsGood && endIsGood
  238. return u
  239. }
  240. func (u Unit) IsActive() bool {
  241. if !u.isActiveReady {
  242. panic("unit is not ready for method IsActive: it's required to call PrepareIsActive method, first")
  243. }
  244. return u.isActive
  245. }
  246. func (u *Unit) prepareChildrenTree(recursive bool) *Unit {
  247. if u.childrenReady {
  248. return u
  249. }
  250. children, err := UnitSQL.Select(Unit{ParentId: &u.Id})
  251. if err == sql.ErrNoRows {
  252. u.childrenReady = true
  253. return u
  254. }
  255. if err != nil {
  256. panic(err)
  257. }
  258. for idx, _ := range children {
  259. child := &children[idx]
  260. if recursive {
  261. child.PrepareChildrenTree()
  262. }
  263. u.children = append(u.children, child)
  264. }
  265. u.childrenReady = true
  266. return u
  267. }
  268. func (u *Unit) PrepareChildrenTree() *Unit {
  269. return u.prepareChildrenTree(true)
  270. }
  271. func (u *Unit) PrepareChildren() *Unit {
  272. return u.prepareChildrenTree(false)
  273. }
  274. func (units Units) PrepareChildrenTree() Units {
  275. for idx, _ := range units {
  276. units[idx].PrepareChildrenTree()
  277. }
  278. return units
  279. }
  280. func (units Units) PrepareChildren() Units {
  281. for idx, _ := range units {
  282. units[idx].PrepareChildren()
  283. }
  284. return units
  285. }
  286. func GetUnitsHeadedBy(empGUID int) (units Units) {
  287. var formulars Formulars
  288. trueValue := true
  289. formulars, err := FormularSQL.ActiveOnly().Select(Formular{EmpGUID: empGUID, IsHead: &trueValue})
  290. if err != nil {
  291. panic(err)
  292. }
  293. for _, formular := range formulars {
  294. if !formular.PrepareIsActive().IsActive() {
  295. continue
  296. }
  297. formular.PrepareUnit()
  298. unit := formular.GetUnit()
  299. unit.PrepareChildrenTree()
  300. units = append(units, unit)
  301. }
  302. return
  303. }
  304. func (units Units) GetPersNumbers() (persNumbers []int) {
  305. for _, unit := range units {
  306. if unit.PersNumber == nil {
  307. continue
  308. }
  309. persNumbers = append(persNumbers, *unit.PersNumber)
  310. }
  311. return
  312. }
  313. func (units Units) GetUnitIds() (unitIds []int) {
  314. for _, unit := range units {
  315. unitIds = append(unitIds, unit.Id)
  316. }
  317. return
  318. }
  319. func (unit Unit) Flush() {
  320. unit.children = []*Unit{}
  321. unit.childrenReady = false
  322. unit.formulars = Formulars{}
  323. unit.formularsReady = false
  324. unit.isActive = false
  325. unit.isActiveReady = false
  326. }
  327. func (unit Unit) IsEqualsTo(compareTo Unit) bool {
  328. unit.Flush()
  329. compareTo.Flush()
  330. return reflect.DeepEqual(unit, compareTo)
  331. }