project.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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.evaluate_list = new EvaluateList(this);
  101. this.bid_evaluation_list = new BidEvaluationList(this);
  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. tools.doAfterLoad(result.data, callback);
  228. // for test calc
  229. //tools.doAfterLoad([{moduleName: 'bills', data: BillsData}, {'moduleName': 'ration', data: DrawingData}], callback);
  230. } else {
  231. alert('error: ' + result.message);
  232. callback(result.error);
  233. }
  234. },
  235. error: function(jqXHR, textStatus, errorThrown){
  236. alert('error ' + textStatus + " " + errorThrown);
  237. }
  238. });
  239. };
  240. project.prototype.beginUpdate = function(operation){
  241. if (tools.updateLock === 0){
  242. tools.operation = operation
  243. }
  244. tools.updateLock += 1;
  245. };
  246. project.prototype.endUpdate = function(){
  247. if (tools.updateLock === 0){
  248. throw "project can not endUpdate before beginUpdate";
  249. }
  250. tools.updateLock -= 1;
  251. if (tools.updateLock === 0) {
  252. $.ajax({
  253. type: "POST",
  254. url: '/project/save',
  255. data: {'data': JSON.stringify({
  256. "project_id": tools._ID,
  257. "user_id": tools._userID,
  258. "date": new Date,
  259. "operation": tools.operation,
  260. "update_data": tools.updateData
  261. })},
  262. dataType: 'json',
  263. cache: false,
  264. timeout: 50000,
  265. success: function (result) {
  266. if (!result.error) {
  267. tools.doAfterUpdate(result.data);
  268. } else {
  269. alert('error: ' + result.message);
  270. }
  271. $.bootstrapLoading.end();
  272. },
  273. error: function(jqXHR, textStatus, errorThrown){
  274. alert('error ' + textStatus + " " + errorThrown);
  275. $.bootstrapLoading.end();
  276. }
  277. });
  278. tools.updateData = [];
  279. tools.operation = "";
  280. }
  281. };
  282. project.prototype.push = function(moduleName, data){
  283. if (tools.updateLock === 0){
  284. throw "project can not push data before beginUpdate";
  285. }
  286. var moduleData = {
  287. moduleName: moduleName,
  288. data: data
  289. };
  290. tools.updateData.push(moduleData);
  291. };
  292. project.prototype.pushNow = function (operation, moduleName, data) {
  293. var that = this;
  294. this.beginUpdate(operation);
  295. if (Object.prototype.toString.apply(moduleName) === "[object Array]" && Object.prototype.toString.apply(data) === "[object Array]") {
  296. moduleName.forEach(function (name, index) {
  297. if(name != 'projCounter'){
  298. that.push(moduleName[index], data[index]);
  299. }
  300. });
  301. } else {
  302. this.push(moduleName, data);
  303. }
  304. this.endUpdate();
  305. };
  306. project.prototype.registerModule = function(moduleName, obj){
  307. if (!tools.modules.hasOwnProperty(moduleName)){
  308. tools.modules[moduleName] = obj;
  309. }
  310. };
  311. project.prototype.markUpdateProject = function (data,type,callback) {
  312. CommonAjax.post("/project/markUpdateProject",{updateInfo:data,type:type},callback);
  313. };
  314. project.prototype.saveProperty = function (propertyName, propertyValue) {
  315. CommonAjax.post("/project/saveProperty", {projectID: this.ID(), propertyName: propertyName, propertyValue: propertyValue});
  316. };
  317. project.prototype.projectMarkChecking = function () {
  318. let me = this;
  319. let changeMark = projectObj.project.projectInfo.changeMark;
  320. if(changeMark&&changeMark!=''){
  321. let totalNode = this.Bills.getTotalCostNode(projectObj.mainController);
  322. if(totalNode) totalNode.changed = true;
  323. this.calcProgram.calcAllNodesAndSave();
  324. CommonAjax.post("/project/removeProjectMark",{projectID:me.ID()},function (data) {
  325. delete projectObj.project.projectInfo.changeMark;
  326. if(socket.connected == true){//如果这时候socket已经连接成功,则发送删除标记广播
  327. socket.emit('removeProjectMark',{projectID:me.ID(),userID:userID});
  328. }else {//如果还没连接成功,则放入消息对象缓存,等socket连接成功后发送
  329. socketObject.messages.push({message:'removeProjectMark',data:{projectID:me.ID(),userID:userID}});
  330. }
  331. });
  332. }
  333. };
  334. project.prototype.updateLockBills = function (value,callback) {
  335. let mixDatas = {
  336. projectID: projectObj.project.ID(),
  337. updateType: 'update',
  338. properties: {
  339. "property.lockBills":value
  340. },
  341. labourCoes: {},
  342. rations: [],
  343. bills: []
  344. };
  345. $.bootstrapLoading.start();
  346. CommonAjax.post('/pm/api/updateMixDatas', {user_id: userID, mixDataArr: mixDatas}, function (rstData) {
  347. projectObj.project.projectInfo.property.lockBills = value;
  348. if(callback){
  349. callback();
  350. }
  351. $.bootstrapLoading.end();
  352. });
  353. };
  354. //返回清单是否被锁定
  355. project.prototype.isBillsLocked =function(){
  356. if(projectObj.project.projectInfo.property.lockBills == true){
  357. return true;
  358. }
  359. return false;
  360. };
  361. // 清单是否属于锁定范围(所有清单)
  362. project.prototype.withinBillsLocked = function (node) {
  363. return node && node.sourceType === commonConstants.SourceType.BILLS;
  364. };
  365. project.prototype.updateNodes = function (datas,callback) {
  366. /* let datas = [
  367. {
  368. type:'ration',
  369. data:{
  370. projectID:1605,
  371. ID:"7b962fb0-1131-11e8-b3da-af725dadd7ae",
  372. name:'testRation'
  373. }
  374. },
  375. {
  376. type:'bills',
  377. data:{
  378. projectID:1605,
  379. ID:"af9f0081-1127-11e8-99a8-2fc02230b6e7",
  380. name:'安全文明施工专项费用123'
  381. }
  382. }
  383. ]*/
  384. CommonAjax.post("/project/updateNodes",datas,function (data) {
  385. if(callback){
  386. callback(data);
  387. }
  388. })
  389. };
  390. project.prototype.syncUpdateNodesAndRefresh =async function (datas) {
  391. let me = this;
  392. return new Promise(function (resolve, reject) {
  393. me.updateNodes(datas,function (result) {
  394. let nodes = me.updateNodesCache(result);
  395. projectObj.mainController.refreshTreeNode(nodes);
  396. resolve(nodes);
  397. });
  398. });
  399. };
  400. project.prototype.updateNodesCache =function (datas) {
  401. let refreshNode = [];
  402. let reclacQuantity = false;
  403. let deleteNode=[],addNodeDatas=[];
  404. for(let d of datas){
  405. let temObj = null;
  406. if(d.type == ModuleNames.bills || d.type == ModuleNames.ration){//如果是树节点类型,直接取树节点更新
  407. if(d.action =="add"){
  408. if(d.type == ModuleNames.ration) this.Ration.datas.push(d.data);
  409. reclacQuantity = true;
  410. addNodeDatas.push(d);
  411. }else {
  412. let temNode = this.mainTree.getNodeByID(d.data.ID);
  413. if(temNode){
  414. if(d.action =="delete"){
  415. if(d.type == ModuleNames.ration){
  416. _.remove(this.Ration.datas,{'ID':d.data.ID});
  417. this.Ration.deleteSubListOfRation({ID:d.data.ID});
  418. reclacQuantity = true;
  419. }
  420. deleteNode.push(temNode);//对于删除节点,统一不加入refreshNode中,由外部加入,避免重复
  421. }else {
  422. temObj = temNode.data;
  423. if(gljUtil.isDef(d.data.quantity))reclacQuantity = true;
  424. refreshNode.push(temNode);
  425. }
  426. }
  427. }
  428. }else if(d.type == ModuleNames.project){
  429. temObj = this;
  430. }else if (d.type == ModuleNames.ration_glj && d.action == "add"){
  431. this[d.type].datas.push(d.data);
  432. if(d.projectGLJ) this.projectGLJ.loadNewProjectGLJToCache(d.projectGLJ);
  433. } else {//其它类型,更新datas
  434. temObj = _.find(this[d.type].datas,{"ID":d.data.ID});
  435. }
  436. if(temObj){
  437. for(let key in d.data){
  438. if(key == 'ID' || key == 'id'){
  439. continue;
  440. }
  441. this.setValue(temObj,key,d.data[key])
  442. }
  443. }
  444. }
  445. //对树节点的操作并删除、添加清单、删除添加定额、删除对应的定额工料机缓存
  446. TREE_SHEET_HELPER.massOperationSheet(projectObj.mainController.sheet, function () {
  447. deleteTreeNodes(deleteNode);
  448. addNewNodes(addNodeDatas,refreshNode);
  449. });
  450. if(reclacQuantity) this.projectGLJ.calcQuantity();
  451. return refreshNode;
  452. function deleteTreeNodes(deleteNodes) {
  453. let controller = projectObj.mainController, project = projectObj.project;
  454. let Bill = project.Bills, Ration = project.Ration;
  455. for(let rd of deleteNodes){
  456. controller.sheet.deleteRows(rd.serialNo(),1);
  457. controller.tree.delete(rd);
  458. if(rd.sourceType == Bill.getSourceType()) Bill.tree.delete(rd.source);
  459. }
  460. }
  461. function addNewNodes(newDatas,refreshNode) {
  462. let controller = projectObj.mainController;
  463. let Bill = projectObj.project.Bills;
  464. let newAddNode = [];
  465. for(let nr of newDatas){
  466. let nextID = -1;
  467. let preNode = projectObj.project.mainTree.getNodeByID(nr.preSiblingID);
  468. if(preNode) nextID = preNode.getNextSiblingID();
  469. let newNode = projectObj.project.mainTree.insert(nr.parentID, nextID, nr.data.ID);
  470. if(nr.type == ModuleNames.bills){
  471. let newSource = Bill.tree.insertByData(nr.data, nr.ParentID, nextID, true);
  472. newNode.source = newSource;
  473. }else {
  474. newNode.source = nr.data;
  475. }
  476. newNode.sourceType = nr.type;
  477. newNode.data = nr.data;
  478. controller.sheet.addRows(newNode.serialNo(), 1);
  479. controller.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  480. newAddNode.push(newNode);
  481. refreshNode.push(newNode);
  482. }
  483. TREE_SHEET_HELPER.refreshTreeNodeData(controller.setting, controller.sheet, newAddNode, false);
  484. }
  485. };
  486. project.prototype.setValue=function (obj,key,value) {
  487. let keyArray = key.split('.');
  488. t_set(obj,value,keyArray,0);
  489. function t_set(obj,value,arr,index) {
  490. let nextIndex = index + 1;
  491. let tkey = arr[index];
  492. if(nextIndex < arr.length){
  493. if(obj[tkey]==undefined){
  494. obj[tkey] = {};
  495. }
  496. t_set(obj[tkey],value,arr,nextIndex)
  497. }else if(nextIndex == arr.length){
  498. obj[tkey] = value;
  499. }
  500. }
  501. }
  502. project.prototype.updateEvalproject = function (evalProject,data,callback){
  503. let programID = null;
  504. if(evalProject ==1){//当打勾估价项目选项后,该定额取费专业读取计算程序名称为“估价项目”的计算程序,且为只读状态
  505. programID = this.calcProgram.compiledTemplateMaps["估价项目"];
  506. if(callback) callback(programID);
  507. }else {//如果evalProject==0,则是去掉勾选估价项目选项,需要获取定额默认的取费专业。
  508. let libID = data.from == 'cpt'?"compleRationLib":data.libID;
  509. let query = {code:data.code,projectID:data.projectID,libID:libID};
  510. CommonAjax.post("/ration/getDefaultProgramID",query,function (result) {
  511. if(callback) callback(result);
  512. })
  513. }
  514. };
  515. //判断项目是否安装工程
  516. project.prototype.isInstall = function () {
  517. return projectObj.project.projectInfo.property.isInstall?true:false;//如果是undefinded 就也返回false
  518. };
  519. /* project.prototype.setBillsCalcMode = function (calcMode) {
  520. this.property.billsCalcMode = calcMode;
  521. this.initCalcFields();
  522. };*/
  523. /*project.prototype.initCalcFields = function () {
  524. // let settingConst = this.projSetting.settingConst;
  525. if (this.calcFields) {
  526. for (let field of this.calcFields) {
  527. // unitFeeCalcFlag
  528. if (field.type === 'zangu') {
  529. field.unitFeeFlag = converseUnitFeeFlag;
  530. } else {
  531. // if (this.projSetting.billsCalcMode === settingConst.billsCalcMode.rationContent) {
  532. if (this.property.billsCalcMode === leafBillGetFeeType.rationContent) {
  533. field.unitFeeFlag = rationContentUnitFeeFlag;
  534. // } else if ( this.projSetting.billsCalcMode === settingConst.billsCalcMode.billsPrice) {
  535. } else if ( this.property.billsCalcMode === leafBillGetFeeType.billsPrice) {
  536. field.unitFeeFlag = billsPriceUnitFeeFlag;
  537. } else {
  538. field.unitFeeFlag = averageQtyUnitFeeFlag;
  539. }
  540. }
  541. // totalFeeCalcFlag
  542. if (field.type === 'common') {
  543. // if (this.projSetting.billsCalcMode === settingConst.billsCalcMode.rationPriceConverse) {
  544. if (this.property.billsCalcMode === leafBillGetFeeType.rationPriceConverse) {
  545. field.totalFeeFlag = sumTotalFeeFlag;
  546. } else {
  547. field.totalFeeFlag = totalFeeFlag;
  548. }
  549. } else {
  550. field.totalFeeFlag = sumTotalFeeFlag;
  551. }
  552. }
  553. }
  554. }*/
  555. return new project();
  556. }
  557. };