project.js 25 KB

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