ration.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. // refresh after update
  51. ration.prototype.doAfterUpdate = function(err, data){
  52. var controller = projectObj.mainController, project = projectObj.project;
  53. //var selected = projectObj.mainController.tree.selected;
  54. console.log(controller.sheet.getSelections());
  55. // to do
  56. console.log(controller.sheet.getCell(8, 9).value());
  57. var selected = projectObj.project.mainTree.selected;
  58. console.log(data);
  59. };
  60. ration.prototype.getTempRationData = function (id, billsID, serialNo) {
  61. var newData = {'ID': id, 'serialNo': serialNo, projectID: this.project.ID()};
  62. newData[project.masterField.ration] = billsID;
  63. return newData;
  64. };
  65. ration.prototype.getBillsSortRation = function (billsID) {
  66. var rations = this.datas.filter(function (data) {
  67. return data[project.masterField.ration] === billsID;
  68. });
  69. rations.sort(function (x, y) {
  70. return x.serialNo - y.serialNo;
  71. });
  72. return rations;
  73. };
  74. ration.prototype.getInsertRationData = function (billsID, preRation) {
  75. var br = this.getBillsSortRation(billsID);
  76. var updateData = [];
  77. if (preRation) {
  78. var preIndex = br.indexOf(preRation), i;
  79. updateData.push({updateType: 'ut_create', updateData: this.getTempRationData(this.maxRationID() + 1, billsID, preIndex < br.length - 1 ? br[preIndex + 1].serialNo : br[preIndex].serialNo + 1)});
  80. for (i = preIndex + 1; i < br.length; i++) {
  81. updateData.push({updateType: 'ut_update', updateData: this.getTempRationData(br[i].ID, billsID, i < br.length - 1 ? br[i+1].serialNo : br[i].serialNo + 1)});
  82. }
  83. } else {
  84. updateData.push({updateType: 'ut_create', updateData: this.getTempRationData(this.maxRationID() + 1, billsID, br.length > 0 ? br[br.length - 1].serialNo + 1 : 1)});
  85. }
  86. return updateData;
  87. };
  88. ration.prototype.getCounterData = function (count) {
  89. var updateData = {'projectID': this.project.ID()};
  90. if (count) {
  91. updateData[this.getSourceType()] = this.maxRationID() + count;
  92. } else {
  93. updateData[this.getSourceType()] = this.maxRationID() + 1;
  94. }
  95. return updateData;
  96. };
  97. ration.prototype.insertRation = function (billsID, preRation) {
  98. var br = this.getBillsSortRation(billsID);
  99. this.project.pushNow('insertRation', [this.getSourceType(), this.project.projCounter()],
  100. [this.getInsertRationData(billsID, preRation), this.getCounterData()]);
  101. var newRation = null;
  102. if (preRation) {
  103. var preIndex = br.indexOf(preRation), i;
  104. newRation = this.getTempRationData(this.getNewRationID(), billsID, preIndex < br.length - 1 ? br[preIndex + 1].serialNo : br[preIndex].serialNo + 1);
  105. this.datas.push(newRation);
  106. for (i = preIndex + 1; i < br.length; i++) {
  107. br[i].serialNo = i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;
  108. }
  109. } else {
  110. newRation = this.getTempRationData(this.getNewRationID(), billsID, br.length > 0 ? br[br.length - 1].serialNo + 1 : 1);
  111. this.datas.push(newRation);
  112. }
  113. return newRation;
  114. };
  115. ration.prototype.insertStdRation = function (billsID, preRation, std) {
  116. var br = this.getBillsSortRation(billsID), updateData = this.getInsertRationData(billsID, preRation), newRation = null;
  117. updateData.forEach(function (data) {
  118. if (data.updateType === 'ut_create') {
  119. data.updateData.code = std.code;
  120. data.updateData.name = std.name;
  121. data.updateData.unit = std.unit;
  122. data.updateData.libID = std.rationRepId;
  123. data.updateData.rationAssList = projectObj.project.ration_ass.CreateNewAss(std);
  124. newRation = data.updateData;
  125. }
  126. });
  127. this.project.pushNow('insertRation', [this.getSourceType()], [updateData]);
  128. newRation.ID = this.getNewRationID();
  129. if (preRation) {
  130. var preIndex = br.indexOf(preRation), i;
  131. this.datas.push(newRation);
  132. for (i = preIndex + 1; i < br.length; i++) {
  133. br[i].serialNo = i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;
  134. }
  135. } else {
  136. this.datas.push(newRation);
  137. }
  138. return newRation;
  139. };
  140. ration.prototype.getDeleteData = function (rationData) {
  141. var updateData = [];
  142. updateData.push({'updateType': 'ut_delete', 'updateData': {'ID': rationData.ID, 'projectID': this.project.ID()}});
  143. return updateData;
  144. };
  145. ration.prototype.getDeleteDataByBill = function (node) {
  146. console.log(node);
  147. };
  148. ration.prototype.delete = function (ration) {
  149. var ration_glj =projectObj.project.ration_glj;
  150. this.project.pushNow('deleteRation', [this.getSourceType(),ration_glj.getSourceType()], [this.getDeleteData(ration),ration_glj.getDeleteDataByRation(ration)]);
  151. this.datas.splice(this.datas.indexOf(ration), 1);
  152. };
  153. ration.prototype.getChangePosUpdateData = function (ration1, ration2) {
  154. var updateData = [];
  155. updateData.push({updateType: 'ut_update', updateData: this.getTempRationData(ration1.ID, ration1.billsItemID, ration2.serialNo)});
  156. updateData.push({updateType: 'ut_update', updateData: this.getTempRationData(ration2.ID, ration2.billsItemID, ration1.serialNo)});
  157. return updateData;
  158. };
  159. ration.prototype.changePos = function (ration1, ration2) {
  160. var updateData = this.getChangePosUpdateData(ration1, ration2);
  161. this.project.pushNow('insertRation', [this.getSourceType()], [updateData]);
  162. var preSerialNo = ration1.serialNo;
  163. ration1.serialNo = ration2.serialNo;
  164. ration2.serialNo = preSerialNo;
  165. };
  166. return new ration(project);
  167. }
  168. };