project_glj.js 11 KB

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