363 lines
12 KiB
JavaScript
363 lines
12 KiB
JavaScript
let form, table;
|
||
let tableIns;
|
||
let pageNum = 1; // 定义分页
|
||
let proListData = []; // 存储工程列表数据(新增)
|
||
|
||
layui.use(["form", "table"], function () {
|
||
form = layui.form;
|
||
table = layui.table;
|
||
getProList(); // 新增:加载工程下拉数据
|
||
initTable();
|
||
getStatistics();
|
||
});
|
||
|
||
// 新增:获取工程列表数据
|
||
function getProList() {
|
||
let url = dataUrl + 'backstage/planApply/getProSelectAndCompanyName';
|
||
ajaxRequest(url, "POST", null, false, function () {
|
||
console.log("开始获取工程列表...");
|
||
}, function (result) {
|
||
console.log("获取工程列表结果:", result);
|
||
if (result.code === 200) {
|
||
if (result.data && result.data.length > 0) {
|
||
console.log("工程数据:", result.data);
|
||
proListData = result.data;
|
||
setSelectProData(result.data); // 渲染工程下拉选
|
||
} else {
|
||
console.warn("工程列表为空");
|
||
setSelectProData([]);
|
||
}
|
||
} else {
|
||
console.error("获取工程列表失败:", result.msg);
|
||
}
|
||
}, function (xhr, status, error) {
|
||
console.error("获取工程列表异常:", error);
|
||
errorFn(xhr, status, error)
|
||
}, null);
|
||
}
|
||
|
||
// 新增:渲染工程下拉选
|
||
function setSelectProData(proList) {
|
||
let html = '<option value="">请选择工程</option>';
|
||
$.each(proList || [], function (index, item) {
|
||
// 拼接工程名称和公司名称(如果有)
|
||
let proText = item.name || item.proName || '';
|
||
if (item.companyName) {
|
||
proText += ' - ' + item.companyName;
|
||
}
|
||
html += '<option value="' + item.id + '">' + proText + '</option>';
|
||
});
|
||
|
||
$('#projectId').empty().append(html);
|
||
|
||
// 重新渲染Layui表单
|
||
if (layui && layui.form) {
|
||
layui.form.render('select');
|
||
}
|
||
}
|
||
|
||
// 数据概览
|
||
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 keyWord = $('#keyWord').val();
|
||
let flag = checkValue(keyWord);
|
||
if (flag) {
|
||
$('#keyWord').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) {
|
||
$('#keyWord').val('');
|
||
$('#useUnit').val('');
|
||
$('#isShortage').val('');
|
||
$('#projectId').val(''); // 新增:重置工程下拉选
|
||
layui.form.render();
|
||
reloadTable(1);
|
||
}
|
||
}
|
||
|
||
// 刷新页面数据
|
||
function reloadData() {
|
||
reloadTable(1);
|
||
getStatistics();
|
||
}
|
||
|
||
// 重载表格 - 修改:添加工程筛选条件
|
||
function reloadTable(pageNum) {
|
||
table.reload("currentTableId", {
|
||
page: {
|
||
curr: pageNum ? pageNum : 1,
|
||
},
|
||
where: {
|
||
encryptedData: JSON.stringify({
|
||
'keyWord': $('#keyWord').val(),
|
||
'proId': $('#projectId').val() // 新增:工程ID筛选条件
|
||
}),
|
||
},
|
||
},
|
||
);
|
||
}
|
||
|
||
// 初始化表格 - 修改:添加工程筛选条件
|
||
function initTable() {
|
||
tableIns = table.render({
|
||
elem: "#currentTableId",
|
||
id: 'currentTableId',
|
||
headers: {
|
||
authorization: sessionStorage.getItem("gz-token"),
|
||
},
|
||
// height: "full-300",
|
||
url: dataUrl + "backstage/inventoryCount/getSafetyToolsLedger",
|
||
where: {
|
||
encryptedData: JSON.stringify({
|
||
'keyWord': $('#keyWord').val(),
|
||
'proId': $('#projectId').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: '15%',
|
||
title: "工程名称",
|
||
unresize: true,
|
||
align: "center",
|
||
},
|
||
{
|
||
field: "name",
|
||
width: '8%',
|
||
title: "物资名称",
|
||
unresize: true,
|
||
align: "center",
|
||
},
|
||
{
|
||
field: "model",
|
||
width: '8%',
|
||
title: "规格型号",
|
||
unresize: true,
|
||
align: "center",
|
||
},
|
||
{
|
||
field: "unit",
|
||
width: '5%',
|
||
title: "单位",
|
||
unresize: true,
|
||
align: "center",
|
||
},
|
||
{
|
||
field: "planApplyCode",
|
||
width: '10%',
|
||
title: "计划编号",
|
||
unresize: true,
|
||
align: "center",
|
||
},
|
||
{
|
||
field: "needNum",
|
||
width: '7%',
|
||
title: "总需用量",
|
||
unresize: true,
|
||
align: "center",
|
||
templet: function (d) {
|
||
return d.needNum || 0;
|
||
},
|
||
},
|
||
{
|
||
field: "price",
|
||
width: '8%',
|
||
title: "单价",
|
||
unresize: true,
|
||
align: "center",
|
||
},
|
||
{
|
||
field: "totalPrice",
|
||
width: '8%',
|
||
title: "总价",
|
||
unresize: true,
|
||
align: "center",
|
||
templet: function (d) {
|
||
// 转换为数字,空值默认0,避免NaN
|
||
const price = Number(d.price || 0);
|
||
const needNum = Number(d.needNum || 0);
|
||
// 计算总价并保留2位小数(金额常用格式)
|
||
const totalPrice = (price * needNum).toFixed(2);
|
||
return totalPrice;
|
||
},
|
||
},
|
||
{
|
||
field: "outInfo",
|
||
width: '14.5%',
|
||
title: "发货时间",
|
||
unresize: true,
|
||
align: "center",
|
||
},
|
||
{
|
||
field: "outAddress",
|
||
width: '15%',
|
||
title: "发货地址",
|
||
unresize: true,
|
||
align: "center",
|
||
},
|
||
{
|
||
field: "supName",
|
||
width: '10%',
|
||
title: "供应商",
|
||
unresize: true,
|
||
align: "center",
|
||
},
|
||
{
|
||
field: "backNum",
|
||
width: '8%',
|
||
title: "退还数量",
|
||
align: "center",
|
||
templet: function (d) {
|
||
return Number(d.backNum ?? 0);
|
||
}
|
||
},
|
||
{
|
||
field: "backTime",
|
||
width: '15.5%',
|
||
title: "退还时间",
|
||
align: "center",
|
||
},
|
||
{
|
||
title: "差缺",
|
||
width: '8%',
|
||
align: "center",
|
||
templet: function (d) {
|
||
let needNum = Number(d.needNum ?? 0);
|
||
let backNum = Number(d.backNum ?? 0);
|
||
|
||
let diff = needNum - backNum; // 允许为负数
|
||
return diff;
|
||
}
|
||
},
|
||
{
|
||
title: "差缺总价",
|
||
width: '8%',
|
||
align: "center",
|
||
templet: function (d) {
|
||
// 转换为数字,空值默认0,避免NaN
|
||
let needNum = Number(d.needNum ?? 0);
|
||
let backNum = Number(d.backNum ?? 0);
|
||
let price = Number(d.price || 0);
|
||
|
||
// 计算差缺数量
|
||
let diff = needNum - backNum;
|
||
// 计算差缺总价 = 差缺数量 × 单价,保留2位小数
|
||
let diffTotalPrice = (diff * price).toFixed(2);
|
||
|
||
return diffTotalPrice;
|
||
}
|
||
},
|
||
{
|
||
field: "remark",
|
||
width: '6%',
|
||
title: "备注",
|
||
align: "center",
|
||
},
|
||
|
||
],
|
||
],
|
||
limits: [10, 20, 30, 50, 100],
|
||
limit: 10,
|
||
page: true,
|
||
done: function (res, curr, count) {
|
||
pageNum = tableIns.config.page.curr;
|
||
table.resize("currentTableId");
|
||
},
|
||
});
|
||
}
|
||
|
||
// 导出Excel - 修改:添加工程筛选条件
|
||
function exportExcel() {
|
||
let keyWord = $('#keyWord').val();
|
||
let projectId = $('#projectId').val();
|
||
|
||
let params = {
|
||
'keyWord': keyWord,
|
||
'proId': projectId // 新增:工程ID筛选条件
|
||
};
|
||
|
||
let encryptedData = JSON.stringify(params);
|
||
let url = dataUrl + 'backstage/inventoryCount/exportLedgerList?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 });
|
||
});
|
||
} |