diff --git a/dal/errx/errx.go b/dal/errx/errx.go index dcb0b0c..3fcaff4 100644 --- a/dal/errx/errx.go +++ b/dal/errx/errx.go @@ -53,6 +53,14 @@ const ( DictionaryNameInUsed ) +// DictionaryDetail +const ( + DictionaryDetailPIDNotEmpty code.Code = iota + 1700 + DictionaryDetailKeyNotEmpty + DictionaryDetailExists + DictionaryDetailIdNotEmpty +) + func init() { // Common msg.Add(WrongCode, "auth.wrongCode") @@ -83,4 +91,14 @@ func init() { // UserSocial 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") } diff --git a/dal/i18n/locale/zh.json b/dal/i18n/locale/zh.json index cb88aba..39ef572 100644 --- a/dal/i18n/locale/zh.json +++ b/dal/i18n/locale/zh.json @@ -50,5 +50,15 @@ }, "userSocial": { "userIdNotEmpty": "用户ID不能为空" + }, + "dictionary": { + "nameNotEmpty": "字典名不能为空", + "nameInUsed": "字典名已被使用", + "detail": { + "pidNotEmpty": "字典ID不能为空", + "keyNotEmpty": "键(Key)不能为空", + "exists": "字典已存在", + "idNotEmpty": "主键(ID)不能为空" + } } } \ No newline at end of file diff --git a/rpc/core/ent/migrate/schema.go b/rpc/core/ent/migrate/schema.go index 9d4dd24..2fb9bce 100644 --- a/rpc/core/ent/migrate/schema.go +++ b/rpc/core/ent/migrate/schema.go @@ -85,6 +85,23 @@ var ( 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 = []*schema.Column{ diff --git a/rpc/core/ent/schema/dictionary_detail.go b/rpc/core/ent/schema/dictionary_detail.go index 1e204b6..6a09a2e 100644 --- a/rpc/core/ent/schema/dictionary_detail.go +++ b/rpc/core/ent/schema/dictionary_detail.go @@ -6,6 +6,7 @@ import ( "entgo.io/ent/schema" "entgo.io/ent/schema/edge" "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/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 { return []schema.Annotation{ entsql.WithComments(true), diff --git a/rpc/core/internal/logic/dictionary/create_dictionary_detail_logic.go b/rpc/core/internal/logic/dictionary/create_dictionary_detail_logic.go index b5f2c6d..e3528a5 100644 --- a/rpc/core/internal/logic/dictionary/create_dictionary_detail_logic.go +++ b/rpc/core/internal/logic/dictionary/create_dictionary_detail_logic.go @@ -2,6 +2,15 @@ package dictionary import ( "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/types/core" @@ -21,7 +30,63 @@ func NewCreateDictionaryDetailLogic(ctx context.Context, svcCtx *svc.ServiceCont // Details 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 } diff --git a/rpc/core/internal/logic/dictionary/delete_dictionary_detail_logic.go b/rpc/core/internal/logic/dictionary/delete_dictionary_detail_logic.go index c0c086d..b41d673 100644 --- a/rpc/core/internal/logic/dictionary/delete_dictionary_detail_logic.go +++ b/rpc/core/internal/logic/dictionary/delete_dictionary_detail_logic.go @@ -2,6 +2,11 @@ package dictionary import ( "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/types/core" @@ -20,7 +25,23 @@ func NewDeleteDictionaryDetailLogic(ctx context.Context, svcCtx *svc.ServiceCont } 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 } diff --git a/rpc/core/internal/logic/dictionary/get_dictionary_detail_list_logic.go b/rpc/core/internal/logic/dictionary/get_dictionary_detail_list_logic.go index f68790c..f254b40 100644 --- a/rpc/core/internal/logic/dictionary/get_dictionary_detail_list_logic.go +++ b/rpc/core/internal/logic/dictionary/get_dictionary_detail_list_logic.go @@ -2,6 +2,11 @@ package dictionary import ( "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/types/core" @@ -20,7 +25,50 @@ func NewGetDictionaryDetailListLogic(ctx context.Context, svcCtx *svc.ServiceCon } 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)) + } + + 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)), + } - return &core.DictionaryDetailListResp{}, nil + for i, v := range list { + resp.Data[i] = l.svcCtx.Convert.ConvertDictionaryDetailRpc(l.ctx, v) + } + return resp, nil } diff --git a/rpc/core/internal/logic/dictionary/get_dictionary_detail_logic.go b/rpc/core/internal/logic/dictionary/get_dictionary_detail_logic.go index a040d23..aea9cae 100644 --- a/rpc/core/internal/logic/dictionary/get_dictionary_detail_logic.go +++ b/rpc/core/internal/logic/dictionary/get_dictionary_detail_logic.go @@ -2,6 +2,9 @@ package dictionary import ( "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/types/core" @@ -20,7 +23,34 @@ func NewGetDictionaryDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext } func (l *GetDictionaryDetailLogic) GetDictionaryDetail(in *core.DictionaryDetailReq) (*core.DictionaryDetailInfo, error) { - // todo: add your logic here and delete this line - - return &core.DictionaryDetailInfo{}, nil + var predicates []predicate.DictionaryDetail + if in.ID != 0 { + 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 } diff --git a/rpc/core/internal/logic/dictionary/update_dictionary_detail_logic.go b/rpc/core/internal/logic/dictionary/update_dictionary_detail_logic.go index 96de827..fa7743d 100644 --- a/rpc/core/internal/logic/dictionary/update_dictionary_detail_logic.go +++ b/rpc/core/internal/logic/dictionary/update_dictionary_detail_logic.go @@ -2,6 +2,15 @@ package dictionary import ( "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/types/core" @@ -20,7 +29,54 @@ func NewUpdateDictionaryDetailLogic(ctx context.Context, svcCtx *svc.ServiceCont } 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 + } - return &core.BaseResp{}, nil + 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 +} + +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 }