query_fuel_rate_list.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. class QueryFuelRateListTool:
  2. name = 'query_fuel_rate_list'
  3. route_path = '/mcp/tools/queryFuelRateList'
  4. backend = 'base'
  5. def __init__(self, api_client=None):
  6. self.api_client = api_client
  7. def metadata(self):
  8. return {
  9. 'name': self.name,
  10. 'description': (
  11. '使用场景:用户要查询燃油系数配置时调用。可按燃油代码、生效时间、'
  12. '燃油类型、状态、产品、渠道、产品分类、渠道分类筛选。'
  13. '产品、渠道、产品分类、渠道分类可以填代码或名称,必须完整匹配,不能按包含搜索。'
  14. '燃油代码格式为RY加数字,例如RY12。燃油类型为销售燃油或成本燃油。'
  15. '状态为草稿、失效或启用。生效时间筛选的是该时间点正在生效的配置。'
  16. '不传筛选条件时返回当前公司的燃油系数。'
  17. '编辑草稿请使用save_fuel_rate并传入这里返回的燃油代码。'
  18. '生效或启用请使用enable_fuel_rate。'
  19. '禁止使用:失效、删除燃油系数。'
  20. '参数只用于工具内部调用;最终回答只能展示中文业务名称和燃油代码。'
  21. ),
  22. 'input_schema': {
  23. 'type': 'object',
  24. 'properties': {
  25. 'fuel_no': {
  26. 'type': 'string',
  27. 'minLength': 1,
  28. 'maxLength': 20,
  29. 'description': '燃油代码,格式为RY加数字,例如RY12。',
  30. },
  31. 'start_date': {
  32. 'type': 'string',
  33. 'minLength': 8,
  34. 'maxLength': 30,
  35. 'description': '生效时间,筛选该时间点生效的配置,格式YYYY-MM-DD。',
  36. },
  37. 'product': {
  38. 'type': 'string',
  39. 'minLength': 1,
  40. 'maxLength': 100,
  41. 'description': '产品代码或产品名称,必须完整匹配。',
  42. },
  43. 'channel': {
  44. 'type': 'string',
  45. 'minLength': 1,
  46. 'maxLength': 100,
  47. 'description': '渠道代码或渠道名称,必须完整匹配。',
  48. },
  49. 'product_type': {
  50. 'type': 'string',
  51. 'minLength': 1,
  52. 'maxLength': 100,
  53. 'description': '产品分类代码或名称,必须完整匹配。只适用于销售燃油。',
  54. },
  55. 'channel_type': {
  56. 'type': 'string',
  57. 'minLength': 1,
  58. 'maxLength': 100,
  59. 'description': '渠道分类代码或名称,必须完整匹配。只适用于成本燃油。',
  60. },
  61. 'price_type': {
  62. 'type': 'string',
  63. 'minLength': 1,
  64. 'maxLength': 20,
  65. 'description': '燃油类型。1或销售燃油,2或成本燃油。',
  66. },
  67. 'status': {
  68. 'type': 'string',
  69. 'minLength': 1,
  70. 'maxLength': 10,
  71. 'description': '状态。1或草稿,2或失效,3或启用。',
  72. },
  73. 'page': {'type': 'integer', 'minimum': 1, 'maximum': 100, 'default': 1},
  74. 'limit': {'type': 'integer', 'minimum': 1, 'maximum': 100, 'default': 20},
  75. },
  76. 'additionalProperties': False,
  77. },
  78. }
  79. def call(
  80. self,
  81. fuel_no=None,
  82. start_date=None,
  83. product=None,
  84. channel=None,
  85. product_type=None,
  86. channel_type=None,
  87. price_type=None,
  88. status=None,
  89. page=1,
  90. limit=20,
  91. request_id='rq_query_fuel_rate',
  92. ):
  93. if self.api_client is None:
  94. raise RuntimeError('api client is required for query_fuel_rate_list')
  95. payload = {
  96. 'page': _page(page),
  97. 'limit': _limit(limit),
  98. }
  99. _put_text(payload, 'fuel_no', fuel_no, 20)
  100. _put_text(payload, 'start_date', start_date, 30)
  101. _put_text(payload, 'product', product, 100)
  102. _put_text(payload, 'channel', channel, 100)
  103. _put_text(payload, 'product_type', product_type, 100)
  104. _put_text(payload, 'channel_type', channel_type, 100)
  105. _put_choice(payload, 'price_type', price_type, ('1', '2', '销售燃油', '成本燃油'))
  106. _put_choice(payload, 'status', status, ('1', '2', '3', '草稿', '失效', '启用'))
  107. return self.api_client.call_tool(self.name, self.route_path, payload, request_id)
  108. def _page(value):
  109. if isinstance(value, bool) or not isinstance(value, int) or value < 1 or value > 100:
  110. raise ValueError('page is invalid')
  111. return value
  112. def _limit(value):
  113. if isinstance(value, bool) or not isinstance(value, int) or value < 1 or value > 100:
  114. raise ValueError('limit is invalid')
  115. return value
  116. def _put_text(payload, key, value, max_length):
  117. if value is None:
  118. return
  119. if not isinstance(value, str):
  120. raise ValueError(key + ' is invalid')
  121. value = value.strip()
  122. if not value:
  123. return
  124. if len(value) > max_length:
  125. raise ValueError(key + ' is invalid')
  126. payload[key] = value
  127. def _put_choice(payload, key, value, choices):
  128. if value is None:
  129. return
  130. if isinstance(value, bool):
  131. raise ValueError(key + ' is invalid')
  132. if isinstance(value, int):
  133. value = str(value)
  134. if not isinstance(value, str) or value.strip() not in choices:
  135. raise ValueError(key + ' is invalid')
  136. payload[key] = value.strip()