project.js 26 KB

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