project.js 11 KB

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