project.js 21 KB

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