project_glj.js 11 KB

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