project_glj.js 13 KB

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