This commit is contained in:
oneao committed 2025-11-12 17:31:12 +08:00
1 parent b8a6b15355
commit 95d645f572
13 files changed
+660 -63

No files matched your search

+1
View File
@@ -21,6 +21,7 @@ require (
github.com/klauspost/compress v1.18.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
github.com/sagikazarmark/locafero v0.12.0 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
github.com/spf13/afero v1.15.0 // indirect
github.com/spf13/cast v1.10.0 // indirect
+2
View File
@@ -27,6 +27,8 @@ github.com/sagikazarmark/locafero v0.11.0 h1:1iurJgmM9G3PA/I+wWYIOw/5SyBtxapeHDc
github.com/sagikazarmark/locafero v0.11.0/go.mod h1:nVIGvgyzw595SUSUE6tvCp3YYTeHs15MvlmU87WwIik=
github.com/sagikazarmark/locafero v0.12.0 h1:/NQhBAkUb4+fH1jivKHWusDYFjMOOKU88eegjfxfHb4=
github.com/sagikazarmark/locafero v0.12.0/go.mod h1:sZh36u/YSZ918v0Io+U9ogLYQJ9tLLBmM4eneO6WwsI=
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 h1:+jumHNA0Wrelhe64i8F6HNlS8pkoyMv5sreGx2Ry5Rw=
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8/go.mod h1:3n1Cwaq1E1/1lhQhtRK2ts/ZwZEhjcQeJQ1RuC6Q/8U=
github.com/spf13/afero v1.15.0 h1:b/YBCLWAJdFWJTN9cLhiXXcD7mzKn9Dm86dNnfyQw1I=
@@ -7,12 +7,15 @@ import (
"base-go-v2/internal/utils/idgen"
"base-go-v2/internal/utils/mapx"
"fmt"
"github.com/shopspring/decimal"
)
func InitMoneyRouter(r *router.Router) {
group := r.Group("money")
group.GET("/category", getUserCategory)
//group := r.Group("auth")
group.POST("/insert", insertMoneyRecord)
group.POST("/update", updateMoneyRecord)
group.POST("/delete", deleteMoneyRecord)
}
// InitDefaultCategories 初始化系统默认分类到用户分类表
@@ -54,7 +57,6 @@ func InitDefaultCategories(userID int64) error {
}
func getUserCategory(c *router.Context) error {
fmt.Println("请求进入了====")
userId := c.GetUserID()
// 查询当前用户的分类
@@ -80,3 +82,110 @@ func getUserCategory(c *router.Context) error {
return response.Success(c).Data(result).Send()
}
func insertMoneyRecord(c *router.Context) error {
bodyData, err := c.GetBodyWithRequired("category_id", "amount", "record_time")
if err != nil {
return c.AddError(err)
}
categoryId := bodyData.GetInt64("category_id")
recordTime := bodyData.GetString("record_time")
amountStr := bodyData.GetString("amount")
remark := bodyData.GetString("remark")
amountDecimal, err := decimal.NewFromString(amountStr)
if err != nil {
return c.AddError(err)
}
newRecord := mapx.New().SetKV("id", idgen.NextId(),
"category_id", categoryId,
"user_id", c.GetUserID(),
"amount", amountDecimal,
"remark", remark,
"record_time", recordTime,
)
insertOne, err := db.InsertOne("money_record", newRecord)
if err != nil {
return response.Fail(c).Message("新增失败").Send()
}
if insertOne != 1 {
return response.Fail(c).Message("新增失败").Send()
}
return response.Success(c).Data(newRecord).Message("新增成功").Send()
}
func updateMoneyRecord(c *router.Context) error {
bodyData, err := c.GetBodyWithRequired("id")
if err != nil {
return c.AddError(err)
}
recordId := bodyData.GetInt64("id")
record, err := db.GetOne("money_record", "id", recordId)
if err != nil {
return c.AddError(err)
}
if record == nil {
return response.Fail(c).Message("该条账单不存在").Send()
}
categoryId := bodyData.GetInt64("category_id")
recordTime := bodyData.GetString("record_time")
amountStr := bodyData.GetString("amount")
remark := bodyData.GetString("remark")
updateRecord := mapx.New().SetKV("id", recordId)
if record.GetInt64("category_id") != categoryId {
updateRecord.SetKV("category_id", categoryId)
}
if record.GetString("record_time") != recordTime {
updateRecord.SetKV("record_time", recordTime)
}
if record.GetString("amount") != amountStr {
amountDecimal, err := decimal.NewFromString(amountStr)
if err != nil {
return c.AddError(err)
}
updateRecord.SetKV("amount", amountDecimal)
}
if record.GetString("remark") != remark {
updateRecord.SetKV("remark", remark)
}
updateOne, err := db.UpdateOne("money_record", "id", recordId, updateRecord)
if err != nil {
return c.AddError(err)
}
if updateOne != 1 {
return response.Fail(c).Message("更新失败").Send()
}
return response.Success(c).Message("更新成功").Send()
}
func deleteMoneyRecord(c *router.Context) error {
bodyData, err := c.GetBodyWithRequired("id")
if err != nil {
return c.AddError(err)
}
recordId := bodyData.GetInt64("id")
deleteOne, err := db.DeleteOne("money_record", "id", recordId)
if err != nil {
return c.AddError(err)
}
if deleteOne != 1 {
return response.Fail(c).Message("删除失败").Send()
}
return response.Success(c).Message("删除成功").Send()
}