bills.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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: false
  12. };
  13. // 用户定义private方法
  14. var tools = {
  15. coverseTreeUpdateData: function (datas) {
  16. var updateDatas = [];
  17. datas.forEach(function (data) {
  18. var updateData = {};
  19. if (data.type === idTree.updateType.new) {
  20. updateData.updateType = 'ut_create';
  21. updateData.updateData = data.data;
  22. } else if (data.type === idTree.updateType.update) {
  23. updateData.updateType = 'ut_update';
  24. updateData.updateData = data.data;
  25. } else if (data.type === idTree.updateType.delete) {
  26. updateData.updateType = 'ut_delete';
  27. updateData.updateData = data.data;
  28. }
  29. updateDatas.push(updateData);
  30. });
  31. return updateDatas;
  32. }
  33. };
  34. // 所有通过this访问的属性,都不应在此单元外部进行写入操作
  35. var bills = function (proj) {
  36. this.project = proj;
  37. this.datas = null;
  38. this.tree = idTree.createNew(billsTreeSetting);
  39. var sourceType = ModuleNames.bills;
  40. this.getSourceType = function () {
  41. return sourceType;
  42. }
  43. proj.registerModule(ModuleNames.bills, this);
  44. };
  45. // 从后台获取数据
  46. /*bills.prototype.pullData = function (){
  47. this.project.pullData(
  48. '/bills/getData',
  49. {projectID: this.project.ID},
  50. function(result){
  51. if (result.error ===0){
  52. this.loadDatas(result.data);
  53. }
  54. else {
  55. // to do: ?错误处理需要细化
  56. alert(result.message);
  57. }
  58. },
  59. function (){}// to do: 错误处理需要细化
  60. )
  61. };*/
  62. // prototype用于定义public方法
  63. bills.prototype.loadData = function (datas) {
  64. this.datas = datas;
  65. // generate Fees & Flags Index, For View & Calculate
  66. this.datas.forEach(function (data) {
  67. data.FeesIndex = {};
  68. if (data.fees) {
  69. data.fees.forEach(function (fee) {
  70. data.FeesIndex[fee.fieldName] = fee;
  71. });
  72. }
  73. data.FlagsIndex = {};
  74. if (data.flags) {
  75. data.flags.forEach(function (flag) {
  76. data.FlagsIndex[flag.fieldName] = flag;
  77. });
  78. }
  79. });
  80. // datas load to Tree
  81. this.tree.loadDatas(this.datas);
  82. };
  83. bills.prototype.setMaxID = function (ID) {
  84. this.tree.maxNodeID(ID);
  85. };
  86. // 提交数据后的错误处理方法
  87. bills.prototype.doAfterUpdate = function(err, data){
  88. // to do
  89. };
  90. bills.prototype.getCounterData = function (count) {
  91. var updateData = {'projectID': this.project.ID()};
  92. if (count) {
  93. updateData[this.getSourceType()] = this.tree.maxNodeID() + count;
  94. } else {
  95. updateData[this.getSourceType()] = this.tree.maxNodeID() + 1;
  96. }
  97. return updateData;
  98. };
  99. bills.prototype.insertBills = function (parentId, nextSiblingId) {
  100. var insertData = this.tree.getInsertData(parentId, nextSiblingId);
  101. var that = this;
  102. insertData.forEach(function (data) {
  103. data.data.projectID = that.project.ID();
  104. if (data.type === idTree.updateType.new) {
  105. that.datas.push(data.data);
  106. }
  107. });
  108. this.project.pushNow('insertBills', [this.getSourceType(), this.project.projCounter()],
  109. [ tools.coverseTreeUpdateData(insertData), this.getCounterData()]);
  110. //project.pushNow('insertBills', ModuleNames.bills, tools.coverseTreeUpdateData(insertData));
  111. return this.tree.insert(parentId, nextSiblingId);
  112. };
  113. bills.prototype.insertStdBills = function (parentId, nextSiblingId, stdBillsData) {
  114. var insertData = this.tree.getInsertData(parentId, nextSiblingId);
  115. var newData = null, that = this;
  116. insertData.forEach(function (data) {
  117. data.data.projectID = that.project.ID();
  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. that.datas.push(data.data);
  123. newData = data.data;
  124. }
  125. });
  126. this.project.pushNow('insertStdBills', [this.getSourceType(), this.project.projCounter()],
  127. [ tools.coverseTreeUpdateData(insertData), this.getCounterData()]);
  128. //project.pushNow('insertStdBills', ModuleNames.bills, tools.coverseTreeUpdateData(insertData));
  129. return this.tree.insertByData(newData, parentId, nextSiblingId);
  130. }
  131. bills.prototype.deleteBills = function (node) {
  132. var deleteData = this.tree.getDeleteData(node);
  133. project.beginUpdate('deleteBills');
  134. project.push(ModuleNames.bills, tools.coverseTreeUpdateData(deleteData, project.ID()));
  135. project.endUpdate();
  136. return this.tree.delete(node);
  137. }
  138. return new bills(project);
  139. }
  140. };