process_student_resume_fixed.sh 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #!/bin/bash
  2. # ===========================================
  3. # 学生状态变更处理脚本 - 复学 (修复版本)
  4. # 处理类型: FU_XUE(复学)
  5. # ===========================================
  6. # set -euo pipefail # Temporarily disable to prevent script exit on errors
  7. # Force C locale to ensure consistent behavior across systems
  8. export LANG=C
  9. export LC_ALL=C
  10. # Load configuration
  11. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  12. CONFIG_FILE="${SCRIPT_DIR}/data.conf"
  13. if [[ ! -f "$CONFIG_FILE" ]]; then
  14. echo "错误:配置文件 $CONFIG_FILE 未找到!"
  15. exit 1
  16. fi
  17. source "$CONFIG_FILE"
  18. # Create log directory
  19. mkdir -p "$LOG_DIR"
  20. # Log file
  21. LOG_FILE="${LOG_DIR}/process_student_resume_fixed_$(date +%Y%m%d_%H%M%S).log"
  22. # Lock file to prevent multiple instances
  23. LOCK_FILE="${SCRIPT_DIR}/process_student_resume_fixed.lock"
  24. # Functions
  25. log() {
  26. local level="$1"
  27. local message="$2"
  28. local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
  29. echo "[$timestamp] [$level] $message" | tee -a "$LOG_FILE"
  30. }
  31. error_exit() {
  32. local message="$1"
  33. log "ERROR" "$message"
  34. # Remove lock file
  35. [[ -f "$LOCK_FILE" ]] && rm -f "$LOCK_FILE"
  36. exit 1
  37. }
  38. # Database functions - Fixed version
  39. execute_sql() {
  40. local sql="$1"
  41. local result
  42. result=$(mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASS" "$DB_NAME" -e "$sql" 2>&1)
  43. local exit_code=$?
  44. if [[ $exit_code -ne 0 ]]; then
  45. log "ERROR" "SQL执行失败: $sql"
  46. log "ERROR" "MySQL错误: $result"
  47. return 1
  48. fi
  49. echo "$result"
  50. }
  51. # Database query function - Fixed version for cross-platform compatibility
  52. execute_query() {
  53. local sql="$1"
  54. local result
  55. # Use -ss for ultra-silent mode to ensure clean tab-separated output
  56. # Redirect stderr to /dev/null to discard warnings, only capture actual data
  57. result=$(mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASS" -ss "$DB_NAME" -e "$sql" 2>/dev/null)
  58. local exit_code=$?
  59. if [[ $exit_code -ne 0 ]]; then
  60. log "ERROR" "查询执行失败: $sql"
  61. log "ERROR" "MySQL错误码: $exit_code"
  62. return 1
  63. fi
  64. echo "$result"
  65. }
  66. # Process student resume
  67. process_student_resume() {
  68. local record_id="$1"
  69. local student_id="$2"
  70. local student_no="$3"
  71. local change_type="$4"
  72. local operator_id="${5:-1}"
  73. # Validate input parameters
  74. if [[ -z "$record_id" || -z "$student_id" || -z "$student_no" || -z "$change_type" ]]; then
  75. log "ERROR" "参数不完整 - record_id: '$record_id', student_id: '$student_id', student_no: '$student_no', change_type: '$change_type'"
  76. return 1
  77. fi
  78. log "INFO" "正在处理学生复学 - ID: $record_id, 学生: $student_no ($student_id), 类型: $change_type"
  79. # Start transaction
  80. local transaction_sql="
  81. START TRANSACTION;
  82. -- 1. Backup original data to his tables (ignore if already exists)
  83. INSERT IGNORE INTO xg_student_msg_his SELECT * FROM xg_student_msg WHERE id = $student_id;
  84. INSERT IGNORE INTO sys_user_his SELECT * FROM sys_user WHERE user_id = $student_id;
  85. -- 2. Update student status to resume (复学)
  86. UPDATE xg_student_msg SET
  87. student_status = '1',
  88. update_by = $operator_id,
  89. update_time = NOW()
  90. WHERE id = $student_id;
  91. -- 3. Enable user account
  92. UPDATE sys_user SET
  93. status = '0',
  94. update_by = $operator_id,
  95. update_time = NOW()
  96. WHERE user_id = $student_id AND account_type = '2';
  97. -- 4. Update change record status
  98. UPDATE xg_student_status_change SET
  99. process_status = 1,
  100. update_by = $operator_id,
  101. update_time = NOW()
  102. WHERE id = $record_id;
  103. COMMIT;
  104. "
  105. # Execute transaction (using execute_sql to allow error output for debugging)
  106. if execute_sql "$transaction_sql"; then
  107. log "INFO" "成功处理学生复学 - ID: $record_id, 学生: $student_no"
  108. return 0
  109. else
  110. log "ERROR" "处理学生复学失败 - ID: $record_id, 学生: $student_no"
  111. return 1
  112. fi
  113. }
  114. # Main function
  115. main() {
  116. log "INFO" "启动学生复学处理守护进程 (修复版本)"
  117. log "INFO" "处理类型: FU_XUE(复学)"
  118. log "INFO" "以连续模式运行,间隔10分钟"
  119. # 测试数据库连接
  120. log "INFO" "正在测试数据库连接..."
  121. if ! execute_sql "SELECT 1;" >/dev/null 2>&1; then
  122. error_exit "数据库连接失败"
  123. fi
  124. log "INFO" "数据库连接成功"
  125. # Main processing loop
  126. while true; do
  127. log "INFO" "开始新的复学处理周期"
  128. # 获取未处理的记录
  129. local query_sql="
  130. SELECT id, student_id, student_no, change_type, operator_id
  131. FROM xg_student_status_change
  132. WHERE process_status = 0
  133. AND change_type = 'FU_XUE'
  134. AND deleted = 0
  135. AND effective_date <= NOW()
  136. ORDER BY create_time ASC
  137. LIMIT $BATCH_SIZE;
  138. "
  139. local records
  140. records=$(execute_query "$query_sql")
  141. if [[ $? -ne 0 ]]; then
  142. log "ERROR" "获取待处理记录失败"
  143. sleep $CYCLE_INTERVAL
  144. continue
  145. fi
  146. # 检查是否有记录需要处理(过滤掉空行和MySQL警告信息)
  147. local clean_records
  148. clean_records=$(echo "$records" | grep -v "^mysql:" | grep -v "^$" | wc -l)
  149. if [[ $clean_records -eq 0 ]]; then
  150. log "INFO" "未找到未处理的复学记录,正在等待..."
  151. else
  152. log "INFO" "找到 $clean_records 条待处理复学记录"
  153. local processed_count=0
  154. local failed_count=0
  155. # Process records directly from the query result, skip header and empty lines
  156. local is_first_line=true
  157. while IFS=$'\t' read -r record_id student_id student_no change_type operator_id; do
  158. # Skip completely empty lines
  159. [[ -z "$record_id$student_id$student_no$change_type$operator_id" ]] && continue
  160. # Skip header line (first non-empty line that looks like column names)
  161. if [[ $is_first_line == true ]]; then
  162. is_first_line=false
  163. # Check if this looks like a header line
  164. if [[ "$record_id" == "id" || "$record_id" =~ ^[[:space:]]*id[[:space:]]*$ ]]; then
  165. log "INFO" "跳过标题行"
  166. continue
  167. fi
  168. fi
  169. # Validate required fields
  170. if [[ -z "$record_id" || -z "$student_id" || -z "$student_no" || -z "$change_type" ]]; then
  171. log "WARN" "跳过无效记录 - ID: '$record_id', 学生ID: '$student_id', 学号: '$student_no', 类型: '$change_type'"
  172. continue
  173. fi
  174. # Log the record being processed for debugging
  175. log "INFO" "读取到复学记录 - ID: $record_id, 学生ID: $student_id, 学号: $student_no, 类型: $change_type, 操作员: ${operator_id:-1}"
  176. local retry_count=0
  177. local success=false
  178. while [[ $retry_count -lt $MAX_RETRY && $success == false ]]; do
  179. if process_student_resume "$record_id" "$student_id" "$student_no" "$change_type" "$operator_id"; then
  180. success=true
  181. ((processed_count++))
  182. else
  183. ((retry_count++))
  184. if [[ $retry_count -lt $MAX_RETRY ]]; then
  185. log "WARN" "处理失败,$RETRY_DELAY 秒后重试... (尝试 $retry_count/$MAX_RETRY)"
  186. sleep $RETRY_DELAY
  187. fi
  188. fi
  189. done
  190. if [[ $success == false ]]; then
  191. ((failed_count++))
  192. log "ERROR" "$MAX_RETRY 次尝试后仍处理失败 - ID: $record_id"
  193. fi
  194. done <<< "$(echo "$records" | grep -v "^mysql:" | grep -v "^$")"
  195. log "INFO" "复学处理周期完成 - 已处理: $processed_count, 失败: $failed_count"
  196. fi
  197. # 等待配置的间隔时间后开始下一个周期
  198. log "INFO" "等待 $CYCLE_INTERVAL 秒后开始下一个复学处理周期..."
  199. sleep $CYCLE_INTERVAL
  200. done
  201. }
  202. # Run main function
  203. main "$@"