project.js 21 KB

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