project_glj.js 12 KB

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