1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813 |
- package budget
- import (
- "bytes"
- "compress/gzip"
- "encoding/json"
- "errors"
- "io/ioutil"
- "reflect"
- "strings"
- "time"
- "zhiyuan/models/budget"
- "zhiyuan/pkg/config"
- "zhiyuan/pkg/db"
- "zhiyuan/pkg/logger"
- "zhiyuan/pkg/utils"
- "github.com/gin-gonic/gin"
- "github.com/tealeg/xlsx/v3"
- )
- const (
- Modify int = iota
- Immediate
- Save
- )
- const (
- Base int = iota
- Quote
- Table
- Header
- Module
- Group
- Row
- Item
- )
- /*type Object interface {
- GetID() int64
- Init(Instance, *Order)
- GetParentInstance() Instance
- Count() int
- Copy(Instance, bool, bool) Object
- GetInstances() []Instance
- }
- type Instance interface {
- GetID() int64
- Init(Object, int, *Order)
- GetObject() Object
- GetIndex() int
- GetParent() Instance
- GetSub() []Instance
- GetName() string
- GetShow() string
- GetNameResult() *PropForm
- GetShowResult() *PropForm
- SetNameResult(prop *PropForm)
- SetShowResult(prop *PropForm)
- GetShowResultBool() bool
- GetPropResult(name string) *PropForm
- SetPropResult(name string, prop *PropForm)
- Copy(Object, bool, bool) Instance
- Type() int
- }*/
- type PropForm struct {
- Key uint64 `json:"k"`
- Cycle int `json:"c" label:"生命周期"`
- Control *Control `json:"l"`
- //Instance Instance `json:"-"`
- Instance *InstanceData `json:"-"`
- Header bool `json:"-"`
- Result interface{} `json:"r" label:"结果"`
- UseKeys []uint64 `json:"s"`
- }
- // func (prop *PropForm) Init(instance Instance, order *Order) {
- func (prop *PropForm) Init(instance *InstanceData, order *Order) {
- prop.Instance = instance
- order.Prop[prop.Key] = prop
- if prop.Key > order.PropKeyMax {
- order.PropKeyMax = prop.Key
- }
- for _, k := range prop.UseKeys {
- order.SetPropUse(k, prop.Key)
- }
- }
- func (prop *PropForm) AddUseKey(key uint64) {
- for _, k := range prop.UseKeys {
- if k == key {
- return
- }
- }
- prop.UseKeys = append(prop.UseKeys, key)
- }
- func (prop *PropForm) ToString() string {
- return db.ToString(prop.Result)
- }
- func (prop *PropForm) ToMap(save bool) map[string]interface{} {
- if prop == nil {
- return nil
- }
- mp := make(map[string]interface{})
- if !save || prop.Cycle != Immediate {
- argParse([]interface{}{prop.Result}, func(result interface{}) {
- if _, ok := result.(*InstanceData); ok {
- logger.Sugar.Infof("prop.ToMap(instance): %v %v %v", prop.Instance.Type, prop.Instance.GetID(), prop.Instance.Name)
- }
- })
- mp["r"] = prop.Result
- }
- if prop.Control != nil {
- mp["k"] = prop.Key
- mp["l"] = prop.Control
- }
- if save {
- mp["s"] = prop.UseKeys
- mp["k"] = prop.Key
- mp["c"] = prop.Cycle
- }
- return mp
- }
- /*type BaseInstance struct {
- Object Object `json:"-" label:"对象"`
- Index int `json:"-" label:"对象"`
- Name string `json:"n" label:"名称"`
- NameResult *PropForm `json:"nr" label:"名称结果"`
- Show string `json:"s" label:"显示"`
- ShowResult *PropForm `json:"sr" label:"显示结果"`
- ItemId int64 `json:"t"`
- Key string `json:"k"`
- PropResult map[string]*PropForm `json:"p" label:"属性结果"`
- }
- func (instance *BaseInstance) GetID() int64 {
- object := instance.GetObject()
- if object == nil {
- return 0
- }
- return object.GetID()
- }
- func (instance *BaseInstance) Init(object Object, index int, order *Order) {
- instance.Object = object
- instance.Index = index
- if instance.PropResult == nil {
- instance.PropResult = make(map[string]*PropForm)
- }
- if instance.NameResult != nil {
- instance.NameResult.Init(instance, order)
- }
- if instance.ShowResult != nil {
- instance.ShowResult.Init(instance, order)
- }
- for name, _ := range instance.PropResult {
- instance.PropResult[name].Init(instance, order)
- }
- }
- func (instance *BaseInstance) GetObject() Object {
- return instance.Object
- }
- func (instance *BaseInstance) GetIndex() int {
- return instance.Index
- }
- func (instance *BaseInstance) GetParent() Instance {
- object := instance.GetObject()
- if object == nil {
- return nil
- }
- return object.GetParentInstance()
- }
- func (instance *BaseInstance) GetName() string {
- return instance.Name
- }
- func (instance *BaseInstance) GetShow() string {
- return instance.Show
- }
- func (instance *BaseInstance) GetNameResult() *PropForm {
- return instance.NameResult
- }
- func (instance *BaseInstance) GetNameResultString() string {
- nameResult := instance.GetNameResult()
- if nameResult == nil {
- return ""
- }
- return nameResult.ToString()
- }
- func (instance *BaseInstance) GetShowResult() *PropForm {
- return instance.ShowResult
- }
- func (instance *BaseInstance) GetShowResultBool() bool {
- if instance.GetShow() == "" {
- return true
- }
- showResult := instance.GetShowResult()
- if showResult == nil {
- return false
- }
- return db.ToBool(showResult.Result)
- }
- func (instance *BaseInstance) SetNameResult(prop *PropForm) {
- instance.NameResult = prop
- }
- func (instance *BaseInstance) SetShowResult(prop *PropForm) {
- instance.ShowResult = prop
- }
- func (instance *BaseInstance) GetPropResult(name string) *PropForm {
- if prop, ok := instance.PropResult[name]; ok {
- return prop
- }
- return nil
- }
- func (instance *BaseInstance) SetPropResult(name string, prop *PropForm) {
- instance.PropResult[name] = prop
- }
- func (instance *BaseInstance) Copy(object Object, getall bool, save bool) Instance {
- s := new(BaseInstance)
- s.Object = object
- s.Index = instance.Index
- s.Name = instance.Name
- if !save || instance.NameResult != nil && instance.NameResult.Cycle != Immediate {
- s.NameResult = instance.NameResult
- }
- s.Show = instance.Show
- if !save || instance.ShowResult != nil && instance.ShowResult.Cycle != Immediate {
- s.ShowResult = instance.ShowResult
- }
- s.PropResult = make(map[string]*PropForm)
- for name, prop := range instance.PropResult {
- if getall || len(prop.UseKeys) != 0 && !prop.Header && prop.Control != nil {
- if !save || prop.Cycle != Immediate {
- s.PropResult[name] = prop
- s.PropResult[name].Instance = s
- }
- }
- }
- return s
- }
- func (instance *BaseInstance) ToMap(save bool) map[string]interface{} {
- mp := make(map[string]interface{})
- if save {
- mp["n"] = instance.Name
- mp["s"] = instance.Show
- mp["t"] = instance.ItemId
- mp["k"] = instance.Key
- }
- if !save || instance.NameResult != nil {
- mp["nr"] = instance.NameResult.ToMap(save)
- }
- if !save || instance.ShowResult != nil {
- mp["sr"] = instance.ShowResult.ToMap(save)
- }
- propResult := make(map[string]interface{})
- for name, prop := range instance.PropResult {
- if save || !save && len(prop.UseKeys) != 0 && !prop.Header && prop.Control != nil {
- propResult[name] = prop.ToMap(save)
- }
- }
- mp["p"] = propResult
- return mp
- }
- func (instance *BaseInstance) GetSub() []Instance {
- return []Instance{}
- }
- func (instance *BaseInstance) Type() int {
- return Base
- }
- type BaseObject struct {
- ID int64 `json:"i" label:"ID" binding:"required"`
- NameResult *PropForm `json:"nr" label:"名称结果"`
- Parent Instance `json:"-" label:"父实例"`
- }
- func (object *BaseObject) GetID() int64 {
- return object.ID
- }
- func (object *BaseObject) Init(parent Instance, order *Order) {
- object.Parent = parent
- if object.NameResult != nil {
- object.NameResult.Init(parent, order)
- }
- }
- func (object *BaseObject) GetParentInstance() Instance {
- return object.Parent
- }
- func (object *BaseObject) Copy(parent Instance, getall bool, save bool) Object {
- s := new(BaseObject)
- s.ID = object.ID
- if !save || object.NameResult != nil && object.NameResult.Cycle != Immediate {
- s.NameResult = object.NameResult
- }
- s.Parent = parent
- return s
- }
- func (object *BaseObject) ToMap(save bool) map[string]interface{} {
- mp := make(map[string]interface{})
- if save {
- mp["i"] = object.ID
- }
- if !save || object.NameResult != nil {
- mp["nr"] = object.NameResult.ToMap(save)
- }
- return mp
- }
- func (object *BaseObject) Count() int {
- return 0
- }
- func (object *BaseObject) GetInstances() []Instance {
- return []Instance{}
- }
- type ItemInstance struct {
- ID int64 `json:"i" label:"ID" binding:"required"`
- Row *RowInstance `json:"-" label:"行"`
- PropResult map[string]*PropForm `json:"p" label:"属性结果"`
- }
- func (instance *ItemInstance) GetID() int64 {
- return instance.ID
- }
- func (instance *ItemInstance) Init(object Object, index int, order *Order) {
- if instance.PropResult == nil {
- instance.PropResult = make(map[string]*PropForm)
- }
- for name, _ := range instance.PropResult {
- instance.PropResult[name].Init(instance, order)
- }
- }
- func (instance *ItemInstance) GetIndex() int {
- return instance.Row.Index
- }
- func (instance *ItemInstance) GetObject() Object {
- return instance.Row.Object
- }
- func (instance *ItemInstance) GetParent() Instance {
- return instance.Row
- }
- func (instance *ItemInstance) GetName() string {
- return ""
- }
- func (instance *ItemInstance) GetShow() string {
- return ""
- }
- func (instance *ItemInstance) GetNameResult() *PropForm {
- return nil
- }
- func (instance *ItemInstance) GetShowResult() *PropForm {
- return nil
- }
- func (instance *ItemInstance) GetShowResultBool() bool {
- return false
- }
- func (instance *ItemInstance) SetNameResult(prop *PropForm) {
- }
- func (instance *ItemInstance) SetShowResult(prop *PropForm) {
- }
- func (instance *ItemInstance) GetPropResult(name string) *PropForm {
- if prop, ok := instance.PropResult[name]; ok {
- return prop
- }
- return nil
- }
- func (instance *ItemInstance) SetPropResult(name string, prop *PropForm) {
- instance.PropResult[name] = prop
- }
- func (instance *ItemInstance) Copy(object Object, getall bool, save bool) Instance {
- s := new(ItemInstance)
- s.ID = instance.ID
- s.PropResult = make(map[string]*PropForm)
- for name, prop := range instance.PropResult {
- if getall || len(prop.UseKeys) != 0 && !prop.Header && prop.Control != nil {
- if !save || prop.Cycle != Immediate {
- s.PropResult[name] = prop
- s.PropResult[name].Instance = s
- }
- }
- }
- return s
- }
- func (instance *ItemInstance) ToMap(save bool) map[string]interface{} {
- mp := make(map[string]interface{})
- if save {
- mp["i"] = instance.ID
- }
- propResult := make(map[string]interface{})
- for name, prop := range instance.PropResult {
- if save || !save && len(prop.UseKeys) != 0 && !prop.Header && prop.Control != nil {
- propResult[name] = prop.ToMap(save)
- }
- }
- mp["p"] = propResult
- return mp
- }
- func (instance *ItemInstance) GetSub() []Instance {
- return []Instance{}
- }
- func (instance *ItemInstance) Type() int {
- return Item
- }
- type RowInstance struct {
- BaseInstance
- Item *ItemInstance `json:"m" label:"项目"`
- HeaderResult map[int64][]*PropForm `json:"hr"`
- }
- func (instance *RowInstance) Init(object Object, index int, order *Order) {
- instance.BaseInstance.Init(object, index, order)
- if instance.Item != nil {
- instance.Item.Row = instance
- instance.Item.Init(object, 0, order)
- }
- if instance.HeaderResult == nil {
- instance.HeaderResult = make(map[int64][]*PropForm)
- }
- for name, _ := range instance.PropResult {
- instance.PropResult[name].Init(instance, order)
- }
- }
- func (instance *RowInstance) Copy(object Object, getall bool, save bool) Instance {
- s := new(RowInstance)
- s.BaseInstance = *instance.BaseInstance.Copy(object, getall, save).(*BaseInstance)
- if instance.Item != nil {
- s.Item = instance.Item.Copy(object, getall, save).(*ItemInstance)
- }
- s.HeaderResult = instance.HeaderResult
- return s
- }
- func (instance *RowInstance) ToMap(save bool) map[string]interface{} {
- mp := instance.BaseInstance.ToMap(save)
- if instance.Item != nil {
- mp["m"] = instance.Item.ToMap(save)
- }
- if !save {
- headerResult := make(map[int64][]interface{})
- for id, results := range instance.HeaderResult {
- headerResult[id] = make([]interface{}, 0)
- for _, result := range results {
- headerResult[id] = append(headerResult[id], result.ToMap(save))
- }
- }
- mp["hr"] = headerResult
- }
- return mp
- }
- func (instance *RowInstance) GetSub() []Instance {
- if instance.Item != nil {
- return []Instance{instance.Item}
- }
- return []Instance{}
- }
- func (instance *RowInstance) Type() int {
- return Row
- }
- type RowObject struct {
- BaseObject
- Instances []RowInstance `json:"is" label:"实例"`
- }
- func (object *RowObject) Init(parent Instance, order *Order) {
- object.BaseObject.Init(parent, order)
- if object.Instances == nil {
- object.Instances = make([]RowInstance, 0)
- }
- for i, _ := range object.Instances {
- object.Instances[i].Init(object, i, order)
- }
- }
- func (object *RowObject) Copy(parent Instance, getall bool, save bool) Object {
- s := new(RowObject)
- s.BaseObject = *object.BaseObject.Copy(parent, getall, save).(*BaseObject)
- s.Instances = make([]RowInstance, 0)
- for _, instance := range object.Instances {
- if getall || instance.GetShowResultBool() {
- s.Instances = append(s.Instances, *instance.Copy(s, getall, save).(*RowInstance))
- }
- }
- return s
- }
- func (object *RowObject) ToMap(save bool) map[string]interface{} {
- mp := object.BaseObject.ToMap(save)
- instances := make([]interface{}, 0)
- for _, instance := range object.Instances {
- if save || instance.GetShowResultBool() {
- instances = append(instances, instance.ToMap(save))
- }
- }
- mp["is"] = instances
- return mp
- }
- func (object *RowObject) Count() int {
- return len(object.Instances)
- }
- func (object *RowObject) GetInstances() []Instance {
- instances := make([]Instance, 0)
- for i, _ := range object.Instances {
- instances = append(instances, &object.Instances[i])
- }
- return instances
- }
- type GroupInstance struct {
- BaseInstance
- Rows []RowObject `json:"o" label:"行"`
- }
- func (instance *GroupInstance) Init(object Object, index int, order *Order) {
- instance.BaseInstance.Init(object, index, order)
- if instance.Rows == nil {
- instance.Rows = make([]RowObject, 0)
- }
- for i, _ := range instance.Rows {
- instance.Rows[i].Init(instance, order)
- }
- for name, _ := range instance.PropResult {
- instance.PropResult[name].Init(instance, order)
- }
- }
- func (instance *GroupInstance) Copy(object Object, getall bool, save bool) Instance {
- s := new(GroupInstance)
- s.BaseInstance = *instance.BaseInstance.Copy(object, getall, save).(*BaseInstance)
- s.Rows = make([]RowObject, 0)
- for _, row := range instance.Rows {
- s.Rows = append(s.Rows, *row.Copy(s, getall, save).(*RowObject))
- }
- return s
- }
- func (instance *GroupInstance) ToMap(save bool) map[string]interface{} {
- mp := instance.BaseInstance.ToMap(save)
- rows := make([]interface{}, 0)
- for _, row := range instance.Rows {
- rows = append(rows, row.ToMap(save))
- }
- mp["o"] = rows
- return mp
- }
- func (instance *GroupInstance) GetSub() []Instance {
- subs := make([]Instance, 0)
- for _, row := range instance.Rows {
- for i, _ := range row.Instances {
- subs = append(subs, &row.Instances[i])
- }
- }
- return subs
- }
- func (instance *GroupInstance) Type() int {
- return Group
- }
- type GroupObject struct {
- BaseObject
- Instances []GroupInstance `json:"is" label:"实例"`
- }
- func (object *GroupObject) Init(parent Instance, order *Order) {
- object.BaseObject.Init(parent, order)
- if object.Instances == nil {
- object.Instances = make([]GroupInstance, 0)
- }
- for i, _ := range object.Instances {
- object.Instances[i].Init(object, i, order)
- }
- }
- func (object *GroupObject) Copy(parent Instance, getall bool, save bool) Object {
- s := new(GroupObject)
- s.BaseObject = *object.BaseObject.Copy(parent, getall, save).(*BaseObject)
- s.Instances = make([]GroupInstance, 0)
- for _, instance := range object.Instances {
- if getall || instance.GetShowResultBool() {
- s.Instances = append(s.Instances, *instance.Copy(s, getall, save).(*GroupInstance))
- }
- }
- return s
- }
- func (object *GroupObject) ToMap(save bool) map[string]interface{} {
- mp := object.BaseObject.ToMap(save)
- instances := make([]interface{}, 0)
- for _, instance := range object.Instances {
- if save || instance.GetShowResultBool() {
- instances = append(instances, instance.ToMap(save))
- }
- }
- mp["is"] = instances
- return mp
- }
- func (object *GroupObject) Count() int {
- return len(object.Instances)
- }
- func (object *GroupObject) GetInstances() []Instance {
- instances := make([]Instance, 0)
- for i, _ := range object.Instances {
- instances = append(instances, &object.Instances[i])
- }
- return instances
- }
- type ModuleInstance struct {
- BaseInstance
- Groups []GroupObject `json:"o" label:"组"`
- }
- func (instance *ModuleInstance) Init(object Object, index int, order *Order) {
- instance.BaseInstance.Init(object, index, order)
- if instance.Groups == nil {
- instance.Groups = make([]GroupObject, 0)
- }
- for i, _ := range instance.Groups {
- instance.Groups[i].Init(instance, order)
- }
- for name, _ := range instance.PropResult {
- instance.PropResult[name].Init(instance, order)
- }
- }
- func (instance *ModuleInstance) Copy(object Object, getall bool, save bool) Instance {
- s := new(ModuleInstance)
- s.BaseInstance = *instance.BaseInstance.Copy(object, getall, save).(*BaseInstance)
- s.Groups = make([]GroupObject, 0)
- for _, group := range instance.Groups {
- s.Groups = append(s.Groups, *group.Copy(s, getall, save).(*GroupObject))
- }
- return s
- }
- func (instance *ModuleInstance) ToMap(save bool) map[string]interface{} {
- mp := instance.BaseInstance.ToMap(save)
- groups := make([]interface{}, 0)
- for _, group := range instance.Groups {
- groups = append(groups, group.ToMap(save))
- }
- mp["o"] = groups
- return mp
- }
- func (instance *ModuleInstance) GetSub() []Instance {
- subs := make([]Instance, 0)
- for _, group := range instance.Groups {
- for i, _ := range group.Instances {
- subs = append(subs, &group.Instances[i])
- }
- }
- return subs
- }
- func (instance *ModuleInstance) Type() int {
- return Module
- }
- type ModuleObject struct {
- BaseObject
- Instances []ModuleInstance `json:"is" label:"实例"`
- }
- func (object *ModuleObject) Init(parent Instance, order *Order) {
- object.BaseObject.Init(parent, order)
- if object.Instances == nil {
- object.Instances = make([]ModuleInstance, 0)
- }
- for i, _ := range object.Instances {
- object.Instances[i].Init(object, i, order)
- }
- }
- func (object *ModuleObject) Copy(parent Instance, getall bool, save bool) Object {
- s := new(ModuleObject)
- s.BaseObject = *object.BaseObject.Copy(parent, getall, save).(*BaseObject)
- s.Instances = make([]ModuleInstance, 0)
- for _, instance := range object.Instances {
- if getall || instance.GetShowResultBool() {
- s.Instances = append(s.Instances, *instance.Copy(s, getall, save).(*ModuleInstance))
- }
- }
- return s
- }
- func (object *ModuleObject) ToMap(save bool) map[string]interface{} {
- mp := object.BaseObject.ToMap(save)
- instances := make([]interface{}, 0)
- for _, instance := range object.Instances {
- if save || instance.GetShowResultBool() {
- instances = append(instances, instance.ToMap(save))
- }
- }
- mp["is"] = instances
- return mp
- }
- func (object *ModuleObject) Count() int {
- return len(object.Instances)
- }
- func (object *ModuleObject) GetInstances() []Instance {
- instances := make([]Instance, 0)
- for i, _ := range object.Instances {
- instances = append(instances, &object.Instances[i])
- }
- return instances
- }
- type HeaderInstance struct {
- BaseInstance
- Result *PropForm `json:"r" label:"计算结果"`
- }
- func (instance *HeaderInstance) Init(object Object, index int, order *Order) {
- instance.BaseInstance.Init(object, index, order)
- if instance.Result != nil {
- instance.Result.Init(instance, order)
- }
- for name, _ := range instance.PropResult {
- instance.PropResult[name].Init(instance, order)
- }
- }
- func (instance *HeaderInstance) Copy(object Object, getall bool, save bool) Instance {
- s := new(HeaderInstance)
- s.BaseInstance = *instance.BaseInstance.Copy(object, getall, save).(*BaseInstance)
- s.Result = instance.Result
- return s
- }
- func (instance *HeaderInstance) ToMap(save bool) map[string]interface{} {
- mp := instance.BaseInstance.ToMap(save)
- mp["r"] = instance.Result.ToMap(save)
- return mp
- }
- func (instance *HeaderInstance) GetSub() []Instance {
- return []Instance{}
- }
- func (instance *HeaderInstance) Type() int {
- return Header
- }
- type HeaderObject struct {
- BaseObject
- Instances []HeaderInstance `json:"is" label:"实例"`
- }
- func (object *HeaderObject) Init(parent Instance, order *Order) {
- object.BaseObject.Init(parent, order)
- if object.Instances == nil {
- object.Instances = make([]HeaderInstance, 0)
- }
- for i, _ := range object.Instances {
- object.Instances[i].Init(object, i, order)
- }
- }
- func (object *HeaderObject) Copy(parent Instance, getall bool, save bool) Object {
- s := new(HeaderObject)
- s.BaseObject = *object.BaseObject.Copy(parent, getall, save).(*BaseObject)
- s.Instances = make([]HeaderInstance, 0)
- for _, instance := range object.Instances {
- if getall || instance.GetShowResultBool() {
- s.Instances = append(s.Instances, *instance.Copy(s, getall, save).(*HeaderInstance))
- }
- }
- return s
- }
- func (object *HeaderObject) ToMap(save bool) map[string]interface{} {
- mp := object.BaseObject.ToMap(save)
- mp["i"] = object.ID
- instances := make([]interface{}, 0)
- for _, instance := range object.Instances {
- if save || instance.GetShowResultBool() {
- instances = append(instances, instance.ToMap(save))
- }
- }
- mp["is"] = instances
- return mp
- }
- func (object *HeaderObject) Count() int {
- return len(object.Instances)
- }
- func (object *HeaderObject) GetInstances() []Instance {
- instances := make([]Instance, 0)
- for i, _ := range object.Instances {
- instances = append(instances, &object.Instances[i])
- }
- return instances
- }
- type TableInstance struct {
- BaseInstance
- Headers []HeaderObject `json:"h" label:"表头"`
- Modules []ModuleObject `json:"o" label:"模块"`
- }
- func (instance *TableInstance) Init(object Object, index int, order *Order) {
- instance.BaseInstance.Init(object, index, order)
- if instance.Headers == nil {
- instance.Headers = make([]HeaderObject, 0)
- }
- if instance.Modules == nil {
- instance.Modules = make([]ModuleObject, 0)
- }
- for i, _ := range instance.Headers {
- instance.Headers[i].Init(instance, order)
- }
- for i, _ := range instance.Modules {
- instance.Modules[i].Init(instance, order)
- }
- for name, _ := range instance.PropResult {
- instance.PropResult[name].Init(instance, order)
- }
- }
- func (instance *TableInstance) Copy(object Object, getall bool, save bool) Instance {
- s := new(TableInstance)
- s.BaseInstance = *instance.BaseInstance.Copy(object, getall, save).(*BaseInstance)
- s.Headers = make([]HeaderObject, 0)
- s.Modules = make([]ModuleObject, 0)
- for _, header := range instance.Headers {
- s.Headers = append(s.Headers, *header.Copy(s, getall, save).(*HeaderObject))
- }
- for _, module := range instance.Modules {
- s.Modules = append(s.Modules, *module.Copy(s, getall, save).(*ModuleObject))
- }
- return s
- }
- func (instance *TableInstance) ToMap(save bool) map[string]interface{} {
- mp := instance.BaseInstance.ToMap(save)
- modules := make([]interface{}, 0)
- for _, module := range instance.Modules {
- modules = append(modules, module.ToMap(save))
- }
- mp["o"] = modules
- headers := make([]interface{}, 0)
- for _, header := range instance.Headers {
- headers = append(headers, header.ToMap(save))
- }
- mp["h"] = headers
- return mp
- }
- func (instance *TableInstance) GetSub() []Instance {
- subs := make([]Instance, 0)
- for _, module := range instance.Modules {
- for i, _ := range module.Instances {
- subs = append(subs, &module.Instances[i])
- }
- }
- return subs
- }
- func (instance *TableInstance) Type() int {
- return Table
- }
- type TableObject struct {
- BaseObject
- Instances []TableInstance `json:"is" label:"实例"`
- }
- func (object *TableObject) Init(parent Instance, order *Order) {
- object.BaseObject.Init(parent, order)
- if object.Instances == nil {
- object.Instances = make([]TableInstance, 0)
- }
- for i, _ := range object.Instances {
- object.Instances[i].Init(object, i, order)
- }
- }
- func (object *TableObject) Copy(parent Instance, getall bool, save bool) Object {
- s := new(TableObject)
- s.BaseObject = *object.BaseObject.Copy(parent, getall, save).(*BaseObject)
- s.Instances = make([]TableInstance, 0)
- for _, instance := range object.Instances {
- if getall || instance.GetShowResultBool() {
- s.Instances = append(s.Instances, *instance.Copy(s, getall, save).(*TableInstance))
- }
- }
- return s
- }
- func (object *TableObject) ToMap(save bool) map[string]interface{} {
- mp := object.BaseObject.ToMap(save)
- instances := make([]interface{}, 0)
- for _, instance := range object.Instances {
- if save || instance.GetShowResultBool() {
- instances = append(instances, instance.ToMap(save))
- }
- }
- mp["is"] = instances
- return mp
- }
- func (object *TableObject) Count() int {
- return len(object.Instances)
- }
- func (object *TableObject) GetInstances() []Instance {
- instances := make([]Instance, 0)
- for i, _ := range object.Instances {
- instances = append(instances, &object.Instances[i])
- }
- return instances
- }
- type QuoteForm struct {
- ID int64 `json:"i" label:"ID" binding:"required"`
- PropResult map[string]*PropForm `json:"p" label:"属性结果"`
- Tables []TableObject `json:"o" label:"表格"`
- }
- func (from *QuoteForm) GetID() int64 {
- return from.ID
- }
- func (from *QuoteForm) Init(object Object, index int, order *Order) {
- if from.PropResult == nil {
- from.PropResult = make(map[string]*PropForm)
- }
- for name, _ := range from.PropResult {
- from.PropResult[name].Init(from, order)
- }
- if from.Tables == nil {
- from.Tables = make([]TableObject, 0)
- }
- for i, _ := range from.Tables {
- from.Tables[i].Init(from, order)
- }
- }
- func (from *QuoteForm) GetIndex() int {
- return 0
- }
- func (from *QuoteForm) GetObject() Object {
- return nil
- }
- func (from *QuoteForm) GetParent() Instance {
- return nil
- }
- func (from *QuoteForm) GetName() string {
- return ""
- }
- func (from *QuoteForm) GetShow() string {
- return ""
- }
- func (from *QuoteForm) GetNameResult() *PropForm {
- return nil
- }
- func (from *QuoteForm) GetShowResult() *PropForm {
- return nil
- }
- func (from *QuoteForm) GetShowResultBool() bool {
- return true
- }
- func (from *QuoteForm) SetNameResult(prop *PropForm) {
- }
- func (from *QuoteForm) SetShowResult(prop *PropForm) {
- }
- func (from *QuoteForm) GetPropResult(name string) *PropForm {
- if prop, ok := from.PropResult[name]; ok {
- return prop
- }
- return nil
- }
- func (from *QuoteForm) SetPropResult(name string, prop *PropForm) {
- from.PropResult[name] = prop
- }
- func (from *QuoteForm) Copy(object Object, getall bool, save bool) Instance {
- s := new(QuoteForm)
- s.ID = from.ID
- s.PropResult = make(map[string]*PropForm)
- s.Tables = make([]TableObject, 0)
- for name, prop := range from.PropResult {
- if getall || len(prop.UseKeys) != 0 && !prop.Header && prop.Control != nil {
- if !save || prop.Cycle != Immediate {
- s.PropResult[name] = prop
- s.PropResult[name].Instance = s
- }
- }
- }
- for _, table := range from.Tables {
- s.Tables = append(s.Tables, *table.Copy(s, getall, save).(*TableObject))
- }
- return s
- }
- func (from *QuoteForm) ToMap(save bool) map[string]interface{} {
- mp := make(map[string]interface{})
- mp["i"] = from.ID
- propResult := make(map[string]interface{})
- for name, prop := range from.PropResult {
- if save || !save && len(prop.UseKeys) != 0 && !prop.Header && prop.Control != nil {
- propResult[name] = prop.ToMap(save)
- }
- }
- mp["p"] = propResult
- tables := make([]interface{}, 0)
- for _, table := range from.Tables {
- tables = append(tables, table.ToMap(save))
- }
- mp["o"] = tables
- return mp
- }
- func (from *QuoteForm) Count() int {
- return 1
- }
- func (from *QuoteForm) GetSub() []Instance {
- subs := make([]Instance, 0)
- for _, table := range from.Tables {
- for i, _ := range table.Instances {
- subs = append(subs, &table.Instances[i])
- }
- }
- return subs
- }
- func (from *QuoteForm) Type() int {
- return Quote
- }*/
- type Order struct {
- ID int64
- AdminId int64
- CustomerId int64
- Models map[int]map[int64]budget.QuoteData
- /*Quote *budget.Quote
- Table map[int64]budget.Table
- Header map[int64]budget.Header
- Module map[int64]budget.Module
- Group map[int64]budget.Group
- Row map[int64]budget.Row
- Item map[int64]budget.Item*/
- Form *InstanceData
- Prop map[uint64]*PropForm
- PropKeyMax uint64
- PropUseCahce map[uint64][]uint64
- }
- func (order *Order) SetPropUse(key uint64, ukey uint64) {
- if _, ok := order.PropUseCahce[key]; !ok {
- order.PropUseCahce[key] = make([]uint64, 0)
- }
- for _, k := range order.PropUseCahce[key] {
- if k == key {
- return
- }
- }
- order.PropUseCahce[key] = append(order.PropUseCahce[key], ukey)
- }
- func (from *InstanceData) LoadOrder() (*Order, error) {
- order := new(Order)
- quote, tables, headers, modules, groups, rows, items := budget.GetQuoteModel(from.ID)
- if quote == nil {
- return nil, errors.New("报价不存在")
- }
- order.Prop = make(map[uint64]*PropForm)
- order.PropUseCahce = make(map[uint64][]uint64)
- order.Models = make(map[int]map[int64]budget.QuoteData)
- order.Models[Quote] = make(map[int64]budget.QuoteData, 0)
- order.Models[Quote][quote.ID] = quote
- order.Models[Table] = make(map[int64]budget.QuoteData, 0)
- for _, table := range tables {
- order.Models[Table][table.GetID()] = table
- }
- order.Models[Header] = make(map[int64]budget.QuoteData, 0)
- for _, header := range headers {
- order.Models[Header][header.GetID()] = header
- }
- order.Models[Module] = make(map[int64]budget.QuoteData, 0)
- for _, module := range modules {
- order.Models[Module][module.GetID()] = module
- }
- order.Models[Group] = make(map[int64]budget.QuoteData, 0)
- for _, group := range groups {
- order.Models[Group][group.GetID()] = group
- }
- order.Models[Row] = make(map[int64]budget.QuoteData, 0)
- for _, row := range rows {
- order.Models[Row][row.GetID()] = row
- }
- order.Models[Item] = make(map[int64]budget.QuoteData, 0)
- for _, item := range items {
- order.Models[Item][item.GetID()] = item
- }
- /*order.Table = make(map[int64]budget.Table)
- order.Header = make(map[int64]budget.Header)
- order.Module = make(map[int64]budget.Module)
- order.Group = make(map[int64]budget.Group)
- order.Row = make(map[int64]budget.Row)
- order.Item = make(map[int64]budget.Item)
- for _, table := range tables {
- order.Table[table.ID] = table
- }
- for _, header := range headers {
- order.Header[header.ID] = header
- }
- for _, module := range modules {
- order.Module[module.ID] = module
- }
- for _, group := range groups {
- order.Group[group.ID] = group
- }
- for _, row := range rows {
- order.Row[row.ID] = row
- }
- for _, item := range items {
- order.Item[item.ID] = item
- }
- order.Quote = quote*/
- from.Init(nil, nil, Quote, 0, order)
- order.Form = from
- return order, nil
- }
- func (order *Order) NewPropKey() uint64 {
- order.PropKeyMax++
- return order.PropKeyMax
- }
- func (order *Order) Save(context *Context) error {
- layout, _ := context.ParseValue("=户型信息")
- area, _ := context.ParseValue("=面积信息")
- jsonData, err := json.Marshal(order.ToMap(true))
- if err != nil {
- logger.Sugar.Error(err)
- return err
- }
- //logger.Sugar.Infof("%v", string(jsonData))
- var gzJson bytes.Buffer
- gz := gzip.NewWriter(&gzJson)
- //gz, err := gzip.NewWriterLevel(&gzJson, gzip.BestSpeed)
- //if err != nil {
- // return err
- //}
- if _, err := gz.Write(jsonData); err != nil {
- return err
- }
- if err := gz.Close(); err != nil {
- return err
- }
- content := gzJson.Bytes()
- logger.Sugar.Infof("content len: %v", len(content))
- data := map[string]interface{}{
- "quoteId": order.Form.GetID(),
- "adminId": order.AdminId,
- "content": content,
- "layout": layout,
- "area": area,
- }
- if order.ID == 0 {
- id, err := db.InsertModel(reflect.TypeOf(budget.Order{}), data)
- if err != nil {
- return err
- }
- order.ID = id
- } else {
- err := db.UpdateModel(reflect.TypeOf(budget.Order{}), order.ID, data)
- if err != nil {
- return err
- }
- }
- return nil
- }
- //func (order *Order) GetForm(getall bool, save bool) *QuoteForm {
- // return order.Form.Copy(nil, getall, save).(*QuoteForm)
- //}
- func (order *Order) ToMap(save bool) map[string]interface{} {
- return order.Form.ToMap(save)
- }
- func LoadOrder(model budget.Order) (*Order, error) {
- from := new(InstanceData)
- if len(model.Content) != 0 {
- gzJson := bytes.NewBuffer(model.Content)
- gz, err := gzip.NewReader(gzJson)
- if err != nil {
- return nil, err
- }
- defer gz.Close()
- jsonData, err := ioutil.ReadAll(gz)
- if err != nil {
- return nil, err
- }
- decoder := json.NewDecoder(strings.NewReader(string(jsonData)))
- decoder.UseNumber()
- err = decoder.Decode(&from)
- if err != nil {
- return nil, err
- }
- /*err = json.Unmarshal(jsonData, &from)
- if err != nil {
- return nil, err
- }*/
- }
- if from.ID == 0 {
- from.ID = model.QuoteId
- }
- order, err := from.LoadOrder()
- if err != nil {
- return nil, err
- }
- order.ID = model.ID
- order.AdminId = model.AdminId
- order.CustomerId = model.CustomerId
- return order, nil
- }
- func CreateOrder(quoteId int64) (*Order, error) {
- from := new(InstanceData)
- if from.ID == 0 {
- from.ID = quoteId
- }
- order, err := from.LoadOrder()
- if err != nil {
- return nil, err
- }
- order.ID = 0
- return order, nil
- }
- func (from *InstanceData) Export() string {
- export := xlsx.NewFile()
- style := xlsx.NewStyle()
- style.Border = *xlsx.NewBorder("thin", "thin", "thin", "thin")
- for _, table := range from.Subs {
- for _, itable := range table.Instances {
- sh, err := export.AddSheet(itable.GetNameResultString())
- if err != nil {
- return ""
- }
- headerRow := sh.AddRow()
- headerRow.SetHeight(20)
- headerRow.AddCell()
- headerNumber := 0
- for _, header := range itable.Headers {
- for _, iheader := range header.Instances {
- headerCell := headerRow.AddCell()
- headerCell.SetStyle(style)
- headerCell.Value = iheader.GetNameResultString()
- headerNumber++
- }
- }
- sh.SetColWidth(1, headerNumber+1, 30)
- for _, module := range itable.Subs {
- for _, imodule := range module.Instances {
- moduleRow := sh.AddRow()
- moduleRow.SetHeight(20)
- moduleCell := moduleRow.AddCell()
- moduleCell.SetStyle(style)
- moduleCell.Value = imodule.GetNameResultString()
- moduleCell.HMerge = headerNumber
- for _, group := range imodule.Subs {
- for _, igroup := range group.Instances {
- var groupCell1 *xlsx.Cell
- rowNumber := 0
- for _, row := range igroup.Subs {
- for _, irow := range row.Instances {
- rowRow := sh.AddRow()
- rowRow.SetHeight(20)
- groupCell := rowRow.AddCell()
- groupCell.SetStyle(style)
- groupCell.Value = igroup.GetNameResultString()
- if groupCell1 == nil {
- groupCell1 = groupCell
- }
- if irow.HeaderResult != nil {
- for _, header := range itable.Headers {
- if headerResult, ok := irow.HeaderResult[header.ID]; ok {
- for headerIndex, _ := range header.Instances {
- rowCell := rowRow.AddCell()
- rowCell.SetStyle(style)
- if headerIndex < len(headerResult) && headerResult[headerIndex] != nil {
- rowCell.Value = headerResult[headerIndex].ToString()
- }
- }
- }
- }
- }
- rowNumber++
- }
- }
- if groupCell1 != nil {
- groupCell1.VMerge = rowNumber - 1
- }
- }
- }
- }
- }
- }
- }
- exportFileName := utils.ToStr(time.Now().UnixNano()) + ".xlsx"
- if err := export.Save(config.Cfg.App.ExportPath + exportFileName); err != nil {
- return ""
- }
- return exportFileName
- }
- func (order *Order) Export2(filename string, c *gin.Context) string {
- template, err := xlsx.OpenFile(filename)
- if err != nil {
- logger.Sugar.Infof("err: %v", err)
- return ""
- }
- export := xlsx.NewFile()
- for sheetName, sheet := range template.Sheet {
- sh, err := export.AddSheet(sheetName)
- if err != nil {
- logger.Sugar.Infof("err: %v", err)
- return ""
- }
- sh.SheetFormat = sheet.SheetFormat
- sheet.Cols.ForEach(func(idx int, col *xlsx.Col) {
- sh.SetColParameters(col)
- //sh.SetColWidth(col.Min, col.Max, *col.Width)
- })
- err = sheet.ForEachRow(func(r *xlsx.Row) error {
- cells := make([]*xlsx.Cell, 0)
- err := r.ForEachCell(func(c *xlsx.Cell) error {
- cells = append(cells, c)
- return nil
- })
- if err != nil {
- return err
- }
- context := NewContext(order, c)
- for _, rows := range readCells(cells, context) {
- row := sh.AddRow()
- height := r.GetHeight()
- if height == 0 {
- height = sheet.SheetFormat.DefaultRowHeight
- }
- row.SetHeight(height)
- row.SetOutlineLevel(r.GetOutlineLevel())
- for i, cell := range rows {
- c := row.AddCell()
- binary, _ := cells[i].MarshalBinary()
- c.UnmarshalBinary(binary)
- c.SetStyle(cells[i].GetStyle())
- c.VMerge = cell.VMerge
- c.Value = cell.value
- }
- }
- return nil
- })
- if err != nil {
- logger.Sugar.Infof("err: %v", err)
- return ""
- }
- }
- exportFileName := utils.ToStr(time.Now().UnixNano()) + ".xlsx"
- if err := export.Save(config.Cfg.App.ExportPath + exportFileName); err != nil {
- logger.Sugar.Infof("err: %v", err)
- return ""
- }
- return exportFileName
- }
- type exportContext struct {
- value string
- VMerge int
- instance *InstanceData
- }
- func runValue(begin string, formula string, context *Context) []exportContext {
- prop, err := context.ParseForm(formula)
- if err != nil || prop.Result == nil {
- return []exportContext{{begin, 0, context.Current}}
- }
- results := make([]exportContext, 0)
- argParse([]interface{}{prop.Result}, func(result interface{}) {
- if instance, ok := result.(*InstanceData); ok {
- results = append(results, exportContext{begin, 0, instance})
- } else {
- results = append(results, exportContext{begin + db.ToString(result), 0, context.Current})
- }
- })
- return results
- }
- func runContext(contexts []exportContext, value string, context *Context) []exportContext {
- results := make([]exportContext, 0)
- for _, v := range contexts {
- last := context.Current
- context.Current = v.instance
- for _, c := range readValue(value, context) {
- c.value = v.value + c.value
- results = append(results, c)
- }
- context.Current = last
- }
- return results
- }
- func readValue(value string, context *Context) []exportContext {
- result := ""
- brackets := false
- begin := ""
- for i := 0; i < len([]rune(value)); i++ {
- switch []rune(value)[i] {
- case '{':
- begin = result
- result = ""
- brackets = true
- case '}':
- if brackets {
- formula := result
- result = ""
- return runContext(runValue(begin, formula, context), string([]rune(value)[i+1:]), context)
- } else {
- result += string([]rune(value)[i])
- }
- default:
- result += string([]rune(value)[i])
- }
- }
- if brackets {
- formula := result
- result = ""
- return runValue(begin, formula, context)
- }
- return []exportContext{{result, 0, context.Current}}
- }
- func readCells(cells []*xlsx.Cell, context *Context) [][]exportContext {
- rows := make([][]exportContext, 0)
- if len(cells) == 0 {
- rows = append(rows, []exportContext{})
- return rows
- }
- for _, c := range readValue(cells[0].String(), context) {
- last := context.Current
- context.Current = c.instance
- rights := readCells(cells[1:], context)
- for n, s := range rights {
- cc := c
- if n == 0 {
- cc.VMerge = len(rights) - 1
- }
- rows = append(rows, append([]exportContext{cc}, s...))
- }
- context.Current = last
- }
- return rows
- }
- type ObjectData struct {
- ID int64 `json:"i" label:"ID"`
- Type int `json:"-" label:"类型"`
- NameResult *PropForm `json:"n" label:"名称结果"`
- Parent *InstanceData `json:"-" label:"父实例"`
- Instances []InstanceData `json:"s" label:"实例"`
- }
- func (object *ObjectData) Init(parent *InstanceData, typ int, order *Order) {
- object.Parent = parent
- object.Type = typ
- if object.NameResult != nil {
- object.NameResult.Init(parent, order)
- }
- if object.Instances == nil {
- object.Instances = make([]InstanceData, 0)
- }
- for i, _ := range object.Instances {
- object.Instances[i].Init(parent, object, typ, i, order)
- }
- }
- func (object *ObjectData) ToMap(save bool) map[string]interface{} {
- mp := make(map[string]interface{})
- if save || object.Type == Header {
- mp["i"] = object.ID
- }
- if save && object.NameResult != nil {
- mp["n"] = object.NameResult.ToMap(save)
- }
- instances := make([]interface{}, 0)
- for _, instance := range object.Instances {
- if save || instance.GetShowResultBool() {
- instances = append(instances, instance.ToMap(save))
- }
- }
- mp["s"] = instances
- return mp
- }
- type InstanceData struct {
- ID int64 `json:"i" label:"ID"`
- Object *ObjectData `json:"-" label:"对象"`
- Parent *InstanceData `json:"-" label:"对象"`
- Index int `json:"-" label:"序号"`
- Type int `json:"-" label:"类型"`
- Name string `json:"n" label:"名称"`
- NameResult *PropForm `json:"nr" label:"名称结果"`
- Show string `json:"s" label:"显示"`
- ShowResult *PropForm `json:"sr" label:"显示结果"`
- ItemId int64 `json:"t"`
- Key string `json:"k"`
- PropResult map[string]*PropForm `json:"p" label:"属性结果"`
- Item *InstanceData `json:"m" label:"项目"`
- HeaderResult map[int64][]*PropForm `json:"hr"`
- Result *PropForm `json:"r" label:"计算结果"`
- Subs []ObjectData `json:"o" label:"行"`
- Headers []ObjectData `json:"h" label:"表头"`
- }
- func (instance *InstanceData) Init(parent *InstanceData, object *ObjectData, typ int, index int, order *Order) {
- instance.Object = object
- instance.Parent = parent
- instance.Index = index
- instance.Type = typ
- if instance.NameResult != nil {
- instance.NameResult.Init(instance, order)
- }
- if instance.ShowResult != nil {
- instance.ShowResult.Init(instance, order)
- }
- if instance.PropResult == nil {
- instance.PropResult = make(map[string]*PropForm)
- }
- for name, _ := range instance.PropResult {
- instance.PropResult[name].Init(instance, order)
- }
- if instance.Item != nil {
- instance.Item.Init(instance, object, Item, instance.Index, order)
- }
- if instance.Result != nil {
- instance.Result.Init(instance, order)
- }
- if instance.Type == Row {
- if instance.HeaderResult == nil {
- instance.HeaderResult = make(map[int64][]*PropForm)
- }
- } else {
- if instance.Type != Header {
- if instance.Subs == nil {
- instance.Subs = make([]ObjectData, 0)
- }
- subType := instance.Type + 1
- if instance.Type == Table {
- subType = Module
- }
- for i, _ := range instance.Subs {
- instance.Subs[i].Init(instance, subType, order)
- }
- }
- }
- if instance.Type == Table {
- if instance.Headers == nil {
- instance.Headers = make([]ObjectData, 0)
- }
- for i, _ := range instance.Headers {
- instance.Headers[i].Init(instance, Header, order)
- }
- }
- }
- func (instance *InstanceData) GetID() int64 {
- if instance.ID != 0 {
- return instance.ID
- }
- if instance.Object == nil {
- return 0
- }
- return instance.Object.ID
- }
- func (instance InstanceData) GetNameResultString() string {
- if instance.NameResult == nil {
- return ""
- }
- return instance.NameResult.ToString()
- }
- func (instance InstanceData) GetShowResultBool() bool {
- if instance.Show == "" {
- return true
- }
- if instance.ShowResult == nil {
- return false
- }
- return db.ToBool(instance.ShowResult.Result)
- }
- func (instance *InstanceData) SetNameResult(prop *PropForm) {
- instance.NameResult = prop
- }
- func (instance *InstanceData) SetShowResult(prop *PropForm) {
- instance.ShowResult = prop
- }
- func (instance InstanceData) GetPropResult(name string) *PropForm {
- if prop, ok := instance.PropResult[name]; ok {
- return prop
- }
- return nil
- }
- func (instance *InstanceData) SetPropResult(name string, prop *PropForm) {
- instance.PropResult[name] = prop
- }
- func (instance *InstanceData) GetSub() []*InstanceData {
- subs := make([]*InstanceData, 0)
- for _, sub := range instance.Subs {
- for i, _ := range sub.Instances {
- subs = append(subs, &sub.Instances[i])
- }
- }
- return subs
- }
- func (instance InstanceData) ToMap(save bool) map[string]interface{} {
- mp := make(map[string]interface{})
- if save {
- if instance.ID != 0 {
- mp["i"] = instance.ID
- } else {
- mp["n"] = instance.Name
- mp["s"] = instance.Show
- mp["t"] = instance.ItemId
- mp["k"] = instance.Key
- }
- } else {
- if instance.HeaderResult != nil {
- headerResult := make(map[int64][]interface{})
- for id, results := range instance.HeaderResult {
- headerResult[id] = make([]interface{}, 0)
- for _, result := range results {
- headerResult[id] = append(headerResult[id], result.ToMap(save))
- }
- }
- mp["hr"] = headerResult
- }
- }
- if !save || instance.NameResult != nil {
- mp["nr"] = instance.NameResult.ToMap(save)
- }
- if !save || instance.ShowResult != nil {
- mp["sr"] = instance.ShowResult.ToMap(save)
- }
- if instance.Item != nil {
- mp["m"] = instance.Item.ToMap(save)
- }
- if instance.Result != nil {
- mp["r"] = instance.Result.ToMap(save)
- }
- if instance.Subs != nil {
- subs := make([]interface{}, 0)
- for _, sub := range instance.Subs {
- subs = append(subs, sub.ToMap(save))
- }
- mp["o"] = subs
- }
- if instance.Headers != nil {
- headers := make([]interface{}, 0)
- for _, header := range instance.Headers {
- headers = append(headers, header.ToMap(save))
- }
- mp["h"] = headers
- }
- propResult := make(map[string]interface{})
- for name, prop := range instance.PropResult {
- if save || !save && len(prop.UseKeys) != 0 && !prop.Header && prop.Control != nil {
- propResult[name] = prop.ToMap(save)
- }
- }
- mp["p"] = propResult
- return mp
- }
|