order.go 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813
  1. package budget
  2. import (
  3. "bytes"
  4. "compress/gzip"
  5. "encoding/json"
  6. "errors"
  7. "io/ioutil"
  8. "reflect"
  9. "strings"
  10. "time"
  11. "zhiyuan/models/budget"
  12. "zhiyuan/pkg/config"
  13. "zhiyuan/pkg/db"
  14. "zhiyuan/pkg/logger"
  15. "zhiyuan/pkg/utils"
  16. "github.com/gin-gonic/gin"
  17. "github.com/tealeg/xlsx/v3"
  18. )
  19. const (
  20. Modify int = iota
  21. Immediate
  22. Save
  23. )
  24. const (
  25. Base int = iota
  26. Quote
  27. Table
  28. Header
  29. Module
  30. Group
  31. Row
  32. Item
  33. )
  34. /*type Object interface {
  35. GetID() int64
  36. Init(Instance, *Order)
  37. GetParentInstance() Instance
  38. Count() int
  39. Copy(Instance, bool, bool) Object
  40. GetInstances() []Instance
  41. }
  42. type Instance interface {
  43. GetID() int64
  44. Init(Object, int, *Order)
  45. GetObject() Object
  46. GetIndex() int
  47. GetParent() Instance
  48. GetSub() []Instance
  49. GetName() string
  50. GetShow() string
  51. GetNameResult() *PropForm
  52. GetShowResult() *PropForm
  53. SetNameResult(prop *PropForm)
  54. SetShowResult(prop *PropForm)
  55. GetShowResultBool() bool
  56. GetPropResult(name string) *PropForm
  57. SetPropResult(name string, prop *PropForm)
  58. Copy(Object, bool, bool) Instance
  59. Type() int
  60. }*/
  61. type PropForm struct {
  62. Key uint64 `json:"k"`
  63. Cycle int `json:"c" label:"生命周期"`
  64. Control *Control `json:"l"`
  65. //Instance Instance `json:"-"`
  66. Instance *InstanceData `json:"-"`
  67. Header bool `json:"-"`
  68. Result interface{} `json:"r" label:"结果"`
  69. UseKeys []uint64 `json:"s"`
  70. }
  71. // func (prop *PropForm) Init(instance Instance, order *Order) {
  72. func (prop *PropForm) Init(instance *InstanceData, order *Order) {
  73. prop.Instance = instance
  74. order.Prop[prop.Key] = prop
  75. if prop.Key > order.PropKeyMax {
  76. order.PropKeyMax = prop.Key
  77. }
  78. for _, k := range prop.UseKeys {
  79. order.SetPropUse(k, prop.Key)
  80. }
  81. }
  82. func (prop *PropForm) AddUseKey(key uint64) {
  83. for _, k := range prop.UseKeys {
  84. if k == key {
  85. return
  86. }
  87. }
  88. prop.UseKeys = append(prop.UseKeys, key)
  89. }
  90. func (prop *PropForm) ToString() string {
  91. return db.ToString(prop.Result)
  92. }
  93. func (prop *PropForm) ToMap(save bool) map[string]interface{} {
  94. if prop == nil {
  95. return nil
  96. }
  97. mp := make(map[string]interface{})
  98. if !save || prop.Cycle != Immediate {
  99. argParse([]interface{}{prop.Result}, func(result interface{}) {
  100. if _, ok := result.(*InstanceData); ok {
  101. logger.Sugar.Infof("prop.ToMap(instance): %v %v %v", prop.Instance.Type, prop.Instance.GetID(), prop.Instance.Name)
  102. }
  103. })
  104. mp["r"] = prop.Result
  105. }
  106. if prop.Control != nil {
  107. mp["k"] = prop.Key
  108. mp["l"] = prop.Control
  109. }
  110. if save {
  111. mp["s"] = prop.UseKeys
  112. mp["k"] = prop.Key
  113. mp["c"] = prop.Cycle
  114. }
  115. return mp
  116. }
  117. /*type BaseInstance struct {
  118. Object Object `json:"-" label:"对象"`
  119. Index int `json:"-" label:"对象"`
  120. Name string `json:"n" label:"名称"`
  121. NameResult *PropForm `json:"nr" label:"名称结果"`
  122. Show string `json:"s" label:"显示"`
  123. ShowResult *PropForm `json:"sr" label:"显示结果"`
  124. ItemId int64 `json:"t"`
  125. Key string `json:"k"`
  126. PropResult map[string]*PropForm `json:"p" label:"属性结果"`
  127. }
  128. func (instance *BaseInstance) GetID() int64 {
  129. object := instance.GetObject()
  130. if object == nil {
  131. return 0
  132. }
  133. return object.GetID()
  134. }
  135. func (instance *BaseInstance) Init(object Object, index int, order *Order) {
  136. instance.Object = object
  137. instance.Index = index
  138. if instance.PropResult == nil {
  139. instance.PropResult = make(map[string]*PropForm)
  140. }
  141. if instance.NameResult != nil {
  142. instance.NameResult.Init(instance, order)
  143. }
  144. if instance.ShowResult != nil {
  145. instance.ShowResult.Init(instance, order)
  146. }
  147. for name, _ := range instance.PropResult {
  148. instance.PropResult[name].Init(instance, order)
  149. }
  150. }
  151. func (instance *BaseInstance) GetObject() Object {
  152. return instance.Object
  153. }
  154. func (instance *BaseInstance) GetIndex() int {
  155. return instance.Index
  156. }
  157. func (instance *BaseInstance) GetParent() Instance {
  158. object := instance.GetObject()
  159. if object == nil {
  160. return nil
  161. }
  162. return object.GetParentInstance()
  163. }
  164. func (instance *BaseInstance) GetName() string {
  165. return instance.Name
  166. }
  167. func (instance *BaseInstance) GetShow() string {
  168. return instance.Show
  169. }
  170. func (instance *BaseInstance) GetNameResult() *PropForm {
  171. return instance.NameResult
  172. }
  173. func (instance *BaseInstance) GetNameResultString() string {
  174. nameResult := instance.GetNameResult()
  175. if nameResult == nil {
  176. return ""
  177. }
  178. return nameResult.ToString()
  179. }
  180. func (instance *BaseInstance) GetShowResult() *PropForm {
  181. return instance.ShowResult
  182. }
  183. func (instance *BaseInstance) GetShowResultBool() bool {
  184. if instance.GetShow() == "" {
  185. return true
  186. }
  187. showResult := instance.GetShowResult()
  188. if showResult == nil {
  189. return false
  190. }
  191. return db.ToBool(showResult.Result)
  192. }
  193. func (instance *BaseInstance) SetNameResult(prop *PropForm) {
  194. instance.NameResult = prop
  195. }
  196. func (instance *BaseInstance) SetShowResult(prop *PropForm) {
  197. instance.ShowResult = prop
  198. }
  199. func (instance *BaseInstance) GetPropResult(name string) *PropForm {
  200. if prop, ok := instance.PropResult[name]; ok {
  201. return prop
  202. }
  203. return nil
  204. }
  205. func (instance *BaseInstance) SetPropResult(name string, prop *PropForm) {
  206. instance.PropResult[name] = prop
  207. }
  208. func (instance *BaseInstance) Copy(object Object, getall bool, save bool) Instance {
  209. s := new(BaseInstance)
  210. s.Object = object
  211. s.Index = instance.Index
  212. s.Name = instance.Name
  213. if !save || instance.NameResult != nil && instance.NameResult.Cycle != Immediate {
  214. s.NameResult = instance.NameResult
  215. }
  216. s.Show = instance.Show
  217. if !save || instance.ShowResult != nil && instance.ShowResult.Cycle != Immediate {
  218. s.ShowResult = instance.ShowResult
  219. }
  220. s.PropResult = make(map[string]*PropForm)
  221. for name, prop := range instance.PropResult {
  222. if getall || len(prop.UseKeys) != 0 && !prop.Header && prop.Control != nil {
  223. if !save || prop.Cycle != Immediate {
  224. s.PropResult[name] = prop
  225. s.PropResult[name].Instance = s
  226. }
  227. }
  228. }
  229. return s
  230. }
  231. func (instance *BaseInstance) ToMap(save bool) map[string]interface{} {
  232. mp := make(map[string]interface{})
  233. if save {
  234. mp["n"] = instance.Name
  235. mp["s"] = instance.Show
  236. mp["t"] = instance.ItemId
  237. mp["k"] = instance.Key
  238. }
  239. if !save || instance.NameResult != nil {
  240. mp["nr"] = instance.NameResult.ToMap(save)
  241. }
  242. if !save || instance.ShowResult != nil {
  243. mp["sr"] = instance.ShowResult.ToMap(save)
  244. }
  245. propResult := make(map[string]interface{})
  246. for name, prop := range instance.PropResult {
  247. if save || !save && len(prop.UseKeys) != 0 && !prop.Header && prop.Control != nil {
  248. propResult[name] = prop.ToMap(save)
  249. }
  250. }
  251. mp["p"] = propResult
  252. return mp
  253. }
  254. func (instance *BaseInstance) GetSub() []Instance {
  255. return []Instance{}
  256. }
  257. func (instance *BaseInstance) Type() int {
  258. return Base
  259. }
  260. type BaseObject struct {
  261. ID int64 `json:"i" label:"ID" binding:"required"`
  262. NameResult *PropForm `json:"nr" label:"名称结果"`
  263. Parent Instance `json:"-" label:"父实例"`
  264. }
  265. func (object *BaseObject) GetID() int64 {
  266. return object.ID
  267. }
  268. func (object *BaseObject) Init(parent Instance, order *Order) {
  269. object.Parent = parent
  270. if object.NameResult != nil {
  271. object.NameResult.Init(parent, order)
  272. }
  273. }
  274. func (object *BaseObject) GetParentInstance() Instance {
  275. return object.Parent
  276. }
  277. func (object *BaseObject) Copy(parent Instance, getall bool, save bool) Object {
  278. s := new(BaseObject)
  279. s.ID = object.ID
  280. if !save || object.NameResult != nil && object.NameResult.Cycle != Immediate {
  281. s.NameResult = object.NameResult
  282. }
  283. s.Parent = parent
  284. return s
  285. }
  286. func (object *BaseObject) ToMap(save bool) map[string]interface{} {
  287. mp := make(map[string]interface{})
  288. if save {
  289. mp["i"] = object.ID
  290. }
  291. if !save || object.NameResult != nil {
  292. mp["nr"] = object.NameResult.ToMap(save)
  293. }
  294. return mp
  295. }
  296. func (object *BaseObject) Count() int {
  297. return 0
  298. }
  299. func (object *BaseObject) GetInstances() []Instance {
  300. return []Instance{}
  301. }
  302. type ItemInstance struct {
  303. ID int64 `json:"i" label:"ID" binding:"required"`
  304. Row *RowInstance `json:"-" label:"行"`
  305. PropResult map[string]*PropForm `json:"p" label:"属性结果"`
  306. }
  307. func (instance *ItemInstance) GetID() int64 {
  308. return instance.ID
  309. }
  310. func (instance *ItemInstance) Init(object Object, index int, order *Order) {
  311. if instance.PropResult == nil {
  312. instance.PropResult = make(map[string]*PropForm)
  313. }
  314. for name, _ := range instance.PropResult {
  315. instance.PropResult[name].Init(instance, order)
  316. }
  317. }
  318. func (instance *ItemInstance) GetIndex() int {
  319. return instance.Row.Index
  320. }
  321. func (instance *ItemInstance) GetObject() Object {
  322. return instance.Row.Object
  323. }
  324. func (instance *ItemInstance) GetParent() Instance {
  325. return instance.Row
  326. }
  327. func (instance *ItemInstance) GetName() string {
  328. return ""
  329. }
  330. func (instance *ItemInstance) GetShow() string {
  331. return ""
  332. }
  333. func (instance *ItemInstance) GetNameResult() *PropForm {
  334. return nil
  335. }
  336. func (instance *ItemInstance) GetShowResult() *PropForm {
  337. return nil
  338. }
  339. func (instance *ItemInstance) GetShowResultBool() bool {
  340. return false
  341. }
  342. func (instance *ItemInstance) SetNameResult(prop *PropForm) {
  343. }
  344. func (instance *ItemInstance) SetShowResult(prop *PropForm) {
  345. }
  346. func (instance *ItemInstance) GetPropResult(name string) *PropForm {
  347. if prop, ok := instance.PropResult[name]; ok {
  348. return prop
  349. }
  350. return nil
  351. }
  352. func (instance *ItemInstance) SetPropResult(name string, prop *PropForm) {
  353. instance.PropResult[name] = prop
  354. }
  355. func (instance *ItemInstance) Copy(object Object, getall bool, save bool) Instance {
  356. s := new(ItemInstance)
  357. s.ID = instance.ID
  358. s.PropResult = make(map[string]*PropForm)
  359. for name, prop := range instance.PropResult {
  360. if getall || len(prop.UseKeys) != 0 && !prop.Header && prop.Control != nil {
  361. if !save || prop.Cycle != Immediate {
  362. s.PropResult[name] = prop
  363. s.PropResult[name].Instance = s
  364. }
  365. }
  366. }
  367. return s
  368. }
  369. func (instance *ItemInstance) ToMap(save bool) map[string]interface{} {
  370. mp := make(map[string]interface{})
  371. if save {
  372. mp["i"] = instance.ID
  373. }
  374. propResult := make(map[string]interface{})
  375. for name, prop := range instance.PropResult {
  376. if save || !save && len(prop.UseKeys) != 0 && !prop.Header && prop.Control != nil {
  377. propResult[name] = prop.ToMap(save)
  378. }
  379. }
  380. mp["p"] = propResult
  381. return mp
  382. }
  383. func (instance *ItemInstance) GetSub() []Instance {
  384. return []Instance{}
  385. }
  386. func (instance *ItemInstance) Type() int {
  387. return Item
  388. }
  389. type RowInstance struct {
  390. BaseInstance
  391. Item *ItemInstance `json:"m" label:"项目"`
  392. HeaderResult map[int64][]*PropForm `json:"hr"`
  393. }
  394. func (instance *RowInstance) Init(object Object, index int, order *Order) {
  395. instance.BaseInstance.Init(object, index, order)
  396. if instance.Item != nil {
  397. instance.Item.Row = instance
  398. instance.Item.Init(object, 0, order)
  399. }
  400. if instance.HeaderResult == nil {
  401. instance.HeaderResult = make(map[int64][]*PropForm)
  402. }
  403. for name, _ := range instance.PropResult {
  404. instance.PropResult[name].Init(instance, order)
  405. }
  406. }
  407. func (instance *RowInstance) Copy(object Object, getall bool, save bool) Instance {
  408. s := new(RowInstance)
  409. s.BaseInstance = *instance.BaseInstance.Copy(object, getall, save).(*BaseInstance)
  410. if instance.Item != nil {
  411. s.Item = instance.Item.Copy(object, getall, save).(*ItemInstance)
  412. }
  413. s.HeaderResult = instance.HeaderResult
  414. return s
  415. }
  416. func (instance *RowInstance) ToMap(save bool) map[string]interface{} {
  417. mp := instance.BaseInstance.ToMap(save)
  418. if instance.Item != nil {
  419. mp["m"] = instance.Item.ToMap(save)
  420. }
  421. if !save {
  422. headerResult := make(map[int64][]interface{})
  423. for id, results := range instance.HeaderResult {
  424. headerResult[id] = make([]interface{}, 0)
  425. for _, result := range results {
  426. headerResult[id] = append(headerResult[id], result.ToMap(save))
  427. }
  428. }
  429. mp["hr"] = headerResult
  430. }
  431. return mp
  432. }
  433. func (instance *RowInstance) GetSub() []Instance {
  434. if instance.Item != nil {
  435. return []Instance{instance.Item}
  436. }
  437. return []Instance{}
  438. }
  439. func (instance *RowInstance) Type() int {
  440. return Row
  441. }
  442. type RowObject struct {
  443. BaseObject
  444. Instances []RowInstance `json:"is" label:"实例"`
  445. }
  446. func (object *RowObject) Init(parent Instance, order *Order) {
  447. object.BaseObject.Init(parent, order)
  448. if object.Instances == nil {
  449. object.Instances = make([]RowInstance, 0)
  450. }
  451. for i, _ := range object.Instances {
  452. object.Instances[i].Init(object, i, order)
  453. }
  454. }
  455. func (object *RowObject) Copy(parent Instance, getall bool, save bool) Object {
  456. s := new(RowObject)
  457. s.BaseObject = *object.BaseObject.Copy(parent, getall, save).(*BaseObject)
  458. s.Instances = make([]RowInstance, 0)
  459. for _, instance := range object.Instances {
  460. if getall || instance.GetShowResultBool() {
  461. s.Instances = append(s.Instances, *instance.Copy(s, getall, save).(*RowInstance))
  462. }
  463. }
  464. return s
  465. }
  466. func (object *RowObject) ToMap(save bool) map[string]interface{} {
  467. mp := object.BaseObject.ToMap(save)
  468. instances := make([]interface{}, 0)
  469. for _, instance := range object.Instances {
  470. if save || instance.GetShowResultBool() {
  471. instances = append(instances, instance.ToMap(save))
  472. }
  473. }
  474. mp["is"] = instances
  475. return mp
  476. }
  477. func (object *RowObject) Count() int {
  478. return len(object.Instances)
  479. }
  480. func (object *RowObject) GetInstances() []Instance {
  481. instances := make([]Instance, 0)
  482. for i, _ := range object.Instances {
  483. instances = append(instances, &object.Instances[i])
  484. }
  485. return instances
  486. }
  487. type GroupInstance struct {
  488. BaseInstance
  489. Rows []RowObject `json:"o" label:"行"`
  490. }
  491. func (instance *GroupInstance) Init(object Object, index int, order *Order) {
  492. instance.BaseInstance.Init(object, index, order)
  493. if instance.Rows == nil {
  494. instance.Rows = make([]RowObject, 0)
  495. }
  496. for i, _ := range instance.Rows {
  497. instance.Rows[i].Init(instance, order)
  498. }
  499. for name, _ := range instance.PropResult {
  500. instance.PropResult[name].Init(instance, order)
  501. }
  502. }
  503. func (instance *GroupInstance) Copy(object Object, getall bool, save bool) Instance {
  504. s := new(GroupInstance)
  505. s.BaseInstance = *instance.BaseInstance.Copy(object, getall, save).(*BaseInstance)
  506. s.Rows = make([]RowObject, 0)
  507. for _, row := range instance.Rows {
  508. s.Rows = append(s.Rows, *row.Copy(s, getall, save).(*RowObject))
  509. }
  510. return s
  511. }
  512. func (instance *GroupInstance) ToMap(save bool) map[string]interface{} {
  513. mp := instance.BaseInstance.ToMap(save)
  514. rows := make([]interface{}, 0)
  515. for _, row := range instance.Rows {
  516. rows = append(rows, row.ToMap(save))
  517. }
  518. mp["o"] = rows
  519. return mp
  520. }
  521. func (instance *GroupInstance) GetSub() []Instance {
  522. subs := make([]Instance, 0)
  523. for _, row := range instance.Rows {
  524. for i, _ := range row.Instances {
  525. subs = append(subs, &row.Instances[i])
  526. }
  527. }
  528. return subs
  529. }
  530. func (instance *GroupInstance) Type() int {
  531. return Group
  532. }
  533. type GroupObject struct {
  534. BaseObject
  535. Instances []GroupInstance `json:"is" label:"实例"`
  536. }
  537. func (object *GroupObject) Init(parent Instance, order *Order) {
  538. object.BaseObject.Init(parent, order)
  539. if object.Instances == nil {
  540. object.Instances = make([]GroupInstance, 0)
  541. }
  542. for i, _ := range object.Instances {
  543. object.Instances[i].Init(object, i, order)
  544. }
  545. }
  546. func (object *GroupObject) Copy(parent Instance, getall bool, save bool) Object {
  547. s := new(GroupObject)
  548. s.BaseObject = *object.BaseObject.Copy(parent, getall, save).(*BaseObject)
  549. s.Instances = make([]GroupInstance, 0)
  550. for _, instance := range object.Instances {
  551. if getall || instance.GetShowResultBool() {
  552. s.Instances = append(s.Instances, *instance.Copy(s, getall, save).(*GroupInstance))
  553. }
  554. }
  555. return s
  556. }
  557. func (object *GroupObject) ToMap(save bool) map[string]interface{} {
  558. mp := object.BaseObject.ToMap(save)
  559. instances := make([]interface{}, 0)
  560. for _, instance := range object.Instances {
  561. if save || instance.GetShowResultBool() {
  562. instances = append(instances, instance.ToMap(save))
  563. }
  564. }
  565. mp["is"] = instances
  566. return mp
  567. }
  568. func (object *GroupObject) Count() int {
  569. return len(object.Instances)
  570. }
  571. func (object *GroupObject) GetInstances() []Instance {
  572. instances := make([]Instance, 0)
  573. for i, _ := range object.Instances {
  574. instances = append(instances, &object.Instances[i])
  575. }
  576. return instances
  577. }
  578. type ModuleInstance struct {
  579. BaseInstance
  580. Groups []GroupObject `json:"o" label:"组"`
  581. }
  582. func (instance *ModuleInstance) Init(object Object, index int, order *Order) {
  583. instance.BaseInstance.Init(object, index, order)
  584. if instance.Groups == nil {
  585. instance.Groups = make([]GroupObject, 0)
  586. }
  587. for i, _ := range instance.Groups {
  588. instance.Groups[i].Init(instance, order)
  589. }
  590. for name, _ := range instance.PropResult {
  591. instance.PropResult[name].Init(instance, order)
  592. }
  593. }
  594. func (instance *ModuleInstance) Copy(object Object, getall bool, save bool) Instance {
  595. s := new(ModuleInstance)
  596. s.BaseInstance = *instance.BaseInstance.Copy(object, getall, save).(*BaseInstance)
  597. s.Groups = make([]GroupObject, 0)
  598. for _, group := range instance.Groups {
  599. s.Groups = append(s.Groups, *group.Copy(s, getall, save).(*GroupObject))
  600. }
  601. return s
  602. }
  603. func (instance *ModuleInstance) ToMap(save bool) map[string]interface{} {
  604. mp := instance.BaseInstance.ToMap(save)
  605. groups := make([]interface{}, 0)
  606. for _, group := range instance.Groups {
  607. groups = append(groups, group.ToMap(save))
  608. }
  609. mp["o"] = groups
  610. return mp
  611. }
  612. func (instance *ModuleInstance) GetSub() []Instance {
  613. subs := make([]Instance, 0)
  614. for _, group := range instance.Groups {
  615. for i, _ := range group.Instances {
  616. subs = append(subs, &group.Instances[i])
  617. }
  618. }
  619. return subs
  620. }
  621. func (instance *ModuleInstance) Type() int {
  622. return Module
  623. }
  624. type ModuleObject struct {
  625. BaseObject
  626. Instances []ModuleInstance `json:"is" label:"实例"`
  627. }
  628. func (object *ModuleObject) Init(parent Instance, order *Order) {
  629. object.BaseObject.Init(parent, order)
  630. if object.Instances == nil {
  631. object.Instances = make([]ModuleInstance, 0)
  632. }
  633. for i, _ := range object.Instances {
  634. object.Instances[i].Init(object, i, order)
  635. }
  636. }
  637. func (object *ModuleObject) Copy(parent Instance, getall bool, save bool) Object {
  638. s := new(ModuleObject)
  639. s.BaseObject = *object.BaseObject.Copy(parent, getall, save).(*BaseObject)
  640. s.Instances = make([]ModuleInstance, 0)
  641. for _, instance := range object.Instances {
  642. if getall || instance.GetShowResultBool() {
  643. s.Instances = append(s.Instances, *instance.Copy(s, getall, save).(*ModuleInstance))
  644. }
  645. }
  646. return s
  647. }
  648. func (object *ModuleObject) ToMap(save bool) map[string]interface{} {
  649. mp := object.BaseObject.ToMap(save)
  650. instances := make([]interface{}, 0)
  651. for _, instance := range object.Instances {
  652. if save || instance.GetShowResultBool() {
  653. instances = append(instances, instance.ToMap(save))
  654. }
  655. }
  656. mp["is"] = instances
  657. return mp
  658. }
  659. func (object *ModuleObject) Count() int {
  660. return len(object.Instances)
  661. }
  662. func (object *ModuleObject) GetInstances() []Instance {
  663. instances := make([]Instance, 0)
  664. for i, _ := range object.Instances {
  665. instances = append(instances, &object.Instances[i])
  666. }
  667. return instances
  668. }
  669. type HeaderInstance struct {
  670. BaseInstance
  671. Result *PropForm `json:"r" label:"计算结果"`
  672. }
  673. func (instance *HeaderInstance) Init(object Object, index int, order *Order) {
  674. instance.BaseInstance.Init(object, index, order)
  675. if instance.Result != nil {
  676. instance.Result.Init(instance, order)
  677. }
  678. for name, _ := range instance.PropResult {
  679. instance.PropResult[name].Init(instance, order)
  680. }
  681. }
  682. func (instance *HeaderInstance) Copy(object Object, getall bool, save bool) Instance {
  683. s := new(HeaderInstance)
  684. s.BaseInstance = *instance.BaseInstance.Copy(object, getall, save).(*BaseInstance)
  685. s.Result = instance.Result
  686. return s
  687. }
  688. func (instance *HeaderInstance) ToMap(save bool) map[string]interface{} {
  689. mp := instance.BaseInstance.ToMap(save)
  690. mp["r"] = instance.Result.ToMap(save)
  691. return mp
  692. }
  693. func (instance *HeaderInstance) GetSub() []Instance {
  694. return []Instance{}
  695. }
  696. func (instance *HeaderInstance) Type() int {
  697. return Header
  698. }
  699. type HeaderObject struct {
  700. BaseObject
  701. Instances []HeaderInstance `json:"is" label:"实例"`
  702. }
  703. func (object *HeaderObject) Init(parent Instance, order *Order) {
  704. object.BaseObject.Init(parent, order)
  705. if object.Instances == nil {
  706. object.Instances = make([]HeaderInstance, 0)
  707. }
  708. for i, _ := range object.Instances {
  709. object.Instances[i].Init(object, i, order)
  710. }
  711. }
  712. func (object *HeaderObject) Copy(parent Instance, getall bool, save bool) Object {
  713. s := new(HeaderObject)
  714. s.BaseObject = *object.BaseObject.Copy(parent, getall, save).(*BaseObject)
  715. s.Instances = make([]HeaderInstance, 0)
  716. for _, instance := range object.Instances {
  717. if getall || instance.GetShowResultBool() {
  718. s.Instances = append(s.Instances, *instance.Copy(s, getall, save).(*HeaderInstance))
  719. }
  720. }
  721. return s
  722. }
  723. func (object *HeaderObject) ToMap(save bool) map[string]interface{} {
  724. mp := object.BaseObject.ToMap(save)
  725. mp["i"] = object.ID
  726. instances := make([]interface{}, 0)
  727. for _, instance := range object.Instances {
  728. if save || instance.GetShowResultBool() {
  729. instances = append(instances, instance.ToMap(save))
  730. }
  731. }
  732. mp["is"] = instances
  733. return mp
  734. }
  735. func (object *HeaderObject) Count() int {
  736. return len(object.Instances)
  737. }
  738. func (object *HeaderObject) GetInstances() []Instance {
  739. instances := make([]Instance, 0)
  740. for i, _ := range object.Instances {
  741. instances = append(instances, &object.Instances[i])
  742. }
  743. return instances
  744. }
  745. type TableInstance struct {
  746. BaseInstance
  747. Headers []HeaderObject `json:"h" label:"表头"`
  748. Modules []ModuleObject `json:"o" label:"模块"`
  749. }
  750. func (instance *TableInstance) Init(object Object, index int, order *Order) {
  751. instance.BaseInstance.Init(object, index, order)
  752. if instance.Headers == nil {
  753. instance.Headers = make([]HeaderObject, 0)
  754. }
  755. if instance.Modules == nil {
  756. instance.Modules = make([]ModuleObject, 0)
  757. }
  758. for i, _ := range instance.Headers {
  759. instance.Headers[i].Init(instance, order)
  760. }
  761. for i, _ := range instance.Modules {
  762. instance.Modules[i].Init(instance, order)
  763. }
  764. for name, _ := range instance.PropResult {
  765. instance.PropResult[name].Init(instance, order)
  766. }
  767. }
  768. func (instance *TableInstance) Copy(object Object, getall bool, save bool) Instance {
  769. s := new(TableInstance)
  770. s.BaseInstance = *instance.BaseInstance.Copy(object, getall, save).(*BaseInstance)
  771. s.Headers = make([]HeaderObject, 0)
  772. s.Modules = make([]ModuleObject, 0)
  773. for _, header := range instance.Headers {
  774. s.Headers = append(s.Headers, *header.Copy(s, getall, save).(*HeaderObject))
  775. }
  776. for _, module := range instance.Modules {
  777. s.Modules = append(s.Modules, *module.Copy(s, getall, save).(*ModuleObject))
  778. }
  779. return s
  780. }
  781. func (instance *TableInstance) ToMap(save bool) map[string]interface{} {
  782. mp := instance.BaseInstance.ToMap(save)
  783. modules := make([]interface{}, 0)
  784. for _, module := range instance.Modules {
  785. modules = append(modules, module.ToMap(save))
  786. }
  787. mp["o"] = modules
  788. headers := make([]interface{}, 0)
  789. for _, header := range instance.Headers {
  790. headers = append(headers, header.ToMap(save))
  791. }
  792. mp["h"] = headers
  793. return mp
  794. }
  795. func (instance *TableInstance) GetSub() []Instance {
  796. subs := make([]Instance, 0)
  797. for _, module := range instance.Modules {
  798. for i, _ := range module.Instances {
  799. subs = append(subs, &module.Instances[i])
  800. }
  801. }
  802. return subs
  803. }
  804. func (instance *TableInstance) Type() int {
  805. return Table
  806. }
  807. type TableObject struct {
  808. BaseObject
  809. Instances []TableInstance `json:"is" label:"实例"`
  810. }
  811. func (object *TableObject) Init(parent Instance, order *Order) {
  812. object.BaseObject.Init(parent, order)
  813. if object.Instances == nil {
  814. object.Instances = make([]TableInstance, 0)
  815. }
  816. for i, _ := range object.Instances {
  817. object.Instances[i].Init(object, i, order)
  818. }
  819. }
  820. func (object *TableObject) Copy(parent Instance, getall bool, save bool) Object {
  821. s := new(TableObject)
  822. s.BaseObject = *object.BaseObject.Copy(parent, getall, save).(*BaseObject)
  823. s.Instances = make([]TableInstance, 0)
  824. for _, instance := range object.Instances {
  825. if getall || instance.GetShowResultBool() {
  826. s.Instances = append(s.Instances, *instance.Copy(s, getall, save).(*TableInstance))
  827. }
  828. }
  829. return s
  830. }
  831. func (object *TableObject) ToMap(save bool) map[string]interface{} {
  832. mp := object.BaseObject.ToMap(save)
  833. instances := make([]interface{}, 0)
  834. for _, instance := range object.Instances {
  835. if save || instance.GetShowResultBool() {
  836. instances = append(instances, instance.ToMap(save))
  837. }
  838. }
  839. mp["is"] = instances
  840. return mp
  841. }
  842. func (object *TableObject) Count() int {
  843. return len(object.Instances)
  844. }
  845. func (object *TableObject) GetInstances() []Instance {
  846. instances := make([]Instance, 0)
  847. for i, _ := range object.Instances {
  848. instances = append(instances, &object.Instances[i])
  849. }
  850. return instances
  851. }
  852. type QuoteForm struct {
  853. ID int64 `json:"i" label:"ID" binding:"required"`
  854. PropResult map[string]*PropForm `json:"p" label:"属性结果"`
  855. Tables []TableObject `json:"o" label:"表格"`
  856. }
  857. func (from *QuoteForm) GetID() int64 {
  858. return from.ID
  859. }
  860. func (from *QuoteForm) Init(object Object, index int, order *Order) {
  861. if from.PropResult == nil {
  862. from.PropResult = make(map[string]*PropForm)
  863. }
  864. for name, _ := range from.PropResult {
  865. from.PropResult[name].Init(from, order)
  866. }
  867. if from.Tables == nil {
  868. from.Tables = make([]TableObject, 0)
  869. }
  870. for i, _ := range from.Tables {
  871. from.Tables[i].Init(from, order)
  872. }
  873. }
  874. func (from *QuoteForm) GetIndex() int {
  875. return 0
  876. }
  877. func (from *QuoteForm) GetObject() Object {
  878. return nil
  879. }
  880. func (from *QuoteForm) GetParent() Instance {
  881. return nil
  882. }
  883. func (from *QuoteForm) GetName() string {
  884. return ""
  885. }
  886. func (from *QuoteForm) GetShow() string {
  887. return ""
  888. }
  889. func (from *QuoteForm) GetNameResult() *PropForm {
  890. return nil
  891. }
  892. func (from *QuoteForm) GetShowResult() *PropForm {
  893. return nil
  894. }
  895. func (from *QuoteForm) GetShowResultBool() bool {
  896. return true
  897. }
  898. func (from *QuoteForm) SetNameResult(prop *PropForm) {
  899. }
  900. func (from *QuoteForm) SetShowResult(prop *PropForm) {
  901. }
  902. func (from *QuoteForm) GetPropResult(name string) *PropForm {
  903. if prop, ok := from.PropResult[name]; ok {
  904. return prop
  905. }
  906. return nil
  907. }
  908. func (from *QuoteForm) SetPropResult(name string, prop *PropForm) {
  909. from.PropResult[name] = prop
  910. }
  911. func (from *QuoteForm) Copy(object Object, getall bool, save bool) Instance {
  912. s := new(QuoteForm)
  913. s.ID = from.ID
  914. s.PropResult = make(map[string]*PropForm)
  915. s.Tables = make([]TableObject, 0)
  916. for name, prop := range from.PropResult {
  917. if getall || len(prop.UseKeys) != 0 && !prop.Header && prop.Control != nil {
  918. if !save || prop.Cycle != Immediate {
  919. s.PropResult[name] = prop
  920. s.PropResult[name].Instance = s
  921. }
  922. }
  923. }
  924. for _, table := range from.Tables {
  925. s.Tables = append(s.Tables, *table.Copy(s, getall, save).(*TableObject))
  926. }
  927. return s
  928. }
  929. func (from *QuoteForm) ToMap(save bool) map[string]interface{} {
  930. mp := make(map[string]interface{})
  931. mp["i"] = from.ID
  932. propResult := make(map[string]interface{})
  933. for name, prop := range from.PropResult {
  934. if save || !save && len(prop.UseKeys) != 0 && !prop.Header && prop.Control != nil {
  935. propResult[name] = prop.ToMap(save)
  936. }
  937. }
  938. mp["p"] = propResult
  939. tables := make([]interface{}, 0)
  940. for _, table := range from.Tables {
  941. tables = append(tables, table.ToMap(save))
  942. }
  943. mp["o"] = tables
  944. return mp
  945. }
  946. func (from *QuoteForm) Count() int {
  947. return 1
  948. }
  949. func (from *QuoteForm) GetSub() []Instance {
  950. subs := make([]Instance, 0)
  951. for _, table := range from.Tables {
  952. for i, _ := range table.Instances {
  953. subs = append(subs, &table.Instances[i])
  954. }
  955. }
  956. return subs
  957. }
  958. func (from *QuoteForm) Type() int {
  959. return Quote
  960. }*/
  961. type Order struct {
  962. ID int64
  963. AdminId int64
  964. CustomerId int64
  965. Models map[int]map[int64]budget.QuoteData
  966. /*Quote *budget.Quote
  967. Table map[int64]budget.Table
  968. Header map[int64]budget.Header
  969. Module map[int64]budget.Module
  970. Group map[int64]budget.Group
  971. Row map[int64]budget.Row
  972. Item map[int64]budget.Item*/
  973. Form *InstanceData
  974. Prop map[uint64]*PropForm
  975. PropKeyMax uint64
  976. PropUseCahce map[uint64][]uint64
  977. }
  978. func (order *Order) SetPropUse(key uint64, ukey uint64) {
  979. if _, ok := order.PropUseCahce[key]; !ok {
  980. order.PropUseCahce[key] = make([]uint64, 0)
  981. }
  982. for _, k := range order.PropUseCahce[key] {
  983. if k == key {
  984. return
  985. }
  986. }
  987. order.PropUseCahce[key] = append(order.PropUseCahce[key], ukey)
  988. }
  989. func (from *InstanceData) LoadOrder() (*Order, error) {
  990. order := new(Order)
  991. quote, tables, headers, modules, groups, rows, items := budget.GetQuoteModel(from.ID)
  992. if quote == nil {
  993. return nil, errors.New("报价不存在")
  994. }
  995. order.Prop = make(map[uint64]*PropForm)
  996. order.PropUseCahce = make(map[uint64][]uint64)
  997. order.Models = make(map[int]map[int64]budget.QuoteData)
  998. order.Models[Quote] = make(map[int64]budget.QuoteData, 0)
  999. order.Models[Quote][quote.ID] = quote
  1000. order.Models[Table] = make(map[int64]budget.QuoteData, 0)
  1001. for _, table := range tables {
  1002. order.Models[Table][table.GetID()] = table
  1003. }
  1004. order.Models[Header] = make(map[int64]budget.QuoteData, 0)
  1005. for _, header := range headers {
  1006. order.Models[Header][header.GetID()] = header
  1007. }
  1008. order.Models[Module] = make(map[int64]budget.QuoteData, 0)
  1009. for _, module := range modules {
  1010. order.Models[Module][module.GetID()] = module
  1011. }
  1012. order.Models[Group] = make(map[int64]budget.QuoteData, 0)
  1013. for _, group := range groups {
  1014. order.Models[Group][group.GetID()] = group
  1015. }
  1016. order.Models[Row] = make(map[int64]budget.QuoteData, 0)
  1017. for _, row := range rows {
  1018. order.Models[Row][row.GetID()] = row
  1019. }
  1020. order.Models[Item] = make(map[int64]budget.QuoteData, 0)
  1021. for _, item := range items {
  1022. order.Models[Item][item.GetID()] = item
  1023. }
  1024. /*order.Table = make(map[int64]budget.Table)
  1025. order.Header = make(map[int64]budget.Header)
  1026. order.Module = make(map[int64]budget.Module)
  1027. order.Group = make(map[int64]budget.Group)
  1028. order.Row = make(map[int64]budget.Row)
  1029. order.Item = make(map[int64]budget.Item)
  1030. for _, table := range tables {
  1031. order.Table[table.ID] = table
  1032. }
  1033. for _, header := range headers {
  1034. order.Header[header.ID] = header
  1035. }
  1036. for _, module := range modules {
  1037. order.Module[module.ID] = module
  1038. }
  1039. for _, group := range groups {
  1040. order.Group[group.ID] = group
  1041. }
  1042. for _, row := range rows {
  1043. order.Row[row.ID] = row
  1044. }
  1045. for _, item := range items {
  1046. order.Item[item.ID] = item
  1047. }
  1048. order.Quote = quote*/
  1049. from.Init(nil, nil, Quote, 0, order)
  1050. order.Form = from
  1051. return order, nil
  1052. }
  1053. func (order *Order) NewPropKey() uint64 {
  1054. order.PropKeyMax++
  1055. return order.PropKeyMax
  1056. }
  1057. func (order *Order) Save(context *Context) error {
  1058. layout, _ := context.ParseValue("=户型信息")
  1059. area, _ := context.ParseValue("=面积信息")
  1060. jsonData, err := json.Marshal(order.ToMap(true))
  1061. if err != nil {
  1062. logger.Sugar.Error(err)
  1063. return err
  1064. }
  1065. //logger.Sugar.Infof("%v", string(jsonData))
  1066. var gzJson bytes.Buffer
  1067. gz := gzip.NewWriter(&gzJson)
  1068. //gz, err := gzip.NewWriterLevel(&gzJson, gzip.BestSpeed)
  1069. //if err != nil {
  1070. // return err
  1071. //}
  1072. if _, err := gz.Write(jsonData); err != nil {
  1073. return err
  1074. }
  1075. if err := gz.Close(); err != nil {
  1076. return err
  1077. }
  1078. content := gzJson.Bytes()
  1079. logger.Sugar.Infof("content len: %v", len(content))
  1080. data := map[string]interface{}{
  1081. "quoteId": order.Form.GetID(),
  1082. "adminId": order.AdminId,
  1083. "content": content,
  1084. "layout": layout,
  1085. "area": area,
  1086. }
  1087. if order.ID == 0 {
  1088. id, err := db.InsertModel(reflect.TypeOf(budget.Order{}), data)
  1089. if err != nil {
  1090. return err
  1091. }
  1092. order.ID = id
  1093. } else {
  1094. err := db.UpdateModel(reflect.TypeOf(budget.Order{}), order.ID, data)
  1095. if err != nil {
  1096. return err
  1097. }
  1098. }
  1099. return nil
  1100. }
  1101. //func (order *Order) GetForm(getall bool, save bool) *QuoteForm {
  1102. // return order.Form.Copy(nil, getall, save).(*QuoteForm)
  1103. //}
  1104. func (order *Order) ToMap(save bool) map[string]interface{} {
  1105. return order.Form.ToMap(save)
  1106. }
  1107. func LoadOrder(model budget.Order) (*Order, error) {
  1108. from := new(InstanceData)
  1109. if len(model.Content) != 0 {
  1110. gzJson := bytes.NewBuffer(model.Content)
  1111. gz, err := gzip.NewReader(gzJson)
  1112. if err != nil {
  1113. return nil, err
  1114. }
  1115. defer gz.Close()
  1116. jsonData, err := ioutil.ReadAll(gz)
  1117. if err != nil {
  1118. return nil, err
  1119. }
  1120. decoder := json.NewDecoder(strings.NewReader(string(jsonData)))
  1121. decoder.UseNumber()
  1122. err = decoder.Decode(&from)
  1123. if err != nil {
  1124. return nil, err
  1125. }
  1126. /*err = json.Unmarshal(jsonData, &from)
  1127. if err != nil {
  1128. return nil, err
  1129. }*/
  1130. }
  1131. if from.ID == 0 {
  1132. from.ID = model.QuoteId
  1133. }
  1134. order, err := from.LoadOrder()
  1135. if err != nil {
  1136. return nil, err
  1137. }
  1138. order.ID = model.ID
  1139. order.AdminId = model.AdminId
  1140. order.CustomerId = model.CustomerId
  1141. return order, nil
  1142. }
  1143. func CreateOrder(quoteId int64) (*Order, error) {
  1144. from := new(InstanceData)
  1145. if from.ID == 0 {
  1146. from.ID = quoteId
  1147. }
  1148. order, err := from.LoadOrder()
  1149. if err != nil {
  1150. return nil, err
  1151. }
  1152. order.ID = 0
  1153. return order, nil
  1154. }
  1155. func (from *InstanceData) Export() string {
  1156. export := xlsx.NewFile()
  1157. style := xlsx.NewStyle()
  1158. style.Border = *xlsx.NewBorder("thin", "thin", "thin", "thin")
  1159. for _, table := range from.Subs {
  1160. for _, itable := range table.Instances {
  1161. sh, err := export.AddSheet(itable.GetNameResultString())
  1162. if err != nil {
  1163. return ""
  1164. }
  1165. headerRow := sh.AddRow()
  1166. headerRow.SetHeight(20)
  1167. headerRow.AddCell()
  1168. headerNumber := 0
  1169. for _, header := range itable.Headers {
  1170. for _, iheader := range header.Instances {
  1171. headerCell := headerRow.AddCell()
  1172. headerCell.SetStyle(style)
  1173. headerCell.Value = iheader.GetNameResultString()
  1174. headerNumber++
  1175. }
  1176. }
  1177. sh.SetColWidth(1, headerNumber+1, 30)
  1178. for _, module := range itable.Subs {
  1179. for _, imodule := range module.Instances {
  1180. moduleRow := sh.AddRow()
  1181. moduleRow.SetHeight(20)
  1182. moduleCell := moduleRow.AddCell()
  1183. moduleCell.SetStyle(style)
  1184. moduleCell.Value = imodule.GetNameResultString()
  1185. moduleCell.HMerge = headerNumber
  1186. for _, group := range imodule.Subs {
  1187. for _, igroup := range group.Instances {
  1188. var groupCell1 *xlsx.Cell
  1189. rowNumber := 0
  1190. for _, row := range igroup.Subs {
  1191. for _, irow := range row.Instances {
  1192. rowRow := sh.AddRow()
  1193. rowRow.SetHeight(20)
  1194. groupCell := rowRow.AddCell()
  1195. groupCell.SetStyle(style)
  1196. groupCell.Value = igroup.GetNameResultString()
  1197. if groupCell1 == nil {
  1198. groupCell1 = groupCell
  1199. }
  1200. if irow.HeaderResult != nil {
  1201. for _, header := range itable.Headers {
  1202. if headerResult, ok := irow.HeaderResult[header.ID]; ok {
  1203. for headerIndex, _ := range header.Instances {
  1204. rowCell := rowRow.AddCell()
  1205. rowCell.SetStyle(style)
  1206. if headerIndex < len(headerResult) && headerResult[headerIndex] != nil {
  1207. rowCell.Value = headerResult[headerIndex].ToString()
  1208. }
  1209. }
  1210. }
  1211. }
  1212. }
  1213. rowNumber++
  1214. }
  1215. }
  1216. if groupCell1 != nil {
  1217. groupCell1.VMerge = rowNumber - 1
  1218. }
  1219. }
  1220. }
  1221. }
  1222. }
  1223. }
  1224. }
  1225. exportFileName := utils.ToStr(time.Now().UnixNano()) + ".xlsx"
  1226. if err := export.Save(config.Cfg.App.ExportPath + exportFileName); err != nil {
  1227. return ""
  1228. }
  1229. return exportFileName
  1230. }
  1231. func (order *Order) Export2(filename string, c *gin.Context) string {
  1232. template, err := xlsx.OpenFile(filename)
  1233. if err != nil {
  1234. logger.Sugar.Infof("err: %v", err)
  1235. return ""
  1236. }
  1237. export := xlsx.NewFile()
  1238. for sheetName, sheet := range template.Sheet {
  1239. sh, err := export.AddSheet(sheetName)
  1240. if err != nil {
  1241. logger.Sugar.Infof("err: %v", err)
  1242. return ""
  1243. }
  1244. sh.SheetFormat = sheet.SheetFormat
  1245. sheet.Cols.ForEach(func(idx int, col *xlsx.Col) {
  1246. sh.SetColParameters(col)
  1247. //sh.SetColWidth(col.Min, col.Max, *col.Width)
  1248. })
  1249. err = sheet.ForEachRow(func(r *xlsx.Row) error {
  1250. cells := make([]*xlsx.Cell, 0)
  1251. err := r.ForEachCell(func(c *xlsx.Cell) error {
  1252. cells = append(cells, c)
  1253. return nil
  1254. })
  1255. if err != nil {
  1256. return err
  1257. }
  1258. context := NewContext(order, c)
  1259. for _, rows := range readCells(cells, context) {
  1260. row := sh.AddRow()
  1261. height := r.GetHeight()
  1262. if height == 0 {
  1263. height = sheet.SheetFormat.DefaultRowHeight
  1264. }
  1265. row.SetHeight(height)
  1266. row.SetOutlineLevel(r.GetOutlineLevel())
  1267. for i, cell := range rows {
  1268. c := row.AddCell()
  1269. binary, _ := cells[i].MarshalBinary()
  1270. c.UnmarshalBinary(binary)
  1271. c.SetStyle(cells[i].GetStyle())
  1272. c.VMerge = cell.VMerge
  1273. c.Value = cell.value
  1274. }
  1275. }
  1276. return nil
  1277. })
  1278. if err != nil {
  1279. logger.Sugar.Infof("err: %v", err)
  1280. return ""
  1281. }
  1282. }
  1283. exportFileName := utils.ToStr(time.Now().UnixNano()) + ".xlsx"
  1284. if err := export.Save(config.Cfg.App.ExportPath + exportFileName); err != nil {
  1285. logger.Sugar.Infof("err: %v", err)
  1286. return ""
  1287. }
  1288. return exportFileName
  1289. }
  1290. type exportContext struct {
  1291. value string
  1292. VMerge int
  1293. instance *InstanceData
  1294. }
  1295. func runValue(begin string, formula string, context *Context) []exportContext {
  1296. prop, err := context.ParseForm(formula)
  1297. if err != nil || prop.Result == nil {
  1298. return []exportContext{{begin, 0, context.Current}}
  1299. }
  1300. results := make([]exportContext, 0)
  1301. argParse([]interface{}{prop.Result}, func(result interface{}) {
  1302. if instance, ok := result.(*InstanceData); ok {
  1303. results = append(results, exportContext{begin, 0, instance})
  1304. } else {
  1305. results = append(results, exportContext{begin + db.ToString(result), 0, context.Current})
  1306. }
  1307. })
  1308. return results
  1309. }
  1310. func runContext(contexts []exportContext, value string, context *Context) []exportContext {
  1311. results := make([]exportContext, 0)
  1312. for _, v := range contexts {
  1313. last := context.Current
  1314. context.Current = v.instance
  1315. for _, c := range readValue(value, context) {
  1316. c.value = v.value + c.value
  1317. results = append(results, c)
  1318. }
  1319. context.Current = last
  1320. }
  1321. return results
  1322. }
  1323. func readValue(value string, context *Context) []exportContext {
  1324. result := ""
  1325. brackets := false
  1326. begin := ""
  1327. for i := 0; i < len([]rune(value)); i++ {
  1328. switch []rune(value)[i] {
  1329. case '{':
  1330. begin = result
  1331. result = ""
  1332. brackets = true
  1333. case '}':
  1334. if brackets {
  1335. formula := result
  1336. result = ""
  1337. return runContext(runValue(begin, formula, context), string([]rune(value)[i+1:]), context)
  1338. } else {
  1339. result += string([]rune(value)[i])
  1340. }
  1341. default:
  1342. result += string([]rune(value)[i])
  1343. }
  1344. }
  1345. if brackets {
  1346. formula := result
  1347. result = ""
  1348. return runValue(begin, formula, context)
  1349. }
  1350. return []exportContext{{result, 0, context.Current}}
  1351. }
  1352. func readCells(cells []*xlsx.Cell, context *Context) [][]exportContext {
  1353. rows := make([][]exportContext, 0)
  1354. if len(cells) == 0 {
  1355. rows = append(rows, []exportContext{})
  1356. return rows
  1357. }
  1358. for _, c := range readValue(cells[0].String(), context) {
  1359. last := context.Current
  1360. context.Current = c.instance
  1361. rights := readCells(cells[1:], context)
  1362. for n, s := range rights {
  1363. cc := c
  1364. if n == 0 {
  1365. cc.VMerge = len(rights) - 1
  1366. }
  1367. rows = append(rows, append([]exportContext{cc}, s...))
  1368. }
  1369. context.Current = last
  1370. }
  1371. return rows
  1372. }
  1373. type ObjectData struct {
  1374. ID int64 `json:"i" label:"ID"`
  1375. Type int `json:"-" label:"类型"`
  1376. NameResult *PropForm `json:"n" label:"名称结果"`
  1377. Parent *InstanceData `json:"-" label:"父实例"`
  1378. Instances []InstanceData `json:"s" label:"实例"`
  1379. }
  1380. func (object *ObjectData) Init(parent *InstanceData, typ int, order *Order) {
  1381. object.Parent = parent
  1382. object.Type = typ
  1383. if object.NameResult != nil {
  1384. object.NameResult.Init(parent, order)
  1385. }
  1386. if object.Instances == nil {
  1387. object.Instances = make([]InstanceData, 0)
  1388. }
  1389. for i, _ := range object.Instances {
  1390. object.Instances[i].Init(parent, object, typ, i, order)
  1391. }
  1392. }
  1393. func (object *ObjectData) ToMap(save bool) map[string]interface{} {
  1394. mp := make(map[string]interface{})
  1395. if save || object.Type == Header {
  1396. mp["i"] = object.ID
  1397. }
  1398. if save && object.NameResult != nil {
  1399. mp["n"] = object.NameResult.ToMap(save)
  1400. }
  1401. instances := make([]interface{}, 0)
  1402. for _, instance := range object.Instances {
  1403. if save || instance.GetShowResultBool() {
  1404. instances = append(instances, instance.ToMap(save))
  1405. }
  1406. }
  1407. mp["s"] = instances
  1408. return mp
  1409. }
  1410. type InstanceData struct {
  1411. ID int64 `json:"i" label:"ID"`
  1412. Object *ObjectData `json:"-" label:"对象"`
  1413. Parent *InstanceData `json:"-" label:"对象"`
  1414. Index int `json:"-" label:"序号"`
  1415. Type int `json:"-" label:"类型"`
  1416. Name string `json:"n" label:"名称"`
  1417. NameResult *PropForm `json:"nr" label:"名称结果"`
  1418. Show string `json:"s" label:"显示"`
  1419. ShowResult *PropForm `json:"sr" label:"显示结果"`
  1420. ItemId int64 `json:"t"`
  1421. Key string `json:"k"`
  1422. PropResult map[string]*PropForm `json:"p" label:"属性结果"`
  1423. Item *InstanceData `json:"m" label:"项目"`
  1424. HeaderResult map[int64][]*PropForm `json:"hr"`
  1425. Result *PropForm `json:"r" label:"计算结果"`
  1426. Subs []ObjectData `json:"o" label:"行"`
  1427. Headers []ObjectData `json:"h" label:"表头"`
  1428. }
  1429. func (instance *InstanceData) Init(parent *InstanceData, object *ObjectData, typ int, index int, order *Order) {
  1430. instance.Object = object
  1431. instance.Parent = parent
  1432. instance.Index = index
  1433. instance.Type = typ
  1434. if instance.NameResult != nil {
  1435. instance.NameResult.Init(instance, order)
  1436. }
  1437. if instance.ShowResult != nil {
  1438. instance.ShowResult.Init(instance, order)
  1439. }
  1440. if instance.PropResult == nil {
  1441. instance.PropResult = make(map[string]*PropForm)
  1442. }
  1443. for name, _ := range instance.PropResult {
  1444. instance.PropResult[name].Init(instance, order)
  1445. }
  1446. if instance.Item != nil {
  1447. instance.Item.Init(instance, object, Item, instance.Index, order)
  1448. }
  1449. if instance.Result != nil {
  1450. instance.Result.Init(instance, order)
  1451. }
  1452. if instance.Type == Row {
  1453. if instance.HeaderResult == nil {
  1454. instance.HeaderResult = make(map[int64][]*PropForm)
  1455. }
  1456. } else {
  1457. if instance.Type != Header {
  1458. if instance.Subs == nil {
  1459. instance.Subs = make([]ObjectData, 0)
  1460. }
  1461. subType := instance.Type + 1
  1462. if instance.Type == Table {
  1463. subType = Module
  1464. }
  1465. for i, _ := range instance.Subs {
  1466. instance.Subs[i].Init(instance, subType, order)
  1467. }
  1468. }
  1469. }
  1470. if instance.Type == Table {
  1471. if instance.Headers == nil {
  1472. instance.Headers = make([]ObjectData, 0)
  1473. }
  1474. for i, _ := range instance.Headers {
  1475. instance.Headers[i].Init(instance, Header, order)
  1476. }
  1477. }
  1478. }
  1479. func (instance *InstanceData) GetID() int64 {
  1480. if instance.ID != 0 {
  1481. return instance.ID
  1482. }
  1483. if instance.Object == nil {
  1484. return 0
  1485. }
  1486. return instance.Object.ID
  1487. }
  1488. func (instance InstanceData) GetNameResultString() string {
  1489. if instance.NameResult == nil {
  1490. return ""
  1491. }
  1492. return instance.NameResult.ToString()
  1493. }
  1494. func (instance InstanceData) GetShowResultBool() bool {
  1495. if instance.Show == "" {
  1496. return true
  1497. }
  1498. if instance.ShowResult == nil {
  1499. return false
  1500. }
  1501. return db.ToBool(instance.ShowResult.Result)
  1502. }
  1503. func (instance *InstanceData) SetNameResult(prop *PropForm) {
  1504. instance.NameResult = prop
  1505. }
  1506. func (instance *InstanceData) SetShowResult(prop *PropForm) {
  1507. instance.ShowResult = prop
  1508. }
  1509. func (instance InstanceData) GetPropResult(name string) *PropForm {
  1510. if prop, ok := instance.PropResult[name]; ok {
  1511. return prop
  1512. }
  1513. return nil
  1514. }
  1515. func (instance *InstanceData) SetPropResult(name string, prop *PropForm) {
  1516. instance.PropResult[name] = prop
  1517. }
  1518. func (instance *InstanceData) GetSub() []*InstanceData {
  1519. subs := make([]*InstanceData, 0)
  1520. for _, sub := range instance.Subs {
  1521. for i, _ := range sub.Instances {
  1522. subs = append(subs, &sub.Instances[i])
  1523. }
  1524. }
  1525. return subs
  1526. }
  1527. func (instance InstanceData) ToMap(save bool) map[string]interface{} {
  1528. mp := make(map[string]interface{})
  1529. if save {
  1530. if instance.ID != 0 {
  1531. mp["i"] = instance.ID
  1532. } else {
  1533. mp["n"] = instance.Name
  1534. mp["s"] = instance.Show
  1535. mp["t"] = instance.ItemId
  1536. mp["k"] = instance.Key
  1537. }
  1538. } else {
  1539. if instance.HeaderResult != nil {
  1540. headerResult := make(map[int64][]interface{})
  1541. for id, results := range instance.HeaderResult {
  1542. headerResult[id] = make([]interface{}, 0)
  1543. for _, result := range results {
  1544. headerResult[id] = append(headerResult[id], result.ToMap(save))
  1545. }
  1546. }
  1547. mp["hr"] = headerResult
  1548. }
  1549. }
  1550. if !save || instance.NameResult != nil {
  1551. mp["nr"] = instance.NameResult.ToMap(save)
  1552. }
  1553. if !save || instance.ShowResult != nil {
  1554. mp["sr"] = instance.ShowResult.ToMap(save)
  1555. }
  1556. if instance.Item != nil {
  1557. mp["m"] = instance.Item.ToMap(save)
  1558. }
  1559. if instance.Result != nil {
  1560. mp["r"] = instance.Result.ToMap(save)
  1561. }
  1562. if instance.Subs != nil {
  1563. subs := make([]interface{}, 0)
  1564. for _, sub := range instance.Subs {
  1565. subs = append(subs, sub.ToMap(save))
  1566. }
  1567. mp["o"] = subs
  1568. }
  1569. if instance.Headers != nil {
  1570. headers := make([]interface{}, 0)
  1571. for _, header := range instance.Headers {
  1572. headers = append(headers, header.ToMap(save))
  1573. }
  1574. mp["h"] = headers
  1575. }
  1576. propResult := make(map[string]interface{})
  1577. for name, prop := range instance.PropResult {
  1578. if save || !save && len(prop.UseKeys) != 0 && !prop.Header && prop.Control != nil {
  1579. propResult[name] = prop.ToMap(save)
  1580. }
  1581. }
  1582. mp["p"] = propResult
  1583. return mp
  1584. }