ration.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. var newRation = null;
  96. if (preRation) {
  97. var preIndex = br.indexOf(preRation), i;
  98. newRation = this.getTempRationData(this.getNewRationID(), billsID, preIndex < br.length - 1 ? br[preIndex + 1].serialNo : br[preIndex].serialNo + 1);
  99. this.datas.push(newRation);
  100. for (i = preIndex + 1; i < br.length; i++) {
  101. br[i].serialNo = i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;
  102. }
  103. } else {
  104. newRation = this.getTempRationData(this.getNewRationID(), billsID, br.length > 0 ? br[br.length - 1].serialNo + 1 : 1);
  105. this.datas.push(newRation);
  106. }
  107. return newRation;
  108. };
  109. ration.prototype.insertStdRation = function (billsID, preRation, std) {
  110. var br = this.getBillsSortRation(billsID), updateData = this.getInsertRationData(billsID, preRation), newRation = null;
  111. updateData.forEach(function (data) {
  112. if (data.updateType === 'ut_create') {
  113. data.updateData.code = std.code;
  114. data.updateData.name = std.name;
  115. data.updateData.unit = std.unit;
  116. data.updateData.libID = std.rationRepId;
  117. data.updateData.rationAssList = projectObj.project.ration_ass.CreateNewAss(std);
  118. newRation = data.updateData;
  119. }
  120. });
  121. this.project.pushNow('insertRation', [this.getSourceType()], [updateData]);
  122. newRation.ID = this.getNewRationID();
  123. if (preRation) {
  124. var preIndex = br.indexOf(preRation), i;
  125. this.datas.push(newRation);
  126. for (i = preIndex + 1; i < br.length; i++) {
  127. br[i].serialNo = i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;
  128. }
  129. } else {
  130. this.datas.push(newRation);
  131. }
  132. return newRation;
  133. };
  134. ration.prototype.getDeleteData = function (rationData) {
  135. var updateData = [];
  136. updateData.push({'updateType': 'ut_delete', 'updateData': {'ID': rationData.ID, 'projectID': this.project.ID()}});
  137. return updateData;
  138. };
  139. ration.prototype.getDeleteDataByBill = function (node) {
  140. console.log(node);
  141. };
  142. ration.prototype.delete = function (ration) {
  143. var ration_glj =projectObj.project.ration_glj;
  144. this.project.pushNow('deleteRation', [this.getSourceType(),ration_glj.getSourceType()], [this.getDeleteData(ration),ration_glj.getDeleteDataByRation(ration)]);
  145. this.datas.splice(this.datas.indexOf(ration), 1);
  146. };
  147. ration.prototype.getChangePosUpdateData = function (ration1, ration2) {
  148. var updateData = [];
  149. updateData.push({updateType: 'ut_update', updateData: this.getTempRationData(ration1.ID, ration1.billsItemID, ration2.serialNo)});
  150. updateData.push({updateType: 'ut_update', updateData: this.getTempRationData(ration2.ID, ration2.billsItemID, ration1.serialNo)});
  151. return updateData;
  152. };
  153. ration.prototype.changePos = function (ration1, ration2) {
  154. var updateData = this.getChangePosUpdateData(ration1, ration2);
  155. this.project.pushNow('insertRation', [this.getSourceType()], [updateData]);
  156. var preSerialNo = ration1.serialNo;
  157. ration1.serialNo = ration2.serialNo;
  158. ration2.serialNo = preSerialNo;
  159. };
  160. return new ration(project);
  161. }
  162. };