runtime.go 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  1. package budget
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "strings"
  6. "zhiyuan/models"
  7. "zhiyuan/models/budget"
  8. "zhiyuan/models/final"
  9. "zhiyuan/pkg/db"
  10. "zhiyuan/pkg/logger"
  11. "github.com/Knetic/govaluate"
  12. "github.com/gin-gonic/gin"
  13. )
  14. var evaluableExpressionCache map[string]*govaluate.EvaluableExpression
  15. type Call struct {
  16. Expression string
  17. //Instance Instance
  18. Instance *InstanceData
  19. }
  20. type Context struct {
  21. Order *Order
  22. //Current Instance
  23. Current *InstanceData
  24. Calls []Call
  25. Modify bool
  26. C *gin.Context
  27. MatTypeCache map[int64]final.FinalMatType
  28. MatTypePidNameCache map[int64]map[string]final.FinalMatType
  29. MatCache map[int64]final.FinalMat
  30. CustomerCache *budget.Customer
  31. AdminCache *models.Admin
  32. GlobalProp map[string]interface{}
  33. LoadInstanceMap map[*InstanceData]bool
  34. ItemTypeCache map[int64]budget.ItemType
  35. ItemTypePidNameCache map[int64]map[string]budget.ItemType
  36. SupplierCache map[int64]final.FinalSupplier
  37. SupplierNameCache map[string]final.FinalSupplier
  38. CurrentPropKey uint64
  39. }
  40. func NewContext(order *Order, c *gin.Context) *Context {
  41. if evaluableExpressionCache == nil {
  42. evaluableExpressionCache = make(map[string]*govaluate.EvaluableExpression)
  43. }
  44. context := new(Context)
  45. context.Order = order
  46. context.Calls = make([]Call, 0)
  47. context.C = c
  48. context.GlobalProp = make(map[string]interface{})
  49. context.Current = context.Order.Form
  50. context.LoadInstanceMap = make(map[*InstanceData]bool)
  51. return context
  52. }
  53. func filterValue(value interface{}) interface{} {
  54. switch val := value.(type) {
  55. case float32:
  56. return float64(val)
  57. case int:
  58. return float64(val)
  59. case uint:
  60. return float64(val)
  61. case int8:
  62. return float64(val)
  63. case uint8:
  64. return float64(val)
  65. case int16:
  66. return float64(val)
  67. case uint16:
  68. return float64(val)
  69. case int32:
  70. return float64(val)
  71. case uint32:
  72. return float64(val)
  73. case int64:
  74. return float64(val)
  75. case uint64:
  76. return float64(val)
  77. case json.Number:
  78. if ret, err := val.Float64(); err == nil {
  79. return ret
  80. }
  81. }
  82. return value
  83. }
  84. func (context *Context) ModifyAfter(key uint64) {
  85. context.Modify = true
  86. for key := range context.Order.Prop {
  87. context.Order.Prop[key].UseKeys = make([]uint64, 0)
  88. }
  89. /*if prop, ok := context.Order.Prop[key]; ok {
  90. if useKeys := context.Order.PropUseCahce[prop.Key]; ok {
  91. for _, k := range useKeys {
  92. if useProp, ok := context.Order.Prop[k]; ok {
  93. for i, uk := range useProp.UseKeys {
  94. if uk == prop.Key {
  95. useProp.UseKeys = append(useProp.UseKeys[:i], useProp.UseKeys[i+1:]...)
  96. break
  97. }
  98. }
  99. }
  100. }
  101. context.Order.PropUseCahce[prop.Key] = make([]uint64, 0)
  102. }
  103. tmpKeys := append([]uint64{}, prop.UseKeys...)
  104. for _, k := range tmpKeys {
  105. context.ModifyAfter(k)
  106. }
  107. prop.UseKeys = make([]uint64, 0)
  108. }*/
  109. context.LoadInstanceMap = make(map[*InstanceData]bool)
  110. }
  111. func (context *Context) Customer() *budget.Customer {
  112. if context.CustomerCache == nil {
  113. db.GetModel(map[string]interface{}{"id": context.Order.CustomerId}, &context.CustomerCache)
  114. }
  115. return context.CustomerCache
  116. }
  117. func (context *Context) Admin() *models.Admin {
  118. if context.AdminCache == nil {
  119. db.GetModel(map[string]interface{}{"id": context.Order.AdminId}, &context.AdminCache)
  120. }
  121. return context.AdminCache
  122. }
  123. func (context *Context) ItemTypeInit() {
  124. if context.ItemTypeCache == nil {
  125. context.ItemTypeCache = make(map[int64]budget.ItemType)
  126. context.ItemTypePidNameCache = make(map[int64]map[string]budget.ItemType)
  127. types := make([]budget.ItemType, 0)
  128. db.GetModel(map[string]interface{}{
  129. "deleted_at": 0,
  130. }, &types)
  131. for _, typ := range types {
  132. if _, ok := context.ItemTypePidNameCache[typ.Pid]; !ok {
  133. context.ItemTypePidNameCache[typ.Pid] = make(map[string]budget.ItemType)
  134. }
  135. context.ItemTypeCache[typ.ID] = typ
  136. context.ItemTypePidNameCache[typ.Pid][typ.Name] = typ
  137. }
  138. }
  139. }
  140. func (context *Context) MatTypeInit() {
  141. if context.MatTypeCache == nil {
  142. context.MatTypeCache = make(map[int64]final.FinalMatType)
  143. context.MatTypePidNameCache = make(map[int64]map[string]final.FinalMatType)
  144. types := make([]final.FinalMatType, 0)
  145. db.GetModel(map[string]interface{}{
  146. "deleted_at": 0,
  147. }, &types)
  148. for _, typ := range types {
  149. if _, ok := context.MatTypePidNameCache[typ.Pid]; !ok {
  150. context.MatTypePidNameCache[typ.Pid] = make(map[string]final.FinalMatType)
  151. }
  152. context.MatTypeCache[typ.ID] = typ
  153. context.MatTypePidNameCache[typ.Pid][typ.Name] = typ
  154. }
  155. }
  156. }
  157. func (context *Context) SupplierInit() {
  158. if context.SupplierCache == nil {
  159. context.SupplierCache = make(map[int64]final.FinalSupplier)
  160. context.SupplierNameCache = make(map[string]final.FinalSupplier)
  161. suppliers := make([]final.FinalSupplier, 0)
  162. db.GetModel(map[string]interface{}{
  163. "deleted_at": 0,
  164. }, &suppliers)
  165. for _, supplier := range suppliers {
  166. context.SupplierCache[supplier.ID] = supplier
  167. context.SupplierNameCache[supplier.Name] = supplier
  168. }
  169. }
  170. }
  171. func (context *Context) GetMat(id int64) *final.FinalMat {
  172. if context.MatCache == nil {
  173. context.MatCache = make(map[int64]final.FinalMat)
  174. }
  175. if _, ok := context.MatCache[id]; !ok {
  176. var mat final.FinalMat
  177. db.GetModel(map[string]interface{}{
  178. "id": id,
  179. "deleted_at": 0,
  180. }, &mat)
  181. if mat.ID != 0 {
  182. context.MatCache[id] = mat
  183. }
  184. }
  185. if mat, ok := context.MatCache[id]; ok {
  186. return &mat
  187. }
  188. return nil
  189. }
  190. /*func (context *Context) GetProp(name string) (*PropForm, error) {
  191. last := context.Current
  192. defer func() {
  193. context.Current = last
  194. }()
  195. for instance := context.Current; instance != nil; instance = instance.GetParent() {
  196. var expression string
  197. ok := false
  198. switch val := instance.(type) {
  199. case *QuoteForm:
  200. expression, ok = context.Order.Quote.Prop[name]
  201. case *TableInstance:
  202. expression, ok = context.Order.Table[val.GetID()].Prop[name]
  203. case *ModuleInstance:
  204. expression, ok = context.Order.Module[val.GetID()].Prop[name]
  205. case *GroupInstance:
  206. expression, ok = context.Order.Group[val.GetID()].Prop[name]
  207. case *RowInstance:
  208. expression, ok = context.Order.Row[val.GetID()].Prop[name]
  209. if !ok {
  210. err := context.LoadInstance(instance)
  211. if err != nil {
  212. return nil, err
  213. }
  214. if val.Item != nil {
  215. expression, ok = context.Order.Item[val.Item.ID].Prop[name]
  216. if !ok {
  217. expression, ok = context.Order.Item[val.Item.ID].ParentProp[name]
  218. }
  219. if ok {
  220. instance = val.Item
  221. }
  222. }
  223. }
  224. case *ItemInstance:
  225. expression, ok = context.Order.Item[val.GetID()].Prop[name]
  226. if !ok {
  227. expression, ok = context.Order.Item[val.GetID()].ParentProp[name]
  228. }
  229. }
  230. if ok {
  231. context.Current = instance
  232. prop := instance.GetPropResult(name)
  233. result, err := context.GetForm(expression, prop)
  234. if err != nil {
  235. return nil, err
  236. }
  237. prop = result
  238. instance.SetPropResult(name, prop)
  239. return prop, nil
  240. }
  241. }
  242. //return nil, errors.New("undeclared name: " + name)
  243. return nil, nil
  244. }*/
  245. func (context *Context) GetProp(name string) (*PropForm, error) {
  246. last := context.Current
  247. defer func() {
  248. context.Current = last
  249. }()
  250. for instance := context.Current; instance != nil; instance = instance.Parent {
  251. var expression string
  252. ok := false
  253. if model, ok1 := context.Order.Models[instance.Type]; ok1 {
  254. if data, ok2 := model[instance.GetID()]; ok2 {
  255. expression, ok = data.GetProp(name)
  256. }
  257. }
  258. if !ok && instance.Type == Row {
  259. err := context.LoadInstance(instance)
  260. if err != nil {
  261. return nil, err
  262. }
  263. if instance.Item != nil {
  264. if model, ok1 := context.Order.Models[instance.Item.Type]; ok1 {
  265. if data, ok2 := model[instance.Item.GetID()]; ok2 {
  266. expression, ok = data.GetProp(name)
  267. }
  268. }
  269. if ok {
  270. instance = instance.Item
  271. }
  272. }
  273. }
  274. if ok {
  275. context.Current = instance
  276. prop := instance.GetPropResult(name)
  277. result, err := context.GetForm(expression, prop)
  278. if err != nil {
  279. return nil, err
  280. }
  281. prop = result
  282. instance.SetPropResult(name, prop)
  283. return prop, nil
  284. }
  285. /*switch instance.Type {
  286. case Quote:
  287. expression, ok = context.Order.Quote.Prop[name]
  288. case Table:
  289. expression, ok = context.Order.Table[instance.GetID()].Prop[name]
  290. case Module:
  291. expression, ok = context.Order.Module[instance.GetID()].Prop[name]
  292. case Group:
  293. expression, ok = context.Order.Group[instance.GetID()].Prop[name]
  294. case Row:
  295. expression, ok = context.Order.Row[instance.GetID()].Prop[name]
  296. if !ok {
  297. err := context.LoadInstance(instance)
  298. if err != nil {
  299. return nil, err
  300. }
  301. if instance.Item != nil {
  302. expression, ok = context.Order.Item[instance.Item.ID].Prop[name]
  303. if !ok {
  304. expression, ok = context.Order.Item[instance.Item.ID].ParentProp[name]
  305. }
  306. if ok {
  307. instance = instance.Item
  308. }
  309. }
  310. }
  311. case Item:
  312. expression, ok = context.Order.Item[instance.GetID()].Prop[name]
  313. if !ok {
  314. expression, ok = context.Order.Item[instance.GetID()].ParentProp[name]
  315. }
  316. }
  317. if ok {
  318. context.Current = instance
  319. prop := instance.GetPropResult(name)
  320. result, err := context.GetForm(expression, prop)
  321. if err != nil {
  322. return nil, err
  323. }
  324. prop = result
  325. instance.SetPropResult(name, prop)
  326. return prop, nil
  327. }*/
  328. }
  329. //return nil, errors.New("undeclared name: " + name)
  330. return nil, nil
  331. }
  332. func (context *Context) Get(name string) (interface{}, error) {
  333. if value, ok := context.GlobalProp[name]; ok {
  334. return filterValue(value), nil
  335. }
  336. prop, err := context.GetProp(name)
  337. if prop == nil {
  338. return nil, err
  339. }
  340. return filterValue(prop.Result), err
  341. }
  342. func (context *Context) Error(err error) {
  343. //logger.Sugar.Infof("context.Current: %v", context.Current)
  344. logger.Sugar.Infof("context.Current.Type(): %v", context.Current.Type)
  345. //logger.Sugar.Infof("context.Current.GetObject(): %v", context.Current.GetObject())
  346. logger.Sugar.Infof("context.Calls: %v", context.Calls)
  347. logger.Sugar.Infof("err: %v", err)
  348. }
  349. func (context *Context) EvaluableExpression(expression string) (interface{}, error) {
  350. for _, call := range context.Calls {
  351. if call.Instance == context.Current && call.Expression == expression {
  352. return nil, errors.New("circular reference: " + expression)
  353. }
  354. }
  355. context.Calls = append(context.Calls, Call{expression, context.Current})
  356. defer func() { context.Calls = context.Calls[:len(context.Calls)-1] }()
  357. //logger.Sugar.Infof("expression: %v", expression)
  358. expr, ok := evaluableExpressionCache[expression]
  359. if !ok {
  360. s, err := govaluate.NewEvaluableExpressionWithFunctions(expression, context.Functions())
  361. if err != nil {
  362. context.Error(err)
  363. return nil, err
  364. }
  365. expr = s
  366. }
  367. result, err := expr.Eval(context)
  368. if err != nil {
  369. context.Error(err)
  370. return nil, err
  371. }
  372. //logger.Sugar.Infof("expressionend: %v", expression)
  373. return result, nil
  374. }
  375. func (context *Context) Exec(expression string) (interface{}, error) {
  376. return context.EvaluableExpression(expression)
  377. }
  378. func (context *Context) ParseValue(value string) (interface{}, error) {
  379. if len(value) == 0 {
  380. return "", nil
  381. }
  382. switch value[0] {
  383. case '.':
  384. n, _ := db.ToFloat64(value[1:])
  385. return n, nil
  386. case '\'':
  387. return db.ToString(value[1:]), nil
  388. case '=':
  389. return context.Exec(value[1:])
  390. case '@':
  391. var count string
  392. var val string
  393. n := strings.Index(value[1:], "@")
  394. if n == -1 {
  395. count = value[1:]
  396. } else {
  397. count = value[1 : n+1]
  398. val = value[n+2:]
  399. }
  400. countResult, err := context.ParseValue(count)
  401. if err != nil {
  402. return nil, err
  403. }
  404. counts, _ := db.ToInt64(countResult)
  405. results := make([]interface{}, counts)
  406. for i := range results {
  407. globalProp := map[string]interface{}{
  408. "key": float64(i),
  409. }
  410. result, err := context.ParseValueinGlobalProp(val, globalProp)
  411. if err != nil {
  412. return nil, err
  413. }
  414. results[i] = result
  415. }
  416. return results, nil
  417. default:
  418. return db.ToString(value), nil
  419. }
  420. }
  421. // func (context *Context) ParseValueinInstance(value string, instance Instance) (interface{}, error) {
  422. func (context *Context) ParseValueinInstance(value string, instance *InstanceData) (interface{}, error) {
  423. last := context.Current
  424. context.Current = instance
  425. defer func() {
  426. context.Current = last
  427. }()
  428. return context.ParseValue(value)
  429. }
  430. func (context *Context) ParseValueinGlobalProp(value string, globalProp map[string]interface{}) (interface{}, error) {
  431. last := context.GlobalProp
  432. context.GlobalProp = make(map[string]interface{})
  433. for k, v := range last {
  434. context.GlobalProp[k] = v
  435. }
  436. for k, v := range globalProp {
  437. context.GlobalProp[k] = v
  438. }
  439. defer func() {
  440. context.GlobalProp = last
  441. }()
  442. return context.ParseValue(value)
  443. }
  444. // func (context *Context) ParseValueInInstanceAndGlobalProp(value string, instance Instance, globalProp map[string]interface{}) (interface{}, error) {
  445. func (context *Context) ParseValueInInstanceAndGlobalProp(value string, instance *InstanceData, globalProp map[string]interface{}) (interface{}, error) {
  446. lastCurrent := context.Current
  447. lastGlobalProp := context.GlobalProp
  448. context.Current = instance
  449. context.GlobalProp = make(map[string]interface{})
  450. for k, v := range lastGlobalProp {
  451. context.GlobalProp[k] = v
  452. }
  453. for k, v := range globalProp {
  454. context.GlobalProp[k] = v
  455. }
  456. defer func() {
  457. context.Current = lastCurrent
  458. context.GlobalProp = lastGlobalProp
  459. }()
  460. return context.ParseValue(value)
  461. }
  462. func (context *Context) ParseForm(value string) (*PropForm, error) {
  463. prop := new(PropForm)
  464. prop.Instance = context.Current
  465. if len(value) != 0 {
  466. switch value[0] {
  467. case '!':
  468. prop.Cycle = Immediate
  469. value = value[1:]
  470. case '%':
  471. prop.Cycle = Save
  472. value = value[1:]
  473. }
  474. }
  475. results, err := context.ParseValue(value)
  476. if err != nil {
  477. return prop, err
  478. }
  479. if control, ok := results.(Control); ok {
  480. prop.Control = &control
  481. prop.Result = control.Value
  482. } else if control, ok := results.(*Control); ok {
  483. prop.Control = control
  484. prop.Result = control.Value
  485. } else {
  486. prop.Result = results
  487. }
  488. return prop, nil
  489. }
  490. func (context *Context) GetForm(value string, prop *PropForm) (*PropForm, error) {
  491. lastPropKey := context.CurrentPropKey
  492. defer func() { context.CurrentPropKey = lastPropKey }()
  493. if prop == nil || prop.Result == nil || len(prop.UseKeys) == 0 && (prop.Cycle == Immediate || prop.Cycle == Modify && context.Modify) {
  494. if prop != nil {
  495. context.CurrentPropKey = prop.Key
  496. } else {
  497. context.CurrentPropKey = context.Order.NewPropKey()
  498. }
  499. props, err := context.ParseForm(value)
  500. if err != nil {
  501. return nil, err
  502. }
  503. props.Key = context.CurrentPropKey
  504. if prop != nil {
  505. props.UseKeys = prop.UseKeys
  506. }
  507. if prop != nil && props.Control != nil && props.Control.Last {
  508. props.Result = prop.Result
  509. }
  510. prop = props
  511. context.Order.Prop[prop.Key] = prop
  512. }
  513. prop.AddUseKey(lastPropKey)
  514. context.Order.SetPropUse(lastPropKey, prop.Key)
  515. return prop, nil
  516. }
  517. /*func (context *Context) CreateObject(id int64, name string, parent Instance) (BaseObject, error) {
  518. var object BaseObject
  519. object.ID = id
  520. object.Init(parent, context.Order)
  521. prop, err := context.GetForm(name, nil)
  522. if err != nil {
  523. return object, err
  524. }
  525. object.NameResult = prop
  526. return object, nil
  527. }
  528. func (context *Context) CreateInstance(object Object, name interface{}, index int) (BaseInstance, error) {
  529. var instance BaseInstance
  530. instance.Init(object, index, context.Order)
  531. if nameMap, ok := name.(map[string]interface{}); ok {
  532. instance.Name = db.ToString(nameMap["name"])
  533. instance.Show = db.ToString(nameMap["show"])
  534. instance.ItemId, _ = db.ToInt64(nameMap["item"])
  535. instance.Key = db.ToString(nameMap["key"])
  536. } else {
  537. instance.Name = db.ToString(name)
  538. instance.Show = ""
  539. }
  540. return instance, nil
  541. }
  542. func (context *Context) CreateInstances(object Object, name interface{}) ([]BaseInstance, error) {
  543. instances := make([]BaseInstance, 0)
  544. if names, ok := name.([]interface{}); ok {
  545. for i, name := range names {
  546. instance, err := context.CreateInstance(object, name, i)
  547. if err != nil {
  548. return instances, err
  549. }
  550. instances = append(instances, instance)
  551. }
  552. } else {
  553. instance, err := context.CreateInstance(object, name, 0)
  554. if err != nil {
  555. return instances, err
  556. }
  557. instances = append(instances, instance)
  558. }
  559. return instances, nil
  560. }
  561. func (context *Context) CreateObject(id int64, name string, parent *InstanceData) (*ObjectData, error) {
  562. object := new(ObjectData)
  563. object.ID = id
  564. typ := parent.Type + 1
  565. if parent.Type == Table {
  566. typ = Module
  567. }
  568. object.Init(parent, typ, context.Order)
  569. prop, err := context.GetForm(name, nil)
  570. if err != nil {
  571. return object, err
  572. }
  573. object.NameResult = prop
  574. return object, nil
  575. }
  576. func (context *Context) CreateInstance(object *ObjectData, name interface{}, index int) (*InstanceData, error) {
  577. instance := new(InstanceData)
  578. instance.Init(object, object.Type, index, context.Order)
  579. if nameMap, ok := name.(map[string]interface{}); ok {
  580. instance.Name = db.ToString(nameMap["name"])
  581. instance.Show = db.ToString(nameMap["show"])
  582. instance.ItemId, _ = db.ToInt64(nameMap["item"])
  583. instance.Key = db.ToString(nameMap["key"])
  584. } else {
  585. instance.Name = db.ToString(name)
  586. instance.Show = ""
  587. }
  588. return instance, nil
  589. }
  590. func (context *Context) CreateInstances(object *ObjectData, name interface{}) ([]InstanceData, error) {
  591. instances := make([]InstanceData, 0)
  592. if names, ok := name.([]interface{}); ok {
  593. for i, name := range names {
  594. instance, err := context.CreateInstance(object, name, i)
  595. if err != nil {
  596. return instances, err
  597. }
  598. instances = append(instances, *instance)
  599. }
  600. } else {
  601. instance, err := context.CreateInstance(object, name, 0)
  602. if err != nil {
  603. return instances, err
  604. }
  605. instances = append(instances, *instance)
  606. }
  607. return instances, nil
  608. }
  609. func (context *Context) LoadInstance(instance Instance) error {
  610. last := context.Current
  611. context.Current = instance
  612. defer func() {
  613. context.Current = last
  614. }()
  615. if load, ok := context.LoadInstanceMap[instance]; ok && load {
  616. switch val := instance.(type) {
  617. case *QuoteForm:
  618. for _, qdata := range val.Tables {
  619. if qdata.NameResult != nil {
  620. qdata.NameResult.AddUseKey(context.CurrentPropKey)
  621. }
  622. }
  623. case *TableInstance:
  624. for _, qdata := range val.Headers {
  625. if qdata.NameResult != nil {
  626. qdata.NameResult.AddUseKey(context.CurrentPropKey)
  627. }
  628. }
  629. for _, qdata := range val.Modules {
  630. if qdata.NameResult != nil {
  631. qdata.NameResult.AddUseKey(context.CurrentPropKey)
  632. }
  633. }
  634. case *HeaderInstance:
  635. val.Result.AddUseKey(context.CurrentPropKey)
  636. case *ModuleInstance:
  637. for _, qdata := range val.Groups {
  638. if qdata.NameResult != nil {
  639. qdata.NameResult.AddUseKey(context.CurrentPropKey)
  640. }
  641. }
  642. case *GroupInstance:
  643. for _, qdata := range val.Rows {
  644. if qdata.NameResult != nil {
  645. qdata.NameResult.AddUseKey(context.CurrentPropKey)
  646. }
  647. }
  648. }
  649. return nil
  650. }
  651. switch val := instance.(type) {
  652. case *QuoteForm:
  653. tables := make([]TableObject, 0)
  654. for _, qtable := range context.Order.Quote.Tables {
  655. var object *TableObject
  656. for i, table := range val.Tables {
  657. if table.ID == qtable.ID {
  658. object = &val.Tables[i]
  659. }
  660. }
  661. if object == nil {
  662. o, err := context.CreateObject(qtable.ID, qtable.Name, instance)
  663. if err != nil {
  664. return err
  665. }
  666. object = new(TableObject)
  667. object.BaseObject = o
  668. object.Init(instance, context.Order)
  669. }
  670. nameResult, err := context.GetForm(context.Order.Table[object.ID].Name, object.NameResult)
  671. if err != nil {
  672. return err
  673. }
  674. object.NameResult = nameResult
  675. instances, err := context.CreateInstances(object, object.NameResult.Result)
  676. if err != nil {
  677. return err
  678. }
  679. objectInstances := make(map[int]TableInstance)
  680. objectInstanceMap := make(map[string]TableInstance)
  681. for _, instance := range object.Instances {
  682. objectInstances[instance.Index] = instance
  683. if instance.Key != "" {
  684. objectInstanceMap[instance.Key] = instance
  685. }
  686. }
  687. //object.Instances = make([]TableInstance, 0)
  688. for _, instance := range instances {
  689. var objectInstance *TableInstance
  690. if instance.Key == "" {
  691. if i, ok := objectInstances[instance.Index]; ok {
  692. objectInstance = &i
  693. }
  694. } else {
  695. if i, ok := objectInstanceMap[instance.Key]; ok {
  696. objectInstance = &i
  697. }
  698. }
  699. if objectInstance == nil {
  700. objectInstance = new(TableInstance)
  701. objectInstance.BaseInstance = instance
  702. } else {
  703. delete(objectInstances, objectInstance.Index)
  704. delete(objectInstanceMap, objectInstance.Key)
  705. objectInstance.Name = instance.Name
  706. objectInstance.Show = instance.Show
  707. objectInstance.ItemId = instance.ItemId
  708. objectInstance.Key = instance.Key
  709. }
  710. objectInstance.Init(object, instance.Index, context.Order)
  711. if len(object.Instances) > instance.Index {
  712. object.Instances[instance.Index] = *objectInstance
  713. } else {
  714. object.Instances = append(object.Instances, *objectInstance)
  715. }
  716. }
  717. if len(object.Instances) > len(instances) {
  718. object.Instances = object.Instances[:len(instances)]
  719. }
  720. tables = append(tables, *object)
  721. }
  722. val.Tables = tables
  723. case *TableInstance:
  724. headers := make([]HeaderObject, 0)
  725. for _, qdata := range context.Order.Table[val.GetID()].Headers {
  726. var object *HeaderObject
  727. for i, o := range val.Headers {
  728. if o.ID == qdata.ID {
  729. object = &val.Headers[i]
  730. }
  731. }
  732. if object == nil {
  733. o, err := context.CreateObject(qdata.ID, qdata.Name, instance)
  734. if err != nil {
  735. return err
  736. }
  737. object = new(HeaderObject)
  738. object.BaseObject = o
  739. object.Init(instance, context.Order)
  740. }
  741. nameResult, err := context.GetForm(context.Order.Header[object.ID].Name, object.NameResult)
  742. if err != nil {
  743. return err
  744. }
  745. object.NameResult = nameResult
  746. instances, err := context.CreateInstances(object, object.NameResult.Result)
  747. if err != nil {
  748. return err
  749. }
  750. objectInstances := make(map[int]HeaderInstance)
  751. objectInstanceMap := make(map[string]HeaderInstance)
  752. for _, instance := range object.Instances {
  753. objectInstances[instance.Index] = instance
  754. if instance.Key != "" {
  755. objectInstanceMap[instance.Key] = instance
  756. }
  757. }
  758. //object.Instances = make([]HeaderInstance, 0)
  759. for _, instance := range instances {
  760. var objectInstance *HeaderInstance
  761. if instance.Key == "" {
  762. if i, ok := objectInstances[instance.Index]; ok {
  763. objectInstance = &i
  764. }
  765. } else {
  766. if i, ok := objectInstanceMap[instance.Key]; ok {
  767. objectInstance = &i
  768. }
  769. }
  770. if objectInstance == nil {
  771. objectInstance = new(HeaderInstance)
  772. objectInstance.BaseInstance = instance
  773. } else {
  774. delete(objectInstances, objectInstance.Index)
  775. delete(objectInstanceMap, objectInstance.Key)
  776. objectInstance.Name = instance.Name
  777. objectInstance.Show = instance.Show
  778. objectInstance.ItemId = instance.ItemId
  779. objectInstance.Key = instance.Key
  780. }
  781. objectInstance.Init(object, instance.Index, context.Order)
  782. if len(object.Instances) > instance.Index {
  783. object.Instances[instance.Index] = *objectInstance
  784. } else {
  785. object.Instances = append(object.Instances, *objectInstance)
  786. }
  787. }
  788. if len(object.Instances) > len(instances) {
  789. object.Instances = object.Instances[:len(instances)]
  790. }
  791. headers = append(headers, *object)
  792. }
  793. val.Headers = headers
  794. modules := make([]ModuleObject, 0)
  795. for _, qdata := range context.Order.Table[val.GetID()].Modules {
  796. var object *ModuleObject
  797. for i, o := range val.Modules {
  798. if o.ID == qdata.ID {
  799. object = &val.Modules[i]
  800. }
  801. }
  802. if object == nil {
  803. o, err := context.CreateObject(qdata.ID, qdata.Name, instance)
  804. if err != nil {
  805. return err
  806. }
  807. object = new(ModuleObject)
  808. object.BaseObject = o
  809. object.Init(instance, context.Order)
  810. }
  811. nameResult, err := context.GetForm(context.Order.Module[object.ID].Name, object.NameResult)
  812. if err != nil {
  813. return err
  814. }
  815. object.NameResult = nameResult
  816. instances, err := context.CreateInstances(object, object.NameResult.Result)
  817. if err != nil {
  818. return err
  819. }
  820. objectInstances := make(map[int]ModuleInstance)
  821. objectInstanceMap := make(map[string]ModuleInstance)
  822. for _, instance := range object.Instances {
  823. objectInstances[instance.Index] = instance
  824. if instance.Key != "" {
  825. objectInstanceMap[instance.Key] = instance
  826. }
  827. }
  828. //object.Instances = make([]ModuleInstance, 0)
  829. for _, instance := range instances {
  830. var objectInstance *ModuleInstance
  831. if instance.Key == "" {
  832. if i, ok := objectInstances[instance.Index]; ok {
  833. objectInstance = &i
  834. }
  835. } else {
  836. if i, ok := objectInstanceMap[instance.Key]; ok {
  837. objectInstance = &i
  838. }
  839. }
  840. if objectInstance == nil {
  841. objectInstance = new(ModuleInstance)
  842. objectInstance.BaseInstance = instance
  843. } else {
  844. delete(objectInstances, objectInstance.Index)
  845. delete(objectInstanceMap, objectInstance.Key)
  846. objectInstance.Name = instance.Name
  847. objectInstance.Show = instance.Show
  848. objectInstance.ItemId = instance.ItemId
  849. objectInstance.Key = instance.Key
  850. }
  851. objectInstance.Init(object, instance.Index, context.Order)
  852. if len(object.Instances) > instance.Index {
  853. object.Instances[instance.Index] = *objectInstance
  854. } else {
  855. object.Instances = append(object.Instances, *objectInstance)
  856. }
  857. }
  858. if len(object.Instances) > len(instances) {
  859. object.Instances = object.Instances[:len(instances)]
  860. }
  861. modules = append(modules, *object)
  862. }
  863. val.Modules = modules
  864. case *HeaderInstance:
  865. prop, err := context.GetForm(context.Order.Header[val.GetID()].Value, val.Result)
  866. if err != nil {
  867. return err
  868. }
  869. val.Result = prop
  870. case *ModuleInstance:
  871. groups := make([]GroupObject, 0)
  872. for _, qdata := range context.Order.Module[val.GetID()].Groups {
  873. var object *GroupObject
  874. for i, o := range val.Groups {
  875. if o.ID == qdata.ID {
  876. object = &val.Groups[i]
  877. }
  878. }
  879. if object == nil {
  880. o, err := context.CreateObject(qdata.ID, qdata.Name, instance)
  881. if err != nil {
  882. return err
  883. }
  884. object = new(GroupObject)
  885. object.BaseObject = o
  886. object.Init(instance, context.Order)
  887. }
  888. nameResult, err := context.GetForm(context.Order.Group[object.ID].Name, object.NameResult)
  889. if err != nil {
  890. return err
  891. }
  892. object.NameResult = nameResult
  893. instances, err := context.CreateInstances(object, object.NameResult.Result)
  894. if err != nil {
  895. return err
  896. }
  897. objectInstances := make(map[int]GroupInstance)
  898. objectInstanceMap := make(map[string]GroupInstance)
  899. for _, instance := range object.Instances {
  900. objectInstances[instance.Index] = instance
  901. if instance.Key != "" {
  902. objectInstanceMap[instance.Key] = instance
  903. }
  904. }
  905. //object.Instances = make([]GroupInstance, 0)
  906. for _, instance := range instances {
  907. var objectInstance *GroupInstance
  908. if instance.Key == "" {
  909. if i, ok := objectInstances[instance.Index]; ok {
  910. objectInstance = &i
  911. }
  912. } else {
  913. if i, ok := objectInstanceMap[instance.Key]; ok {
  914. objectInstance = &i
  915. }
  916. }
  917. if objectInstance == nil {
  918. objectInstance = new(GroupInstance)
  919. objectInstance.BaseInstance = instance
  920. } else {
  921. delete(objectInstances, objectInstance.Index)
  922. delete(objectInstanceMap, objectInstance.Key)
  923. objectInstance.Name = instance.Name
  924. objectInstance.Show = instance.Show
  925. objectInstance.ItemId = instance.ItemId
  926. objectInstance.Key = instance.Key
  927. }
  928. objectInstance.Init(object, instance.Index, context.Order)
  929. if len(object.Instances) > instance.Index {
  930. object.Instances[instance.Index] = *objectInstance
  931. } else {
  932. object.Instances = append(object.Instances, *objectInstance)
  933. }
  934. }
  935. if len(object.Instances) > len(instances) {
  936. object.Instances = object.Instances[:len(instances)]
  937. }
  938. groups = append(groups, *object)
  939. }
  940. val.Groups = groups
  941. case *GroupInstance:
  942. rows := make([]RowObject, 0)
  943. for _, qdata := range context.Order.Group[val.GetID()].Rows {
  944. var object *RowObject
  945. for i, o := range val.Rows {
  946. if o.ID == qdata.ID {
  947. object = &val.Rows[i]
  948. }
  949. }
  950. if object == nil {
  951. o, err := context.CreateObject(qdata.ID, qdata.Name, instance)
  952. if err != nil {
  953. return err
  954. }
  955. object = new(RowObject)
  956. object.BaseObject = o
  957. object.Init(instance, context.Order)
  958. }
  959. nameResult, err := context.GetForm(context.Order.Row[object.ID].Name, object.NameResult)
  960. if err != nil {
  961. return err
  962. }
  963. object.NameResult = nameResult
  964. instances, err := context.CreateInstances(object, object.NameResult.Result)
  965. if err != nil {
  966. return err
  967. }
  968. objectInstances := make(map[int]RowInstance)
  969. objectInstanceMap := make(map[string]RowInstance)
  970. for _, instance := range object.Instances {
  971. objectInstances[instance.Index] = instance
  972. if instance.Key != "" {
  973. objectInstanceMap[instance.Key] = instance
  974. }
  975. }
  976. //object.Instances = make([]RowInstance, 0)
  977. for _, instance := range instances {
  978. var objectInstance *RowInstance
  979. if instance.Key == "" {
  980. if i, ok := objectInstances[instance.Index]; ok {
  981. objectInstance = &i
  982. }
  983. } else {
  984. if i, ok := objectInstanceMap[instance.Key]; ok {
  985. objectInstance = &i
  986. }
  987. }
  988. if objectInstance == nil {
  989. objectInstance = new(RowInstance)
  990. objectInstance.BaseInstance = instance
  991. } else {
  992. delete(objectInstances, objectInstance.Index)
  993. delete(objectInstanceMap, objectInstance.Key)
  994. objectInstance.Name = instance.Name
  995. objectInstance.Show = instance.Show
  996. objectInstance.ItemId = instance.ItemId
  997. objectInstance.Key = instance.Key
  998. }
  999. objectInstance.Init(object, instance.Index, context.Order)
  1000. if len(object.Instances) > instance.Index {
  1001. object.Instances[instance.Index] = *objectInstance
  1002. } else {
  1003. object.Instances = append(object.Instances, *objectInstance)
  1004. }
  1005. }
  1006. if len(object.Instances) > len(instances) {
  1007. object.Instances = object.Instances[:len(instances)]
  1008. }
  1009. rows = append(rows, *object)
  1010. }
  1011. val.Rows = rows
  1012. case *RowInstance:
  1013. if val.ItemId != 0 {
  1014. if _, ok := context.Order.Item[val.ItemId]; !ok {
  1015. items := budget.GetItemModels(map[string]interface{}{
  1016. "id": val.ItemId,
  1017. "deleted_at": 0,
  1018. })
  1019. if len(items) != 0 {
  1020. context.Order.Item[val.ItemId] = items[0]
  1021. }
  1022. }
  1023. }
  1024. itemId := int64(0)
  1025. if val.ItemId != 0 {
  1026. if item, ok := context.Order.Item[val.ItemId]; ok {
  1027. itemId = item.ID
  1028. }
  1029. } else if context.Order.Row[val.GetID()].Item != nil {
  1030. itemId = context.Order.Row[val.GetID()].Item.ID
  1031. }
  1032. if itemId == 0 {
  1033. val.Item = nil
  1034. } else {
  1035. if val.Item == nil || itemId != val.Item.ID {
  1036. val.Item = new(ItemInstance)
  1037. val.Item.ID = itemId
  1038. val.Item.Init(val.Object, 0, context.Order)
  1039. }
  1040. val.Item.Row = val
  1041. }
  1042. }
  1043. context.LoadInstanceMap[instance] = true
  1044. return nil
  1045. }*/
  1046. func (context *Context) LoadInstance(instance *InstanceData) error {
  1047. last := context.Current
  1048. context.Current = instance
  1049. defer func() {
  1050. context.Current = last
  1051. }()
  1052. if load, ok := context.LoadInstanceMap[instance]; ok && load {
  1053. if instance.Subs != nil {
  1054. for _, sub := range instance.Subs {
  1055. if sub.NameResult != nil {
  1056. sub.NameResult.AddUseKey(context.CurrentPropKey)
  1057. context.Order.SetPropUse(context.CurrentPropKey, sub.NameResult.Key)
  1058. }
  1059. }
  1060. }
  1061. if instance.Headers != nil {
  1062. for _, header := range instance.Headers {
  1063. if header.NameResult != nil {
  1064. header.NameResult.AddUseKey(context.CurrentPropKey)
  1065. context.Order.SetPropUse(context.CurrentPropKey, header.NameResult.Key)
  1066. }
  1067. }
  1068. }
  1069. if instance.Result != nil {
  1070. instance.Result.AddUseKey(context.CurrentPropKey)
  1071. context.Order.SetPropUse(context.CurrentPropKey, instance.Result.Key)
  1072. }
  1073. return nil
  1074. }
  1075. if model, ok := context.Order.Models[instance.Type]; ok {
  1076. if data, ok := model[instance.GetID()]; ok {
  1077. var err error
  1078. qsubs := data.GetSubs()
  1079. if qsubs != nil {
  1080. if instance.Subs == nil {
  1081. instance.Subs = make([]ObjectData, 0)
  1082. }
  1083. subType := instance.Type + 1
  1084. if instance.Type == Table {
  1085. subType = Module
  1086. }
  1087. instance.Subs, err = context.LoadSubs(instance, subType, &instance.Subs, qsubs)
  1088. if err != nil {
  1089. return err
  1090. }
  1091. }
  1092. qheaders := data.GetHeaders()
  1093. if qheaders != nil {
  1094. if instance.Headers == nil {
  1095. instance.Headers = make([]ObjectData, 0)
  1096. }
  1097. instance.Headers, err = context.LoadSubs(instance, Header, &instance.Headers, qheaders)
  1098. if err != nil {
  1099. return err
  1100. }
  1101. }
  1102. itemId := int64(0)
  1103. if instance.ItemId != 0 {
  1104. item := context.LoadItem(instance.ItemId)
  1105. if item != nil {
  1106. itemId = item.GetID()
  1107. }
  1108. } else if data.GetItem() != nil {
  1109. itemId = data.GetItem().GetID()
  1110. }
  1111. if itemId == 0 {
  1112. instance.Item = nil
  1113. } else {
  1114. if instance.Item == nil || itemId != instance.Item.ID {
  1115. instance.Item = new(InstanceData)
  1116. instance.Item.ID = itemId
  1117. instance.Item.Init(instance, instance.Object, Item, instance.Index, context.Order)
  1118. }
  1119. }
  1120. if instance.Type == Header {
  1121. prop, err := context.GetForm(data.GetValue(), instance.Result)
  1122. if err != nil {
  1123. return err
  1124. }
  1125. instance.Result = prop
  1126. }
  1127. }
  1128. }
  1129. context.LoadInstanceMap[instance] = true
  1130. return nil
  1131. }
  1132. func (context *Context) LoadItem(itemId int64) budget.QuoteData {
  1133. if _, ok := context.Order.Models[Item][itemId]; !ok {
  1134. _, items := budget.GetItemModels(map[string]interface{}{
  1135. "id": itemId,
  1136. "deleted_at": 0,
  1137. })
  1138. if len(items) != 0 {
  1139. context.Order.Models[Item][itemId] = items[0]
  1140. }
  1141. }
  1142. if item, ok := context.Order.Models[Item][itemId]; ok {
  1143. return item
  1144. }
  1145. return nil
  1146. }
  1147. func (context *Context) LoadSubs(instance *InstanceData, typ int, oldSubs *[]ObjectData, quoteDatas *[]budget.QuoteData) ([]ObjectData, error) {
  1148. subs := make([]ObjectData, 0)
  1149. for _, qsub := range *quoteDatas {
  1150. var object *ObjectData
  1151. for i, sub := range *oldSubs {
  1152. if sub.ID == qsub.GetID() {
  1153. object = &(*oldSubs)[i]
  1154. }
  1155. }
  1156. if object == nil {
  1157. object = new(ObjectData)
  1158. object.ID = qsub.GetID()
  1159. object.Init(instance, typ, context.Order)
  1160. }
  1161. nameResult, err := context.GetForm(qsub.GetName(), object.NameResult)
  1162. if err != nil {
  1163. return nil, err
  1164. }
  1165. object.NameResult = nameResult
  1166. var instanceNames []interface{}
  1167. if names, ok := object.NameResult.Result.([]interface{}); ok {
  1168. instanceNames = names
  1169. } else {
  1170. instanceNames = []interface{}{object.NameResult.Result}
  1171. }
  1172. objectInstanceMap := make(map[string]*InstanceData)
  1173. for i, instance := range object.Instances {
  1174. if instance.Key != "" {
  1175. objectInstanceMap[instance.Key] = &object.Instances[i]
  1176. }
  1177. }
  1178. subInstances := make([]InstanceData, 0)
  1179. for i, instanceName := range instanceNames {
  1180. var subInstance *InstanceData
  1181. var name, show, key string
  1182. var itemid int64
  1183. if nameMap, ok := instanceName.(map[string]interface{}); ok {
  1184. name = db.ToString(nameMap["name"])
  1185. show = db.ToString(nameMap["show"])
  1186. itemid, _ = db.ToInt64(nameMap["item"])
  1187. key = db.ToString(nameMap["key"])
  1188. } else {
  1189. name = db.ToString(instanceName)
  1190. }
  1191. if key == "" {
  1192. if len(object.Instances) > i {
  1193. subInstance = &object.Instances[i]
  1194. }
  1195. } else {
  1196. if i, ok := objectInstanceMap[key]; ok {
  1197. subInstance = i
  1198. }
  1199. }
  1200. if subInstance == nil {
  1201. subInstance = new(InstanceData)
  1202. } else {
  1203. delete(objectInstanceMap, subInstance.Key)
  1204. }
  1205. subInstance.Name = name
  1206. subInstance.Show = show
  1207. subInstance.ItemId = itemid
  1208. subInstance.Key = key
  1209. subInstance.Init(instance, object, object.Type, i, context.Order)
  1210. subInstances = append(subInstances, *subInstance)
  1211. }
  1212. object.Instances = subInstances
  1213. subs = append(subs, *object)
  1214. }
  1215. return subs, nil
  1216. }
  1217. func (context *Context) LoadNameResult(instance *InstanceData) (*PropForm, error) {
  1218. last := context.Current
  1219. context.Current = instance
  1220. defer func() {
  1221. context.Current = last
  1222. }()
  1223. nameResult, err := context.GetForm(instance.Name, instance.NameResult)
  1224. if err != nil {
  1225. return nil, err
  1226. }
  1227. if nameResult != nil && instance.Object != nil && instance.Object.NameResult != nil {
  1228. instance.Object.NameResult.AddUseKey(nameResult.Key)
  1229. context.Order.SetPropUse(nameResult.Key, instance.Object.NameResult.Key)
  1230. }
  1231. instance.SetNameResult(nameResult)
  1232. return nameResult, nil
  1233. }
  1234. func (context *Context) LoadShowResult(instance *InstanceData) (*PropForm, error) {
  1235. last := context.Current
  1236. context.Current = instance
  1237. defer func() {
  1238. context.Current = last
  1239. }()
  1240. showResult, err := context.GetForm(instance.Show, instance.ShowResult)
  1241. if err != nil {
  1242. return nil, err
  1243. }
  1244. if showResult != nil && instance.Object != nil && instance.Object.NameResult != nil {
  1245. instance.Object.NameResult.AddUseKey(showResult.Key)
  1246. context.Order.SetPropUse(showResult.Key, instance.Object.NameResult.Key)
  1247. }
  1248. instance.SetShowResult(showResult)
  1249. return showResult, nil
  1250. }
  1251. func (context *Context) EvalRowHeaderResult(row *InstanceData, table *InstanceData) error {
  1252. last := context.Current
  1253. context.Current = row
  1254. defer func() {
  1255. context.Current = last
  1256. }()
  1257. for _, header := range table.Headers {
  1258. row.HeaderResult[header.ID] = make([]*PropForm, 0)
  1259. for _, hInstance := range header.Instances {
  1260. if hInstance.Result == nil {
  1261. row.HeaderResult[header.ID] = append(row.HeaderResult[header.ID], nil)
  1262. continue
  1263. }
  1264. prop, err := context.GetProp(db.ToString(hInstance.Result.Result))
  1265. if err != nil {
  1266. return err
  1267. }
  1268. if prop != nil {
  1269. prop.Header = true
  1270. }
  1271. row.HeaderResult[header.ID] = append(row.HeaderResult[header.ID], prop)
  1272. }
  1273. }
  1274. return nil
  1275. }
  1276. func (context *Context) EvalResult() error {
  1277. err := context.LoadInstance(context.Order.Form)
  1278. if err != nil {
  1279. return err
  1280. }
  1281. for i := range context.Order.Form.Subs {
  1282. table := &context.Order.Form.Subs[i]
  1283. for tableIndex := 0; tableIndex < len(table.Instances); tableIndex++ {
  1284. tableInstance := &table.Instances[tableIndex]
  1285. _, err := context.LoadNameResult(tableInstance)
  1286. if err != nil {
  1287. return err
  1288. }
  1289. _, err = context.LoadShowResult(tableInstance)
  1290. if err != nil {
  1291. return err
  1292. }
  1293. err = context.LoadInstance(tableInstance)
  1294. if err != nil {
  1295. return err
  1296. }
  1297. for i := range tableInstance.Headers {
  1298. header := &tableInstance.Headers[i]
  1299. for headerIndex := 0; headerIndex < len(header.Instances); headerIndex++ {
  1300. _, err := context.LoadNameResult(&header.Instances[headerIndex])
  1301. if err != nil {
  1302. return err
  1303. }
  1304. _, err = context.LoadShowResult(&header.Instances[headerIndex])
  1305. if err != nil {
  1306. return err
  1307. }
  1308. err = context.LoadInstance(&header.Instances[headerIndex])
  1309. if err != nil {
  1310. return err
  1311. }
  1312. }
  1313. }
  1314. for i := range tableInstance.Subs {
  1315. module := &tableInstance.Subs[i]
  1316. for moduleIndex := 0; moduleIndex < len(module.Instances); moduleIndex++ {
  1317. moduleInstance := &module.Instances[moduleIndex]
  1318. _, err := context.LoadNameResult(moduleInstance)
  1319. if err != nil {
  1320. return err
  1321. }
  1322. _, err = context.LoadShowResult(moduleInstance)
  1323. if err != nil {
  1324. return err
  1325. }
  1326. err = context.LoadInstance(moduleInstance)
  1327. if err != nil {
  1328. return err
  1329. }
  1330. for i := range moduleInstance.Subs {
  1331. group := &moduleInstance.Subs[i]
  1332. for groupIndex := 0; groupIndex < len(group.Instances); groupIndex++ {
  1333. groupInstance := &group.Instances[groupIndex]
  1334. _, err := context.LoadNameResult(groupInstance)
  1335. if err != nil {
  1336. return err
  1337. }
  1338. _, err = context.LoadShowResult(groupInstance)
  1339. if err != nil {
  1340. return err
  1341. }
  1342. err = context.LoadInstance(groupInstance)
  1343. if err != nil {
  1344. return err
  1345. }
  1346. for i := range groupInstance.Subs {
  1347. row := &groupInstance.Subs[i]
  1348. for rowIndex := 0; rowIndex < len(row.Instances); rowIndex++ {
  1349. rowInstance := &row.Instances[rowIndex]
  1350. _, err := context.LoadNameResult(rowInstance)
  1351. if err != nil {
  1352. return err
  1353. }
  1354. _, err = context.LoadShowResult(rowInstance)
  1355. if err != nil {
  1356. return err
  1357. }
  1358. err = context.LoadInstance(rowInstance)
  1359. if err != nil {
  1360. return err
  1361. }
  1362. err = context.EvalRowHeaderResult(rowInstance, tableInstance)
  1363. if err != nil {
  1364. return err
  1365. }
  1366. }
  1367. }
  1368. }
  1369. }
  1370. }
  1371. }
  1372. }
  1373. }
  1374. return nil
  1375. }
  1376. func (context *Context) Eval() error {
  1377. //if context.Current == nil {
  1378. /*err := context.LoadInstance(context.Order.Form)
  1379. if err != nil {
  1380. return err
  1381. }*/
  1382. return context.EvalResult()
  1383. //}
  1384. return nil
  1385. }
  1386. func (context *Context) Handle(key uint64, typ string, value interface{}) error {
  1387. if prop, ok := context.Order.Prop[key]; ok {
  1388. last := context.Current
  1389. context.Current = prop.Instance
  1390. defer func() {
  1391. context.Current = last
  1392. }()
  1393. if prop.Control == nil {
  1394. return errors.New("undeclared control: " + db.ToString(key))
  1395. }
  1396. err := prop.Control.Handle(typ, value, prop, context)
  1397. if err != nil {
  1398. context.Error(err)
  1399. return err
  1400. }
  1401. } else {
  1402. return errors.New("undeclared object: " + db.ToString(key))
  1403. }
  1404. return nil
  1405. }