project.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /**
  2. * Created by Mai on 2017/4/1.
  3. */
  4. var PROJECT = {
  5. createNew: function (projectID, userID) {
  6. // 定义private方法
  7. var tools = {
  8. _project: null,
  9. _ID: projectID,
  10. _userID: userID,
  11. updateLock: 0,
  12. updateData: [],
  13. operation: '',
  14. modules: {},
  15. projCounter: 'projCounter'
  16. };
  17. var me = tools;
  18. tools.doAfterUpdate = function(result){
  19. if (!result) { return }
  20. result.forEach(function(item){
  21. if(_.isArray(item)){
  22. item.forEach(function (e) {
  23. me.eachItem(e);
  24. })
  25. }else {
  26. me.eachItem(item)
  27. }
  28. });
  29. };
  30. tools.doAfterLoad = function(result, callback){
  31. var counter;
  32. result.forEach(function(item){
  33. if (me.modules[item.moduleName]){
  34. me.modules[item.moduleName].loadData(item.data);
  35. } else if (item.moduleName === me.projCounter) {
  36. counter = item.data;
  37. }
  38. });
  39. for (module in counter) {
  40. if (me.modules[module]) {
  41. me.modules[module].setMaxID(counter[module]);
  42. }
  43. }
  44. me._project.loadMainTree();
  45. //me.test(result[0].data[0]);
  46. callback(0);
  47. };
  48. tools.eachItem=function(item){
  49. if (me.modules[item.moduleName]){
  50. me.modules[item.moduleName].doAfterUpdate(item.err, item.data);
  51. }
  52. };
  53. /*tools.test = function(data){
  54. me._project.beginUpdate('修改名称');
  55. data.name = 'test';
  56. data['updateType'] = 'ut_update';
  57. me._project.push(ModuleNames.bills, [data]);
  58. me._project.endUpdate();
  59. };*/
  60. // 所有通过this访问的属性,都不应在此单元外部进行写入操作
  61. var project = function () {
  62. tools._project = this;
  63. this.mainTree = cacheTree.createNew(this);
  64. this.Bills = Bills.createNew(this);
  65. this.Ration = Ration.createNew(this);
  66. this.GLJ = GLJ.createNew(this);
  67. this.ration_glj = ration_glj.createNew(this);
  68. this.ration_coe = ration_coe.createNew(this);
  69. this.ration_ass = ration_ass.createNew(this);
  70. this.quantity_detail = quantity_detail.createNew(this);
  71. this.FeeRate = FeeRate.createNew(this);
  72. this.VolumePrice = VolumePrice.createNew(this);
  73. this.masterField = {ration: 'billsItemID', volumePrice: 'billsItemID'};
  74. };
  75. // prototype用于定义public方法
  76. project.prototype.modify = function (modifyDatas, callback) {
  77. // To Do
  78. };
  79. // prototype用于定义public方法
  80. project.prototype.ID = function () {
  81. return tools._ID;
  82. };
  83. project.prototype.projCounter = function () {
  84. return tools.projCounter;
  85. }
  86. project.prototype.loadMainTree = function () {
  87. var that = this;
  88. var loadRationNode = function (rations, cacheNode) {
  89. var newNode, br = that.Ration.getBillsSortRation(cacheNode.source.getID()), i;
  90. for (i = 0; i < br.length; i++) {
  91. newNode = that.mainTree.addNode(cacheNode);
  92. newNode.source = br[i];
  93. newNode.sourceType = that.Ration.getSourceType();
  94. newNode.data = br[i];
  95. }
  96. };
  97. let loadVolumePriceNode = function (cacheNode) {
  98. let newNode = null, bv = that.VolumePrice.getBillsSortVolumePrice(cacheNode.source.getID());
  99. for (let v of bv) {
  100. newNode = that.mainTree.addNode(cacheNode);
  101. newNode.source = v;
  102. newNode.sourceType = that.VolumePrice.getSourceType();
  103. newNode.data = v;
  104. }
  105. };
  106. var loadIdTreeNode = function (nodes, parent) {
  107. var newNode, i;
  108. for (i = 0; i < nodes.length; i++) {
  109. newNode = that.mainTree.addNode(parent);
  110. newNode.source = nodes[i];
  111. newNode.sourceType = that.Bills.getSourceType();
  112. newNode.data = nodes[i].data;
  113. if (nodes[i].children.length === 0) {
  114. loadRationNode(that.Ration.datas, newNode);
  115. loadVolumePriceNode(newNode);
  116. } else {
  117. loadIdTreeNode(nodes[i].children, newNode);
  118. }
  119. }
  120. };
  121. loadIdTreeNode(this.Bills.tree.roots, null);
  122. this.mainTree.sortTreeItems();
  123. };
  124. project.prototype.getParentTarget = function (node, targetField, targetValue) {
  125. var parent = node;
  126. while (parent && parent[targetField] !== targetValue) {
  127. parent = parent.parent;
  128. }
  129. return parent;
  130. };
  131. // 提供给各模块调用的统一从后台获取数据的方法
  132. /*project.prototype.pullData = function (url, data, successCallback, errorCallback) {
  133. $.ajax({
  134. type:"POST",
  135. url: url,
  136. data: {'data': JSON.stringify(data)},
  137. dataType: 'json',
  138. cache: false,
  139. timeout: 50000,
  140. success: function(result){
  141. successCallback(result);
  142. },
  143. error: function(jqXHR, textStatus, errorThrown){
  144. alert('error ' + textStatus + " " + errorThrown);
  145. errorCallback();
  146. }
  147. });
  148. };*/
  149. // 所有模块在此从后台获取数据
  150. project.prototype.loadDatas = function (callback){
  151. $.ajax({
  152. type: "POST",
  153. url: '/project/getData',
  154. data: {'data': JSON.stringify({
  155. "project_id": tools._ID,
  156. "user_id": tools._userID
  157. })},
  158. dataType: 'json',
  159. cache: false,
  160. timeout: 50000,
  161. success: function (result) {
  162. if (!result.error) {
  163. tools.doAfterLoad(result.data, callback);
  164. // for test calc
  165. //tools.doAfterLoad([{moduleName: 'bills', data: BillsData}, {'moduleName': 'ration', data: DrawingData}], callback);
  166. } else {
  167. alert('error: ' + result.message);
  168. callback(result.error);
  169. }
  170. },
  171. error: function(jqXHR, textStatus, errorThrown){
  172. alert('error ' + textStatus + " " + errorThrown);
  173. }
  174. });
  175. };
  176. project.prototype.beginUpdate = function(operation){
  177. if (tools.updateLock === 0){
  178. tools.operation = operation
  179. }
  180. tools.updateLock += 1;
  181. };
  182. project.prototype.endUpdate = function(){
  183. if (tools.updateLock === 0){
  184. throw "project can not endUpdate before beginUpdate";
  185. }
  186. tools.updateLock -= 1;
  187. if (tools.updateLock === 0) {
  188. $.ajax({
  189. type: "POST",
  190. url: '/project/save',
  191. data: {'data': JSON.stringify({
  192. "project_id": tools._ID,
  193. "user_id": tools._userID,
  194. "date": new Date,
  195. "operation": tools.operation,
  196. "update_data": tools.updateData
  197. })},
  198. dataType: 'json',
  199. cache: false,
  200. timeout: 50000,
  201. success: function (result) {
  202. if (!result.error) {
  203. tools.doAfterUpdate(result.data);
  204. } else {
  205. alert('error: ' + result.message);
  206. }
  207. },
  208. error: function(jqXHR, textStatus, errorThrown){
  209. alert('error ' + textStatus + " " + errorThrown);
  210. }
  211. });
  212. tools.updateData = [];
  213. tools.operation = "";
  214. }
  215. };
  216. project.prototype.push = function(moduleName, data){
  217. if (tools.updateLock === 0){
  218. throw "project can not push data before beginUpdate";
  219. }
  220. var moduleData = {
  221. moduleName: moduleName,
  222. data: data
  223. };
  224. tools.updateData.push(moduleData);
  225. };
  226. project.prototype.pushNow = function (operation, moduleName, data) {
  227. var that = this;
  228. this.beginUpdate(operation);
  229. if (Object.prototype.toString.apply(moduleName) === "[object Array]" && Object.prototype.toString.apply(data) === "[object Array]") {
  230. moduleName.forEach(function (name, index) {
  231. that.push(moduleName[index], data[index]);
  232. });
  233. } else {
  234. this.push(moduleName, data);
  235. }
  236. this.endUpdate();
  237. }
  238. project.prototype.registerModule = function(moduleName, obj){
  239. if (!tools.modules.hasOwnProperty(moduleName)){
  240. tools.modules[moduleName] = obj;
  241. }
  242. };
  243. return new project();
  244. }
  245. };