暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

user_count.go 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package tools
  2. import (
  3. "encoding/json"
  4. "git.x2erp.com/qdy/go-svc-mcp/internal/mcp"
  5. )
  6. func init() {
  7. mcp.Register("get_user_count", "获取用户数量,演示数据库工厂和上下文注入",
  8. map[string]interface{}{
  9. "type": "object",
  10. "properties": map[string]interface{}{
  11. "active_only": map[string]interface{}{
  12. "type": "boolean",
  13. "description": "是否只统计活跃用户",
  14. "default": false,
  15. },
  16. },
  17. "required": []string{},
  18. },
  19. func(input json.RawMessage, deps *mcp.ToolDependencies) (interface{}, error) {
  20. var params struct {
  21. ActiveOnly bool `json:"active_only"`
  22. }
  23. if len(input) > 0 {
  24. if err := json.Unmarshal(input, &params); err != nil {
  25. return nil, err
  26. }
  27. }
  28. // 使用数据库工厂执行查询(示例)
  29. var count int64
  30. if deps.DBFactory != nil {
  31. // 实际项目中,这里会执行数据库查询
  32. // 示例:假设我们有一个用户表
  33. // db := deps.DBFactory.GetDB()
  34. // query := "SELECT COUNT(*) FROM users WHERE tenant_id = ?"
  35. // if params.ActiveOnly {
  36. // query += " AND status = 'active'"
  37. // }
  38. // err := db.QueryRow(query, deps.ReqCtx.TenantID).Scan(&count)
  39. // 模拟查询结果
  40. count = 42
  41. if params.ActiveOnly {
  42. count = 25
  43. }
  44. } else {
  45. // 无数据库工厂,返回模拟数据
  46. count = 100
  47. if params.ActiveOnly {
  48. count = 60
  49. }
  50. }
  51. return map[string]interface{}{
  52. "tenant_id": deps.ReqCtx.TenantID,
  53. "user_count": count,
  54. "active_only": params.ActiveOnly,
  55. "has_db": deps.DBFactory != nil,
  56. "timestamp": "2024-01-01T00:00:00Z", // 实际应使用 time.Now()
  57. }, nil
  58. },
  59. )
  60. }