project_glj.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. $(document).ready(function () {
  29. $('#tab_gongliaoji').on('show.bs.tab', function (e) {
  30. $(e.relatedTarget.hash).removeClass('active');
  31. init();
  32. });
  33. slideResize($("#glj-main"), function() {
  34. projectGLJSpread.sheetObj.spread.refresh();
  35. });
  36. // 单价文件切换弹框
  37. $('#change-dj').on('shown.bs.modal', function () {
  38. // 获取当前建设项数据
  39. let projectName = projectInfoObj.projectInfo.fullFolder !== undefined &&
  40. projectInfoObj.projectInfo.fullFolder.length > 0 ? projectInfoObj.projectInfo.fullFolder[0] : '';
  41. $("#current-project-name").text(projectName);
  42. // 获取切换单价文件相关数据
  43. $.ajax({
  44. url: '/glj/get-project-info',
  45. type: 'post',
  46. data: {project_id: scUrlUtil.GetQueryString('project')},
  47. dataType: 'json',
  48. success: function(response) {
  49. if (response.err === 1) {
  50. alert('数据传输错误!');
  51. return false;
  52. }
  53. let data = response.data;
  54. // 本项目中的单价文件
  55. if (data.self.length > 0) {
  56. let selfFileHtml = '';
  57. for(let tmp of data.self) {
  58. let select = usedUnitPriceInfo === tmp.id ? ' selected="selected"' : '';
  59. selfFileHtml += '<option'+ select +' value="'+ tmp.id +'">'+ tmp.name +'</option>';
  60. }
  61. $("#self-file").html(selfFileHtml);
  62. }
  63. // 其他建设项目数据
  64. if (data.other.length > 0) {
  65. let otherProjectHtml = '';
  66. let otherFileHtml = '';
  67. for(let tmp of data.other) {
  68. otherProjectHtml += '<option value="'+ tmp.ID +'">'+ tmp.name +'</option>';
  69. otherFileData[tmp.ID] = tmp.unitPriceList;
  70. if (otherFileHtml !== '') {
  71. continue;
  72. }
  73. for(let unitPrice of tmp.unitPriceList) {
  74. otherFileHtml += '<option value="'+ unitPrice.id +'">'+ unitPrice.name +'</option>';
  75. }
  76. }
  77. $("#other-project").html(otherProjectHtml);
  78. $("#other-file").html(otherFileHtml);
  79. }
  80. }
  81. });
  82. });
  83. // 单价文件另存为弹框
  84. $("#file-save-as-dialog").on('shown.bs.modal', function() {
  85. // 获取当前建设项数据
  86. $("#save-as-name").val(usedUnitPriceInfo.name + '(复件)');
  87. });
  88. // 单价文件另存为操作
  89. $("#save-as-confirm").click(function() {
  90. let name = $("#save-as-name").val();
  91. if (name === '') {
  92. $("#save-as-tips").text('请填写单价文件名称').show();
  93. return false;
  94. }
  95. if (isChanging) {
  96. return false;
  97. }
  98. $.ajax({
  99. url: '/glj/save-as',
  100. type: 'post',
  101. data: {name: name, project_id: scUrlUtil.GetQueryString('project')},
  102. dataType: 'json',
  103. error: function() {
  104. isChanging = false;
  105. },
  106. beforeSend: function() {
  107. isChanging = true;
  108. },
  109. success: function(response) {
  110. isChanging = false;
  111. if (response.err === 1) {
  112. let msg = response.msg !== undefined && response.msg !== '' ? response.msg : '另存为失败!';
  113. $("#save-as-tips").text(msg).show();
  114. return false;
  115. }
  116. $("#file-save-as-dialog").modal("hide");
  117. }
  118. });
  119. });
  120. // 从其他建设项目中复制 选择建设项目
  121. $("#other-project").change(function() {
  122. let projectId = $(this).val();
  123. if (otherFileData[projectId] === undefined) {
  124. return false;
  125. }
  126. let otherFileHtml = '';
  127. for(let unitPrice of otherFileData[projectId]) {
  128. otherFileHtml += '<option value="'+ unitPrice.id +'">'+ unitPrice.name +'</option>';
  129. }
  130. $("#other-file").html(otherFileHtml);
  131. });
  132. // 单价文件选项切换
  133. $("input[name='change-type']").change(function() {
  134. let type = $(this).val();
  135. type = parseInt(type);
  136. $("#change-dj .option").hide();
  137. if (type === 0) {
  138. $(".option.select").show();
  139. } else {
  140. $(".option.copy").show();
  141. }
  142. });
  143. // 单价文件切换确认
  144. $("#change-file-confirm").click(function() {
  145. if (isChanging) {
  146. return false;
  147. }
  148. let type = $("input[name='change-type']:checked").val();
  149. type = parseInt(type);
  150. let changeUnitPriceId = 0;
  151. if (type === 0) {
  152. // 从本项目中选择
  153. changeUnitPriceId = $("#self-file").val();
  154. } else {
  155. // 从其他项目中复制
  156. changeUnitPriceId = $("#other-file").val();
  157. }
  158. $('#change-dj').modal("hide");
  159. $.bootstrapLoading.start();
  160. $.ajax({
  161. url: '/glj/change-file',
  162. type: 'post',
  163. data: {project_id: scUrlUtil.GetQueryString('project'), change_id: changeUnitPriceId, type: type},
  164. error: function() {
  165. isChanging = false;
  166. $.bootstrapLoading.end();
  167. },
  168. beforeSend: function() {
  169. isChanging = true;
  170. },
  171. success: function(response) {
  172. isChanging = false;
  173. if (response.err === 1) {
  174. let msg = response.msg !== undefined ? response.msg : '未知错误';
  175. alert(msg);
  176. $.bootstrapLoading.end();
  177. return false;
  178. }
  179. projectObj.project.projectGLJ.loadData(function () {
  180. let projectGLJ = projectObj.project.projectGLJ;
  181. projectGLJ.loadCacheData();
  182. unitPriceFileInit();
  183. gljOprObj.refreshView();
  184. $.bootstrapLoading.end();
  185. });
  186. }
  187. });
  188. });
  189. // 是否主动更改数据
  190. $("#message").on('click', '#load-data', function() {
  191. $("#message").html('正在加载...');
  192. // 重新加载数据到缓存
  193. projectObj.project.projectGLJ.loadData(function() {
  194. projectObj.project.projectGLJ.loadCacheData();
  195. $("#notify").slideUp('fast');
  196. });
  197. });
  198. $('#pop-dj').popover({
  199. placement:"bottom",
  200. html:true,
  201. trigger:"hover | focus",
  202. content: getUsedTenderInfo
  203. }
  204. );
  205. });
  206. /**
  207. * 初始化数据
  208. *
  209. * @return {void|boolean}
  210. */
  211. function init() {
  212. projectObj.project.projectGLJ.loadData(function(data) {
  213. if (jsonData.length <= 0) {
  214. // 赋值
  215. jsonData = data.gljList !== undefined && data.gljList.length > 0 ? data.gljList : [];
  216. jsonData= filterProjectGLJ(jsonData);
  217. mixRatioConnectData = data.mixRatioConnectData !== undefined ? data.mixRatioConnectData : mixRatioConnectData;
  218. mixRatioMap = data.mixRatioMap !== undefined ? data.mixRatioMap : mixRatioMap;
  219. host = data.constData.hostname !== undefined ? data.constData.hostname : '';
  220. materialIdList = data.constData.materialIdList !== undefined ? data.constData.materialIdList : materialIdList;
  221. roomId = data.constData.roomId !== undefined ? data.constData.roomId : roomId;
  222. canNotChangeTypeId = data.constData.ownCompositionTypes !== undefined ?
  223. data.constData.ownCompositionTypes : canNotChangeTypeId;
  224. GLJTypeConst = data.constData.GLJTypeConst !== undefined ? JSON.parse(data.constData.GLJTypeConst) : GLJTypeConst;
  225. // 连接socket服务器
  226. socketInit();
  227. unitPriceFileInit();
  228. setTimeout(spreadInit, 1);
  229. } else {
  230. projectObj.project.projectGLJ.loadCacheData();
  231. }
  232. });
  233. }
  234. /**
  235. * spreadjs相关初始化
  236. *
  237. * @return {void}
  238. */
  239. function spreadInit() {
  240. projectGLJSpread = new ProjectGLJSpread();
  241. projectGLJSpread.successCallback = successTrigger;
  242. projectGLJSheet = projectGLJSpread.init();
  243. // 绑定单击事件
  244. let lastRow = 0;
  245. projectGLJSheet.bind(GC.Spread.Sheets.Events.CellClick, function (element, info) {
  246. let currentRow = info.row;
  247. if (currentRow === undefined || currentRow === lastRow) {
  248. return;
  249. }
  250. if (currentTag !== 'mix-ratio' && currentTag !== 'machine') {
  251. return;
  252. }
  253. let spread = currentTag === 'mix-ratio' ? mixRatioSpread : machineSpread;
  254. if (spread === null) {
  255. return;
  256. }
  257. let projectGLJId = projectGLJSheet.getActiveDataByField('id');
  258. spread.getRatioData(projectGLJId);
  259. lastRow = currentRow;
  260. });
  261. // 切换tab触发refresh
  262. $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
  263. currentTag = $(e.target).data('name');
  264. if (currentTag === 'ration') {
  265. projectGLJSheet.filterData('unit_price.type', []);
  266. }
  267. });
  268. loadSize("glj-main", function() {
  269. projectGLJSpread.sheetObj.spread.refresh();
  270. });
  271. }
  272. /**
  273. * 单价文件相关初始化
  274. *
  275. * @param {String} name
  276. * @param {Array} data
  277. * @return {void}
  278. */
  279. function unitPriceFileInit() {
  280. let projectGLJ = projectObj.project.projectGLJ;
  281. let data = projectGLJ.datas;
  282. usedTenderList = data.usedTenderList !== undefined ? data.usedTenderList : [];
  283. usedUnitPriceInfo = data.constData.usedUnitPriceInfo !== undefined ?
  284. data.constData.usedUnitPriceInfo : {};
  285. $("#used-name").text(usedUnitPriceInfo.name);
  286. let usedCount = usedTenderList.length <= 0 ? 1 : usedTenderList.length;
  287. $("#used-count").text(usedCount);
  288. }
  289. function getUsedTenderInfo() {
  290. return usedTenderList.join("<br>");
  291. }
  292. /**
  293. * 成功事件
  294. *
  295. * @param {string} field
  296. * @param {object} info
  297. * @return {void}
  298. */
  299. function successTrigger(field, info) {
  300. let updateData = {};
  301. switch (field) {
  302. case 'unit_price.market_price':
  303. // 计算价格
  304. updateData = projectGLJSpread.priceCalculate(info);
  305. // 触发websocket通知
  306. socket.emit('dataNotify', JSON.stringify(info));
  307. console.log(info);
  308. break;
  309. case 'supply':
  310. // 供货方式更改成功后
  311. projectGLJSpread.changeSupplyType(info);
  312. break;
  313. }
  314. // 重新加载数据到缓存
  315. projectObj.project.projectGLJ.loadData();
  316. // 更新定额工料机
  317. gljOprObj.refreshView();
  318. }
  319. /**
  320. * socket.io相关初始化
  321. *
  322. * @return {void}
  323. */
  324. function socketInit() {
  325. if (socket === null) {
  326. socket = io('http://'+ host +':3300');
  327. socket.on('connect', function () {
  328. socket.emit('join', roomId);
  329. console.log('单价文件同步连接成功');
  330. });
  331. }
  332. // 接收到改变
  333. socket.on('dataChange', function(data) {
  334. data = JSON.parse(data);
  335. if (data.newValue === undefined) {
  336. return false;
  337. }
  338. $("#message").html('市场单位已被修改,<a href="javascript:void(0);" id="load-data">点击加载</a>');
  339. $("#notify").slideDown('fast');
  340. });
  341. }
  342. //过滤消耗量为0的项目工料机
  343. function filterProjectGLJ(jsonData) {
  344. if (jsonData.length > 0) {
  345. // 不显示消耗量为0的数据
  346. let tmpData = [];
  347. for(let data of jsonData) {
  348. if (data.quantity !== 0&&data.quantity !=='0') {
  349. tmpData.push(data);
  350. }
  351. }
  352. jsonData = tmpData;
  353. }
  354. return jsonData;
  355. }