gz_safety_ui/js/aq_inventory/project_material_statistics.js

445 lines
15 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 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('');
layui.form.render();
reloadTable(1);
}
}
// 刷新页面数据
function reloadData() {
reloadTable(1);
getStatistics();
}
// 重载表格
function reloadTable(pageNum) {
table.reload("currentTableId", {
where: {
encryptedData: JSON.stringify({
'keyWord': $('#keyWord').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({
'keyWord': $('#keyWord').val(),
'leaseUnit': $('#useUnit').val(),
'isShortage': $('#isShortage').val()
}),
},
request: {
pageName: 'pageNum',
limitName: 'pageSize'
},
parseData: function (res) { // res 即为原始返回的数据
if(res.code === 401){
closeWindowOpen();
}
// 确保数据按工程名称排序
res.list.sort(function(a, b) {
if (a.proName < b.proName) return -1;
if (a.proName > b.proName) return 1;
return 0;
});
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: '9%',
title: "工程名称",
unresize: true,
align: "center",
sort: true
},
{
field: "type",
width: '8%',
title: "物资类型",
unresize: true,
align: "center",
sort: true,
},
{
field: "name",
width: '8%',
title: "物资名称",
unresize: true,
align: "center",
sort: true,
},
{
field: "model",
width: '8%',
title: "规格型号",
unresize: true,
align: "center",
sort: true,
},
{
field: "unit",
width: '5%',
title: "单位",
unresize: true,
align: "center",
sort: true,
},
{
field: "needNum",
width: '6%',
title: "需用量",
unresize: true,
align: "center",
sort: true,
templet: function (d) {
return d.totalCg || 0;
},
},
{
field: "totalCg",
width: '6%',
title: "采购量",
unresize: true,
align: "center",
sort: true,
templet: function (d) {
return d.totalCg || 0;
},
},
{
field: "totalLk",
title: "利库量",
width: '6%',
unresize: true,
align: "center",
sort: true,
templet: function (d) {
const totalLk = d.totalLk || 0;
const rowData = JSON.stringify(d).replace(/"/g, '&quot;');
return totalLk > 0
? `<a style='color: #1E9FFF' onclick='viewDetail(${rowData},1)'>${totalLk}</a>`
: `<span style='color: #666'>${totalLk}</span>`;
},
},
{
field: "price",
width: '6%',
title: "单价",
unresize: true,
align: "center",
sort: true,
templet: function (d) {
return d.price || 0;
},
},
{
field: "totalCk",
width: '7%',
title: "出库总量",
unresize: true,
align: "center",
sort: true,
templet: function (d) {
const totalCk = d.totalCk || 0;
const rowData = JSON.stringify(d).replace(/"/g, '&quot;');
return totalCk > 0
? `<a style='color: #1E9FFF' onclick='viewDetail(${rowData},2)'>${totalCk}</a>`
: `<span style='color: #666'>${totalCk}</span>`;
},
},
{
field: "totalPrice",
width: '6%',
title: "总价",
unresize: true,
align: "center",
sort: true,
templet: function (d) {
return d.totalPrice || 0;
},
},
{
field: "totalBack",
width: '7%',
title: "退还数量",
unresize: true,
align: "center",
sort: true,
templet: function (d) {
const totalBack = d.totalBack || 0;
const rowData = JSON.stringify(d).replace(/"/g, '&quot;');
return totalBack > 0
? `<a style='color: #1E9FFF' onclick='viewDetail(${rowData},3)'>${totalBack}</a>`
: `<span style='color: #666'>${totalBack}</span>`;
},
},
{
field: "totalDiff",
width: '6%',
title: "差缺量",
unresize: true,
align: "center",
sort: true,
templet: function (d) {
let totalDiff = d.totalDiff || 0;
if (totalDiff > 0) {
return '<span style="background-color: #f56c6c; color: #fff; border-radius: 5px; padding: 2px 5px;">' + totalDiff + '</span>';
}
return totalDiff;
},
},
{
field: "totalDiffPrice",
width: '7%',
title: "差缺总价",
unresize: true,
align: "center",
sort: true,
templet: function (d) {
let totalDiffPrice = d.totalDiffPrice || 0;
if (totalDiffPrice > 0) {
return '<span style="background-color: #f56c6c; color: #fff; border-radius: 5px; padding: 2px 5px;">' + totalDiffPrice + '</span>';
}
return totalDiffPrice;
},
},
],
],
limits: [10, 20, 30, 50, 100],
limit: 50,
page: false,
done: function (res, curr, count) {
pageNum = tableIns.config.page.curr;
table.resize("currentTableId");
// 延迟执行合并操作,确保表格完全渲染
setTimeout(function() {
mergeProNameCells();
}, 100);
}
});
}
// 使用原生JavaScript合并工程名称相同的单元格
function mergeProNameCells() {
// 获取表格元素
let tableElem = document.getElementById('currentTableId');
if (!tableElem) {
console.error('找不到表格元素');
return;
}
// 获取表格的tbody
let tbody = tableElem.nextElementSibling.querySelector('.layui-table-body tbody');
if (!tbody) {
console.error('找不到表格tbody元素');
return;
}
// 获取所有行
let rows = tbody.querySelectorAll('tr');
if (rows.length <= 1) return;
console.log('开始合并工程名称相同的单元格,共' + rows.length + '行');
let startRow = 0;
let mergeCount = 1;
let currentProName = getProNameFromRow(rows[0]);
for (let i = 1; i < rows.length; i++) {
let nextProName = getProNameFromRow(rows[i]);
console.log('比较行 ' + i + ':当前=' + currentProName + ',下一个=' + nextProName);
if (currentProName === nextProName) {
mergeCount++;
console.log('行 ' + i + ' 与前一行工程名称相同,合并计数:' + mergeCount);
} else {
// 执行合并
if (mergeCount > 1) {
console.log('合并行 ' + startRow + ' 到 ' + (i - 1) + ',共 ' + mergeCount + ' 行');
mergeCells(rows, startRow, 1, mergeCount); // 工程名称在第2列索引1
}
// 重置计数
startRow = i;
mergeCount = 1;
currentProName = nextProName;
}
}
// 合并最后一组
if (mergeCount > 1) {
console.log('合并最后一组:行 ' + startRow + ' 到 ' + (rows.length - 1) + ',共 ' + mergeCount + ' 行');
mergeCells(rows, startRow, 1, mergeCount); // 工程名称在第2列索引1
}
console.log('单元格合并完成');
}
// 从行中获取工程名称
function getProNameFromRow(row) {
// 工程名称在第2列索引1
let cell = row.querySelectorAll('td')[1];
if (cell) {
return cell.textContent.trim();
}
return '';
}
// 合并单元格
function mergeCells(rows, startRow, colIndex, rowspan) {
if (startRow < 0 || startRow >= rows.length) return;
// 获取起始行的单元格
let startCell = rows[startRow].querySelectorAll('td')[colIndex];
if (!startCell) return;
// 设置rowspan
startCell.rowSpan = rowspan;
// 隐藏被合并的单元格
for (let i = 1; i < rowspan; i++) {
let cell = rows[startRow + i].querySelectorAll('td')[colIndex];
if (cell) {
cell.style.display = 'none';
}
}
}
// 查看详情
function viewDetail(obj, type) {
console.log('查看详情', type, obj);
if (type === 1) {
openIframeByParamObj("projectLkDetails", "利库详情", "./child/project_lk_details.html", "92%", "95%", obj, 0);
} else if (type===2){
openIframeByParamObj("projectCkDetails", "出库详情", "./child/project_ck_details.html", "92%", "95%", obj, 0);
} else {
openIframeByParamObj("projectTkDetails", "退库详情", "./child/project_tk_details.html", "92%", "95%", obj, 0);
}
}
// 导出Excel
function exportExcel() {
let keyWord = $('#keyWord').val();
let isShortage = $('#isShortage').val();
let params = {
'keyWord': keyWord,
'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 });
});
}