|
|
@ -5,13 +5,9 @@ import com.fasterxml.jackson.databind.ObjectMapper; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.jdbc.core.JdbcTemplate; |
|
|
import org.springframework.jdbc.core.JdbcTemplate; |
|
|
import org.springframework.jdbc.support.GeneratedKeyHolder; |
|
|
|
|
|
import org.springframework.jdbc.support.KeyHolder; |
|
|
|
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
|
import java.sql.PreparedStatement; |
|
|
|
|
|
import java.sql.Statement; |
|
|
|
|
|
import java.util.ArrayList; |
|
|
import java.util.ArrayList; |
|
|
import java.util.LinkedHashMap; |
|
|
import java.util.LinkedHashMap; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
@ -114,23 +110,16 @@ public class CustomerServiceRoleService { |
|
|
String key = (roleKey == null || roleKey.trim().isEmpty()) |
|
|
String key = (roleKey == null || roleKey.trim().isEmpty()) |
|
|
? "role_" + System.currentTimeMillis() |
|
|
? "role_" + System.currentTimeMillis() |
|
|
: roleKey.trim(); |
|
|
: roleKey.trim(); |
|
|
KeyHolder keyHolder = new GeneratedKeyHolder(); |
|
|
|
|
|
jdbcTemplate.update(con -> { |
|
|
|
|
|
PreparedStatement ps = con.prepareStatement( |
|
|
|
|
|
"INSERT INTO customer_service_role (role_key, name, description, prompt, enabled) VALUES (?, ?, ?, ?, ?)", |
|
|
|
|
|
Statement.RETURN_GENERATED_KEYS); |
|
|
|
|
|
ps.setString(1, key); |
|
|
|
|
|
ps.setString(2, name.trim()); |
|
|
|
|
|
ps.setString(3, description); |
|
|
|
|
|
ps.setString(4, prompt); |
|
|
|
|
|
ps.setBoolean(5, enabled != null ? enabled : true); |
|
|
|
|
|
return ps; |
|
|
|
|
|
}, keyHolder); |
|
|
|
|
|
Number generatedId = keyHolder.getKey(); |
|
|
|
|
|
|
|
|
// 使用 RETURNING id 显式取回主键,避免 PostgreSQL JDBC 对 RETURN_GENERATED_KEYS |
|
|
|
|
|
// 追加 RETURNING *(返回整行多列),导致 GeneratedKeyHolder.getKey() 抛"multiple keys"异常 |
|
|
|
|
|
Long generatedId = jdbcTemplate.queryForObject( |
|
|
|
|
|
"INSERT INTO customer_service_role (role_key, name, description, prompt, enabled) VALUES (?, ?, ?, ?, ?) RETURNING id", |
|
|
|
|
|
Long.class, |
|
|
|
|
|
key, name.trim(), description, prompt, enabled != null ? enabled : true); |
|
|
if (generatedId == null) { |
|
|
if (generatedId == null) { |
|
|
throw new RuntimeException("创建角色失败:无法获取自动生成的 ID"); |
|
|
throw new RuntimeException("创建角色失败:无法获取自动生成的 ID"); |
|
|
} |
|
|
} |
|
|
return generatedId.longValue(); |
|
|
|
|
|
|
|
|
return generatedId; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
|