ManyToMany.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. const express = require('express');
  2. const path = require('path');
  3. const RepositorySystem = require('../systems/RepositorySystem');
  4. const Actions = require('../systems/ActionSystem');
  5. class ManyToMany {
  6. constructor(name, inceptor){
  7. this.name = name;
  8. this.scheme = {
  9. auto_increment: 2000,
  10. fields: [
  11. {
  12. key: "name",
  13. type: "string",
  14. keyLength: 200
  15. }
  16. ]
  17. };
  18. this.repository = RepositorySystem.create(this.name, this.scheme);
  19. this.inceptor = inceptor;
  20. this.inceptorScheme = {
  21. fields: [
  22. {
  23. key: name + "_ids",
  24. type: "text"
  25. }
  26. ]
  27. };
  28. this.inceptorRepo = RepositorySystem.create(this.inceptor, this.inceptorScheme);
  29. this._init2();
  30. }
  31. _init2() {
  32. Actions.on('model'+this.inceptor, (model) => {
  33. return this.repository.getPage(0,1000)
  34. .then((categories) => {
  35. return {
  36. ...model,
  37. [this.name + "_ids"]: {
  38. disabled: true
  39. },
  40. [this.name]: {
  41. Filter: {
  42. type: "manytomany",
  43. options: categories.reduce((data,a) => {return {...data, [a.id]: a.name }}, {}),
  44. name: this.name,
  45. field: this.name + '_ids',
  46. order: 60
  47. }
  48. }
  49. }
  50. })
  51. });
  52. Actions.on('get'+this.inceptor, (product) => {
  53. if (!product[this.name + "_ids"])return product;
  54. let cids = product[this.name + "_ids"].split('|');
  55. return this.repository.raw().whereIn('id',cids)
  56. .then((categories) => {
  57. let dict = categories.map((i) => i.name);
  58. product[this.name] = dict;
  59. delete product[this.name + "_ids"];
  60. return product;
  61. });
  62. });
  63. Actions.on('import'+this.inceptor, (dataset) => {
  64. return this.repository.getPage(0,1000).then((cats) => {
  65. let catNames = cats.reduce((w, a) => {
  66. w[a.name] = a.id;
  67. return w;
  68. },{});
  69. let categories = dataset.reduce((acc, item) => {
  70. let categories = (item[this.name] || '').split(',');
  71. for(var i in categories) {
  72. if(!acc.includes(categories[i]))acc.push(categories[i])
  73. }
  74. return acc;
  75. },[]);
  76. var filtered = categories.filter((value, index, arr) => {
  77. return !catNames[value];
  78. });
  79. console.log("FILTERED", filtered);
  80. filtered = filtered.map((name) => {return {name}})
  81. return this.repository.batchInsert(filtered)
  82. .then(() => this.repository.getPage(0,1000))
  83. .then((cats) => {
  84. return cats.reduce((w, a) => {
  85. w[a.name] = a.id;
  86. return w;
  87. },{});
  88. })
  89. .then((names) => dataset.map((item) => {
  90. let categories = (item[this.name] || '').split(',');
  91. let res = [];
  92. for(var i in categories) {
  93. res.push(names[categories[i]])
  94. }
  95. if(res.length)
  96. item[this.name + "_ids"] = res.join('|');
  97. return item;
  98. }));
  99. });
  100. });
  101. Actions.on('post'+this.inceptor, (product) => {
  102. if(!product[this.name + "_ids"])
  103. return product;
  104. let cats = product[this.name + "_ids"];
  105. return this.repository.raw().whereIn('id', cats)
  106. .then((categories) => {
  107. let cids = categories.map((c) => c.id);
  108. if(cids.length)
  109. product[this.name + "_ids"] = cids.join('|');
  110. return product;
  111. });
  112. });
  113. }
  114. }
  115. module.exports = ManyToMany;