u
This commit is contained in:
1 parent
098a061308
commit
994321d049
40 files changed
+1401
-359
No files matched your search
@@ -0,0 +1,79 @@
|
||||
package money
|
||||
|
||||
import (
|
||||
"base-go-v2/internal/db"
|
||||
response "base-go-v2/internal/reponse"
|
||||
"base-go-v2/internal/router"
|
||||
"base-go-v2/internal/utils/idgen"
|
||||
"base-go-v2/internal/utils/mapx"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func InitMoneyRouter(r *router.Router) {
|
||||
group := r.Group("money")
|
||||
group.GET("/category", getUserCategory)
|
||||
//group := r.Group("auth")
|
||||
}
|
||||
|
||||
// InitDefaultCategories 初始化系统默认分类到用户分类表
|
||||
func InitDefaultCategories(userID int64) error {
|
||||
fmt.Println("进入 InitDefaultCategories ===")
|
||||
|
||||
// 1. 查询系统分类表
|
||||
sysCategories, err := db.FindAll("money_sys_category", "sort_number")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("系统分类数量: %d\n", len(sysCategories))
|
||||
|
||||
// 2. 构建批量插入数据
|
||||
userCategories := make([]mapx.M, 0, len(sysCategories))
|
||||
for i, row := range sysCategories {
|
||||
m := mapx.New().SetKV(
|
||||
"id", idgen.NextId(),
|
||||
"user_id", userID,
|
||||
"name", row.GetString("name"),
|
||||
"icon", row.GetString("icon"),
|
||||
"type", row.GetInt("type"),
|
||||
"sort_number", row.GetInt("sort_number"),
|
||||
)
|
||||
fmt.Printf("用户分类 %d: %+v\n", i+1, m)
|
||||
userCategories = append(userCategories, m)
|
||||
}
|
||||
|
||||
// 3. 批量插入到用户分类表
|
||||
if _, err := db.InsertBatch("money_user_category", userCategories); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println("初始化用户分类成功!")
|
||||
return nil
|
||||
}
|
||||
|
||||
func getUserCategory(c *router.Context) error {
|
||||
userId := c.GetUserID()
|
||||
|
||||
// 查询当前用户的分类
|
||||
userCategories, err := db.Find("money_user_category", mapx.New().Set("user_id", userId), "sort_number ASC")
|
||||
if err != nil {
|
||||
return c.AddError(err)
|
||||
}
|
||||
|
||||
// 按 type 分组
|
||||
result := map[string][]mapx.M{
|
||||
"income": {},
|
||||
"expense": {},
|
||||
}
|
||||
|
||||
for _, cat := range userCategories {
|
||||
switch cat.GetInt("type") {
|
||||
case 0:
|
||||
result["income"] = append(result["income"], cat)
|
||||
case 1:
|
||||
result["expense"] = append(result["expense"], cat)
|
||||
}
|
||||
}
|
||||
|
||||
return response.Success(c).Data(result).Send()
|
||||
}
|
||||
Reference in new issue
Block a user