project_glj.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /**
  2. * 工料机汇总相关
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/6/15
  6. * @version
  7. */
  8. let projectGLJSpread = null;
  9. let projectGLJSheet = null;
  10. // websocket所需
  11. let host = '';
  12. let socket = null;
  13. let roomId = 0;
  14. // 判定常量
  15. let materialIdList = [];
  16. let canNotChangeTypeId = [];
  17. let GLJTypeConst = [];
  18. // spreadjs载入数据所需
  19. let jsonData = [];
  20. let mixRatioConnectData = [];
  21. let mixRatioMap = {};
  22. // 单价文件相关
  23. let usedUnitPriceInfo = {};
  24. let usedTenderList = [];
  25. let otherFileData = {};
  26. let currentTag = '';
  27. let isChanging = false;
  28. let initPage = false;
  29. $(document).ready(function () {
  30. $('#tab_gongliaoji').on('show.bs.tab', function (e) {
  31. $(e.relatedTarget.hash).removeClass('active');
  32. init();
  33. });
  34. slideResize($("#glj-main"), function () {
  35. projectGLJSpread.sheetObj.spread.refresh();
  36. });
  37. // 单价文件切换弹框
  38. $('#change-dj').on('shown.bs.modal', function () {
  39. // 获取当前建设项数据
  40. let projectName = projectInfoObj.projectInfo.fullFolder !== undefined &&
  41. projectInfoObj.projectInfo.fullFolder.length > 0 ? projectInfoObj.projectInfo.fullFolder[0] : '';
  42. $("#current-project-name").text(projectName);
  43. // 获取切换单价文件相关数据
  44. $.ajax({
  45. url: '/glj/get-project-info',
  46. type: 'post',
  47. data: {project_id: scUrlUtil.GetQueryString('project')},
  48. dataType: 'json',
  49. success: function (response) {
  50. if (response.err === 1) {
  51. alert('数据传输错误!');
  52. return false;
  53. }
  54. let data = response.data;
  55. // 本项目中的单价文件
  56. if (data.self.length > 0) {
  57. let selfFileHtml = '';
  58. for (let tmp of data.self) {
  59. let select = usedUnitPriceInfo === tmp.id ? ' selected="selected"' : '';
  60. selfFileHtml += '<option' + select + ' value="' + tmp.id + '">' + tmp.name + '</option>';
  61. }
  62. $("#self-file").html(selfFileHtml);
  63. }
  64. // 其他建设项目数据
  65. if (data.other.length > 0) {
  66. let otherProjectHtml = '';
  67. let otherFileHtml = '';
  68. for (let tmp of data.other) {
  69. otherProjectHtml += '<option value="' + tmp.ID + '">' + tmp.name + '</option>';
  70. otherFileData[tmp.ID] = tmp.unitPriceList;
  71. if (otherFileHtml !== '') {
  72. continue;
  73. }
  74. for (let unitPrice of tmp.unitPriceList) {
  75. otherFileHtml += '<option value="' + unitPrice.id + '">' + unitPrice.name + '</option>';
  76. }
  77. }
  78. $("#other-project").html(otherProjectHtml);
  79. $("#other-file").html(otherFileHtml);
  80. }
  81. }
  82. });
  83. });
  84. // 单价文件另存为弹框
  85. $("#file-save-as-dialog").on('shown.bs.modal', function () {
  86. // 获取当前建设项数据
  87. $("#save-as-name").val(usedUnitPriceInfo.name + '(复件)');
  88. });
  89. // 单价文件另存为操作
  90. $("#save-as-confirm").click(function () {
  91. let name = $("#save-as-name").val();
  92. if (name === '') {
  93. $("#save-as-tips").text('请填写单价文件名称').show();
  94. return false;
  95. }
  96. if (isChanging) {
  97. return false;
  98. }
  99. $.ajax({
  100. url: '/glj/save-as',
  101. type: 'post',
  102. data: {name: name, project_id: scUrlUtil.GetQueryString('project')},
  103. dataType: 'json',
  104. error: function () {
  105. isChanging = false;
  106. },
  107. beforeSend: function () {
  108. isChanging = true;
  109. },
  110. success: function (response) {
  111. isChanging = false;
  112. if (response.err === 1) {
  113. let msg = response.msg !== undefined && response.msg !== '' ? response.msg : '另存为失败!';
  114. $("#save-as-tips").text(msg).show();
  115. return false;
  116. }
  117. projectObj.project.projectGLJ.loadData(function () {
  118. let projectGLJ = projectObj.project.projectGLJ;
  119. projectGLJ.loadCacheData();
  120. unitPriceFileInit();
  121. projectObj.project.calcProgram.calcAllNodesAndSave();
  122. gljOprObj.refreshView();
  123. if(socketObject.roomInfo){
  124. let data ={
  125. projectID:projectObj.project.ID(),
  126. oldRoom:socketObject.roomInfo.unitFile,
  127. newRoom:socketObject.getUnitFileRoomID(),
  128. name:'unitFile'
  129. }
  130. socket.emit('changeNewRoom',data);
  131. socketObject.roomInfo.unitFile = socketObject.getUnitFileRoomID();
  132. }
  133. $.bootstrapLoading.end();
  134. });
  135. $("#file-save-as-dialog").modal("hide");
  136. }
  137. });
  138. });
  139. // 从其他建设项目中复制 选择建设项目
  140. $("#other-project").change(function () {
  141. let projectId = $(this).val();
  142. if (otherFileData[projectId] === undefined) {
  143. return false;
  144. }
  145. let otherFileHtml = '';
  146. for (let unitPrice of otherFileData[projectId]) {
  147. otherFileHtml += '<option value="' + unitPrice.id + '">' + unitPrice.name + '</option>';
  148. }
  149. $("#other-file").html(otherFileHtml);
  150. });
  151. // 单价文件选项切换
  152. $("input[name='change-type']").change(function () {
  153. let type = $(this).val();
  154. type = parseInt(type);
  155. $("#change-dj .option").hide();
  156. if (type === 0) {
  157. $(".option.select").show();
  158. } else {
  159. $(".option.copy").show();
  160. }
  161. });
  162. // 单价文件切换确认
  163. $("#change-file-confirm").click(function () {
  164. if (isChanging) {
  165. return false;
  166. }
  167. let type = $("input[name='change-type']:checked").val();
  168. type = parseInt(type);
  169. let changeUnitPriceId = 0;
  170. if (type === 0) {
  171. // 从本项目中选择
  172. changeUnitPriceId = $("#self-file").val();
  173. } else {
  174. // 从其他项目中复制
  175. changeUnitPriceId = $("#other-file").val();
  176. }
  177. $('#change-dj').modal("hide");
  178. $.bootstrapLoading.start();
  179. $.ajax({
  180. url: '/glj/change-file',
  181. type: 'post',
  182. data: {project_id: scUrlUtil.GetQueryString('project'), change_id: changeUnitPriceId, type: type},
  183. error: function () {
  184. isChanging = false;
  185. $.bootstrapLoading.end();
  186. },
  187. beforeSend: function () {
  188. isChanging = true;
  189. },
  190. success: function (response) {
  191. isChanging = false;
  192. if (response.err === 1) {
  193. let msg = response.msg !== undefined ? response.msg : '未知错误';
  194. alert(msg);
  195. $.bootstrapLoading.end();
  196. return false;
  197. }
  198. projectObj.project.projectGLJ.loadData(function () {
  199. let projectGLJ = projectObj.project.projectGLJ;
  200. projectGLJ.loadCacheData();
  201. unitPriceFileInit();
  202. gljOprObj.refreshView();
  203. projectObj.project.calcProgram.calcAllNodesAndSave();
  204. if(socketObject.roomInfo){
  205. let data ={
  206. projectID:projectObj.project.ID(),
  207. oldRoom:socketObject.roomInfo.unitFile,
  208. newRoom:socketObject.getUnitFileRoomID(),
  209. name:'unitFile'
  210. }
  211. socket.emit('changeNewRoom',data);
  212. socketObject.roomInfo.unitFile = socketObject.getUnitFileRoomID();
  213. }
  214. $.bootstrapLoading.end();
  215. });
  216. }
  217. });
  218. });
  219. // 是否主动更改数据
  220. $("#message").on('click', '#load-data', function () {
  221. $("#message").html('正在加载...');
  222. // 重新加载数据到缓存
  223. projectObj.project.projectGLJ.loadData(function () {
  224. projectObj.project.projectGLJ.loadCacheData();
  225. $("#notify").slideUp('fast');
  226. });
  227. });
  228. $('#pop-dj').popover({
  229. placement: "bottom",
  230. html: true,
  231. trigger: "hover | focus",
  232. content: getUsedTenderInfo
  233. }
  234. );
  235. });
  236. /**
  237. * 初始化数据
  238. *
  239. * @return {void|boolean}
  240. */
  241. function init() {
  242. projectObj.project.projectGLJ.loadData(function (data) {
  243. if (initPage==false||jsonData.length <= 0) {
  244. // 赋值
  245. jsonData = data.gljList !== undefined && data.gljList.length > 0 ? data.gljList : [];
  246. jsonData = filterProjectGLJ(jsonData);
  247. jsonData = sortProjectGLJ(jsonData);
  248. mixRatioConnectData = data.mixRatioConnectData !== undefined ? data.mixRatioConnectData : mixRatioConnectData;
  249. mixRatioMap = data.mixRatioMap !== undefined ? data.mixRatioMap : mixRatioMap;
  250. host = data.constData.hostname !== undefined ? data.constData.hostname : '';
  251. materialIdList = data.constData.materialIdList !== undefined ? data.constData.materialIdList : materialIdList;
  252. roomId = data.constData.roomId !== undefined ? data.constData.roomId : roomId;
  253. canNotChangeTypeId = data.constData.ownCompositionTypes !== undefined ?
  254. data.constData.ownCompositionTypes : canNotChangeTypeId;
  255. GLJTypeConst = data.constData.GLJTypeConst !== undefined ? JSON.parse(data.constData.GLJTypeConst) : GLJTypeConst;
  256. // 连接socket服务器 这里改成和费率的socket统一管理了,需要在主页面打开后就连接
  257. // socketInit();
  258. unitPriceFileInit();
  259. setTimeout(spreadInit, 1);
  260. initPage=true;
  261. } else {
  262. projectObj.project.projectGLJ.loadCacheData();
  263. }
  264. });
  265. }
  266. /**
  267. * spreadjs相关初始化
  268. *
  269. * @return {void}
  270. */
  271. function spreadInit() {
  272. if(projectGLJSheet&&projectGLJSpread){
  273. projectGLJSheet.spread.destroy();
  274. }
  275. projectGLJSpread = new ProjectGLJSpread();
  276. projectGLJSpread.successCallback = successTrigger;
  277. projectGLJSheet = projectGLJSpread.init();
  278. // 绑定单击事件
  279. let lastRow = 0;
  280. projectGLJSheet.bind(GC.Spread.Sheets.Events.CellClick, function (element, info) {
  281. let currentRow = info.row;
  282. if (currentRow === undefined || currentRow === lastRow) {
  283. return;
  284. }
  285. if (currentTag !== 'mix-ratio' && currentTag !== 'machine') {
  286. return;
  287. }
  288. let spread = currentTag === 'mix-ratio' ? mixRatioSpread : machineSpread;
  289. if (spread === null) {
  290. return;
  291. }
  292. let projectGLJId = projectGLJSheet.getActiveDataByField('id');
  293. spread.getRatioData(projectGLJId);
  294. lastRow = currentRow;
  295. });
  296. if(currentTag){
  297. if (currentTag === 'ration') {
  298. projectGLJSheet.filterData('unit_price.type', []);
  299. }
  300. if(currentTag === "mix-ratio"){
  301. projectGLJSheet.filterData('unit_price.type', [GLJTypeConst.CONCRETE, GLJTypeConst.MORTAR, GLJTypeConst.MIX_RATIO,GLJTypeConst.MAIN_MATERIAL]);
  302. }
  303. if(currentTag === "machine"){
  304. projectGLJSheet.filterData('unit_price.type', [GLJTypeConst.GENERAL_MACHINE]);
  305. }
  306. }
  307. /* // 切换tab触发refresh
  308. $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
  309. if($(e.target).data('name')==undefined){
  310. return;
  311. }
  312. currentTag = $(e.target).data('name');
  313. if (currentTag === 'ration') {
  314. projectGLJSheet.filterData('unit_price.type', []);
  315. }
  316. });*/
  317. loadSize("glj-main", function () {
  318. projectGLJSpread.sheetObj.spread.refresh();
  319. });
  320. }
  321. /**
  322. * 单价文件相关初始化
  323. *
  324. * @param {String} name
  325. * @param {Array} data
  326. * @return {void}
  327. */
  328. function unitPriceFileInit() {
  329. let projectGLJ = projectObj.project.projectGLJ;
  330. let data = projectGLJ.datas;
  331. usedTenderList = data.usedTenderList !== undefined ? data.usedTenderList : [];
  332. usedUnitPriceInfo = data.constData.usedUnitPriceInfo !== undefined ?
  333. data.constData.usedUnitPriceInfo : {};
  334. $("#used-name").text(usedUnitPriceInfo.name);
  335. let usedCount = usedTenderList.length <= 0 ? 1 : usedTenderList.length;
  336. $("#used-count").text(usedCount);
  337. }
  338. function getUsedTenderInfo() {
  339. return usedTenderList.join("<br>");
  340. }
  341. /**
  342. * 成功事件
  343. *
  344. * @param {string} field
  345. * @param {object} info
  346. * @return {void}
  347. */
  348. function successTrigger(field, info,id) {
  349. let updateData = {};
  350. switch (field) {
  351. case 'unit_price.market_price':
  352. // 计算价格
  353. updateData = projectGLJSpread.priceCalculate(info);
  354. // 触发websocket通知
  355. socket.emit('dataNotify', JSON.stringify(info));
  356. break;
  357. case 'supply':
  358. // 供货方式更改成功后
  359. projectGLJSpread.changeSupplyType(info);
  360. break;
  361. case 'is_evaluate':
  362. changeIsEvaluate(id);
  363. break;
  364. }
  365. // 重新加载数据到缓存
  366. projectObj.project.projectGLJ.loadData(function () {
  367. projectObj.project.projectGLJ.loadCacheData();
  368. $.bootstrapLoading.end();
  369. });
  370. // 更新定额工料机
  371. gljOprObj.refreshView();
  372. }
  373. //更新是否暂估
  374. function changeIsEvaluate (id){
  375. let projectGLJ = projectObj.project.projectGLJ;
  376. let datas = projectGLJ.datas;
  377. let gljList = datas.gljList;
  378. let glj = _.find(gljList, {'id': id});
  379. if(glj){
  380. let con_key = gljOprObj.getIndex(glj,gljKeyArray);
  381. let pratioM =datas.mixRatioConnectData[con_key];//找到父key
  382. let conditions = [];
  383. if(pratioM&&pratioM.length>0){
  384. for(let p_key of pratioM ){
  385. conditions.push(gljOprObj.getConditionByKey(p_key));
  386. }
  387. }
  388. let gljs = projectGLJ.getProjectGLJs(conditions,false);
  389. gljs.push(glj);
  390. let nodes = projectGLJ.getImpactRationNodes(gljs);//取到因为改变工料机价格而受影响的定额
  391. //更新对应的工料机类型的定额
  392. let ration =_.find(projectObj.project.Ration.datas,{'type':rationType.gljRation,'projectGLJID':glj.id});
  393. if(ration){
  394. ration.isEstimate =glj.is_evaluate?1:0;
  395. let ration_node = projectObj.project.mainTree.getNodeByID(ration.ID);
  396. ration_node?projectObj.mainController.refreshTreeNode([ration_node]):"";
  397. }
  398. projectObj.project.calcProgram.calcRationsAndSave(nodes);//触发计算程序
  399. }
  400. }
  401. /**
  402. * socket.io相关初始化
  403. *
  404. * @return {void}
  405. */
  406. function socketInit() {
  407. if (socket === null) {
  408. socket = io('http://' + host + ':3300');
  409. socket.on('connect', function () {
  410. socket.emit('join', roomId);
  411. console.log('单价文件同步连接成功');
  412. });
  413. }
  414. // 接收到改变
  415. socket.on('dataChange', function (data) {
  416. data = JSON.parse(data);
  417. if (data.newValue === undefined) {
  418. return false;
  419. }
  420. $("#message").html('市场单位已被修改,<a href="javascript:void(0);" id="load-data">点击加载</a>');
  421. $("#notify").slideDown('fast');
  422. });
  423. }
  424. //过滤消耗量为0的项目工料机
  425. function filterProjectGLJ(jsonData) {
  426. if (jsonData.length > 0) {
  427. // 不显示消耗量为0的数据
  428. jsonData= _.filter(jsonData,function (item) {
  429. return item.quantity !== 0 && item.quantity !== '0'
  430. })
  431. }
  432. return jsonData;
  433. }
  434. function sortProjectGLJ(jsonData) {
  435. if (jsonData.length > 0) {
  436. jsonData = _.sortByAll(jsonData, [function (item) {
  437. return item.unit_price.type + "";
  438. }, 'code']);
  439. }
  440. return jsonData
  441. }