package com.rongyichuang; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.test.context.ActiveProfiles; @SpringBootTest @ActiveProfiles("dev") public class CreateTestUsersTest { @Autowired private JdbcTemplate jdbcTemplate; @Autowired private PasswordEncoder passwordEncoder; @Test public void createTestUsers() { try { // 先删除现有的测试用户(如果存在) System.out.println("删除现有测试用户..."); jdbcTemplate.update("DELETE FROM t_employee WHERE phone IN ('13800000001', '13800000002')"); jdbcTemplate.update("DELETE FROM t_judge WHERE phone IN ('13800000001', '13800000002')"); jdbcTemplate.update("DELETE FROM t_user WHERE mobile IN ('13800000001', '13800000002') OR phone IN ('13800000001', '13800000002')"); System.out.println("现有测试用户已删除"); // 加密密码 (密码为: 123456) String encodedPassword = passwordEncoder.encode("123456"); // 插入测试用户到 t_user 表 (包含密码,同时设置phone和mobile字段) jdbcTemplate.update( "INSERT INTO t_user (name, wx_openid, wx_unionid, state, mobile, phone, password, create_time, update_time, version) VALUES (?, ?, ?, ?, ?, ?, ?, NOW(), NOW(), 0)", "测试员工", "test_employee_openid", "test_employee_unionid", 1, "13800000001", "13800000001", encodedPassword ); jdbcTemplate.update( "INSERT INTO t_user (name, wx_openid, wx_unionid, state, mobile, phone, password, create_time, update_time, version) VALUES (?, ?, ?, ?, ?, ?, ?, NOW(), NOW(), 0)", "测试评委", "test_judge_openid", "test_judge_unionid", 1, "13800000002", "13800000002", encodedPassword ); // 获取用户ID Long employeeUserId = jdbcTemplate.queryForObject( "SELECT id FROM t_user WHERE mobile = ?", Long.class, "13800000001" ); Long judgeUserId = jdbcTemplate.queryForObject( "SELECT id FROM t_user WHERE mobile = ?", Long.class, "13800000002" ); // 插入测试员工到 t_employee 表 (不包含密码) jdbcTemplate.update( "INSERT INTO t_employee (name, phone, role_id, user_id, state, create_time, update_time, version, description) VALUES (?, ?, ?, ?, ?, NOW(), NOW(), 0, ?)", "测试员工", "13800000001", "ADMIN", employeeUserId, 1, "系统测试员工账号" ); // 插入测试评委到 t_judge 表 jdbcTemplate.update( "INSERT INTO t_judge (name, user_id, phone, gender, state, description, create_time, update_time, version, title, company, introduction) VALUES (?, ?, ?, ?, ?, ?, NOW(), NOW(), 0, ?, ?, ?)", "测试评委", judgeUserId, "13800000002", 1, 1, "系统测试评委账号", "高级技术专家", "测试公司", "拥有丰富的技术评审经验" ); System.out.println("测试用户创建成功!"); System.out.println("员工账号: 13800000001, 密码: 123456"); System.out.println("评委账号: 13800000002, 密码: 123456"); } catch (Exception e) { System.err.println("创建测试用户失败: " + e.getMessage()); e.printStackTrace(); } } }