project.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. callback(0);
  60. };
  61. tools.eachItem=function(item){
  62. if (me.modules[item.moduleName]){
  63. me.modules[item.moduleName].doAfterUpdate(item.err, item.data);
  64. }
  65. };
  66. /*tools.test = function(data){
  67. me._project.beginUpdate('修改名称');
  68. data.name = 'test';
  69. data['updateType'] = 'ut_update';
  70. me._project.push(ModuleNames.bills, [data]);
  71. me._project.endUpdate();
  72. };*/
  73. // 所有通过this访问的属性,都不应在此单元外部进行写入操作
  74. var project = function () {
  75. tools._project = this;
  76. this.mainTree = cacheTree.createNew(this);
  77. this.Bills = Bills.createNew(this);
  78. this.Ration = Ration.createNew(this);
  79. this.GLJ = GLJ.createNew(this);
  80. this.ration_glj = ration_glj.createNew(this);
  81. this.ration_coe = ration_coe.createNew(this);
  82. this.ration_ass = ration_ass.createNew(this);
  83. this.ration_installation = ration_installation.createNew(this);
  84. this.ration_template = ration_template.createNew(this);
  85. this.quantity_detail = quantity_detail.createNew(this);
  86. this.installation_fee = installation_fee.createNew(this);
  87. this.FeeRate = FeeRate.createNew(this);
  88. // this.VolumePrice = VolumePrice.createNew(this);
  89. this.projectGLJ = new ProjectGLJ();
  90. // this.projectGLJ.loadData();
  91. this.composition = new Composition();
  92. this.composition.loadData();
  93. this.labourCoe = new LabourCoe(this);
  94. this.calcProgram = new CalcProgram(this);
  95. this.calcBase = calcBase;
  96. this.divide_setting = new DivideSetting(this);
  97. this.evaluate_list = new EvaluateList(this);
  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.beginUpdate = function(operation){
  237. if (tools.updateLock === 0){
  238. tools.operation = operation
  239. }
  240. tools.updateLock += 1;
  241. };
  242. project.prototype.endUpdate = function(){
  243. if (tools.updateLock === 0){
  244. throw "project can not endUpdate before beginUpdate";
  245. }
  246. tools.updateLock -= 1;
  247. if (tools.updateLock === 0) {
  248. $.ajax({
  249. type: "POST",
  250. url: '/project/save',
  251. data: {'data': JSON.stringify({
  252. "project_id": tools._ID,
  253. "user_id": tools._userID,
  254. "date": new Date,
  255. "operation": tools.operation,
  256. "update_data": tools.updateData
  257. })},
  258. dataType: 'json',
  259. cache: false,
  260. timeout: 50000,
  261. success: function (result) {
  262. if (!result.error) {
  263. tools.doAfterUpdate(result.data);
  264. } else {
  265. alert('error: ' + result.message);
  266. }
  267. $.bootstrapLoading.end();
  268. },
  269. error: function(jqXHR, textStatus, errorThrown){
  270. alert('error ' + textStatus + " " + errorThrown);
  271. $.bootstrapLoading.end();
  272. }
  273. });
  274. tools.updateData = [];
  275. tools.operation = "";
  276. }
  277. };
  278. project.prototype.push = function(moduleName, data){
  279. if (tools.updateLock === 0){
  280. throw "project can not push data before beginUpdate";
  281. }
  282. var moduleData = {
  283. moduleName: moduleName,
  284. data: data
  285. };
  286. tools.updateData.push(moduleData);
  287. };
  288. project.prototype.pushNow = function (operation, moduleName, data) {
  289. var that = this;
  290. this.beginUpdate(operation);
  291. if (Object.prototype.toString.apply(moduleName) === "[object Array]" && Object.prototype.toString.apply(data) === "[object Array]") {
  292. moduleName.forEach(function (name, index) {
  293. if(name != 'projCounter'){
  294. that.push(moduleName[index], data[index]);
  295. }
  296. });
  297. } else {
  298. this.push(moduleName, data);
  299. }
  300. this.endUpdate();
  301. };
  302. project.prototype.registerModule = function(moduleName, obj){
  303. if (!tools.modules.hasOwnProperty(moduleName)){
  304. tools.modules[moduleName] = obj;
  305. }
  306. };
  307. project.prototype.markUpdateProject = function (data,type,callback) {
  308. CommonAjax.post("/project/markUpdateProject",{updateInfo:data,type:type},callback);
  309. };
  310. project.prototype.saveProperty = function (propertyName, propertyValue) {
  311. CommonAjax.post("/project/saveProperty", {projectID: this.ID(), propertyName: propertyName, propertyValue: propertyValue});
  312. };
  313. project.prototype.projectMarkChecking = function () {
  314. let me = this;
  315. let changeMark = projectObj.project.projectInfo.changeMark;
  316. if(changeMark&&changeMark!=''){
  317. let totalNode = this.Bills.getTotalCostNode(projectObj.mainController);
  318. if(totalNode) totalNode.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. return node && node.sourceType === commonConstants.SourceType.BILLS;
  360. };
  361. project.prototype.updateNodes = function (datas,callback) {
  362. /* let datas = [
  363. {
  364. type:'ration',
  365. data:{
  366. projectID:1605,
  367. ID:"7b962fb0-1131-11e8-b3da-af725dadd7ae",
  368. name:'testRation'
  369. }
  370. },
  371. {
  372. type:'bills',
  373. data:{
  374. projectID:1605,
  375. ID:"af9f0081-1127-11e8-99a8-2fc02230b6e7",
  376. name:'安全文明施工专项费用123'
  377. }
  378. }
  379. ]*/
  380. CommonAjax.post("/project/updateNodes",datas,function (data) {
  381. if(callback){
  382. callback(data);
  383. }
  384. })
  385. };
  386. project.prototype.syncUpdateNodesAndRefresh =async function (datas) {
  387. let me = this;
  388. return new Promise(function (resolve, reject) {
  389. me.updateNodes(datas,function (result) {
  390. let nodes = me.updateNodesCache(result);
  391. projectObj.mainController.refreshTreeNode(nodes);
  392. resolve(nodes);
  393. });
  394. });
  395. };
  396. project.prototype.updateNodesCache =function (datas) {
  397. let refreshNode = [];
  398. let reclacQuantity = false;
  399. let deleteNode=[],addNodeDatas=[];
  400. for(let d of datas){
  401. let temObj = null;
  402. if(d.type == ModuleNames.bills || d.type == ModuleNames.ration){//如果是树节点类型,直接取树节点更新
  403. if(d.action =="add"){
  404. if(d.type == ModuleNames.ration) this.Ration.datas.push(d.data);
  405. reclacQuantity = true;
  406. addNodeDatas.push(d);
  407. }else {
  408. let temNode = this.mainTree.getNodeByID(d.data.ID);
  409. if(temNode){
  410. if(d.action =="delete"){
  411. if(d.type == ModuleNames.ration){
  412. _.remove(this.Ration.datas,{'ID':d.data.ID});
  413. this.Ration.deleteSubListOfRation({ID:d.data.ID});
  414. reclacQuantity = true;
  415. }
  416. deleteNode.push(temNode);//对于删除节点,统一不加入refreshNode中,由外部加入,避免重复
  417. }else {
  418. temObj = temNode.data;
  419. if(gljUtil.isDef(d.data.quantity))reclacQuantity = true;
  420. refreshNode.push(temNode);
  421. }
  422. }
  423. }
  424. }else if(d.type == ModuleNames.project){
  425. temObj = this;
  426. }else if (d.type == ModuleNames.ration_glj && d.action == "add"){
  427. this[d.type].datas.push(d.data);
  428. if(d.projectGLJ) this.projectGLJ.loadNewProjectGLJToCache(d.projectGLJ);
  429. } else {//其它类型,更新datas
  430. temObj = _.find(this[d.type].datas,{"ID":d.data.ID});
  431. }
  432. if(temObj){
  433. for(let key in d.data){
  434. if(key == 'ID' || key == 'id'){
  435. continue;
  436. }
  437. this.setValue(temObj,key,d.data[key])
  438. }
  439. }
  440. }
  441. //对树节点的操作并删除、添加清单、删除添加定额、删除对应的定额工料机缓存
  442. TREE_SHEET_HELPER.massOperationSheet(projectObj.mainController.sheet, function () {
  443. deleteTreeNodes(deleteNode);
  444. addNewNodes(addNodeDatas,refreshNode);
  445. });
  446. if(reclacQuantity) this.projectGLJ.calcQuantity();
  447. return refreshNode;
  448. function deleteTreeNodes(deleteNodes) {
  449. let controller = projectObj.mainController, project = projectObj.project;
  450. let Bill = project.Bills, Ration = project.Ration;
  451. for(let rd of deleteNodes){
  452. controller.sheet.deleteRows(rd.serialNo(),1);
  453. controller.tree.delete(rd);
  454. if(rd.sourceType == Bill.getSourceType()) Bill.tree.delete(rd.source);
  455. }
  456. }
  457. function addNewNodes(newDatas,refreshNode) {
  458. let controller = projectObj.mainController;
  459. let Bill = projectObj.project.Bills;
  460. let newAddNode = [];
  461. for(let nr of newDatas){
  462. let nextID = -1;
  463. let preNode = projectObj.project.mainTree.getNodeByID(nr.preSiblingID);
  464. if(preNode) nextID = preNode.getNextSiblingID();
  465. let newNode = projectObj.project.mainTree.insert(nr.parentID, nextID, nr.data.ID);
  466. if(nr.type == ModuleNames.bills){
  467. let newSource = Bill.tree.insertByData(nr.data, nr.ParentID, nextID, true);
  468. newNode.source = newSource;
  469. }else {
  470. newNode.source = nr.data;
  471. }
  472. newNode.sourceType = nr.type;
  473. newNode.data = nr.data;
  474. controller.sheet.addRows(newNode.serialNo(), 1);
  475. controller.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  476. newAddNode.push(newNode);
  477. refreshNode.push(newNode);
  478. }
  479. TREE_SHEET_HELPER.refreshTreeNodeData(controller.setting, controller.sheet, newAddNode, false);
  480. }
  481. };
  482. project.prototype.setValue=function (obj,key,value) {
  483. let keyArray = key.split('.');
  484. t_set(obj,value,keyArray,0);
  485. function t_set(obj,value,arr,index) {
  486. let nextIndex = index + 1;
  487. let tkey = arr[index];
  488. if(nextIndex < arr.length){
  489. if(obj[tkey]==undefined){
  490. obj[tkey] = {};
  491. }
  492. t_set(obj[tkey],value,arr,nextIndex)
  493. }else if(nextIndex == arr.length){
  494. obj[tkey] = value;
  495. }
  496. }
  497. }
  498. project.prototype.updateEvalproject = function (evalProject,data,callback){
  499. let programID = null;
  500. if(evalProject ==1){//当打勾估价项目选项后,该定额取费专业读取计算程序名称为“估价项目”的计算程序,且为只读状态
  501. programID = this.calcProgram.compiledTemplateMaps["估价项目"];
  502. if(callback) callback(programID);
  503. }else {//如果evalProject==0,则是去掉勾选估价项目选项,需要获取定额默认的取费专业。
  504. let libID = data.from == 'cpt'?"compleRationLib":data.libID;
  505. let query = {code:data.code,projectID:data.projectID,libID:libID};
  506. CommonAjax.post("/ration/getDefaultProgramID",query,function (result) {
  507. if(callback) callback(result);
  508. })
  509. }
  510. };
  511. //判断项目是否安装工程
  512. project.prototype.isInstall = function () {
  513. return projectObj.project.projectInfo.property.isInstall?true:false;//如果是undefinded 就也返回false
  514. };
  515. /* project.prototype.setBillsCalcMode = function (calcMode) {
  516. this.property.billsCalcMode = calcMode;
  517. this.initCalcFields();
  518. };*/
  519. /*project.prototype.initCalcFields = function () {
  520. // let settingConst = this.projSetting.settingConst;
  521. if (this.calcFields) {
  522. for (let field of this.calcFields) {
  523. // unitFeeCalcFlag
  524. if (field.type === 'zangu') {
  525. field.unitFeeFlag = converseUnitFeeFlag;
  526. } else {
  527. // if (this.projSetting.billsCalcMode === settingConst.billsCalcMode.rationContent) {
  528. if (this.property.billsCalcMode === leafBillGetFeeType.rationContent) {
  529. field.unitFeeFlag = rationContentUnitFeeFlag;
  530. // } else if ( this.projSetting.billsCalcMode === settingConst.billsCalcMode.billsPrice) {
  531. } else if ( this.property.billsCalcMode === leafBillGetFeeType.billsPrice) {
  532. field.unitFeeFlag = billsPriceUnitFeeFlag;
  533. } else {
  534. field.unitFeeFlag = averageQtyUnitFeeFlag;
  535. }
  536. }
  537. // totalFeeCalcFlag
  538. if (field.type === 'common') {
  539. // if (this.projSetting.billsCalcMode === settingConst.billsCalcMode.rationPriceConverse) {
  540. if (this.property.billsCalcMode === leafBillGetFeeType.rationPriceConverse) {
  541. field.totalFeeFlag = sumTotalFeeFlag;
  542. } else {
  543. field.totalFeeFlag = totalFeeFlag;
  544. }
  545. } else {
  546. field.totalFeeFlag = sumTotalFeeFlag;
  547. }
  548. }
  549. }
  550. }*/
  551. return new project();
  552. }
  553. };