365 lines
11 KiB
JavaScript
365 lines
11 KiB
JavaScript
const axios = require('axios');
|
|
const colors = require('colors');
|
|
|
|
/**
|
|
* 指派人员数据更新验证脚本
|
|
* 功能:验证指派工单时,维修人员等数据是否正确更新到数据库
|
|
* 时间:2025-06-30 18:45: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 getPendingOrders(token) {
|
|
console.log('📋 步骤1:获取待分派的工单列表'.blue);
|
|
|
|
const result = await callAPI('/pm/repairOrder/list', {
|
|
token: token,
|
|
method: 'GET',
|
|
params: {
|
|
orderStatus: '1', // 待分派状态
|
|
pageNum: 1,
|
|
pageSize: 10,
|
|
},
|
|
});
|
|
|
|
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})`.yellow);
|
|
console.log(` 标题: ${order.title}`);
|
|
console.log(` 状态: ${order.orderStatus} (${getStatusText(order.orderStatus)})`);
|
|
console.log(` 当前维修员: ${order.assigneeId || '未分派'}`);
|
|
return order;
|
|
} else {
|
|
console.log('⚠️ 没有找到待分派的工单,创建一个测试工单'.yellow);
|
|
return await createTestOrder(token);
|
|
}
|
|
} else {
|
|
console.log('❌ 获取工单列表失败:', result.error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 步骤2:获取维修人员列表
|
|
*/
|
|
async function getMaintenanceWorkers(token) {
|
|
console.log('\n🧑🔧 步骤2:获取维修人员列表'.blue);
|
|
|
|
const result = await callAPI('/pm/repairOrder/maintenance-workers', {
|
|
token: token,
|
|
method: 'GET',
|
|
});
|
|
|
|
if (result.success) {
|
|
const workers = result.data || [];
|
|
console.log(`✅ 获取到 ${workers.length} 个维修人员`.green);
|
|
|
|
workers.forEach((worker, index) => {
|
|
console.log(` ${index + 1}. ${worker.label} (ID: ${worker.value})`);
|
|
});
|
|
|
|
if (workers.length > 0) {
|
|
const selectedWorker = workers[0]; // 选择第一个维修人员
|
|
console.log(
|
|
`🎯 选择维修人员: ${selectedWorker.label} (ID: ${selectedWorker.value})`.yellow,
|
|
);
|
|
return selectedWorker;
|
|
} else {
|
|
console.log('❌ 没有找到可用的维修人员');
|
|
return null;
|
|
}
|
|
} else {
|
|
console.log('❌ 获取维修人员列表失败:', result.error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 步骤3:执行工单分派
|
|
*/
|
|
async function assignOrder(token, orderId, assigneeId, assigneeName) {
|
|
console.log('\n🎯 步骤3:执行工单分派'.blue);
|
|
|
|
console.log(`📋 分派参数:`);
|
|
console.log(` 工单ID: ${orderId}`);
|
|
console.log(` 维修员ID: ${assigneeId}`);
|
|
console.log(` 维修员姓名: ${assigneeName}`);
|
|
console.log(` 分派备注: 自动测试分派`);
|
|
|
|
const result = await callAPI(`/pm/repairOrder/${orderId}/assign`, {
|
|
token: token,
|
|
method: 'PUT',
|
|
params: {
|
|
assigneeId: assigneeId,
|
|
assignRemark: '自动测试分派 - ' + new Date().toLocaleString(),
|
|
},
|
|
});
|
|
|
|
if (result.success) {
|
|
console.log('✅ 工单分派成功'.green);
|
|
console.log(`📝 响应数据:`, JSON.stringify(result.data, null, 2));
|
|
return true;
|
|
} else {
|
|
console.log('❌ 工单分派失败:', result.error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 步骤4:验证数据库更新结果
|
|
*/
|
|
async function verifyAssignmentResult(token, orderId, expectedAssigneeId) {
|
|
console.log('\n🔍 步骤4:验证数据库更新结果'.blue);
|
|
|
|
// 重新获取工单详情
|
|
const result = await callAPI(`/pm/repairOrder/${orderId}`, {
|
|
token: token,
|
|
method: 'GET',
|
|
});
|
|
|
|
if (result.success) {
|
|
const order = 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',
|
|
},
|
|
{
|
|
name: '维修员ID',
|
|
expected: expectedAssigneeId,
|
|
actual: order.assigneeId,
|
|
valid: order.assigneeId == expectedAssigneeId,
|
|
},
|
|
{
|
|
name: '分派时间',
|
|
expected: '已设置',
|
|
actual: order.assignTime,
|
|
valid: !!order.assignTime,
|
|
},
|
|
{
|
|
name: '分派备注',
|
|
expected: '已设置',
|
|
actual: order.assignRemark,
|
|
valid: !!order.assignRemark,
|
|
},
|
|
];
|
|
|
|
console.log('\n📋 数据验证结果:');
|
|
let allValid = true;
|
|
validations.forEach((v) => {
|
|
const status = v.valid ? '✅ 正确'.green : '❌ 错误'.red;
|
|
console.log(` ${v.name}: ${status} (期望: ${v.expected}, 实际: ${v.actual})`);
|
|
if (!v.valid) allValid = false;
|
|
});
|
|
|
|
return {
|
|
success: allValid,
|
|
order: order,
|
|
validations: validations,
|
|
};
|
|
} else {
|
|
console.log('❌ 获取工单详情失败:', result.error);
|
|
return { success: false, error: result.error };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 创建测试工单(如果没有待分派工单)
|
|
*/
|
|
async function createTestOrder(token) {
|
|
console.log('\n🆕 创建测试工单'.blue);
|
|
|
|
const testOrder = {
|
|
title: '测试工单 - 指派功能验证',
|
|
description: '自动生成的测试工单,用于验证指派功能是否正常工作',
|
|
repairLocation: '测试实验室A101',
|
|
categoryId: '1',
|
|
priorityLevel: 3,
|
|
reporterName: '测试用户',
|
|
reporterPhone: '13800000001',
|
|
reporterDept: '测试部门',
|
|
remark: '指派功能测试工单',
|
|
};
|
|
|
|
const result = await callAPI('/pm/repairOrder', {
|
|
token: token,
|
|
method: 'POST',
|
|
data: testOrder,
|
|
});
|
|
|
|
if (result.success) {
|
|
console.log('✅ 测试工单创建成功'.green);
|
|
// 重新获取工单详情
|
|
const listResult = await callAPI('/pm/repairOrder/list', {
|
|
token: token,
|
|
method: 'GET',
|
|
params: { pageNum: 1, pageSize: 1 },
|
|
});
|
|
|
|
if (listResult.success && listResult.data.rows.length > 0) {
|
|
return listResult.data.rows[0];
|
|
}
|
|
} else {
|
|
console.log('❌ 创建测试工单失败:', result.error);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* 获取状态文本
|
|
*/
|
|
function getStatusText(status) {
|
|
const statusMap = {
|
|
1: '待分派',
|
|
2: '已分派',
|
|
3: '进行中',
|
|
4: '已完成',
|
|
5: '已评价',
|
|
6: '已归档',
|
|
9: '已取消',
|
|
};
|
|
return statusMap[status] || '未知状态';
|
|
}
|
|
|
|
/**
|
|
* 主测试流程
|
|
*/
|
|
async function main() {
|
|
console.log('🚀 指派人员数据更新验证开始'.cyan);
|
|
console.log('=' * 50);
|
|
|
|
try {
|
|
// 获取Token
|
|
console.log('🔐 获取登录Token...'.blue);
|
|
const token = await getToken();
|
|
console.log(`✅ Token获取成功: ${token.substring(0, 50)}...`.green);
|
|
|
|
// 步骤1:获取待分派工单
|
|
const order = await getPendingOrders(token);
|
|
if (!order) {
|
|
console.log('❌ 无法获取测试工单,测试终止'.red);
|
|
return;
|
|
}
|
|
|
|
// 步骤2:获取维修人员
|
|
const worker = await getMaintenanceWorkers(token);
|
|
if (!worker) {
|
|
console.log('❌ 无法获取维修人员,测试终止'.red);
|
|
return;
|
|
}
|
|
|
|
// 步骤3:执行分派
|
|
const assignSuccess = await assignOrder(token, order.id, worker.value, worker.label);
|
|
if (!assignSuccess) {
|
|
console.log('❌ 工单分派失败,测试终止'.red);
|
|
return;
|
|
}
|
|
|
|
// 等待1秒,确保数据库更新完成
|
|
console.log('\n⏳ 等待数据库更新...'.yellow);
|
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
|
|
// 步骤4:验证结果
|
|
const verification = await verifyAssignmentResult(token, order.id, worker.value);
|
|
|
|
// 测试结论
|
|
console.log('\n' + '=' * 50);
|
|
if (verification.success) {
|
|
console.log('🎉 测试结果:指派功能正常工作,数据已成功更新到数据库'.green);
|
|
} else {
|
|
console.log('❌ 测试结果:指派功能存在问题,数据未正确更新到数据库'.red);
|
|
console.log('🔧 问题诊断:');
|
|
verification.validations?.forEach((v) => {
|
|
if (!v.valid) {
|
|
console.log(` - ${v.name}不正确: 期望 ${v.expected}, 实际 ${v.actual}`.red);
|
|
}
|
|
});
|
|
}
|
|
} catch (error) {
|
|
console.log('💥 测试过程中发生错误:', error.message.red);
|
|
console.error(error);
|
|
}
|
|
}
|
|
|
|
// 导出模块
|
|
module.exports = {
|
|
main,
|
|
getPendingOrders,
|
|
getMaintenanceWorkers,
|
|
assignOrder,
|
|
verifyAssignmentResult,
|
|
};
|
|
|
|
// 如果直接运行此脚本
|
|
if (require.main === module) {
|
|
main();
|
|
}
|