ration.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. ration.prototype.setMaxID = function (ID) {
  48. this.maxRationID(ID);
  49. }
  50. // 提交数据后的错误处理方法
  51. ration.prototype.doAfterUpdate = function(err, data){
  52. // to do
  53. };
  54. ration.prototype.getTempRationData = function (id, billsID, serialNo) {
  55. var newData = {'ID': id, 'serialNo': serialNo, projectID: this.project.ID()};
  56. newData[project.masterField.ration] = billsID;
  57. return newData;
  58. };
  59. ration.prototype.getBillsSortRation = function (billsID) {
  60. var rations = this.datas.filter(function (data) {
  61. return data[project.masterField.ration] === billsID;
  62. });
  63. rations.sort(function (x, y) {
  64. return x.serialNo - y.serialNo;
  65. });
  66. return rations;
  67. };
  68. ration.prototype.getInsertRationData = function (billsID, preRation) {
  69. var br = this.getBillsSortRation(billsID);
  70. var updateData = [];
  71. if (preRation) {
  72. var preIndex = br.indexOf(preRation), i;
  73. updateData.push({updateType: 'ut_create', updateData: this.getTempRationData(this.maxRationID() + 1, billsID, preIndex < br.length - 1 ? br[preIndex + 1].serialNo : br[preIndex].serialNo + 1)});
  74. for (i = preIndex + 1; i < br.length; i++) {
  75. updateData.push({updateType: 'ut_update', updateData: this.getTempRationData(br[i].ID, billsID, i < br.length - 1 ? br[i+1].serialNo : br[i].serialNo + 1)});
  76. }
  77. } else {
  78. updateData.push({updateType: 'ut_create', updateData: this.getTempRationData(this.maxRationID() + 1, billsID, br.length > 0 ? br[br.length - 1].serialNo + 1 : 1)});
  79. }
  80. return updateData;
  81. };
  82. ration.prototype.getCounterData = function (count) {
  83. var updateData = {'projectID': this.project.ID()};
  84. if (count) {
  85. updateData[this.getSourceType()] = this.maxRationID() + count;
  86. } else {
  87. updateData[this.getSourceType()] = this.maxRationID() + 1;
  88. }
  89. return updateData;
  90. };
  91. ration.prototype.insertRation = function (billsID, preRation) {
  92. var br = this.getBillsSortRation(billsID);
  93. this.project.pushNow('insertRation', [this.getSourceType(), this.project.projCounter()],
  94. [this.getInsertRationData(billsID, preRation), this.getCounterData()]);
  95. //this.project.pushNow('insertRation', [this.getSourceType()], [this.getInsertRationData(billsID, preRation)]);
  96. var newRation = null;
  97. if (preRation) {
  98. var preIndex = br.indexOf(preRation), i;
  99. newRation = this.getTempRationData(this.getNewRationID(), billsID, preIndex < br.length - 1 ? br[preIndex + 1].serialNo : br[preIndex].serialNo + 1);
  100. this.datas.push(newRation);
  101. for (i = preIndex + 1; i < br.length; i++) {
  102. br[i].serialNo = i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;
  103. }
  104. } else {
  105. newRation = this.getTempRationData(this.getNewRationID(), billsID, br.length > 0 ? br[br.length - 1].serialNo + 1 : 1);
  106. this.datas.push(newRation);
  107. }
  108. return newRation;
  109. };
  110. ration.prototype.insertStdRation = function (billsID, preRation, std) {
  111. var br = this.getBillsSortRation(billsID), updateData = this.getInsertRationData(billsID, preRation), newRation = null;
  112. updateData.forEach(function (data) {
  113. if (data.updateType === 'ut_create') {
  114. data.updateData.code = std.code;
  115. data.updateData.name = std.name;
  116. data.updateData.unit = std.unit;
  117. newRation = data.updateData;
  118. }
  119. });
  120. this.project.pushNow('insertRation', [this.getSourceType()], [updateData]);
  121. newRation.ID = this.getNewRationID();
  122. if (preRation) {
  123. var preIndex = br.indexOf(preRation), i;
  124. this.datas.push(newRation);
  125. for (i = preIndex + 1; i < br.length; i++) {
  126. br[i].serialNo = i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;
  127. }
  128. } else {
  129. this.datas.push(newRation);
  130. }
  131. return newRation;
  132. };
  133. ration.prototype.getChangePosUpdateData = function (ration1, ration2) {
  134. var updateData = [];
  135. updateData.push({updateType: 'ut_update', updateData: this.getTempRationData(ration1.ID, ration1.billsItemID, ration2.serialNo)});
  136. updateData.push({updateType: 'ut_update', updateData: this.getTempRationData(ration2.ID, ration2.billsItemID, ration1.serialNo)});
  137. return updateData;
  138. };
  139. ration.prototype.changePos = function (ration1, ration2) {
  140. var updateData = this.getChangePosUpdateData(ration1, ration2);
  141. this.project.pushNow('insertRation', [this.getSourceType()], [updateData]);
  142. var preSerialNo = ration1.serialNo;
  143. ration1.serialNo = ration2.serialNo;
  144. ration2.serialNo = preSerialNo;
  145. }
  146. return new ration(project);
  147. }
  148. };