project.js 25 KB

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