project.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. }else if(item.moduleName === ModuleNames.projectGLJ){
  42. me._project.projectGLJ.loadToCache(item.data);
  43. }
  44. });
  45. for (module in counter) {
  46. if (me.modules[module]) {
  47. me.modules[module].setMaxID(counter[module]);
  48. }
  49. }
  50. me._project.loadMainTree();
  51. //me.test(result[0].data[0]);
  52. callback(0);
  53. };
  54. tools.eachItem=function(item){
  55. if (me.modules[item.moduleName]){
  56. me.modules[item.moduleName].doAfterUpdate(item.err, item.data);
  57. }
  58. };
  59. /*tools.test = function(data){
  60. me._project.beginUpdate('修改名称');
  61. data.name = 'test';
  62. data['updateType'] = 'ut_update';
  63. me._project.push(ModuleNames.bills, [data]);
  64. me._project.endUpdate();
  65. };*/
  66. // 所有通过this访问的属性,都不应在此单元外部进行写入操作
  67. var project = function () {
  68. tools._project = this;
  69. this.mainTree = cacheTree.createNew(this);
  70. this.Bills = Bills.createNew(this);
  71. this.Ration = Ration.createNew(this);
  72. this.GLJ = GLJ.createNew(this);
  73. this.ration_glj = ration_glj.createNew(this);
  74. this.ration_coe = ration_coe.createNew(this);
  75. this.ration_ass = ration_ass.createNew(this);
  76. this.quantity_detail = quantity_detail.createNew(this);
  77. this.installation_fee = installation_fee.createNew(this);
  78. this.FeeRate = FeeRate.createNew(this);
  79. // this.VolumePrice = VolumePrice.createNew(this);
  80. this.projectGLJ = new ProjectGLJ();
  81. // this.projectGLJ.loadData();
  82. this.composition = new Composition();
  83. this.composition.loadData();
  84. this.labourCoe = new LabourCoe(this);
  85. this.calcProgram = new CalcProgram(this);
  86. this.calcBase = calcBase;
  87. // this.masterField = {ration: 'billsItemID', volumePrice: 'billsItemID'};
  88. this.masterField = {ration: 'billsItemID'};
  89. };
  90. // prototype用于定义public方法
  91. project.prototype.modify = function (modifyDatas, callback) {
  92. // To Do
  93. };
  94. // prototype用于定义public方法
  95. project.prototype.ID = function () {
  96. return tools._ID;
  97. };
  98. project.prototype.projCounter = function () {
  99. return tools.projCounter;
  100. };
  101. project.prototype.getDecimal = function (fullName) {
  102. let names = fullName.split('.'), decimal = this.Decimal;
  103. for (let name of names) {
  104. if (decimal[name]) {
  105. decimal = decimal[name];
  106. } else {
  107. return null;
  108. }
  109. }
  110. if (Object.prototype.toString.apply(decimal) === '[object Number]') {
  111. return decimal;
  112. } else {
  113. return null;
  114. }
  115. };
  116. project.prototype.loadMainTree = function () {
  117. var that = this;
  118. let loadRationGLJNode = function (cacheNode) {
  119. var newNode, bj = that.ration_glj.getMainAndEquGLJ(cacheNode.source.ID), i;
  120. for(i=0;i<bj.length;i++){
  121. that.ration_glj.transferToNodeData(bj[i]);
  122. newNode = that.mainTree.addNode(cacheNode, null, bj[i].ID);
  123. newNode.source = bj[i];
  124. newNode.sourceType = that.ration_glj.getSourceType();
  125. newNode.data = bj[i];
  126. }
  127. };
  128. var loadRationNode = function (rations, cacheNode) {
  129. var newNode, br = that.Ration.getBillsSortRation(cacheNode.source.getID()), i;
  130. for (i = 0; i < br.length; i++) {
  131. newNode = that.mainTree.addNode(cacheNode, null, br[i].ID);
  132. if(br[i].type==rationType.gljRation){
  133. br[i]= that.ration_glj.combineRationAndGLJ(br[i]);
  134. }
  135. newNode.source = br[i];
  136. newNode.sourceType = that.Ration.getSourceType();
  137. newNode.data = br[i];
  138. if(projectInfoObj.projectInfo.property.displaySetting !== undefined &&
  139. projectInfoObj.projectInfo.property.displaySetting.disPlayMainMaterial==true){
  140. loadRationGLJNode(newNode);
  141. }
  142. }
  143. };
  144. /* let loadVolumePriceNode = function (cacheNode) {
  145. let newNode = null, bv = that.VolumePrice.getBillsSortVolumePrice(cacheNode.source.getID());
  146. for (let v of bv) {
  147. newNode = that.mainTree.addNode(cacheNode);
  148. newNode.source = v;
  149. newNode.sourceType = that.VolumePrice.getSourceType();
  150. newNode.data = v;
  151. }
  152. };*/
  153. var loadIdTreeNode = function (nodes, parent) {
  154. var newNode, i;
  155. for (i = 0; i < nodes.length; i++) {
  156. newNode = that.mainTree.addNode(parent, null, nodes[i].data.ID);
  157. newNode.source = nodes[i];
  158. newNode.sourceType = that.Bills.getSourceType();
  159. newNode.data = nodes[i].data;
  160. that.FeeRate.loadFeeRateToBill(newNode);
  161. if (nodes[i].children.length === 0) {
  162. loadRationNode(that.Ration.datas, newNode);
  163. // loadVolumePriceNode(newNode);
  164. } else {
  165. loadIdTreeNode(nodes[i].children, newNode);
  166. }
  167. }
  168. };
  169. loadIdTreeNode(this.Bills.tree.roots, null);
  170. this.mainTree.sortTreeItems();
  171. this.mainTree.selected = this.mainTree.firstNode();
  172. };
  173. project.prototype.getParentTarget = function (node, targetField, targetValue) {
  174. var parent = node;
  175. while (parent && parent[targetField] !== targetValue) {
  176. parent = parent.parent;
  177. }
  178. return parent;
  179. };
  180. // 提供给各模块调用的统一从后台获取数据的方法
  181. /*project.prototype.pullData = function (url, data, successCallback, errorCallback) {
  182. $.ajax({
  183. type:"POST",
  184. url: url,
  185. data: {'data': JSON.stringify(data)},
  186. dataType: 'json',
  187. cache: false,
  188. timeout: 50000,
  189. success: function(result){
  190. successCallback(result);
  191. },
  192. error: function(jqXHR, textStatus, errorThrown){
  193. alert('error ' + textStatus + " " + errorThrown);
  194. errorCallback();
  195. }
  196. });
  197. };*/
  198. // 所有模块在此从后台获取数据
  199. project.prototype.loadDatas = function (callback){
  200. $.ajax({
  201. type: "POST",
  202. url: '/project/getData',
  203. data: {'data': JSON.stringify({
  204. "project_id": tools._ID,
  205. "user_id": tools._userID
  206. })},
  207. dataType: 'json',
  208. cache: false,
  209. timeout: 50000,
  210. success: function (result) {
  211. if (!result.error) {
  212. tools.doAfterLoad(result.data, callback);
  213. // for test calc
  214. //tools.doAfterLoad([{moduleName: 'bills', data: BillsData}, {'moduleName': 'ration', data: DrawingData}], callback);
  215. } else {
  216. alert('error: ' + result.message);
  217. callback(result.error);
  218. }
  219. },
  220. error: function(jqXHR, textStatus, errorThrown){
  221. alert('error ' + textStatus + " " + errorThrown);
  222. }
  223. });
  224. };
  225. project.prototype.beginUpdate = function(operation){
  226. if (tools.updateLock === 0){
  227. tools.operation = operation
  228. }
  229. tools.updateLock += 1;
  230. };
  231. project.prototype.endUpdate = function(){
  232. if (tools.updateLock === 0){
  233. throw "project can not endUpdate before beginUpdate";
  234. }
  235. tools.updateLock -= 1;
  236. if (tools.updateLock === 0) {
  237. $.ajax({
  238. type: "POST",
  239. url: '/project/save',
  240. data: {'data': JSON.stringify({
  241. "project_id": tools._ID,
  242. "user_id": tools._userID,
  243. "date": new Date,
  244. "operation": tools.operation,
  245. "update_data": tools.updateData
  246. })},
  247. dataType: 'json',
  248. cache: false,
  249. timeout: 50000,
  250. success: function (result) {
  251. if (!result.error) {
  252. tools.doAfterUpdate(result.data);
  253. } else {
  254. alert('error: ' + result.message);
  255. }
  256. $.bootstrapLoading.end();
  257. },
  258. error: function(jqXHR, textStatus, errorThrown){
  259. alert('error ' + textStatus + " " + errorThrown);
  260. $.bootstrapLoading.end();
  261. }
  262. });
  263. tools.updateData = [];
  264. tools.operation = "";
  265. }
  266. };
  267. project.prototype.push = function(moduleName, data){
  268. if (tools.updateLock === 0){
  269. throw "project can not push data before beginUpdate";
  270. }
  271. var moduleData = {
  272. moduleName: moduleName,
  273. data: data
  274. };
  275. tools.updateData.push(moduleData);
  276. };
  277. project.prototype.pushNow = function (operation, moduleName, data) {
  278. var that = this;
  279. this.beginUpdate(operation);
  280. if (Object.prototype.toString.apply(moduleName) === "[object Array]" && Object.prototype.toString.apply(data) === "[object Array]") {
  281. moduleName.forEach(function (name, index) {
  282. if(name != 'projCounter'){
  283. that.push(moduleName[index], data[index]);
  284. }
  285. });
  286. } else {
  287. this.push(moduleName, data);
  288. }
  289. this.endUpdate();
  290. };
  291. project.prototype.registerModule = function(moduleName, obj){
  292. if (!tools.modules.hasOwnProperty(moduleName)){
  293. tools.modules[moduleName] = obj;
  294. }
  295. };
  296. project.prototype.markUpdateProject = function (data,type) {
  297. CommonAjax.post("/project/markUpdateProject",{updateInfo:data,type:type});
  298. };
  299. project.prototype.projectMarkChecking = function () {
  300. let changeMark = projectInfoObj.projectInfo.changeMark;
  301. if(changeMark&&changeMark!=''){
  302. $.bootstrapLoading.start();
  303. this.Bills.getEngineeringCostNode(projectObj.mainController).changed = true;
  304. this.calcProgram.calcAllNodesAndSave();
  305. CommonAjax.post("/project/removeProjectMark",{projectID:this.ID()},function (data) {
  306. delete projectInfoObj.projectInfo.changeMark;
  307. });
  308. }
  309. };
  310. /* project.prototype.setBillsCalcMode = function (calcMode) {
  311. this.property.billsCalcMode = calcMode;
  312. this.initCalcFields();
  313. };*/
  314. /*project.prototype.initCalcFields = function () {
  315. // let settingConst = this.projSetting.settingConst;
  316. if (this.calcFields) {
  317. for (let field of this.calcFields) {
  318. // unitFeeCalcFlag
  319. if (field.type === 'zangu') {
  320. field.unitFeeFlag = converseUnitFeeFlag;
  321. } else {
  322. // if (this.projSetting.billsCalcMode === settingConst.billsCalcMode.rationContent) {
  323. if (this.property.billsCalcMode === leafBillGetFeeType.rationContent) {
  324. field.unitFeeFlag = rationContentUnitFeeFlag;
  325. // } else if ( this.projSetting.billsCalcMode === settingConst.billsCalcMode.billsPrice) {
  326. } else if ( this.property.billsCalcMode === leafBillGetFeeType.billsPrice) {
  327. field.unitFeeFlag = billsPriceUnitFeeFlag;
  328. } else {
  329. field.unitFeeFlag = averageQtyUnitFeeFlag;
  330. }
  331. }
  332. // totalFeeCalcFlag
  333. if (field.type === 'common') {
  334. // if (this.projSetting.billsCalcMode === settingConst.billsCalcMode.rationPriceConverse) {
  335. if (this.property.billsCalcMode === leafBillGetFeeType.rationPriceConverse) {
  336. field.totalFeeFlag = sumTotalFeeFlag;
  337. } else {
  338. field.totalFeeFlag = totalFeeFlag;
  339. }
  340. } else {
  341. field.totalFeeFlag = sumTotalFeeFlag;
  342. }
  343. }
  344. }
  345. }*/
  346. return new project();
  347. }
  348. };