Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
小滋润 committed Oct 29, 2024
1 parent 6ac6662 commit 403caf2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ docker/*
!docker/Dockerfile.debug
!docker/Dockerfile.test

docker-compose.override.yaml

production_config.yaml
23 changes: 13 additions & 10 deletions service/category/categoryService.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package categoryService

import (
"context"
"strings"
"time"

"KeepAccount/global"
"KeepAccount/global/constant"
"KeepAccount/global/cus"
Expand All @@ -11,10 +15,8 @@ import (
transactionModel "KeepAccount/model/transaction"
userModel "KeepAccount/model/user"
"KeepAccount/util/dataTool"
"context"
"github.com/pkg/errors"
"gorm.io/gorm"
"time"
)

type Category struct {
Expand Down Expand Up @@ -109,7 +111,7 @@ func (catSvc *Category) UpdateCategoryMapping(category categoryModel.Category, c
return err
}
for _, mainCategory := range mainCategoryList {
if target == mainCategory.Name {
if strings.Compare(target, mainCategory.Name) == 0 {
_, err = categoryDao.CreateMapping(mainCategory, category)
if err != nil && !errors.Is(err, gorm.ErrDuplicatedKey) {
return err
Expand Down Expand Up @@ -458,15 +460,10 @@ func (catSvc *Category) MappingCategoryToAccountMapping(

func (catSvc *Category) mappingAccountCategoryByAI(
mainAccount, mappingAccount accountModel.Account, ctx context.Context,
) error {
tx := db.Get(ctx)
if false == aiService.IsOpen() {
return nil
}
) (err error) {
var mainCategoryList, mappingCategoryList dataTool.Slice[string, categoryModel.Category]
var err error
var matchingResult map[string]string
categoryDao := categoryModel.NewDao(tx)
categoryDao := categoryModel.NewDao(db.Get(ctx))
for _, ie := range []constant.IncomeExpense{constant.Income, constant.Expense} {
// 查询交易类型
mainCategoryList, err = categoryDao.GetListByAccount(mainAccount, &ie)
Expand Down Expand Up @@ -507,6 +504,12 @@ func (catSvc *Category) mappingAccountCategoryByAI(
},
)
for mappingName, mainName := range matchingResult {
if _, exit := mainNameMap[mainName]; !exit {
continue
}
if _, exit := mappingNameMap[mappingName]; !exit {
continue
}
_, err = categoryDao.CreateMapping(mainNameMap[mainName], mappingNameMap[mappingName])
if err != nil && !errors.Is(err, gorm.ErrDuplicatedKey) {
return err
Expand Down
5 changes: 0 additions & 5 deletions service/category/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ func init() {
)
}

func (t *_task) UpdateCategoryMapping(category categoryModel.Category) error {
_ = nats.PublishTaskWithPayload[categoryModel.Category](nats.TaskUpdateCategoryMapping, category)
return nil
}

func (t *_task) MappingCategoryToAccountMapping(mappingAccount accountModel.Mapping) error {
_ = nats.PublishTaskWithPayload[accountModel.Mapping](nats.TaskMappingCategoryToAccountMapping, mappingAccount)
return nil
Expand Down
28 changes: 24 additions & 4 deletions service/thirdparty/ai.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package thirdpartyService

import (
"KeepAccount/global"
"context"
"strings"

"KeepAccount/global"
"github.com/go-resty/resty/v2"
)

Expand All @@ -23,7 +25,12 @@ func (as *aiServer) ChineseSimilarityMatching(sourceStr string, targetList []str
target string, err error,
) {
if false == as.IsOpen() {
return
for _, targetStr := range targetList {
if strings.Compare(sourceStr, targetStr) == 0 {
return targetStr, nil
}
}
return target, nil
}
responseData, err := as.requestChineseSimilarity([]string{sourceStr}, targetList, ctx)
if err != nil {
Expand All @@ -40,7 +47,18 @@ func (as *aiServer) BatchChineseSimilarityMatching(sourceList, targetList []stri
map[string]string, error,
) {
if false == as.IsOpen() {
return make(map[string]string), nil
targetNameMap := make(map[string]struct{})
for _, targetStr := range targetList {
targetNameMap[targetStr] = struct{}{}
}
result := make(map[string]string)
for _, sourceStr := range sourceList {
if _, exist := targetNameMap[sourceStr]; !exist {
continue
}
result[sourceStr] = sourceStr
}
return result, nil
}
responseData, err := as.requestChineseSimilarity(sourceList, targetList, ctx)
if err != nil {
Expand All @@ -61,7 +79,9 @@ type chineseSimilarityResponse []struct {
Similarity float32
}

func (as *aiServer) requestChineseSimilarity(SourceList, TargetList []string, ctx context.Context) (chineseSimilarityResponse, error) {
func (as *aiServer) requestChineseSimilarity(
SourceList, TargetList []string,
ctx context.Context) (chineseSimilarityResponse, error) {
var response struct {
aiApiResponse
Data chineseSimilarityResponse
Expand Down

0 comments on commit 403caf2

Please sign in to comment.