full
This commit is contained in:
parent
a5caa734c3
commit
b5ef982645
@ -5,12 +5,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"management/internal/config"
|
|
||||||
db "management/internal/db/sqlc"
|
db "management/internal/db/sqlc"
|
||||||
"management/internal/erpserver"
|
"management/internal/erpserver"
|
||||||
"management/internal/erpserver/biz"
|
"management/internal/erpserver/biz"
|
||||||
"management/internal/erpserver/handler"
|
"management/internal/erpserver/handler"
|
||||||
"management/internal/pkg/binding"
|
"management/internal/pkg/binding"
|
||||||
|
"management/internal/pkg/config"
|
||||||
"management/internal/pkg/logger"
|
"management/internal/pkg/logger"
|
||||||
"management/internal/pkg/middleware"
|
"management/internal/pkg/middleware"
|
||||||
"management/internal/pkg/redis"
|
"management/internal/pkg/redis"
|
||||||
|
|||||||
@ -5,7 +5,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"management/internal/config"
|
"management/internal/pkg/config"
|
||||||
|
|
||||||
"github.com/jackc/pgconn"
|
"github.com/jackc/pgconn"
|
||||||
"github.com/jackc/pgx/v5"
|
"github.com/jackc/pgx/v5"
|
||||||
|
|||||||
@ -4,20 +4,27 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"management/internal/db/model/dto"
|
"management/internal/db/model/dto"
|
||||||
db "management/internal/db/sqlc"
|
db "management/internal/db/sqlc"
|
||||||
|
"management/internal/erpserver/model/form"
|
||||||
"management/internal/erpserver/model/view"
|
"management/internal/erpserver/model/view"
|
||||||
|
"management/internal/pkg/convertor"
|
||||||
"management/internal/pkg/know"
|
"management/internal/pkg/know"
|
||||||
"management/internal/pkg/redis"
|
"management/internal/pkg/redis"
|
||||||
"management/internal/pkg/tpl/html"
|
"management/internal/pkg/tpl/html"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CategoryBiz interface {
|
type CategoryBiz interface {
|
||||||
Create(ctx context.Context, arg *db.CreateCategoryParams) (*db.Category, error)
|
Create(ctx context.Context, req *form.Category) error
|
||||||
Update(ctx context.Context, arg *db.UpdateCategoryParams) (*db.Category, error)
|
Update(ctx context.Context, req *form.Category) error
|
||||||
All(ctx context.Context) ([]*db.Category, error)
|
All(ctx context.Context) ([]*db.Category, error)
|
||||||
List(ctx context.Context, q dto.SearchDto) ([]*db.Category, int64, error)
|
List(ctx context.Context, q dto.SearchDto) ([]*db.Category, int64, error)
|
||||||
Get(ctx context.Context, id int32) (*db.Category, error)
|
Get(ctx context.Context, id int32) (*db.Category, error)
|
||||||
@ -97,12 +104,117 @@ func (b *categoryBiz) Get(ctx context.Context, id int32) (*db.Category, error) {
|
|||||||
return b.store.GetCategory(ctx, id)
|
return b.store.GetCategory(ctx, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *categoryBiz) Create(ctx context.Context, arg *db.CreateCategoryParams) (*db.Category, error) {
|
func (b *categoryBiz) Create(ctx context.Context, req *form.Category) error {
|
||||||
return b.store.CreateCategory(ctx, arg)
|
if len(req.Icon) > 0 && !strings.HasPrefix(req.Icon, "/") {
|
||||||
|
req.Icon = "/" + req.Icon
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(req.Letter) == 0 {
|
||||||
|
req.Letter = uuid.New().String()
|
||||||
|
}
|
||||||
|
|
||||||
|
parent := &db.Category{
|
||||||
|
ID: 0,
|
||||||
|
ParentID: 0,
|
||||||
|
ParentPath: ",0,",
|
||||||
|
}
|
||||||
|
if *req.ParentID > 0 {
|
||||||
|
var err error
|
||||||
|
parent, err = b.store.GetCategory(ctx, *req.ParentID)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("父级节点错误")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var order int32 = 6666
|
||||||
|
if *req.Sort > 0 {
|
||||||
|
order = *req.Sort
|
||||||
|
}
|
||||||
|
|
||||||
|
arg := &db.CreateCategoryParams{
|
||||||
|
Name: req.Name,
|
||||||
|
Icon: req.Icon,
|
||||||
|
Description: req.Description,
|
||||||
|
Letter: req.Letter,
|
||||||
|
ParentID: parent.ID,
|
||||||
|
ParentPath: convertor.HandleParentPath(fmt.Sprintf("%s,%d,", parent.ParentPath, parent.ID)),
|
||||||
|
Status: *req.Status,
|
||||||
|
Sort: order,
|
||||||
|
}
|
||||||
|
_, err := b.store.CreateCategory(ctx, arg)
|
||||||
|
if err != nil {
|
||||||
|
if db.IsUniqueViolation(err) {
|
||||||
|
return errors.New("类别已存在")
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *categoryBiz) Update(ctx context.Context, arg *db.UpdateCategoryParams) (*db.Category, error) {
|
func (b *categoryBiz) Update(ctx context.Context, req *form.Category) error {
|
||||||
return b.store.UpdateCategory(ctx, arg)
|
if len(req.Icon) > 0 && !strings.HasPrefix(req.Icon, "/") {
|
||||||
|
req.Icon = "/" + req.Icon
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(req.Letter) == 0 {
|
||||||
|
req.Letter = uuid.New().String()
|
||||||
|
}
|
||||||
|
|
||||||
|
parent := &db.Category{
|
||||||
|
ID: 0,
|
||||||
|
ParentID: 0,
|
||||||
|
ParentPath: ",0,",
|
||||||
|
}
|
||||||
|
if *req.ParentID > 0 {
|
||||||
|
var err error
|
||||||
|
parent, err = b.store.GetCategory(ctx, *req.ParentID)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("父级节点错误")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var order int32 = 6666
|
||||||
|
if *req.Sort > 0 {
|
||||||
|
order = *req.Sort
|
||||||
|
}
|
||||||
|
|
||||||
|
arg := &db.UpdateCategoryParams{
|
||||||
|
ID: *req.ID,
|
||||||
|
Name: pgtype.Text{
|
||||||
|
String: req.Name,
|
||||||
|
Valid: true,
|
||||||
|
},
|
||||||
|
Icon: pgtype.Text{
|
||||||
|
String: req.Icon,
|
||||||
|
Valid: len(req.Icon) > 0,
|
||||||
|
},
|
||||||
|
Description: pgtype.Text{
|
||||||
|
String: req.Description,
|
||||||
|
Valid: len(req.Description) > 0,
|
||||||
|
},
|
||||||
|
Letter: pgtype.Text{
|
||||||
|
String: req.Letter,
|
||||||
|
Valid: len(req.Letter) > 0,
|
||||||
|
},
|
||||||
|
ParentID: pgtype.Int4{
|
||||||
|
Int32: *req.ParentID,
|
||||||
|
Valid: true,
|
||||||
|
},
|
||||||
|
ParentPath: pgtype.Text{
|
||||||
|
String: convertor.HandleParentPath(fmt.Sprintf("%s,%d,", parent.ParentPath, parent.ID)),
|
||||||
|
Valid: true,
|
||||||
|
},
|
||||||
|
Sort: pgtype.Int4{
|
||||||
|
Int32: order,
|
||||||
|
Valid: true,
|
||||||
|
},
|
||||||
|
Status: pgtype.Int2{
|
||||||
|
Int16: *req.Status,
|
||||||
|
Valid: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
_, err := b.store.UpdateCategory(ctx, arg)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *categoryBiz) Refresh(ctx context.Context) ([]*db.Category, error) {
|
func (b *categoryBiz) Refresh(ctx context.Context) ([]*db.Category, error) {
|
||||||
|
|||||||
@ -3,8 +3,8 @@ package common
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"management/internal/config"
|
|
||||||
commonv1 "management/internal/erpserver/biz/v1/common"
|
commonv1 "management/internal/erpserver/biz/v1/common"
|
||||||
|
"management/internal/pkg/config"
|
||||||
"management/internal/pkg/tpl"
|
"management/internal/pkg/tpl"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"management/internal/config"
|
|
||||||
commonv1 "management/internal/erpserver/biz/v1/common"
|
commonv1 "management/internal/erpserver/biz/v1/common"
|
||||||
|
"management/internal/pkg/config"
|
||||||
"management/internal/pkg/tpl"
|
"management/internal/pkg/tpl"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"management/internal/config"
|
|
||||||
"management/internal/erpserver/biz"
|
"management/internal/erpserver/biz"
|
||||||
"management/internal/erpserver/handler/budget"
|
"management/internal/erpserver/handler/budget"
|
||||||
"management/internal/erpserver/handler/common"
|
"management/internal/erpserver/handler/common"
|
||||||
@ -10,6 +9,7 @@ import (
|
|||||||
"management/internal/erpserver/handler/income"
|
"management/internal/erpserver/handler/income"
|
||||||
"management/internal/erpserver/handler/project"
|
"management/internal/erpserver/handler/project"
|
||||||
"management/internal/erpserver/handler/system"
|
"management/internal/erpserver/handler/system"
|
||||||
|
"management/internal/pkg/config"
|
||||||
"management/internal/pkg/middleware"
|
"management/internal/pkg/middleware"
|
||||||
"management/internal/pkg/redis"
|
"management/internal/pkg/redis"
|
||||||
"management/internal/pkg/session"
|
"management/internal/pkg/session"
|
||||||
|
|||||||
@ -1,18 +1,15 @@
|
|||||||
package system
|
package system
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"management/internal/db/model/dto"
|
"management/internal/db/model/dto"
|
||||||
db "management/internal/db/sqlc"
|
db "management/internal/db/sqlc"
|
||||||
"management/internal/erpserver/biz"
|
"management/internal/erpserver/biz"
|
||||||
|
"management/internal/erpserver/model/form"
|
||||||
|
"management/internal/pkg/binding"
|
||||||
"management/internal/pkg/convertor"
|
"management/internal/pkg/convertor"
|
||||||
"management/internal/pkg/tpl"
|
"management/internal/pkg/tpl"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type CategoryHandler interface {
|
type CategoryHandler interface {
|
||||||
@ -98,98 +95,25 @@ func (h *categoryHandler) Edit(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *categoryHandler) Save(w http.ResponseWriter, r *http.Request) {
|
func (h *categoryHandler) Save(w http.ResponseWriter, r *http.Request) {
|
||||||
id := convertor.ConvertInt(r.PostFormValue("ID"), 0)
|
var req form.Category
|
||||||
parentID := convertor.ConvertInt[int32](r.PostFormValue("ParentID"), 0)
|
if err := binding.Form.Bind(r, &req); err != nil {
|
||||||
name := r.PostFormValue("Name")
|
h.render.JSONERR(w, binding.ValidatorErrors(err))
|
||||||
icon := r.PostFormValue("File")
|
|
||||||
if len(icon) > 0 && !strings.HasPrefix(icon, "/") {
|
|
||||||
icon = "/" + icon
|
|
||||||
}
|
|
||||||
description := r.PostFormValue("Description")
|
|
||||||
letter := r.PostFormValue("Letter")
|
|
||||||
if len(letter) == 0 {
|
|
||||||
letter = uuid.New().String()
|
|
||||||
}
|
|
||||||
sort := convertor.ConvertInt[int32](r.PostFormValue("Sort"), 6666)
|
|
||||||
status := convertor.ConvertInt[int16](r.PostFormValue("Status"), 9999)
|
|
||||||
|
|
||||||
ctx := r.Context()
|
|
||||||
var parent *db.Category
|
|
||||||
if parentID > 0 {
|
|
||||||
var err error
|
|
||||||
parent, err = h.biz.SystemV1().CategoryBiz().Get(ctx, parentID)
|
|
||||||
if err != nil {
|
|
||||||
h.render.JSONERR(w, "父级节点错误")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
path := fmt.Sprintf("%s,%d,", parent.ParentPath, parent.ID)
|
if *req.ID == 0 {
|
||||||
path = strings.ReplaceAll(path, ",,", ",")
|
err := h.biz.SystemV1().CategoryBiz().Create(r.Context(), &req)
|
||||||
if id == 0 {
|
|
||||||
arg := &db.CreateCategoryParams{
|
|
||||||
Name: name,
|
|
||||||
Icon: icon,
|
|
||||||
Description: description,
|
|
||||||
Letter: letter,
|
|
||||||
ParentID: parentID,
|
|
||||||
ParentPath: path,
|
|
||||||
Status: status,
|
|
||||||
Sort: sort,
|
|
||||||
}
|
|
||||||
_, err := h.biz.SystemV1().CategoryBiz().Create(ctx, arg)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if db.IsUniqueViolation(err) {
|
|
||||||
h.render.JSONERR(w, "名称已存在")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
h.render.JSONERR(w, err.Error())
|
h.render.JSONERR(w, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
h.render.JSONOK(w, "添加成功")
|
h.render.JSONOK(w, "添加成功")
|
||||||
} else {
|
} else {
|
||||||
arg := &db.UpdateCategoryParams{
|
err := h.biz.SystemV1().CategoryBiz().Update(r.Context(), &req)
|
||||||
ID: int32(id),
|
|
||||||
Name: pgtype.Text{
|
|
||||||
String: name,
|
|
||||||
Valid: true,
|
|
||||||
},
|
|
||||||
Icon: pgtype.Text{
|
|
||||||
String: icon,
|
|
||||||
Valid: len(icon) > 0,
|
|
||||||
},
|
|
||||||
Description: pgtype.Text{
|
|
||||||
String: description,
|
|
||||||
Valid: len(description) > 0,
|
|
||||||
},
|
|
||||||
Letter: pgtype.Text{
|
|
||||||
String: letter,
|
|
||||||
Valid: len(letter) > 0,
|
|
||||||
},
|
|
||||||
ParentID: pgtype.Int4{
|
|
||||||
Int32: parentID,
|
|
||||||
Valid: true,
|
|
||||||
},
|
|
||||||
ParentPath: pgtype.Text{
|
|
||||||
String: path,
|
|
||||||
Valid: true,
|
|
||||||
},
|
|
||||||
Sort: pgtype.Int4{
|
|
||||||
Int32: sort,
|
|
||||||
Valid: true,
|
|
||||||
},
|
|
||||||
Status: pgtype.Int2{
|
|
||||||
Int16: status,
|
|
||||||
Valid: true,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
_, err := h.biz.SystemV1().CategoryBiz().Update(ctx, arg)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.render.JSONERR(w, err.Error())
|
h.render.JSONERR(w, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
h.render.JSONOK(w, "更新成功")
|
h.render.JSONOK(w, "更新成功")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
12
internal/erpserver/model/form/category.go
Normal file
12
internal/erpserver/model/form/category.go
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package form
|
||||||
|
|
||||||
|
type Category struct {
|
||||||
|
ID *int32 `form:"id" binding:"required"`
|
||||||
|
Name string `form:"name" binding:"required"`
|
||||||
|
ParentID *int32 `form:"parent_id" binding:"required"`
|
||||||
|
Icon string `form:"File"`
|
||||||
|
Description string `form:"description"`
|
||||||
|
Letter string `form:"letter"`
|
||||||
|
Sort *int32 `form:"sort"`
|
||||||
|
Status *int16 `form:"status" binding:"required"`
|
||||||
|
}
|
||||||
@ -4,7 +4,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"management/internal/config"
|
"management/internal/pkg/config"
|
||||||
|
|
||||||
"github.com/natefinch/lumberjack"
|
"github.com/natefinch/lumberjack"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
|
|||||||
@ -44,7 +44,7 @@ func RandomString(n int) string {
|
|||||||
var sb strings.Builder
|
var sb strings.Builder
|
||||||
k := len(alphabet)
|
k := len(alphabet)
|
||||||
|
|
||||||
for i := 0; i < n; i++ {
|
for range n {
|
||||||
c := alphabet[randv2.IntN(k)]
|
c := alphabet[randv2.IntN(k)]
|
||||||
sb.WriteByte(c)
|
sb.WriteByte(c)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,7 +8,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"management/internal/config"
|
"management/internal/pkg/config"
|
||||||
|
|
||||||
"github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
)
|
)
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import (
|
|||||||
"io/fs"
|
"io/fs"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"management/internal/config"
|
"management/internal/pkg/config"
|
||||||
|
|
||||||
"github.com/hirochachacha/go-smb2"
|
"github.com/hirochachacha/go-smb2"
|
||||||
)
|
)
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"management/internal/config"
|
"management/internal/pkg/config"
|
||||||
|
|
||||||
"github.com/aead/chacha20poly1305"
|
"github.com/aead/chacha20poly1305"
|
||||||
"github.com/o1egl/paseto"
|
"github.com/o1egl/paseto"
|
||||||
|
|||||||
BIN
management
BIN
management
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user