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 }