project.js 21 KB

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