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