project.js 25 KB

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