project.js 24 KB

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