volume_price.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. };
  34. setMaxID (ID) {
  35. this.maxID(ID);
  36. }
  37. getTempVolumePrice (newID, billsID, serialNo) {
  38. var newData = {'ID': newID, 'serialNo': serialNo, projectID: tools.owner.ID()};
  39. newData[project.masterField.volumePrice] = billsID;
  40. return newData;
  41. };
  42. getBillsSortVolumePrice (billsID) {
  43. var arr = this.datas.filter(function (data) {
  44. return data[tools.owner.masterField.volumePrice] === billsID;
  45. });
  46. arr.sort(function (x, y) {
  47. return x.serialNo - y.serialNo;
  48. });
  49. return arr;
  50. };
  51. getInsertVolumePriceData (billsID, pre) {
  52. let bv = this.getBillsSortVolumePrice(billsID);
  53. let updateData = [];
  54. if (pre) {
  55. let preIndex = bv.indexOf(pre), i;
  56. updateData.push({updateType: 'ut_create', updateData: this.getTempVolumePrice(this.maxID() + 1, billsID, preIndex < bv.length - 1 ? bv[preIndex + 1].serialNo : bv[preIndex].serialNo + 1)});
  57. for (i = preIndex + 1; i < bv.length; i++) {
  58. updateData.push({updateType: 'ut_update', updateData: this.getTempVolumePrice(bv[i].ID, billsID, i < bv.length - 1 ? bv[i+1].serialNo : bv[i].serialNo + 1)});
  59. }
  60. } else {
  61. updateData.push({updateType: 'ut_create', updateData: this.getTempVolumePrice(this.maxID() + 1, billsID, bv.length > 0 ? bv[bv.length - 1].serialNo + 1 : 1)});
  62. }
  63. return updateData;
  64. };
  65. insertVolumePrice (billsID, pre) {
  66. tools.owner.pushNow('insertVolumePrice', [this.getSourceType()], [this.getInsertVolumePriceData(billsID, pre)]);
  67. let bv = this.getBillsSortVolumePrice(billsID), newVP = null;
  68. if (pre) {
  69. let preIndex = bv.indexOf(pre);
  70. newVP = this.getTempVolumePrice(this.getNewID(), billsID, preIndex < bv.length - 1 ? bv[preIndex + 1].serialNo : bv[preIndex].serialNo + 1);
  71. this.datas.push(newVP);
  72. for (let i = preIndex + 1; i < bv.length; i++) {
  73. bv[i].serialNo = i < bv.length - 1 ? bv [i + 1].serialNo : bv[i].serialNo + 1;
  74. }
  75. } else {
  76. newVP = this.getTempVolumePrice(this.getNewID(), billsID, bv.length > 0 ? bv[bv.length - 1].serialNo + 1 : 1);
  77. this.datas.push(newVP);
  78. }
  79. return newVP;
  80. }
  81. }
  82. return new volumePrice();
  83. }
  84. }