project.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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: parseInt(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. //必须要先load ProjectInfo的信息
  34. let projectInfoModule = result.find(data => data.moduleName === ModuleNames.projectInfo);
  35. if (projectInfoModule) {
  36. me._project.projectInfo = projectInfoModule.data;
  37. }
  38. result.forEach(function(item){
  39. if (item.moduleName !== ModuleNames.projectInfo) {
  40. if (me.modules[item.moduleName]){
  41. me.modules[item.moduleName].loadData(item.data);
  42. } else if (item.moduleName === me.projCounter) {
  43. counter = item.data;
  44. } else if (item.moduleName === me.projSetting) {
  45. me._project.projSetting = item.data;
  46. me._project.projSetting.moduleName = me.projSetting;
  47. } else if(item.moduleName === ModuleNames.projectGLJ){
  48. me._project.projectGLJ.loadToCache(item.data);
  49. }
  50. }
  51. });
  52. for (module in counter) {
  53. if (me.modules[module]) {
  54. me.modules[module].setMaxID(counter[module]);
  55. }
  56. }
  57. me._project.loadMainTree();
  58. //me.test(result[0].data[0]);
  59. callback(0);
  60. };
  61. tools.eachItem=function(item){
  62. if (me.modules[item.moduleName]){
  63. me.modules[item.moduleName].doAfterUpdate(item.err, item.data);
  64. }
  65. };
  66. /*tools.test = function(data){
  67. me._project.beginUpdate('修改名称');
  68. data.name = 'test';
  69. data['updateType'] = 'ut_update';
  70. me._project.push(ModuleNames.bills, [data]);
  71. me._project.endUpdate();
  72. };*/
  73. // 所有通过this访问的属性,都不应在此单元外部进行写入操作
  74. var project = function () {
  75. tools._project = this;
  76. this.mainTree = cacheTree.createNew(this);
  77. this.Bills = Bills.createNew(this);
  78. this.Ration = Ration.createNew(this);
  79. this.GLJ = GLJ.createNew(this);
  80. this.ration_glj = ration_glj.createNew(this);
  81. this.ration_coe = ration_coe.createNew(this);
  82. this.ration_ass = ration_ass.createNew(this);
  83. this.ration_installation = ration_installation.createNew(this);
  84. this.ration_template = ration_template.createNew(this);
  85. this.quantity_detail = quantity_detail.createNew(this);
  86. this.installation_fee = installation_fee.createNew(this);
  87. this.FeeRate = FeeRate.createNew(this);
  88. // this.VolumePrice = VolumePrice.createNew(this);
  89. this.projectGLJ = new ProjectGLJ();
  90. // this.projectGLJ.loadData();
  91. this.composition = new Composition();
  92. this.composition.loadData();
  93. this.labourCoe = new LabourCoe(this);
  94. this.calcProgram = new CalcProgram(this);
  95. this.calcBase = calcBase;
  96. this.divide_setting = new DivideSetting(this);
  97. // this.masterField = {ration: 'billsItemID', volumePrice: 'billsItemID'};
  98. this.masterField = {ration: 'billsItemID'};
  99. };
  100. // prototype用于定义public方法
  101. project.prototype.modify = function (modifyDatas, callback) {
  102. // To Do
  103. };
  104. // prototype用于定义public方法
  105. project.prototype.ID = function () {
  106. return tools._ID;
  107. };
  108. project.prototype.projCounter = function () {
  109. return tools.projCounter;
  110. };
  111. project.prototype.getDecimal = function (fullName) {
  112. let names = fullName.split('.'), decimal = this.Decimal;
  113. for (let name of names) {
  114. if (decimal[name]) {
  115. decimal = decimal[name];
  116. } else {
  117. return null;
  118. }
  119. }
  120. if (Object.prototype.toString.apply(decimal) === '[object Number]') {
  121. return decimal;
  122. } else {
  123. return null;
  124. }
  125. };
  126. project.prototype.loadMainTree = function () {
  127. var that = this;
  128. let loadRationGLJNode = function (cacheNode) {
  129. var newNode, bj = that.ration_glj.getMainAndEquGLJ(cacheNode.source.ID), i;
  130. for(i=0;i<bj.length;i++){
  131. that.ration_glj.transferToNodeData(bj[i]);
  132. newNode = that.mainTree.addNode(cacheNode, null, bj[i].ID);
  133. newNode.source = bj[i];
  134. newNode.sourceType = that.ration_glj.getSourceType();
  135. newNode.data = bj[i];
  136. }
  137. };
  138. var loadRationNode = function (rations, cacheNode) {
  139. var newNode, br = that.Ration.getBillsSortRation(cacheNode.source.getID()), i;
  140. for (i = 0; i < br.length; i++) {
  141. newNode = that.mainTree.addNode(cacheNode, null, br[i].ID);
  142. if(br[i].type==rationType.gljRation){
  143. br[i]= that.ration_glj.combineRationAndGLJ(br[i]);
  144. }
  145. newNode.source = br[i];
  146. newNode.sourceType = that.Ration.getSourceType();
  147. newNode.data = br[i];
  148. if(projectObj.project.projectInfo.property.displaySetting !== undefined &&
  149. projectObj.project.projectInfo.property.displaySetting.disPlayMainMaterial==true){
  150. loadRationGLJNode(newNode);
  151. }
  152. }
  153. };
  154. /* let loadVolumePriceNode = function (cacheNode) {
  155. let newNode = null, bv = that.VolumePrice.getBillsSortVolumePrice(cacheNode.source.getID());
  156. for (let v of bv) {
  157. newNode = that.mainTree.addNode(cacheNode);
  158. newNode.source = v;
  159. newNode.sourceType = that.VolumePrice.getSourceType();
  160. newNode.data = v;
  161. }
  162. };*/
  163. var loadIdTreeNode = function (nodes, parent) {
  164. var newNode, i;
  165. for (i = 0; i < nodes.length; i++) {
  166. newNode = that.mainTree.addNode(parent, null, nodes[i].data.ID);
  167. newNode.source = nodes[i];
  168. newNode.sourceType = that.Bills.getSourceType();
  169. newNode.data = nodes[i].data;
  170. that.FeeRate.loadFeeRateToBill(newNode);
  171. if (nodes[i].children.length === 0) {
  172. loadRationNode(that.Ration.datas, newNode);
  173. // loadVolumePriceNode(newNode);
  174. } else {
  175. loadIdTreeNode(nodes[i].children, newNode);
  176. }
  177. }
  178. };
  179. loadIdTreeNode(this.Bills.tree.roots, null);
  180. this.mainTree.sortTreeItems();
  181. this.mainTree.selected = this.mainTree.firstNode();
  182. };
  183. project.prototype.getParentTarget = function (node, targetField, targetValue) {
  184. var parent = node;
  185. while (parent && parent[targetField] !== targetValue) {
  186. parent = parent.parent;
  187. }
  188. return parent;
  189. };
  190. // 提供给各模块调用的统一从后台获取数据的方法
  191. /*project.prototype.pullData = function (url, data, successCallback, errorCallback) {
  192. $.ajax({
  193. type:"POST",
  194. url: url,
  195. data: {'data': JSON.stringify(data)},
  196. dataType: 'json',
  197. cache: false,
  198. timeout: 50000,
  199. success: function(result){
  200. successCallback(result);
  201. },
  202. error: function(jqXHR, textStatus, errorThrown){
  203. alert('error ' + textStatus + " " + errorThrown);
  204. errorCallback();
  205. }
  206. });
  207. };*/
  208. // 所有模块在此从后台获取数据
  209. project.prototype.loadDatas = function (callback){
  210. $.ajax({
  211. type: "POST",
  212. url: '/project/getData',
  213. data: {'data': JSON.stringify({
  214. "project_id": tools._ID,
  215. "user_id": tools._userID
  216. })},
  217. dataType: 'json',
  218. cache: false,
  219. timeout: 50000,
  220. success: function (result) {
  221. if (!result.error) {
  222. tools.doAfterLoad(result.data, callback);
  223. // for test calc
  224. //tools.doAfterLoad([{moduleName: 'bills', data: BillsData}, {'moduleName': 'ration', data: DrawingData}], callback);
  225. } else {
  226. alert('error: ' + result.message);
  227. callback(result.error);
  228. }
  229. },
  230. error: function(jqXHR, textStatus, errorThrown){
  231. alert('error ' + textStatus + " " + errorThrown);
  232. }
  233. });
  234. };
  235. project.prototype.beginUpdate = function(operation){
  236. if (tools.updateLock === 0){
  237. tools.operation = operation
  238. }
  239. tools.updateLock += 1;
  240. };
  241. project.prototype.endUpdate = function(){
  242. if (tools.updateLock === 0){
  243. throw "project can not endUpdate before beginUpdate";
  244. }
  245. tools.updateLock -= 1;
  246. if (tools.updateLock === 0) {
  247. $.ajax({
  248. type: "POST",
  249. url: '/project/save',
  250. data: {'data': JSON.stringify({
  251. "project_id": tools._ID,
  252. "user_id": tools._userID,
  253. "date": new Date,
  254. "operation": tools.operation,
  255. "update_data": tools.updateData
  256. })},
  257. dataType: 'json',
  258. cache: false,
  259. timeout: 50000,
  260. success: function (result) {
  261. if (!result.error) {
  262. tools.doAfterUpdate(result.data);
  263. } else {
  264. alert('error: ' + result.message);
  265. }
  266. $.bootstrapLoading.end();
  267. },
  268. error: function(jqXHR, textStatus, errorThrown){
  269. alert('error ' + textStatus + " " + errorThrown);
  270. $.bootstrapLoading.end();
  271. }
  272. });
  273. tools.updateData = [];
  274. tools.operation = "";
  275. }
  276. };
  277. project.prototype.push = function(moduleName, data){
  278. if (tools.updateLock === 0){
  279. throw "project can not push data before beginUpdate";
  280. }
  281. var moduleData = {
  282. moduleName: moduleName,
  283. data: data
  284. };
  285. tools.updateData.push(moduleData);
  286. };
  287. project.prototype.pushNow = function (operation, moduleName, data) {
  288. var that = this;
  289. this.beginUpdate(operation);
  290. if (Object.prototype.toString.apply(moduleName) === "[object Array]" && Object.prototype.toString.apply(data) === "[object Array]") {
  291. moduleName.forEach(function (name, index) {
  292. if(name != 'projCounter'){
  293. that.push(moduleName[index], data[index]);
  294. }
  295. });
  296. } else {
  297. this.push(moduleName, data);
  298. }
  299. this.endUpdate();
  300. };
  301. project.prototype.registerModule = function(moduleName, obj){
  302. if (!tools.modules.hasOwnProperty(moduleName)){
  303. tools.modules[moduleName] = obj;
  304. }
  305. };
  306. project.prototype.markUpdateProject = function (data,type,callback) {
  307. CommonAjax.post("/project/markUpdateProject",{updateInfo:data,type:type},callback);
  308. };
  309. project.prototype.saveProperty = function (propertyName, propertyValue) {
  310. CommonAjax.post("/project/saveProperty", {projectID: this.ID(), propertyName: propertyName, propertyValue: propertyValue});
  311. };
  312. project.prototype.projectMarkChecking = function () {
  313. let me = this;
  314. let changeMark = projectObj.project.projectInfo.changeMark;
  315. if(changeMark&&changeMark!=''){
  316. let totalNode = this.Bills.getTotalCostNode(projectObj.mainController);
  317. if(totalNode) totalNode.changed = true;
  318. this.calcProgram.calcAllNodesAndSave();
  319. CommonAjax.post("/project/removeProjectMark",{projectID:me.ID()},function (data) {
  320. delete projectObj.project.projectInfo.changeMark;
  321. if(socket.connected == true){//如果这时候socket已经连接成功,则发送删除标记广播
  322. socket.emit('removeProjectMark',{projectID:me.ID(),userID:userID});
  323. }else {//如果还没连接成功,则放入消息对象缓存,等socket连接成功后发送
  324. socketObject.messages.push({message:'removeProjectMark',data:{projectID:me.ID(),userID:userID}});
  325. }
  326. });
  327. }
  328. };
  329. project.prototype.updateLockBills = function (value,callback) {
  330. let mixDatas = {
  331. projectID: projectObj.project.ID(),
  332. updateType: 'update',
  333. properties: {
  334. "property.lockBills":value
  335. },
  336. labourCoes: {},
  337. rations: [],
  338. bills: []
  339. };
  340. $.bootstrapLoading.start();
  341. CommonAjax.post('/pm/api/updateMixDatas', {user_id: userID, mixDataArr: mixDatas}, function (rstData) {
  342. projectObj.project.projectInfo.property.lockBills = value;
  343. if(callback){
  344. callback();
  345. }
  346. $.bootstrapLoading.end();
  347. });
  348. };
  349. //返回清单是否被锁定
  350. project.prototype.isBillsLocked =function(){
  351. if(projectObj.project.projectInfo.property.lockBills == true){
  352. return true;
  353. }
  354. return false;
  355. };
  356. // 清单是否属于锁定范围(所有清单)
  357. project.prototype.withinBillsLocked = function (node) {
  358. return node && node.sourceType === commonConstants.SourceType.BILLS;
  359. };
  360. project.prototype.updateNodes = function (datas,callback) {
  361. /* let datas = [
  362. {
  363. type:'ration',
  364. data:{
  365. projectID:1605,
  366. ID:"7b962fb0-1131-11e8-b3da-af725dadd7ae",
  367. name:'testRation'
  368. }
  369. },
  370. {
  371. type:'bills',
  372. data:{
  373. projectID:1605,
  374. ID:"af9f0081-1127-11e8-99a8-2fc02230b6e7",
  375. name:'安全文明施工专项费用123'
  376. }
  377. }
  378. ]*/
  379. CommonAjax.post("/project/updateNodes",datas,function (data) {
  380. if(callback){
  381. callback(data);
  382. }
  383. })
  384. };
  385. project.prototype.syncUpdateNodesAndRefresh =async function (datas) {
  386. let me = this;
  387. return new Promise(function (resolve, reject) {
  388. me.updateNodes(datas,function (result) {
  389. let nodes = me.updateNodesCache(result);
  390. projectObj.mainController.refreshTreeNode(nodes);
  391. resolve(nodes);
  392. });
  393. });
  394. };
  395. project.prototype.updateNodesCache =function (datas) {
  396. let refreshNode = [];
  397. let reclacQuantity = false;
  398. let deleteNode=[],addNodeDatas=[];
  399. for(let d of datas){
  400. let temObj = null;
  401. if(d.type == ModuleNames.bills || d.type == ModuleNames.ration){//如果是树节点类型,直接取树节点更新
  402. if(d.action =="add"){
  403. if(d.type == ModuleNames.ration) this.Ration.datas.push(d.data);
  404. reclacQuantity = true;
  405. addNodeDatas.push(d);
  406. }else {
  407. let temNode = this.mainTree.getNodeByID(d.data.ID);
  408. if(temNode){
  409. if(d.action =="delete"){
  410. if(d.type == ModuleNames.ration){
  411. _.remove(this.Ration.datas,{'ID':d.data.ID});
  412. this.Ration.deleteSubListOfRation({ID:d.data.ID});
  413. reclacQuantity = true;
  414. }
  415. deleteNode.push(temNode);//对于删除节点,统一不加入refreshNode中,由外部加入,避免重复
  416. }else {
  417. temObj = temNode.data;
  418. if(gljUtil.isDef(d.data.quantity))reclacQuantity = true;
  419. refreshNode.push(temNode);
  420. }
  421. }
  422. }
  423. }else if(d.type == ModuleNames.project){
  424. temObj = this;
  425. }else if (d.type == ModuleNames.ration_glj && d.action == "add"){
  426. this[d.type].datas.push(d.data);
  427. if(d.projectGLJ) this.projectGLJ.loadNewProjectGLJToCache(d.projectGLJ);
  428. } else {//其它类型,更新datas
  429. temObj = _.find(this[d.type].datas,{"ID":d.data.ID});
  430. }
  431. if(temObj){
  432. for(let key in d.data){
  433. if(key == 'ID' || key == 'id'){
  434. continue;
  435. }
  436. this.setValue(temObj,key,d.data[key])
  437. }
  438. }
  439. }
  440. //对树节点的操作并删除、添加清单、删除添加定额、删除对应的定额工料机缓存
  441. TREE_SHEET_HELPER.massOperationSheet(projectObj.mainController.sheet, function () {
  442. deleteTreeNodes(deleteNode);
  443. addNewNodes(addNodeDatas,refreshNode);
  444. });
  445. if(reclacQuantity) this.projectGLJ.calcQuantity();
  446. return refreshNode;
  447. function deleteTreeNodes(deleteNodes) {
  448. let controller = projectObj.mainController, project = projectObj.project;
  449. let Bill = project.Bills, Ration = project.Ration;
  450. for(let rd of deleteNodes){
  451. controller.sheet.deleteRows(rd.serialNo(),1);
  452. controller.tree.delete(rd);
  453. if(rd.sourceType == Bill.getSourceType()) Bill.tree.delete(rd.source);
  454. }
  455. }
  456. function addNewNodes(newDatas,refreshNode) {
  457. let controller = projectObj.mainController;
  458. let Bill = projectObj.project.Bills;
  459. let newAddNode = [];
  460. for(let nr of newDatas){
  461. let nextID = -1;
  462. let preNode = projectObj.project.mainTree.getNodeByID(nr.preSiblingID);
  463. if(preNode) nextID = preNode.getNextSiblingID();
  464. let newNode = projectObj.project.mainTree.insert(nr.parentID, nextID, nr.data.ID);
  465. if(nr.type == ModuleNames.bills){
  466. let newSource = Bill.tree.insertByData(nr.data, nr.ParentID, nextID, true);
  467. newNode.source = newSource;
  468. }else {
  469. newNode.source = nr.data;
  470. }
  471. newNode.sourceType = nr.type;
  472. newNode.data = nr.data;
  473. controller.sheet.addRows(newNode.serialNo(), 1);
  474. controller.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  475. newAddNode.push(newNode);
  476. refreshNode.push(newNode);
  477. }
  478. TREE_SHEET_HELPER.refreshTreeNodeData(controller.setting, controller.sheet, newAddNode, false);
  479. }
  480. };
  481. project.prototype.setValue=function (obj,key,value) {
  482. let keyArray = key.split('.');
  483. t_set(obj,value,keyArray,0);
  484. function t_set(obj,value,arr,index) {
  485. let nextIndex = index + 1;
  486. let tkey = arr[index];
  487. if(nextIndex < arr.length){
  488. if(obj[tkey]==undefined){
  489. obj[tkey] = {};
  490. }
  491. t_set(obj[tkey],value,arr,nextIndex)
  492. }else if(nextIndex == arr.length){
  493. obj[tkey] = value;
  494. }
  495. }
  496. }
  497. project.prototype.updateEvalproject = function (evalProject,data,callback){
  498. let programID = null;
  499. if(evalProject ==1){//当打勾估价项目选项后,该定额取费专业读取计算程序名称为“估价项目”的计算程序,且为只读状态
  500. programID = this.calcProgram.compiledTemplateMaps["估价项目"];
  501. if(callback) callback(programID);
  502. }else {//如果evalProject==0,则是去掉勾选估价项目选项,需要获取定额默认的取费专业。
  503. let libID = data.from == 'cpt'?"compleRationLib":data.libID;
  504. let query = {code:data.code,projectID:data.projectID,libID:libID};
  505. CommonAjax.post("/ration/getDefaultProgramID",query,function (result) {
  506. if(callback) callback(result);
  507. })
  508. }
  509. };
  510. //判断项目是否安装工程
  511. project.prototype.isInstall = function () {
  512. return projectObj.project.projectInfo.property.isInstall?true:false;//如果是undefinded 就也返回false
  513. };
  514. /* project.prototype.setBillsCalcMode = function (calcMode) {
  515. this.property.billsCalcMode = calcMode;
  516. this.initCalcFields();
  517. };*/
  518. /*project.prototype.initCalcFields = function () {
  519. // let settingConst = this.projSetting.settingConst;
  520. if (this.calcFields) {
  521. for (let field of this.calcFields) {
  522. // unitFeeCalcFlag
  523. if (field.type === 'zangu') {
  524. field.unitFeeFlag = converseUnitFeeFlag;
  525. } else {
  526. // if (this.projSetting.billsCalcMode === settingConst.billsCalcMode.rationContent) {
  527. if (this.property.billsCalcMode === leafBillGetFeeType.rationContent) {
  528. field.unitFeeFlag = rationContentUnitFeeFlag;
  529. // } else if ( this.projSetting.billsCalcMode === settingConst.billsCalcMode.billsPrice) {
  530. } else if ( this.property.billsCalcMode === leafBillGetFeeType.billsPrice) {
  531. field.unitFeeFlag = billsPriceUnitFeeFlag;
  532. } else {
  533. field.unitFeeFlag = averageQtyUnitFeeFlag;
  534. }
  535. }
  536. // totalFeeCalcFlag
  537. if (field.type === 'common') {
  538. // if (this.projSetting.billsCalcMode === settingConst.billsCalcMode.rationPriceConverse) {
  539. if (this.property.billsCalcMode === leafBillGetFeeType.rationPriceConverse) {
  540. field.totalFeeFlag = sumTotalFeeFlag;
  541. } else {
  542. field.totalFeeFlag = totalFeeFlag;
  543. }
  544. } else {
  545. field.totalFeeFlag = sumTotalFeeFlag;
  546. }
  547. }
  548. }
  549. }*/
  550. return new project();
  551. }
  552. };