feat: 完善 dictionary_detail 相关功能。

main
NoahLan 1 year ago
parent 8d19325240
commit de2a4ad976

@ -53,6 +53,14 @@ const (
DictionaryNameInUsed DictionaryNameInUsed
) )
// DictionaryDetail
const (
DictionaryDetailPIDNotEmpty code.Code = iota + 1700
DictionaryDetailKeyNotEmpty
DictionaryDetailExists
DictionaryDetailIdNotEmpty
)
func init() { func init() {
// Common // Common
msg.Add(WrongCode, "auth.wrongCode") msg.Add(WrongCode, "auth.wrongCode")
@ -83,4 +91,14 @@ func init() {
// UserSocial // UserSocial
msg.Add(UserSocialUserIdNotEmpty, "userSocial.userIdNotEmpty") msg.Add(UserSocialUserIdNotEmpty, "userSocial.userIdNotEmpty")
// Dictionary
msg.Add(DictionaryNameNotEmpty, "dictionary.nameNotEmpty")
msg.Add(DictionaryNameInUsed, "dictionary.nameInUsed")
// DictionaryDetail
msg.Add(DictionaryDetailPIDNotEmpty, "dictionary.detail.pidNotEmpty")
msg.Add(DictionaryDetailKeyNotEmpty, "dictionary.detail.keyNotEmpty")
msg.Add(DictionaryDetailExists, "dictionary.detail.exists")
msg.Add(DictionaryDetailIdNotEmpty, "dictionary.detail.idNotEmpty")
} }

@ -50,5 +50,15 @@
}, },
"userSocial": { "userSocial": {
"userIdNotEmpty": "用户ID不能为空" "userIdNotEmpty": "用户ID不能为空"
},
"dictionary": {
"nameNotEmpty": "字典名不能为空",
"nameInUsed": "字典名已被使用",
"detail": {
"pidNotEmpty": "字典ID不能为空",
"keyNotEmpty": "键Key不能为空",
"exists": "字典已存在",
"idNotEmpty": "主键ID不能为空"
}
} }
} }

@ -85,6 +85,23 @@ var (
OnDelete: schema.Cascade, OnDelete: schema.Cascade,
}, },
}, },
Indexes: []*schema.Index{
{
Name: "dictionarydetail_dictionary_id_key",
Unique: true,
Columns: []*schema.Column{SysDictionaryDetailsColumns[7], SysDictionaryDetailsColumns[5]},
},
{
Name: "dictionarydetail_key",
Unique: false,
Columns: []*schema.Column{SysDictionaryDetailsColumns[5]},
},
{
Name: "dictionarydetail_title",
Unique: false,
Columns: []*schema.Column{SysDictionaryDetailsColumns[4]},
},
},
} }
// SysDistrictColumns holds the columns for the "sys_district" table. // SysDistrictColumns holds the columns for the "sys_district" table.
SysDistrictColumns = []*schema.Column{ SysDistrictColumns = []*schema.Column{

@ -6,6 +6,7 @@ import (
"entgo.io/ent/schema" "entgo.io/ent/schema"
"entgo.io/ent/schema/edge" "entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field" "entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
"git.noahlan.cn/noahlan/ntool-biz/core/orm/nent/mixins" "git.noahlan.cn/noahlan/ntool-biz/core/orm/nent/mixins"
"git.noahlan.cn/noahlan/ntool/nrandom" "git.noahlan.cn/noahlan/ntool/nrandom"
) )
@ -46,6 +47,16 @@ func (DictionaryDetail) Edges() []ent.Edge {
} }
} }
func (DictionaryDetail) Indexes() []ent.Index {
return []ent.Index{
// 唯一索引
index.Fields("dictionary_id", "key").Unique(),
// 查询优化索引
index.Fields("key"),
index.Fields("title"),
}
}
func (DictionaryDetail) Annotations() []schema.Annotation { func (DictionaryDetail) Annotations() []schema.Annotation {
return []schema.Annotation{ return []schema.Annotation{
entsql.WithComments(true), entsql.WithComments(true),

@ -2,6 +2,15 @@ package dictionary
import ( import (
"context" "context"
"git.noahlan.cn/n-admin/n-admin-server/dal/errx"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/dictionarydetail"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/utils/entx"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/utils/hander"
"git.noahlan.cn/noahlan/ntool-biz/core/nstatus"
"git.noahlan.cn/noahlan/ntool-biz/core/nstatus/code"
"git.noahlan.cn/noahlan/ntool-biz/core/nstatus/msg"
"git.noahlan.cn/noahlan/ntool-biz/core/orm/nent/types"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc" "git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core" "git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core"
@ -21,7 +30,63 @@ func NewCreateDictionaryDetailLogic(ctx context.Context, svcCtx *svc.ServiceCont
// Details // Details
func (l *CreateDictionaryDetailLogic) CreateDictionaryDetail(in *core.DictionaryDetailInfo) (*core.BaseIDResp, error) { func (l *CreateDictionaryDetailLogic) CreateDictionaryDetail(in *core.DictionaryDetailInfo) (*core.BaseIDResp, error) {
// todo: add your logic here and delete this line var dbData *ent.DictionaryDetail
if err := entx.WithTx(l.ctx, l.svcCtx.DB, func(tx *ent.Tx) error {
var err error
if err := l.preCheck(tx, in); err != nil {
return err
}
// query last sort
var maxStruct struct {
Max int
}
err = tx.DictionaryDetail.Query().
Select(dictionarydetail.FieldSort).
Where(dictionarydetail.DictionaryIDEQ(in.DictionaryId)).
Aggregate(ent.Max(dictionarydetail.FieldSort)).
Scan(l.ctx, &maxStruct)
if err != nil {
return hander.HandleEntErr(err, in)
}
return &core.BaseIDResp{}, nil // create
create := tx.DictionaryDetail.Create()
create.SetStatus(types.ParseStatus(in.Status)).
SetTitle(in.Title).
SetKey(in.Key).
SetValue(in.Value).
SetDictionaryID(in.DictionaryId).
SetSort(uint32(maxStruct.Max + 1))
dbData, err = create.Save(l.ctx)
if err != nil {
return hander.HandleEntErr(err, in)
}
return nil
}); err != nil {
return nil, err
}
return &core.BaseIDResp{
ID: dbData.ID,
Code: code.StatusOK,
Msg: msg.CreateSuccess,
}, nil
}
func (l *CreateDictionaryDetailLogic) preCheck(tx *ent.Tx, in *core.DictionaryDetailInfo) error {
if in.DictionaryId == 0 {
return nstatus.NewBizErrWithCode(errx.DictionaryDetailPIDNotEmpty)
}
if in.Key == "" {
return nstatus.NewBizErrWithCode(errx.DictionaryDetailKeyNotEmpty)
}
exist, err := tx.DictionaryDetail.Query().
Where(dictionarydetail.KeyEQ(in.Key), dictionarydetail.DictionaryIDEQ(in.DictionaryId)).
Exist(l.ctx)
if err != nil {
return hander.HandleEntErr(err, in)
}
if exist {
return nstatus.NewBizErrWithCode(errx.DictionaryDetailExists)
}
return nil
} }

@ -2,6 +2,11 @@ package dictionary
import ( import (
"context" "context"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/dictionarydetail"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/predicate"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/utils/hander"
"git.noahlan.cn/noahlan/ntool-biz/core/nstatus/code"
"git.noahlan.cn/noahlan/ntool-biz/core/nstatus/msg"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc" "git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core" "git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core"
@ -20,7 +25,23 @@ func NewDeleteDictionaryDetailLogic(ctx context.Context, svcCtx *svc.ServiceCont
} }
func (l *DeleteDictionaryDetailLogic) DeleteDictionaryDetail(in *core.DeleteDictionaryDetailReq) (*core.BaseResp, error) { func (l *DeleteDictionaryDetailLogic) DeleteDictionaryDetail(in *core.DeleteDictionaryDetailReq) (*core.BaseResp, error) {
// todo: add your logic here and delete this line var predicates []predicate.DictionaryDetail
if in.DictId != 0 {
predicates = append(predicates, dictionarydetail.DictionaryIDEQ(in.DictId))
goto Delete
}
if len(in.Ids) > 0 {
predicates = append(predicates, dictionarydetail.IDIn(in.Ids...))
}
Delete:
_, err := l.svcCtx.DB.DictionaryDetail.Delete().Where(predicates...).Exec(l.ctx)
if err != nil {
return nil, hander.HandleEntErr(err, in)
}
return &core.BaseResp{}, nil return &core.BaseResp{
Code: code.StatusOK,
Msg: msg.DeleteSuccess,
}, nil
} }

@ -2,6 +2,11 @@ package dictionary
import ( import (
"context" "context"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/dictionarydetail"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/predicate"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/utils"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/utils/hander"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc" "git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core" "git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core"
@ -20,7 +25,50 @@ func NewGetDictionaryDetailListLogic(ctx context.Context, svcCtx *svc.ServiceCon
} }
func (l *GetDictionaryDetailListLogic) GetDictionaryDetailList(in *core.DictionaryDetailReq) (*core.DictionaryDetailListResp, error) { func (l *GetDictionaryDetailListLogic) GetDictionaryDetailList(in *core.DictionaryDetailReq) (*core.DictionaryDetailListResp, error) {
// todo: add your logic here and delete this line var predicates []predicate.DictionaryDetail
if in.DictId != 0 {
predicates = append(predicates, dictionarydetail.DictionaryIDEQ(in.DictId))
}
if in.Title != "" {
predicates = append(predicates, dictionarydetail.TitleContains(in.Title))
}
if in.Key != "" {
predicates = append(predicates, dictionarydetail.KeyContains(in.Key))
}
if in.Value != "" {
predicates = append(predicates, dictionarydetail.ValueEQ(in.Value))
}
return &core.DictionaryDetailListResp{}, nil query := l.svcCtx.DB.DictionaryDetail.Query().Where(predicates...)
if in.WithDictionary {
query.WithDictionary()
}
var (
pagination *core.Pagination
list []*ent.DictionaryDetail
err error
)
if in.Page != nil {
result, pageErr := query.Page(l.ctx, in.Page.Current, in.Page.Size)
if pageErr == nil {
pagination = utils.ConvertPaginationDb(result.PageDetails)
list = result.List
} else {
err = pageErr
}
} else {
list, err = query.All(l.ctx)
}
if err != nil {
return nil, hander.HandleEntErr(err, in)
}
resp := &core.DictionaryDetailListResp{
Page: pagination,
Data: make([]*core.DictionaryDetailInfo, len(list)),
}
for i, v := range list {
resp.Data[i] = l.svcCtx.Convert.ConvertDictionaryDetailRpc(l.ctx, v)
}
return resp, nil
} }

@ -2,6 +2,9 @@ package dictionary
import ( import (
"context" "context"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/dictionarydetail"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/predicate"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/utils/hander"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc" "git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core" "git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core"
@ -20,7 +23,34 @@ func NewGetDictionaryDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext
} }
func (l *GetDictionaryDetailLogic) GetDictionaryDetail(in *core.DictionaryDetailReq) (*core.DictionaryDetailInfo, error) { func (l *GetDictionaryDetailLogic) GetDictionaryDetail(in *core.DictionaryDetailReq) (*core.DictionaryDetailInfo, error) {
// todo: add your logic here and delete this line var predicates []predicate.DictionaryDetail
if in.ID != 0 {
return &core.DictionaryDetailInfo{}, nil predicates = append(predicates, dictionarydetail.IDEQ(in.ID))
goto Query
}
if in.DictId != 0 {
predicates = append(predicates, dictionarydetail.DictionaryIDEQ(in.DictId))
}
if in.Title != "" {
predicates = append(predicates, dictionarydetail.TitleContains(in.Title))
}
if in.Key != "" {
predicates = append(predicates, dictionarydetail.KeyEQ(in.Key))
}
if in.Value != "" {
predicates = append(predicates, dictionarydetail.ValueEQ(in.Value))
}
if len(predicates) == 0 {
return nil, nil
}
Query:
query := l.svcCtx.DB.DictionaryDetail.Query().Where(predicates...)
if in.WithDictionary {
query.WithDictionary()
}
dbData, err := query.First(l.ctx)
if err != nil {
return nil, hander.HandleEntErr(err, in)
}
return l.svcCtx.Convert.ConvertDictionaryDetailRpc(l.ctx, dbData), nil
} }

@ -2,6 +2,15 @@ package dictionary
import ( import (
"context" "context"
"git.noahlan.cn/n-admin/n-admin-server/dal/errx"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/dictionarydetail"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/utils/entx"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/utils/hander"
"git.noahlan.cn/noahlan/ntool-biz/core/nstatus"
"git.noahlan.cn/noahlan/ntool-biz/core/nstatus/code"
"git.noahlan.cn/noahlan/ntool-biz/core/nstatus/msg"
"git.noahlan.cn/noahlan/ntool-biz/core/orm/nent/types"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc" "git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core" "git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core"
@ -20,7 +29,54 @@ func NewUpdateDictionaryDetailLogic(ctx context.Context, svcCtx *svc.ServiceCont
} }
func (l *UpdateDictionaryDetailLogic) UpdateDictionaryDetail(in *core.DictionaryDetailInfo) (*core.BaseResp, error) { func (l *UpdateDictionaryDetailLogic) UpdateDictionaryDetail(in *core.DictionaryDetailInfo) (*core.BaseResp, error) {
// todo: add your logic here and delete this line if err := entx.WithTx(l.ctx, l.svcCtx.DB, func(tx *ent.Tx) error {
var err error
if err := l.preCheck(tx, in); err != nil {
return err
}
update := tx.DictionaryDetail.Update()
update.SetNotEmptyTitle(in.Title).
SetNotEmptyKey(in.Key).
SetValue(in.Value)
if in.Sort != 0 {
update.SetSort(in.Sort)
}
if in.Status != "" {
update.SetStatus(types.ParseStatus(in.Status))
}
if in.DictionaryId != 0 {
update.SetDictionaryID(in.DictionaryId)
}
err = update.Exec(l.ctx)
if err != nil {
return hander.HandleEntErr(err, in)
}
return nil
}); err != nil {
return nil, err
}
return &core.BaseResp{
Code: code.StatusOK,
Msg: msg.UpdateSuccess,
}, nil
}
return &core.BaseResp{}, nil func (l *UpdateDictionaryDetailLogic) preCheck(tx *ent.Tx, in *core.DictionaryDetailInfo) error {
if in.ID == 0 {
return nstatus.NewBizErrWithCode(errx.DictionaryDetailIdNotEmpty)
}
if in.Key == "" {
return nstatus.NewBizErrWithCode(errx.DictionaryDetailKeyNotEmpty)
}
exist, err := tx.DictionaryDetail.Query().
Where(dictionarydetail.KeyEQ(in.Key), dictionarydetail.IDEQ(in.ID)).
Exist(l.ctx)
if err != nil {
return hander.HandleEntErr(err, in)
}
if exist {
return nstatus.NewBizErrWithCode(errx.DictionaryDetailExists)
}
return nil
} }

Loading…
Cancel
Save