project.js 11 KB

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