ration.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * Created by Mai on 2017/4/1.
  3. */
  4. var Ration = {
  5. createNew: function (project) {
  6. // 用户定义private方法
  7. var tools = {
  8. };
  9. // 所有通过this访问的属性,都不应在此单元外部进行写入操作
  10. var ration = function (proj) {
  11. this.project = proj;
  12. this.datas = null;
  13. var sourceType = ModuleNames.ration;
  14. this.getSourceType = function () {
  15. return sourceType;
  16. }
  17. proj.registerModule(ModuleNames.ration, this);
  18. var maxRationID = 0;
  19. this.getNewRationID = function () {
  20. return maxRationID += 1;
  21. };
  22. this.maxRationID = function (maxID) {
  23. if (arguments.length === 0) {
  24. return maxRationID;
  25. } else {
  26. maxRationID = Math.max(maxID, maxRationID);
  27. }
  28. };
  29. };
  30. // prototype用于定义public方法
  31. ration.prototype.loadData = function (datas) {
  32. var that = this;
  33. this.datas = datas;
  34. // generate Fees & Flags Index,For View & Calculate
  35. this.datas.forEach(function (data) {
  36. data.FeesIndex = {};
  37. data.fees.forEach(function (fee) {
  38. data.FeesIndex[fee.fieldName] = fee;
  39. });
  40. data.FlagsIndex = {};
  41. data.flags.forEach(function (flag) {
  42. data.FlagsIndex[flag.fieldName] = flag;
  43. });
  44. that.maxRationID(data.ID);
  45. });
  46. };
  47. // 提交数据后的错误处理方法
  48. ration.prototype.doAfterUpdate = function(err, data){
  49. // to do
  50. };
  51. ration.prototype.getTempRationData = function (id, billsID, serialNo) {
  52. var newData = {'ID': id, 'serialNo': serialNo, projectID: this.project.ID()};
  53. newData[project.masterField.ration] = billsID;
  54. return newData;
  55. };
  56. ration.prototype.getBillsSortRation = function (billsID) {
  57. var rations = this.datas.filter(function (data) {
  58. return data[project.masterField.ration] === billsID;
  59. });
  60. rations.sort(function (x, y) {
  61. return x.serialNo - y.serialNo;
  62. });
  63. return rations;
  64. };
  65. ration.prototype.getInsertRationData = function (billsID, preRation) {
  66. var br = this.getBillsSortRation(billsID);
  67. var updateData = [];
  68. if (preRation) {
  69. var preIndex = br.indexOf(preRation), i;
  70. updateData.push({updateType: 'ut_create', updateData: this.getTempRationData(this.maxRationID() + 1, billsID, preIndex < br.length - 1 ? br[preIndex + 1].serialNo : br[preIndex].serialNo + 1)});
  71. for (i = preIndex + 1; i < br.length; i++) {
  72. updateData.push({updateType: 'ut_update', updateData: this.getTempRationData(br[i].ID, billsID, i < br.length - 1 ? br[i+1].serialNo : br[i].serialNo + 1)});
  73. }
  74. } else {
  75. updateData.push({updateType: 'ut_create', updateData: this.getTempRationData(this.maxRationID() + 1, billsID, br.length > 0 ? br[br.length - 1].serialNo + 1 : 1)});
  76. }
  77. return updateData;
  78. };
  79. ration.prototype.getCounterData = function (count) {
  80. var updateData = {'projectID': this.project.ID()};
  81. if (count) {
  82. updateData[this.getSourceType()] = this.maxRationID() + count;
  83. } else {
  84. updateData[this.getSourceType()] = this.maxRationID() + 1;
  85. }
  86. return updateData;
  87. };
  88. ration.prototype.insertRation = function (billsID, preRation) {
  89. var br = this.getBillsSortRation(billsID);
  90. /*this.project.pushNow('insertRation', [this.getSourceType(), 'proj_counter'],
  91. [this.getInsertRationData(billsID, preRation), this.getCounterData()]);*/
  92. this.project.pushNow('insertRation', [this.getSourceType()], [this.getInsertRationData(billsID, preRation)]);
  93. var newRation = null;
  94. if (preRation) {
  95. var preIndex = br.indexOf(preRation), i;
  96. newRation = this.getTempRationData(this.getNewRationID(), billsID, preIndex < br.length - 1 ? br[preIndex + 1].serialNo : br[preIndex].serialNo + 1);
  97. this.datas.push(newRation);
  98. for (i = preIndex + 1; i < br.length; i++) {
  99. br[i].serialNo = i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;
  100. }
  101. } else {
  102. newRation = this.getTempRationData(this.getNewRationID(), billsID, br.length > 0 ? br[br.length - 1].serialNo + 1 : 1);
  103. this.datas.push(newRation);
  104. }
  105. return newRation;
  106. };
  107. ration.prototype.insertStdRation = function (billsID, preRation, std) {
  108. var br = this.getBillsSortRation(billsID), updateData = this.getInsertRationData(billsID, preRation), newRation = null;
  109. updateData.forEach(function (data) {
  110. if (data.updateType === 'ut_create') {
  111. data.updateData.code = std.code;
  112. data.updateData.name = std.name;
  113. data.updateData.unit = std.unit;
  114. newRation = data.updateData;
  115. }
  116. });
  117. this.project.pushNow('insertRation', [this.getSourceType()], [updateData]);
  118. if (preRation) {
  119. var preIndex = br.indexOf(preRation), i;
  120. this.datas.push(newRation);
  121. for (i = preIndex + 1; i < br.length; i++) {
  122. br[i].serialNo = i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;
  123. }
  124. } else {
  125. this.datas.push(newRation);
  126. }
  127. return newRation;
  128. };
  129. return new ration(project);
  130. }
  131. };