后端重构
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
package example
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"kra/internal/biz/example"
|
||||
"kra/internal/data/model"
|
||||
"kra/internal/data/query"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type attachmentCategoryRepo struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewAttachmentCategoryRepo 创建附件分类仓储
|
||||
func NewAttachmentCategoryRepo(db *gorm.DB) example.AttachmentCategoryRepo {
|
||||
return &attachmentCategoryRepo{db: db}
|
||||
}
|
||||
|
||||
func (r *attachmentCategoryRepo) Create(ctx context.Context, category *example.AttachmentCategory) error {
|
||||
m := &model.ExaAttachmentCategory{
|
||||
Name: category.Name,
|
||||
Pid: int64(category.Pid),
|
||||
}
|
||||
return r.db.WithContext(ctx).Create(m).Error
|
||||
}
|
||||
|
||||
func (r *attachmentCategoryRepo) Update(ctx context.Context, category *example.AttachmentCategory) error {
|
||||
return r.db.WithContext(ctx).Model(&model.ExaAttachmentCategory{}).
|
||||
Where("id = ?", category.ID).
|
||||
Updates(map[string]any{
|
||||
"name": category.Name,
|
||||
"pid": category.Pid,
|
||||
}).Error
|
||||
}
|
||||
|
||||
func (r *attachmentCategoryRepo) Delete(ctx context.Context, id uint) error {
|
||||
return r.db.WithContext(ctx).Unscoped().
|
||||
Where("id = ?", id).
|
||||
Delete(&model.ExaAttachmentCategory{}).Error
|
||||
}
|
||||
|
||||
func (r *attachmentCategoryRepo) FindByID(ctx context.Context, id uint) (*example.AttachmentCategory, error) {
|
||||
c := query.ExaAttachmentCategory
|
||||
m, err := c.WithContext(ctx).Where(c.ID.Eq(int64(id))).First()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return toCategoryBiz(m), nil
|
||||
}
|
||||
|
||||
func (r *attachmentCategoryRepo) FindByNameAndPid(ctx context.Context, name string, pid uint) (*example.AttachmentCategory, error) {
|
||||
c := query.ExaAttachmentCategory
|
||||
m, err := c.WithContext(ctx).Where(c.Name.Eq(name), c.Pid.Eq(int64(pid))).First()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return toCategoryBiz(m), nil
|
||||
}
|
||||
|
||||
func (r *attachmentCategoryRepo) FindAll(ctx context.Context) ([]*example.AttachmentCategory, error) {
|
||||
c := query.ExaAttachmentCategory
|
||||
list, err := c.WithContext(ctx).Find()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := make([]*example.AttachmentCategory, len(list))
|
||||
for i, m := range list {
|
||||
result[i] = toCategoryBiz(m)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r *attachmentCategoryRepo) HasChildren(ctx context.Context, id uint) (bool, error) {
|
||||
c := query.ExaAttachmentCategory
|
||||
count, err := c.WithContext(ctx).Where(c.Pid.Eq(int64(id))).Count()
|
||||
return count > 0, err
|
||||
}
|
||||
|
||||
// 转换函数
|
||||
func toCategoryBiz(m *model.ExaAttachmentCategory) *example.AttachmentCategory {
|
||||
return &example.AttachmentCategory{
|
||||
ID: uint(m.ID),
|
||||
Name: m.Name,
|
||||
Pid: uint(m.Pid),
|
||||
CreatedAt: m.CreatedAt,
|
||||
UpdatedAt: m.UpdatedAt,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package example
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"kra/internal/biz/example"
|
||||
"kra/internal/data/model"
|
||||
"kra/internal/data/query"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type customerRepo struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewCustomerRepo 创建客户仓储
|
||||
func NewCustomerRepo(db *gorm.DB) example.CustomerRepo {
|
||||
return &customerRepo{db: db}
|
||||
}
|
||||
|
||||
func (r *customerRepo) Create(ctx context.Context, customer *example.Customer) error {
|
||||
m := toCustomerModel(customer)
|
||||
return r.db.WithContext(ctx).Create(m).Error
|
||||
}
|
||||
|
||||
func (r *customerRepo) Update(ctx context.Context, customer *example.Customer) error {
|
||||
return r.db.WithContext(ctx).Save(toCustomerModel(customer)).Error
|
||||
}
|
||||
|
||||
func (r *customerRepo) Delete(ctx context.Context, id uint) error {
|
||||
return r.db.WithContext(ctx).Delete(&model.ExaCustomer{}, id).Error
|
||||
}
|
||||
|
||||
func (r *customerRepo) FindByID(ctx context.Context, id uint) (*example.Customer, error) {
|
||||
c := query.ExaCustomer
|
||||
m, err := c.WithContext(ctx).Where(c.ID.Eq(int64(id))).First()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return toCustomerBiz(m), nil
|
||||
}
|
||||
|
||||
func (r *customerRepo) List(ctx context.Context, authorityIds []uint, page, pageSize int) ([]*example.Customer, int64, error) {
|
||||
c := query.ExaCustomer
|
||||
q := c.WithContext(ctx)
|
||||
|
||||
if len(authorityIds) > 0 {
|
||||
ids := make([]int64, len(authorityIds))
|
||||
for i, id := range authorityIds {
|
||||
ids[i] = int64(id)
|
||||
}
|
||||
q = q.Where(c.SysUserAuthorityID.In(ids...))
|
||||
}
|
||||
|
||||
total, err := q.Count()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
offset := (page - 1) * pageSize
|
||||
list, err := q.Offset(offset).Limit(pageSize).Find()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
result := make([]*example.Customer, len(list))
|
||||
for i, m := range list {
|
||||
result[i] = toCustomerBiz(m)
|
||||
}
|
||||
return result, total, nil
|
||||
}
|
||||
|
||||
// 转换函数
|
||||
func toCustomerModel(c *example.Customer) *model.ExaCustomer {
|
||||
return &model.ExaCustomer{
|
||||
ID: int64(c.ID),
|
||||
CustomerName: c.CustomerName,
|
||||
CustomerPhoneData: c.CustomerPhoneData,
|
||||
SysUserID: int64(c.SysUserID),
|
||||
SysUserAuthorityID: int64(c.SysUserAuthorityID),
|
||||
}
|
||||
}
|
||||
|
||||
func toCustomerBiz(m *model.ExaCustomer) *example.Customer {
|
||||
return &example.Customer{
|
||||
ID: uint(m.ID),
|
||||
CustomerName: m.CustomerName,
|
||||
CustomerPhoneData: m.CustomerPhoneData,
|
||||
SysUserID: uint(m.SysUserID),
|
||||
SysUserAuthorityID: uint(m.SysUserAuthorityID),
|
||||
CreatedAt: m.CreatedAt,
|
||||
UpdatedAt: m.UpdatedAt,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
package example
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"kra/internal/biz/example"
|
||||
"kra/internal/data/model"
|
||||
"kra/internal/data/query"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type fileUploadRepo struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewFileUploadRepo 创建文件上传仓储
|
||||
func NewFileUploadRepo(db *gorm.DB) example.FileUploadRepo {
|
||||
return &fileUploadRepo{db: db}
|
||||
}
|
||||
|
||||
func (r *fileUploadRepo) Create(ctx context.Context, file *example.FileUpload) error {
|
||||
m := toFileUploadModel(file)
|
||||
return r.db.WithContext(ctx).Create(m).Error
|
||||
}
|
||||
|
||||
func (r *fileUploadRepo) Update(ctx context.Context, file *example.FileUpload) error {
|
||||
return r.db.WithContext(ctx).Model(&model.ExaFileUploadAndDownload{}).
|
||||
Where("id = ?", file.ID).
|
||||
Updates(map[string]any{
|
||||
"name": file.Name,
|
||||
"class_id": file.ClassId,
|
||||
}).Error
|
||||
}
|
||||
|
||||
func (r *fileUploadRepo) Delete(ctx context.Context, id uint) error {
|
||||
return r.db.WithContext(ctx).Unscoped().
|
||||
Where("id = ?", id).
|
||||
Delete(&model.ExaFileUploadAndDownload{}).Error
|
||||
}
|
||||
|
||||
func (r *fileUploadRepo) FindByID(ctx context.Context, id uint) (*example.FileUpload, error) {
|
||||
f := query.ExaFileUploadAndDownload
|
||||
m, err := f.WithContext(ctx).Where(f.ID.Eq(int64(id))).First()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return toFileUploadBiz(m), nil
|
||||
}
|
||||
|
||||
func (r *fileUploadRepo) FindByKey(ctx context.Context, key string) (*example.FileUpload, error) {
|
||||
f := query.ExaFileUploadAndDownload
|
||||
m, err := f.WithContext(ctx).Where(f.Key.Eq(key)).First()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return toFileUploadBiz(m), nil
|
||||
}
|
||||
|
||||
func (r *fileUploadRepo) List(ctx context.Context, req *example.FileUploadSearchReq) ([]*example.FileUpload, int64, error) {
|
||||
f := query.ExaFileUploadAndDownload
|
||||
q := f.WithContext(ctx)
|
||||
|
||||
if req.Keyword != "" {
|
||||
q = q.Where(f.Name.Like("%" + req.Keyword + "%"))
|
||||
}
|
||||
if req.ClassId > 0 {
|
||||
q = q.Where(f.ClassID.Eq(int64(req.ClassId)))
|
||||
}
|
||||
|
||||
total, err := q.Count()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
offset := (req.Page - 1) * req.PageSize
|
||||
list, err := q.Order(f.ID.Desc()).Offset(offset).Limit(req.PageSize).Find()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
result := make([]*example.FileUpload, len(list))
|
||||
for i, m := range list {
|
||||
result[i] = toFileUploadBiz(m)
|
||||
}
|
||||
return result, total, nil
|
||||
}
|
||||
|
||||
func (r *fileUploadRepo) BatchCreate(ctx context.Context, files []*example.FileUpload) error {
|
||||
models := make([]*model.ExaFileUploadAndDownload, len(files))
|
||||
for i, f := range files {
|
||||
models[i] = toFileUploadModel(f)
|
||||
}
|
||||
return r.db.WithContext(ctx).Create(&models).Error
|
||||
}
|
||||
|
||||
// 转换函数
|
||||
func toFileUploadModel(f *example.FileUpload) *model.ExaFileUploadAndDownload {
|
||||
return &model.ExaFileUploadAndDownload{
|
||||
ID: int64(f.ID),
|
||||
Name: f.Name,
|
||||
ClassID: int64(f.ClassId),
|
||||
URL: f.Url,
|
||||
Tag: f.Tag,
|
||||
Key: f.Key,
|
||||
}
|
||||
}
|
||||
|
||||
func toFileUploadBiz(m *model.ExaFileUploadAndDownload) *example.FileUpload {
|
||||
return &example.FileUpload{
|
||||
ID: uint(m.ID),
|
||||
Name: m.Name,
|
||||
ClassId: int(m.ClassID),
|
||||
Url: m.URL,
|
||||
Tag: m.Tag,
|
||||
Key: m.Key,
|
||||
CreatedAt: m.CreatedAt,
|
||||
UpdatedAt: m.UpdatedAt,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user