let form, table; let tableIns; let pageNum = 1; // 定义分页 layui.use(["form", "table"], function () { form = layui.form; table = layui.table; initTable(); getStatistics(); }); // 数据概览 function getStatistics() { let encryptedData = {}; let url = dataUrl + 'backstage/projectMaterial/getStatistics?encryptedData=' + encodeURIComponent(JSON.stringify(encryptedData)); ajaxRequest(url, "GET", null, true, function () { }, function (result) { if (result.code === 200) { setNum(result.data); } }, function (xhr, status, error) { errorFn(xhr, status, error) }, null); // 数据概览赋值 function setNum(obj) { $('#num').html(parseInt(obj.projectCount || 0)); $('#num2').html(parseInt(obj.totalPurchaseCount || 0)); $('#num3').html(parseInt(obj.totalOutCount || 0)); $('#num4').html(parseInt(obj.totalShortageCount || 0)); } } // 查询/重置 function queryTable(type) { if (type === 1) { let projectName = $('#projectName').val(); let flag = checkValue(projectName); if (flag) { $('#projectName').val(''); return layer.msg('工程名称查询包含特殊字符,请重新输入', { icon: 2 }); } let useUnit = $('#useUnit').val(); let flag2 = checkValue(useUnit); if (flag2) { $('#useUnit').val(''); return layer.msg('领用单位查询包含特殊字符,请重新输入', { icon: 2 }); } reloadTable(1); } else if (type === 2) { $('#projectName').val(''); $('#useUnit').val(''); $('#isShortage').val(''); layui.form.render(); reloadTable(1); } } // 刷新页面数据 function reloadData() { reloadTable(1); getStatistics(); } // 重载表格 function reloadTable(pageNum) { table.reload("currentTableId", { where: { encryptedData: JSON.stringify({ 'proName': $('#projectName').val(), 'isShortage': $('#isShortage').val() }), }, }, ); } // 初始化表格 function initTable() { tableIns = table.render({ elem: "#currentTableId", id: 'currentTableId', headers: { authorization: sessionStorage.getItem("gz-token"), }, // height: "full-300", url: dataUrl + "backstage/inventoryCount/queryLedgerListByProjectAndModel", where: { encryptedData: JSON.stringify({ 'proName': $('#projectName').val(), 'leaseUnit': $('#useUnit').val(), 'isShortage': $('#isShortage').val() }), }, request: { pageName: 'pageNum', limitName: 'pageSize' }, parseData: function (res) { // res 即为原始返回的数据 if(res.code === 401){ closeWindowOpen(); } return { "code": 0, // 解析接口状态 "msg": '获取成功', // 解析提示文本 "count": res.total, // 解析数据长度 "data": res.list // 解析数据列表 }; }, cols: [ [ { width: '5%', title: "序号", align: "center", templet: function (d) { return d.LAY_NUM; }, }, { field: "proName", width: '10%', title: "工程名称", unresize: true, align: "center", sort: true, }, { field: "type", width: '10%', title: "物资类型", unresize: true, align: "center", sort: true, }, { field: "name", width: '10%', title: "物资名称", unresize: true, align: "center", sort: true, }, { field: "model", width: '10%', title: "规格型号", unresize: true, align: "center", sort: true, }, { field: "unit", width: '6%', title: "单位", unresize: true, align: "center", sort: true, }, { field: "needNum", width: '8%', title: "需用量", unresize: true, align: "center", sort: true, templet: function (d) { return d.totalCg || 0; }, }, { field: "totalCg", width: '8%', title: "采购量", unresize: true, align: "center", sort: true, templet: function (d) { return d.totalCg || 0; }, }, { field: "totalLk", title: "利库量", width: '8%', unresize: true, align: "center", sort: true, templet: function (d) { return d.totalLk || 0; }, }, { field: "totalCk", width: '8%', title: "出库总量", unresize: true, align: "center", sort: true, templet: function (d) { return d.totalCk || 0; }, }, { field: "totalBack", width: '8%', title: "退还数量", unresize: true, align: "center", sort: true, templet: function (d) { return d.totalBack || 0; }, }, { field: "totalDiff", width: '8%', title: "差缺量", unresize: true, align: "center", sort: true, templet: function (d) { let totalDiff = d.totalDiff || 0; if (totalDiff > 0) { return '' + totalDiff + ''; } return totalDiff; }, }, ], ], limits: [10, 20, 30, 50, 100], limit: 50, page: false, done: function (res, curr, count) { pageNum = tableIns.config.page.curr; table.resize("currentTableId"); }, }); } // 查看详情 function viewDetail(id) { layer.msg('查看工程物料详情功能待开发', { icon: 1 }); // 这里可以添加查看详情的逻辑,比如打开新窗口或弹窗显示详细信息 // openIframe('工程物料详情', 'page/aq_inventory/project_material_detail.html?id=' + id); } // 导出Excel function exportExcel() { let projectName = $('#projectName').val(); let isShortage = $('#isShortage').val(); let params = { 'proName': projectName, 'isShortage': isShortage }; let encryptedData = JSON.stringify(params); let url = dataUrl + 'backstage/inventoryCount/exportLedgerListByProjectAndModel?encryptedData=' + encodeURIComponent(encryptedData); // 创建隐藏的下载链接 let link = document.createElement('a'); link.href = url; link.download = '工程物料统计_' + new Date().getTime() + '.xlsx'; link.style.display = 'none'; // 添加授权头并下载 fetch(url, { method: 'GET', headers: { 'authorization': sessionStorage.getItem("gz-token") } }).then(response => { if (response.ok) { return response.blob(); } throw new Error('导出失败'); }).then(blob => { let downloadUrl = window.URL.createObjectURL(blob); link.href = downloadUrl; document.body.appendChild(link); link.click(); document.body.removeChild(link); window.URL.revokeObjectURL(downloadUrl); layer.msg('导出成功', { icon: 1 }); }).catch(error => { layer.msg('导出失败:' + error.message, { icon: 2 }); }); }