ration.js 8.4 KB

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