functions.go 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099
  1. package budget
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "math"
  6. "reflect"
  7. "strings"
  8. "time"
  9. "zhiyuan/models/budget"
  10. "zhiyuan/models/final"
  11. "zhiyuan/pkg/db"
  12. "zhiyuan/pkg/logger"
  13. "zhiyuan/services/admin"
  14. "github.com/Knetic/govaluate"
  15. )
  16. func argParse(args []interface{}, parse func(interface{})) {
  17. for _, arg := range args {
  18. if array, ok := arg.([]interface{}); ok {
  19. argParse(array, parse)
  20. } else {
  21. parse(arg)
  22. }
  23. }
  24. }
  25. func (context *Context) Functions() map[string]govaluate.ExpressionFunction {
  26. return map[string]govaluate.ExpressionFunction{
  27. "JsonEncode": context.funcJsonEncode,
  28. "JsonDecode": context.funcJsonDecode,
  29. "use": context.funcUse,
  30. "sum": context.funcSum,
  31. "len": context.funcLen,
  32. "name": context.funcName,
  33. "show": context.funcShow,
  34. "id": context.funcId,
  35. "index": context.funcIndex,
  36. "SubIndex": context.funcSubIndex,
  37. "count": context.funcCount,
  38. "instances": context.funcInstances,
  39. "round": context.funcRound,
  40. "ceil": context.funcCeil,
  41. "current": context.funcCurrent,
  42. "quote": context.funcQuote,
  43. "parent": context.funcParent,
  44. "filter": context.funcFilter,
  45. "sub": context.funcSub,
  46. "row": context.funcRow,
  47. "group": context.funcGroup,
  48. "module": context.funcModule,
  49. "table": context.funcTable,
  50. "eval": context.funcEval,
  51. "IsNull": context.funcIsNull,
  52. "StringIndex": context.funcStringIndex,
  53. "Index": context.funcCompoundIndex,
  54. "Array": context.funcArray,
  55. "InArray": context.funcInArray,
  56. "ArrayFilter": context.funcArrayFilter,
  57. "ArrayDedup": context.funcArrayDedup,
  58. "ArrayIndex": context.funcCompoundIndex,
  59. "ArrayMap": context.funcArrayMap,
  60. "ArraySplice": context.funcArraySplice,
  61. "ArrayMerge": context.funcArrayMerge,
  62. "Map": context.funcMap,
  63. "MapKeys": context.funcMapKeys,
  64. "MapValues": context.funcMapValues,
  65. "MapIndex": context.funcCompoundIndex,
  66. "MapMap": context.funcCompoundMap,
  67. "MapMerge": context.funcMapMerge,
  68. "SetProp": context.funcSetProp,
  69. "Control": context.funcControl,
  70. "Input": context.funcControlInput,
  71. "Number": context.funcControlNumber,
  72. "Checkbox": context.funcControlCheckbox,
  73. "Select": context.funcControlSelect,
  74. "Text": context.funcControlText,
  75. "SelectMat": context.funcControlSelectMat,
  76. "SelectItem": context.funcControlSelectItem,
  77. "Button": context.funcControlButton,
  78. "Group": context.funcControlGroup,
  79. "Popover": context.funcControlPopover,
  80. "GetMatTypeForName": context.funcGetMatTypeForName,
  81. "MatName": context.funcMatName,
  82. "MatType": context.funcMatType,
  83. "MatPrice": context.funcMatPrice,
  84. "MatControlPrice": context.funcMatControlPrice,
  85. "MatSalesPrice": context.funcMatSalesPrice,
  86. "MatPurchasePrice": context.funcMatPurchasePrice,
  87. "MatSupplier": context.funcMatSupplier,
  88. "GetMatTopType": context.funcGetMatTopType,
  89. "MatTypeName": context.funcMatTypeName,
  90. "MatTypeUnit": context.funcMatTypeUnit,
  91. "DataType": context.funcDataType,
  92. "debug": context.funcDebug,
  93. "CheckAuth": context.funcCheckAuth,
  94. "customer": context.funcCustomer,
  95. "phone": context.funcPhone,
  96. "address": context.funcAddress,
  97. "admin": context.funcAdmin,
  98. "GetItemTypeForName": context.funcGetItemTypeForName,
  99. "ItemTypeName": context.funcItemTypeName,
  100. "GetItemForType": context.funcGetItemForType,
  101. "GetSupplierForName": context.funcGetSupplierForName,
  102. "SupplierName": context.funcSupplierName,
  103. "now": context.funcNow,
  104. }
  105. }
  106. func (context *Context) funcJsonEncode(args ...interface{}) (interface{}, error) {
  107. if len(args) < 1 {
  108. return nil, errors.New("not enough arguments in call to call")
  109. }
  110. str, err := json.Marshal(args[0])
  111. if err != nil {
  112. return nil, err
  113. }
  114. return string(str), nil
  115. }
  116. func (context *Context) funcJsonDecode(args ...interface{}) (interface{}, error) {
  117. if len(args) < 1 {
  118. return nil, errors.New("not enough arguments in call to call")
  119. }
  120. var dest interface{}
  121. decoder := json.NewDecoder(strings.NewReader(string(db.ToString(args[0]))))
  122. decoder.UseNumber()
  123. err := decoder.Decode(&dest)
  124. return dest, err
  125. }
  126. func (context *Context) funcUse(args ...interface{}) (interface{}, error) {
  127. return "", nil
  128. }
  129. func (context *Context) funcSum(args ...interface{}) (interface{}, error) {
  130. var result float64
  131. argParse(args, func(arg interface{}) {
  132. ret, _ := db.ToFloat64(arg)
  133. result += ret
  134. })
  135. /*for _, arg := range args {
  136. ret, _ := db.ToFloat64(arg)
  137. result += ret
  138. }*/
  139. return result, nil
  140. }
  141. func (context *Context) funcLen(args ...interface{}) (interface{}, error) {
  142. return float64(len(args)), nil
  143. }
  144. func (context *Context) funcName(args ...interface{}) (interface{}, error) {
  145. names := make([]interface{}, 0)
  146. for _, arg := range args {
  147. if instance, ok := arg.(*InstanceData); ok {
  148. nameResult, err := context.LoadNameResult(instance)
  149. if err != nil {
  150. return nil, err
  151. }
  152. if nameResult == nil {
  153. names = append(names, nil)
  154. } else {
  155. names = append(names, filterValue(nameResult.Result))
  156. }
  157. } else {
  158. names = append(names, nil)
  159. }
  160. }
  161. if len(names) == 0 {
  162. nameResult, err := context.LoadNameResult(context.Current)
  163. if err != nil {
  164. return nil, err
  165. }
  166. if nameResult == nil {
  167. return nil, nil
  168. }
  169. return filterValue(nameResult.Result), nil
  170. }
  171. if len(names) == 1 {
  172. return names[0], nil
  173. }
  174. return names, nil
  175. }
  176. func (context *Context) funcShow(args ...interface{}) (interface{}, error) {
  177. shows := make([]interface{}, 0)
  178. for _, arg := range args {
  179. if instance, ok := arg.(*InstanceData); ok {
  180. context.LoadShowResult(instance)
  181. shows = append(shows, instance.GetShowResultBool())
  182. } else {
  183. shows = append(shows, nil)
  184. }
  185. }
  186. if len(shows) == 0 {
  187. context.LoadShowResult(context.Current)
  188. shows = append(shows, context.Current.GetShowResultBool())
  189. }
  190. if len(shows) == 1 {
  191. return shows[0], nil
  192. }
  193. return shows, nil
  194. }
  195. func (context *Context) funcId(args ...interface{}) (interface{}, error) {
  196. ids := make([]interface{}, 0)
  197. for _, arg := range args {
  198. if instance, ok := arg.(*InstanceData); ok {
  199. ids = append(ids, filterValue(instance.GetID()))
  200. } else {
  201. ids = append(ids, nil)
  202. }
  203. }
  204. if len(ids) == 0 {
  205. return filterValue(context.Current.GetID()), nil
  206. }
  207. if len(ids) == 1 {
  208. return ids[0], nil
  209. }
  210. return ids, nil
  211. }
  212. func (context *Context) funcIndex(args ...interface{}) (interface{}, error) {
  213. indexs := make([]interface{}, 0)
  214. for _, arg := range args {
  215. if instance, ok := arg.(*InstanceData); ok {
  216. indexs = append(indexs, filterValue(instance.Index))
  217. } else {
  218. indexs = append(indexs, 0)
  219. }
  220. }
  221. if len(indexs) == 0 {
  222. return filterValue(context.Current.Index), nil
  223. }
  224. if len(indexs) == 1 {
  225. return indexs[0], nil
  226. }
  227. return indexs, nil
  228. }
  229. func getSubIndex(instance *InstanceData) int {
  230. parent := instance.Parent
  231. if parent == nil {
  232. return 0
  233. }
  234. for n, i := range parent.GetSub() {
  235. if i.GetID() == instance.GetID() && i.Index == instance.Index {
  236. return n
  237. }
  238. }
  239. return 0
  240. }
  241. func (context *Context) funcSubIndex(args ...interface{}) (interface{}, error) {
  242. indexs := make([]interface{}, 0)
  243. for _, arg := range args {
  244. if instance, ok := arg.(*InstanceData); ok {
  245. indexs = append(indexs, filterValue(getSubIndex(instance)))
  246. } else {
  247. indexs = append(indexs, 0)
  248. }
  249. }
  250. if len(indexs) == 0 {
  251. return filterValue(getSubIndex(context.Current)), nil
  252. }
  253. if len(indexs) == 1 {
  254. return indexs[0], nil
  255. }
  256. return indexs, nil
  257. }
  258. func (context *Context) funcCount(args ...interface{}) (interface{}, error) {
  259. counts := make([]interface{}, 0)
  260. for _, arg := range args {
  261. if instance, ok := arg.(*InstanceData); ok {
  262. if instance.Object == nil {
  263. counts = append(counts, 1)
  264. } else {
  265. counts = append(counts, filterValue(len(instance.Object.Instances)))
  266. }
  267. } else {
  268. counts = append(counts, 0)
  269. }
  270. }
  271. if len(counts) == 0 {
  272. if context.Current.Object == nil {
  273. return filterValue(1), nil
  274. } else {
  275. return filterValue(len(context.Current.Object.Instances)), nil
  276. }
  277. }
  278. if len(counts) == 1 {
  279. return counts[0], nil
  280. }
  281. return counts, nil
  282. }
  283. func (context *Context) funcInstances(args ...interface{}) (interface{}, error) {
  284. names := make([]string, 0)
  285. instances := make([]*InstanceData, 0)
  286. argParse(args, func(arg interface{}) {
  287. if instance, ok := arg.(*InstanceData); ok {
  288. instances = append(instances, instance)
  289. } else {
  290. names = append(names, db.ToString(arg))
  291. }
  292. })
  293. /*for _, arg := range args {
  294. if ss, ok := arg.([]interface{}); ok {
  295. for _, s := range ss {
  296. if instance, ok := s.(Instance); ok {
  297. instances = append(instances, instance)
  298. } else if instance, ok := s.(*Instance); ok {
  299. instances = append(instances, *instance)
  300. } else {
  301. names = append(names, db.ToString(s))
  302. }
  303. }
  304. } else if instance, ok := arg.(Instance); ok {
  305. instances = append(instances, instance)
  306. } else if instance, ok := arg.(*Instance); ok {
  307. instances = append(instances, *instance)
  308. } else {
  309. names = append(names, db.ToString(arg))
  310. }
  311. }*/
  312. if len(instances) == 0 {
  313. instances = append(instances, context.Current)
  314. }
  315. resultss := make([]interface{}, 0)
  316. for _, instance := range instances {
  317. results := make([]interface{}, 0)
  318. if instance.Object == nil {
  319. if len(names) == 0 {
  320. results = append(results, instance)
  321. } else {
  322. for _, name := range names {
  323. if instance.NameResult != nil && db.ToString(instance.NameResult.Result) == name {
  324. results = append(results, instance)
  325. }
  326. }
  327. }
  328. } else {
  329. for i, _ := range instance.Object.Instances {
  330. if len(names) == 0 {
  331. err := context.LoadInstance(&instance.Object.Instances[i])
  332. if err != nil {
  333. return nil, err
  334. }
  335. results = append(results, &instance.Object.Instances[i])
  336. } else {
  337. _, err := context.LoadNameResult(&instance.Object.Instances[i])
  338. if err != nil {
  339. return nil, err
  340. }
  341. for _, name := range names {
  342. if instance.Object.Instances[i].NameResult != nil && db.ToString(instance.Object.Instances[i].NameResult.Result) == name {
  343. err = context.LoadInstance(&instance.Object.Instances[i])
  344. if err != nil {
  345. return nil, err
  346. }
  347. results = append(results, &instance.Object.Instances[i])
  348. }
  349. }
  350. }
  351. }
  352. }
  353. if len(results) == 0 {
  354. resultss = append(resultss, nil)
  355. } else if len(results) == 1 {
  356. resultss = append(resultss, results[0])
  357. } else {
  358. resultss = append(resultss, results)
  359. }
  360. }
  361. if len(resultss) == 0 {
  362. return nil, nil
  363. }
  364. if len(resultss) == 1 {
  365. return resultss[0], nil
  366. }
  367. return resultss, nil
  368. }
  369. func (context *Context) funcRound(args ...interface{}) (interface{}, error) {
  370. if len(args) < 1 {
  371. return nil, errors.New("not enough arguments in call to call")
  372. }
  373. p, _ := db.ToInt64(args[0])
  374. precision := math.Pow10(int(p))
  375. numbers := make([]interface{}, 0)
  376. for _, arg := range args[1:] {
  377. number, _ := db.ToFloat64(arg)
  378. numbers = append(numbers, filterValue(float64(math.Round(number*precision)/precision)))
  379. }
  380. if len(numbers) == 0 {
  381. return nil, nil
  382. }
  383. if len(numbers) == 1 {
  384. return numbers[0], nil
  385. }
  386. return numbers, nil
  387. }
  388. func (context *Context) funcCeil(args ...interface{}) (interface{}, error) {
  389. if len(args) < 1 {
  390. return nil, errors.New("not enough arguments in call to call")
  391. }
  392. p, _ := db.ToInt64(args[0])
  393. precision := math.Pow10(int(p))
  394. precision2 := math.Pow10(int(p + 2))
  395. numbers := make([]interface{}, 0)
  396. for _, arg := range args[1:] {
  397. number, _ := db.ToFloat64(arg)
  398. numbers = append(numbers, filterValue(float64(math.Ceil(math.Round(number*precision2)/100)/precision)))
  399. }
  400. if len(numbers) == 0 {
  401. return nil, nil
  402. }
  403. if len(numbers) == 1 {
  404. return numbers[0], nil
  405. }
  406. return numbers, nil
  407. }
  408. func (context *Context) funcCurrent(args ...interface{}) (interface{}, error) {
  409. return context.Current, nil
  410. }
  411. func (context *Context) funcQuote(args ...interface{}) (interface{}, error) {
  412. return context.Order.Form, nil
  413. }
  414. func (context *Context) funcParent(args ...interface{}) (interface{}, error) {
  415. parents := make([]interface{}, 0)
  416. for _, arg := range args {
  417. if instance, ok := arg.(*InstanceData); ok {
  418. parents = append(parents, instance.Parent)
  419. } else {
  420. parents = append(parents, nil)
  421. }
  422. }
  423. if len(parents) == 0 {
  424. return context.Current.Parent, nil
  425. }
  426. if len(parents) == 1 {
  427. return parents[0], nil
  428. }
  429. return parents, nil
  430. }
  431. func (context *Context) funcFilter(args ...interface{}) (interface{}, error) {
  432. expressions := make([]string, 0)
  433. instances := make([]*InstanceData, 0)
  434. argParse(args, func(arg interface{}) {
  435. if instance, ok := arg.(*InstanceData); ok {
  436. instances = append(instances, instance)
  437. } else {
  438. expressions = append(expressions, db.ToString(arg))
  439. }
  440. })
  441. /*for _, arg := range args {
  442. if ss, ok := arg.([]interface{}); ok {
  443. for _, s := range ss {
  444. if instance, ok := s.(Instance); ok {
  445. instances = append(instances, instance)
  446. } else if instance, ok := s.(*Instance); ok {
  447. instances = append(instances, *instance)
  448. } else {
  449. expressions = append(expressions, db.ToString(s))
  450. }
  451. }
  452. } else if instance, ok := arg.(Instance); ok {
  453. instances = append(instances, instance)
  454. } else if instance, ok := arg.(*Instance); ok {
  455. instances = append(instances, *instance)
  456. } else {
  457. expressions = append(expressions, db.ToString(arg))
  458. }
  459. }*/
  460. results := make([]interface{}, 0)
  461. for _, instance := range instances {
  462. ok := true
  463. for _, expression := range expressions {
  464. result, err := context.ParseValueinInstance(expression, instance)
  465. if err != nil {
  466. ok = false
  467. } else {
  468. ok = db.ToBool(result)
  469. }
  470. if !ok {
  471. break
  472. }
  473. }
  474. if ok {
  475. results = append(results, instance)
  476. }
  477. }
  478. if len(results) == 0 {
  479. return nil, nil
  480. }
  481. if len(results) == 1 {
  482. return results[0], nil
  483. }
  484. return results, nil
  485. }
  486. func (context *Context) findInstance(names []string, instance *InstanceData, typ int) ([]interface{}, error) {
  487. instances := make([]interface{}, 0)
  488. //logger.Sugar.Infof("instance.GetID(): %v %v", instance.GetID(), instance.Type())
  489. if typ == Base || instance.Type == typ {
  490. if len(names) == 0 {
  491. err := context.LoadInstance(instance)
  492. if err != nil {
  493. return nil, err
  494. }
  495. instances = append(instances, instance)
  496. } else {
  497. _, err := context.LoadNameResult(instance)
  498. if err != nil {
  499. return nil, err
  500. }
  501. for _, name := range names {
  502. nameResult := instance.NameResult
  503. if nameResult != nil && db.ToString(nameResult.Result) == name {
  504. err = context.LoadInstance(instance)
  505. if err != nil {
  506. return nil, err
  507. }
  508. instances = append(instances, instance)
  509. break
  510. }
  511. }
  512. }
  513. } else {
  514. err := context.LoadInstance(instance)
  515. if err != nil {
  516. return nil, err
  517. }
  518. for _, sub := range instance.GetSub() {
  519. ss, err := context.findInstance(names, sub, typ)
  520. if err != nil {
  521. return nil, err
  522. }
  523. instances = append(instances, ss...)
  524. }
  525. }
  526. return instances, nil
  527. }
  528. func (context *Context) findInstances(typ int, dinstance *InstanceData, args ...interface{}) (interface{}, error) {
  529. names := make([]string, 0)
  530. instances := make([]*InstanceData, 0)
  531. argParse(args, func(arg interface{}) {
  532. if arg != nil {
  533. if instance, ok := arg.(*InstanceData); ok {
  534. instances = append(instances, instance)
  535. } else {
  536. names = append(names, db.ToString(arg))
  537. }
  538. }
  539. })
  540. /*for _, arg := range args {
  541. if ss, ok := arg.([]interface{}); ok {
  542. for _, s := range ss {
  543. if instance, ok := s.(Instance); ok {
  544. instances = append(instances, instance)
  545. } else if instance, ok := s.(*Instance); ok {
  546. instances = append(instances, *instance)
  547. } else {
  548. names = append(names, db.ToString(s))
  549. }
  550. }
  551. } else if instance, ok := arg.(Instance); ok {
  552. instances = append(instances, instance)
  553. } else if instance, ok := arg.(*Instance); ok {
  554. instances = append(instances, *instance)
  555. } else {
  556. names = append(names, db.ToString(arg))
  557. }
  558. }*/
  559. if len(instances) == 0 {
  560. instances = append(instances, dinstance)
  561. }
  562. //logger.Sugar.Infof("instances: %v %v %v", instances, typ, names)
  563. subss := make([]interface{}, 0)
  564. for _, instance := range instances {
  565. var subs []interface{}
  566. if typ == Base {
  567. subs = make([]interface{}, 0)
  568. err := context.LoadInstance(instance)
  569. if err != nil {
  570. return nil, err
  571. }
  572. for _, sub := range instance.GetSub() {
  573. ss, err := context.findInstance(names, sub, typ)
  574. if err != nil {
  575. return nil, err
  576. }
  577. subs = append(subs, ss...)
  578. }
  579. } else {
  580. ss, err := context.findInstance(names, instance, typ)
  581. if err != nil {
  582. return nil, err
  583. }
  584. subs = ss
  585. }
  586. /*if len(subs) == 0 {
  587. subss = append(subss, nil)
  588. } else if len(subs) == 1 {
  589. subss = append(subss, subs[0])
  590. } else {
  591. subss = append(subss, subs)
  592. }*/
  593. subss = append(subss, subs...)
  594. }
  595. //logger.Sugar.Infof("instancesend: %v", instances)
  596. if len(subss) == 0 {
  597. return nil, nil
  598. }
  599. if len(subss) == 1 {
  600. return subss[0], nil
  601. }
  602. return subss, nil
  603. }
  604. func (context *Context) funcSub(args ...interface{}) (interface{}, error) {
  605. return context.findInstances(Base, context.Current, args...)
  606. }
  607. func (context *Context) funcRow(args ...interface{}) (interface{}, error) {
  608. return context.findInstances(Row, context.Order.Form, args...)
  609. }
  610. func (context *Context) funcGroup(args ...interface{}) (interface{}, error) {
  611. return context.findInstances(Group, context.Order.Form, args...)
  612. }
  613. func (context *Context) funcModule(args ...interface{}) (interface{}, error) {
  614. return context.findInstances(Module, context.Order.Form, args...)
  615. }
  616. func (context *Context) funcTable(args ...interface{}) (interface{}, error) {
  617. return context.findInstances(Table, context.Order.Form, args...)
  618. }
  619. func (context *Context) funcEval(args ...interface{}) (interface{}, error) {
  620. expressions := make([]string, 0)
  621. instances := make([]*InstanceData, 0)
  622. var globalProp map[string]interface{}
  623. argParse(args, func(arg interface{}) {
  624. if mp, ok := arg.(map[string]interface{}); ok {
  625. globalProp = mp
  626. } else if instance, ok := arg.(*InstanceData); ok {
  627. instances = append(instances, instance)
  628. } else {
  629. expressions = append(expressions, db.ToString(arg))
  630. }
  631. })
  632. /*for _, arg := range args {
  633. if ss, ok := arg.([]interface{}); ok {
  634. for _, s := range ss {
  635. if instance, ok := s.(Instance); ok {
  636. instances = append(instances, instance)
  637. } else if instance, ok := s.(*Instance); ok {
  638. instances = append(instances, *instance)
  639. } else {
  640. expressions = append(expressions, db.ToString(s))
  641. }
  642. }
  643. } else if mp, ok := arg.(map[string]interface{}); ok {
  644. globalProp = mp
  645. } else if instance, ok := arg.(Instance); ok {
  646. instances = append(instances, instance)
  647. } else if instance, ok := arg.(*Instance); ok {
  648. instances = append(instances, *instance)
  649. } else {
  650. expressions = append(expressions, db.ToString(arg))
  651. }
  652. }*/
  653. if len(instances) == 0 {
  654. instances = append(instances, context.Current)
  655. }
  656. resultss := make([]interface{}, 0)
  657. for _, instance := range instances {
  658. results := make([]interface{}, 0)
  659. for _, expression := range expressions {
  660. if globalProp == nil {
  661. result, err := context.ParseValueinInstance(expression, instance)
  662. if err != nil {
  663. results = append(results, nil)
  664. } else {
  665. results = append(results, filterValue(result))
  666. }
  667. } else {
  668. result, err := context.ParseValueInInstanceAndGlobalProp(expression, instance, globalProp)
  669. if err != nil {
  670. results = append(results, nil)
  671. } else {
  672. results = append(results, filterValue(result))
  673. }
  674. }
  675. }
  676. if len(results) == 0 {
  677. resultss = append(resultss, nil)
  678. } else if len(results) == 1 {
  679. resultss = append(resultss, results[0])
  680. } else {
  681. resultss = append(resultss, results)
  682. }
  683. }
  684. if len(resultss) == 0 {
  685. return nil, nil
  686. }
  687. if len(resultss) == 1 {
  688. return resultss[0], nil
  689. }
  690. return resultss, nil
  691. }
  692. func (context *Context) funcIsNull(args ...interface{}) (interface{}, error) {
  693. iss := make([]interface{}, 0)
  694. for _, arg := range args {
  695. iss = append(iss, arg == nil)
  696. }
  697. if len(iss) == 0 {
  698. return true, nil
  699. }
  700. if len(iss) == 1 {
  701. return iss[0], nil
  702. }
  703. return iss, nil
  704. }
  705. func (context *Context) funcArray(args ...interface{}) (interface{}, error) {
  706. if len(args) == 0 {
  707. return []interface{}{}, nil
  708. }
  709. return []interface{}{args}, nil
  710. }
  711. func (context *Context) funcInArray(args ...interface{}) (interface{}, error) {
  712. if len(args) < 2 {
  713. return nil, errors.New("not enough arguments in call to call")
  714. }
  715. index := db.ToString(args[0])
  716. if array, ok := args[1].([]interface{}); ok {
  717. for _, item := range array {
  718. if index == db.ToString(item) {
  719. return true, nil
  720. }
  721. }
  722. } else {
  723. return index == db.ToString(args[1]), nil
  724. }
  725. return false, nil
  726. }
  727. func (context *Context) funcStringIndex(args ...interface{}) (interface{}, error) {
  728. if len(args) < 2 {
  729. return nil, errors.New("not enough arguments in call to call")
  730. }
  731. s := db.ToString(args[0])
  732. sub := db.ToString(args[1])
  733. return filterValue(strings.Index(s, sub)), nil
  734. }
  735. func (context *Context) funcCompoundIndex(args ...interface{}) (interface{}, error) {
  736. if len(args) < 1 {
  737. return nil, errors.New("not enough arguments in call to call")
  738. }
  739. indexs := args[:len(args)-1]
  740. object := args[len(args)-1]
  741. values := make([]interface{}, 0)
  742. if array, ok := object.([]interface{}); ok {
  743. for _, index := range indexs {
  744. key, _ := db.ToInt64(index)
  745. if key < int64(len(array)) {
  746. values = append(values, filterValue(array[key]))
  747. } else {
  748. values = append(values, nil)
  749. }
  750. }
  751. }
  752. if mp, ok := object.(map[string]interface{}); ok {
  753. for _, index := range indexs {
  754. key := db.ToString(index)
  755. values = append(values, filterValue(mp[key]))
  756. }
  757. }
  758. if str, ok := object.(string); ok {
  759. for _, index := range indexs {
  760. key, _ := db.ToInt64(index)
  761. if key < int64(len(str)) {
  762. values = append(values, filterValue(string(str[key])))
  763. } else {
  764. values = append(values, nil)
  765. }
  766. }
  767. }
  768. if len(values) == 0 {
  769. return object, nil
  770. }
  771. if len(values) == 1 {
  772. return values[0], nil
  773. }
  774. return values, nil
  775. /*
  776. indexs := make([]interface{}, 0)
  777. objects := make([]interface{}, 0)
  778. for _, arg := range args {
  779. if array, ok := arg.([]interface{}); ok {
  780. objects = append(objects, array)
  781. } else if mp, ok := arg.(map[string]interface{}); ok {
  782. objects = append(objects, mp)
  783. } else {
  784. indexs = append(indexs, arg)
  785. }
  786. }
  787. resultss := make([]interface{}, 0)
  788. for _, object := range objects {
  789. results := make([]interface{}, 0)
  790. if array, ok := object.([]interface{}); ok {
  791. for _, index := range indexs {
  792. key, _ := db.ToInt64(index)
  793. if key < int64(len(array)) {
  794. results = append(values, array[key])
  795. } else {
  796. results = append(values, nil)
  797. }
  798. }
  799. } else if mp, ok := object.(map[string]interface{}); ok {
  800. for _, index := range indexs {
  801. key := db.ToString(index)
  802. results = append(values, mp[key])
  803. }
  804. }
  805. if len(results) == 0 {
  806. resultss = append(resultss, nil)
  807. } else if len(results) == 1 {
  808. resultss = append(resultss, results[0])
  809. } else {
  810. resultss = append(resultss, results)
  811. }
  812. }
  813. if len(resultss) == 0 {
  814. return nil, nil
  815. }
  816. if len(resultss) == 1 {
  817. return resultss[0], nil
  818. }
  819. return resultss, nil*/
  820. }
  821. func (context *Context) funcArrayMap(args ...interface{}) (interface{}, error) {
  822. expressions := make([]string, 0)
  823. objects := make([]interface{}, 0)
  824. for _, arg := range args {
  825. if array, ok := arg.([]interface{}); ok {
  826. objects = append(objects, array)
  827. } else if expression, ok := arg.(string); ok {
  828. expressions = append(expressions, expression)
  829. } else {
  830. objects = append(objects, []interface{}{arg})
  831. }
  832. }
  833. resultsss := make([]interface{}, 0)
  834. for _, object := range objects {
  835. resultss := make([]interface{}, 0)
  836. if array, ok := object.([]interface{}); ok {
  837. for k, v := range array {
  838. results := make([]interface{}, 0)
  839. globalProp := map[string]interface{}{
  840. "key": float64(k),
  841. "value": v,
  842. }
  843. for _, expression := range expressions {
  844. result, err := context.ParseValueinGlobalProp(expression, globalProp)
  845. if err != nil {
  846. results = append(results, nil)
  847. } else {
  848. results = append(results, filterValue(result))
  849. }
  850. }
  851. if len(results) == 0 {
  852. resultss = append(resultss, nil)
  853. } else if len(results) == 1 {
  854. resultss = append(resultss, results[0])
  855. } else {
  856. resultss = append(resultss, results)
  857. }
  858. }
  859. } else if mp, ok := object.(map[string]interface{}); ok {
  860. for k, v := range mp {
  861. results := make([]interface{}, 0)
  862. globalProp := map[string]interface{}{
  863. "key": k,
  864. "value": v,
  865. }
  866. for _, expression := range expressions {
  867. result, err := context.ParseValueinGlobalProp(expression, globalProp)
  868. if err != nil {
  869. results = append(results, nil)
  870. } else {
  871. results = append(results, filterValue(result))
  872. }
  873. }
  874. if len(results) == 0 {
  875. resultss = append(resultss, nil)
  876. } else if len(results) == 1 {
  877. resultss = append(resultss, results[0])
  878. } else {
  879. resultss = append(resultss, results)
  880. }
  881. }
  882. }
  883. if len(resultss) == 0 {
  884. resultsss = append(resultsss, nil)
  885. } else if len(resultss) == 1 {
  886. resultsss = append(resultsss, resultss[0])
  887. } else {
  888. resultsss = append(resultsss, resultss)
  889. }
  890. }
  891. if len(resultsss) == 0 {
  892. return nil, nil
  893. }
  894. if len(resultsss) == 1 {
  895. return resultsss[0], nil
  896. }
  897. return resultsss, nil
  898. }
  899. func (context *Context) funcCompoundMap(args ...interface{}) (interface{}, error) {
  900. expressions := make([]string, 0)
  901. objects := make([]interface{}, 0)
  902. for _, arg := range args {
  903. if array, ok := arg.([]interface{}); ok {
  904. objects = append(objects, array)
  905. } else if mp, ok := arg.(map[string]interface{}); ok {
  906. objects = append(objects, mp)
  907. } else {
  908. expressions = append(expressions, db.ToString(arg))
  909. }
  910. }
  911. resultsss := make([]interface{}, 0)
  912. for _, object := range objects {
  913. resultss := make([]interface{}, 0)
  914. if array, ok := object.([]interface{}); ok {
  915. for k, v := range array {
  916. results := make([]interface{}, 0)
  917. globalProp := map[string]interface{}{
  918. "key": float64(k),
  919. "value": v,
  920. }
  921. for _, expression := range expressions {
  922. result, err := context.ParseValueinGlobalProp(expression, globalProp)
  923. if err != nil {
  924. results = append(results, nil)
  925. } else {
  926. results = append(results, filterValue(result))
  927. }
  928. }
  929. if len(results) == 0 {
  930. resultss = append(resultss, nil)
  931. } else if len(results) == 1 {
  932. resultss = append(resultss, results[0])
  933. } else {
  934. resultss = append(resultss, results)
  935. }
  936. }
  937. } else if mp, ok := object.(map[string]interface{}); ok {
  938. for k, v := range mp {
  939. results := make([]interface{}, 0)
  940. globalProp := map[string]interface{}{
  941. "key": k,
  942. "value": v,
  943. }
  944. for _, expression := range expressions {
  945. result, err := context.ParseValueinGlobalProp(expression, globalProp)
  946. if err != nil {
  947. results = append(results, nil)
  948. } else {
  949. results = append(results, filterValue(result))
  950. }
  951. }
  952. if len(results) == 0 {
  953. resultss = append(resultss, nil)
  954. } else if len(results) == 1 {
  955. resultss = append(resultss, results[0])
  956. } else {
  957. resultss = append(resultss, results)
  958. }
  959. }
  960. }
  961. if len(resultss) == 0 {
  962. resultsss = append(resultsss, nil)
  963. } else if len(resultss) == 1 {
  964. resultsss = append(resultsss, resultss[0])
  965. } else {
  966. resultsss = append(resultsss, resultss)
  967. }
  968. }
  969. if len(resultsss) == 0 {
  970. return nil, nil
  971. }
  972. if len(resultsss) == 1 {
  973. return resultsss[0], nil
  974. }
  975. return resultsss, nil
  976. }
  977. func (context *Context) funcArrayFilter(args ...interface{}) (interface{}, error) {
  978. array := make([]interface{}, 0)
  979. for _, arg := range args {
  980. if db.ToBool(arg) {
  981. array = append(array, filterValue(arg))
  982. }
  983. }
  984. return array, nil
  985. }
  986. func (context *Context) funcArrayDedup(args ...interface{}) (interface{}, error) {
  987. tmp := make(map[string]int)
  988. array := make([]interface{}, 0)
  989. for _, arg := range args {
  990. s := db.ToString(arg)
  991. if _, ok := tmp[s]; !ok {
  992. array = append(array, filterValue(arg))
  993. tmp[s] = 1
  994. }
  995. }
  996. return array, nil
  997. }
  998. func (context *Context) funcArraySplice(args ...interface{}) (interface{}, error) {
  999. if len(args) < 2 {
  1000. return nil, errors.New("not enough arguments in call to call")
  1001. }
  1002. index, _ := db.ToInt64(args[0])
  1003. array := []interface{}{}
  1004. if a, ok := args[1].([]interface{}); ok {
  1005. array = a
  1006. } else {
  1007. array = append(array, args[1])
  1008. }
  1009. n := int64(len(array)) - index
  1010. if len(args) > 2 {
  1011. n, _ = db.ToInt64(args[2])
  1012. }
  1013. if index < int64(len(array)) && index >= 0 {
  1014. before := array[:index]
  1015. after := []interface{}{}
  1016. if n <= 0 {
  1017. after = append(after, array[index:]...)
  1018. } else if index+n < int64(len(array)) {
  1019. after = append(after, array[index+n:]...)
  1020. }
  1021. array = before
  1022. for n := 3; n < len(args); n++ {
  1023. array = append(array, args[n])
  1024. }
  1025. array = append(array, after...)
  1026. }
  1027. return array, nil
  1028. }
  1029. func (context *Context) funcArrayMerge(args ...interface{}) (interface{}, error) {
  1030. array := make([]interface{}, 0)
  1031. for _, arg := range args {
  1032. if s, ok := arg.([]interface{}); ok {
  1033. array = append(array, s...)
  1034. } else {
  1035. array = append(array, arg)
  1036. }
  1037. }
  1038. return array, nil
  1039. }
  1040. func (context *Context) funcMap(args ...interface{}) (interface{}, error) {
  1041. m := make(map[string]interface{})
  1042. for i := 0; i < len(args)-1; i += 2 {
  1043. m[db.ToString(args[i])] = filterValue(args[i+1])
  1044. }
  1045. return m, nil
  1046. }
  1047. func (context *Context) funcMapKeys(args ...interface{}) (interface{}, error) {
  1048. if len(args) < 1 {
  1049. return nil, errors.New("not enough arguments in call to call")
  1050. }
  1051. keys := make([]interface{}, 0)
  1052. if mp, ok := args[0].(map[string]interface{}); ok {
  1053. for key, _ := range mp {
  1054. keys = append(keys, key)
  1055. }
  1056. }
  1057. return keys, nil
  1058. }
  1059. func (context *Context) funcMapValues(args ...interface{}) (interface{}, error) {
  1060. if len(args) < 1 {
  1061. return nil, errors.New("not enough arguments in call to call")
  1062. }
  1063. values := make([]interface{}, 0)
  1064. if mp, ok := args[0].(map[string]interface{}); ok {
  1065. for _, value := range mp {
  1066. values = append(values, filterValue(value))
  1067. }
  1068. }
  1069. return values, nil
  1070. }
  1071. func (context *Context) funcMapMerge(args ...interface{}) (interface{}, error) {
  1072. mp := make(map[string]interface{})
  1073. for _, arg := range args {
  1074. if m, ok := arg.(map[string]interface{}); ok {
  1075. for k, v := range m {
  1076. mp[k] = filterValue(v)
  1077. }
  1078. }
  1079. }
  1080. return mp, nil
  1081. }
  1082. func (context *Context) funcSetProp(args ...interface{}) (interface{}, error) {
  1083. if len(args) < 2 {
  1084. return nil, errors.New("not enough arguments in call to call")
  1085. }
  1086. name := db.ToString(args[0])
  1087. prop, err := context.GetProp(name)
  1088. if prop == nil {
  1089. return nil, err
  1090. }
  1091. prop.Result = args[1]
  1092. context.ModifyAfter(prop.Key)
  1093. return prop.Result, nil
  1094. }
  1095. func (context *Context) funcControl(args ...interface{}) (interface{}, error) {
  1096. if len(args) < 1 {
  1097. return nil, errors.New("not enough arguments in call to call")
  1098. }
  1099. var control Control
  1100. control.PraseForArgs(args...)
  1101. return control, nil
  1102. }
  1103. func (context *Context) funcControlInput(args ...interface{}) (interface{}, error) {
  1104. var control Control
  1105. control.PraseForArgs(append([]interface{}{"input"}, args...)...)
  1106. return control, nil
  1107. }
  1108. func (context *Context) funcControlNumber(args ...interface{}) (interface{}, error) {
  1109. var control Control
  1110. control.PraseForArgs(append([]interface{}{"number"}, args...)...)
  1111. return control, nil
  1112. }
  1113. func (context *Context) funcControlCheckbox(args ...interface{}) (interface{}, error) {
  1114. var value interface{}
  1115. if len(args) > 0 {
  1116. value = args[0]
  1117. }
  1118. var param map[string]interface{}
  1119. if len(args) > 2 {
  1120. if p, ok := args[2].(map[string]interface{}); ok {
  1121. param = p
  1122. } else {
  1123. param = make(map[string]interface{})
  1124. }
  1125. } else {
  1126. param = make(map[string]interface{})
  1127. }
  1128. if len(args) > 1 {
  1129. param["label"] = args[1]
  1130. }
  1131. if len(args) > 3 {
  1132. args = args[3:]
  1133. } else {
  1134. args = make([]interface{}, 0)
  1135. }
  1136. var control Control
  1137. control.PraseForArgs(append([]interface{}{"checkbox", value, param}, args...)...)
  1138. return control, nil
  1139. }
  1140. func (context *Context) funcControlText(args ...interface{}) (interface{}, error) {
  1141. var control Control
  1142. var value interface{}
  1143. if len(args) > 1 {
  1144. value = args[1]
  1145. }
  1146. var param map[string]interface{}
  1147. if len(args) > 2 {
  1148. if p, ok := args[2].(map[string]interface{}); ok {
  1149. param = p
  1150. } else {
  1151. param = make(map[string]interface{})
  1152. }
  1153. } else {
  1154. param = make(map[string]interface{})
  1155. }
  1156. if len(args) > 0 {
  1157. param["label"] = args[0]
  1158. }
  1159. var event map[string]string
  1160. if len(args) > 3 {
  1161. if e, ok := args[3].(map[string]string); ok {
  1162. event = e
  1163. } else {
  1164. event = make(map[string]string)
  1165. if e, ok := args[3].(map[string]interface{}); ok {
  1166. for key, val := range e {
  1167. event[key] = db.ToString(val)
  1168. }
  1169. }
  1170. }
  1171. } else {
  1172. event = make(map[string]string)
  1173. }
  1174. last := false
  1175. if len(args) > 4 {
  1176. if p, ok := args[4].(bool); ok {
  1177. last = p
  1178. }
  1179. }
  1180. control.PraseForArgs("text", value, param, event, last)
  1181. return control, nil
  1182. }
  1183. func (context *Context) funcControlSelect(args ...interface{}) (interface{}, error) {
  1184. var value interface{}
  1185. if len(args) > 1 {
  1186. value = args[1]
  1187. }
  1188. var param map[string]interface{}
  1189. if len(args) > 2 {
  1190. if p, ok := args[2].(map[string]interface{}); ok {
  1191. param = p
  1192. } else {
  1193. param = make(map[string]interface{})
  1194. }
  1195. } else {
  1196. param = make(map[string]interface{})
  1197. }
  1198. if len(args) > 0 {
  1199. if p, ok := args[0].([]interface{}); ok {
  1200. param["options"] = p
  1201. } else {
  1202. param["options"] = make([]interface{}, 0)
  1203. }
  1204. }
  1205. if len(args) > 3 {
  1206. args = args[3:]
  1207. } else {
  1208. args = make([]interface{}, 0)
  1209. }
  1210. var control Control
  1211. control.PraseForArgs(append([]interface{}{"select", value, param}, args...)...)
  1212. return control, nil
  1213. }
  1214. func (context *Context) funcControlGroup(args ...interface{}) (interface{}, error) {
  1215. controls := make([]Control, 0)
  1216. values := make([]interface{}, 0)
  1217. for _, arg := range args {
  1218. var control Control
  1219. if c, ok := arg.(Control); ok {
  1220. control = c
  1221. } else if c, ok := arg.(*Control); ok {
  1222. control = *c
  1223. } else if c, ok := arg.(map[string]interface{}); ok {
  1224. control.PraseForMap(c)
  1225. }
  1226. controls = append(controls, control)
  1227. values = append(values, control.Value)
  1228. }
  1229. var control Control
  1230. control.Type = "group"
  1231. control.Value = values
  1232. control.Param = map[string]interface{}{
  1233. "controls": controls,
  1234. }
  1235. control.Event = make(map[string]string)
  1236. control.Last = true
  1237. return control, nil
  1238. }
  1239. func (context *Context) funcControlPopover(args ...interface{}) (interface{}, error) {
  1240. var param map[string]interface{}
  1241. if len(args) > 1 {
  1242. if p, ok := args[1].(map[string]interface{}); ok {
  1243. param = p
  1244. } else {
  1245. param = make(map[string]interface{})
  1246. }
  1247. } else {
  1248. param = make(map[string]interface{})
  1249. }
  1250. if len(args) > 0 {
  1251. param["label"] = args[0]
  1252. }
  1253. if len(args) > 2 {
  1254. args = args[2:]
  1255. } else {
  1256. args = make([]interface{}, 0)
  1257. }
  1258. controls := make([]Control, 0)
  1259. values := make([]interface{}, 0)
  1260. last := true
  1261. for _, arg := range args {
  1262. if control, ok := arg.(Control); ok {
  1263. controls = append(controls, control)
  1264. values = append(values, control.Value)
  1265. } else if control, ok := arg.(*Control); ok {
  1266. controls = append(controls, *control)
  1267. values = append(values, control.Value)
  1268. } else if c, ok := arg.(map[string]interface{}); ok {
  1269. var control Control
  1270. control.PraseForMap(c)
  1271. controls = append(controls, control)
  1272. values = append(values, control.Value)
  1273. } else if c, ok := arg.(bool); ok {
  1274. last = c
  1275. }
  1276. }
  1277. param["controls"] = controls
  1278. var control Control
  1279. control.Type = "popover"
  1280. control.Value = values
  1281. control.Param = param
  1282. control.Event = make(map[string]string)
  1283. control.Last = last
  1284. return control, nil
  1285. }
  1286. func (context *Context) funcControlSelectMat(args ...interface{}) (interface{}, error) {
  1287. var control Control
  1288. var value interface{}
  1289. if len(args) > 1 {
  1290. value = args[1]
  1291. }
  1292. var param map[string]interface{}
  1293. if len(args) > 2 {
  1294. if p, ok := args[2].(map[string]interface{}); ok {
  1295. param = p
  1296. } else {
  1297. param = make(map[string]interface{})
  1298. }
  1299. } else {
  1300. param = make(map[string]interface{})
  1301. }
  1302. if len(args) > 0 {
  1303. if s, ok := args[0].([]interface{}); ok {
  1304. param["type"] = s
  1305. } else {
  1306. id, _ := db.ToInt64(args[0])
  1307. param["type"] = []interface{}{id}
  1308. }
  1309. }
  1310. if len(args) > 3 {
  1311. args = args[3:]
  1312. } else {
  1313. args = make([]interface{}, 0)
  1314. }
  1315. control.PraseForArgs(append([]interface{}{"selectmat", value, param}, args...)...)
  1316. return control, nil
  1317. }
  1318. func (context *Context) funcControlSelectItem(args ...interface{}) (interface{}, error) {
  1319. var control Control
  1320. var value interface{}
  1321. if len(args) > 1 {
  1322. value = args[1]
  1323. }
  1324. var param map[string]interface{}
  1325. if len(args) > 2 {
  1326. if p, ok := args[2].(map[string]interface{}); ok {
  1327. param = p
  1328. } else {
  1329. param = make(map[string]interface{})
  1330. }
  1331. } else {
  1332. param = make(map[string]interface{})
  1333. }
  1334. if len(args) > 0 {
  1335. if s, ok := args[0].([]interface{}); ok {
  1336. param["type"] = s
  1337. } else {
  1338. id, _ := db.ToInt64(args[0])
  1339. param["type"] = []interface{}{id}
  1340. }
  1341. }
  1342. if len(args) > 3 {
  1343. args = args[3:]
  1344. } else {
  1345. args = make([]interface{}, 0)
  1346. }
  1347. control.PraseForArgs(append([]interface{}{"selectitem", value, param}, args...)...)
  1348. return control, nil
  1349. }
  1350. func (context *Context) funcControlButton(args ...interface{}) (interface{}, error) {
  1351. var param map[string]interface{}
  1352. if len(args) > 2 {
  1353. if p, ok := args[2].(map[string]interface{}); ok {
  1354. param = p
  1355. } else {
  1356. param = make(map[string]interface{})
  1357. }
  1358. } else {
  1359. param = make(map[string]interface{})
  1360. }
  1361. var event map[string]string
  1362. if len(args) > 3 {
  1363. if e, ok := args[3].(map[string]string); ok {
  1364. event = e
  1365. } else {
  1366. event = make(map[string]string)
  1367. if event, ok := args[3].(map[string]interface{}); ok {
  1368. for key, val := range event {
  1369. event[key] = db.ToString(val)
  1370. }
  1371. }
  1372. }
  1373. } else {
  1374. event = make(map[string]string)
  1375. }
  1376. if len(args) > 0 {
  1377. param["label"] = args[0]
  1378. }
  1379. if len(args) > 1 {
  1380. event["click"] = db.ToString(args[1])
  1381. }
  1382. if len(args) > 4 {
  1383. args = args[4:]
  1384. } else {
  1385. args = make([]interface{}, 0)
  1386. }
  1387. var control Control
  1388. control.PraseForArgs(append([]interface{}{"button", nil, param, event}, args...)...)
  1389. return control, nil
  1390. }
  1391. func (context *Context) funcGetMatTypeForName(args ...interface{}) (interface{}, error) {
  1392. context.MatTypeInit()
  1393. pids := make([]interface{}, 0)
  1394. for _, arg := range args {
  1395. typs := strings.Split(db.ToString(arg), "/")
  1396. pid := int64(0)
  1397. for _, name := range typs {
  1398. if name == "" {
  1399. continue
  1400. }
  1401. if nameCache, ok := context.MatTypePidNameCache[pid]; ok {
  1402. if typ, ok := nameCache[name]; ok {
  1403. pid = typ.ID
  1404. }
  1405. }
  1406. }
  1407. if pid == 0 {
  1408. pids = append(pids, nil)
  1409. } else {
  1410. pids = append(pids, filterValue(pid))
  1411. }
  1412. }
  1413. if len(pids) == 0 {
  1414. return nil, nil
  1415. }
  1416. if len(pids) == 1 {
  1417. return pids[0], nil
  1418. }
  1419. return pids, nil
  1420. }
  1421. func (context *Context) funcMatName(args ...interface{}) (interface{}, error) {
  1422. names := make([]interface{}, 0)
  1423. for _, arg := range args {
  1424. id, _ := db.ToInt64(arg)
  1425. mat := context.GetMat(id)
  1426. if mat != nil {
  1427. name := mat.Brand + " " + mat.Series + " " + mat.Model + " " + mat.Specs + " " + mat.Color
  1428. names = append(names, name)
  1429. } else {
  1430. names = append(names, nil)
  1431. }
  1432. }
  1433. if len(names) == 0 {
  1434. return nil, nil
  1435. }
  1436. if len(names) == 1 {
  1437. return names[0], nil
  1438. }
  1439. return names, nil
  1440. }
  1441. func (context *Context) funcMatType(args ...interface{}) (interface{}, error) {
  1442. types := make([]interface{}, 0)
  1443. for _, arg := range args {
  1444. id, _ := db.ToInt64(arg)
  1445. mat := context.GetMat(id)
  1446. if mat != nil {
  1447. types = append(types, filterValue(mat.TypeId))
  1448. } else {
  1449. types = append(types, nil)
  1450. }
  1451. }
  1452. if len(types) == 0 {
  1453. return nil, nil
  1454. }
  1455. if len(types) == 1 {
  1456. return types[0], nil
  1457. }
  1458. return types, nil
  1459. }
  1460. func (context *Context) funcMatPrice(args ...interface{}) (interface{}, error) {
  1461. prices := make([]interface{}, 0)
  1462. for _, arg := range args {
  1463. id, _ := db.ToInt64(arg)
  1464. mat := context.GetMat(id)
  1465. if mat != nil {
  1466. prices = append(prices, filterValue(mat.Price))
  1467. } else {
  1468. prices = append(prices, nil)
  1469. }
  1470. }
  1471. if len(prices) == 0 {
  1472. return nil, nil
  1473. }
  1474. if len(prices) == 1 {
  1475. return prices[0], nil
  1476. }
  1477. return prices, nil
  1478. }
  1479. func (context *Context) funcMatControlPrice(args ...interface{}) (interface{}, error) {
  1480. prices := make([]interface{}, 0)
  1481. for _, arg := range args {
  1482. id, _ := db.ToInt64(arg)
  1483. mat := context.GetMat(id)
  1484. if mat != nil {
  1485. prices = append(prices, filterValue(final.ControlPrice(mat.ControlPrice, mat.Price)))
  1486. } else {
  1487. prices = append(prices, nil)
  1488. }
  1489. }
  1490. if len(prices) == 0 {
  1491. return nil, nil
  1492. }
  1493. if len(prices) == 1 {
  1494. return prices[0], nil
  1495. }
  1496. return prices, nil
  1497. }
  1498. func (context *Context) funcMatSalesPrice(args ...interface{}) (interface{}, error) {
  1499. prices := make([]interface{}, 0)
  1500. for _, arg := range args {
  1501. id, _ := db.ToInt64(arg)
  1502. mat := context.GetMat(id)
  1503. if mat != nil {
  1504. prices = append(prices, filterValue(final.SalesPrice(mat.SalesPrice, mat.Price)))
  1505. } else {
  1506. prices = append(prices, nil)
  1507. }
  1508. }
  1509. if len(prices) == 0 {
  1510. return nil, nil
  1511. }
  1512. if len(prices) == 1 {
  1513. return prices[0], nil
  1514. }
  1515. return prices, nil
  1516. }
  1517. func (context *Context) funcMatPurchasePrice(args ...interface{}) (interface{}, error) {
  1518. prices := make([]interface{}, 0)
  1519. for _, arg := range args {
  1520. id, _ := db.ToInt64(arg)
  1521. mat := context.GetMat(id)
  1522. if mat != nil {
  1523. prices = append(prices, filterValue(final.PurchasePrice(mat.PurchasePrice, mat.Price)))
  1524. } else {
  1525. prices = append(prices, nil)
  1526. }
  1527. }
  1528. if len(prices) == 0 {
  1529. return nil, nil
  1530. }
  1531. if len(prices) == 1 {
  1532. return prices[0], nil
  1533. }
  1534. return prices, nil
  1535. }
  1536. func (context *Context) funcMatSupplier(args ...interface{}) (interface{}, error) {
  1537. suppliers := make([]interface{}, 0)
  1538. for _, arg := range args {
  1539. id, _ := db.ToInt64(arg)
  1540. mat := context.GetMat(id)
  1541. if mat != nil {
  1542. suppliers = append(suppliers, filterValue(mat.SupplierId))
  1543. } else {
  1544. suppliers = append(suppliers, nil)
  1545. }
  1546. }
  1547. if len(suppliers) == 0 {
  1548. return nil, nil
  1549. }
  1550. if len(suppliers) == 1 {
  1551. return suppliers[0], nil
  1552. }
  1553. return suppliers, nil
  1554. }
  1555. func (context *Context) funcGetMatTopType(args ...interface{}) (interface{}, error) {
  1556. context.MatTypeInit()
  1557. types := make([]interface{}, 0)
  1558. for _, arg := range args {
  1559. id, _ := db.ToInt64(arg)
  1560. for {
  1561. if typ, ok := context.MatTypeCache[id]; ok {
  1562. if typ.Pid != 0 {
  1563. id = typ.Pid
  1564. } else {
  1565. break
  1566. }
  1567. } else {
  1568. break
  1569. }
  1570. }
  1571. types = append(types, filterValue(id))
  1572. }
  1573. if len(types) == 0 {
  1574. return nil, nil
  1575. }
  1576. if len(types) == 1 {
  1577. return types[0], nil
  1578. }
  1579. return types, nil
  1580. }
  1581. func (context *Context) funcMatTypeName(args ...interface{}) (interface{}, error) {
  1582. context.MatTypeInit()
  1583. names := make([]interface{}, 0)
  1584. for _, arg := range args {
  1585. id, _ := db.ToInt64(arg)
  1586. if typ, ok := context.MatTypeCache[id]; ok {
  1587. names = append(names, typ.Name)
  1588. } else {
  1589. names = append(names, nil)
  1590. }
  1591. }
  1592. if len(names) == 0 {
  1593. return nil, nil
  1594. }
  1595. if len(names) == 1 {
  1596. return names[0], nil
  1597. }
  1598. return names, nil
  1599. }
  1600. func (context *Context) funcMatTypeUnit(args ...interface{}) (interface{}, error) {
  1601. context.MatTypeInit()
  1602. types := make([]interface{}, 0)
  1603. for _, arg := range args {
  1604. id, _ := db.ToInt64(arg)
  1605. if typ, ok := context.MatTypeCache[id]; ok {
  1606. types = append(types, typ.Unit)
  1607. } else {
  1608. types = append(types, "")
  1609. }
  1610. }
  1611. if len(types) == 0 {
  1612. return nil, nil
  1613. }
  1614. if len(types) == 1 {
  1615. return types[0], nil
  1616. }
  1617. return types, nil
  1618. }
  1619. func (context *Context) funcDataType(args ...interface{}) (interface{}, error) {
  1620. types := make([]interface{}, 0)
  1621. for _, arg := range args {
  1622. typ := reflect.TypeOf(arg)
  1623. if typ == nil {
  1624. types = append(types, nil)
  1625. } else {
  1626. types = append(types, typ.String())
  1627. }
  1628. }
  1629. if len(types) == 0 {
  1630. return nil, nil
  1631. }
  1632. if len(types) == 1 {
  1633. return types[0], nil
  1634. }
  1635. return types, nil
  1636. }
  1637. func (context *Context) funcDebug(args ...interface{}) (interface{}, error) {
  1638. logger.Sugar.Infof("Debug %v: %v arg", context.Calls[len(context.Calls)-1].Expression, len(args))
  1639. for n, arg := range args {
  1640. typ := reflect.TypeOf(arg)
  1641. typs := ""
  1642. if typ == nil {
  1643. } else {
  1644. typs = typ.String()
  1645. }
  1646. logger.Sugar.Infof("arg[%v]: %+v %v", n, arg, typs)
  1647. }
  1648. if len(args) == 1 {
  1649. return args[0], nil
  1650. }
  1651. return args, nil
  1652. }
  1653. func (context *Context) funcCheckAuth(args ...interface{}) (interface{}, error) {
  1654. auths := make([]string, 0)
  1655. argParse(args, func(arg interface{}) {
  1656. auths = append(auths, db.ToString(arg))
  1657. })
  1658. return admin.CheckAuth(auths, context.C.GetInt("adminID")), nil
  1659. }
  1660. func (context *Context) funcCustomer(args ...interface{}) (interface{}, error) {
  1661. customer := context.Customer()
  1662. if customer != nil {
  1663. return customer.Username, nil
  1664. }
  1665. return "", nil
  1666. }
  1667. func (context *Context) funcPhone(args ...interface{}) (interface{}, error) {
  1668. customer := context.Customer()
  1669. if customer != nil {
  1670. return customer.Phone, nil
  1671. }
  1672. return "", nil
  1673. }
  1674. func (context *Context) funcAddress(args ...interface{}) (interface{}, error) {
  1675. customer := context.Customer()
  1676. if customer != nil {
  1677. return customer.Address + " " + customer.Village + " " + customer.RoomNo, nil
  1678. }
  1679. return "", nil
  1680. }
  1681. func (context *Context) funcAdmin(args ...interface{}) (interface{}, error) {
  1682. admin := context.Admin()
  1683. if admin != nil {
  1684. return admin.Username, nil
  1685. }
  1686. return "", nil
  1687. }
  1688. func (context *Context) funcGetItemTypeForName(args ...interface{}) (interface{}, error) {
  1689. context.ItemTypeInit()
  1690. pids := make([]interface{}, 0)
  1691. for _, arg := range args {
  1692. typs := strings.Split(db.ToString(arg), "/")
  1693. pid := int64(0)
  1694. for _, name := range typs {
  1695. if name == "" {
  1696. continue
  1697. }
  1698. if nameCache, ok := context.ItemTypePidNameCache[pid]; ok {
  1699. if typ, ok := nameCache[name]; ok {
  1700. pid = typ.ID
  1701. }
  1702. }
  1703. }
  1704. if pid == 0 {
  1705. pids = append(pids, nil)
  1706. } else {
  1707. pids = append(pids, filterValue(pid))
  1708. }
  1709. }
  1710. if len(pids) == 0 {
  1711. return nil, nil
  1712. }
  1713. if len(pids) == 1 {
  1714. return pids[0], nil
  1715. }
  1716. return pids, nil
  1717. }
  1718. func (context *Context) funcItemTypeName(args ...interface{}) (interface{}, error) {
  1719. context.ItemTypeInit()
  1720. names := make([]interface{}, 0)
  1721. for _, arg := range args {
  1722. id, _ := db.ToInt64(arg)
  1723. if typ, ok := context.ItemTypeCache[id]; ok {
  1724. names = append(names, typ.Name)
  1725. } else {
  1726. names = append(names, nil)
  1727. }
  1728. }
  1729. if len(names) == 0 {
  1730. return nil, nil
  1731. }
  1732. if len(names) == 1 {
  1733. return names[0], nil
  1734. }
  1735. return names, nil
  1736. }
  1737. func (context *Context) funcGetItemForType(args ...interface{}) (interface{}, error) {
  1738. ids := make([]interface{}, 0)
  1739. for _, arg := range args {
  1740. typid, _ := db.ToInt64(arg)
  1741. tids := budget.AllItemTypes([]int64{typid})
  1742. items, _ := budget.GetItemModels(map[string]interface{}{
  1743. "typeId in": tids,
  1744. "deleted_at": 0,
  1745. })
  1746. for _, item := range items {
  1747. if _, ok := context.Order.Models[Item][item.ID]; !ok {
  1748. context.Order.Models[Item][item.ID] = item
  1749. }
  1750. ids = append(ids, filterValue(item.ID))
  1751. }
  1752. }
  1753. if len(ids) == 1 {
  1754. return ids[0], nil
  1755. }
  1756. return ids, nil
  1757. }
  1758. func (context *Context) funcGetSupplierForName(args ...interface{}) (interface{}, error) {
  1759. context.SupplierInit()
  1760. ids := make([]interface{}, 0)
  1761. for _, arg := range args {
  1762. name := db.ToString(arg)
  1763. if supplier, ok := context.SupplierNameCache[name]; ok {
  1764. ids = append(ids, filterValue(supplier.ID))
  1765. } else {
  1766. ids = append(ids, nil)
  1767. }
  1768. }
  1769. if len(ids) == 0 {
  1770. return nil, nil
  1771. }
  1772. if len(ids) == 1 {
  1773. return ids[0], nil
  1774. }
  1775. return ids, nil
  1776. }
  1777. func (context *Context) funcSupplierName(args ...interface{}) (interface{}, error) {
  1778. context.SupplierInit()
  1779. names := make([]interface{}, 0)
  1780. for _, arg := range args {
  1781. id, _ := db.ToInt64(arg)
  1782. if supplier, ok := context.SupplierCache[id]; ok {
  1783. names = append(names, supplier.Name)
  1784. } else {
  1785. names = append(names, nil)
  1786. }
  1787. }
  1788. if len(names) == 0 {
  1789. return nil, nil
  1790. }
  1791. if len(names) == 1 {
  1792. return names[0], nil
  1793. }
  1794. return names, nil
  1795. }
  1796. func (context *Context) funcNow(args ...interface{}) (interface{}, error) {
  1797. return float64(time.Now().Unix()), nil
  1798. }