370 lines
12 KiB
JavaScript
370 lines
12 KiB
JavaScript
const axios = require('axios');
|
|
const colors = require('colors');
|
|
|
|
/**
|
|
* 指派人员数据更新修复验证脚本
|
|
* 功能:验证修复后的指派功能,确保维修员姓名和备注正确保存
|
|
* 时间:2025-06-30 19:00:00
|
|
* 说明:测试维修人员查询和分派功能的完整流程
|
|
*/
|
|
|
|
// 配置信息
|
|
const CONFIG = {
|
|
baseURL: 'http://localhost/dev-api',
|
|
timeout: 10000,
|
|
headers: {
|
|
'Content-Type': 'application/json;charset=utf-8',
|
|
clientid: 'e5cd7e4891bf95d1d19206ce24a7b32e',
|
|
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
|
|
referer: 'http://localhost/login?redirect=%252Findex',
|
|
},
|
|
};
|
|
|
|
// 获取Token
|
|
async function getToken() {
|
|
const { getToken } = require('./quick-token-getter');
|
|
return await getToken();
|
|
}
|
|
|
|
/**
|
|
* 通用API调用方法
|
|
*/
|
|
async function callAPI(endpoint, options = {}) {
|
|
try {
|
|
const response = await axios({
|
|
method: options.method || 'GET',
|
|
url: `${CONFIG.baseURL}${endpoint}`,
|
|
data: options.data,
|
|
params: options.params,
|
|
headers: {
|
|
...CONFIG.headers,
|
|
Authorization: options.token ? `Bearer ${options.token}` : undefined,
|
|
...options.headers,
|
|
},
|
|
timeout: CONFIG.timeout,
|
|
});
|
|
|
|
return {
|
|
success: true,
|
|
data: response.data,
|
|
status: response.status,
|
|
};
|
|
} catch (error) {
|
|
return {
|
|
success: false,
|
|
error: error.response?.data || error.message,
|
|
status: error.response?.status || 'ERROR',
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 步骤1:测试维修人员查询接口
|
|
*/
|
|
async function testMaintenanceWorkersAPI(token) {
|
|
console.log('🔧 步骤1:测试维修人员查询接口'.blue);
|
|
|
|
const result = await callAPI('/pm/repairOrder/maintenance-workers', {
|
|
token: token,
|
|
method: 'GET',
|
|
});
|
|
|
|
if (result.success) {
|
|
const workers = result.data?.data || result.data || [];
|
|
console.log(`✅ 维修人员查询成功,获取到 ${workers.length} 个维修人员`.green);
|
|
|
|
if (Array.isArray(workers) && workers.length > 0) {
|
|
console.log('📋 维修人员列表:');
|
|
workers.forEach((worker, index) => {
|
|
console.log(
|
|
` ${index + 1}. ${worker.label} (ID: ${worker.value}, 部门: ${
|
|
worker.deptName || '未设置'
|
|
})`,
|
|
);
|
|
});
|
|
return {
|
|
success: true,
|
|
workers: workers,
|
|
selectedWorker: workers[0], // 选择第一个维修人员进行测试
|
|
};
|
|
} else {
|
|
console.log('⚠️ 维修人员列表为空,检查角色配置是否正确'.yellow);
|
|
return { success: false, reason: '维修人员列表为空' };
|
|
}
|
|
} else {
|
|
console.log('❌ 维修人员查询失败:', result.error);
|
|
return { success: false, reason: '接口调用失败', error: result.error };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 步骤2:获取待分派工单
|
|
*/
|
|
async function getPendingOrder(token) {
|
|
console.log('\n📋 步骤2:获取待分派工单'.blue);
|
|
|
|
const result = await callAPI('/pm/repairOrder/list', {
|
|
token: token,
|
|
method: 'GET',
|
|
params: {
|
|
orderStatus: '1', // 待分派状态
|
|
pageNum: 1,
|
|
pageSize: 5,
|
|
},
|
|
});
|
|
|
|
if (result.success) {
|
|
const orders = result.data?.rows || [];
|
|
console.log(`✅ 获取到 ${orders.length} 个待分派工单`.green);
|
|
|
|
if (orders.length > 0) {
|
|
const order = orders[0];
|
|
console.log(`📝 选择测试工单: ${order.orderNo} (ID: ${order.id})`);
|
|
console.log(` 标题: ${order.title}`);
|
|
console.log(` 当前状态: ${order.orderStatus} (${getStatusText(order.orderStatus)})`);
|
|
console.log(` 当前维修员: ${order.assigneeName || '未分派'}`);
|
|
return { success: true, order: order };
|
|
} else {
|
|
console.log('⚠️ 没有待分派的工单,需要创建测试工单'.yellow);
|
|
return { success: false, reason: '无待分派工单' };
|
|
}
|
|
} else {
|
|
console.log('❌ 获取工单列表失败:', result.error);
|
|
return { success: false, error: result.error };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 步骤3:执行工单分派(重点测试)
|
|
*/
|
|
async function testAssignOrder(token, orderId, worker) {
|
|
console.log('\n🎯 步骤3:执行工单分派(重点测试)'.blue);
|
|
|
|
const assignRemark = `测试分派备注 - 修复验证 - ${new Date().toLocaleString()}`;
|
|
|
|
console.log(`📋 分派参数:`);
|
|
console.log(` 工单ID: ${orderId}`);
|
|
console.log(` 维修员ID: ${worker.value}`);
|
|
console.log(` 维修员姓名: ${worker.label}`);
|
|
console.log(` 分派备注: ${assignRemark}`);
|
|
|
|
const result = await callAPI(`/pm/repairOrder/${orderId}/assign`, {
|
|
token: token,
|
|
method: 'PUT',
|
|
params: {
|
|
assigneeId: worker.value,
|
|
assignRemark: assignRemark,
|
|
},
|
|
});
|
|
|
|
if (result.success) {
|
|
console.log('✅ 工单分派请求成功'.green);
|
|
console.log(`📝 响应数据:`, JSON.stringify(result.data, null, 2));
|
|
return {
|
|
success: true,
|
|
assigneeId: worker.value,
|
|
assigneeName: worker.label,
|
|
assignRemark: assignRemark,
|
|
};
|
|
} else {
|
|
console.log('❌ 工单分派失败:', result.error);
|
|
return { success: false, error: result.error };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 步骤4:验证分派结果(核心验证)
|
|
*/
|
|
async function verifyAssignmentResult(token, orderId, expectedData) {
|
|
console.log('\n🔍 步骤4:验证分派结果(核心验证)'.blue);
|
|
|
|
// 等待1秒确保数据库更新完成
|
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
|
|
const result = await callAPI(`/pm/repairOrder/${orderId}`, {
|
|
token: token,
|
|
method: 'GET',
|
|
});
|
|
|
|
if (result.success) {
|
|
const order = result.data?.data || result.data;
|
|
console.log('📊 工单分派后状态:');
|
|
console.log(` 工单编号: ${order.orderNo}`);
|
|
console.log(` 工单状态: ${order.orderStatus} (${getStatusText(order.orderStatus)})`);
|
|
console.log(` 维修员ID: ${order.assigneeId || '未设置'}`);
|
|
console.log(` 维修员姓名: ${order.assigneeName || '未设置'} 🎯`); // 重点检查
|
|
console.log(` 分派时间: ${order.assignTime || '未设置'}`);
|
|
console.log(` 分派备注: ${order.assignRemark || '未设置'} 🎯`); // 重点检查
|
|
|
|
// 核心验证项目
|
|
const validations = [
|
|
{
|
|
name: '工单状态',
|
|
expected: '2',
|
|
actual: order.orderStatus,
|
|
valid: order.orderStatus === '2',
|
|
critical: true,
|
|
},
|
|
{
|
|
name: '维修员ID',
|
|
expected: expectedData.assigneeId,
|
|
actual: order.assigneeId,
|
|
valid: order.assigneeId == expectedData.assigneeId,
|
|
critical: true,
|
|
},
|
|
{
|
|
name: '维修员姓名',
|
|
expected: expectedData.assigneeName,
|
|
actual: order.assigneeName,
|
|
valid: order.assigneeName === expectedData.assigneeName,
|
|
critical: true, // 🚨 这是本次修复的重点
|
|
},
|
|
{
|
|
name: '分派时间',
|
|
expected: '已设置',
|
|
actual: order.assignTime,
|
|
valid: !!order.assignTime,
|
|
critical: true,
|
|
},
|
|
{
|
|
name: '分派备注',
|
|
expected: expectedData.assignRemark,
|
|
actual: order.assignRemark,
|
|
valid: order.assignRemark === expectedData.assignRemark,
|
|
critical: true, // 🚨 这是本次修复的重点
|
|
},
|
|
];
|
|
|
|
console.log('\n📋 核心验证结果:');
|
|
let allValid = true;
|
|
let criticalIssues = [];
|
|
|
|
validations.forEach((v) => {
|
|
const status = v.valid ? '✅ 正确'.green : '❌ 错误'.red;
|
|
const critical = v.critical ? ' 🚨' : '';
|
|
console.log(
|
|
` ${v.name}: ${status}${critical} (期望: ${v.expected}, 实际: ${v.actual})`,
|
|
);
|
|
|
|
if (!v.valid) {
|
|
allValid = false;
|
|
if (v.critical) {
|
|
criticalIssues.push(v.name);
|
|
}
|
|
}
|
|
});
|
|
|
|
return {
|
|
success: allValid,
|
|
order: order,
|
|
validations: validations,
|
|
criticalIssues: criticalIssues,
|
|
};
|
|
} else {
|
|
console.log('❌ 获取工单详情失败:', result.error);
|
|
return { success: false, error: result.error };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取状态文本
|
|
*/
|
|
function getStatusText(status) {
|
|
const statusMap = {
|
|
1: '待分派',
|
|
2: '已分派',
|
|
3: '进行中',
|
|
4: '已完成',
|
|
5: '已评价',
|
|
6: '已归档',
|
|
9: '已取消',
|
|
};
|
|
return statusMap[status] || '未知状态';
|
|
}
|
|
|
|
/**
|
|
* 主测试流程
|
|
*/
|
|
async function main() {
|
|
console.log('🚀 指派人员数据更新修复验证开始'.cyan);
|
|
console.log('=' * 60);
|
|
|
|
try {
|
|
// 获取Token
|
|
console.log('🔐 获取登录Token...'.blue);
|
|
const token = await getToken();
|
|
console.log(`✅ Token获取成功`.green);
|
|
|
|
// 步骤1:测试维修人员查询
|
|
const workersResult = await testMaintenanceWorkersAPI(token);
|
|
if (!workersResult.success) {
|
|
console.log('❌ 维修人员查询失败,测试终止'.red);
|
|
console.log(
|
|
`💡 解决方案: 检查维修人员角色配置,运行SQL脚本: test/指派人员问题诊断SQL.sql`,
|
|
);
|
|
return;
|
|
}
|
|
|
|
// 步骤2:获取待分派工单
|
|
const orderResult = await getPendingOrder(token);
|
|
if (!orderResult.success) {
|
|
console.log('❌ 无法获取待分派工单,测试终止'.red);
|
|
return;
|
|
}
|
|
|
|
// 步骤3:执行分派
|
|
const assignResult = await testAssignOrder(
|
|
token,
|
|
orderResult.order.id,
|
|
workersResult.selectedWorker,
|
|
);
|
|
if (!assignResult.success) {
|
|
console.log('❌ 工单分派失败,测试终止'.red);
|
|
return;
|
|
}
|
|
|
|
// 步骤4:验证结果
|
|
const verification = await verifyAssignmentResult(
|
|
token,
|
|
orderResult.order.id,
|
|
assignResult,
|
|
);
|
|
|
|
// 测试结论
|
|
console.log('\n' + '=' * 60);
|
|
if (verification.success) {
|
|
console.log('🎉 修复验证成功:指派功能正常工作!'.green);
|
|
console.log('✅ 维修员姓名和分派备注已正确保存到数据库'.green);
|
|
console.log('✅ 所有关键字段都已正确更新'.green);
|
|
} else {
|
|
console.log('❌ 修复验证失败:指派功能仍存在问题'.red);
|
|
console.log('🔧 发现问题:');
|
|
verification.criticalIssues?.forEach((issue) => {
|
|
console.log(` - ${issue}数据不正确`.red);
|
|
});
|
|
|
|
console.log('\n💡 可能的解决方案:');
|
|
console.log(' 1. 检查后端Service层代码是否正确编译');
|
|
console.log(' 2. 重启后端服务以确保代码生效');
|
|
console.log(' 3. 检查数据库字段是否存在');
|
|
console.log(' 4. 检查MyBatis映射是否正确');
|
|
}
|
|
} catch (error) {
|
|
console.log('💥 测试过程中发生错误:', error.message.red);
|
|
console.error(error);
|
|
}
|
|
}
|
|
|
|
// 导出模块
|
|
module.exports = {
|
|
main,
|
|
testMaintenanceWorkersAPI,
|
|
testAssignOrder,
|
|
verifyAssignmentResult,
|
|
};
|
|
|
|
// 如果直接运行此脚本
|
|
if (require.main === module) {
|
|
main();
|
|
}
|