project_glj.js 12 KB

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