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