project_glj.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. // 加载工料机数据
  190. let data = projectObj.project.projectGLJ === null ? null : projectObj.project.projectGLJ.datas;
  191. if (data === null) {
  192. return false;
  193. }
  194. // 赋值
  195. jsonData = data.gljList !== undefined && data.gljList.length > 0 ? data.gljList : [];
  196. mixRatioConnectData = data.mixRatioConnectData !== undefined ? data.mixRatioConnectData : mixRatioConnectData;
  197. host = data.constData.hostname !== undefined ? data.constData.hostname : '';
  198. materialIdList = data.constData.materialIdList !== undefined ? data.constData.materialIdList : materialIdList;
  199. roomId = data.constData.roomId !== undefined ? data.constData.roomId : roomId;
  200. canNotChangeTypeId = data.constData.ownCompositionTypes !== undefined ?
  201. data.constData.ownCompositionTypes : canNotChangeTypeId;
  202. GLJTypeConst = data.constData.GLJTypeConst !== undefined ? JSON.parse(data.constData.GLJTypeConst) : GLJTypeConst;
  203. let usedTenderList = data.usedTenderList !== undefined ? data.usedTenderList : [];
  204. usedUnitPriceInfo = data.constData.usedUnitPriceInfo !== undefined ?
  205. data.constData.usedUnitPriceInfo : {};
  206. // 连接socket服务器
  207. socketInit();
  208. unitPriceFileInit(usedUnitPriceInfo.name, usedTenderList);
  209. setTimeout(spreadInit, 1);
  210. }
  211. /**
  212. * spreadjs相关初始化
  213. *
  214. * @return {void}
  215. */
  216. function spreadInit() {
  217. projectGLJSpread = new ProjectGLJSpread();
  218. projectGLJSpread.successCallback = successTrigger;
  219. projectGLJSheet = projectGLJSpread.init();
  220. // 绑定单击事件
  221. let lastRow = 0;
  222. projectGLJSheet.bind(GC.Spread.Sheets.Events.CellClick, function (element, info) {
  223. let currentRow = info.row;
  224. if (currentRow === undefined || currentRow === lastRow) {
  225. return;
  226. }
  227. if (currentTag !== 'mix-ratio' && currentTag !== 'machine') {
  228. return;
  229. }
  230. let spread = currentTag === 'mix-ratio' ? mixRatioSpread : machineSpread;
  231. if (spread === null) {
  232. return;
  233. }
  234. let projectGLJId = projectGLJSheet.getActiveDataByField('id');
  235. spread.getRatioData(projectGLJId);
  236. lastRow = currentRow;
  237. });
  238. // 切换tab触发refresh
  239. $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
  240. currentTag = $(e.target).data('name');
  241. if (currentTag === 'ration') {
  242. projectGLJSheet.filterData('unit_price.type', []);
  243. }
  244. });
  245. }
  246. /**
  247. * 单价文件相关初始化
  248. *
  249. * @param {String} name
  250. * @param {Array} data
  251. * @return {void}
  252. */
  253. function unitPriceFileInit(name, data) {
  254. $("#used-name").text(name);
  255. let usedCount = data.length <= 0 ? 1 : data.length;
  256. $("#used-count").text(usedCount);
  257. $('#pop-dj').popover({
  258. placement:"bottom",
  259. html:true,
  260. trigger:"hover | focus",
  261. content: data.join('<br>')
  262. }
  263. );
  264. }
  265. /**
  266. * 成功事件
  267. *
  268. * @param {string} field
  269. * @param {object} info
  270. * @return {void}
  271. */
  272. function successTrigger(field, info) {
  273. let updateData = {};
  274. switch (field) {
  275. case 'unit_price.market_price':
  276. // 计算价格
  277. updateData = projectGLJSpread.priceCalculate(info);
  278. // 触发websocket通知
  279. socket.emit('dataNotify', JSON.stringify(info));
  280. console.log(info);
  281. break;
  282. case 'supply':
  283. // 供货方式更改成功后
  284. projectGLJSpread.changeSupplyType(info);
  285. break;
  286. }
  287. // 重新加载数据到缓存
  288. projectObj.project.projectGLJ.loadData();
  289. // 更新定额工料机
  290. gljOprObj.refreshView();
  291. }
  292. /**
  293. * socket.io相关初始化
  294. *
  295. * @return {void}
  296. */
  297. function socketInit() {
  298. if (socket === null) {
  299. socket = io('http://'+ host +':3300');
  300. socket.on('connect', function () {
  301. socket.emit('join', roomId);
  302. console.log('单价文件同步连接成功');
  303. });
  304. }
  305. // 接收到改变
  306. socket.on('dataChange', function(data) {
  307. data = JSON.parse(data);
  308. if (data.newValue === undefined) {
  309. return false;
  310. }
  311. $("#message").html('市场单位已被修改,<a href="javascript:void(0);" id="load-data">点击加载</a>');
  312. $("#notify").slideDown('fast');
  313. });
  314. }