project.js 10 KB

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