ConfigBase.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. cc.Class({
  2. name: 'ConfigBase',
  3. properties: {
  4. mainKey: "",
  5. indexsCache:null,
  6. multiple:[],
  7. multipleCache:null,
  8. table:null,
  9. tableName:"",
  10. isLoadFinish:false,
  11. },
  12. ctor() {
  13. },
  14. initTable(tableName, mainKey, multipleKey) {
  15. this.tableName = tableName;
  16. this.mainKey = mainKey;
  17. this.multiple = multipleKey;
  18. this.indexsCache = {};
  19. this.multipleCache = {};
  20. return this
  21. },
  22. setTable(table) {
  23. this.table = table
  24. },
  25. getConfigAll() {
  26. return this.table;
  27. },
  28. // 从主键中拿某一行数据
  29. getByMainKey(idx) {
  30. // 从缓存中拿数据
  31. let data = this.indexsCache[idx]
  32. if (data != undefined) {
  33. return data;
  34. }
  35. // 找数据
  36. for (let d of this.table) {
  37. if (d[this.mainKey] == idx) {
  38. data = d;
  39. break;
  40. }
  41. }
  42. // 缓存数据
  43. if (data) {
  44. this.indexsCache[data[this.mainKey]] = data;
  45. }
  46. return data;
  47. },
  48. // 从多主键中,拿数据
  49. getByMultipleKey() {
  50. // 参数跟多主键数量对应不上
  51. if (arguments.length != this.multiple.length) {
  52. if (arguments.length <= 0) {
  53. cc.log("multiple key is null");
  54. } else {
  55. cc.log("multiple key num not match curNum :" + arguments.length + " need num " + this.multiple.length);
  56. }
  57. return null;
  58. }
  59. // 从缓存中拿数据
  60. let curCache = this.findMultipleCache(arguments);
  61. if (curCache != null)
  62. {
  63. return curCache;
  64. }
  65. // 查找数据
  66. return this.getAndAddMultiple(arguments);
  67. },
  68. // 拿多主键数据并缓存
  69. getAndAddMultiple(args)
  70. {
  71. var result = [];
  72. // 结果拿出来
  73. for (let d of this.table)
  74. {
  75. let isMatch = true;
  76. for (let i = 0; i < this.multiple.length; i++) {
  77. const k = this.multiple[i];
  78. if (d[k] != args[i]) {
  79. isMatch = false;
  80. break
  81. }
  82. }
  83. if (isMatch)
  84. {
  85. result.push(d);
  86. }
  87. }
  88. // 添加到缓存区
  89. let curCache = this.multipleCache;
  90. for (let i = 0; i < args.length - 1; i++) {
  91. let k = args[i];
  92. let tmpCache = curCache[k];
  93. if (tmpCache == undefined)
  94. {
  95. curCache[k] = tmpCache = {}
  96. }
  97. curCache = tmpCache;
  98. }
  99. curCache[args[args.length - 1]] = result;
  100. return result;
  101. },
  102. // 从多主键缓存中拿数据
  103. findMultipleCache(args)
  104. {
  105. // 索引缓存
  106. let curCache = this.multipleCache;
  107. let isFindCache = true;
  108. for(let p of args) {
  109. if (curCache[p] == undefined) {
  110. isFindCache = false
  111. break
  112. }
  113. curCache = curCache[p]
  114. }
  115. if (isFindCache)
  116. {
  117. return curCache;
  118. }
  119. return null;
  120. },
  121. })