Compare commits
55 Commits
feature/wc
...
master
| Author | SHA1 | Date |
|---|---|---|
|
|
ec5623bc07 | |
|
|
08932ad38f | |
|
|
bf0870865e | |
|
|
3510be1db7 | |
|
|
aba8a18900 | |
|
|
c8d3050c70 | |
|
|
a706948504 | |
|
|
8895f29963 | |
|
|
4656468e8a | |
|
|
a844d12979 | |
|
|
43522037db | |
|
|
2a312dc205 | |
|
|
5264f78fb0 | |
|
|
4626efe7b2 | |
|
|
4619389f4f | |
|
|
33965dae47 | |
|
|
c086e9ba15 | |
|
|
8cef32b7c8 | |
|
|
9a1ff9d8a2 | |
|
|
2c46dcacaa | |
|
|
65de970292 | |
|
|
6e5b4e1921 | |
|
|
2cc897507a | |
|
|
cae16fbae2 | |
|
|
48482d8b42 | |
|
|
17ff02d461 | |
|
|
e2b6454d29 | |
|
|
bf0fa441d9 | |
|
|
4497e30a50 | |
|
|
3eaba6004a | |
|
|
1b423fc509 | |
|
|
af54224504 | |
|
|
57788c9908 | |
|
|
9ec4146e17 | |
|
|
9f132dc99c | |
|
|
65aa6ca411 | |
|
|
baff6ef573 | |
|
|
e34d7376c1 | |
|
|
2cd19e5fdf | |
|
|
75756557b6 | |
|
|
c71c72038e | |
|
|
a8cfb118fe | |
|
|
ed6ea71bb0 | |
|
|
10db75a7aa | |
|
|
3de460658d | |
|
|
4a0cdacbe9 | |
|
|
5067932e92 | |
|
|
599a2aad44 | |
|
|
386c565f01 | |
|
|
fca01b6f94 | |
|
|
be1a268f14 | |
|
|
46f9285bea | |
|
|
305a842d29 | |
|
|
6c1cab37b3 | |
|
|
8b4e696c77 |
|
|
@ -3,4 +3,5 @@
|
||||||
.trae/
|
.trae/
|
||||||
docs
|
docs
|
||||||
cmd/server/wire_gen.go
|
cmd/server/wire_gen.go
|
||||||
__debug*
|
__debug*
|
||||||
|
.bin/
|
||||||
File diff suppressed because one or more lines are too long
|
|
@ -10,7 +10,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
configPath := flag.String("config", "./config/config.yaml", "Path to configuration file")
|
configPath := flag.String("config", "./config/config_test.yaml", "Path to configuration file")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
bc, err := config.LoadConfig(*configPath)
|
bc, err := config.LoadConfig(*configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -55,14 +55,15 @@ tools:
|
||||||
enabled: true
|
enabled: true
|
||||||
api_key: "dingsbbntrkeiyazcfdg"
|
api_key: "dingsbbntrkeiyazcfdg"
|
||||||
api_secret: "ObqxwyR20r9rVNhju0sCPQyQA98_FZSc32W4vgxnGFH_b02HZr1BPCJsOAF816nu"
|
api_secret: "ObqxwyR20r9rVNhju0sCPQyQA98_FZSc32W4vgxnGFH_b02HZr1BPCJsOAF816nu"
|
||||||
zltxOrderAfterSaleDetail:
|
zltxOrderAfterSaleSupplier:
|
||||||
enabled: true
|
enabled: true
|
||||||
base_url: "https://revcl.1688sup.com/api/admin/afterSales/direct/%s"
|
base_url: "https://revcl.1688sup.com/api/admin/afterSales/directs"
|
||||||
api_key : "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ1c2VyQ2VudGVyIiwiZXhwIjoxNzU2MTgyNTM1LCJuYmYiOjE3NTYxODA3MzUsImp0aSI6IjEiLCJQaG9uZSI6IjE4MDAwMDAwMDAwIiwiVXNlck5hbWUiOiJsc3hkIiwiUmVhbE5hbWUiOiLotoXnuqfnrqHnkIblkZgiLCJBY2NvdW50VHlwZSI6MSwiR3JvdXBDb2RlcyI6IlZDTF9DQVNISUVSLFZDTF9PUEVSQVRFLFZDTF9BRE1JTixWQ0xfQUFBLFZDTF9WQ0xfT1BFUkFULFZDTF9JTlZPSUNFLENSTV9BRE1JTixMSUFOTElBTl9BRE1JTixNQVJLRVRNQUcyX0FETUlOLFBIT05FQklMTF9BRE1JTixRSUFOWkhVX1NVUFBFUl9BRE0sTUFSS0VUSU5HU0FBU19TVVBFUkFETUlOLENBUkRfQ09ERSxDQVJEX1BST0NVUkVNRU5ULE1BUktFVElOR1NZU1RFTV9TVVBFUixTVEFUSVNUSUNBTFNZU1RFTV9BRE1JTixaTFRYX0FETUlOLFpMVFhfT1BFUkFURSIsIkRpbmdVc2VySWQiOiIxNjIwMjYxMjMwMjg5MzM4MzQifQ.N1xv1PYbcO8_jR5adaczc16YzGsr4z101gwEZdulkRaREBJNYTOnFrvRxTFx3RJTooXsqTqroE1MR84v_1WPX6BS6kKonA-kC1Jgot6yrt5rFWhGNGb2Cpr9rKIFCCQYmiGd3AUgDazEeaQ0_sodv3E-EXg9VfE1SX8nMcck9Yjnc8NCy7RTWaBIaSeOdZcEl-JfCD0S6GSx3oErp_hk-U9FKGwf60wAuDGTY1R0BP4BYpcEqS-C2LSnsSGyURi54Cuk5xH8r1WuF0Dm5bwAj5d7Hvs77-N_sUF-C5ONqyZJRAEhYLgcmN9RX_WQZfizdQJxizlTczdpzYfy-v-1eQ"
|
zltxOrderAfterSaleReseller:
|
||||||
zltxOrderAfterSalePreCheck:
|
|
||||||
enabled: true
|
enabled: true
|
||||||
base_url: "https://gateway.dev.cdlsxd.cn/zltx_api/admin/afterSales/reseller_pre"
|
base_url: "https://revcl.1688sup.com/api/admin/afterSales/reseller_pre_ai"
|
||||||
api_key : "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ1c2VyQ2VudGVyIiwiZXhwIjoxNzU2MTgyNTM1LCJuYmYiOjE3NTYxODA3MzUsImp0aSI6IjEiLCJQaG9uZSI6IjE4MDAwMDAwMDAwIiwiVXNlck5hbWUiOiJsc3hkIiwiUmVhbE5hbWUiOiLotoXnuqfnrqHnkIblkZgiLCJBY2NvdW50VHlwZSI6MSwiR3JvdXBDb2RlcyI6IlZDTF9DQVNISUVSLFZDTF9PUEVSQVRFLFZDTF9BRE1JTixWQ0xfQUFBLFZDTF9WQ0xfT1BFUkFULFZDTF9JTlZPSUNFLENSTV9BRE1JTixMSUFOTElBTl9BRE1JTixNQVJLRVRNQUcyX0FETUlOLFBIT05FQklMTF9BRE1JTixRSUFOWkhVX1NVUFBFUl9BRE0sTUFSS0VUSU5HU0FBU19TVVBFUkFETUlOLENBUkRfQ09ERSxDQVJEX1BST0NVUkVNRU5ULE1BUktFVElOR1NZU1RFTV9TVVBFUixTVEFUSVNUSUNBTFNZU1RFTV9BRE1JTixaTFRYX0FETUlOLFpMVFhfT1BFUkFURSIsIkRpbmdVc2VySWQiOiIxNjIwMjYxMjMwMjg5MzM4MzQifQ.N1xv1PYbcO8_jR5adaczc16YzGsr4z101gwEZdulkRaREBJNYTOnFrvRxTFx3RJTooXsqTqroE1MR84v_1WPX6BS6kKonA-kC1Jgot6yrt5rFWhGNGb2Cpr9rKIFCCQYmiGd3AUgDazEeaQ0_sodv3E-EXg9VfE1SX8nMcck9Yjnc8NCy7RTWaBIaSeOdZcEl-JfCD0S6GSx3oErp_hk-U9FKGwf60wAuDGTY1R0BP4BYpcEqS-C2LSnsSGyURi54Cuk5xH8r1WuF0Dm5bwAj5d7Hvs77-N_sUF-C5ONqyZJRAEhYLgcmN9RX_WQZfizdQJxizlTczdpzYfy-v-1eQ"
|
zltxOrderAfterSaleResellerBatch:
|
||||||
|
enabled: true
|
||||||
|
base_url: "https://revcl.1688sup.com/api/admin/afterSales/reseller_pre_ai"
|
||||||
|
|
||||||
|
|
||||||
default_prompt:
|
default_prompt:
|
||||||
|
|
@ -73,4 +74,8 @@ default_prompt:
|
||||||
提取出图片中对用户可能有用的关键信息(例如金额、日期、标题、编号、联系信息、商品名称等)。
|
提取出图片中对用户可能有用的关键信息(例如金额、日期、标题、编号、联系信息、商品名称等)。
|
||||||
若图片为文档类(如合同、发票、收据),请结构化输出关键字段(如客户名称、金额、开票日期等)。
|
若图片为文档类(如合同、发票、收据),请结构化输出关键字段(如客户名称、金额、开票日期等)。
|
||||||
'
|
'
|
||||||
user_prompt: '识别图片内容'
|
user_prompt: '识别图片内容'
|
||||||
|
|
||||||
|
# 权限配置
|
||||||
|
permissionConfig:
|
||||||
|
permission_url: "https://api.user.1688sup.com/v1/menu/myCodes?systemId="
|
||||||
|
|
@ -0,0 +1,81 @@
|
||||||
|
# 服务器配置
|
||||||
|
server:
|
||||||
|
port: 8090
|
||||||
|
host: "0.0.0.0"
|
||||||
|
|
||||||
|
ollama:
|
||||||
|
base_url: "http://127.0.0.1:11434"
|
||||||
|
model: "qwen3-coder:480b-cloud"
|
||||||
|
generate_model: "qwen3-coder:480b-cloud"
|
||||||
|
vl_model: "qwen2.5vl:7b"
|
||||||
|
timeout: "120s"
|
||||||
|
level: "info"
|
||||||
|
format: "json"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
sys:
|
||||||
|
session_len: 6
|
||||||
|
channel_pool_len: 100
|
||||||
|
channel_pool_size: 32
|
||||||
|
llm_pool_len: 5
|
||||||
|
redis:
|
||||||
|
host: 47.97.27.195:6379
|
||||||
|
type: node
|
||||||
|
pass: lansexiongdi@666
|
||||||
|
key: report-api-test
|
||||||
|
pollSize: 5 #连接池大小,不配置,或配置为0表示不启用连接池
|
||||||
|
minIdleConns: 2 #最小空闲连接数
|
||||||
|
maxIdleTime: 30 #每个连接最大空闲时间,如果超过了这个时间会被关闭
|
||||||
|
tls: 30
|
||||||
|
db:
|
||||||
|
db:
|
||||||
|
driver: mysql
|
||||||
|
source: root:SD###sdf323r343@tcp(121.199.38.107:3306)/sys_ai_test?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai
|
||||||
|
|
||||||
|
tools:
|
||||||
|
zltxOrderDetail:
|
||||||
|
enabled: true
|
||||||
|
base_url: "https://gateway.dev.cdlsxd.cn/zltx_api/admin/direct/ai/%s"
|
||||||
|
add_url: "https://gateway.dev.cdlsxd.cn/zltx_api/admin/direct/log/%s/%s"
|
||||||
|
api_key: "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ1c2VyQ2VudGVyIiwiZXhwIjoxNzU4MDkxOTU4LCJuYmYiOjE3NTgwOTAxNTgsImp0aSI6IjEiLCJQaG9uZSI6IjE4MDAwMDAwMDAwIiwiVXNlck5hbWUiOiJsc3hkIiwiUmVhbE5hbWUiOiLotoXnuqfnrqHnkIblkZgiLCJBY2NvdW50VHlwZSI6MSwiR3JvdXBDb2RlcyI6IlZDTF9DQVNISUVSLFZDTF9PUEVSQVRFLFZDTF9BRE1JTixWQ0xfQUFBLFZDTF9WQ0xfT1BFUkFULFZDTF9JTlZPSUNFLENSTV9BRE1JTixMSUFOTElBTl9BRE1JTixNQVJLRVRNQUcyX0FETUlOLFBIT05FQklMTF9BRE1JTixRSUFOWkhVX1NVUFBFUl9BRE0sTUFSS0VUSU5HU0FBU19TVVBFUkFETUlOLENBUkRfQ09ERSxDQVJEX1BST0NVUkVNRU5ULE1BUktFVElOR1NZU1RFTV9TVVBFUixTVEFUSVNUSUNBTFNZU1RFTV9BRE1JTixaTFRYX0FETUlOLFpMVFhfT1BFUkFURSIsIkRpbmdVc2VySWQiOiIxNjIwMjYxMjMwMjg5MzM4MzQifQ.Bjsx9f8yfcrV9EWxb0n6POwnXVOq9XPRD78JFZnnf1_VAVMN78W4W570SZL27PWuDnkD7E4oUg6RzeZwZgl7BZrNpNr-a-QpNC5qCptqrqXeNfVStmX7pxWA8GqnzI8ybkZgbhQ58Gje7DzdJtBq_8zte_LDaYhTYXdIc5EAG0AbCzAk22nPTl47nkMeHtmisXQVLEsdibl1hW3ViFJlXwfXvUrOENItmL1_mRYkggUB0MaTu2nHJOYM6PaOVGLHx-74eepnmK2rm6konFEb6ed-Ukc6gVR-nM9yWZaYLYNGNKJLwZoCX3tRuerq74n4kzQgWmUEJeaVI1yIGSw1zw"
|
||||||
|
zltxProduct:
|
||||||
|
enabled: true
|
||||||
|
base_url: "https://gateway.dev.cdlsxd.cn/zltx_api/admin/oursProduct"
|
||||||
|
add_url: "https://gateway.dev.cdlsxd.cn/zltx_api/admin/platformProduct/getProductsByOfficialProductId"
|
||||||
|
api_key: "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ1c2VyQ2VudGVyIiwiZXhwIjoxNzU2MTgyNTM1LCJuYmYiOjE3NTYxODA3MzUsImp0aSI6IjEiLCJQaG9uZSI6IjE4MDAwMDAwMDAwIiwiVXNlck5hbWUiOiJsc3hkIiwiUmVhbE5hbWUiOiLotoXnuqfnrqHnkIblkZgiLCJBY2NvdW50VHlwZSI6MSwiR3JvdXBDb2RlcyI6IlZDTF9DQVNISUVSLFZDTF9PUEVSQVRFLFZDTF9BRE1JTixWQ0xfQUFBLFZDTF9WQ0xfT1BFUkFULFZDTF9JTlZPSUNFLENSTV9BRE1JTixMSUFOTElBTl9BRE1JTixNQVJLRVRNQUcyX0FETUlOLFBIT05FQklMTF9BRE1JTixRSUFOWkhVX1NVUFBFUl9BRE0sTUFSS0VUSU5HU0FBU19TVVBFUkFETUlOLENBUkRfQ09ERSxDQVJEX1BST0NVUkVNRU5ULE1BUktFVElOR1NZU1RFTV9TVVBFUixTVEFUSVNUSUNBTFNZU1RFTV9BRE1JTixaTFRYX0FETUlOLFpMVFhfT1BFUkFURSIsIkRpbmdVc2VySWQiOiIxNjIwMjYxMjMwMjg5MzM4MzQifQ.N1xv1PYbcO8_jR5adaczc16YzGsr4z101gwEZdulkRaREBJNYTOnFrvRxTFx3RJTooXsqTqroE1MR84v_1WPX6BS6kKonA-kC1Jgot6yrt5rFWhGNGb2Cpr9rKIFCCQYmiGd3AUgDazEeaQ0_sodv3E-EXg9VfE1SX8nMcck9Yjnc8NCy7RTWaBIaSeOdZcEl-JfCD0S6GSx3oErp_hk-U9FKGwf60wAuDGTY1R0BP4BYpcEqS-C2LSnsSGyURi54Cuk5xH8r1WuF0Dm5bwAj5d7Hvs77-N_sUF-C5ONqyZJRAEhYLgcmN9RX_WQZfizdQJxizlTczdpzYfy-v-1eQ"
|
||||||
|
zltxOrderStatistics:
|
||||||
|
base_url: "https://gateway.dev.cdlsxd.cn/zltx_api/admin/direct/ai/search/"
|
||||||
|
enabled: true
|
||||||
|
api_key: "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ1c2VyQ2VudGVyIiwiZXhwIjoxNzU2MTgyNTM1LCJuYmYiOjE3NTYxODA3MzUsImp0aSI6IjEiLCJQaG9uZSI6IjE4MDAwMDAwMDAwIiwiVXNlck5hbWUiOiJsc3hkIiwiUmVhbE5hbWUiOiLotoXnuqfnrqHnkIblkZgiLCJBY2NvdW50VHlwZSI6MSwiR3JvdXBDb2RlcyI6IlZDTF9DQVNISUVSLFZDTF9PUEVSQVRFLFZDTF9BRE1JTixWQ0xfQUFBLFZDTF9WQ0xfT1BFUkFULFZDTF9JTlZPSUNFLENSTV9BRE1JTixMSUFOTElBTl9BRE1JTixNQVJLRVRNQUcyX0FETUlOLFBIT05FQklMTF9BRE1JTixRSUFOWkhVX1NVUFBFUl9BRE0sTUFSS0VUSU5HU0FBU19TVVBFUkFETUlOLENBUkRfQ09ERSxDQVJEX1BST0NVUkVNRU5ULE1BUktFVElOR1NZU1RFTV9TVVBFUixTVEFUSVNUSUNBTFNZU1RFTV9BRE1JTixaTFRYX0FETUlOLFpMVFhfT1BFUkFURSIsIkRpbmdVc2VySWQiOiIxNjIwMjYxMjMwMjg5MzM4MzQifQ.N1xv1PYbcO8_jR5adaczc16YzGsr4z101gwEZdulkRaREBJNYTOnFrvRxTFx3RJTooXsqTqroE1MR84v_1WPX6BS6kKonA-kC1Jgot6yrt5rFWhGNGb2Cpr9rKIFCCQYmiGd3AUgDazEeaQ0_sodv3E-EXg9VfE1SX8nMcck9Yjnc8NCy7RTWaBIaSeOdZcEl-JfCD0S6GSx3oErp_hk-U9FKGwf60wAuDGTY1R0BP4BYpcEqS-C2LSnsSGyURi54Cuk5xH8r1WuF0Dm5bwAj5d7Hvs77-N_sUF-C5ONqyZJRAEhYLgcmN9RX_WQZfizdQJxizlTczdpzYfy-v-1eQ"
|
||||||
|
knowledge:
|
||||||
|
base_url: "http://117.175.169.61:10000"
|
||||||
|
enabled: true
|
||||||
|
DingTalkBot:
|
||||||
|
enabled: true
|
||||||
|
api_key: "dingsbbntrkeiyazcfdg"
|
||||||
|
api_secret: "ObqxwyR20r9rVNhju0sCPQyQA98_FZSc32W4vgxnGFH_b02HZr1BPCJsOAF816nu"
|
||||||
|
zltxOrderAfterSaleSupplier:
|
||||||
|
enabled: true
|
||||||
|
base_url: "https://gateway.dev.cdlsxd.cn/zltx_api/admin/afterSales/directs"
|
||||||
|
zltxOrderAfterSaleReseller:
|
||||||
|
enabled: true
|
||||||
|
base_url: "https://gateway.dev.cdlsxd.cn/zltx_api/admin/afterSales/reseller_pre_ai"
|
||||||
|
zltxOrderAfterSaleResellerBatch:
|
||||||
|
enabled: true
|
||||||
|
base_url: "https://gateway.dev.cdlsxd.cn/zltx_api/admin/afterSales/reseller_pre_ai"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
default_prompt:
|
||||||
|
img_recognize:
|
||||||
|
system_prompt:
|
||||||
|
'你是一个具备图像理解与用户意图分析能力的智能助手。当用户提供一张图片时,请完成以下任务:
|
||||||
|
1. 关键信息提取:
|
||||||
|
提取出图片中对用户可能有用的关键信息(例如金额、日期、标题、编号、联系信息、商品名称等)。
|
||||||
|
若图片为文档类(如合同、发票、收据),请结构化输出关键字段(如客户名称、金额、开票日期等)。
|
||||||
|
'
|
||||||
|
user_prompt: '识别图片内容'
|
||||||
|
# 权限配置
|
||||||
|
permissionConfig:
|
||||||
|
permission_url: "http://api.test.user.1688sup.cn:8001/v1/menu/myCodes?systemId="
|
||||||
20
deploy.sh
20
deploy.sh
|
|
@ -6,7 +6,22 @@ export CONTAINER_NAME=ai_scheduler
|
||||||
export CGO_ENABLED='0'
|
export CGO_ENABLED='0'
|
||||||
|
|
||||||
|
|
||||||
git pull origin master
|
MODE="$1"
|
||||||
|
if [ "$MODE" != "dev" ] && [ "$MODE" != "prod" ]; then
|
||||||
|
echo "Usage: $0 dev|prod"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
CONFIG_FILE="config/config.yaml"
|
||||||
|
BRANCH="master"
|
||||||
|
if [ "$MODE" = "dev" ]; then
|
||||||
|
CONFIG_FILE="config/config_test.yaml"
|
||||||
|
BRANCH="test"
|
||||||
|
fi
|
||||||
|
|
||||||
|
git fetch origin
|
||||||
|
git checkout "$BRANCH"
|
||||||
|
git pull origin "$BRANCH"
|
||||||
go mod tidy
|
go mod tidy
|
||||||
make build
|
make build
|
||||||
docker build -t ${CONTAINER_NAME} .
|
docker build -t ${CONTAINER_NAME} .
|
||||||
|
|
@ -16,7 +31,8 @@ docker run -itd \
|
||||||
--name "${CONTAINER_NAME}" \
|
--name "${CONTAINER_NAME}" \
|
||||||
--restart=always \
|
--restart=always \
|
||||||
-e "OLLAMA_BASE_URL=${OLLAMA_BASE_URL:-http://host.docker.internal:11434}" \
|
-e "OLLAMA_BASE_URL=${OLLAMA_BASE_URL:-http://host.docker.internal:11434}" \
|
||||||
|
-e "MODE=${MODE}" \
|
||||||
-p 8090:8090 \
|
-p 8090:8090 \
|
||||||
"${CONTAINER_NAME}"
|
"${CONTAINER_NAME}" ./bin/server --config "./${CONFIG_FILE}"
|
||||||
|
|
||||||
docker logs -f ${CONTAINER_NAME}
|
docker logs -f ${CONTAINER_NAME}
|
||||||
2
gen.sh
2
gen.sh
|
|
@ -16,4 +16,4 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
gentool --dsn "root:SD###sdf323r343@tcp(121.199.38.107:3306)/sys_ai?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai" -outPath ${modeldir} -onlyModel -modelPkgName "model" -tables ${prefix}${tables}
|
gentool --dsn "root:SD###sdf323r343@tcp(121.199.38.107:3306)/sys_ai_test?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai" -outPath ${modeldir} -onlyModel -modelPkgName "model" -tables ${prefix}${tables}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,8 @@ import (
|
||||||
"ai_scheduler/internal/data/model"
|
"ai_scheduler/internal/data/model"
|
||||||
"ai_scheduler/internal/entitys"
|
"ai_scheduler/internal/entitys"
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"xorm.io/builder"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ChatHistoryBiz struct {
|
type ChatHistoryBiz struct {
|
||||||
|
|
@ -15,35 +17,41 @@ func NewChatHistoryBiz(chatRepo *impl.ChatImpl) *ChatHistoryBiz {
|
||||||
s := &ChatHistoryBiz{
|
s := &ChatHistoryBiz{
|
||||||
chatRepo: chatRepo,
|
chatRepo: chatRepo,
|
||||||
}
|
}
|
||||||
go s.AsyncProcess(context.Background())
|
//go s.AsyncProcess(context.Background())
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ChatHistoryBiz) create(ctx context.Context, sessionID, role, content string) error {
|
//func (s *ChatHistoryBiz) create(ctx context.Context, sessionID, role, content string) error {
|
||||||
chat := model.AiChatHi{
|
// chat := model.AiChatHi{
|
||||||
SessionID: sessionID,
|
// SessionID: sessionID,
|
||||||
Role: role,
|
// Role: role,
|
||||||
Content: content,
|
// Content: content,
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
return s.chatRepo.Create(&chat)
|
// return s.chatRepo.Create(&chat)
|
||||||
}
|
//}
|
||||||
|
//
|
||||||
// 添加会话历史
|
//// 添加会话历史
|
||||||
func (s *ChatHistoryBiz) Create(ctx context.Context, chat entitys.ChatHistory) error {
|
//func (s *ChatHistoryBiz) Create(ctx context.Context, chat entitys.ChatHistory) error {
|
||||||
return s.create(ctx, chat.SessionID, chat.Role.String(), chat.Content)
|
// return s.create(ctx, chat.SessionID, chat.Role.String(), chat.Content)
|
||||||
}
|
//}
|
||||||
|
|
||||||
// 异步添加会话历史
|
// 异步添加会话历史
|
||||||
func (s *ChatHistoryBiz) AsyncCreate(ctx context.Context, chat entitys.ChatHistory) {
|
//func (s *ChatHistoryBiz) AsyncCreate(ctx context.Context, chat entitys.ChatHistory) {
|
||||||
s.chatRepo.AsyncCreate(ctx, model.AiChatHi{
|
// s.chatRepo.AsyncCreate(ctx, model.AiChatHi{
|
||||||
SessionID: chat.SessionID,
|
// SessionID: chat.SessionID,
|
||||||
Role: chat.Role.String(),
|
// Role: chat.Role.String(),
|
||||||
Content: chat.Content,
|
// Content: chat.Content,
|
||||||
})
|
// })
|
||||||
}
|
//}
|
||||||
|
|
||||||
// 异步处理会话历史
|
// 异步处理会话历史
|
||||||
func (s *ChatHistoryBiz) AsyncProcess(ctx context.Context) {
|
//func (s *ChatHistoryBiz) AsyncProcess(ctx context.Context) {
|
||||||
s.chatRepo.AsyncProcess(ctx)
|
// s.chatRepo.AsyncProcess(ctx)
|
||||||
|
//}
|
||||||
|
|
||||||
|
func (s *ChatHistoryBiz) Update(ctx context.Context, chat *entitys.UseFulRequest) error {
|
||||||
|
cond := builder.NewCond()
|
||||||
|
cond = cond.And(builder.Eq{"his_id": chat.HisId})
|
||||||
|
return s.chatRepo.UpdateByCond(&cond, &model.AiChatHi{HisID: chat.HisId, Useful: chat.Useful})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,10 +6,13 @@ import (
|
||||||
"ai_scheduler/internal/data/impl"
|
"ai_scheduler/internal/data/impl"
|
||||||
"ai_scheduler/internal/data/model"
|
"ai_scheduler/internal/data/model"
|
||||||
"ai_scheduler/internal/entitys"
|
"ai_scheduler/internal/entitys"
|
||||||
|
"ai_scheduler/internal/gateway"
|
||||||
"ai_scheduler/internal/pkg"
|
"ai_scheduler/internal/pkg"
|
||||||
"ai_scheduler/tmpl/dataTemp"
|
"ai_scheduler/tmpl/dataTemp"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -22,7 +25,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Do struct {
|
type Do struct {
|
||||||
Ctx *entitys.RequireData
|
//Ctx *entitys.RequireData
|
||||||
sessionImpl *impl.SessionImpl
|
sessionImpl *impl.SessionImpl
|
||||||
sysImpl *impl.SysImpl
|
sysImpl *impl.SysImpl
|
||||||
taskImpl *impl.TaskImpl
|
taskImpl *impl.TaskImpl
|
||||||
|
|
@ -44,78 +47,125 @@ func NewDo(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Do) InitCtx(req *entitys.ChatSockRequest) *Do {
|
func (d *Do) DataAuth(ctx context.Context, client *gateway.Client, requireData *entitys.RequireData) (err error) {
|
||||||
d.Ctx = &entitys.RequireData{
|
// 1. 验证客户端数据
|
||||||
Req: req,
|
if err = d.validateClientData(client, requireData); err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
return d
|
|
||||||
|
// 2. 加载系统信息
|
||||||
|
if err = d.loadSystemInfo(ctx, client, requireData); err != nil {
|
||||||
|
return fmt.Errorf("获取系统信息失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 加载任务列表
|
||||||
|
if err = d.loadTaskList(ctx, client, requireData); err != nil {
|
||||||
|
return fmt.Errorf("获取任务列表失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. 加载聊天历史
|
||||||
|
if err = d.loadChatHistory(ctx, requireData); err != nil {
|
||||||
|
return fmt.Errorf("获取历史记录失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. 加载图片数据
|
||||||
|
if err = d.getImgData(requireData); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 6. 加载用户权限
|
||||||
|
if _, err = d.LoadUserPermission(client, requireData); err != nil {
|
||||||
|
return fmt.Errorf("获取用户权限失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Do) DataAuth(c *websocket.Conn) (err error) {
|
// 提取数据验证为单独函数
|
||||||
d.Ctx.Session = c.Query("x-session", "")
|
func (d *Do) validateClientData(client *gateway.Client, requireData *entitys.RequireData) error {
|
||||||
if len(d.Ctx.Session) == 0 {
|
requireData.Session = client.GetSession()
|
||||||
err = errors.SessionNotFound
|
if len(requireData.Session) == 0 {
|
||||||
return
|
return errors.SessionNotFound
|
||||||
}
|
|
||||||
d.Ctx.Auth = c.Query("x-authorization", "")
|
|
||||||
if len(d.Ctx.Auth) == 0 {
|
|
||||||
err = errors.AuthNotFound
|
|
||||||
return
|
|
||||||
}
|
|
||||||
d.Ctx.Key = c.Query("x-app-key", "")
|
|
||||||
if len(d.Ctx.Key) == 0 {
|
|
||||||
err = errors.KeyNotFound
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
d.Ctx.Sys, err = d.getSysInfo()
|
requireData.Auth = client.GetAuth()
|
||||||
if err != nil {
|
if len(requireData.Auth) == 0 {
|
||||||
err = errors.SysErr("获取系统信息失败:%v", err.Error())
|
return errors.AuthNotFound
|
||||||
return
|
|
||||||
}
|
|
||||||
d.Ctx.Histories, err = d.getSessionChatHis()
|
|
||||||
if err != nil {
|
|
||||||
err = errors.SysErr("获取历史记录失败:%v", err.Error())
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
d.Ctx.Tasks, err = d.getTasks(d.Ctx.Sys.SysID)
|
requireData.Key = client.GetKey()
|
||||||
if err != nil {
|
if len(requireData.Key) == 0 {
|
||||||
err = errors.SysErr("获取任务列表失败:%v", err.Error())
|
return errors.KeyNotFound
|
||||||
return
|
|
||||||
}
|
|
||||||
if err = d.getImgData(); err != nil {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Do) MakeCh(c *websocket.Conn) (ctx context.Context, deferFunc func()) {
|
// 获取系统信息的辅助函数
|
||||||
d.Ctx.Ch = make(chan entitys.Response)
|
func (d *Do) loadSystemInfo(ctx context.Context, client *gateway.Client, requireData *entitys.RequireData) error {
|
||||||
|
if sysInfo := client.GetSysInfo(); sysInfo == nil {
|
||||||
|
sys, err := d.getSysInfo(requireData)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
client.SetSysInfo(&sys)
|
||||||
|
requireData.Sys = sys
|
||||||
|
} else {
|
||||||
|
requireData.Sys = *sysInfo
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取任务列表的辅助函数
|
||||||
|
func (d *Do) loadTaskList(ctx context.Context, client *gateway.Client, requireData *entitys.RequireData) error {
|
||||||
|
if taskInfo := client.GetTasks(); len(taskInfo) == 0 {
|
||||||
|
tasks, err := d.getTasks(requireData.Sys.SysID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
requireData.Tasks = tasks
|
||||||
|
client.SetTasks(tasks)
|
||||||
|
} else {
|
||||||
|
requireData.Tasks = taskInfo
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取历史记录的辅助函数
|
||||||
|
func (d *Do) loadChatHistory(ctx context.Context, requireData *entitys.RequireData) error {
|
||||||
|
histories, err := d.getSessionChatHis(requireData)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
requireData.Histories = histories
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Do) MakeCh(c *websocket.Conn, requireData *entitys.RequireData) (ctx context.Context, deferFunc func()) {
|
||||||
|
requireData.Ch = make(chan entitys.Response)
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
done := d.startMessageHandler(ctx, c, d.hisImpl)
|
done := d.startMessageHandler(ctx, c, requireData)
|
||||||
return ctx, func() {
|
return ctx, func() {
|
||||||
close(d.Ctx.Ch) //关闭主通道
|
close(requireData.Ch) //关闭主通道
|
||||||
<-done // 等待消息处理完成
|
<-done // 等待消息处理完成
|
||||||
cancel()
|
cancel()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Do) getImgData() (err error) {
|
func (d *Do) getImgData(requireData *entitys.RequireData) (err error) {
|
||||||
if len(d.Ctx.Req.Img) == 0 {
|
if len(requireData.Req.Img) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
imgs := strings.Split(d.Ctx.Req.Img, ",")
|
imgs := strings.Split(requireData.Req.Img, ",")
|
||||||
if len(imgs) == 0 {
|
if len(imgs) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for k, img := range imgs {
|
for k, img := range imgs {
|
||||||
baseErr := "获取第" + strconv.Itoa(k+1) + "张图片失败:"
|
baseErr := "获取第" + strconv.Itoa(k+1) + "张图片失败:"
|
||||||
entitys.ResLog(d.Ctx.Ch, "img_get_start", "正在获取第"+strconv.Itoa(k+1)+"张图片")
|
entitys.ResLog(requireData.Ch, "img_get_start", "正在获取第"+strconv.Itoa(k+1)+"张图片")
|
||||||
if err = pkg.ValidateImageURL(img); err != nil {
|
if err = pkg.ValidateImageURL(img); err != nil {
|
||||||
entitys.ResLog(d.Ctx.Ch, "", baseErr+":expected image content")
|
entitys.ResLog(requireData.Ch, "", baseErr+":expected image content")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
req := l_request.Request{
|
req := l_request.Request{
|
||||||
|
|
@ -128,20 +178,20 @@ func (d *Do) getImgData() (err error) {
|
||||||
}
|
}
|
||||||
res, _err := req.Send()
|
res, _err := req.Send()
|
||||||
if _err != nil {
|
if _err != nil {
|
||||||
entitys.ResLog(d.Ctx.Ch, "", baseErr+_err.Error())
|
entitys.ResLog(requireData.Ch, "", baseErr+_err.Error())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if _, ex := res.Headers["Content-Type"]; !ex {
|
if _, ex := res.Headers["Content-Type"]; !ex {
|
||||||
entitys.ResLog(d.Ctx.Ch, "", baseErr+":Content-Type不存在")
|
entitys.ResLog(requireData.Ch, "", baseErr+":Content-Type不存在")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if !strings.HasPrefix(res.Headers["Content-Type"], "image/") {
|
if !strings.HasPrefix(res.Headers["Content-Type"], "image/") {
|
||||||
entitys.ResLog(d.Ctx.Ch, "", baseErr+":expected image content")
|
entitys.ResLog(requireData.Ch, "", baseErr+":expected image content")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
d.Ctx.ImgByte = append(d.Ctx.ImgByte, res.Content)
|
requireData.ImgByte = append(requireData.ImgByte, res.Content)
|
||||||
d.Ctx.ImgUrls = append(d.Ctx.ImgUrls, img)
|
requireData.ImgUrls = append(requireData.ImgUrls, img)
|
||||||
entitys.ResLog(d.Ctx.Ch, "img_get_end", "第"+strconv.Itoa(k+1)+"张图片获取成功")
|
entitys.ResLog(requireData.Ch, "img_get_end", "第"+strconv.Itoa(k+1)+"张图片获取成功")
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
@ -152,19 +202,19 @@ func (d *Do) getRequireData() (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Do) getSysInfo() (sysInfo model.AiSy, err error) {
|
func (d *Do) getSysInfo(requireData *entitys.RequireData) (sysInfo model.AiSy, err error) {
|
||||||
cond := builder.NewCond()
|
cond := builder.NewCond()
|
||||||
cond = cond.And(builder.Eq{"app_key": d.Ctx.Key})
|
cond = cond.And(builder.Eq{"app_key": requireData.Key})
|
||||||
cond = cond.And(builder.IsNull{"delete_at"})
|
cond = cond.And(builder.IsNull{"delete_at"})
|
||||||
cond = cond.And(builder.Eq{"status": 1})
|
cond = cond.And(builder.Eq{"status": 1})
|
||||||
err = d.sysImpl.GetOneBySearchToStrut(&cond, &sysInfo)
|
err = d.sysImpl.GetOneBySearchToStrut(&cond, &sysInfo)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Do) getSessionChatHis() (his []model.AiChatHi, err error) {
|
func (d *Do) getSessionChatHis(requireData *entitys.RequireData) (his []model.AiChatHi, err error) {
|
||||||
|
|
||||||
cond := builder.NewCond()
|
cond := builder.NewCond()
|
||||||
cond = cond.And(builder.Eq{"session_id": d.Ctx.Session})
|
cond = cond.And(builder.Eq{"session_id": requireData.Session})
|
||||||
|
|
||||||
_, err = d.hisImpl.GetListToStruct(&cond, &dataTemp.ReqPageBo{Limit: d.conf.Sys.SessionLen}, &his, "his_id desc")
|
_, err = d.hisImpl.GetListToStruct(&cond, &dataTemp.ReqPageBo{Limit: d.conf.Sys.SessionLen}, &his, "his_id desc")
|
||||||
|
|
||||||
|
|
@ -186,7 +236,7 @@ func (d *Do) getTasks(sysId int32) (tasks []model.AiTask, err error) {
|
||||||
func (d *Do) startMessageHandler(
|
func (d *Do) startMessageHandler(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
c *websocket.Conn,
|
c *websocket.Conn,
|
||||||
hisImpl *impl.ChatImpl,
|
requireData *entitys.RequireData,
|
||||||
) <-chan struct{} {
|
) <-chan struct{} {
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
var chat []string
|
var chat []string
|
||||||
|
|
@ -195,26 +245,28 @@ func (d *Do) startMessageHandler(
|
||||||
defer func() {
|
defer func() {
|
||||||
close(done)
|
close(done)
|
||||||
// 保存历史记录
|
// 保存历史记录
|
||||||
var his = []*model.AiChatHi{
|
var (
|
||||||
{
|
hisLog = &entitys.ChatHisLog{}
|
||||||
SessionID: d.Ctx.Session,
|
)
|
||||||
Role: "user",
|
|
||||||
Content: d.Ctx.Req.Text, // 用户输入在外部处理
|
|
||||||
},
|
|
||||||
}
|
|
||||||
if len(chat) > 0 {
|
if len(chat) > 0 {
|
||||||
his = append(his, &model.AiChatHi{
|
AiRes := &model.AiChatHi{
|
||||||
SessionID: d.Ctx.Session,
|
SessionID: requireData.Session,
|
||||||
Role: "assistant",
|
Ques: requireData.Req.Text,
|
||||||
Content: strings.Join(chat, ""),
|
Ans: strings.Join(chat, ""),
|
||||||
})
|
Files: requireData.Req.Img,
|
||||||
}
|
}
|
||||||
for _, hi := range his {
|
d.hisImpl.AddWithData(AiRes)
|
||||||
hisImpl.Add(hi)
|
hisLog.HisId = AiRes.HisID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_ = entitys.MsgSend(c, entitys.Response{
|
||||||
|
Content: pkg.JsonStringIgonErr(hisLog),
|
||||||
|
Type: entitys.ResponseEnd,
|
||||||
|
})
|
||||||
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
for v := range d.Ctx.Ch { // 自动检测通道关闭
|
for v := range requireData.Ch { // 自动检测通道关闭
|
||||||
if err := sendWithTimeout(c, v, 2*time.Second); err != nil {
|
if err := sendWithTimeout(c, v, 2*time.Second); err != nil {
|
||||||
log.Errorf("Send error: %v", err)
|
log.Errorf("Send error: %v", err)
|
||||||
return
|
return
|
||||||
|
|
@ -253,3 +305,51 @@ func sendWithTimeout(c *websocket.Conn, data entitys.Response, timeout time.Dura
|
||||||
return sendCtx.Err()
|
return sendCtx.Err()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 从统一登录平台获取用户权限
|
||||||
|
func (d *Do) LoadUserPermission(client *gateway.Client, requireData *entitys.RequireData) (codes []string, err error) {
|
||||||
|
if len(client.GetCodes()) > 0 {
|
||||||
|
return client.GetCodes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
request l_request.Request
|
||||||
|
)
|
||||||
|
|
||||||
|
// 构建请求URL
|
||||||
|
request.Url = d.conf.PermissionConfig.PermissionURL + strconv.Itoa(int(requireData.Sys.SysID))
|
||||||
|
|
||||||
|
request.Method = "GET"
|
||||||
|
request.Headers = map[string]string{
|
||||||
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
|
||||||
|
"Accept": "application/json, text/plain, */*",
|
||||||
|
"Authorization": "Bearer " + client.GetAuth(),
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发送请求
|
||||||
|
res, err := request.Send()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查响应状态码
|
||||||
|
if res.StatusCode != http.StatusOK {
|
||||||
|
err = errors.SysErr("获取用户权限失败")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
type resp struct {
|
||||||
|
Codes []string `json:"codes"`
|
||||||
|
}
|
||||||
|
// 解析响应体
|
||||||
|
var respBody resp
|
||||||
|
err = json.Unmarshal([]byte(res.Text), &respBody)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置客户端权限
|
||||||
|
client.SetCodes(respBody.Codes)
|
||||||
|
|
||||||
|
return respBody.Codes, nil
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"ai_scheduler/internal/data/impl"
|
"ai_scheduler/internal/data/impl"
|
||||||
"ai_scheduler/internal/data/model"
|
"ai_scheduler/internal/data/model"
|
||||||
"ai_scheduler/internal/entitys"
|
"ai_scheduler/internal/entitys"
|
||||||
|
"ai_scheduler/internal/gateway"
|
||||||
"ai_scheduler/internal/pkg"
|
"ai_scheduler/internal/pkg"
|
||||||
"ai_scheduler/internal/pkg/l_request"
|
"ai_scheduler/internal/pkg/l_request"
|
||||||
"ai_scheduler/internal/pkg/mapstructure"
|
"ai_scheduler/internal/pkg/mapstructure"
|
||||||
|
|
@ -16,6 +17,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"gorm.io/gorm/utils"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -68,7 +70,7 @@ func (r *Handle) handleOtherTask(ctx context.Context, requireData *entitys.Requi
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Handle) HandleMatch(ctx context.Context, requireData *entitys.RequireData) (err error) {
|
func (r *Handle) HandleMatch(ctx context.Context, client *gateway.Client, requireData *entitys.RequireData) (err error) {
|
||||||
|
|
||||||
if !requireData.Match.IsMatch {
|
if !requireData.Match.IsMatch {
|
||||||
if len(requireData.Match.Chat) != 0 {
|
if len(requireData.Match.Chat) != 0 {
|
||||||
|
|
@ -90,7 +92,14 @@ func (r *Handle) HandleMatch(ctx context.Context, requireData *entitys.RequireDa
|
||||||
if pointTask == nil || pointTask.Index == "other" {
|
if pointTask == nil || pointTask.Index == "other" {
|
||||||
return r.OtherTask(ctx, requireData)
|
return r.OtherTask(ctx, requireData)
|
||||||
}
|
}
|
||||||
switch pointTask.Type {
|
|
||||||
|
// 校验用户权限
|
||||||
|
// if err = r.PermissionAuth(client, pointTask); err != nil {
|
||||||
|
// log.Errorf("权限验证失败: %s", err.Error())
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
|
switch constants.TaskType(pointTask.Type) {
|
||||||
case constants.TaskTypeApi:
|
case constants.TaskTypeApi:
|
||||||
return r.handleApiTask(ctx, requireData, pointTask)
|
return r.handleApiTask(ctx, requireData, pointTask)
|
||||||
case constants.TaskTypeFunc:
|
case constants.TaskTypeFunc:
|
||||||
|
|
@ -252,3 +261,13 @@ func (r *Handle) handleApiTask(ctx context.Context, requireData *entitys.Require
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 权限验证
|
||||||
|
func (r *Handle) PermissionAuth(client *gateway.Client, pointTask *model.AiTask) (err error) {
|
||||||
|
// 授权检查权限
|
||||||
|
if !utils.Contains(client.GetCodes(), pointTask.Index) {
|
||||||
|
return fmt.Errorf("用户权限不足: %s", pointTask.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
package llm_service
|
package llm_service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"ai_scheduler/internal/data/constants"
|
||||||
"ai_scheduler/internal/data/model"
|
"ai_scheduler/internal/data/model"
|
||||||
"ai_scheduler/internal/entitys"
|
"ai_scheduler/internal/entitys"
|
||||||
"context"
|
"context"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type LlmService interface {
|
type LlmService interface {
|
||||||
|
|
@ -13,7 +15,7 @@ type LlmService interface {
|
||||||
// buildSystemPrompt 构建系统提示词
|
// buildSystemPrompt 构建系统提示词
|
||||||
func buildSystemPrompt(prompt string) string {
|
func buildSystemPrompt(prompt string) string {
|
||||||
if len(prompt) == 0 {
|
if len(prompt) == 0 {
|
||||||
prompt = "[system] 你是一个智能路由系统,核心职责是 **精准解析用户意图并路由至对应任务模块**\n[rule]\n1.返回以下格式的JSON:{ \"index\": \"工具索引index\", \"confidence\": 0.0-1.0,\"reasoning\": \"判断理由\"}\n2.严格返回字符串格式,禁用markdown格式返回\n3.只返回json字符串,不包含任何其他解释性文字\n4.当用户意图非常不清晰时使用,尝试进行追问具体希望查询内容"
|
prompt = "[system] 你是一个智能路由系统,核心职责是 **精准解析用户意图并路由至对应任务模块**\n[rule]\n1.返回以下格式的JSON:{ \"index\": \"工具索引index\", \"confidence\": 0.0-1.0,\"reasoning\": \"判断理由\"}\n2.严格返回字符串格式,禁用markdown格式返回\n3.只返回json字符串,不包含任何其他解释性文字\n4.当用户意图非常不清晰时使用,尝试进行追问具体希望查询内容,\n当前时间是:" + time.Now().Format(time.DateTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
return prompt
|
return prompt
|
||||||
|
|
@ -24,11 +26,18 @@ func buildAssistant(his []model.AiChatHi) (chatHis entitys.ChatHis) {
|
||||||
if len(chatHis.SessionId) == 0 {
|
if len(chatHis.SessionId) == 0 {
|
||||||
chatHis.SessionId = item.SessionID
|
chatHis.SessionId = item.SessionID
|
||||||
}
|
}
|
||||||
chatHis.Messages = append(chatHis.Messages, entitys.HisMessage{
|
chatHis.Messages = append(chatHis.Messages, []entitys.HisMessage{
|
||||||
Role: item.Role,
|
{
|
||||||
Content: item.Content,
|
Role: constants.RoleUser,
|
||||||
Timestamp: item.CreateAt.Format("2006-01-02 15:04:05"),
|
Content: item.Ques,
|
||||||
})
|
Timestamp: item.CreateAt.Format(time.DateTime),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Role: constants.RoleAssistant,
|
||||||
|
Content: item.Ans,
|
||||||
|
Timestamp: item.CreateAt.Format(time.DateTime),
|
||||||
|
},
|
||||||
|
}...)
|
||||||
}
|
}
|
||||||
chatHis.Context = entitys.HisContext{
|
chatHis.Context = entitys.HisContext{
|
||||||
UserLanguage: "zh-CN",
|
UserLanguage: "zh-CN",
|
||||||
|
|
@ -36,3 +45,23 @@ func buildAssistant(his []model.AiChatHi) (chatHis entitys.ChatHis) {
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BuildChatHisMessage(his []model.AiChatHi) (chatHis []entitys.HisMessage) {
|
||||||
|
for _, item := range his {
|
||||||
|
|
||||||
|
chatHis = append(chatHis, []entitys.HisMessage{
|
||||||
|
{
|
||||||
|
Role: constants.RoleUser,
|
||||||
|
Content: item.Ques,
|
||||||
|
Timestamp: item.CreateAt.Format(time.DateTime),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Role: constants.RoleAssistant,
|
||||||
|
Content: item.Ans,
|
||||||
|
Timestamp: item.CreateAt.Format(time.DateTime),
|
||||||
|
},
|
||||||
|
}...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package llm_service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"ai_scheduler/internal/config"
|
"ai_scheduler/internal/config"
|
||||||
|
"ai_scheduler/internal/data/impl"
|
||||||
"ai_scheduler/internal/data/model"
|
"ai_scheduler/internal/data/model"
|
||||||
"ai_scheduler/internal/entitys"
|
"ai_scheduler/internal/entitys"
|
||||||
"ai_scheduler/internal/pkg"
|
"ai_scheduler/internal/pkg"
|
||||||
|
|
@ -13,20 +14,24 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ollama/ollama/api"
|
"github.com/ollama/ollama/api"
|
||||||
|
"xorm.io/builder"
|
||||||
)
|
)
|
||||||
|
|
||||||
type OllamaService struct {
|
type OllamaService struct {
|
||||||
client *utils_ollama.Client
|
client *utils_ollama.Client
|
||||||
config *config.Config
|
config *config.Config
|
||||||
|
chatHis *impl.ChatImpl
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewOllamaGenerate(
|
func NewOllamaGenerate(
|
||||||
client *utils_ollama.Client,
|
client *utils_ollama.Client,
|
||||||
config *config.Config,
|
config *config.Config,
|
||||||
|
chatHis *impl.ChatImpl,
|
||||||
) *OllamaService {
|
) *OllamaService {
|
||||||
return &OllamaService{
|
return &OllamaService{
|
||||||
client: client,
|
client: client,
|
||||||
config: config,
|
config: config,
|
||||||
|
chatHis: chatHis,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -66,6 +71,10 @@ func (r *OllamaService) getPrompt(ctx context.Context, requireData *entitys.Requ
|
||||||
var (
|
var (
|
||||||
prompt = make([]api.Message, 0)
|
prompt = make([]api.Message, 0)
|
||||||
)
|
)
|
||||||
|
content, err := r.getUserContent(ctx, requireData)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
prompt = append(prompt, api.Message{
|
prompt = append(prompt, api.Message{
|
||||||
Role: "system",
|
Role: "system",
|
||||||
Content: buildSystemPrompt(requireData.Sys.SysPrompt),
|
Content: buildSystemPrompt(requireData.Sys.SysPrompt),
|
||||||
|
|
@ -74,32 +83,17 @@ func (r *OllamaService) getPrompt(ctx context.Context, requireData *entitys.Requ
|
||||||
Content: "### 聊天记录:" + pkg.JsonStringIgonErr(buildAssistant(requireData.Histories)),
|
Content: "### 聊天记录:" + pkg.JsonStringIgonErr(buildAssistant(requireData.Histories)),
|
||||||
}, api.Message{
|
}, api.Message{
|
||||||
Role: "user",
|
Role: "user",
|
||||||
Content: r.getUserContent(requireData),
|
Content: content,
|
||||||
//Images: requireData.ImgByte,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
if len(requireData.ImgByte) > 0 {
|
|
||||||
desc, err := r.RecognizeWithImg(ctx, requireData)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var imgs strings.Builder
|
|
||||||
imgs.WriteString("### 用户上传图片解析内容:\n")
|
|
||||||
|
|
||||||
prompt = append(prompt, api.Message{
|
|
||||||
Role: "image_desc",
|
|
||||||
Content: "" + desc.Response,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return prompt, nil
|
return prompt, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *OllamaService) getUserContent(requireData *entitys.RequireData) string {
|
func (r *OllamaService) getUserContent(ctx context.Context, requireData *entitys.RequireData) (string, error) {
|
||||||
var content strings.Builder
|
var content strings.Builder
|
||||||
content.WriteString(requireData.Req.Text)
|
content.WriteString(requireData.Req.Text)
|
||||||
if len(requireData.ImgByte) > 0 {
|
if len(requireData.ImgByte) > 0 {
|
||||||
content.WriteString("\n")
|
content.WriteString("\n")
|
||||||
content.WriteString("### 图片内容已经解析到image_desc里")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(requireData.Req.Tags) > 0 {
|
if len(requireData.Req.Tags) > 0 {
|
||||||
|
|
@ -107,7 +101,29 @@ func (r *OllamaService) getUserContent(requireData *entitys.RequireData) string
|
||||||
content.WriteString("### 工具必须使用:")
|
content.WriteString("### 工具必须使用:")
|
||||||
content.WriteString(requireData.Req.Tags)
|
content.WriteString(requireData.Req.Tags)
|
||||||
}
|
}
|
||||||
return content.String()
|
|
||||||
|
if len(requireData.ImgByte) > 0 {
|
||||||
|
desc, err := r.RecognizeWithImg(ctx, requireData)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
content.WriteString("### 上传图片解析内容:\n")
|
||||||
|
content.WriteString(requireData.Req.Tags)
|
||||||
|
content.WriteString(desc.Response)
|
||||||
|
}
|
||||||
|
|
||||||
|
if requireData.Req.MarkHis > 0 {
|
||||||
|
var his model.AiChatHi
|
||||||
|
cond := builder.NewCond()
|
||||||
|
cond = cond.And(builder.Eq{"his_id": requireData.Req.MarkHis})
|
||||||
|
err := r.chatHis.GetOneBySearchToStrut(&cond, &his)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
content.WriteString("### 引用历史聊天记录:\n")
|
||||||
|
content.WriteString(pkg.JsonStringIgonErr(BuildChatHisMessage([]model.AiChatHi{his})))
|
||||||
|
}
|
||||||
|
return content.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *OllamaService) RecognizeWithImg(ctx context.Context, requireData *entitys.RequireData) (desc api.GenerateResponse, err error) {
|
func (r *OllamaService) RecognizeWithImg(ctx context.Context, requireData *entitys.RequireData) (desc api.GenerateResponse, err error) {
|
||||||
|
|
|
||||||
|
|
@ -2,11 +2,11 @@ package biz
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"ai_scheduler/internal/biz/do"
|
"ai_scheduler/internal/biz/do"
|
||||||
|
"ai_scheduler/internal/gateway"
|
||||||
|
|
||||||
"ai_scheduler/internal/entitys"
|
"ai_scheduler/internal/entitys"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2/log"
|
"github.com/gofiber/fiber/v2/log"
|
||||||
"github.com/gofiber/websocket/v2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// AiRouterBiz 智能路由服务
|
// AiRouterBiz 智能路由服务
|
||||||
|
|
@ -26,28 +26,45 @@ func NewAiRouterBiz(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *AiRouterBiz) RouteWithSocket(c *websocket.Conn, req *entitys.ChatSockRequest) (err error) {
|
// 路由处理WebSocket请求
|
||||||
//必要数据验证和获取
|
//
|
||||||
dos := r.do.InitCtx(req)
|
// 参数:
|
||||||
|
// - client: 网关客户端
|
||||||
|
// - req: 聊天请求结构体
|
||||||
|
//
|
||||||
|
// 返回:
|
||||||
|
// - err: 错误信息
|
||||||
|
func (r *AiRouterBiz) RouteWithSocket(client *gateway.Client, req *entitys.ChatSockRequest) (err error) {
|
||||||
|
// 创建请求上下文数据
|
||||||
|
requireData := &entitys.RequireData{
|
||||||
|
Req: req,
|
||||||
|
}
|
||||||
|
// 获取WebSocket连接
|
||||||
|
conn := client.GetConn()
|
||||||
|
|
||||||
//初始化通道/上下文
|
//初始化通道/上下文
|
||||||
ctx, clearFunc := dos.MakeCh(c)
|
ctx, clearFunc := r.do.MakeCh(conn, requireData)
|
||||||
defer clearFunc()
|
defer func() {
|
||||||
|
if err != nil {
|
||||||
|
entitys.ResError(requireData.Ch, "", err.Error())
|
||||||
|
}
|
||||||
|
clearFunc()
|
||||||
|
}()
|
||||||
|
|
||||||
//数据验证和收集
|
//数据验证和收集
|
||||||
if err = dos.DataAuth(c); err != nil {
|
if err = r.do.DataAuth(ctx, client, requireData); err != nil {
|
||||||
log.Errorf("数据验证和收集失败: %s", err.Error())
|
log.Errorf("数据验证和收集失败: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//意图识别
|
//意图识别
|
||||||
if err = r.handle.Recognize(ctx, dos.Ctx); err != nil {
|
if err = r.handle.Recognize(ctx, requireData); err != nil {
|
||||||
log.Errorf("意图识别失败: %s", err.Error())
|
log.Errorf("意图识别失败: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//向下传递
|
//向下传递
|
||||||
if err = r.handle.HandleMatch(ctx, dos.Ctx); err != nil {
|
if err = r.handle.HandleMatch(ctx, client, requireData); err != nil {
|
||||||
log.Errorf("任务处理失败: %s", err.Error())
|
log.Errorf("任务处理失败: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,122 +1,122 @@
|
||||||
package biz
|
package biz
|
||||||
|
|
||||||
import (
|
// import (
|
||||||
"ai_scheduler/internal/config"
|
// "ai_scheduler/internal/config"
|
||||||
"ai_scheduler/internal/data/impl"
|
// "ai_scheduler/internal/data/impl"
|
||||||
"ai_scheduler/internal/data/model"
|
// "ai_scheduler/internal/data/model"
|
||||||
"ai_scheduler/internal/entitys"
|
// "ai_scheduler/internal/entitys"
|
||||||
"ai_scheduler/internal/pkg"
|
// "ai_scheduler/internal/pkg"
|
||||||
"ai_scheduler/internal/pkg/utils_ollama"
|
// "ai_scheduler/internal/pkg/utils_ollama"
|
||||||
"ai_scheduler/internal/tools"
|
// "ai_scheduler/internal/tools"
|
||||||
"ai_scheduler/utils"
|
// "ai_scheduler/utils"
|
||||||
"encoding/json"
|
// "encoding/json"
|
||||||
"flag"
|
// "flag"
|
||||||
"fmt"
|
// "fmt"
|
||||||
"os"
|
// "os"
|
||||||
"path/filepath"
|
// "path/filepath"
|
||||||
"testing"
|
// "testing"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2/log"
|
// "github.com/gofiber/fiber/v2/log"
|
||||||
)
|
// )
|
||||||
|
|
||||||
func Test_task(t *testing.T) {
|
// func Test_task(t *testing.T) {
|
||||||
var c entitys.TaskConfig
|
// var c entitys.TaskConfig
|
||||||
config := `{"param": {"type": "object", "required": ["number"], "properties": {"number": {"type": "string", "description": "订单编号/流水号"}}}, "request": {"url": "http://www.baidu.com/${number}", "headers": {"Authorization": "${authorization}"}, "method": "GET"}}`
|
// config := `{"param": {"type": "object", "required": ["number"], "properties": {"number": {"type": "string", "description": "订单编号/流水号"}}}, "request": {"url": "http://www.baidu.com/${number}", "headers": {"Authorization": "${authorization}"}, "method": "GET"}}`
|
||||||
err := json.Unmarshal([]byte(config), &c)
|
// err := json.Unmarshal([]byte(config), &c)
|
||||||
t.Log(err)
|
// t.Log(err)
|
||||||
}
|
// }
|
||||||
|
|
||||||
type configData struct {
|
// type configData struct {
|
||||||
Param map[string]interface{} `json:"param"`
|
// Param map[string]interface{} `json:"param"`
|
||||||
Do map[string]interface{} `json:"do"`
|
// Do map[string]interface{} `json:"do"`
|
||||||
}
|
// }
|
||||||
|
|
||||||
func Test_Order(t *testing.T) {
|
// func Test_Order(t *testing.T) {
|
||||||
routerBiz := in()
|
// routerBiz := in()
|
||||||
ch := make(chan entitys.Response, 5)
|
// ch := make(chan entitys.Response, 5)
|
||||||
defer close(ch)
|
// defer close(ch)
|
||||||
err := routerBiz.handleTask(ch, nil, &entitys.Match{Index: "order_diagnosis", Parameters: `{"order_number":"822895927188791297"}`}, &model.AiTask{Config: `{"tool": "zltxOrderDetail", "param": {"type": "object", "optional": [], "required": ["order_number"], "properties": {"order_number": {"type": "string", "description": "订单编号/流水号"}}}}`})
|
// err := routerBiz.handleTask(ch, nil, &entitys.Match{Index: "order_diagnosis", Parameters: `{"order_number":"822895927188791297"}`}, &model.AiTask{Config: `{"tool": "zltxOrderDetail", "param": {"type": "object", "optional": [], "required": ["order_number"], "properties": {"order_number": {"type": "string", "description": "订单编号/流水号"}}}}`})
|
||||||
select {
|
// select {
|
||||||
case v := <-ch: // 尝试接收
|
// case v := <-ch: // 尝试接收
|
||||||
fmt.Println("接收到值:", v)
|
// fmt.Println("接收到值:", v)
|
||||||
default:
|
// default:
|
||||||
fmt.Println("无数据可接收")
|
// fmt.Println("无数据可接收")
|
||||||
}
|
// }
|
||||||
t.Log(err)
|
// t.Log(err)
|
||||||
}
|
// }
|
||||||
|
|
||||||
func Test_OrderLog(t *testing.T) {
|
// func Test_OrderLog(t *testing.T) {
|
||||||
routerBiz := in()
|
// routerBiz := in()
|
||||||
ch := make(chan entitys.Response, 5)
|
// ch := make(chan entitys.Response, 5)
|
||||||
defer close(ch)
|
// defer close(ch)
|
||||||
err := routerBiz.handleTask(ch, nil, &entitys.Match{Index: "order_diagnosis", Parameters: `{"order_number":"822979421673758721","serial_number":"822979421979938817"}`}, &model.AiTask{Config: `{"tool": "zltxOrderDirectLog", "param": {"type": "object", "optional": [], "required": ["order_number"], "properties": {"order_number": {"type": "string", "description": "订单编号/流水号"}}}}`})
|
// err := routerBiz.handleTask(ch, nil, &entitys.Match{Index: "order_diagnosis", Parameters: `{"order_number":"822979421673758721","serial_number":"822979421979938817"}`}, &model.AiTask{Config: `{"tool": "zltxOrderDirectLog", "param": {"type": "object", "optional": [], "required": ["order_number"], "properties": {"order_number": {"type": "string", "description": "订单编号/流水号"}}}}`})
|
||||||
t.Log(err)
|
// t.Log(err)
|
||||||
}
|
// }
|
||||||
|
|
||||||
func Test_ProductLog(t *testing.T) {
|
// func Test_ProductLog(t *testing.T) {
|
||||||
routerBiz := in()
|
// routerBiz := in()
|
||||||
ch := make(chan entitys.Response, 5)
|
// ch := make(chan entitys.Response, 5)
|
||||||
defer close(ch)
|
// defer close(ch)
|
||||||
err := routerBiz.handleTask(ch, nil, &entitys.Match{Index: "order_diagnosis", Parameters: `{"name":"利楚测试"}`}, &model.AiTask{Config: `{"tool": "zltxProduct", "param": {"type": "object", "optional": [], "required": ["order_number"], "properties": {"order_number": {"type": "string", "description": "订单编号/流水号"}}}}`})
|
// err := routerBiz.handleTask(ch, nil, &entitys.Match{Index: "order_diagnosis", Parameters: `{"name":"利楚测试"}`}, &model.AiTask{Config: `{"tool": "zltxProduct", "param": {"type": "object", "optional": [], "required": ["order_number"], "properties": {"order_number": {"type": "string", "description": "订单编号/流水号"}}}}`})
|
||||||
t.Log(err)
|
// t.Log(err)
|
||||||
}
|
// }
|
||||||
|
|
||||||
func Test_ZltxStatistics(t *testing.T) {
|
// func Test_ZltxStatistics(t *testing.T) {
|
||||||
routerBiz := in()
|
// routerBiz := in()
|
||||||
ch := make(chan entitys.Response, 5)
|
// ch := make(chan entitys.Response, 5)
|
||||||
defer close(ch)
|
// defer close(ch)
|
||||||
err := routerBiz.handleTask(ch, nil, &entitys.Match{Index: "order_diagnosis", Parameters: `{"number":"13737882067"}`}, &model.AiTask{Config: `{"tool": "zltxOrderStatistics", "param": {"type": "object", "optional": [], "required": ["number"], "properties": {"number": {"type": "string", "description": "充值账号/分销商ID"}}}}`})
|
// err := routerBiz.handleTask(ch, nil, &entitys.Match{Index: "order_diagnosis", Parameters: `{"number":"13737882067"}`}, &model.AiTask{Config: `{"tool": "zltxOrderStatistics", "param": {"type": "object", "optional": [], "required": ["number"], "properties": {"number": {"type": "string", "description": "充值账号/分销商ID"}}}}`})
|
||||||
t.Log(err)
|
// t.Log(err)
|
||||||
}
|
// }
|
||||||
|
|
||||||
func in() *AiRouterBiz {
|
// func in() *AiRouterBiz {
|
||||||
|
|
||||||
modDir, err := getModuleDir()
|
// modDir, err := getModuleDir()
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
panic("1")
|
// panic("1")
|
||||||
}
|
// }
|
||||||
configPath := flag.String("config", fmt.Sprintf("%s/config/config.yaml", modDir), "Path to configuration file")
|
// configPath := flag.String("config", fmt.Sprintf("%s/config/config.yaml", modDir), "Path to configuration file")
|
||||||
flag.Parse()
|
// flag.Parse()
|
||||||
|
|
||||||
configConfig, err := config.LoadConfig(*configPath)
|
// configConfig, err := config.LoadConfig(*configPath)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
panic("加载配置失败")
|
// panic("加载配置失败")
|
||||||
}
|
// }
|
||||||
client, _, err := utils_ollama.NewClient(configConfig)
|
// client, _, err := utils_ollama.NewClient(configConfig)
|
||||||
allLogger := log.DefaultLogger()
|
// allLogger := log.DefaultLogger()
|
||||||
utilOllama := utils_ollama.NewUtilOllama(configConfig, allLogger)
|
// utilOllama := utils_ollama.NewUtilOllama(configConfig, allLogger)
|
||||||
manager := tools.NewManager(configConfig, client)
|
// manager := tools.NewManager(configConfig, client)
|
||||||
|
|
||||||
db, _ := utils.NewGormDb(configConfig)
|
// db, _ := utils.NewGormDb(configConfig)
|
||||||
sessionImpl := impl.NewSessionImpl(db)
|
// sessionImpl := impl.NewSessionImpl(db)
|
||||||
sysImpl := impl.NewSysImpl(db)
|
// sysImpl := impl.NewSysImpl(db)
|
||||||
taskImpl := impl.NewTaskImpl(db)
|
// taskImpl := impl.NewTaskImpl(db)
|
||||||
chatImpl := impl.NewChatImpl(db)
|
// chatImpl := impl.NewChatImpl(db)
|
||||||
safeChannelPool, _ := pkg.NewSafeChannelPool(configConfig)
|
// safeChannelPool, _ := pkg.NewSafeChannelPool(configConfig)
|
||||||
routerBiz := NewAiRouterBiz(manager, sessionImpl, sysImpl, taskImpl, chatImpl, configConfig, utilOllama, safeChannelPool, client)
|
// routerBiz := NewAiRouterBiz(manager, sessionImpl, sysImpl, taskImpl, chatImpl, configConfig, utilOllama, safeChannelPool, client)
|
||||||
|
|
||||||
return routerBiz
|
// return routerBiz
|
||||||
}
|
// }
|
||||||
|
|
||||||
func getModuleDir() (string, error) {
|
// func getModuleDir() (string, error) {
|
||||||
dir, err := os.Getwd()
|
// dir, err := os.Getwd()
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return "", err
|
// return "", err
|
||||||
}
|
// }
|
||||||
|
|
||||||
for {
|
// for {
|
||||||
modPath := filepath.Join(dir, "go.mod")
|
// modPath := filepath.Join(dir, "go.mod")
|
||||||
if _, err := os.Stat(modPath); err == nil {
|
// if _, err := os.Stat(modPath); err == nil {
|
||||||
return dir, nil // 找到 go.mod
|
// return dir, nil // 找到 go.mod
|
||||||
}
|
// }
|
||||||
|
|
||||||
// 向上查找父目录
|
// // 向上查找父目录
|
||||||
parent := filepath.Dir(dir)
|
// parent := filepath.Dir(dir)
|
||||||
if parent == dir {
|
// if parent == dir {
|
||||||
break // 到达根目录,未找到
|
// break // 到达根目录,未找到
|
||||||
}
|
// }
|
||||||
dir = parent
|
// dir = parent
|
||||||
}
|
// }
|
||||||
|
|
||||||
return "", fmt.Errorf("go.mod not found in current directory or parents")
|
// return "", fmt.Errorf("go.mod not found in current directory or parents")
|
||||||
}
|
// }
|
||||||
|
|
|
||||||
|
|
@ -80,11 +80,11 @@ func (s *SessionBiz) SessionInit(ctx context.Context, req *entitys.SessionInitRe
|
||||||
result.Prologue = sysConfig.Prologue
|
result.Prologue = sysConfig.Prologue
|
||||||
|
|
||||||
// 开场白写入会话历史
|
// 开场白写入会话历史
|
||||||
s.chatRepo.AsyncCreate(ctx, model.AiChatHi{
|
//s.chatRepo.AsyncCreate(ctx, model.AiChatHi{
|
||||||
SessionID: chat.SessionID,
|
// SessionID: chat.SessionID,
|
||||||
Role: chat.Role.String(),
|
// Role: chat.Role.String(),
|
||||||
Content: chat.Content,
|
// Content: chat.Content,
|
||||||
})
|
//})
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
result.SessionId = session.SessionID
|
result.SessionId = session.SessionID
|
||||||
|
|
@ -102,12 +102,20 @@ func (s *SessionBiz) SessionInit(ctx context.Context, req *entitys.SessionInitRe
|
||||||
|
|
||||||
// 转换为 entitys.ChatHistory 类型
|
// 转换为 entitys.ChatHistory 类型
|
||||||
for _, chat := range chatList {
|
for _, chat := range chatList {
|
||||||
result.Chat = append(result.Chat, entitys.ChatHistory{
|
result.Chat = append(result.Chat, []entitys.ChatHistory{
|
||||||
SessionID: chat.SessionID,
|
{
|
||||||
Role: constants.Caller(chat.Role),
|
SessionID: chat.SessionID,
|
||||||
Content: chat.Content,
|
Role: constants.RoleUser,
|
||||||
Prologue: sysConfig.Prologue,
|
Content: chat.Ques,
|
||||||
})
|
Prologue: sysConfig.Prologue,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
SessionID: chat.SessionID,
|
||||||
|
Role: constants.RoleAssistant,
|
||||||
|
Content: chat.Ans,
|
||||||
|
Prologue: sysConfig.Prologue,
|
||||||
|
},
|
||||||
|
}...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,18 @@
|
||||||
package biz
|
package biz
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"ai_scheduler/internal/data/constants"
|
||||||
|
errors "ai_scheduler/internal/data/error"
|
||||||
"ai_scheduler/internal/data/impl"
|
"ai_scheduler/internal/data/impl"
|
||||||
"ai_scheduler/internal/data/model"
|
"ai_scheduler/internal/data/model"
|
||||||
"ai_scheduler/internal/entitys"
|
"ai_scheduler/internal/entitys"
|
||||||
|
"ai_scheduler/internal/pkg/l_request"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"gorm.io/gorm/utils"
|
||||||
|
|
||||||
"xorm.io/builder"
|
"xorm.io/builder"
|
||||||
|
|
||||||
|
|
@ -24,12 +32,63 @@ func NewTaskBiz(conf *config.Config, taskRepo *impl.TaskImpl) *TaskBiz {
|
||||||
}
|
}
|
||||||
|
|
||||||
// taskList 功能列表
|
// taskList 功能列表
|
||||||
func (t *TaskBiz) TaskList(ctx context.Context, req *entitys.TaskRequest) (list []model.AiTask, err error) {
|
func (t *TaskBiz) TaskList(ctx context.Context, req *entitys.TaskRequest, auth string) (list []model.AiTask, err error) {
|
||||||
|
tasks := make([]model.AiTask, 0)
|
||||||
cond := builder.NewCond()
|
cond := builder.NewCond()
|
||||||
cond = cond.And(builder.Eq{"status": 1})
|
cond = cond.And(builder.Eq{"status": constants.Enable})
|
||||||
|
|
||||||
cond = cond.And(builder.Eq{"sys_id": req.SysId})
|
cond = cond.And(builder.Eq{"sys_id": req.SysId})
|
||||||
err = t.taskRepo.GetRangeToMapStruct(&cond, &list)
|
cond = cond.And(builder.Eq{"is_show": constants.IsSHOW})
|
||||||
|
err = t.taskRepo.GetRangeToMapStruct(&cond, &tasks)
|
||||||
|
|
||||||
|
codes, err := t.GetUserPermission(req, auth)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查用户是否有权限
|
||||||
|
for _, task := range tasks {
|
||||||
|
if utils.Contains(codes, task.Index) {
|
||||||
|
list = append(list, task)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 从统一登录平台获取用户权限
|
||||||
|
func (t *TaskBiz) GetUserPermission(req *entitys.TaskRequest, auth string) (codes []string, err error) {
|
||||||
|
|
||||||
|
request := l_request.Request{
|
||||||
|
Method: "GET",
|
||||||
|
Url: t.conf.PermissionConfig.PermissionURL + strconv.Itoa(int(req.SysId)),
|
||||||
|
Headers: map[string]string{
|
||||||
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
|
||||||
|
"Accept": "application/json, text/plain, */*",
|
||||||
|
"Authorization": auth,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
// 发送请求
|
||||||
|
res, err := request.Send()
|
||||||
|
if err != nil {
|
||||||
|
err = errors.SysErr("请求用户权限失败")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查响应状态码
|
||||||
|
if res.StatusCode != http.StatusOK {
|
||||||
|
err = errors.SysErr("获取用户权限失败")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
type resp struct {
|
||||||
|
Codes []string `json:"codes"`
|
||||||
|
}
|
||||||
|
// 解析响应体s
|
||||||
|
var respBody resp
|
||||||
|
err = json.Unmarshal([]byte(res.Text), &respBody)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return respBody.Codes, nil
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,14 +9,15 @@ import (
|
||||||
|
|
||||||
// Config 应用配置
|
// Config 应用配置
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Server ServerConfig `mapstructure:"server"`
|
Server ServerConfig `mapstructure:"server"`
|
||||||
Ollama OllamaConfig `mapstructure:"ollama"`
|
Ollama OllamaConfig `mapstructure:"ollama"`
|
||||||
Sys SysConfig `mapstructure:"sys"`
|
Sys SysConfig `mapstructure:"sys"`
|
||||||
Tools ToolsConfig `mapstructure:"tools"`
|
Tools ToolsConfig `mapstructure:"tools"`
|
||||||
Logging LoggingConfig `mapstructure:"logging"`
|
Logging LoggingConfig `mapstructure:"logging"`
|
||||||
Redis Redis `mapstructure:"redis"`
|
Redis Redis `mapstructure:"redis"`
|
||||||
DB DB `mapstructure:"db"`
|
DB DB `mapstructure:"db"`
|
||||||
DefaultPrompt SysPrompt `mapstructure:"default_prompt"`
|
DefaultPrompt SysPrompt `mapstructure:"default_prompt"`
|
||||||
|
PermissionConfig PermissionConfig `mapstructure:"permissionConfig"`
|
||||||
// LLM *LLM `mapstructure:"llm"`
|
// LLM *LLM `mapstructure:"llm"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -89,10 +90,12 @@ type ToolsConfig struct {
|
||||||
//通过账号获取订单统计信息
|
//通过账号获取订单统计信息
|
||||||
ZltxOrderStatistics ToolConfig `mapstructure:"zltxOrderStatistics"`
|
ZltxOrderStatistics ToolConfig `mapstructure:"zltxOrderStatistics"`
|
||||||
DingTalkBot ToolConfig `mapstructure:"dingTalkBot"`
|
DingTalkBot ToolConfig `mapstructure:"dingTalkBot"`
|
||||||
//上游售后订单流水详情
|
// 上游订单售后
|
||||||
ZltxOrderAfterSaleDetail ToolConfig `mapstructure:"zltxOrderAfterSaleDetail"`
|
ZltxOrderAfterSaleSupplier ToolConfig `mapstructure:"zltxOrderAfterSaleSupplier"`
|
||||||
//下游订单预检
|
// 下游订单售后
|
||||||
ZltxOrderAfterSalePreCheck ToolConfig `mapstructure:"zltxOrderAfterSalePreCheck"`
|
ZltxOrderAfterSaleReseller ToolConfig `mapstructure:"zltxOrderAfterSaleReseller"`
|
||||||
|
// 下游批充订单售后
|
||||||
|
ZltxOrderAfterSaleResellerBatch ToolConfig `mapstructure:"zltxOrderAfterSaleResellerBatch"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToolConfig 单个工具配置
|
// ToolConfig 单个工具配置
|
||||||
|
|
@ -111,6 +114,12 @@ type LoggingConfig struct {
|
||||||
Format string `mapstructure:"format"`
|
Format string `mapstructure:"format"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PermissionConfig 权限校验配置
|
||||||
|
type PermissionConfig struct {
|
||||||
|
// 获取权限的地址
|
||||||
|
PermissionURL string `mapstructure:"permission_url"`
|
||||||
|
}
|
||||||
|
|
||||||
// LoadConfig 加载配置
|
// LoadConfig 加载配置
|
||||||
func LoadConfig(configPath string) (*Config, error) {
|
func LoadConfig(configPath string) (*Config, error) {
|
||||||
viper.SetConfigFile(configPath)
|
viper.SetConfigFile(configPath)
|
||||||
|
|
|
||||||
|
|
@ -11,8 +11,22 @@ const (
|
||||||
type TaskType int32
|
type TaskType int32
|
||||||
|
|
||||||
const (
|
const (
|
||||||
TaskTypeApi = 1
|
TaskTypeApi TaskType = 1
|
||||||
TaskTypeKnowle = 2
|
TaskTypeKnowle TaskType = 2
|
||||||
TaskTypeFunc = 3
|
TaskTypeFunc TaskType = 3
|
||||||
TaskTypeBot = 4
|
TaskTypeBot TaskType = 4
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type UseFul int32
|
||||||
|
|
||||||
|
const (
|
||||||
|
UseFulNotSolve UseFul = 2
|
||||||
|
UseFulNotUnclear UseFul = 3
|
||||||
|
UseFulNotError UseFul = 4
|
||||||
|
)
|
||||||
|
|
||||||
|
var UseFulMap = map[UseFul]string{
|
||||||
|
UseFulNotSolve: "未解决问题",
|
||||||
|
UseFulNotUnclear: "回答不明确",
|
||||||
|
UseFulNotError: "理解错误",
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
package constants
|
||||||
|
|
||||||
|
const (
|
||||||
|
IsSHOW = 1
|
||||||
|
NotShow = 2
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
Enable = 1
|
||||||
|
Disable = 2
|
||||||
|
)
|
||||||
|
|
@ -13,6 +13,7 @@ var (
|
||||||
AuthNotFound = &BusinessErr{code: 408, message: "身份验证失败"}
|
AuthNotFound = &BusinessErr{code: 408, message: "身份验证失败"}
|
||||||
KeyNotFound = &BusinessErr{code: 409, message: "身份验证失败"}
|
KeyNotFound = &BusinessErr{code: 409, message: "身份验证失败"}
|
||||||
SysNotFound = &BusinessErr{code: 410, message: "未找到系统信息"}
|
SysNotFound = &BusinessErr{code: 410, message: "未找到系统信息"}
|
||||||
|
SysCodeNotFound = &BusinessErr{code: 411, message: "未找到系统编码"}
|
||||||
InvalidParam = &BusinessErr{code: InvalidParamCode, message: "无效参数"}
|
InvalidParam = &BusinessErr{code: InvalidParamCode, message: "无效参数"}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,11 +12,14 @@ const TableNameAiChatHi = "ai_chat_his"
|
||||||
|
|
||||||
// AiChatHi mapped from table <ai_chat_his>
|
// AiChatHi mapped from table <ai_chat_his>
|
||||||
type AiChatHi struct {
|
type AiChatHi struct {
|
||||||
HisID int64 `gorm:"column:his_id;primaryKey" json:"his_id"`
|
HisID int64 `gorm:"column:his_id;primaryKey;autoIncrement:true" json:"his_id"`
|
||||||
SessionID string `gorm:"column:session_id;not null" json:"session_id"`
|
SessionID string `gorm:"column:session_id;not null" json:"session_id"`
|
||||||
Role string `gorm:"column:role;not null;comment:system系统输出,assistant助手输出,user用户输入" json:"role"` // system系统输出,assistant助手输出,user用户输入
|
Ques string `gorm:"column:ques;not null" json:"ques"`
|
||||||
Content string `gorm:"column:content;not null" json:"content"`
|
Ans string `gorm:"column:ans;not null" json:"ans"`
|
||||||
CreateAt *time.Time `gorm:"column:create_at;default:CURRENT_TIMESTAMP" json:"create_at"`
|
Files string `gorm:"column:files;not null" json:"files"`
|
||||||
|
Useful int32 `gorm:"column:useful;not null;comment:0不评价,1有用,其他为无用" json:"useful"` // 0不评价,1有用,其他为无用
|
||||||
|
CreateAt time.Time `gorm:"column:create_at;default:CURRENT_TIMESTAMP" json:"create_at"`
|
||||||
|
UpdatedAt time.Time `gorm:"column:updated_at;default:CURRENT_TIMESTAMP" json:"updated_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// TableName AiChatHi's table name
|
// TableName AiChatHi's table name
|
||||||
|
|
|
||||||
|
|
@ -12,17 +12,21 @@ const TableNameAiTask = "ai_task"
|
||||||
|
|
||||||
// AiTask mapped from table <ai_task>
|
// AiTask mapped from table <ai_task>
|
||||||
type AiTask struct {
|
type AiTask struct {
|
||||||
TaskID int32 `gorm:"column:task_id;primaryKey" json:"task_id"`
|
TaskID int32 `gorm:"column:task_id;primaryKey;autoIncrement:true" json:"task_id"`
|
||||||
SysID int32 `gorm:"column:sys_id;not null" json:"sys_id"`
|
SysID int32 `gorm:"column:sys_id;not null" json:"sys_id"`
|
||||||
Name string `gorm:"column:name;not null" json:"name"`
|
Name string `gorm:"column:name;not null" json:"name"`
|
||||||
Index string `gorm:"column:index;not null" json:"index"`
|
Index string `gorm:"column:index;not null" json:"index"`
|
||||||
Desc string `gorm:"column:desc;not null" json:"desc"`
|
Desc string `gorm:"column:desc;not null" json:"desc"`
|
||||||
Type int32 `gorm:"column:type;not null;comment:类型,1:api,2:知识库" json:"type"` // 类型,1:api,2:知识库
|
UseCase string `gorm:"column:use_case;not null;comment:适用场景" json:"use_case"` // 适用场景
|
||||||
Config string `gorm:"column:config" json:"config"`
|
TempPrompt string `gorm:"column:temp_prompt;not null;comment:提示词模板" json:"temp_prompt"` // 提示词模板
|
||||||
CreateAt time.Time `gorm:"column:create_at;default:CURRENT_TIMESTAMP" json:"create_at"`
|
Type int32 `gorm:"column:type;not null;default:1;comment:类型,1:api,2:知识库" json:"type"` // 类型,1:api,2:知识库
|
||||||
UpdateAt time.Time `gorm:"column:update_at;default:CURRENT_TIMESTAMP" json:"update_at"`
|
Config string `gorm:"column:config" json:"config"`
|
||||||
Status int32 `gorm:"column:status;not null;default:1" json:"status"`
|
TagType int32 `gorm:"column:tag_type;comment:标签类型:1.AI日常 2.AI查询 3.AI执行" json:"tag_type"` // 标签类型:1.AI日常 2.AI查询 3.AI执行
|
||||||
DeleteAt time.Time `gorm:"column:delete_at" json:"delete_at"`
|
CreateAt time.Time `gorm:"column:create_at;default:CURRENT_TIMESTAMP" json:"create_at"`
|
||||||
|
UpdatedAt time.Time `gorm:"column:updated_at;default:CURRENT_TIMESTAMP" json:"updated_at"`
|
||||||
|
IsShow int32 `gorm:"column:is_show;not null;default:1;comment:是否展示,1为展示,2为不展示" json:"is_show"` // 是否展示,1为展示,2为不展示
|
||||||
|
Status int32 `gorm:"column:status;not null;default:1" json:"status"`
|
||||||
|
DeleteAt time.Time `gorm:"column:delete_at" json:"delete_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// TableName AiTask's table name
|
// TableName AiTask's table name
|
||||||
|
|
|
||||||
|
|
@ -10,3 +10,7 @@ type ChatHistory struct {
|
||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
Prologue string `json:"prologue"`
|
Prologue string `json:"prologue"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ChatHisLog struct {
|
||||||
|
HisId int64 `json:"his_id"`
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ package entitys
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
"github.com/gofiber/websocket/v2"
|
"github.com/gofiber/websocket/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -68,6 +67,13 @@ func ResLoading(ch chan Response, index string, content string) {
|
||||||
Type: ResponseLoading,
|
Type: ResponseLoading,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func ResError(ch chan Response, index string, content string) {
|
||||||
|
ch <- Response{
|
||||||
|
Index: index,
|
||||||
|
Content: content,
|
||||||
|
Type: ResponseErr,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type ResponseData struct {
|
type ResponseData struct {
|
||||||
Done bool
|
Done bool
|
||||||
|
|
|
||||||
|
|
@ -23,3 +23,8 @@ type SessionListRequest struct {
|
||||||
type TaskRequest struct {
|
type TaskRequest struct {
|
||||||
SysId int32 `json:"sys_id"`
|
SysId int32 `json:"sys_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UseFulRequest struct {
|
||||||
|
HisId int64 `json:"his_id"`
|
||||||
|
Useful int32 `json:"useful"`
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package entitys
|
package entitys
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"ai_scheduler/internal/data/constants"
|
||||||
"ai_scheduler/internal/data/model"
|
"ai_scheduler/internal/data/model"
|
||||||
|
|
||||||
"context"
|
"context"
|
||||||
|
|
@ -31,7 +32,9 @@ type FirstSockRequest struct {
|
||||||
type ChatSockRequest struct {
|
type ChatSockRequest struct {
|
||||||
Text string `json:"text" binding:"required"`
|
Text string `json:"text" binding:"required"`
|
||||||
Img string `json:"img" binding:"required"`
|
Img string `json:"img" binding:"required"`
|
||||||
|
File string `json:"file" binding:"required"`
|
||||||
Tags string `json:"tags" binding:"required"`
|
Tags string `json:"tags" binding:"required"`
|
||||||
|
MarkHis int64 `json:"mark_his" `
|
||||||
Caller string `json:"caller" binding:"required"`
|
Caller string `json:"caller" binding:"required"`
|
||||||
SessionID string `json:"session_id"`
|
SessionID string `json:"session_id"`
|
||||||
}
|
}
|
||||||
|
|
@ -130,9 +133,9 @@ type ChatHis struct {
|
||||||
Context HisContext `json:"context"`
|
Context HisContext `json:"context"`
|
||||||
}
|
}
|
||||||
type HisMessage struct {
|
type HisMessage struct {
|
||||||
Role string `json:"role"`
|
Role constants.Caller `json:"role"`
|
||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
Timestamp string `json:"timestamp"`
|
Timestamp string `json:"timestamp"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type HisContext struct {
|
type HisContext struct {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,138 @@
|
||||||
|
package gateway
|
||||||
|
|
||||||
|
import (
|
||||||
|
errors "ai_scheduler/internal/data/error"
|
||||||
|
"ai_scheduler/internal/data/model"
|
||||||
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
|
"github.com/gofiber/websocket/v2"
|
||||||
|
"math/rand"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrConnClosed = errors.SysErr("连接不存在或已关闭")
|
||||||
|
)
|
||||||
|
|
||||||
|
type Client struct {
|
||||||
|
id string // 客户端唯一ID
|
||||||
|
conn *websocket.Conn // WebSocket 连接
|
||||||
|
session string // 会话ID
|
||||||
|
key string // 应用密钥
|
||||||
|
auth string // 用户凭证token
|
||||||
|
codes []string // 用户权限code
|
||||||
|
sysInfo *model.AiSy // 系统信息
|
||||||
|
tasks []model.AiTask // 任务列表
|
||||||
|
sysCode string // 系统编码
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewClient(conn *websocket.Conn) *Client {
|
||||||
|
return &Client{
|
||||||
|
id: generateClientID(),
|
||||||
|
conn: conn,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetID 获取客户端的唯一ID
|
||||||
|
func (c *Client) GetID() string {
|
||||||
|
return c.id
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetConn 获取客户端的 WebSocket 连接
|
||||||
|
func (c *Client) GetConn() *websocket.Conn {
|
||||||
|
return c.conn
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetSession 获取会话ID
|
||||||
|
func (c *Client) GetSession() string {
|
||||||
|
return c.session
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetKey 获取应用密钥
|
||||||
|
func (c *Client) GetKey() string {
|
||||||
|
return c.key
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAuth 获取用户凭证token
|
||||||
|
func (c *Client) GetAuth() string {
|
||||||
|
return c.auth
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCodes 获取用户权限code
|
||||||
|
func (c *Client) GetCodes() []string {
|
||||||
|
return c.codes
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetSysCode 获取系统编码
|
||||||
|
func (c *Client) GetSysCode() string {
|
||||||
|
return c.sysCode
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetSysInfo 获取系统信息
|
||||||
|
func (c *Client) GetSysInfo() *model.AiSy {
|
||||||
|
return c.sysInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetSysInfo 设置系统信息
|
||||||
|
func (c *Client) SetSysInfo(sysInfo *model.AiSy) {
|
||||||
|
c.sysInfo = sysInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetTasks 获取任务列表
|
||||||
|
func (c *Client) GetTasks() []model.AiTask {
|
||||||
|
return c.tasks
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetTasks 设置任务列表
|
||||||
|
func (c *Client) SetTasks(tasks []model.AiTask) {
|
||||||
|
c.tasks = tasks
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置用户权限code
|
||||||
|
func (c *Client) SetCodes(codes []string) {
|
||||||
|
c.codes = codes
|
||||||
|
}
|
||||||
|
|
||||||
|
// SendFunc 发送消息到客户端
|
||||||
|
func (c *Client) SendFunc(msg []byte) error {
|
||||||
|
if c.conn != nil {
|
||||||
|
return c.conn.WriteMessage(websocket.TextMessage, msg)
|
||||||
|
}
|
||||||
|
return ErrConnClosed
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成唯一的客户端ID
|
||||||
|
func generateClientID() string {
|
||||||
|
// 使用时间戳+随机数确保唯一性
|
||||||
|
timestamp := time.Now().UnixNano()
|
||||||
|
randomBytes := make([]byte, 4)
|
||||||
|
rand.Read(randomBytes)
|
||||||
|
randomStr := hex.EncodeToString(randomBytes)
|
||||||
|
return fmt.Sprintf("%d%s", timestamp, randomStr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 连接数据验证和收集
|
||||||
|
func (c *Client) DataAuth() (err error) {
|
||||||
|
c.session = c.conn.Query("x-session", "")
|
||||||
|
if len(c.session) == 0 {
|
||||||
|
err = errors.SessionNotFound
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.auth = c.conn.Query("x-authorization", "")
|
||||||
|
if len(c.auth) == 0 {
|
||||||
|
err = errors.AuthNotFound
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.key = c.conn.Query("x-app-key", "")
|
||||||
|
if len(c.key) == 0 {
|
||||||
|
err = errors.KeyNotFound
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 系统编码
|
||||||
|
c.sysCode = c.conn.Query("x-sys-code", "")
|
||||||
|
if len(c.sysCode) == 0 {
|
||||||
|
err = errors.SysCodeNotFound
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
@ -5,11 +5,6 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Client struct {
|
|
||||||
ID string
|
|
||||||
SendFunc func(data []byte) error
|
|
||||||
}
|
|
||||||
|
|
||||||
type Gateway struct {
|
type Gateway struct {
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
clients map[string]*Client // clientID -> Client
|
clients map[string]*Client // clientID -> Client
|
||||||
|
|
@ -26,7 +21,7 @@ func NewGateway() *Gateway {
|
||||||
func (g *Gateway) AddClient(c *Client) {
|
func (g *Gateway) AddClient(c *Client) {
|
||||||
g.mu.Lock()
|
g.mu.Lock()
|
||||||
defer g.mu.Unlock()
|
defer g.mu.Unlock()
|
||||||
g.clients[c.ID] = c
|
g.clients[c.GetID()] = c
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Gateway) RemoveClient(clientID string) {
|
func (g *Gateway) RemoveClient(clientID string) {
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -19,3 +20,15 @@ func EscapeJSONString(s string) string {
|
||||||
b, _ := json.Marshal(s)
|
b, _ := json.Marshal(s)
|
||||||
return string(b[1 : len(b)-1])
|
return string(b[1 : len(b)-1])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// string 转 int
|
||||||
|
func StringToInt(s string) int {
|
||||||
|
i, _ := strconv.Atoi(s)
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
|
||||||
|
// string 转 float64
|
||||||
|
func StringToFloat64(s string) float64 {
|
||||||
|
i, _ := strconv.ParseFloat(s, 64)
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -50,9 +50,13 @@ func SetupRoutes(app *fiber.App, ChatService *services.ChatService, sessionServi
|
||||||
|
|
||||||
r.Post("/session/init", sessionService.SessionInit) // 会话初始化,不存在则创建,存在则返回会话ID和默认条数会话历史
|
r.Post("/session/init", sessionService.SessionInit) // 会话初始化,不存在则创建,存在则返回会话ID和默认条数会话历史
|
||||||
r.Post("/session/list", sessionService.SessionList)
|
r.Post("/session/list", sessionService.SessionList)
|
||||||
|
|
||||||
r.Post("/sys/tasks", task.Tasks)
|
r.Post("/sys/tasks", task.Tasks)
|
||||||
// 回调
|
// 评价
|
||||||
r.Post("/callback", callbackService.Callback)
|
r.Post("/chat/useful/list", ChatService.UsefulList)
|
||||||
|
r.Post("/chat/useful", ChatService.Useful)
|
||||||
|
// 回调
|
||||||
|
r.Post("/callback", callbackService.Callback)
|
||||||
//广播
|
//广播
|
||||||
r.Get("/broadcast", func(ctx *fiber.Ctx) error {
|
r.Get("/broadcast", func(ctx *fiber.Ctx) error {
|
||||||
action := ctx.Query("action")
|
action := ctx.Query("action")
|
||||||
|
|
|
||||||
|
|
@ -48,8 +48,9 @@ type Envelope struct {
|
||||||
|
|
||||||
// bug_optimization_submit 工单回调
|
// bug_optimization_submit 工单回调
|
||||||
const (
|
const (
|
||||||
ActionBugOptimizationSubmitDone = "bug_optimization_submit_done" // 工单完成回调
|
ActionBugOptimizationSubmitProcess = "bug_optimization_submit_process" // 工单过程回调
|
||||||
ActionBugOptimizationSubmitUpdate = "bug_optimization_submit_update" // 工单更新回调
|
ActionBugOptimizationSubmitDone = "bug_optimization_submit_done" // 工单完成回调
|
||||||
|
ActionBugOptimizationSubmitUpdate = "bug_optimization_submit_update" // 工单更新回调
|
||||||
)
|
)
|
||||||
|
|
||||||
// BugOptimizationSubmitDoneData 工单完成回调数据
|
// BugOptimizationSubmitDoneData 工单完成回调数据
|
||||||
|
|
@ -167,6 +168,18 @@ func (s *CallbackService) handleDingTalkCallback(c *fiber.Ctx, env Envelope) err
|
||||||
// 删除映射
|
// 删除映射
|
||||||
s.botTool.DelTaskMapping(env.TaskID)
|
s.botTool.DelTaskMapping(env.TaskID)
|
||||||
|
|
||||||
|
return c.JSON(fiber.Map{"code": 0, "message": "ok"})
|
||||||
|
case ActionBugOptimizationSubmitProcess:
|
||||||
|
type processData struct {
|
||||||
|
Process string `json:"process"`
|
||||||
|
}
|
||||||
|
var data processData
|
||||||
|
if err := json.Unmarshal(env.Data, &data); err != nil {
|
||||||
|
return errorcode.ParamErr("invalid json: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
s.sendStreamLoading(sessionID, data.Process)
|
||||||
|
|
||||||
return c.JSON(fiber.Map{"code": 0, "message": "ok"})
|
return c.JSON(fiber.Map{"code": 0, "message": "ok"})
|
||||||
default:
|
default:
|
||||||
return errorcode.ParamErr("unknown action: %s", env.Action)
|
return errorcode.ParamErr("unknown action: %s", env.Action)
|
||||||
|
|
@ -194,6 +207,10 @@ func (s *CallbackService) getDingtalkReceivers(ctx context.Context, receiverIds
|
||||||
|
|
||||||
// sendStreamLog 发送流式日志
|
// sendStreamLog 发送流式日志
|
||||||
func (s *CallbackService) sendStreamLog(sessionID string, content string) {
|
func (s *CallbackService) sendStreamLog(sessionID string, content string) {
|
||||||
|
if content == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
streamLog := entitys.Response{
|
streamLog := entitys.Response{
|
||||||
Index: constants.BotToolsBugOptimizationSubmit,
|
Index: constants.BotToolsBugOptimizationSubmit,
|
||||||
Content: content,
|
Content: content,
|
||||||
|
|
@ -205,6 +222,10 @@ func (s *CallbackService) sendStreamLog(sessionID string, content string) {
|
||||||
|
|
||||||
// sendStreamTxt 发送流式文本
|
// sendStreamTxt 发送流式文本
|
||||||
func (s *CallbackService) sendStreamTxt(sessionID string, content string) {
|
func (s *CallbackService) sendStreamTxt(sessionID string, content string) {
|
||||||
|
if content == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
streamLog := entitys.Response{
|
streamLog := entitys.Response{
|
||||||
Index: constants.BotToolsBugOptimizationSubmit,
|
Index: constants.BotToolsBugOptimizationSubmit,
|
||||||
Content: content,
|
Content: content,
|
||||||
|
|
@ -214,6 +235,21 @@ func (s *CallbackService) sendStreamTxt(sessionID string, content string) {
|
||||||
s.gateway.SendToUid(sessionID, streamLogBytes)
|
s.gateway.SendToUid(sessionID, streamLogBytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sendStreamLoading 发送流式加载过程
|
||||||
|
func (s *CallbackService) sendStreamLoading(sessionID string, content string) {
|
||||||
|
if content == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
streamLog := entitys.Response{
|
||||||
|
Index: constants.BotToolsBugOptimizationSubmit,
|
||||||
|
Content: content,
|
||||||
|
Type: entitys.ResponseLoading,
|
||||||
|
}
|
||||||
|
streamLogBytes := pkg.JsonByteIgonErr(streamLog)
|
||||||
|
s.gateway.SendToUid(sessionID, streamLogBytes)
|
||||||
|
}
|
||||||
|
|
||||||
// handleBugOptimizationSubmitUpdate 处理 bug 优化提交更新回调
|
// handleBugOptimizationSubmitUpdate 处理 bug 优化提交更新回调
|
||||||
func (s *CallbackService) handleBugOptimizationSubmitUpdate(ctx context.Context, taskData json.RawMessage) (string, *errorcode.BusinessErr) {
|
func (s *CallbackService) handleBugOptimizationSubmitUpdate(ctx context.Context, taskData json.RawMessage) (string, *errorcode.BusinessErr) {
|
||||||
var data BugOptimizationSubmitUpdateData
|
var data BugOptimizationSubmitUpdateData
|
||||||
|
|
|
||||||
|
|
@ -2,17 +2,15 @@ package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"ai_scheduler/internal/biz"
|
"ai_scheduler/internal/biz"
|
||||||
|
"ai_scheduler/internal/config"
|
||||||
"ai_scheduler/internal/data/constants"
|
"ai_scheduler/internal/data/constants"
|
||||||
"ai_scheduler/internal/entitys"
|
"ai_scheduler/internal/entitys"
|
||||||
"ai_scheduler/internal/gateway"
|
"ai_scheduler/internal/gateway"
|
||||||
"encoding/hex"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/gofiber/websocket/v2"
|
"github.com/gofiber/websocket/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -21,13 +19,22 @@ type ChatService struct {
|
||||||
routerBiz *biz.AiRouterBiz
|
routerBiz *biz.AiRouterBiz
|
||||||
Gw *gateway.Gateway
|
Gw *gateway.Gateway
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
|
ChatHis *biz.ChatHistoryBiz
|
||||||
|
cfg *config.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewChatHandler 创建聊天处理器
|
// NewChatHandler 创建聊天处理器
|
||||||
func NewChatService(routerService *biz.AiRouterBiz, gw *gateway.Gateway) *ChatService {
|
func NewChatService(
|
||||||
|
routerService *biz.AiRouterBiz,
|
||||||
|
chatHis *biz.ChatHistoryBiz,
|
||||||
|
gw *gateway.Gateway,
|
||||||
|
cfg *config.Config,
|
||||||
|
) *ChatService {
|
||||||
return &ChatService{
|
return &ChatService{
|
||||||
routerBiz: routerService,
|
routerBiz: routerService,
|
||||||
Gw: gw,
|
Gw: gw,
|
||||||
|
ChatHis: chatHis,
|
||||||
|
cfg: cfg,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -53,36 +60,52 @@ func (h *ChatService) ChatFail(c *websocket.Conn, content string) {
|
||||||
_ = c.Close()
|
_ = c.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateClientID() string {
|
// Chat 处理WebSocket聊天连接
|
||||||
// 使用时间戳+随机数确保唯一性
|
// 这是WebSocket处理的主入口函数
|
||||||
timestamp := time.Now().UnixNano()
|
|
||||||
randomBytes := make([]byte, 4)
|
|
||||||
rand.Read(randomBytes)
|
|
||||||
randomStr := hex.EncodeToString(randomBytes)
|
|
||||||
return fmt.Sprintf("%d%s", timestamp, randomStr)
|
|
||||||
}
|
|
||||||
func (h *ChatService) Chat(c *websocket.Conn) {
|
func (h *ChatService) Chat(c *websocket.Conn) {
|
||||||
|
// 创建新的客户端实例
|
||||||
h.mu.Lock()
|
h.mu.Lock()
|
||||||
clientID := generateClientID()
|
client := gateway.NewClient(c)
|
||||||
h.mu.Unlock()
|
h.mu.Unlock()
|
||||||
client := &gateway.Client{
|
|
||||||
ID: clientID,
|
// 将客户端添加到网关管理
|
||||||
SendFunc: func(data []byte) error {
|
|
||||||
return c.WriteMessage(websocket.TextMessage, data)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
h.Gw.AddClient(client)
|
h.Gw.AddClient(client)
|
||||||
log.Println("client connected:", clientID)
|
log.Println("client connected:", client.GetID())
|
||||||
log.Println("客户端已连接")
|
log.Println("客户端已连接")
|
||||||
|
|
||||||
|
// 绑定会话ID
|
||||||
|
uid := c.Query("x-session")
|
||||||
|
if uid != "" {
|
||||||
|
if err := h.Gw.BindUid(client.GetID(), uid); err != nil {
|
||||||
|
log.Println("绑定UID错误:", err)
|
||||||
|
}
|
||||||
|
log.Printf("bind %s -> uid:%s\n", client.GetID(), uid)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证并收集连接数据,后续对话中会使用
|
||||||
|
if err := client.DataAuth(); err != nil {
|
||||||
|
log.Println("数据验证错误:", err)
|
||||||
|
h.ChatFail(c, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 确保在函数返回时移除客户端并关闭连接
|
||||||
|
defer func() {
|
||||||
|
h.Gw.RemoveClient(client.GetID())
|
||||||
|
_ = c.Close()
|
||||||
|
log.Println("client disconnected:", client.GetID())
|
||||||
|
}()
|
||||||
|
|
||||||
// 循环读取客户端消息
|
// 循环读取客户端消息
|
||||||
for {
|
for {
|
||||||
|
// 读取消息
|
||||||
messageType, message, err := c.ReadMessage()
|
messageType, message, err := c.ReadMessage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("读取错误:", err)
|
log.Println("读取错误:", err)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理消息
|
||||||
msg, chatType := h.handleMessageToString(c, messageType, message)
|
msg, chatType := h.handleMessageToString(c, messageType, message)
|
||||||
if chatType == constants.ConnStatusClosed {
|
if chatType == constants.ConnStatusClosed {
|
||||||
break
|
break
|
||||||
|
|
@ -92,42 +115,31 @@ func (h *ChatService) Chat(c *websocket.Conn) {
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("收到消息: %s", string(msg))
|
log.Printf("收到消息: %s", string(msg))
|
||||||
|
|
||||||
|
// 解析请求
|
||||||
var req entitys.ChatSockRequest
|
var req entitys.ChatSockRequest
|
||||||
if err := json.Unmarshal(msg, &req); err != nil {
|
if err = json.Unmarshal(msg, &req); err != nil {
|
||||||
log.Println("JSON parse error:", err)
|
log.Println("JSON parse error:", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
//简单协议:bind:<uid>
|
// 路由处理请求
|
||||||
// if c.Headers("Sec-Websocket-Protocol") == "bind" && req.SessionID != "" {
|
err = h.routerBiz.RouteWithSocket(client, &req)
|
||||||
// uid := c.Query("x-session")
|
|
||||||
// _ = h.Gw.BindUid(clientID, req.SessionID)
|
|
||||||
// log.Printf("bind %s -> uid:%s\n", clientID, uid)
|
|
||||||
// }
|
|
||||||
uid := c.Query("x-session")
|
|
||||||
if uid != "" {
|
|
||||||
_ = h.Gw.BindUid(clientID, uid)
|
|
||||||
log.Printf("bind %s -> uid:%s\n", clientID, uid)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = h.routerBiz.RouteWithSocket(c, &req)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("处理失败:", err)
|
log.Println("处理失败:", err)
|
||||||
entitys.MsgSend(c, entitys.Response{
|
|
||||||
Content: err.Error(),
|
|
||||||
Type: entitys.ResponseText,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
_ = entitys.MsgSend(c, entitys.Response{
|
|
||||||
Content: "",
|
|
||||||
Type: entitys.ResponseEnd,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
h.Gw.RemoveClient(clientID)
|
|
||||||
_ = c.Close()
|
|
||||||
log.Println("client disconnected:", clientID)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handleMessageToString 处理不同类型的WebSocket消息
|
||||||
|
// 参数:
|
||||||
|
// - c: WebSocket连接
|
||||||
|
// - msgType: 消息类型
|
||||||
|
// - msg: 消息内容
|
||||||
|
//
|
||||||
|
// 返回:
|
||||||
|
// - text: 处理后的文本内容
|
||||||
|
// - chatType: 连接状态
|
||||||
func (h *ChatService) handleMessageToString(c *websocket.Conn, msgType int, msg any) (text []byte, chatType constants.ConnStatus) {
|
func (h *ChatService) handleMessageToString(c *websocket.Conn, msgType int, msg any) (text []byte, chatType constants.ConnStatus) {
|
||||||
switch msgType {
|
switch msgType {
|
||||||
case websocket.TextMessage:
|
case websocket.TextMessage:
|
||||||
|
|
@ -148,3 +160,23 @@ func (h *ChatService) handleMessageToString(c *websocket.Conn, msgType int, msg
|
||||||
}
|
}
|
||||||
return msg.([]byte), constants.ConnStatusIgnore
|
return msg.([]byte), constants.ConnStatusIgnore
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *ChatService) Useful(c *fiber.Ctx) error {
|
||||||
|
req := &entitys.UseFulRequest{}
|
||||||
|
if err := c.BodyParser(req); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err := s.ChatHis.Update(c.Context(), req)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *ChatService) UsefulList(c *fiber.Ctx) error {
|
||||||
|
|
||||||
|
return c.JSON(constants.UseFulMap)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@ package services
|
||||||
import (
|
import (
|
||||||
"ai_scheduler/internal/biz"
|
"ai_scheduler/internal/biz"
|
||||||
"ai_scheduler/internal/entitys"
|
"ai_scheduler/internal/entitys"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -25,7 +24,15 @@ func (s *TaskService) Tasks(c *fiber.Ctx) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := s.taskBiz.TaskList(c.Context(), req)
|
auth := ""
|
||||||
|
if auths := c.GetReqHeaders()["Authorization"]; len(auths) > 0 {
|
||||||
|
auth = auths[0]
|
||||||
|
}
|
||||||
|
if auth == "" {
|
||||||
|
return fiber.ErrUnauthorized
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := s.taskBiz.TaskList(c.Context(), req, auth)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,17 @@
|
||||||
package tools
|
package tools
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"ai_scheduler/internal/config"
|
|
||||||
"ai_scheduler/internal/entitys"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestKnowledgeBaseTool_Execute(t *testing.T) {
|
func TestKnowledgeBaseTool_Execute(t *testing.T) {
|
||||||
|
|
||||||
kb := NewKnowledgeBaseTool(config.ToolConfig{})
|
// kb := NewKnowledgeBaseTool(config.ToolConfig{})
|
||||||
channel := make(chan entitys.ResponseData)
|
// channel := make(chan entitys.ResponseData)
|
||||||
err := kb.Execute(channel, nil, nil)
|
// err := kb.Execute(channel, nil, nil)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
t.Errorf("Execute() error = %v", err)
|
// t.Errorf("Execute() error = %v", err)
|
||||||
}
|
// }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"ai_scheduler/internal/data/constants"
|
"ai_scheduler/internal/data/constants"
|
||||||
"ai_scheduler/internal/entitys"
|
"ai_scheduler/internal/entitys"
|
||||||
"ai_scheduler/internal/pkg/utils_ollama"
|
"ai_scheduler/internal/pkg/utils_ollama"
|
||||||
|
zltxtool "ai_scheduler/internal/tools/zltx"
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
@ -70,19 +71,20 @@ func NewManager(config *config.Config, llm *utils_ollama.Client) *Manager {
|
||||||
m.tools[knowledgeTool.Name()] = knowledgeTool
|
m.tools[knowledgeTool.Name()] = knowledgeTool
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.Tools.Knowledge.Enabled {
|
// 注册直连天下上游订单售后工具
|
||||||
knowledgeTool := NewKnowledgeBaseTool(config.Tools.Knowledge)
|
if config.Tools.ZltxOrderAfterSaleSupplier.Enabled {
|
||||||
m.tools[knowledgeTool.Name()] = knowledgeTool
|
zltxOrderAfterSaleSupplierTool := zltxtool.NewOrderAfterSaleSupplierTool(config.Tools.ZltxOrderAfterSaleSupplier)
|
||||||
|
m.tools[zltxOrderAfterSaleSupplierTool.Name()] = zltxOrderAfterSaleSupplierTool
|
||||||
}
|
}
|
||||||
//注册直连天下售后订单详情工具
|
// 注册直连天下下游订单售后工具
|
||||||
if config.Tools.ZltxOrderAfterSaleDetail.Enabled {
|
if config.Tools.ZltxOrderAfterSaleReseller.Enabled {
|
||||||
zltxOrderAfterSaleDetailTool := NewZltxOrderAfterSaleDetailTool(config.Tools.ZltxOrderAfterSaleDetail)
|
zltxOrderAfterSaleResellerTool := zltxtool.NewOrderAfterSaleResellerTool(config.Tools.ZltxOrderAfterSaleReseller)
|
||||||
m.tools[zltxOrderAfterSaleDetailTool.Name()] = zltxOrderAfterSaleDetailTool
|
m.tools[zltxOrderAfterSaleResellerTool.Name()] = zltxOrderAfterSaleResellerTool
|
||||||
}
|
}
|
||||||
//注册直连天下售后订单预检工具
|
// 注册直连天下下游批充订单售后工具
|
||||||
if config.Tools.ZltxOrderAfterSalePreCheck.Enabled {
|
if config.Tools.ZltxOrderAfterSaleResellerBatch.Enabled {
|
||||||
zltxOrderAfterSalePreCheckTool := NewZltxOrderAfterSalePreCheckTool(config.Tools.ZltxOrderAfterSalePreCheck)
|
zltxOrderAfterSaleResellerBatchTool := zltxtool.NewOrderAfterSaleResellerBatchTool(config.Tools.ZltxOrderAfterSaleResellerBatch)
|
||||||
m.tools[zltxOrderAfterSalePreCheckTool.Name()] = zltxOrderAfterSalePreCheckTool
|
m.tools[zltxOrderAfterSaleResellerBatchTool.Name()] = zltxOrderAfterSaleResellerBatchTool
|
||||||
}
|
}
|
||||||
|
|
||||||
// 普通对话
|
// 普通对话
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,357 @@
|
||||||
|
package zltx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"ai_scheduler/internal/config"
|
||||||
|
"ai_scheduler/internal/entitys"
|
||||||
|
"ai_scheduler/internal/pkg/l_request"
|
||||||
|
"ai_scheduler/internal/pkg/util"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type OrderAfterSaleResellerTool struct {
|
||||||
|
config config.ToolConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewOrderAfterSaleResellerTool 创建售后订单预检工具
|
||||||
|
func NewOrderAfterSaleResellerTool(config config.ToolConfig) *OrderAfterSaleResellerTool {
|
||||||
|
return &OrderAfterSaleResellerTool{config: config}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name 返回工具名称
|
||||||
|
func (t *OrderAfterSaleResellerTool) Name() string {
|
||||||
|
return "zltxOrderAfterSaleReseller"
|
||||||
|
}
|
||||||
|
|
||||||
|
// 未使用-仅实现接口
|
||||||
|
func (t *OrderAfterSaleResellerTool) Description() string {
|
||||||
|
return "直连天下下游分销商直充订单售后工具"
|
||||||
|
}
|
||||||
|
|
||||||
|
// 未使用-仅实现接口
|
||||||
|
func (t *OrderAfterSaleResellerTool) Definition() entitys.ToolDefinition {
|
||||||
|
return entitys.ToolDefinition{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type OrderAfterSaleResellerRequest struct {
|
||||||
|
OrderNumber []string `json:"orderNumber"` // 订单号
|
||||||
|
Account []string `json:"account"` // 充值账号
|
||||||
|
SerialCreateTime string `json:"serialCreateTime"` // 流水创建时间
|
||||||
|
AfterType string `json:"afterType"` // 处理方式 1.退款 2.扣款
|
||||||
|
AfterSalesPrice string `json:"afterSalesPrice"` // 售后金额
|
||||||
|
AfterSalesReason string `json:"afterSalesReason"` // 售后原因
|
||||||
|
ResponsibleType string `json:"responsibleType"` // 费用承担者 1.供应商 2.商务 3.公司 4.无
|
||||||
|
ResponsiblePerson string `json:"responsiblePerson"` // 费用承担供应商
|
||||||
|
}
|
||||||
|
|
||||||
|
type OrderAfterSaleResellerResponse struct {
|
||||||
|
Code int `json:"code"`
|
||||||
|
Msg string `json:"msg"`
|
||||||
|
Data []*OrderAfterSaleResellerData `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type OrderAfterSaleResellerData struct {
|
||||||
|
OrderType int `json:"orderType"`
|
||||||
|
OrderNumber string `json:"orderNumber"`
|
||||||
|
OrderAmount float64 `json:"orderAmount"`
|
||||||
|
OrderPrice float64 `json:"orderPrice"`
|
||||||
|
SignCompany int `json:"signCompany"`
|
||||||
|
OrderQuantity int `json:"orderQuantity"`
|
||||||
|
ResellerID int `json:"resellerId"`
|
||||||
|
ResellerName string `json:"resellerName"`
|
||||||
|
OurProductID int `json:"ourProductId"`
|
||||||
|
OurProductTitle string `json:"ourProductTitle"`
|
||||||
|
Account []string `json:"account"`
|
||||||
|
Platforms map[int]string `json:"platforms"`
|
||||||
|
AfterType int `json:"afterType"` // 处理方式 1.退款 2.扣款
|
||||||
|
Remark string `json:"remark"` // 售后原因
|
||||||
|
AfterAmount float64 `json:"afterAmount"` // 售后金额
|
||||||
|
ResponsibleType int `json:"responsibleType"` // 费用承担者 1.供应商 2.商务 3.公司 4.无
|
||||||
|
ResponsiblePerson string `json:"responsiblePerson"` // 费用承担供应商
|
||||||
|
IsExistsAfterSale bool `json:"isExistsAfterSale"` // 是否已存在售后
|
||||||
|
CreateTime int `json:"createTime"` // 流水创建时间
|
||||||
|
}
|
||||||
|
|
||||||
|
// 接口返回
|
||||||
|
type OrderAfterSaleResellerApiResponse struct {
|
||||||
|
Code int `json:"code"`
|
||||||
|
Error string `json:"error"`
|
||||||
|
Data OrderAfterSaleResellerApiData `json:"data"`
|
||||||
|
}
|
||||||
|
type OrderAfterSaleResellerApiData struct {
|
||||||
|
Data []OrderAfterSaleResellerApiBase `json:"data"`
|
||||||
|
ExtData map[string]OrderAfterSaleResellerApiExtItem `json:"extraData"`
|
||||||
|
}
|
||||||
|
type OrderAfterSaleResellerApiBase struct {
|
||||||
|
OrderType int `json:"orderType"`
|
||||||
|
OrderNumber string `json:"orderNumber"`
|
||||||
|
OrderAmount float64 `json:"orderAmount"`
|
||||||
|
OrderPrice float64 `json:"orderPrice"`
|
||||||
|
SignCompany int `json:"signCompany"`
|
||||||
|
OrderQuantity int `json:"orderQuantity"`
|
||||||
|
ResellerID int `json:"resellerId"`
|
||||||
|
ResellerName string `json:"resellerName"`
|
||||||
|
OurProductID int `json:"ourProductId"`
|
||||||
|
OurProductTitle string `json:"ourProductTitle"`
|
||||||
|
Account []string `json:"account"`
|
||||||
|
Platforms map[int]string `json:"platforms"`
|
||||||
|
}
|
||||||
|
type OrderAfterSaleResellerApiExtItem struct {
|
||||||
|
IsExistsAfterSale bool `json:"isExistsAfterSale"` // 是否已存在售后 - 未使用
|
||||||
|
SerialCreateTime int `json:"createTime"` // 流水创建时间
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *OrderAfterSaleResellerTool) Execute(ctx context.Context, requireData *entitys.RequireData) error {
|
||||||
|
var req OrderAfterSaleResellerRequest
|
||||||
|
if err := json.Unmarshal([]byte(requireData.Match.Parameters), &req); err != nil {
|
||||||
|
return fmt.Errorf("解析参数失败,请重试或联系管理员")
|
||||||
|
}
|
||||||
|
if len(req.OrderNumber) == 0 && len(req.Account) == 0 {
|
||||||
|
return fmt.Errorf("订单号 和 充值账号 不能同时为空")
|
||||||
|
}
|
||||||
|
// 时间格式不匹配,直接置为空
|
||||||
|
if req.SerialCreateTime != "" {
|
||||||
|
_, err := time.ParseInLocation(time.DateTime, req.SerialCreateTime, time.Local)
|
||||||
|
if err != nil {
|
||||||
|
entitys.ResLog(requireData.Ch, t.Name(), "时间格式不匹配,已置为空")
|
||||||
|
req.SerialCreateTime = ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
entitys.ResLog(requireData.Ch, t.Name(), "正在拉取售后订单信息")
|
||||||
|
|
||||||
|
return t.checkOrderAfterSaleReseller(req, requireData)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *OrderAfterSaleResellerTool) checkOrderAfterSaleReseller(toolReq OrderAfterSaleResellerRequest, requireData *entitys.RequireData) error {
|
||||||
|
var serialStartTime, serialEndTime int64
|
||||||
|
if toolReq.SerialCreateTime != "" {
|
||||||
|
// 流水创建时间上下浮动10min
|
||||||
|
serialCreateTime, err := time.ParseInLocation(time.DateTime, toolReq.SerialCreateTime, time.Local)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
serialStartTime = serialCreateTime.Unix() - 10*60
|
||||||
|
serialEndTime = serialCreateTime.Unix() + 10*60
|
||||||
|
} else {
|
||||||
|
// 未指定流水创建时间,默认30天内
|
||||||
|
serialEndTime = time.Now().Unix()
|
||||||
|
serialStartTime = serialEndTime - 60*60*24*30 // 30天内
|
||||||
|
}
|
||||||
|
|
||||||
|
// 账号数量超过10直接截断
|
||||||
|
if len(toolReq.Account) > 10 {
|
||||||
|
entitys.ResLog(requireData.Ch, t.Name(), "账号数量超过10已被截断")
|
||||||
|
toolReq.Account = toolReq.Account[:10]
|
||||||
|
}
|
||||||
|
|
||||||
|
headers := map[string]string{
|
||||||
|
"Authorization": fmt.Sprintf("Bearer %s", requireData.Auth),
|
||||||
|
}
|
||||||
|
|
||||||
|
// 最终输出
|
||||||
|
var orderList []*OrderAfterSaleResellerData
|
||||||
|
var err error
|
||||||
|
|
||||||
|
// 多订单号
|
||||||
|
if len(toolReq.OrderNumber) > 0 {
|
||||||
|
body := map[string]any{
|
||||||
|
"order_numbers": toolReq.OrderNumber, // 订单号
|
||||||
|
}
|
||||||
|
orderList, err = t.getAfterSaleResellerList(headers, body, toolReq)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else if len(toolReq.Account) > 0 {
|
||||||
|
// 多充值账号并发
|
||||||
|
orderListChan := make(chan []*OrderAfterSaleResellerData, len(toolReq.Account))
|
||||||
|
waitGroup := sync.WaitGroup{}
|
||||||
|
|
||||||
|
// 并发请求
|
||||||
|
for _, account := range toolReq.Account {
|
||||||
|
waitGroup.Add(1)
|
||||||
|
go func(account string) {
|
||||||
|
defer waitGroup.Done()
|
||||||
|
|
||||||
|
body := map[string]any{
|
||||||
|
"account": account, // 充值账号
|
||||||
|
"create_time": []int64{serialStartTime, serialEndTime}, // 流水创建时间区间
|
||||||
|
"order_type": 1, // 1.直充
|
||||||
|
}
|
||||||
|
orderListIn, errIn := t.getAfterSaleResellerList(headers, body, toolReq)
|
||||||
|
if errIn != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
orderListChan <- orderListIn
|
||||||
|
}(account)
|
||||||
|
}
|
||||||
|
|
||||||
|
waitGroup.Wait()
|
||||||
|
close(orderListChan)
|
||||||
|
|
||||||
|
// 合并结果
|
||||||
|
for orderListIn := range orderListChan {
|
||||||
|
orderList = append(orderList, orderListIn...)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("订单号 和 充值账号 不能同时为空")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 未查询到相应售后订单,请核实提供信息是否正确
|
||||||
|
if len(orderList) == 0 {
|
||||||
|
return fmt.Errorf("未查询到相应售后订单,请核实提供信息是否正确")
|
||||||
|
}
|
||||||
|
|
||||||
|
toolResp := OrderAfterSaleResellerResponse{
|
||||||
|
Code: 0,
|
||||||
|
Msg: "Success",
|
||||||
|
Data: orderList,
|
||||||
|
}
|
||||||
|
|
||||||
|
var jsonByte []byte
|
||||||
|
jsonByte, err = json.Marshal(toolResp)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
entitys.ResLog(requireData.Ch, t.Name(), "售后订单信息拉取完成")
|
||||||
|
entitys.ResJson(requireData.Ch, t.Name(), string(jsonByte))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *OrderAfterSaleResellerTool) getAfterSaleResellerList(headers map[string]string, body map[string]any, originInput OrderAfterSaleResellerRequest) ([]*OrderAfterSaleResellerData, error) {
|
||||||
|
req := l_request.Request{
|
||||||
|
Url: t.config.BaseURL,
|
||||||
|
Headers: headers,
|
||||||
|
Method: "POST",
|
||||||
|
Json: body,
|
||||||
|
}
|
||||||
|
res, err := req.Send()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// 解析响应
|
||||||
|
var resp OrderAfterSaleResellerApiResponse
|
||||||
|
if err = json.Unmarshal(res.Content, &resp); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if resp.Code != 200 {
|
||||||
|
return nil, fmt.Errorf("after sale reseller failed: %s", resp.Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
orderList := make([]*OrderAfterSaleResellerData, 0, len(resp.Data.Data))
|
||||||
|
|
||||||
|
// 转换数据
|
||||||
|
for _, item := range resp.Data.Data {
|
||||||
|
// 处理方式
|
||||||
|
afterType := util.StringToInt(originInput.AfterType)
|
||||||
|
if afterType == 0 {
|
||||||
|
afterType = 1 // 默认退款
|
||||||
|
}
|
||||||
|
// 费用承担者
|
||||||
|
responsibleType := util.StringToInt(originInput.ResponsibleType)
|
||||||
|
if responsibleType == 0 {
|
||||||
|
responsibleType = 4 // 默认无
|
||||||
|
}
|
||||||
|
// 售后金额
|
||||||
|
afterSalesPrice := util.StringToFloat64(originInput.AfterSalesPrice)
|
||||||
|
if afterSalesPrice == 0 {
|
||||||
|
afterSalesPrice = item.OrderPrice
|
||||||
|
}
|
||||||
|
|
||||||
|
orderList = append(orderList, &OrderAfterSaleResellerData{
|
||||||
|
OrderType: item.OrderType,
|
||||||
|
OrderNumber: item.OrderNumber,
|
||||||
|
OrderAmount: item.OrderAmount,
|
||||||
|
OrderPrice: item.OrderPrice,
|
||||||
|
SignCompany: item.SignCompany,
|
||||||
|
OrderQuantity: item.OrderQuantity,
|
||||||
|
ResellerID: item.ResellerID,
|
||||||
|
ResellerName: item.ResellerName,
|
||||||
|
OurProductID: item.OurProductID,
|
||||||
|
OurProductTitle: item.OurProductTitle,
|
||||||
|
Account: item.Account,
|
||||||
|
Platforms: item.Platforms,
|
||||||
|
AfterType: afterType,
|
||||||
|
Remark: originInput.AfterSalesReason,
|
||||||
|
AfterAmount: afterSalesPrice,
|
||||||
|
ResponsibleType: responsibleType,
|
||||||
|
ResponsiblePerson: originInput.ResponsiblePerson,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 追加扩展数据
|
||||||
|
for _, item := range orderList {
|
||||||
|
if extItem, ok := resp.Data.ExtData[item.OrderNumber]; ok {
|
||||||
|
item.IsExistsAfterSale = item.OrderType > 100 // 101 直充&已售后
|
||||||
|
item.CreateTime = extItem.SerialCreateTime
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return orderList, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// func (t *OrderAfterSaleResellerTool) checkOrderAfterSaleResellerMock(req OrderAfterSaleResellerRequest, requireData *entitys.RequireData) error {
|
||||||
|
// resp := OrderAfterSaleResellerResponse{
|
||||||
|
// Code: 0,
|
||||||
|
// Msg: "success",
|
||||||
|
// Data: []*OrderAfterSaleResellerData{
|
||||||
|
// {
|
||||||
|
// OrderType: 1,
|
||||||
|
// OrderNumber: "846784115378364417",
|
||||||
|
// OrderAmount: 0.1,
|
||||||
|
// OrderPrice: 0.1,
|
||||||
|
// SignCompany: 1,
|
||||||
|
// OrderQuantity: 1,
|
||||||
|
// ResellerID: 23329,
|
||||||
|
// ResellerName: "分销商23329",
|
||||||
|
// OurProductID: 106,
|
||||||
|
// OurProductTitle: "爱奇艺黄金会员周卡",
|
||||||
|
// Account: []string{"15516353308"},
|
||||||
|
// Platforms: map[int]string{4: "爱奇艺"},
|
||||||
|
// CreateTime: 1723304000,
|
||||||
|
// AfterType: 1,
|
||||||
|
// Remark: "测试售后",
|
||||||
|
// AfterAmount: 50,
|
||||||
|
// ResponsibleType: 1,
|
||||||
|
// IsExistsAfterSale: false,
|
||||||
|
// },
|
||||||
|
// {
|
||||||
|
// OrderType: 101,
|
||||||
|
// OrderNumber: "846052057729867777",
|
||||||
|
// OrderAmount: 23,
|
||||||
|
// OrderPrice: 23,
|
||||||
|
// SignCompany: 1,
|
||||||
|
// OrderQuantity: 1,
|
||||||
|
// ResellerID: 25629,
|
||||||
|
// ResellerName: "二期财务分销商简称",
|
||||||
|
// OurProductID: 104,
|
||||||
|
// OurProductTitle: "优酷年卡",
|
||||||
|
// Account: []string{"18380416326"},
|
||||||
|
// Platforms: map[int]string{1: "爱瓦力"},
|
||||||
|
// CreateTime: 1723305000,
|
||||||
|
// AfterType: 2,
|
||||||
|
// Remark: "测试售后2",
|
||||||
|
// AfterAmount: 30,
|
||||||
|
// ResponsibleType: 2,
|
||||||
|
// IsExistsAfterSale: false,
|
||||||
|
// },
|
||||||
|
// },
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if len(req.OrderNumber) == 1 {
|
||||||
|
// resp.Data = resp.Data[:1]
|
||||||
|
// }
|
||||||
|
|
||||||
|
// jsonByte, err := json.Marshal(resp)
|
||||||
|
// if err != nil {
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
|
||||||
|
// entitys.ResLog(requireData.Ch, t.Name(), "售后订单信息拉取完成")
|
||||||
|
// entitys.ResJson(requireData.Ch, t.Name(), string(jsonByte))
|
||||||
|
// return nil
|
||||||
|
// }
|
||||||
|
|
@ -0,0 +1,206 @@
|
||||||
|
package zltx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"ai_scheduler/internal/config"
|
||||||
|
"ai_scheduler/internal/entitys"
|
||||||
|
"ai_scheduler/internal/pkg/l_request"
|
||||||
|
"ai_scheduler/internal/pkg/util"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type OrderAfterSaleResellerBatchTool struct {
|
||||||
|
config config.ToolConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewOrderAfterSaleResellerBatchTool 创建售后订单预检工具
|
||||||
|
func NewOrderAfterSaleResellerBatchTool(config config.ToolConfig) *OrderAfterSaleResellerBatchTool {
|
||||||
|
return &OrderAfterSaleResellerBatchTool{config: config}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name 返回工具名称
|
||||||
|
func (t *OrderAfterSaleResellerBatchTool) Name() string {
|
||||||
|
return "zltxOrderAfterSaleResellerBatch"
|
||||||
|
}
|
||||||
|
|
||||||
|
// 未使用-仅实现接口
|
||||||
|
func (t *OrderAfterSaleResellerBatchTool) Description() string {
|
||||||
|
return "直连天下下游分销商批充订单售后工具"
|
||||||
|
}
|
||||||
|
|
||||||
|
// 未使用-仅实现接口
|
||||||
|
func (t *OrderAfterSaleResellerBatchTool) Definition() entitys.ToolDefinition {
|
||||||
|
return entitys.ToolDefinition{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type OrderAfterSaleResellerBatchRequest struct {
|
||||||
|
OrderNumber []string `json:"orderNumber"` // 订单号
|
||||||
|
AfterType string `json:"afterType"` // 处理方式 1.退款 2.扣款
|
||||||
|
AfterSalesPrice string `json:"afterSalesPrice"` // 售后金额
|
||||||
|
AfterSalesReason string `json:"afterSalesReason"` // 售后原因
|
||||||
|
ResponsibleType string `json:"responsibleType"` // 费用承担者 1.供应商 2.商务 3.公司 4.无
|
||||||
|
ResponsiblePerson string `json:"responsiblePerson"` // 费用承担供应商
|
||||||
|
}
|
||||||
|
|
||||||
|
type OrderAfterSaleResellerBatchResponse struct {
|
||||||
|
Code int `json:"code"`
|
||||||
|
Msg string `json:"msg"`
|
||||||
|
Data []*OrderAfterSaleResellerBatchData `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type OrderAfterSaleResellerBatchData struct {
|
||||||
|
OrderType int `json:"orderType"`
|
||||||
|
OrderNumber string `json:"orderNumber"`
|
||||||
|
OrderAmount float64 `json:"orderAmount"`
|
||||||
|
OrderPrice float64 `json:"orderPrice"`
|
||||||
|
SignCompany int `json:"signCompany"`
|
||||||
|
OrderQuantity int `json:"orderQuantity"`
|
||||||
|
ResellerID int `json:"resellerId"`
|
||||||
|
ResellerName string `json:"resellerName"`
|
||||||
|
OurProductID int `json:"ourProductId"`
|
||||||
|
OurProductTitle string `json:"ourProductTitle"`
|
||||||
|
Account []string `json:"account"`
|
||||||
|
Platforms map[int]string `json:"platforms"`
|
||||||
|
AfterType int `json:"afterType"` // 处理方式 1.退款 2.扣款
|
||||||
|
Remark string `json:"remark"` // 售后原因
|
||||||
|
AfterAmount float64 `json:"afterAmount"` // 售后金额
|
||||||
|
ResponsibleType int `json:"responsibleType"` // 费用承担者 1.供应商 2.商务 3.公司 4.无
|
||||||
|
ResponsiblePerson string `json:"responsiblePerson"` // 费用承担供应商
|
||||||
|
IsExistsAfterSale bool `json:"isExistsAfterSale"` // 是否已存在售后
|
||||||
|
CreateTime int `json:"createTime"` // 创建时间
|
||||||
|
}
|
||||||
|
|
||||||
|
// 接口返回
|
||||||
|
type OrderAfterSaleResellerBatchApiResponse struct {
|
||||||
|
Code int `json:"code"`
|
||||||
|
Error string `json:"error"`
|
||||||
|
Data OrderAfterSaleResellerBatchApiData `json:"data"`
|
||||||
|
}
|
||||||
|
type OrderAfterSaleResellerBatchApiData struct {
|
||||||
|
Data []OrderAfterSaleResellerBatchApiBase `json:"data"`
|
||||||
|
ExtData map[string]OrderAfterSaleResellerBatchApiExtItem `json:"extraData"`
|
||||||
|
}
|
||||||
|
type OrderAfterSaleResellerBatchApiBase struct {
|
||||||
|
OrderType int `json:"orderType"`
|
||||||
|
OrderNumber string `json:"orderNumber"`
|
||||||
|
OrderAmount float64 `json:"orderAmount"`
|
||||||
|
OrderPrice float64 `json:"orderPrice"`
|
||||||
|
SignCompany int `json:"signCompany"`
|
||||||
|
OrderQuantity int `json:"orderQuantity"`
|
||||||
|
ResellerID int `json:"resellerId"`
|
||||||
|
ResellerName string `json:"resellerName"`
|
||||||
|
OurProductID int `json:"ourProductId"`
|
||||||
|
OurProductTitle string `json:"ourProductTitle"`
|
||||||
|
Account []string `json:"account"`
|
||||||
|
Platforms map[int]string `json:"platforms"`
|
||||||
|
}
|
||||||
|
type OrderAfterSaleResellerBatchApiExtItem struct {
|
||||||
|
IsExistsAfterSale bool `json:"isExistsAfterSale"` // 是否已存在售后 - 未使用
|
||||||
|
SerialCreateTime int `json:"createTime"` // 流水创建时间
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *OrderAfterSaleResellerBatchTool) Execute(ctx context.Context, requireData *entitys.RequireData) error {
|
||||||
|
var req OrderAfterSaleResellerBatchRequest
|
||||||
|
if err := json.Unmarshal([]byte(requireData.Match.Parameters), &req); err != nil {
|
||||||
|
return fmt.Errorf("解析参数失败,请重试或联系管理员")
|
||||||
|
}
|
||||||
|
if len(req.OrderNumber) == 0 {
|
||||||
|
return fmt.Errorf("批充订单号不能为空")
|
||||||
|
}
|
||||||
|
|
||||||
|
entitys.ResLog(requireData.Ch, t.Name(), "正在拉取售后订单信息")
|
||||||
|
|
||||||
|
return t.checkOrderAfterSaleResellerBatch(req, requireData)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *OrderAfterSaleResellerBatchTool) checkOrderAfterSaleResellerBatch(toolReq OrderAfterSaleResellerBatchRequest, requireData *entitys.RequireData) error {
|
||||||
|
req := l_request.Request{
|
||||||
|
Url: t.config.BaseURL,
|
||||||
|
Headers: map[string]string{
|
||||||
|
"Authorization": fmt.Sprintf("Bearer %s", requireData.Auth),
|
||||||
|
},
|
||||||
|
Method: "POST",
|
||||||
|
Json: map[string]any{
|
||||||
|
"order_numbers": toolReq.OrderNumber, // 流水号
|
||||||
|
"order_type": 2, // 2.批充
|
||||||
|
},
|
||||||
|
}
|
||||||
|
res, err := req.Send()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// 解析响应
|
||||||
|
var resp OrderAfterSaleResellerBatchApiResponse
|
||||||
|
if err = json.Unmarshal(res.Content, &resp); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if resp.Code != 200 {
|
||||||
|
return fmt.Errorf("售后订单查询异常: %s", resp.Error)
|
||||||
|
}
|
||||||
|
if len(resp.Data.Data) == 0 {
|
||||||
|
return fmt.Errorf("未查询到相应售后订单,请核实订单号是否正确")
|
||||||
|
}
|
||||||
|
|
||||||
|
toolResp := OrderAfterSaleResellerBatchResponse{
|
||||||
|
Code: resp.Code,
|
||||||
|
Msg: resp.Error,
|
||||||
|
Data: make([]*OrderAfterSaleResellerBatchData, 0, len(resp.Data.Data)),
|
||||||
|
}
|
||||||
|
|
||||||
|
// 转换数据
|
||||||
|
for _, item := range resp.Data.Data {
|
||||||
|
// 处理方式
|
||||||
|
afterType := util.StringToInt(toolReq.AfterType)
|
||||||
|
if afterType == 0 {
|
||||||
|
afterType = 1 // 默认退款
|
||||||
|
}
|
||||||
|
// 费用承担者
|
||||||
|
responsibleType := util.StringToInt(toolReq.ResponsibleType)
|
||||||
|
if responsibleType == 0 {
|
||||||
|
responsibleType = 4 // 默认无
|
||||||
|
}
|
||||||
|
// 售后金额
|
||||||
|
afterSalesPrice := util.StringToFloat64(toolReq.AfterSalesPrice)
|
||||||
|
if afterSalesPrice == 0 {
|
||||||
|
afterSalesPrice = item.OrderPrice
|
||||||
|
}
|
||||||
|
|
||||||
|
toolResp.Data = append(toolResp.Data, &OrderAfterSaleResellerBatchData{
|
||||||
|
OrderType: item.OrderType,
|
||||||
|
OrderNumber: item.OrderNumber,
|
||||||
|
OrderAmount: item.OrderAmount,
|
||||||
|
OrderPrice: item.OrderPrice,
|
||||||
|
SignCompany: item.SignCompany,
|
||||||
|
OrderQuantity: item.OrderQuantity,
|
||||||
|
ResellerID: item.ResellerID,
|
||||||
|
ResellerName: item.ResellerName,
|
||||||
|
OurProductID: item.OurProductID,
|
||||||
|
OurProductTitle: item.OurProductTitle,
|
||||||
|
Account: item.Account,
|
||||||
|
Platforms: item.Platforms,
|
||||||
|
AfterType: afterType,
|
||||||
|
Remark: toolReq.AfterSalesReason,
|
||||||
|
AfterAmount: afterSalesPrice,
|
||||||
|
ResponsibleType: responsibleType,
|
||||||
|
ResponsiblePerson: toolReq.ResponsiblePerson,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 追加扩展数据
|
||||||
|
for _, item := range toolResp.Data {
|
||||||
|
if extItem, ok := resp.Data.ExtData[item.OrderNumber]; ok {
|
||||||
|
item.IsExistsAfterSale = item.OrderType > 100 // 102 批充&已售后
|
||||||
|
item.CreateTime = extItem.SerialCreateTime
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonByte, err := json.Marshal(toolResp)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
entitys.ResLog(requireData.Ch, t.Name(), "售后订单信息拉取完成")
|
||||||
|
entitys.ResJson(requireData.Ch, t.Name(), string(jsonByte))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,279 @@
|
||||||
|
package zltx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"ai_scheduler/internal/config"
|
||||||
|
"ai_scheduler/internal/entitys"
|
||||||
|
"ai_scheduler/internal/pkg/l_request"
|
||||||
|
"ai_scheduler/internal/pkg/util"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type OrderAfterSaleSupplierTool struct {
|
||||||
|
config config.ToolConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewOrderAfterSaleSupplierTool 创建售后订单预检工具
|
||||||
|
func NewOrderAfterSaleSupplierTool(config config.ToolConfig) *OrderAfterSaleSupplierTool {
|
||||||
|
return &OrderAfterSaleSupplierTool{config: config}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name 返回工具名称
|
||||||
|
func (t *OrderAfterSaleSupplierTool) Name() string {
|
||||||
|
return "zltxOrderAfterSaleSupplier"
|
||||||
|
}
|
||||||
|
|
||||||
|
// 未使用-仅实现接口
|
||||||
|
func (t *OrderAfterSaleSupplierTool) Description() string {
|
||||||
|
return "直连天下上游供应商直充订单售后工具"
|
||||||
|
}
|
||||||
|
|
||||||
|
// 未使用-仅实现接口
|
||||||
|
func (t *OrderAfterSaleSupplierTool) Definition() entitys.ToolDefinition {
|
||||||
|
return entitys.ToolDefinition{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type OrderAfterSaleSupplierRequest struct {
|
||||||
|
SerialNumber []string `json:"serialNumber"` // 流水号
|
||||||
|
Account []string `json:"account"` // 充值账号
|
||||||
|
SerialCreateTime string `json:"serialCreateTime"` // 流水创建时间
|
||||||
|
AfterSalesReason string `json:"afterSalesReason"` // 售后原因
|
||||||
|
AfterSalesPrice string `json:"afterSalesPrice"` // 售后金额
|
||||||
|
AfterType string `json:"afterType"` // 售后类型 1.加款 2.扣款
|
||||||
|
}
|
||||||
|
|
||||||
|
// 工具最终返回
|
||||||
|
type OrderAfterSaleSupplierResponse struct {
|
||||||
|
Code int `json:"code"`
|
||||||
|
Msg string `json:"msg"`
|
||||||
|
Data []*OrderAfterSaleSupplierData `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type OrderAfterSaleSupplierData struct {
|
||||||
|
SerialNumber string `json:"serialNumber"` // 流水号
|
||||||
|
PlatformName string `json:"platformName"` // 供应商名称
|
||||||
|
SignCompany int `json:"signCompany"` // 签约主体
|
||||||
|
PlatformProductName string `json:"platformProductName"` // 商品名称
|
||||||
|
PlatformPrice float64 `json:"platformPrice"` // 上游价格
|
||||||
|
TerminalAccount string `json:"terminalAccount"` // 充值账号
|
||||||
|
Status int `json:"status"` // 充值状态
|
||||||
|
PlatformProductID int `json:"platformProductId"` // 上有商品id
|
||||||
|
PlatformID int `json:"platformId"` // 上游平台id
|
||||||
|
SignCompanyName string `json:"signCompanyName"` // 签约主体名称
|
||||||
|
Reason string `json:"reason"` // 售后原因
|
||||||
|
SalePrice float64 `json:"salePrice"` // 售后金额
|
||||||
|
SaleType int `json:"saleType"` // 处理方式 1.加款 2.扣款
|
||||||
|
ExecuteTime int `json:"executeTime"` // 流水创建时间
|
||||||
|
IsExistsAfterSale bool `json:"isExistsAfterSale"` // 是否已存在售后
|
||||||
|
}
|
||||||
|
|
||||||
|
// 接口返回
|
||||||
|
type OrderAfterSaleSupplierApiResponse struct {
|
||||||
|
Code int `json:"code"`
|
||||||
|
Error string `json:"error"`
|
||||||
|
Data OrderAfterSaleSupplierApiData `json:"data"`
|
||||||
|
}
|
||||||
|
type OrderAfterSaleSupplierApiData struct {
|
||||||
|
Data []OrderAfterSaleSupplierApiBase `json:"data"`
|
||||||
|
ExtData map[string]OrderAfterSaleSupplierApiExtItem `json:"extraData"`
|
||||||
|
}
|
||||||
|
type OrderAfterSaleSupplierApiBase struct {
|
||||||
|
SerialNumber string `json:"serialNumber"` // 流水号
|
||||||
|
PlatformName string `json:"platformName"` // 供应商名称
|
||||||
|
SignCompany int `json:"signCompany"` // 签约主体
|
||||||
|
PlatformProductName string `json:"platformProductName"` // 商品名称
|
||||||
|
PlatformPrice float64 `json:"platformPrice"` // 上游价格
|
||||||
|
TerminalAccount string `json:"terminalAccount"` // 充值账号
|
||||||
|
Status int `json:"status"` // 充值状态
|
||||||
|
PlatformProductID int `json:"platformProductId"` // 上有商品id
|
||||||
|
PlatformID int `json:"platformId"` // 上游平台id
|
||||||
|
SignCompanyName string `json:"signCompanyName"` // 签约主体名称
|
||||||
|
ExecuteTime int `json:"executeTime"` // 充值执行时间
|
||||||
|
}
|
||||||
|
type OrderAfterSaleSupplierApiExtItem struct {
|
||||||
|
IsExistsAfterSale bool `json:"existAfterSales"` // 是否已存在售后 - 未使用
|
||||||
|
SerialCreateTime int `json:"createTime"` // 流水创建时间
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *OrderAfterSaleSupplierTool) Execute(ctx context.Context, requireData *entitys.RequireData) error {
|
||||||
|
var req OrderAfterSaleSupplierRequest
|
||||||
|
if err := json.Unmarshal([]byte(requireData.Match.Parameters), &req); err != nil {
|
||||||
|
return fmt.Errorf("解析参数失败,请重试或联系管理员")
|
||||||
|
}
|
||||||
|
if len(req.SerialNumber) == 0 && len(req.Account) == 0 {
|
||||||
|
return fmt.Errorf("充值流水号 和 充值账号 不能同时为空")
|
||||||
|
}
|
||||||
|
// 时间格式不匹配,直接置为空
|
||||||
|
if req.SerialCreateTime != "" {
|
||||||
|
_, err := time.ParseInLocation(time.DateTime, req.SerialCreateTime, time.Local)
|
||||||
|
if err != nil {
|
||||||
|
entitys.ResLog(requireData.Ch, t.Name(), "时间格式不匹配,已置为空")
|
||||||
|
req.SerialCreateTime = ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
entitys.ResLog(requireData.Ch, t.Name(), "正在拉取售后订单信息")
|
||||||
|
|
||||||
|
return t.checkOrderAfterSaleSupplier(req, requireData)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *OrderAfterSaleSupplierTool) checkOrderAfterSaleSupplier(toolReq OrderAfterSaleSupplierRequest, requireData *entitys.RequireData) error {
|
||||||
|
var serialStartTime, serialEndTime int64
|
||||||
|
if toolReq.SerialCreateTime != "" {
|
||||||
|
// 流水创建时间上下浮动10min
|
||||||
|
serialCreateTime, err := time.ParseInLocation(time.DateTime, toolReq.SerialCreateTime, time.Local)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
serialStartTime = serialCreateTime.Unix() - 10*60
|
||||||
|
serialEndTime = serialCreateTime.Unix() + 10*60
|
||||||
|
} else {
|
||||||
|
// 未指定流水创建时间,默认30天内
|
||||||
|
serialEndTime = time.Now().Unix()
|
||||||
|
serialStartTime = serialEndTime - 60*60*24*30 // 30天内
|
||||||
|
}
|
||||||
|
|
||||||
|
// 账号数量超过10直接截断
|
||||||
|
if len(toolReq.Account) > 10 {
|
||||||
|
entitys.ResLog(requireData.Ch, t.Name(), "账号数量超过10已被截断")
|
||||||
|
toolReq.Account = toolReq.Account[:10]
|
||||||
|
}
|
||||||
|
|
||||||
|
headers := map[string]string{
|
||||||
|
"Authorization": fmt.Sprintf("Bearer %s", requireData.Auth),
|
||||||
|
}
|
||||||
|
|
||||||
|
// 最终输出
|
||||||
|
var orderList []*OrderAfterSaleSupplierData
|
||||||
|
var err error
|
||||||
|
|
||||||
|
// 多流水号
|
||||||
|
if len(toolReq.SerialNumber) > 0 {
|
||||||
|
body := map[string]any{
|
||||||
|
"serial_numbers": toolReq.SerialNumber, // 流水号
|
||||||
|
}
|
||||||
|
orderList, err = t.getAfterSaleSupplierList(headers, body, toolReq)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else if len(toolReq.Account) > 0 {
|
||||||
|
// 多充值账号并发
|
||||||
|
orderListChan := make(chan []*OrderAfterSaleSupplierData, len(toolReq.Account))
|
||||||
|
waitGroup := sync.WaitGroup{}
|
||||||
|
|
||||||
|
// 并发请求
|
||||||
|
for _, account := range toolReq.Account {
|
||||||
|
waitGroup.Add(1)
|
||||||
|
go func(account string) {
|
||||||
|
defer waitGroup.Done()
|
||||||
|
|
||||||
|
body := map[string]any{
|
||||||
|
"account": account, // 充值账号
|
||||||
|
"create_time": []int64{serialStartTime, serialEndTime}, // 流水创建时间区间
|
||||||
|
}
|
||||||
|
orderListIn, errIn := t.getAfterSaleSupplierList(headers, body, toolReq)
|
||||||
|
if errIn != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
orderListChan <- orderListIn
|
||||||
|
}(account)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 等待所有请求完成
|
||||||
|
waitGroup.Wait()
|
||||||
|
close(orderListChan)
|
||||||
|
|
||||||
|
for orderListIn := range orderListChan {
|
||||||
|
orderList = append(orderList, orderListIn...)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("充值流水号 和 充值账号 不能同时为空")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 未查询到相应售后订单,请核实提供信息是否正确
|
||||||
|
if len(orderList) == 0 {
|
||||||
|
return fmt.Errorf("未查询到相应售后订单,请核实提供信息是否正确")
|
||||||
|
}
|
||||||
|
|
||||||
|
toolResp := OrderAfterSaleSupplierResponse{
|
||||||
|
Code: 0,
|
||||||
|
Msg: "Success",
|
||||||
|
Data: orderList,
|
||||||
|
}
|
||||||
|
|
||||||
|
var jsonByte []byte
|
||||||
|
jsonByte, err = json.Marshal(toolResp)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
entitys.ResLog(requireData.Ch, t.Name(), "售后订单信息拉取完成")
|
||||||
|
entitys.ResJson(requireData.Ch, t.Name(), string(jsonByte))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *OrderAfterSaleSupplierTool) getAfterSaleSupplierList(headers map[string]string, body map[string]any, originInput OrderAfterSaleSupplierRequest) ([]*OrderAfterSaleSupplierData, error) {
|
||||||
|
req := l_request.Request{
|
||||||
|
Url: t.config.BaseURL,
|
||||||
|
Headers: headers,
|
||||||
|
Method: "POST",
|
||||||
|
Json: body,
|
||||||
|
}
|
||||||
|
res, err := req.Send()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// 解析响应
|
||||||
|
var resp OrderAfterSaleSupplierApiResponse
|
||||||
|
if err = json.Unmarshal(res.Content, &resp); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if resp.Code != 200 {
|
||||||
|
return nil, fmt.Errorf("after sale supplier failed: %s", resp.Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
orderList := make([]*OrderAfterSaleSupplierData, 0, len(resp.Data.Data))
|
||||||
|
|
||||||
|
// 转换数据
|
||||||
|
for _, item := range resp.Data.Data {
|
||||||
|
// 处理方式
|
||||||
|
afterType := util.StringToInt(originInput.AfterType)
|
||||||
|
if afterType == 0 {
|
||||||
|
afterType = 1 // 默认退款
|
||||||
|
}
|
||||||
|
// 售后金额
|
||||||
|
afterSalesPrice := util.StringToFloat64(originInput.AfterSalesPrice)
|
||||||
|
if afterSalesPrice == 0 {
|
||||||
|
afterSalesPrice = item.PlatformPrice
|
||||||
|
}
|
||||||
|
|
||||||
|
orderList = append(orderList, &OrderAfterSaleSupplierData{
|
||||||
|
SerialNumber: item.SerialNumber,
|
||||||
|
PlatformName: item.PlatformName,
|
||||||
|
SignCompany: item.SignCompany,
|
||||||
|
PlatformProductName: item.PlatformProductName,
|
||||||
|
PlatformPrice: item.PlatformPrice,
|
||||||
|
TerminalAccount: item.TerminalAccount,
|
||||||
|
Status: item.Status,
|
||||||
|
PlatformProductID: item.PlatformProductID,
|
||||||
|
PlatformID: item.PlatformID,
|
||||||
|
SignCompanyName: item.SignCompanyName,
|
||||||
|
Reason: originInput.AfterSalesReason,
|
||||||
|
SalePrice: afterSalesPrice,
|
||||||
|
SaleType: afterType,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 追加扩展数据
|
||||||
|
for _, item := range orderList {
|
||||||
|
if extItem, ok := resp.Data.ExtData[item.SerialNumber]; ok {
|
||||||
|
item.IsExistsAfterSale = extItem.IsExistsAfterSale
|
||||||
|
item.ExecuteTime = extItem.SerialCreateTime
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return orderList, nil
|
||||||
|
}
|
||||||
|
|
@ -77,6 +77,7 @@ func (w *BotTool) BugOptimizationSubmit(ctx context.Context, requireData *entity
|
||||||
// 记录 task_id 到 session_id 的映射
|
// 记录 task_id 到 session_id 的映射
|
||||||
w.SetTaskMapping(body.TaskId, requireData.Session)
|
w.SetTaskMapping(body.TaskId, requireData.Session)
|
||||||
entitys.ResLog(requireData.Ch, requireData.Match.Index, "问题记录中")
|
entitys.ResLog(requireData.Ch, requireData.Match.Index, "问题记录中")
|
||||||
|
entitys.ResLoading(requireData.Ch, requireData.Match.Index, "问题记录中...")
|
||||||
|
|
||||||
// 等待异步回调完成再结束
|
// 等待异步回调完成再结束
|
||||||
for {
|
for {
|
||||||
|
|
@ -84,8 +85,8 @@ func (w *BotTool) BugOptimizationSubmit(ctx context.Context, requireData *entity
|
||||||
if !ok || sessionID != requireData.Session {
|
if !ok || sessionID != requireData.Session {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
entitys.ResLoading(requireData.Ch, requireData.Match.Index, "问题记录中...")
|
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second * 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ type BotTool struct {
|
||||||
llm *utils_ollama.Client
|
llm *utils_ollama.Client
|
||||||
sessionImpl *impl.SessionImpl
|
sessionImpl *impl.SessionImpl
|
||||||
taskMap map[string]string // task_id -> session_id
|
taskMap map[string]string // task_id -> session_id
|
||||||
|
// zltxOrderAfterSaleTool tools.ZltxOrderAfterSaleTool
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBotTool 创建直连天下订单详情工具
|
// NewBotTool 创建直连天下订单详情工具
|
||||||
|
|
|
||||||
|
|
@ -68,6 +68,14 @@ func (k DataTemp) Add(data interface{}) (id int, err error) {
|
||||||
return primary.Id, add.Error
|
return primary.Id, add.Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (k DataTemp) AddWithData(data interface{}) (interface{}, error) {
|
||||||
|
result := k.Db.Model(k.Model).Create(data)
|
||||||
|
if result.Error != nil {
|
||||||
|
return data, result.Error
|
||||||
|
}
|
||||||
|
return data, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (k DataTemp) GetList(cond *builder.Cond, pageBoIn *ReqPageBo) (list []map[string]interface{}, pageBoOut *RespPageBo, err error) {
|
func (k DataTemp) GetList(cond *builder.Cond, pageBoIn *ReqPageBo) (list []map[string]interface{}, pageBoOut *RespPageBo, err error) {
|
||||||
var (
|
var (
|
||||||
query, _ = builder.ToBoundSQL(*cond)
|
query, _ = builder.ToBoundSQL(*cond)
|
||||||
|
|
|
||||||
|
|
@ -1,72 +1,72 @@
|
||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
// import (
|
||||||
"fmt"
|
// "fmt"
|
||||||
"github.com/go-kratos/kratos/v2/log"
|
// "github.com/go-kratos/kratos/v2/log"
|
||||||
"google.golang.org/protobuf/runtime/protoimpl"
|
// "google.golang.org/protobuf/runtime/protoimpl"
|
||||||
"gopkg.in/yaml.v3"
|
// "gopkg.in/yaml.v3"
|
||||||
"io/fs"
|
// "io/fs"
|
||||||
"os"
|
// "os"
|
||||||
"path/filepath"
|
// "path/filepath"
|
||||||
"testing"
|
// "testing"
|
||||||
baseconf "trans_hub/base_conf"
|
// baseconf "trans_hub/base_conf"
|
||||||
"trans_hub/pkg"
|
// "trans_hub/pkg"
|
||||||
"trans_hub/pkg/mapstructure"
|
// "trans_hub/pkg/mapstructure"
|
||||||
)
|
// )
|
||||||
|
|
||||||
const SPACE = "public"
|
// const SPACE = "public"
|
||||||
const PORT = 8848
|
// const PORT = 8848
|
||||||
const User = ""
|
// const User = ""
|
||||||
const Pass = ""
|
// const Pass = ""
|
||||||
const IP = "192.168.110.93"
|
// const IP = "192.168.110.93"
|
||||||
const Group = "DEFAULT_GROUP"
|
// const Group = "DEFAULT_GROUP"
|
||||||
const DataId = "PG_BASE_CONFIG"
|
// const DataId = "PG_BASE_CONFIG"
|
||||||
|
|
||||||
func TestConfig(t *testing.T) {
|
// func TestConfig(t *testing.T) {
|
||||||
type Nacos struct {
|
// type Nacos struct {
|
||||||
state protoimpl.MessageState
|
// state protoimpl.MessageState
|
||||||
sizeCache protoimpl.SizeCache
|
// sizeCache protoimpl.SizeCache
|
||||||
unknownFields protoimpl.UnknownFields
|
// unknownFields protoimpl.UnknownFields
|
||||||
Ip string `protobuf:"bytes,1,opt,name=ip,proto3" json:"ip,omitempty"`
|
// Ip string `protobuf:"bytes,1,opt,name=ip,proto3" json:"ip,omitempty"`
|
||||||
Port uint64 `protobuf:"varint,2,opt,name=port,proto3" json:"port,omitempty"`
|
// Port uint64 `protobuf:"varint,2,opt,name=port,proto3" json:"port,omitempty"`
|
||||||
}
|
// }
|
||||||
type Conf struct {
|
// type Conf struct {
|
||||||
Nacos *Nacos `protobuf:"bytes,8,opt,name=nacos,proto3" json:"nacos,omitempty"`
|
// Nacos *Nacos `protobuf:"bytes,8,opt,name=nacos,proto3" json:"nacos,omitempty"`
|
||||||
}
|
// }
|
||||||
var c Conf
|
// var c Conf
|
||||||
nc := &baseconf.Nacos{Ip: IP, Port: PORT, Space: SPACE, User: User, Password: Pass}
|
// nc := &baseconf.Nacos{Ip: IP, Port: PORT, Space: SPACE, User: User, Password: Pass}
|
||||||
|
|
||||||
var s = ServerConfig(nc, Group, DataId)
|
// var s = ServerConfig(nc, Group, DataId)
|
||||||
err := mapstructure.Decode(s, &c)
|
// err := mapstructure.Decode(s, &c)
|
||||||
t.Log(s, err)
|
// t.Log(s, err)
|
||||||
}
|
// }
|
||||||
|
|
||||||
func TestMod(t *testing.T) {
|
// func TestMod(t *testing.T) {
|
||||||
dir := pkg.GetRootPath()
|
// dir := pkg.GetRootPath()
|
||||||
// 读取目录内容
|
// // 读取目录内容
|
||||||
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
|
// err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
if !d.IsDir() && filepath.Ext(path) == ".yaml" {
|
// if !d.IsDir() && filepath.Ext(path) == ".yaml" {
|
||||||
data, err := os.ReadFile(path)
|
// data, err := os.ReadFile(path)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
var result map[string]interface{}
|
// var result map[string]interface{}
|
||||||
err = yaml.Unmarshal(data, &result) // 解析YAML到map中,使用gopkg.v3的yaml包或其他你选择的版本(例如encoding/yaml)
|
// err = yaml.Unmarshal(data, &result) // 解析YAML到map中,使用gopkg.v3的yaml包或其他你选择的版本(例如encoding/yaml)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
fmt.Printf("File: %s\nContent: %+v\n", path, result)
|
// fmt.Printf("File: %s\nContent: %+v\n", path, result)
|
||||||
}
|
// }
|
||||||
return nil
|
// return nil
|
||||||
})
|
// })
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
log.Fatal(err)
|
// log.Fatal(err)
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
func TestYaml(t *testing.T) {
|
// func TestYaml(t *testing.T) {
|
||||||
t.Log(GetBaseYaml())
|
// t.Log(GetBaseYaml())
|
||||||
}
|
// }
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue