bills.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * Created by Mai on 2017/4/1.
  3. */
  4. var Bills = {
  5. createNew: function (project) {
  6. var billsTreeSetting = {
  7. id: 'ID',
  8. pid: 'ParentID',
  9. nid: 'NextSiblingID',
  10. rootId: -1,
  11. autoUpdate: true
  12. };
  13. // 用户定义private方法
  14. var tools = {
  15. coverseTreeUpdateData: function (datas, projectID) {
  16. var updateDatas = [];
  17. datas.forEach(function (data) {
  18. var updateData = {};
  19. data.data.projectID = projectID;
  20. if (data.type === idTree.updateType.new) {
  21. updateData.updateType = 'ut_create';
  22. updateData.updateData = data.data;
  23. } else if (data.type === idTree.updateType.update) {
  24. updateData.updateType = 'ut_update';
  25. updateData.updateData = data.data;
  26. } else if (data.type === idTree.updateType.delete) {
  27. updateData.updateType = 'ut_delete';
  28. updateData.updateData = data.data;
  29. }
  30. updateDatas.push(updateData);
  31. });
  32. return updateDatas;
  33. }
  34. };
  35. // 所有通过this访问的属性,都不应在此单元外部进行写入操作
  36. var bills = function (proj) {
  37. this.project = proj;
  38. this.datas = null;
  39. this.tree = idTree.createNew(billsTreeSetting);
  40. var sourceType = ModuleNames.bills;
  41. this.getSourceType = function () {
  42. return sourceType;
  43. }
  44. proj.registerModule(ModuleNames.bills, this);
  45. };
  46. // 从后台获取数据
  47. /*bills.prototype.pullData = function (){
  48. this.project.pullData(
  49. '/bills/getData',
  50. {projectID: this.project.ID},
  51. function(result){
  52. if (result.error ===0){
  53. this.loadDatas(result.data);
  54. }
  55. else {
  56. // to do: ?错误处理需要细化
  57. alert(result.message);
  58. }
  59. },
  60. function (){}// to do: 错误处理需要细化
  61. )
  62. };*/
  63. // prototype用于定义public方法
  64. bills.prototype.loadData = function (datas) {
  65. this.datas = datas;
  66. // generate Fees & Flags Index, For View & Calculate
  67. this.datas.forEach(function (data) {
  68. data.FeesIndex = {};
  69. if (data.fees) {
  70. data.fees.forEach(function (fee) {
  71. data.FeesIndex[fee.fieldName] = fee;
  72. });
  73. }
  74. data.FlagsIndex = {};
  75. if (data.flags) {
  76. data.flags.forEach(function (flag) {
  77. data.FlagsIndex[flag.fieldName] = flag;
  78. });
  79. }
  80. });
  81. // datas load to Tree
  82. this.tree.loadDatas(this.datas);
  83. };
  84. bills.prototype.setMaxID = function (ID) {
  85. this.tree.maxNodeID(ID);
  86. };
  87. // 提交数据后的错误处理方法
  88. bills.prototype.doAfterUpdate = function(err, data){
  89. // to do
  90. };
  91. bills.prototype.getCounterData = function (count) {
  92. var updateData = {'projectID': this.project.ID()};
  93. if (count) {
  94. updateData[this.getSourceType()] = this.tree.maxNodeID() + count;
  95. } else {
  96. updateData[this.getSourceType()] = this.tree.maxNodeID() + 1;
  97. }
  98. return updateData;
  99. };
  100. bills.prototype.insertBills = function (parentId, nextSiblingId) {
  101. var insertData = this.tree.getInsertData(parentId, nextSiblingId);
  102. var that = this, newData = null;
  103. insertData.forEach(function (data) {
  104. if (data.type === idTree.updateType.new) {
  105. newData = data.data;
  106. }
  107. });
  108. this.project.pushNow('insertBills', [this.getSourceType(), this.project.projCounter()],
  109. [ tools.coverseTreeUpdateData(insertData, this.project.ID()), this.getCounterData()]);
  110. //project.pushNow('insertBills', ModuleNames.bills, tools.coverseTreeUpdateData(insertData));
  111. this.datas.push(newData);
  112. return this.tree.insert(parentId, nextSiblingId);
  113. };
  114. bills.prototype.insertStdBills = function (parentId, nextSiblingId, stdBillsData) {
  115. var insertData = this.tree.getInsertData(parentId, nextSiblingId);
  116. var newData = null, that = this;
  117. insertData.forEach(function (data) {
  118. if (data.type === idTree.updateType.new) {
  119. data.data.code = stdBillsData.code;
  120. data.data.name = stdBillsData.name;
  121. data.data.unit = stdBillsData.unit;
  122. newData = data.data;
  123. }
  124. });
  125. this.project.pushNow('insertStdBills', [this.getSourceType(), this.project.projCounter()],
  126. [ tools.coverseTreeUpdateData(insertData, this.project.ID()), this.getCounterData()]);
  127. this.datas.push(newData);
  128. return this.tree.insertByData(newData, parentId, nextSiblingId);
  129. };
  130. bills.prototype.deleteBills = function (node) {
  131. var deleteData = this.tree.getDeleteData(node);
  132. var ration_glj =projectObj.project.ration_glj;
  133. // project.beginUpdate('deleteBills');
  134. //project.push(ModuleNames.bills, tools.coverseTreeUpdateData(deleteData, this.project.ID()));
  135. //project.endUpdate();
  136. var moudles =[ModuleNames.bills,ModuleNames.ration_glj];
  137. var deleteDatas=[tools.coverseTreeUpdateData(deleteData, this.project.ID()),ration_glj.getDeleteDataByBills(deleteData)];
  138. // project.ration.getDeleteDataByBill(node);
  139. project.pushNow('deleteBILL', moudles, deleteDatas);
  140. // return this.tree.delete(node);
  141. };
  142. bills.prototype.upMoveBills = function (node) {
  143. var upMoveData = node.getUpMoveData();
  144. project.pushNow('upMoveBills', this.getSourceType(), tools.coverseTreeUpdateData(upMoveData, this.project.ID()));
  145. return node.upMove();
  146. };
  147. bills.prototype.downMoveBills = function (node) {
  148. var downMoveData = node.getDownMoveData();
  149. project.pushNow('downMoveBills', this.getSourceType(), tools.coverseTreeUpdateData(downMoveData, this.project.ID()));
  150. return node.downMove();
  151. };
  152. bills.prototype.upLevelBills = function (node) {
  153. var upLevelData = node.getUpLevelData();
  154. project.pushNow('upLevelBills', this.getSourceType(), tools.coverseTreeUpdateData(upLevelData, this.project.ID()));
  155. return node.upLevel();
  156. };
  157. bills.prototype.downLevelBills = function (node) {
  158. var downLevelData = node.getDownLevelData();
  159. project.pushNow('downLevelBills', [this.getSourceType()], [tools.coverseTreeUpdateData(downLevelData, this.project.ID())]);
  160. return node.downLevel();
  161. }
  162. return new bills(project);
  163. }
  164. };