This commit is contained in:
zlwu committed 2026-01-04 10:00:08 +08:00
1 parent 48f15745be
commit e421030ada
72 files changed
+17064 -35

No files matched your search

@@ -5,7 +5,7 @@
# 处理类型: CLASS_CHANGE(换班级)
# ===========================================
set -euo pipefail
# set -euo pipefail # Temporarily disable to prevent script exit on errors
# Force C locale to ensure consistent behavior across systems
export LANG=C
@@ -261,54 +261,86 @@ main() {
continue
fi
# 检查是否有记录需要处理(过滤掉空行、MySQL警告信息和表头)
# 检查是否有记录需要处理(过滤掉空行和MySQL警告信息)
local clean_records
# 过滤掉MySQL警告、空行,然后排除表头行(第一行通常是列名)
local filtered_records
filtered_records=$(echo "$records" | grep -v "^mysql:" | grep -v "^$" | sed '1d')
clean_records=$(echo "$filtered_records" | wc -l)
clean_records=$(echo "$records" | grep -v "^mysql:" | grep -v "^$" | wc -l)
# 去除可能的空白字符
clean_records=$(echo "$clean_records" | tr -d ' ')
if [[ $clean_records -eq 0 ]]; then
if [[ $clean_records -le 1 ]]; then
log "INFO" "未找到未处理的记录,正在等待..."
else
log "INFO" "找到 $clean_records 条待处理记录"
# 排除标题行后的实际记录数
local actual_records=$((clean_records - 1))
log "INFO" "找到 $actual_records 条待处理记录"
# 调试:显示前几条记录的信息
if [[ $clean_records -gt 0 ]]; then
log "DEBUG" "待处理记录预览:"
echo "$filtered_records" | head -3 | while read -r line; do
log "DEBUG" " $line"
done
if [[ $actual_records -gt 0 ]]; then
log "DEBUG" "将处理 $actual_records 条记录"
fi
local processed_count=0
local failed_count=0
# Create a temporary file to store records for processing
local temp_records_file="${SCRIPT_DIR}/temp_class_change_records_fixed_$$.txt"
echo "$filtered_records" > "$temp_records_file"
# Process records line by line using a simple loop
local filtered_data
filtered_data=$(echo "$records" | grep -v "^mysql:" | grep -v "^$")
# Process each record from the temporary file
while IFS=$'\t' read -r record_id student_id student_no new_class_id operator_id; do
# Skip empty lines or invalid records
[[ -z "$record_id" || -z "$student_id" || -z "$student_no" || -z "$new_class_id" ]] && continue
log "DEBUG" "过滤后数据行数: $(echo "$filtered_data" | wc -l)"
log "DEBUG" "过滤后数据内容预览:"
echo "$filtered_data" | head -3 | while read -r preview_line; do
log "DEBUG" " DATA: $preview_line"
done
# Create temporary file for processing to avoid subshell variable scope issues
local temp_file="/tmp/student_class_change_process_$$.txt"
echo "$filtered_data" > "$temp_file"
# Log the record being processed for debugging
log "INFO" "读取到记录 - ID: $record_id, 学生ID: $student_id, 学号: $student_no, 新班级ID: $new_class_id, 操作员: ${operator_id:-1}"
# Process records from temporary file (variables will be in same shell)
local is_first_line=true
while IFS= read -r line; do
# Parse the tab-separated values for processing
IFS=$'\t' read -r record_id student_id student_no new_class_id operator_id <<< "$line"
# Skip completely empty lines
if [[ -z "$record_id$student_id$student_no$new_class_id$operator_id" ]]; then
continue
fi
# Skip header line (first non-empty line that looks like column names)
if [[ $is_first_line == true ]]; then
is_first_line=false
# Check if this looks like a header line
if [[ "$record_id" == "id" || "$record_id" =~ ^[[:space:]]*id[[:space:]]*$ ]]; then
log "INFO" "跳过标题行"
continue
fi
fi
# Validate required fields
if [[ -z "$record_id" || -z "$student_id" || -z "$student_no" || -z "$new_class_id" ]]; then
log "WARN" "跳过无效记录 - ID: '$record_id'"
continue
fi
# Log the record being processed
log "INFO" "处理记录 - ID: $record_id, 学号: $student_no"
local retry_count=0
local success=false
while [[ $retry_count -lt $MAX_RETRY && $success == false ]]; do
log "DEBUG" "开始调用process_student_class_change函数,参数: $record_id, $student_id, $student_no, $new_class_id, $operator_id"
if process_student_class_change "$record_id" "$student_id" "$student_no" "$new_class_id" "$operator_id"; then
success=true
((processed_count++))
log "INFO" "成功处理记录 $record_id (总计: $processed_count)"
log "DEBUG" "函数调用成功,success=$success, processed_count=$processed_count"
else
((retry_count++))
log "DEBUG" "函数调用失败,retry_count=$retry_count, MAX_RETRY=$MAX_RETRY"
if [[ $retry_count -lt $MAX_RETRY ]]; then
log "WARN" "处理失败,$RETRY_DELAY 秒后重试... (尝试 $retry_count/$MAX_RETRY)"
log "WARN" "处理失败,重试中..."
sleep $RETRY_DELAY
fi
fi
@@ -316,13 +348,70 @@ main() {
if [[ $success == false ]]; then
((failed_count++))
log "ERROR" "$MAX_RETRY 次尝试后仍处理失败 - ID: $record_id"
log "ERROR" "记录 $record_id 处理失败"
fi
((line_count++))
echo "DEBUG: 处理第 $line_count 行: $line" >&2
log "DEBUG" "处理第 $line_count 行: $line"
# Parse the tab-separated values for processing
IFS=$'\t' read -r record_id student_id student_no new_class_id operator_id <<< "$line"
# Skip completely empty lines
if [[ -z "$record_id$student_id$student_no$new_class_id$operator_id" ]]; then
continue
fi
done < "$temp_records_file"
# Skip header line (first non-empty line that looks like column names)
if [[ $is_first_line == true ]]; then
is_first_line=false
if [[ "$record_id" == "id" || "$record_id" =~ ^[[:space:]]*id[[:space:]]*$ ]]; then
continue
fi
fi
# Validate required fields
if [[ -z "$record_id" || -z "$student_id" || -z "$student_no" || -z "$new_class_id" ]]; then
continue
fi
# Log the record being processed
log "INFO" "处理记录 - ID: $record_id, 学号: $student_no"
local retry_count=0
local success=false
while [[ $retry_count -lt $MAX_RETRY && $success == false ]]; do
log "DEBUG" "开始调用process_student_class_change函数,参数: $record_id, $student_id, $student_no, $new_class_id, $operator_id"
if process_student_class_change "$record_id" "$student_id" "$student_no" "$new_class_id" "$operator_id"; then
success=true
((processed_count++))
log "INFO" "成功处理记录 $record_id (总计: $processed_count)"
log "DEBUG" "函数调用成功,success=$success, processed_count=$processed_count"
else
((retry_count++))
log "DEBUG" "函数调用失败,retry_count=$retry_count, MAX_RETRY=$MAX_RETRY"
if [[ $retry_count -lt $MAX_RETRY ]]; then
log "WARN" "处理失败,重试中..."
sleep $RETRY_DELAY
fi
fi
done
if [[ $success == false ]]; then
((failed_count++))
log "ERROR" "记录 $record_id 处理失败"
fi
log "DEBUG" "完成处理记录 $record_id,继续下一条"
done < "$temp_file"
log "DEBUG" "所有记录处理完成,processed_count=$processed_count, failed_count=$failed_count"
# Clean up temporary file
rm -f "$temp_records_file"
rm -f "$temp_file"
log "INFO" "处理周期完成 - 已处理: $processed_count, 失败: $failed_count"
log "INFO" "处理周期完成 - 已处理: $processed_count, 失败: $failed_count"
fi
+20
View File
@@ -0,0 +1,20 @@
#!/bin/bash
echo "测试here string..."
data="line1
line2
line3"
echo "数据内容:"
echo "$data"
echo "---"
echo "使用here string:"
count=0
while IFS= read -r line; do
((count++))
echo "处理第 $count 行: $line"
done <<< "$data"
echo "测试完成"
+24
View File
@@ -0,0 +1,24 @@
#!/bin/bash
set -euo pipefail
echo "开始测试循环..."
data="line1
line2
line3"
echo "数据行数: $(echo "$data" | wc -l)"
echo "进入循环..."
count=0
while IFS= read -r line; do
((count++))
echo "处理第 $count 行: $line"
if [[ $count -eq 1 ]]; then
echo "只处理第一行,退出测试"
break
fi
done <<< "$data"
echo "循环结束"
+20
View File
@@ -0,0 +1,20 @@
#!/bin/bash
echo "测试管道方式..."
data="line1
line2
line3"
echo "数据内容:"
echo "$data"
echo "---"
echo "使用管道:"
count=0
echo "$data" | while IFS= read -r line; do
((count++))
echo "处理第 $count 行: $line"
done
echo "管道测试完成, count=$count"