fonchain-fiee/pkg/service/asChat/chatAutoReplyRulerHandler.go

210 lines
5.7 KiB
Go
Raw Normal View History

2025-06-12 11:31:40 +00:00
package asChat
import (
2025-06-14 06:46:01 +00:00
"encoding/json"
"fmt"
"fonchain-fiee/api/account"
2025-06-12 11:31:40 +00:00
"fonchain-fiee/api/accountFiee"
"fonchain-fiee/cmd/config"
2025-06-12 11:31:40 +00:00
"fonchain-fiee/pkg/service"
2025-06-14 06:46:01 +00:00
"fonchain-fiee/pkg/service/asChat/dto"
2025-06-12 11:31:40 +00:00
"fonchain-fiee/pkg/utils"
"fonchain-fiee/pkg/utils/secret"
"fonchain-fiee/pkg/utils/stime"
2025-06-12 11:31:40 +00:00
"github.com/gin-gonic/gin"
"math/rand"
"time"
2025-06-12 11:31:40 +00:00
)
var Handler = &ChatAutoReplyRulerHandler{}
type ChatAutoReplyRulerHandler struct {
}
// 创建自动回复规则
func (a *ChatAutoReplyRulerHandler) CreateChatAutoReplyRuler(c *gin.Context) {
2025-06-14 06:46:01 +00:00
var req dto.ChatAutoReplyData
2025-06-12 11:31:40 +00:00
if err := c.ShouldBindJSON(&req); err != nil {
service.Error(c, err)
return
}
2025-06-14 06:46:01 +00:00
rulerBytes, _ := json.Marshal(req.Rules)
protoReq := accountFiee.ChatAutoReplyRulerData{
Title: req.Title,
Ruler: string(rulerBytes),
Response: req.Response,
}
_, err := service.AccountFieeProvider.CreateChatAutoReplyRuler(c, &protoReq)
2025-06-12 11:31:40 +00:00
if err != nil {
service.Error(c, err)
return
}
service.Success(c)
}
// 删除自动回复规则
func (a *ChatAutoReplyRulerHandler) DeleteChatAutoReplyRuler(c *gin.Context) {
var req accountFiee.DeleteChatAutoReplyRulerRequest
if err := c.ShouldBindJSON(&req); err != nil {
service.Error(c, err)
return
}
_, err := service.AccountFieeProvider.DeleteChatAutoReplyRuler(c, &req)
if err != nil {
service.Error(c, err)
return
}
service.Success(c)
}
// 更新自动回复规则
func (a *ChatAutoReplyRulerHandler) UpdateChatAutoReplyRuler(c *gin.Context) {
2025-06-14 06:46:01 +00:00
var req dto.ChatAutoReplyData
2025-06-12 11:31:40 +00:00
if err := c.ShouldBindJSON(&req); err != nil {
service.Error(c, err)
return
}
2025-06-14 06:46:01 +00:00
protoReq := req.ToProtoData()
_, err := service.AccountFieeProvider.UpdateChatAutoReplyRuler(c, protoReq)
2025-06-12 11:31:40 +00:00
if err != nil {
service.Error(c, err)
return
}
service.Success(c)
}
// 使用id查询自动回复规则
func (a *ChatAutoReplyRulerHandler) GetChatAutoReplyRulerDetail(c *gin.Context) {
var req accountFiee.GetChatAutoReplyRulerByIdRequest
if err := c.ShouldBindJSON(&req); err != nil {
service.Error(c, err)
return
}
resp, err := service.AccountFieeProvider.GetChatAutoReplyRulerDetail(c, &req)
if err != nil {
service.Error(c, err)
return
}
2025-06-14 06:46:01 +00:00
tmp := dto.ChatAutoReplyData{}
tmp.Parse(resp)
service.Success(c, tmp)
2025-06-12 11:31:40 +00:00
}
// 批量查询自动回复规则
func (a *ChatAutoReplyRulerHandler) GetChatAutoReplyRulerList(c *gin.Context) {
2025-06-14 06:46:01 +00:00
var req dto.GetChatAutoReplyRulerListRequest
2025-06-12 11:31:40 +00:00
if err := c.ShouldBindJSON(&req); err != nil {
service.Error(c, err)
return
}
var protoReq = accountFiee.GetChatAutoReplyRulerListRequest{Query: &accountFiee.ChatAutoReplyRulerData{}}
utils.RequestDataConvert(&req, &protoReq)
resp, err := service.AccountFieeProvider.GetChatAutoReplyRulerList(c, &protoReq)
if err != nil {
service.Error(c, err)
return
}
2025-06-14 06:46:01 +00:00
var data []dto.ChatAutoReplyData
for _, v := range resp.List {
tmp := dto.ChatAutoReplyData{}
tmp.Parse(v)
data = append(data, tmp)
}
service.Success(c, map[string]interface{}{
"data": data,
"page": resp.Page,
"pagesize": resp.PageSize,
"total": resp.Total,
})
2025-06-12 11:31:40 +00:00
}
func (a *ChatAutoReplyRulerHandler) ErpLoginDemo(c *gin.Context) {
2025-06-14 06:46:01 +00:00
var req dto.ErpLoginDemoReq
if err := c.ShouldBindJSON(&req); err != nil {
service.Error(c, err)
return
}
loginRes, err := service.AccountProvider.Login(c, &account.LoginRequest{
Domain: "fontree",
TelNum: req.TelNum,
Password: req.Password,
})
if err != nil {
if err.Error() == "没有找到数据" || err.Error() == "No data found" {
registerRequest := account.RegistRequest{
Domain: "fontree",
NickName: req.TelNum,
TelNum: req.TelNum,
Password: req.Password,
EnterDate: time.Now().Format(stime.Format_Normal_YMD),
Extend: &account.Extend{JumpTo: "onsite"}, //origin-老平台 onsite 当前
JobNum: fmt.Sprintf("%d", rand.Intn(1000)),
}
registerRes, errs := service.AccountProvider.Register(c, &registerRequest)
if errs != nil {
service.Error(c, errs)
return
}
service.Success(c, registerRes)
} else {
service.Error(c, err)
}
return
}
departmentName := ""
if loginRes.AccountInfo != nil && len(loginRes.AccountInfo.Departments) > 0 {
departmentName = loginRes.AccountInfo.Departments[0].Name
}
loginRes.Token, err = secret.CombineSecret("xxx", departmentName, loginRes.Token)
if err != nil {
service.Error(c, err)
return
}
service.Success(c, loginRes)
}
func (a *ChatAutoReplyRulerHandler) FieeLoginDemo(c *gin.Context) {
2025-06-14 06:46:01 +00:00
var req dto.ErpLoginDemoReq
if err := c.ShouldBindJSON(&req); err != nil {
service.Error(c, err)
return
}
loginRes, err := service.AccountFieeProvider.Login(c, &accountFiee.LoginRequest{
Domain: config.AppConfig.System.Domain,
TelNum: req.TelNum,
Password: req.Password,
})
if err != nil {
if err.Error() == "账号不存在" || err.Error() == "Account does not exist" {
registerRequest := accountFiee.RegistRequest{
Domain: config.AppConfig.System.Domain,
NickName: req.TelNum,
TelNum: req.TelNum,
//Password: req.Password,
//EnterDate: time.Now().Format(stime.Format_Normal_YMD),
//Extend: &account.Extend{JumpTo: "onsite"}, //origin-老平台 onsite 当前
//JobNum: fmt.Sprintf("%d", rand.Intn(1000)),
}
registerRes, errs := service.AccountFieeProvider.Register(c, &registerRequest)
if errs != nil {
service.Error(c, errs)
return
}
service.Success(c, registerRes)
} else {
service.Error(c, err)
}
return
}
departmentName := ""
if loginRes.AccountInfo != nil && len(loginRes.AccountInfo.Departments) > 0 {
departmentName = loginRes.AccountInfo.Departments[0].Name
}
loginRes.Token, err = secret.CombineSecret("xxx", departmentName, loginRes.Token)
if err != nil {
service.Error(c, err)
return
}
service.Success(c, loginRes)
}