project_glj.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. projectGLJSpread = new ProjectGLJSpread();
  244. projectGLJSpread.successCallback = successTrigger;
  245. projectGLJSheet = projectGLJSpread.init();
  246. // 绑定单击事件
  247. let lastRow = 0;
  248. projectGLJSheet.bind(GC.Spread.Sheets.Events.CellClick, function (element, info) {
  249. let currentRow = info.row;
  250. if (currentRow === undefined || currentRow === lastRow) {
  251. return;
  252. }
  253. if (currentTag !== 'mix-ratio' && currentTag !== 'machine') {
  254. return;
  255. }
  256. let spread = currentTag === 'mix-ratio' ? mixRatioSpread : machineSpread;
  257. if (spread === null) {
  258. return;
  259. }
  260. let projectGLJId = projectGLJSheet.getActiveDataByField('id');
  261. spread.getRatioData(projectGLJId);
  262. lastRow = currentRow;
  263. });
  264. // 切换tab触发refresh
  265. $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
  266. currentTag = $(e.target).data('name');
  267. if (currentTag === 'ration') {
  268. projectGLJSheet.filterData('unit_price.type', []);
  269. }
  270. });
  271. loadSize("glj-main", function () {
  272. projectGLJSpread.sheetObj.spread.refresh();
  273. });
  274. }
  275. /**
  276. * 单价文件相关初始化
  277. *
  278. * @param {String} name
  279. * @param {Array} data
  280. * @return {void}
  281. */
  282. function unitPriceFileInit() {
  283. let projectGLJ = projectObj.project.projectGLJ;
  284. let data = projectGLJ.datas;
  285. usedTenderList = data.usedTenderList !== undefined ? data.usedTenderList : [];
  286. usedUnitPriceInfo = data.constData.usedUnitPriceInfo !== undefined ?
  287. data.constData.usedUnitPriceInfo : {};
  288. $("#used-name").text(usedUnitPriceInfo.name);
  289. let usedCount = usedTenderList.length <= 0 ? 1 : usedTenderList.length;
  290. $("#used-count").text(usedCount);
  291. }
  292. function getUsedTenderInfo() {
  293. return usedTenderList.join("<br>");
  294. }
  295. /**
  296. * 成功事件
  297. *
  298. * @param {string} field
  299. * @param {object} info
  300. * @return {void}
  301. */
  302. function successTrigger(field, info,id) {
  303. let updateData = {};
  304. switch (field) {
  305. case 'unit_price.market_price':
  306. // 计算价格
  307. updateData = projectGLJSpread.priceCalculate(info);
  308. // 触发websocket通知
  309. socket.emit('dataNotify', JSON.stringify(info));
  310. break;
  311. case 'supply':
  312. // 供货方式更改成功后
  313. projectGLJSpread.changeSupplyType(info);
  314. break;
  315. case 'is_evaluate':
  316. changeIsEvaluate(id);
  317. break;
  318. }
  319. // 重新加载数据到缓存
  320. projectObj.project.projectGLJ.loadData(function () {
  321. projectObj.project.projectGLJ.loadCacheData();
  322. $.bootstrapLoading.end();
  323. });
  324. // 更新定额工料机
  325. gljOprObj.refreshView();
  326. }
  327. //更新是否暂估
  328. function changeIsEvaluate (id){
  329. let projectGLJ = projectObj.project.projectGLJ
  330. let datas = projectGLJ.datas;
  331. let gljList = datas.gljList;
  332. let glj = _.find(gljList, {'id': id});
  333. if(glj){
  334. let con_key = gljOprObj.getIndex(glj,gljKeyArray);
  335. let pratioM =datas.mixRatioConnectData[con_key];//找到父key
  336. let conditions = [];
  337. if(pratioM&&pratioM.length>0){
  338. for(let p_key in pratioM ){
  339. conditions.push(gljOprObj.getConditionByKey(p_key));
  340. }
  341. }
  342. let gljs = projectGLJ.getProjectGLJs(conditions,false);
  343. gljs.push(glj);
  344. let nodes = projectGLJ.getImpactRationNodes(gljs);//取到因为改变工料机价格而受影响的定额
  345. projectObj.project.calcProgram.calcRationsAndSave(nodes);//触发计算程序
  346. }
  347. }
  348. /**
  349. * socket.io相关初始化
  350. *
  351. * @return {void}
  352. */
  353. function socketInit() {
  354. if (socket === null) {
  355. socket = io('http://' + host + ':3300');
  356. socket.on('connect', function () {
  357. socket.emit('join', roomId);
  358. console.log('单价文件同步连接成功');
  359. });
  360. }
  361. // 接收到改变
  362. socket.on('dataChange', function (data) {
  363. data = JSON.parse(data);
  364. if (data.newValue === undefined) {
  365. return false;
  366. }
  367. $("#message").html('市场单位已被修改,<a href="javascript:void(0);" id="load-data">点击加载</a>');
  368. $("#notify").slideDown('fast');
  369. });
  370. }
  371. //过滤消耗量为0的项目工料机
  372. function filterProjectGLJ(jsonData) {
  373. if (jsonData.length > 0) {
  374. // 不显示消耗量为0的数据
  375. let tmpData = [];
  376. for (let data of jsonData) {
  377. if (data.quantity !== 0 && data.quantity !== '0') {
  378. tmpData.push(data);
  379. }
  380. }
  381. jsonData = tmpData;
  382. }
  383. return jsonData;
  384. }
  385. function sortProjectGLJ(jsonData) {
  386. if (jsonData.length > 0) {
  387. jsonData = _.sortByAll(jsonData, [function (item) {
  388. return item.unit_price.type + "";
  389. }, 'code']);
  390. }
  391. return jsonData
  392. }