project.js 11 KB

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