12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099 |
- package budget
- import (
- "encoding/json"
- "errors"
- "math"
- "reflect"
- "strings"
- "time"
- "zhiyuan/models/budget"
- "zhiyuan/models/final"
- "zhiyuan/pkg/db"
- "zhiyuan/pkg/logger"
- "zhiyuan/services/admin"
- "github.com/Knetic/govaluate"
- )
- func argParse(args []interface{}, parse func(interface{})) {
- for _, arg := range args {
- if array, ok := arg.([]interface{}); ok {
- argParse(array, parse)
- } else {
- parse(arg)
- }
- }
- }
- func (context *Context) Functions() map[string]govaluate.ExpressionFunction {
- return map[string]govaluate.ExpressionFunction{
- "JsonEncode": context.funcJsonEncode,
- "JsonDecode": context.funcJsonDecode,
- "use": context.funcUse,
- "sum": context.funcSum,
- "len": context.funcLen,
- "name": context.funcName,
- "show": context.funcShow,
- "id": context.funcId,
- "index": context.funcIndex,
- "SubIndex": context.funcSubIndex,
- "count": context.funcCount,
- "instances": context.funcInstances,
- "round": context.funcRound,
- "ceil": context.funcCeil,
- "current": context.funcCurrent,
- "quote": context.funcQuote,
- "parent": context.funcParent,
- "filter": context.funcFilter,
- "sub": context.funcSub,
- "row": context.funcRow,
- "group": context.funcGroup,
- "module": context.funcModule,
- "table": context.funcTable,
- "eval": context.funcEval,
- "IsNull": context.funcIsNull,
- "StringIndex": context.funcStringIndex,
- "Index": context.funcCompoundIndex,
- "Array": context.funcArray,
- "InArray": context.funcInArray,
- "ArrayFilter": context.funcArrayFilter,
- "ArrayDedup": context.funcArrayDedup,
- "ArrayIndex": context.funcCompoundIndex,
- "ArrayMap": context.funcArrayMap,
- "ArraySplice": context.funcArraySplice,
- "ArrayMerge": context.funcArrayMerge,
- "Map": context.funcMap,
- "MapKeys": context.funcMapKeys,
- "MapValues": context.funcMapValues,
- "MapIndex": context.funcCompoundIndex,
- "MapMap": context.funcCompoundMap,
- "MapMerge": context.funcMapMerge,
- "SetProp": context.funcSetProp,
- "Control": context.funcControl,
- "Input": context.funcControlInput,
- "Number": context.funcControlNumber,
- "Checkbox": context.funcControlCheckbox,
- "Select": context.funcControlSelect,
- "Text": context.funcControlText,
- "SelectMat": context.funcControlSelectMat,
- "SelectItem": context.funcControlSelectItem,
- "Button": context.funcControlButton,
- "Group": context.funcControlGroup,
- "Popover": context.funcControlPopover,
- "GetMatTypeForName": context.funcGetMatTypeForName,
- "MatName": context.funcMatName,
- "MatType": context.funcMatType,
- "MatPrice": context.funcMatPrice,
- "MatControlPrice": context.funcMatControlPrice,
- "MatSalesPrice": context.funcMatSalesPrice,
- "MatPurchasePrice": context.funcMatPurchasePrice,
- "MatSupplier": context.funcMatSupplier,
- "GetMatTopType": context.funcGetMatTopType,
- "MatTypeName": context.funcMatTypeName,
- "MatTypeUnit": context.funcMatTypeUnit,
- "DataType": context.funcDataType,
- "debug": context.funcDebug,
- "CheckAuth": context.funcCheckAuth,
- "customer": context.funcCustomer,
- "phone": context.funcPhone,
- "address": context.funcAddress,
- "admin": context.funcAdmin,
- "GetItemTypeForName": context.funcGetItemTypeForName,
- "ItemTypeName": context.funcItemTypeName,
- "GetItemForType": context.funcGetItemForType,
- "GetSupplierForName": context.funcGetSupplierForName,
- "SupplierName": context.funcSupplierName,
- "now": context.funcNow,
- }
- }
- func (context *Context) funcJsonEncode(args ...interface{}) (interface{}, error) {
- if len(args) < 1 {
- return nil, errors.New("not enough arguments in call to call")
- }
- str, err := json.Marshal(args[0])
- if err != nil {
- return nil, err
- }
- return string(str), nil
- }
- func (context *Context) funcJsonDecode(args ...interface{}) (interface{}, error) {
- if len(args) < 1 {
- return nil, errors.New("not enough arguments in call to call")
- }
- var dest interface{}
- decoder := json.NewDecoder(strings.NewReader(string(db.ToString(args[0]))))
- decoder.UseNumber()
- err := decoder.Decode(&dest)
- return dest, err
- }
- func (context *Context) funcUse(args ...interface{}) (interface{}, error) {
- return "", nil
- }
- func (context *Context) funcSum(args ...interface{}) (interface{}, error) {
- var result float64
- argParse(args, func(arg interface{}) {
- ret, _ := db.ToFloat64(arg)
- result += ret
- })
- /*for _, arg := range args {
- ret, _ := db.ToFloat64(arg)
- result += ret
- }*/
- return result, nil
- }
- func (context *Context) funcLen(args ...interface{}) (interface{}, error) {
- return float64(len(args)), nil
- }
- func (context *Context) funcName(args ...interface{}) (interface{}, error) {
- names := make([]interface{}, 0)
- for _, arg := range args {
- if instance, ok := arg.(*InstanceData); ok {
- nameResult, err := context.LoadNameResult(instance)
- if err != nil {
- return nil, err
- }
- if nameResult == nil {
- names = append(names, nil)
- } else {
- names = append(names, filterValue(nameResult.Result))
- }
- } else {
- names = append(names, nil)
- }
- }
- if len(names) == 0 {
- nameResult, err := context.LoadNameResult(context.Current)
- if err != nil {
- return nil, err
- }
- if nameResult == nil {
- return nil, nil
- }
- return filterValue(nameResult.Result), nil
- }
- if len(names) == 1 {
- return names[0], nil
- }
- return names, nil
- }
- func (context *Context) funcShow(args ...interface{}) (interface{}, error) {
- shows := make([]interface{}, 0)
- for _, arg := range args {
- if instance, ok := arg.(*InstanceData); ok {
- context.LoadShowResult(instance)
- shows = append(shows, instance.GetShowResultBool())
- } else {
- shows = append(shows, nil)
- }
- }
- if len(shows) == 0 {
- context.LoadShowResult(context.Current)
- shows = append(shows, context.Current.GetShowResultBool())
- }
- if len(shows) == 1 {
- return shows[0], nil
- }
- return shows, nil
- }
- func (context *Context) funcId(args ...interface{}) (interface{}, error) {
- ids := make([]interface{}, 0)
- for _, arg := range args {
- if instance, ok := arg.(*InstanceData); ok {
- ids = append(ids, filterValue(instance.GetID()))
- } else {
- ids = append(ids, nil)
- }
- }
- if len(ids) == 0 {
- return filterValue(context.Current.GetID()), nil
- }
- if len(ids) == 1 {
- return ids[0], nil
- }
- return ids, nil
- }
- func (context *Context) funcIndex(args ...interface{}) (interface{}, error) {
- indexs := make([]interface{}, 0)
- for _, arg := range args {
- if instance, ok := arg.(*InstanceData); ok {
- indexs = append(indexs, filterValue(instance.Index))
- } else {
- indexs = append(indexs, 0)
- }
- }
- if len(indexs) == 0 {
- return filterValue(context.Current.Index), nil
- }
- if len(indexs) == 1 {
- return indexs[0], nil
- }
- return indexs, nil
- }
- func getSubIndex(instance *InstanceData) int {
- parent := instance.Parent
- if parent == nil {
- return 0
- }
- for n, i := range parent.GetSub() {
- if i.GetID() == instance.GetID() && i.Index == instance.Index {
- return n
- }
- }
- return 0
- }
- func (context *Context) funcSubIndex(args ...interface{}) (interface{}, error) {
- indexs := make([]interface{}, 0)
- for _, arg := range args {
- if instance, ok := arg.(*InstanceData); ok {
- indexs = append(indexs, filterValue(getSubIndex(instance)))
- } else {
- indexs = append(indexs, 0)
- }
- }
- if len(indexs) == 0 {
- return filterValue(getSubIndex(context.Current)), nil
- }
- if len(indexs) == 1 {
- return indexs[0], nil
- }
- return indexs, nil
- }
- func (context *Context) funcCount(args ...interface{}) (interface{}, error) {
- counts := make([]interface{}, 0)
- for _, arg := range args {
- if instance, ok := arg.(*InstanceData); ok {
- if instance.Object == nil {
- counts = append(counts, 1)
- } else {
- counts = append(counts, filterValue(len(instance.Object.Instances)))
- }
- } else {
- counts = append(counts, 0)
- }
- }
- if len(counts) == 0 {
- if context.Current.Object == nil {
- return filterValue(1), nil
- } else {
- return filterValue(len(context.Current.Object.Instances)), nil
- }
- }
- if len(counts) == 1 {
- return counts[0], nil
- }
- return counts, nil
- }
- func (context *Context) funcInstances(args ...interface{}) (interface{}, error) {
- names := make([]string, 0)
- instances := make([]*InstanceData, 0)
- argParse(args, func(arg interface{}) {
- if instance, ok := arg.(*InstanceData); ok {
- instances = append(instances, instance)
- } else {
- names = append(names, db.ToString(arg))
- }
- })
- /*for _, arg := range args {
- if ss, ok := arg.([]interface{}); ok {
- for _, s := range ss {
- if instance, ok := s.(Instance); ok {
- instances = append(instances, instance)
- } else if instance, ok := s.(*Instance); ok {
- instances = append(instances, *instance)
- } else {
- names = append(names, db.ToString(s))
- }
- }
- } else if instance, ok := arg.(Instance); ok {
- instances = append(instances, instance)
- } else if instance, ok := arg.(*Instance); ok {
- instances = append(instances, *instance)
- } else {
- names = append(names, db.ToString(arg))
- }
- }*/
- if len(instances) == 0 {
- instances = append(instances, context.Current)
- }
- resultss := make([]interface{}, 0)
- for _, instance := range instances {
- results := make([]interface{}, 0)
- if instance.Object == nil {
- if len(names) == 0 {
- results = append(results, instance)
- } else {
- for _, name := range names {
- if instance.NameResult != nil && db.ToString(instance.NameResult.Result) == name {
- results = append(results, instance)
- }
- }
- }
- } else {
- for i, _ := range instance.Object.Instances {
- if len(names) == 0 {
- err := context.LoadInstance(&instance.Object.Instances[i])
- if err != nil {
- return nil, err
- }
- results = append(results, &instance.Object.Instances[i])
- } else {
- _, err := context.LoadNameResult(&instance.Object.Instances[i])
- if err != nil {
- return nil, err
- }
- for _, name := range names {
- if instance.Object.Instances[i].NameResult != nil && db.ToString(instance.Object.Instances[i].NameResult.Result) == name {
- err = context.LoadInstance(&instance.Object.Instances[i])
- if err != nil {
- return nil, err
- }
- results = append(results, &instance.Object.Instances[i])
- }
- }
- }
- }
- }
- if len(results) == 0 {
- resultss = append(resultss, nil)
- } else if len(results) == 1 {
- resultss = append(resultss, results[0])
- } else {
- resultss = append(resultss, results)
- }
- }
- if len(resultss) == 0 {
- return nil, nil
- }
- if len(resultss) == 1 {
- return resultss[0], nil
- }
- return resultss, nil
- }
- func (context *Context) funcRound(args ...interface{}) (interface{}, error) {
- if len(args) < 1 {
- return nil, errors.New("not enough arguments in call to call")
- }
- p, _ := db.ToInt64(args[0])
- precision := math.Pow10(int(p))
- numbers := make([]interface{}, 0)
- for _, arg := range args[1:] {
- number, _ := db.ToFloat64(arg)
- numbers = append(numbers, filterValue(float64(math.Round(number*precision)/precision)))
- }
- if len(numbers) == 0 {
- return nil, nil
- }
- if len(numbers) == 1 {
- return numbers[0], nil
- }
- return numbers, nil
- }
- func (context *Context) funcCeil(args ...interface{}) (interface{}, error) {
- if len(args) < 1 {
- return nil, errors.New("not enough arguments in call to call")
- }
- p, _ := db.ToInt64(args[0])
- precision := math.Pow10(int(p))
- precision2 := math.Pow10(int(p + 2))
- numbers := make([]interface{}, 0)
- for _, arg := range args[1:] {
- number, _ := db.ToFloat64(arg)
- numbers = append(numbers, filterValue(float64(math.Ceil(math.Round(number*precision2)/100)/precision)))
- }
- if len(numbers) == 0 {
- return nil, nil
- }
- if len(numbers) == 1 {
- return numbers[0], nil
- }
- return numbers, nil
- }
- func (context *Context) funcCurrent(args ...interface{}) (interface{}, error) {
- return context.Current, nil
- }
- func (context *Context) funcQuote(args ...interface{}) (interface{}, error) {
- return context.Order.Form, nil
- }
- func (context *Context) funcParent(args ...interface{}) (interface{}, error) {
- parents := make([]interface{}, 0)
- for _, arg := range args {
- if instance, ok := arg.(*InstanceData); ok {
- parents = append(parents, instance.Parent)
- } else {
- parents = append(parents, nil)
- }
- }
- if len(parents) == 0 {
- return context.Current.Parent, nil
- }
- if len(parents) == 1 {
- return parents[0], nil
- }
- return parents, nil
- }
- func (context *Context) funcFilter(args ...interface{}) (interface{}, error) {
- expressions := make([]string, 0)
- instances := make([]*InstanceData, 0)
- argParse(args, func(arg interface{}) {
- if instance, ok := arg.(*InstanceData); ok {
- instances = append(instances, instance)
- } else {
- expressions = append(expressions, db.ToString(arg))
- }
- })
- /*for _, arg := range args {
- if ss, ok := arg.([]interface{}); ok {
- for _, s := range ss {
- if instance, ok := s.(Instance); ok {
- instances = append(instances, instance)
- } else if instance, ok := s.(*Instance); ok {
- instances = append(instances, *instance)
- } else {
- expressions = append(expressions, db.ToString(s))
- }
- }
- } else if instance, ok := arg.(Instance); ok {
- instances = append(instances, instance)
- } else if instance, ok := arg.(*Instance); ok {
- instances = append(instances, *instance)
- } else {
- expressions = append(expressions, db.ToString(arg))
- }
- }*/
- results := make([]interface{}, 0)
- for _, instance := range instances {
- ok := true
- for _, expression := range expressions {
- result, err := context.ParseValueinInstance(expression, instance)
- if err != nil {
- ok = false
- } else {
- ok = db.ToBool(result)
- }
- if !ok {
- break
- }
- }
- if ok {
- results = append(results, instance)
- }
- }
- if len(results) == 0 {
- return nil, nil
- }
- if len(results) == 1 {
- return results[0], nil
- }
- return results, nil
- }
- func (context *Context) findInstance(names []string, instance *InstanceData, typ int) ([]interface{}, error) {
- instances := make([]interface{}, 0)
- //logger.Sugar.Infof("instance.GetID(): %v %v", instance.GetID(), instance.Type())
- if typ == Base || instance.Type == typ {
- if len(names) == 0 {
- err := context.LoadInstance(instance)
- if err != nil {
- return nil, err
- }
- instances = append(instances, instance)
- } else {
- _, err := context.LoadNameResult(instance)
- if err != nil {
- return nil, err
- }
- for _, name := range names {
- nameResult := instance.NameResult
- if nameResult != nil && db.ToString(nameResult.Result) == name {
- err = context.LoadInstance(instance)
- if err != nil {
- return nil, err
- }
- instances = append(instances, instance)
- break
- }
- }
- }
- } else {
- err := context.LoadInstance(instance)
- if err != nil {
- return nil, err
- }
- for _, sub := range instance.GetSub() {
- ss, err := context.findInstance(names, sub, typ)
- if err != nil {
- return nil, err
- }
- instances = append(instances, ss...)
- }
- }
- return instances, nil
- }
- func (context *Context) findInstances(typ int, dinstance *InstanceData, args ...interface{}) (interface{}, error) {
- names := make([]string, 0)
- instances := make([]*InstanceData, 0)
- argParse(args, func(arg interface{}) {
- if arg != nil {
- if instance, ok := arg.(*InstanceData); ok {
- instances = append(instances, instance)
- } else {
- names = append(names, db.ToString(arg))
- }
- }
- })
- /*for _, arg := range args {
- if ss, ok := arg.([]interface{}); ok {
- for _, s := range ss {
- if instance, ok := s.(Instance); ok {
- instances = append(instances, instance)
- } else if instance, ok := s.(*Instance); ok {
- instances = append(instances, *instance)
- } else {
- names = append(names, db.ToString(s))
- }
- }
- } else if instance, ok := arg.(Instance); ok {
- instances = append(instances, instance)
- } else if instance, ok := arg.(*Instance); ok {
- instances = append(instances, *instance)
- } else {
- names = append(names, db.ToString(arg))
- }
- }*/
- if len(instances) == 0 {
- instances = append(instances, dinstance)
- }
- //logger.Sugar.Infof("instances: %v %v %v", instances, typ, names)
- subss := make([]interface{}, 0)
- for _, instance := range instances {
- var subs []interface{}
- if typ == Base {
- subs = make([]interface{}, 0)
- err := context.LoadInstance(instance)
- if err != nil {
- return nil, err
- }
- for _, sub := range instance.GetSub() {
- ss, err := context.findInstance(names, sub, typ)
- if err != nil {
- return nil, err
- }
- subs = append(subs, ss...)
- }
- } else {
- ss, err := context.findInstance(names, instance, typ)
- if err != nil {
- return nil, err
- }
- subs = ss
- }
- /*if len(subs) == 0 {
- subss = append(subss, nil)
- } else if len(subs) == 1 {
- subss = append(subss, subs[0])
- } else {
- subss = append(subss, subs)
- }*/
- subss = append(subss, subs...)
- }
- //logger.Sugar.Infof("instancesend: %v", instances)
- if len(subss) == 0 {
- return nil, nil
- }
- if len(subss) == 1 {
- return subss[0], nil
- }
- return subss, nil
- }
- func (context *Context) funcSub(args ...interface{}) (interface{}, error) {
- return context.findInstances(Base, context.Current, args...)
- }
- func (context *Context) funcRow(args ...interface{}) (interface{}, error) {
- return context.findInstances(Row, context.Order.Form, args...)
- }
- func (context *Context) funcGroup(args ...interface{}) (interface{}, error) {
- return context.findInstances(Group, context.Order.Form, args...)
- }
- func (context *Context) funcModule(args ...interface{}) (interface{}, error) {
- return context.findInstances(Module, context.Order.Form, args...)
- }
- func (context *Context) funcTable(args ...interface{}) (interface{}, error) {
- return context.findInstances(Table, context.Order.Form, args...)
- }
- func (context *Context) funcEval(args ...interface{}) (interface{}, error) {
- expressions := make([]string, 0)
- instances := make([]*InstanceData, 0)
- var globalProp map[string]interface{}
- argParse(args, func(arg interface{}) {
- if mp, ok := arg.(map[string]interface{}); ok {
- globalProp = mp
- } else if instance, ok := arg.(*InstanceData); ok {
- instances = append(instances, instance)
- } else {
- expressions = append(expressions, db.ToString(arg))
- }
- })
- /*for _, arg := range args {
- if ss, ok := arg.([]interface{}); ok {
- for _, s := range ss {
- if instance, ok := s.(Instance); ok {
- instances = append(instances, instance)
- } else if instance, ok := s.(*Instance); ok {
- instances = append(instances, *instance)
- } else {
- expressions = append(expressions, db.ToString(s))
- }
- }
- } else if mp, ok := arg.(map[string]interface{}); ok {
- globalProp = mp
- } else if instance, ok := arg.(Instance); ok {
- instances = append(instances, instance)
- } else if instance, ok := arg.(*Instance); ok {
- instances = append(instances, *instance)
- } else {
- expressions = append(expressions, db.ToString(arg))
- }
- }*/
- if len(instances) == 0 {
- instances = append(instances, context.Current)
- }
- resultss := make([]interface{}, 0)
- for _, instance := range instances {
- results := make([]interface{}, 0)
- for _, expression := range expressions {
- if globalProp == nil {
- result, err := context.ParseValueinInstance(expression, instance)
- if err != nil {
- results = append(results, nil)
- } else {
- results = append(results, filterValue(result))
- }
- } else {
- result, err := context.ParseValueInInstanceAndGlobalProp(expression, instance, globalProp)
- if err != nil {
- results = append(results, nil)
- } else {
- results = append(results, filterValue(result))
- }
- }
- }
- if len(results) == 0 {
- resultss = append(resultss, nil)
- } else if len(results) == 1 {
- resultss = append(resultss, results[0])
- } else {
- resultss = append(resultss, results)
- }
- }
- if len(resultss) == 0 {
- return nil, nil
- }
- if len(resultss) == 1 {
- return resultss[0], nil
- }
- return resultss, nil
- }
- func (context *Context) funcIsNull(args ...interface{}) (interface{}, error) {
- iss := make([]interface{}, 0)
- for _, arg := range args {
- iss = append(iss, arg == nil)
- }
- if len(iss) == 0 {
- return true, nil
- }
- if len(iss) == 1 {
- return iss[0], nil
- }
- return iss, nil
- }
- func (context *Context) funcArray(args ...interface{}) (interface{}, error) {
- if len(args) == 0 {
- return []interface{}{}, nil
- }
- return []interface{}{args}, nil
- }
- func (context *Context) funcInArray(args ...interface{}) (interface{}, error) {
- if len(args) < 2 {
- return nil, errors.New("not enough arguments in call to call")
- }
- index := db.ToString(args[0])
- if array, ok := args[1].([]interface{}); ok {
- for _, item := range array {
- if index == db.ToString(item) {
- return true, nil
- }
- }
- } else {
- return index == db.ToString(args[1]), nil
- }
- return false, nil
- }
- func (context *Context) funcStringIndex(args ...interface{}) (interface{}, error) {
- if len(args) < 2 {
- return nil, errors.New("not enough arguments in call to call")
- }
- s := db.ToString(args[0])
- sub := db.ToString(args[1])
- return filterValue(strings.Index(s, sub)), nil
- }
- func (context *Context) funcCompoundIndex(args ...interface{}) (interface{}, error) {
- if len(args) < 1 {
- return nil, errors.New("not enough arguments in call to call")
- }
- indexs := args[:len(args)-1]
- object := args[len(args)-1]
- values := make([]interface{}, 0)
- if array, ok := object.([]interface{}); ok {
- for _, index := range indexs {
- key, _ := db.ToInt64(index)
- if key < int64(len(array)) {
- values = append(values, filterValue(array[key]))
- } else {
- values = append(values, nil)
- }
- }
- }
- if mp, ok := object.(map[string]interface{}); ok {
- for _, index := range indexs {
- key := db.ToString(index)
- values = append(values, filterValue(mp[key]))
- }
- }
- if str, ok := object.(string); ok {
- for _, index := range indexs {
- key, _ := db.ToInt64(index)
- if key < int64(len(str)) {
- values = append(values, filterValue(string(str[key])))
- } else {
- values = append(values, nil)
- }
- }
- }
- if len(values) == 0 {
- return object, nil
- }
- if len(values) == 1 {
- return values[0], nil
- }
- return values, nil
- /*
- indexs := make([]interface{}, 0)
- objects := make([]interface{}, 0)
- for _, arg := range args {
- if array, ok := arg.([]interface{}); ok {
- objects = append(objects, array)
- } else if mp, ok := arg.(map[string]interface{}); ok {
- objects = append(objects, mp)
- } else {
- indexs = append(indexs, arg)
- }
- }
- resultss := make([]interface{}, 0)
- for _, object := range objects {
- results := make([]interface{}, 0)
- if array, ok := object.([]interface{}); ok {
- for _, index := range indexs {
- key, _ := db.ToInt64(index)
- if key < int64(len(array)) {
- results = append(values, array[key])
- } else {
- results = append(values, nil)
- }
- }
- } else if mp, ok := object.(map[string]interface{}); ok {
- for _, index := range indexs {
- key := db.ToString(index)
- results = append(values, mp[key])
- }
- }
- if len(results) == 0 {
- resultss = append(resultss, nil)
- } else if len(results) == 1 {
- resultss = append(resultss, results[0])
- } else {
- resultss = append(resultss, results)
- }
- }
- if len(resultss) == 0 {
- return nil, nil
- }
- if len(resultss) == 1 {
- return resultss[0], nil
- }
- return resultss, nil*/
- }
- func (context *Context) funcArrayMap(args ...interface{}) (interface{}, error) {
- expressions := make([]string, 0)
- objects := make([]interface{}, 0)
- for _, arg := range args {
- if array, ok := arg.([]interface{}); ok {
- objects = append(objects, array)
- } else if expression, ok := arg.(string); ok {
- expressions = append(expressions, expression)
- } else {
- objects = append(objects, []interface{}{arg})
- }
- }
- resultsss := make([]interface{}, 0)
- for _, object := range objects {
- resultss := make([]interface{}, 0)
- if array, ok := object.([]interface{}); ok {
- for k, v := range array {
- results := make([]interface{}, 0)
- globalProp := map[string]interface{}{
- "key": float64(k),
- "value": v,
- }
- for _, expression := range expressions {
- result, err := context.ParseValueinGlobalProp(expression, globalProp)
- if err != nil {
- results = append(results, nil)
- } else {
- results = append(results, filterValue(result))
- }
- }
- if len(results) == 0 {
- resultss = append(resultss, nil)
- } else if len(results) == 1 {
- resultss = append(resultss, results[0])
- } else {
- resultss = append(resultss, results)
- }
- }
- } else if mp, ok := object.(map[string]interface{}); ok {
- for k, v := range mp {
- results := make([]interface{}, 0)
- globalProp := map[string]interface{}{
- "key": k,
- "value": v,
- }
- for _, expression := range expressions {
- result, err := context.ParseValueinGlobalProp(expression, globalProp)
- if err != nil {
- results = append(results, nil)
- } else {
- results = append(results, filterValue(result))
- }
- }
- if len(results) == 0 {
- resultss = append(resultss, nil)
- } else if len(results) == 1 {
- resultss = append(resultss, results[0])
- } else {
- resultss = append(resultss, results)
- }
- }
- }
- if len(resultss) == 0 {
- resultsss = append(resultsss, nil)
- } else if len(resultss) == 1 {
- resultsss = append(resultsss, resultss[0])
- } else {
- resultsss = append(resultsss, resultss)
- }
- }
- if len(resultsss) == 0 {
- return nil, nil
- }
- if len(resultsss) == 1 {
- return resultsss[0], nil
- }
- return resultsss, nil
- }
- func (context *Context) funcCompoundMap(args ...interface{}) (interface{}, error) {
- expressions := make([]string, 0)
- objects := make([]interface{}, 0)
- for _, arg := range args {
- if array, ok := arg.([]interface{}); ok {
- objects = append(objects, array)
- } else if mp, ok := arg.(map[string]interface{}); ok {
- objects = append(objects, mp)
- } else {
- expressions = append(expressions, db.ToString(arg))
- }
- }
- resultsss := make([]interface{}, 0)
- for _, object := range objects {
- resultss := make([]interface{}, 0)
- if array, ok := object.([]interface{}); ok {
- for k, v := range array {
- results := make([]interface{}, 0)
- globalProp := map[string]interface{}{
- "key": float64(k),
- "value": v,
- }
- for _, expression := range expressions {
- result, err := context.ParseValueinGlobalProp(expression, globalProp)
- if err != nil {
- results = append(results, nil)
- } else {
- results = append(results, filterValue(result))
- }
- }
- if len(results) == 0 {
- resultss = append(resultss, nil)
- } else if len(results) == 1 {
- resultss = append(resultss, results[0])
- } else {
- resultss = append(resultss, results)
- }
- }
- } else if mp, ok := object.(map[string]interface{}); ok {
- for k, v := range mp {
- results := make([]interface{}, 0)
- globalProp := map[string]interface{}{
- "key": k,
- "value": v,
- }
- for _, expression := range expressions {
- result, err := context.ParseValueinGlobalProp(expression, globalProp)
- if err != nil {
- results = append(results, nil)
- } else {
- results = append(results, filterValue(result))
- }
- }
- if len(results) == 0 {
- resultss = append(resultss, nil)
- } else if len(results) == 1 {
- resultss = append(resultss, results[0])
- } else {
- resultss = append(resultss, results)
- }
- }
- }
- if len(resultss) == 0 {
- resultsss = append(resultsss, nil)
- } else if len(resultss) == 1 {
- resultsss = append(resultsss, resultss[0])
- } else {
- resultsss = append(resultsss, resultss)
- }
- }
- if len(resultsss) == 0 {
- return nil, nil
- }
- if len(resultsss) == 1 {
- return resultsss[0], nil
- }
- return resultsss, nil
- }
- func (context *Context) funcArrayFilter(args ...interface{}) (interface{}, error) {
- array := make([]interface{}, 0)
- for _, arg := range args {
- if db.ToBool(arg) {
- array = append(array, filterValue(arg))
- }
- }
- return array, nil
- }
- func (context *Context) funcArrayDedup(args ...interface{}) (interface{}, error) {
- tmp := make(map[string]int)
- array := make([]interface{}, 0)
- for _, arg := range args {
- s := db.ToString(arg)
- if _, ok := tmp[s]; !ok {
- array = append(array, filterValue(arg))
- tmp[s] = 1
- }
- }
- return array, nil
- }
- func (context *Context) funcArraySplice(args ...interface{}) (interface{}, error) {
- if len(args) < 2 {
- return nil, errors.New("not enough arguments in call to call")
- }
- index, _ := db.ToInt64(args[0])
- array := []interface{}{}
- if a, ok := args[1].([]interface{}); ok {
- array = a
- } else {
- array = append(array, args[1])
- }
- n := int64(len(array)) - index
- if len(args) > 2 {
- n, _ = db.ToInt64(args[2])
- }
- if index < int64(len(array)) && index >= 0 {
- before := array[:index]
- after := []interface{}{}
- if n <= 0 {
- after = append(after, array[index:]...)
- } else if index+n < int64(len(array)) {
- after = append(after, array[index+n:]...)
- }
- array = before
- for n := 3; n < len(args); n++ {
- array = append(array, args[n])
- }
- array = append(array, after...)
- }
- return array, nil
- }
- func (context *Context) funcArrayMerge(args ...interface{}) (interface{}, error) {
- array := make([]interface{}, 0)
- for _, arg := range args {
- if s, ok := arg.([]interface{}); ok {
- array = append(array, s...)
- } else {
- array = append(array, arg)
- }
- }
- return array, nil
- }
- func (context *Context) funcMap(args ...interface{}) (interface{}, error) {
- m := make(map[string]interface{})
- for i := 0; i < len(args)-1; i += 2 {
- m[db.ToString(args[i])] = filterValue(args[i+1])
- }
- return m, nil
- }
- func (context *Context) funcMapKeys(args ...interface{}) (interface{}, error) {
- if len(args) < 1 {
- return nil, errors.New("not enough arguments in call to call")
- }
- keys := make([]interface{}, 0)
- if mp, ok := args[0].(map[string]interface{}); ok {
- for key, _ := range mp {
- keys = append(keys, key)
- }
- }
- return keys, nil
- }
- func (context *Context) funcMapValues(args ...interface{}) (interface{}, error) {
- if len(args) < 1 {
- return nil, errors.New("not enough arguments in call to call")
- }
- values := make([]interface{}, 0)
- if mp, ok := args[0].(map[string]interface{}); ok {
- for _, value := range mp {
- values = append(values, filterValue(value))
- }
- }
- return values, nil
- }
- func (context *Context) funcMapMerge(args ...interface{}) (interface{}, error) {
- mp := make(map[string]interface{})
- for _, arg := range args {
- if m, ok := arg.(map[string]interface{}); ok {
- for k, v := range m {
- mp[k] = filterValue(v)
- }
- }
- }
- return mp, nil
- }
- func (context *Context) funcSetProp(args ...interface{}) (interface{}, error) {
- if len(args) < 2 {
- return nil, errors.New("not enough arguments in call to call")
- }
- name := db.ToString(args[0])
- prop, err := context.GetProp(name)
- if prop == nil {
- return nil, err
- }
- prop.Result = args[1]
- context.ModifyAfter(prop.Key)
- return prop.Result, nil
- }
- func (context *Context) funcControl(args ...interface{}) (interface{}, error) {
- if len(args) < 1 {
- return nil, errors.New("not enough arguments in call to call")
- }
- var control Control
- control.PraseForArgs(args...)
- return control, nil
- }
- func (context *Context) funcControlInput(args ...interface{}) (interface{}, error) {
- var control Control
- control.PraseForArgs(append([]interface{}{"input"}, args...)...)
- return control, nil
- }
- func (context *Context) funcControlNumber(args ...interface{}) (interface{}, error) {
- var control Control
- control.PraseForArgs(append([]interface{}{"number"}, args...)...)
- return control, nil
- }
- func (context *Context) funcControlCheckbox(args ...interface{}) (interface{}, error) {
- var value interface{}
- if len(args) > 0 {
- value = args[0]
- }
- var param map[string]interface{}
- if len(args) > 2 {
- if p, ok := args[2].(map[string]interface{}); ok {
- param = p
- } else {
- param = make(map[string]interface{})
- }
- } else {
- param = make(map[string]interface{})
- }
- if len(args) > 1 {
- param["label"] = args[1]
- }
- if len(args) > 3 {
- args = args[3:]
- } else {
- args = make([]interface{}, 0)
- }
- var control Control
- control.PraseForArgs(append([]interface{}{"checkbox", value, param}, args...)...)
- return control, nil
- }
- func (context *Context) funcControlText(args ...interface{}) (interface{}, error) {
- var control Control
- var value interface{}
- if len(args) > 1 {
- value = args[1]
- }
- var param map[string]interface{}
- if len(args) > 2 {
- if p, ok := args[2].(map[string]interface{}); ok {
- param = p
- } else {
- param = make(map[string]interface{})
- }
- } else {
- param = make(map[string]interface{})
- }
- if len(args) > 0 {
- param["label"] = args[0]
- }
- var event map[string]string
- if len(args) > 3 {
- if e, ok := args[3].(map[string]string); ok {
- event = e
- } else {
- event = make(map[string]string)
- if e, ok := args[3].(map[string]interface{}); ok {
- for key, val := range e {
- event[key] = db.ToString(val)
- }
- }
- }
- } else {
- event = make(map[string]string)
- }
- last := false
- if len(args) > 4 {
- if p, ok := args[4].(bool); ok {
- last = p
- }
- }
- control.PraseForArgs("text", value, param, event, last)
- return control, nil
- }
- func (context *Context) funcControlSelect(args ...interface{}) (interface{}, error) {
- var value interface{}
- if len(args) > 1 {
- value = args[1]
- }
- var param map[string]interface{}
- if len(args) > 2 {
- if p, ok := args[2].(map[string]interface{}); ok {
- param = p
- } else {
- param = make(map[string]interface{})
- }
- } else {
- param = make(map[string]interface{})
- }
- if len(args) > 0 {
- if p, ok := args[0].([]interface{}); ok {
- param["options"] = p
- } else {
- param["options"] = make([]interface{}, 0)
- }
- }
- if len(args) > 3 {
- args = args[3:]
- } else {
- args = make([]interface{}, 0)
- }
- var control Control
- control.PraseForArgs(append([]interface{}{"select", value, param}, args...)...)
- return control, nil
- }
- func (context *Context) funcControlGroup(args ...interface{}) (interface{}, error) {
- controls := make([]Control, 0)
- values := make([]interface{}, 0)
- for _, arg := range args {
- var control Control
- if c, ok := arg.(Control); ok {
- control = c
- } else if c, ok := arg.(*Control); ok {
- control = *c
- } else if c, ok := arg.(map[string]interface{}); ok {
- control.PraseForMap(c)
- }
- controls = append(controls, control)
- values = append(values, control.Value)
- }
- var control Control
- control.Type = "group"
- control.Value = values
- control.Param = map[string]interface{}{
- "controls": controls,
- }
- control.Event = make(map[string]string)
- control.Last = true
- return control, nil
- }
- func (context *Context) funcControlPopover(args ...interface{}) (interface{}, error) {
- var param map[string]interface{}
- if len(args) > 1 {
- if p, ok := args[1].(map[string]interface{}); ok {
- param = p
- } else {
- param = make(map[string]interface{})
- }
- } else {
- param = make(map[string]interface{})
- }
- if len(args) > 0 {
- param["label"] = args[0]
- }
- if len(args) > 2 {
- args = args[2:]
- } else {
- args = make([]interface{}, 0)
- }
- controls := make([]Control, 0)
- values := make([]interface{}, 0)
- last := true
- for _, arg := range args {
- if control, ok := arg.(Control); ok {
- controls = append(controls, control)
- values = append(values, control.Value)
- } else if control, ok := arg.(*Control); ok {
- controls = append(controls, *control)
- values = append(values, control.Value)
- } else if c, ok := arg.(map[string]interface{}); ok {
- var control Control
- control.PraseForMap(c)
- controls = append(controls, control)
- values = append(values, control.Value)
- } else if c, ok := arg.(bool); ok {
- last = c
- }
- }
- param["controls"] = controls
- var control Control
- control.Type = "popover"
- control.Value = values
- control.Param = param
- control.Event = make(map[string]string)
- control.Last = last
- return control, nil
- }
- func (context *Context) funcControlSelectMat(args ...interface{}) (interface{}, error) {
- var control Control
- var value interface{}
- if len(args) > 1 {
- value = args[1]
- }
- var param map[string]interface{}
- if len(args) > 2 {
- if p, ok := args[2].(map[string]interface{}); ok {
- param = p
- } else {
- param = make(map[string]interface{})
- }
- } else {
- param = make(map[string]interface{})
- }
- if len(args) > 0 {
- if s, ok := args[0].([]interface{}); ok {
- param["type"] = s
- } else {
- id, _ := db.ToInt64(args[0])
- param["type"] = []interface{}{id}
- }
- }
- if len(args) > 3 {
- args = args[3:]
- } else {
- args = make([]interface{}, 0)
- }
- control.PraseForArgs(append([]interface{}{"selectmat", value, param}, args...)...)
- return control, nil
- }
- func (context *Context) funcControlSelectItem(args ...interface{}) (interface{}, error) {
- var control Control
- var value interface{}
- if len(args) > 1 {
- value = args[1]
- }
- var param map[string]interface{}
- if len(args) > 2 {
- if p, ok := args[2].(map[string]interface{}); ok {
- param = p
- } else {
- param = make(map[string]interface{})
- }
- } else {
- param = make(map[string]interface{})
- }
- if len(args) > 0 {
- if s, ok := args[0].([]interface{}); ok {
- param["type"] = s
- } else {
- id, _ := db.ToInt64(args[0])
- param["type"] = []interface{}{id}
- }
- }
- if len(args) > 3 {
- args = args[3:]
- } else {
- args = make([]interface{}, 0)
- }
- control.PraseForArgs(append([]interface{}{"selectitem", value, param}, args...)...)
- return control, nil
- }
- func (context *Context) funcControlButton(args ...interface{}) (interface{}, error) {
- var param map[string]interface{}
- if len(args) > 2 {
- if p, ok := args[2].(map[string]interface{}); ok {
- param = p
- } else {
- param = make(map[string]interface{})
- }
- } else {
- param = make(map[string]interface{})
- }
- var event map[string]string
- if len(args) > 3 {
- if e, ok := args[3].(map[string]string); ok {
- event = e
- } else {
- event = make(map[string]string)
- if event, ok := args[3].(map[string]interface{}); ok {
- for key, val := range event {
- event[key] = db.ToString(val)
- }
- }
- }
- } else {
- event = make(map[string]string)
- }
- if len(args) > 0 {
- param["label"] = args[0]
- }
- if len(args) > 1 {
- event["click"] = db.ToString(args[1])
- }
- if len(args) > 4 {
- args = args[4:]
- } else {
- args = make([]interface{}, 0)
- }
- var control Control
- control.PraseForArgs(append([]interface{}{"button", nil, param, event}, args...)...)
- return control, nil
- }
- func (context *Context) funcGetMatTypeForName(args ...interface{}) (interface{}, error) {
- context.MatTypeInit()
- pids := make([]interface{}, 0)
- for _, arg := range args {
- typs := strings.Split(db.ToString(arg), "/")
- pid := int64(0)
- for _, name := range typs {
- if name == "" {
- continue
- }
- if nameCache, ok := context.MatTypePidNameCache[pid]; ok {
- if typ, ok := nameCache[name]; ok {
- pid = typ.ID
- }
- }
- }
- if pid == 0 {
- pids = append(pids, nil)
- } else {
- pids = append(pids, filterValue(pid))
- }
- }
- if len(pids) == 0 {
- return nil, nil
- }
- if len(pids) == 1 {
- return pids[0], nil
- }
- return pids, nil
- }
- func (context *Context) funcMatName(args ...interface{}) (interface{}, error) {
- names := make([]interface{}, 0)
- for _, arg := range args {
- id, _ := db.ToInt64(arg)
- mat := context.GetMat(id)
- if mat != nil {
- name := mat.Brand + " " + mat.Series + " " + mat.Model + " " + mat.Specs + " " + mat.Color
- names = append(names, name)
- } else {
- names = append(names, nil)
- }
- }
- if len(names) == 0 {
- return nil, nil
- }
- if len(names) == 1 {
- return names[0], nil
- }
- return names, nil
- }
- func (context *Context) funcMatType(args ...interface{}) (interface{}, error) {
- types := make([]interface{}, 0)
- for _, arg := range args {
- id, _ := db.ToInt64(arg)
- mat := context.GetMat(id)
- if mat != nil {
- types = append(types, filterValue(mat.TypeId))
- } else {
- types = append(types, nil)
- }
- }
- if len(types) == 0 {
- return nil, nil
- }
- if len(types) == 1 {
- return types[0], nil
- }
- return types, nil
- }
- func (context *Context) funcMatPrice(args ...interface{}) (interface{}, error) {
- prices := make([]interface{}, 0)
- for _, arg := range args {
- id, _ := db.ToInt64(arg)
- mat := context.GetMat(id)
- if mat != nil {
- prices = append(prices, filterValue(mat.Price))
- } else {
- prices = append(prices, nil)
- }
- }
- if len(prices) == 0 {
- return nil, nil
- }
- if len(prices) == 1 {
- return prices[0], nil
- }
- return prices, nil
- }
- func (context *Context) funcMatControlPrice(args ...interface{}) (interface{}, error) {
- prices := make([]interface{}, 0)
- for _, arg := range args {
- id, _ := db.ToInt64(arg)
- mat := context.GetMat(id)
- if mat != nil {
- prices = append(prices, filterValue(final.ControlPrice(mat.ControlPrice, mat.Price)))
- } else {
- prices = append(prices, nil)
- }
- }
- if len(prices) == 0 {
- return nil, nil
- }
- if len(prices) == 1 {
- return prices[0], nil
- }
- return prices, nil
- }
- func (context *Context) funcMatSalesPrice(args ...interface{}) (interface{}, error) {
- prices := make([]interface{}, 0)
- for _, arg := range args {
- id, _ := db.ToInt64(arg)
- mat := context.GetMat(id)
- if mat != nil {
- prices = append(prices, filterValue(final.SalesPrice(mat.SalesPrice, mat.Price)))
- } else {
- prices = append(prices, nil)
- }
- }
- if len(prices) == 0 {
- return nil, nil
- }
- if len(prices) == 1 {
- return prices[0], nil
- }
- return prices, nil
- }
- func (context *Context) funcMatPurchasePrice(args ...interface{}) (interface{}, error) {
- prices := make([]interface{}, 0)
- for _, arg := range args {
- id, _ := db.ToInt64(arg)
- mat := context.GetMat(id)
- if mat != nil {
- prices = append(prices, filterValue(final.PurchasePrice(mat.PurchasePrice, mat.Price)))
- } else {
- prices = append(prices, nil)
- }
- }
- if len(prices) == 0 {
- return nil, nil
- }
- if len(prices) == 1 {
- return prices[0], nil
- }
- return prices, nil
- }
- func (context *Context) funcMatSupplier(args ...interface{}) (interface{}, error) {
- suppliers := make([]interface{}, 0)
- for _, arg := range args {
- id, _ := db.ToInt64(arg)
- mat := context.GetMat(id)
- if mat != nil {
- suppliers = append(suppliers, filterValue(mat.SupplierId))
- } else {
- suppliers = append(suppliers, nil)
- }
- }
- if len(suppliers) == 0 {
- return nil, nil
- }
- if len(suppliers) == 1 {
- return suppliers[0], nil
- }
- return suppliers, nil
- }
- func (context *Context) funcGetMatTopType(args ...interface{}) (interface{}, error) {
- context.MatTypeInit()
- types := make([]interface{}, 0)
- for _, arg := range args {
- id, _ := db.ToInt64(arg)
- for {
- if typ, ok := context.MatTypeCache[id]; ok {
- if typ.Pid != 0 {
- id = typ.Pid
- } else {
- break
- }
- } else {
- break
- }
- }
- types = append(types, filterValue(id))
- }
- if len(types) == 0 {
- return nil, nil
- }
- if len(types) == 1 {
- return types[0], nil
- }
- return types, nil
- }
- func (context *Context) funcMatTypeName(args ...interface{}) (interface{}, error) {
- context.MatTypeInit()
- names := make([]interface{}, 0)
- for _, arg := range args {
- id, _ := db.ToInt64(arg)
- if typ, ok := context.MatTypeCache[id]; ok {
- names = append(names, typ.Name)
- } else {
- names = append(names, nil)
- }
- }
- if len(names) == 0 {
- return nil, nil
- }
- if len(names) == 1 {
- return names[0], nil
- }
- return names, nil
- }
- func (context *Context) funcMatTypeUnit(args ...interface{}) (interface{}, error) {
- context.MatTypeInit()
- types := make([]interface{}, 0)
- for _, arg := range args {
- id, _ := db.ToInt64(arg)
- if typ, ok := context.MatTypeCache[id]; ok {
- types = append(types, typ.Unit)
- } else {
- types = append(types, "")
- }
- }
- if len(types) == 0 {
- return nil, nil
- }
- if len(types) == 1 {
- return types[0], nil
- }
- return types, nil
- }
- func (context *Context) funcDataType(args ...interface{}) (interface{}, error) {
- types := make([]interface{}, 0)
- for _, arg := range args {
- typ := reflect.TypeOf(arg)
- if typ == nil {
- types = append(types, nil)
- } else {
- types = append(types, typ.String())
- }
- }
- if len(types) == 0 {
- return nil, nil
- }
- if len(types) == 1 {
- return types[0], nil
- }
- return types, nil
- }
- func (context *Context) funcDebug(args ...interface{}) (interface{}, error) {
- logger.Sugar.Infof("Debug %v: %v arg", context.Calls[len(context.Calls)-1].Expression, len(args))
- for n, arg := range args {
- typ := reflect.TypeOf(arg)
- typs := ""
- if typ == nil {
- } else {
- typs = typ.String()
- }
- logger.Sugar.Infof("arg[%v]: %+v %v", n, arg, typs)
- }
- if len(args) == 1 {
- return args[0], nil
- }
- return args, nil
- }
- func (context *Context) funcCheckAuth(args ...interface{}) (interface{}, error) {
- auths := make([]string, 0)
- argParse(args, func(arg interface{}) {
- auths = append(auths, db.ToString(arg))
- })
- return admin.CheckAuth(auths, context.C.GetInt("adminID")), nil
- }
- func (context *Context) funcCustomer(args ...interface{}) (interface{}, error) {
- customer := context.Customer()
- if customer != nil {
- return customer.Username, nil
- }
- return "", nil
- }
- func (context *Context) funcPhone(args ...interface{}) (interface{}, error) {
- customer := context.Customer()
- if customer != nil {
- return customer.Phone, nil
- }
- return "", nil
- }
- func (context *Context) funcAddress(args ...interface{}) (interface{}, error) {
- customer := context.Customer()
- if customer != nil {
- return customer.Address + " " + customer.Village + " " + customer.RoomNo, nil
- }
- return "", nil
- }
- func (context *Context) funcAdmin(args ...interface{}) (interface{}, error) {
- admin := context.Admin()
- if admin != nil {
- return admin.Username, nil
- }
- return "", nil
- }
- func (context *Context) funcGetItemTypeForName(args ...interface{}) (interface{}, error) {
- context.ItemTypeInit()
- pids := make([]interface{}, 0)
- for _, arg := range args {
- typs := strings.Split(db.ToString(arg), "/")
- pid := int64(0)
- for _, name := range typs {
- if name == "" {
- continue
- }
- if nameCache, ok := context.ItemTypePidNameCache[pid]; ok {
- if typ, ok := nameCache[name]; ok {
- pid = typ.ID
- }
- }
- }
- if pid == 0 {
- pids = append(pids, nil)
- } else {
- pids = append(pids, filterValue(pid))
- }
- }
- if len(pids) == 0 {
- return nil, nil
- }
- if len(pids) == 1 {
- return pids[0], nil
- }
- return pids, nil
- }
- func (context *Context) funcItemTypeName(args ...interface{}) (interface{}, error) {
- context.ItemTypeInit()
- names := make([]interface{}, 0)
- for _, arg := range args {
- id, _ := db.ToInt64(arg)
- if typ, ok := context.ItemTypeCache[id]; ok {
- names = append(names, typ.Name)
- } else {
- names = append(names, nil)
- }
- }
- if len(names) == 0 {
- return nil, nil
- }
- if len(names) == 1 {
- return names[0], nil
- }
- return names, nil
- }
- func (context *Context) funcGetItemForType(args ...interface{}) (interface{}, error) {
- ids := make([]interface{}, 0)
- for _, arg := range args {
- typid, _ := db.ToInt64(arg)
- tids := budget.AllItemTypes([]int64{typid})
- items, _ := budget.GetItemModels(map[string]interface{}{
- "typeId in": tids,
- "deleted_at": 0,
- })
- for _, item := range items {
- if _, ok := context.Order.Models[Item][item.ID]; !ok {
- context.Order.Models[Item][item.ID] = item
- }
- ids = append(ids, filterValue(item.ID))
- }
- }
- if len(ids) == 1 {
- return ids[0], nil
- }
- return ids, nil
- }
- func (context *Context) funcGetSupplierForName(args ...interface{}) (interface{}, error) {
- context.SupplierInit()
- ids := make([]interface{}, 0)
- for _, arg := range args {
- name := db.ToString(arg)
- if supplier, ok := context.SupplierNameCache[name]; ok {
- ids = append(ids, filterValue(supplier.ID))
- } else {
- ids = append(ids, nil)
- }
- }
- if len(ids) == 0 {
- return nil, nil
- }
- if len(ids) == 1 {
- return ids[0], nil
- }
- return ids, nil
- }
- func (context *Context) funcSupplierName(args ...interface{}) (interface{}, error) {
- context.SupplierInit()
- names := make([]interface{}, 0)
- for _, arg := range args {
- id, _ := db.ToInt64(arg)
- if supplier, ok := context.SupplierCache[id]; ok {
- names = append(names, supplier.Name)
- } else {
- names = append(names, nil)
- }
- }
- if len(names) == 0 {
- return nil, nil
- }
- if len(names) == 1 {
- return names[0], nil
- }
- return names, nil
- }
- func (context *Context) funcNow(args ...interface{}) (interface{}, error) {
- return float64(time.Now().Unix()), nil
- }
|