volume_price.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * Created by Mai on 2017/7/25.
  3. */
  4. var VolumePrice = {
  5. createNew: function (project) {
  6. let tools = {
  7. owner: project
  8. };
  9. class volumePrice {
  10. constructor () {
  11. this.datas = [];
  12. let maxID = 0;
  13. this.getNewID = function () {
  14. return maxID += 1;
  15. };
  16. this.maxID = function (ID) {
  17. if (arguments.length === 0) {
  18. return maxID;
  19. } else {
  20. maxID = Math.max(ID, maxID);
  21. }
  22. };
  23. tools.owner.registerModule(ModuleNames.volume_price, this);
  24. };
  25. getProject () {
  26. return tools.owner;
  27. };
  28. getSourceType () {
  29. return ModuleNames.volume_price;
  30. };
  31. loadData (datas) {
  32. this.datas = datas;
  33. for (let data of datas) {
  34. this.maxID(data.ID);
  35. }
  36. };
  37. setMaxID (ID) {
  38. this.maxID(ID);
  39. }
  40. getTempVolumePrice (newID, billsID, serialNo) {
  41. var newData = {'ID': newID, 'serialNo': serialNo, projectID: tools.owner.ID()};
  42. newData[project.masterField.volumePrice] = billsID;
  43. return newData;
  44. };
  45. getBillsSortVolumePrice (billsID) {
  46. var arr = this.datas.filter(function (data) {
  47. return data[tools.owner.masterField.volumePrice] === billsID;
  48. });
  49. arr.sort(function (x, y) {
  50. return x.serialNo - y.serialNo;
  51. });
  52. return arr;
  53. };
  54. getCounterData (count) {
  55. var updateData = {'projectID': this.getProject().ID()};
  56. if (count) {
  57. updateData[this.getSourceType()] = this.maxID() + count;
  58. } else {
  59. updateData[this.getSourceType()] = this.maxID() + 1;
  60. }
  61. return updateData;
  62. };
  63. getInsertVolumePriceData (billsID, pre) {
  64. let bv = this.getBillsSortVolumePrice(billsID);
  65. let updateData = [];
  66. if (pre) {
  67. let preIndex = bv.indexOf(pre), i;
  68. updateData.push({updateType: 'ut_create', updateData: this.getTempVolumePrice(this.maxID() + 1, billsID, preIndex < bv.length - 1 ? bv[preIndex + 1].serialNo : bv[preIndex].serialNo + 1)});
  69. for (i = preIndex + 1; i < bv.length; i++) {
  70. updateData.push({updateType: 'ut_update', updateData: this.getTempVolumePrice(bv[i].ID, billsID, i < bv.length - 1 ? bv[i+1].serialNo : bv[i].serialNo + 1)});
  71. }
  72. } else {
  73. updateData.push({updateType: 'ut_create', updateData: this.getTempVolumePrice(this.maxID() + 1, billsID, bv.length > 0 ? bv[bv.length - 1].serialNo + 1 : 1)});
  74. }
  75. return updateData;
  76. };
  77. insertVolumePrice (billsID, pre) {
  78. tools.owner.pushNow('insertVolumePrice', [this.getSourceType(), this.getProject().projCounter()], [this.getInsertVolumePriceData(billsID, pre), this.getCounterData()]);
  79. let bv = this.getBillsSortVolumePrice(billsID), newVP = null;
  80. if (pre) {
  81. let preIndex = bv.indexOf(pre);
  82. newVP = this.getTempVolumePrice(this.getNewID(), billsID, preIndex < bv.length - 1 ? bv[preIndex + 1].serialNo : bv[preIndex].serialNo + 1);
  83. this.datas.push(newVP);
  84. for (let i = preIndex + 1; i < bv.length; i++) {
  85. bv[i].serialNo = i < bv.length - 1 ? bv [i + 1].serialNo : bv[i].serialNo + 1;
  86. }
  87. } else {
  88. newVP = this.getTempVolumePrice(this.getNewID(), billsID, bv.length > 0 ? bv[bv.length - 1].serialNo + 1 : 1);
  89. this.datas.push(newVP);
  90. }
  91. return newVP;
  92. };
  93. getDeleteData (volumePrice) {
  94. var updateData = [];
  95. updateData.push({'updateType': 'ut_delete', 'updateData': {'ID': volumePrice.ID, 'projectID': this.getProject().ID()}});
  96. return updateData;
  97. };
  98. delete (volumePrice) {
  99. this.getProject().pushNow('deleteVolumePrice', [this.getSourceType()], [this.getDeleteData(volumePrice)]);
  100. this.datas.splice(this.datas.indexOf(volumePrice), 1);
  101. };
  102. getDeleteDataByBills (nodes) {
  103. let updateData = [];
  104. for (let node of nodes) {
  105. if (node.children.length > 0) {
  106. updateData = updateData.concat(this.getDeleteDataByBills[node.children]);
  107. } else {
  108. let vps = this.getBillsSortVolumePrice(node.getID());
  109. for (let vp of vps) {
  110. updateData.push({'updateType': 'ut_delete', 'updateData': {'ID': vp.ID, 'projectID': this.getProject().ID()}});
  111. }
  112. }
  113. }
  114. return updateData;
  115. };
  116. deleteByBills (nodes) {
  117. for (let node of nodes) {
  118. if (node.children.length > 0) {
  119. this.deleteByBills([node.children]);
  120. } else {
  121. let vps = this.getBillsSortVolumePrice(node.getID());
  122. for (let vp of vps) {
  123. this.datas.splice(this.datas.indexOf(vp), 1);
  124. }
  125. }
  126. }
  127. };
  128. }
  129. return new volumePrice();
  130. }
  131. }