unit.go 10 KB

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