project_glj.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. $("#file-save-as-dialog").modal("hide");
  118. }
  119. });
  120. });
  121. // 从其他建设项目中复制 选择建设项目
  122. $("#other-project").change(function () {
  123. let projectId = $(this).val();
  124. if (otherFileData[projectId] === undefined) {
  125. return false;
  126. }
  127. let otherFileHtml = '';
  128. for (let unitPrice of otherFileData[projectId]) {
  129. otherFileHtml += '<option value="' + unitPrice.id + '">' + unitPrice.name + '</option>';
  130. }
  131. $("#other-file").html(otherFileHtml);
  132. });
  133. // 单价文件选项切换
  134. $("input[name='change-type']").change(function () {
  135. let type = $(this).val();
  136. type = parseInt(type);
  137. $("#change-dj .option").hide();
  138. if (type === 0) {
  139. $(".option.select").show();
  140. } else {
  141. $(".option.copy").show();
  142. }
  143. });
  144. // 单价文件切换确认
  145. $("#change-file-confirm").click(function () {
  146. if (isChanging) {
  147. return false;
  148. }
  149. let type = $("input[name='change-type']:checked").val();
  150. type = parseInt(type);
  151. let changeUnitPriceId = 0;
  152. if (type === 0) {
  153. // 从本项目中选择
  154. changeUnitPriceId = $("#self-file").val();
  155. } else {
  156. // 从其他项目中复制
  157. changeUnitPriceId = $("#other-file").val();
  158. }
  159. $('#change-dj').modal("hide");
  160. $.bootstrapLoading.start();
  161. $.ajax({
  162. url: '/glj/change-file',
  163. type: 'post',
  164. data: {project_id: scUrlUtil.GetQueryString('project'), change_id: changeUnitPriceId, type: type},
  165. error: function () {
  166. isChanging = false;
  167. $.bootstrapLoading.end();
  168. },
  169. beforeSend: function () {
  170. isChanging = true;
  171. },
  172. success: function (response) {
  173. isChanging = false;
  174. if (response.err === 1) {
  175. let msg = response.msg !== undefined ? response.msg : '未知错误';
  176. alert(msg);
  177. $.bootstrapLoading.end();
  178. return false;
  179. }
  180. projectObj.project.projectGLJ.loadData(function () {
  181. let projectGLJ = projectObj.project.projectGLJ;
  182. projectGLJ.loadCacheData();
  183. unitPriceFileInit();
  184. gljOprObj.refreshView();
  185. $.bootstrapLoading.end();
  186. });
  187. }
  188. });
  189. });
  190. // 是否主动更改数据
  191. $("#message").on('click', '#load-data', function () {
  192. $("#message").html('正在加载...');
  193. // 重新加载数据到缓存
  194. projectObj.project.projectGLJ.loadData(function () {
  195. projectObj.project.projectGLJ.loadCacheData();
  196. $("#notify").slideUp('fast');
  197. });
  198. });
  199. $('#pop-dj').popover({
  200. placement: "bottom",
  201. html: true,
  202. trigger: "hover | focus",
  203. content: getUsedTenderInfo
  204. }
  205. );
  206. });
  207. /**
  208. * 初始化数据
  209. *
  210. * @return {void|boolean}
  211. */
  212. function init() {
  213. projectObj.project.projectGLJ.loadData(function (data) {
  214. if (initPage==false||jsonData.length <= 0) {
  215. // 赋值
  216. jsonData = data.gljList !== undefined && data.gljList.length > 0 ? data.gljList : [];
  217. jsonData = filterProjectGLJ(jsonData);
  218. jsonData = sortProjectGLJ(jsonData);
  219. mixRatioConnectData = data.mixRatioConnectData !== undefined ? data.mixRatioConnectData : mixRatioConnectData;
  220. mixRatioMap = data.mixRatioMap !== undefined ? data.mixRatioMap : mixRatioMap;
  221. host = data.constData.hostname !== undefined ? data.constData.hostname : '';
  222. materialIdList = data.constData.materialIdList !== undefined ? data.constData.materialIdList : materialIdList;
  223. roomId = data.constData.roomId !== undefined ? data.constData.roomId : roomId;
  224. canNotChangeTypeId = data.constData.ownCompositionTypes !== undefined ?
  225. data.constData.ownCompositionTypes : canNotChangeTypeId;
  226. GLJTypeConst = data.constData.GLJTypeConst !== undefined ? JSON.parse(data.constData.GLJTypeConst) : GLJTypeConst;
  227. // 连接socket服务器
  228. socketInit();
  229. unitPriceFileInit();
  230. setTimeout(spreadInit, 1);
  231. initPage=true;
  232. } else {
  233. projectObj.project.projectGLJ.loadCacheData();
  234. }
  235. });
  236. }
  237. /**
  238. * spreadjs相关初始化
  239. *
  240. * @return {void}
  241. */
  242. function spreadInit() {
  243. if(projectGLJSheet&&projectGLJSpread){
  244. projectGLJSheet.spread.destroy();
  245. }
  246. projectGLJSpread = new ProjectGLJSpread();
  247. projectGLJSpread.successCallback = successTrigger;
  248. projectGLJSheet = projectGLJSpread.init();
  249. // 绑定单击事件
  250. let lastRow = 0;
  251. projectGLJSheet.bind(GC.Spread.Sheets.Events.CellClick, function (element, info) {
  252. let currentRow = info.row;
  253. if (currentRow === undefined || currentRow === lastRow) {
  254. return;
  255. }
  256. if (currentTag !== 'mix-ratio' && currentTag !== 'machine') {
  257. return;
  258. }
  259. let spread = currentTag === 'mix-ratio' ? mixRatioSpread : machineSpread;
  260. if (spread === null) {
  261. return;
  262. }
  263. let projectGLJId = projectGLJSheet.getActiveDataByField('id');
  264. spread.getRatioData(projectGLJId);
  265. lastRow = currentRow;
  266. });
  267. if(currentTag){
  268. if (currentTag === 'ration') {
  269. projectGLJSheet.filterData('unit_price.type', []);
  270. }
  271. if(currentTag === "mix-ratio"){
  272. projectGLJSheet.filterData('unit_price.type', [GLJTypeConst.CONCRETE, GLJTypeConst.MORTAR, GLJTypeConst.MIX_RATIO,GLJTypeConst.MAIN_MATERIAL]);
  273. }
  274. if(currentTag === "machine"){
  275. projectGLJSheet.filterData('unit_price.type', [GLJTypeConst.GENERAL_MACHINE]);
  276. }
  277. }
  278. /* // 切换tab触发refresh
  279. $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
  280. if($(e.target).data('name')==undefined){
  281. return;
  282. }
  283. currentTag = $(e.target).data('name');
  284. if (currentTag === 'ration') {
  285. projectGLJSheet.filterData('unit_price.type', []);
  286. }
  287. });*/
  288. loadSize("glj-main", function () {
  289. projectGLJSpread.sheetObj.spread.refresh();
  290. });
  291. }
  292. /**
  293. * 单价文件相关初始化
  294. *
  295. * @param {String} name
  296. * @param {Array} data
  297. * @return {void}
  298. */
  299. function unitPriceFileInit() {
  300. let projectGLJ = projectObj.project.projectGLJ;
  301. let data = projectGLJ.datas;
  302. usedTenderList = data.usedTenderList !== undefined ? data.usedTenderList : [];
  303. usedUnitPriceInfo = data.constData.usedUnitPriceInfo !== undefined ?
  304. data.constData.usedUnitPriceInfo : {};
  305. $("#used-name").text(usedUnitPriceInfo.name);
  306. let usedCount = usedTenderList.length <= 0 ? 1 : usedTenderList.length;
  307. $("#used-count").text(usedCount);
  308. }
  309. function getUsedTenderInfo() {
  310. return usedTenderList.join("<br>");
  311. }
  312. /**
  313. * 成功事件
  314. *
  315. * @param {string} field
  316. * @param {object} info
  317. * @return {void}
  318. */
  319. function successTrigger(field, info,id) {
  320. let updateData = {};
  321. switch (field) {
  322. case 'unit_price.market_price':
  323. // 计算价格
  324. updateData = projectGLJSpread.priceCalculate(info);
  325. // 触发websocket通知
  326. socket.emit('dataNotify', JSON.stringify(info));
  327. break;
  328. case 'supply':
  329. // 供货方式更改成功后
  330. projectGLJSpread.changeSupplyType(info);
  331. break;
  332. case 'is_evaluate':
  333. changeIsEvaluate(id);
  334. break;
  335. }
  336. // 重新加载数据到缓存
  337. projectObj.project.projectGLJ.loadData(function () {
  338. projectObj.project.projectGLJ.loadCacheData();
  339. $.bootstrapLoading.end();
  340. });
  341. // 更新定额工料机
  342. gljOprObj.refreshView();
  343. }
  344. //更新是否暂估
  345. function changeIsEvaluate (id){
  346. let projectGLJ = projectObj.project.projectGLJ;
  347. let datas = projectGLJ.datas;
  348. let gljList = datas.gljList;
  349. let glj = _.find(gljList, {'id': id});
  350. if(glj){
  351. let con_key = gljOprObj.getIndex(glj,gljKeyArray);
  352. let pratioM =datas.mixRatioConnectData[con_key];//找到父key
  353. let conditions = [];
  354. if(pratioM&&pratioM.length>0){
  355. for(let p_key of pratioM ){
  356. conditions.push(gljOprObj.getConditionByKey(p_key));
  357. }
  358. }
  359. let gljs = projectGLJ.getProjectGLJs(conditions,false);
  360. gljs.push(glj);
  361. let nodes = projectGLJ.getImpactRationNodes(gljs);//取到因为改变工料机价格而受影响的定额
  362. //更新对应的工料机类型的定额
  363. let ration =_.find(projectObj.project.Ration.datas,{'type':rationType.gljRation,'projectGLJID':glj.id});
  364. if(ration){
  365. ration.isEstimate =glj.is_evaluate?1:0;
  366. let ration_node = projectObj.project.mainTree.getNodeByID(ration.ID);
  367. ration_node?projectObj.mainController.refreshTreeNode([ration_node]):"";
  368. }
  369. projectObj.project.calcProgram.calcRationsAndSave(nodes);//触发计算程序
  370. }
  371. }
  372. /**
  373. * socket.io相关初始化
  374. *
  375. * @return {void}
  376. */
  377. function socketInit() {
  378. if (socket === null) {
  379. socket = io('http://' + host + ':3300');
  380. socket.on('connect', function () {
  381. socket.emit('join', roomId);
  382. console.log('单价文件同步连接成功');
  383. });
  384. }
  385. // 接收到改变
  386. socket.on('dataChange', function (data) {
  387. data = JSON.parse(data);
  388. if (data.newValue === undefined) {
  389. return false;
  390. }
  391. $("#message").html('市场单位已被修改,<a href="javascript:void(0);" id="load-data">点击加载</a>');
  392. $("#notify").slideDown('fast');
  393. });
  394. }
  395. //过滤消耗量为0的项目工料机
  396. function filterProjectGLJ(jsonData) {
  397. if (jsonData.length > 0) {
  398. // 不显示消耗量为0的数据
  399. jsonData= _.filter(jsonData,function (item) {
  400. return item.quantity !== 0 && item.quantity !== '0'
  401. })
  402. }
  403. return jsonData;
  404. }
  405. function sortProjectGLJ(jsonData) {
  406. if (jsonData.length > 0) {
  407. jsonData = _.sortByAll(jsonData, [function (item) {
  408. return item.unit_price.type + "";
  409. }, 'code']);
  410. }
  411. return jsonData
  412. }