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 CreateMultiRoleUserTest { @Autowired private JdbcTemplate jdbcTemplate; @Autowired private PasswordEncoder passwordEncoder; @Test public void createMultiRoleUser() { try { // 检查是否已存在多角色测试用户 Integer userCount = jdbcTemplate.queryForObject( "SELECT COUNT(*) FROM t_user WHERE mobile = '13800000003'", Integer.class ); if (userCount > 0) { System.out.println("多角色测试用户已存在,跳过创建"); return; } // 加密密码 (密码为: 123456) String encodedPassword = passwordEncoder.encode("123456"); // 插入多角色测试用户到 t_user 表 jdbcTemplate.update( "INSERT INTO t_user (name, wx_openid, wx_unionid, state, mobile, password, create_time, update_time, version) VALUES (?, ?, ?, ?, ?, ?, NOW(), NOW(), 0)", "多角色测试用户", "test_multi_role_openid", "test_multi_role_unionid", 1, "13800000003", encodedPassword ); // 获取用户ID Long userId = jdbcTemplate.queryForObject( "SELECT id FROM t_user WHERE mobile = ?", Long.class, "13800000003" ); // 插入为员工 jdbcTemplate.update( "INSERT INTO t_employee (name, phone, role_id, user_id, state, create_time, update_time, version, description) VALUES (?, ?, ?, ?, ?, NOW(), NOW(), 0, ?)", "多角色测试用户", "13800000003", "MANAGER", userId, 1, "既是员工又是评委的测试账号" ); // 插入为评委 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, ?, ?, ?)", "多角色测试用户", userId, "13800000003", 1, 1, "既是员工又是评委的测试账号", "技术总监", "测试公司", "拥有管理和技术评审双重职能" ); System.out.println("多角色测试用户创建成功!"); System.out.println("账号: 13800000003, 密码: 123456"); System.out.println("该用户同时具有员工和评委角色,用于测试角色优先级"); } catch (Exception e) { System.err.println("创建多角色测试用户失败: " + e.getMessage()); e.printStackTrace(); } } }