project.js 25 KB

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