From 5a314f4cf82cd8ad7ab78d0e86eab730c72c28f5 Mon Sep 17 00:00:00 2001 From: jagger Date: Thu, 14 Mar 2024 10:41:06 +0800 Subject: [PATCH] :sparkles: Dictionary data lists support paging queries Signed-off-by: jagger Former-commit-id: bd79becb758037abb2338825453e13760cd15580 --- api/desc/core/pcm-core.api | 20 ++++++++ api/desc/pcm.api | 7 ++- .../dictionary/listdictitembycodehandler.go | 24 ++++++++++ api/internal/handler/routes.go | 5 ++ .../logic/dictionary/editdictitemlogic.go | 1 + .../logic/dictionary/editdictlogic.go | 1 + .../dictionary/listdictitembycodelogic.go | 41 +++++++++++++++++ .../logic/dictionary/listdictitemlogic.go | 46 +++++++++++++++---- .../logic/dictionary/listdictlogic.go | 40 ++++++++++++---- api/internal/types/types.go | 18 ++++++++ 10 files changed, 181 insertions(+), 22 deletions(-) create mode 100644 api/internal/handler/dictionary/listdictitembycodehandler.go create mode 100644 api/internal/logic/dictionary/listdictitembycodelogic.go diff --git a/api/desc/core/pcm-core.api b/api/desc/core/pcm-core.api index 87a531a6..82611a16 100644 --- a/api/desc/core/pcm-core.api +++ b/api/desc/core/pcm-core.api @@ -839,6 +839,7 @@ type ( Description string `form:"description,optional"` Type string `form:"type,optional"` Status string `form:"status,optional"` + PageInfo } DictEditReq { @@ -889,6 +890,7 @@ type ( Type string `form:"type,optional"` ParentId string `form:"parentId,optional"` Status string `form:"status,optional"` + PageInfo } DictItemEditReq { @@ -920,6 +922,10 @@ type ( DictItems { List []DictItemInfo `json:"list,omitempty"` } + + DictCodeReq { + DictCode string `path:"dictCode"` + } ) type ( @@ -930,4 +936,18 @@ type ( CIds { Ids []string `json:"ids,omitempty" validate:"required"` } +) + +type ( + PageInfo { + PageNum int `form:"pageNum"` + PageSize int `form:"pageSize"` + } + + PageResult { + List interface{} `json:"list,omitempty"` + Total int64 `json:"total,omitempty"` + PageNum int `json:"pageNum,omitempty"` + PageSize int `json:"pageSize,omitempty"` + } ) \ No newline at end of file diff --git a/api/desc/pcm.api b/api/desc/pcm.api index f082f3f2..465621dd 100644 --- a/api/desc/pcm.api +++ b/api/desc/pcm.api @@ -878,7 +878,7 @@ service pcm { get /dict/:id (CId) returns (DictResp) @handler ListDict - get /dicts (DictReq) returns (Dicts) + get /dicts (DictReq) returns (PageResult) @handler AddDict post /dict (DictEditReq) returns (DictResp) @@ -893,7 +893,7 @@ service pcm { get /dictItem/:id (CId) returns (DictItemResp) @handler ListDictItem - get /dictItems (DictItemReq) returns (DictItems) + get /dictItems (DictItemReq) returns (PageResult) @handler AddDictItem post /dictItem (DictItemEditReq) returns (DictItemResp) @@ -903,4 +903,7 @@ service pcm { @handler DeleteDictItem delete /dictItem/:id (CId) returns (DictItemResp) + + @handler ListDictItemByCode + get /dictItem/code/:dictCode (DictCodeReq) returns (PageResult) } \ No newline at end of file diff --git a/api/internal/handler/dictionary/listdictitembycodehandler.go b/api/internal/handler/dictionary/listdictitembycodehandler.go new file mode 100644 index 00000000..c6400185 --- /dev/null +++ b/api/internal/handler/dictionary/listdictitembycodehandler.go @@ -0,0 +1,24 @@ +package dictionary + +import ( + "github.com/zeromicro/go-zero/rest/httpx" + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/logic/dictionary" + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" + "net/http" +) + +func ListDictItemByCodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.DictCodeReq + if err := httpx.Parse(r, &req); err != nil { + result.ParamErrorResult(r, w, err) + return + } + + l := dictionary.NewListDictItemByCodeLogic(r.Context(), svcCtx) + resp, err := l.ListDictItemByCode(&req) + result.HttpResult(r, w, resp, err) + } +} diff --git a/api/internal/handler/routes.go b/api/internal/handler/routes.go index 73b25377..33f818f4 100644 --- a/api/internal/handler/routes.go +++ b/api/internal/handler/routes.go @@ -1130,6 +1130,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/dictItem/:id", Handler: dictionary.DeleteDictItemHandler(serverCtx), }, + { + Method: http.MethodGet, + Path: "/dictItem/code/:dictCode", + Handler: dictionary.ListDictItemByCodeHandler(serverCtx), + }, }, rest.WithPrefix("/pcm/v1"), ) diff --git a/api/internal/logic/dictionary/editdictitemlogic.go b/api/internal/logic/dictionary/editdictitemlogic.go index 1960a483..b09de93b 100644 --- a/api/internal/logic/dictionary/editdictitemlogic.go +++ b/api/internal/logic/dictionary/editdictitemlogic.go @@ -30,6 +30,7 @@ func (l *EditDictItemLogic) EditDictItem(req *types.DictItemEditReq) (resp *type dictItem := &types.DictItemInfo{} result := l.svcCtx.DbEngin.Table("t_dict_item").First(&dictItem, req.Id) if errors.Is(result.Error, gorm.ErrRecordNotFound) { + logx.Errorf("Dictionary data editing failure. errors: %s", err.Error()) return nil, errors.New("DictItem does not exist") } utils.Convert(req, &dictItem) diff --git a/api/internal/logic/dictionary/editdictlogic.go b/api/internal/logic/dictionary/editdictlogic.go index 26e6930b..a9323e29 100644 --- a/api/internal/logic/dictionary/editdictlogic.go +++ b/api/internal/logic/dictionary/editdictlogic.go @@ -31,6 +31,7 @@ func (l *EditDictLogic) EditDict(req *types.DictEditReq) (resp *types.DictResp, dict := &types.DictInfo{} result := l.svcCtx.DbEngin.Table("t_dict").First(&dict, req.Id) if errors.Is(result.Error, gorm.ErrRecordNotFound) { + logx.Errorf("Dictionary editing failure. errors: %s", err.Error()) return nil, errors.New("Dict does not exist") } utils.Convert(req, &dict) diff --git a/api/internal/logic/dictionary/listdictitembycodelogic.go b/api/internal/logic/dictionary/listdictitembycodelogic.go new file mode 100644 index 00000000..b8eb2331 --- /dev/null +++ b/api/internal/logic/dictionary/listdictitembycodelogic.go @@ -0,0 +1,41 @@ +package dictionary + +import ( + "context" + + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type ListDictItemByCodeLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewListDictItemByCodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListDictItemByCodeLogic { + return &ListDictItemByCodeLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *ListDictItemByCodeLogic) ListDictItemByCode(req *types.DictCodeReq) (resp *types.PageResult, err error) { + var dictList []types.DictItemInfo + resp = &types.PageResult{} + db := l.svcCtx.DbEngin.Model(&types.DictInfo{}).Table("t_dict") + + // 左连接查询 + db.Select("t_dict_item.*").Joins("left join t_dict_item on t_dict.id = t_dict_item.dict_id"). + Where("t_dict.dict_code = ?", req.DictCode). + Where("t_dict_item.status", 1). + Order("t_dict_item.sort_order").Scan(&dictList) + if err != nil { + return resp, err + } + resp.List = dictList + return resp, nil +} diff --git a/api/internal/logic/dictionary/listdictitemlogic.go b/api/internal/logic/dictionary/listdictitemlogic.go index 2179aeec..3c209ad8 100644 --- a/api/internal/logic/dictionary/listdictitemlogic.go +++ b/api/internal/logic/dictionary/listdictitemlogic.go @@ -2,8 +2,6 @@ package dictionary import ( "context" - "fmt" - "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" @@ -24,16 +22,44 @@ func NewListDictItemLogic(ctx context.Context, svcCtx *svc.ServiceContext) *List } } -func (l *ListDictItemLogic) ListDictItem(req *types.DictItemReq) (resp *types.DictItems, err error) { - resp = &types.DictItems{} - sql := fmt.Sprintf(`select c.* from t_dict_item c left join t_dict a on c.dict_id = a.id where c.deleted_at is null ORDER BY create_time Desc`) +func (l *ListDictItemLogic) ListDictItem(req *types.DictItemReq) (resp *types.PageResult, err error) { + limit := req.PageSize + offset := req.PageSize * (req.PageNum - 1) + resp = &types.PageResult{} + var dictList []types.DictItemInfo + db := l.svcCtx.DbEngin.Model(&types.DictItemInfo{}).Table("t_dict_item") + if req.ItemText != "" { - sql = fmt.Sprintf(`select c.* from t_dict_item c left join t_dict a on c.dict_id = a.id where c.deleted_at is null and c.item_text like '%%%s%%'`, req.ItemText) + db = db.Where("item_text LIKE ?", "%"+req.ItemText+"%") } - tx := l.svcCtx.DbEngin.Raw(sql).Scan(&resp.List) - if tx.Error != nil { - logx.Errorf(tx.Error.Error()) - return nil, tx.Error + if req.ItemValue != "" { + db = db.Where("item_value LIKE ?", "%"+req.ItemValue+"%") } + if req.Type != "" { + db = db.Where("type = ?", req.Type) + } + if req.ParentId != "" { + db = db.Where("parent_id = ?", req.ParentId) + } + if req.Status != "" { + db = db.Where("status = ?", req.Status) + } + if req.DictId != "" { + db = db.Where("dict_id = ?", req.DictId) + } + var total int64 + err = db.Count(&total).Error + + if err != nil { + return resp, err + } + db = db.Limit(limit).Offset(offset) + err = db.Order("create_time desc").Find(&dictList).Error + + resp.List = dictList + resp.PageSize = req.PageSize + resp.PageNum = req.PageNum + resp.Total = total + return resp, nil } diff --git a/api/internal/logic/dictionary/listdictlogic.go b/api/internal/logic/dictionary/listdictlogic.go index 15959cef..35122a3e 100644 --- a/api/internal/logic/dictionary/listdictlogic.go +++ b/api/internal/logic/dictionary/listdictlogic.go @@ -2,8 +2,6 @@ package dictionary import ( "context" - "fmt" - "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" @@ -24,16 +22,38 @@ func NewListDictLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListDict } } -func (l *ListDictLogic) ListDict(req *types.DictReq) (resp *types.Dicts, err error) { - resp = &types.Dicts{} - sqlStr := "select * from t_dict where `deleted_at` IS NULL ORDER BY create_time Desc" +func (l *ListDictLogic) ListDict(req *types.DictReq) (resp *types.PageResult, err error) { + limit := req.PageSize + offset := req.PageSize * (req.PageNum - 1) + resp = &types.PageResult{} + var dictList []types.DictInfo + db := l.svcCtx.DbEngin.Model(&types.DictInfo{}).Table("t_dict") + if req.DictName != "" { - sqlStr = fmt.Sprintf("select * from t_dict where `deleted_at` IS NULL and dict_name like '%%%s%%' ORDER BY create_time Desc", req.DictName) + db = db.Where("dict_name LIKE ?", "%"+req.DictName+"%") } - tx := l.svcCtx.DbEngin.Raw(sqlStr).Scan(&resp.List) - if tx.Error != nil { - logx.Errorf(tx.Error.Error()) - return nil, tx.Error + if req.DictCode != "" { + db = db.Where("dict_code LIKE ?", "%"+req.DictCode+"%") } + if req.Type != "" { + db = db.Where("type = ?", req.Type) + } + if req.Status != "" { + db = db.Where("status = ?", req.Status) + } + var total int64 + err = db.Count(&total).Error + + if err != nil { + return resp, err + } + db = db.Limit(limit).Offset(offset) + err = db.Order("create_time desc").Find(&dictList).Error + + resp.List = dictList + resp.PageSize = req.PageSize + resp.PageNum = req.PageNum + resp.Total = total + return resp, nil } diff --git a/api/internal/types/types.go b/api/internal/types/types.go index 361b4a4c..e51aa30b 100644 --- a/api/internal/types/types.go +++ b/api/internal/types/types.go @@ -805,6 +805,7 @@ type DictReq struct { Description string `form:"description,optional"` Type string `form:"type,optional"` Status string `form:"status,optional"` + PageInfo } type DictEditReq struct { @@ -854,6 +855,7 @@ type DictItemReq struct { Type string `form:"type,optional"` ParentId string `form:"parentId,optional"` Status string `form:"status,optional"` + PageInfo } type DictItemEditReq struct { @@ -886,6 +888,10 @@ type DictItems struct { List []DictItemInfo `json:"list,omitempty"` } +type DictCodeReq struct { + DictCode string `path:"dictCode"` +} + type CId struct { Id string `path:"id":"id,omitempty" validate:"required"` } @@ -894,6 +900,18 @@ type CIds struct { Ids []string `json:"ids,omitempty" validate:"required"` } +type PageInfo struct { + PageNum int `form:"pageNum"` + PageSize int `form:"pageSize"` +} + +type PageResult struct { + List interface{} `json:"list,omitempty"` + Total int64 `json:"total,omitempty"` + PageNum int `json:"pageNum,omitempty"` + PageSize int `json:"pageSize,omitempty"` +} + type Job struct { SlurmVersion string `json:"slurmVersion"` Name string `json:"name"`