From 83e45f26b2e1610ca99595444defcd0ddaab8618 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 14 Jun 2023 15:45:56 +0800 Subject: [PATCH] fix --- ddl/fs_map_library.sql | 12 +++ goctl_template/api/context.tpl | 2 +- model/fsmaplibrarymodel.go | 24 +++++ model/fsmaplibrarymodel_gen.go | 89 +++++++++++++++++++ model/gmodel/fsmaplibrarymodel.go | 31 +++++++ model/gmodel/fsproducttemplatetagsmodel.go | 16 +++- .../handler/getmaplibrarylisthandler.go | 10 +-- .../internal/logic/getmaplibrarylistlogic.go | 50 +++++++++++ .../internal/svc/servicecontext.go | 3 +- 9 files changed, 228 insertions(+), 9 deletions(-) create mode 100644 ddl/fs_map_library.sql create mode 100755 model/fsmaplibrarymodel.go create mode 100755 model/fsmaplibrarymodel_gen.go create mode 100755 model/gmodel/fsmaplibrarymodel.go diff --git a/ddl/fs_map_library.sql b/ddl/fs_map_library.sql new file mode 100644 index 00000000..1d1a10a3 --- /dev/null +++ b/ddl/fs_map_library.sql @@ -0,0 +1,12 @@ +-- fusentest.fs_map_library definition + +CREATE TABLE `fs_map_library` ( + `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Id', + `title` varchar(255) NOT NULL DEFAULT '' COMMENT '名称', + `info` text NOT NULL COMMENT '贴图数据', + `sort` smallint(5) NOT NULL DEFAULT '0' COMMENT '排序', + `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '状态 1启用', + `ctime` int(10) NOT NULL DEFAULT '0' COMMENT '创建时间', + `tag_id` int(10) NOT NULL DEFAULT '0' COMMENT '模板标签id', + PRIMARY KEY (`id`) USING BTREE +) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='贴图库'; \ No newline at end of file diff --git a/goctl_template/api/context.tpl b/goctl_template/api/context.tpl index 26bd8a5d..1bb0c6c5 100644 --- a/goctl_template/api/context.tpl +++ b/goctl_template/api/context.tpl @@ -19,7 +19,7 @@ func NewServiceContext(c {{.config}}) *ServiceContext { return &ServiceContext{ Config: c, - MysqlConn: initalize.InitMysql(c.DataSource), + MysqlConn: initalize.InitMysql(c.SourceMysql), {{.middlewareAssignment}} } } diff --git a/model/fsmaplibrarymodel.go b/model/fsmaplibrarymodel.go new file mode 100755 index 00000000..c3c7d0e7 --- /dev/null +++ b/model/fsmaplibrarymodel.go @@ -0,0 +1,24 @@ +package model + +import "github.com/zeromicro/go-zero/core/stores/sqlx" + +var _ FsMapLibraryModel = (*customFsMapLibraryModel)(nil) + +type ( + // FsMapLibraryModel is an interface to be customized, add more methods here, + // and implement the added methods in customFsMapLibraryModel. + FsMapLibraryModel interface { + fsMapLibraryModel + } + + customFsMapLibraryModel struct { + *defaultFsMapLibraryModel + } +) + +// NewFsMapLibraryModel returns a model for the database table. +func NewFsMapLibraryModel(conn sqlx.SqlConn) FsMapLibraryModel { + return &customFsMapLibraryModel{ + defaultFsMapLibraryModel: newFsMapLibraryModel(conn), + } +} diff --git a/model/fsmaplibrarymodel_gen.go b/model/fsmaplibrarymodel_gen.go new file mode 100755 index 00000000..2b662b7f --- /dev/null +++ b/model/fsmaplibrarymodel_gen.go @@ -0,0 +1,89 @@ +// Code generated by goctl. DO NOT EDIT. + +package model + +import ( + "context" + "database/sql" + "fmt" + "strings" + + "github.com/zeromicro/go-zero/core/stores/builder" + "github.com/zeromicro/go-zero/core/stores/sqlc" + "github.com/zeromicro/go-zero/core/stores/sqlx" + "github.com/zeromicro/go-zero/core/stringx" +) + +var ( + fsMapLibraryFieldNames = builder.RawFieldNames(&FsMapLibrary{}) + fsMapLibraryRows = strings.Join(fsMapLibraryFieldNames, ",") + fsMapLibraryRowsExpectAutoSet = strings.Join(stringx.Remove(fsMapLibraryFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",") + fsMapLibraryRowsWithPlaceHolder = strings.Join(stringx.Remove(fsMapLibraryFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?" +) + +type ( + fsMapLibraryModel interface { + Insert(ctx context.Context, data *FsMapLibrary) (sql.Result, error) + FindOne(ctx context.Context, id int64) (*FsMapLibrary, error) + Update(ctx context.Context, data *FsMapLibrary) error + Delete(ctx context.Context, id int64) error + } + + defaultFsMapLibraryModel struct { + conn sqlx.SqlConn + table string + } + + FsMapLibrary struct { + Id int64 `db:"id"` // Id + Title string `db:"title"` // 名称 + Info string `db:"info"` // 贴图数据 + Sort int64 `db:"sort"` // 排序 + Status int64 `db:"status"` // 状态 1启用 + Ctime int64 `db:"ctime"` // 创建时间 + TagId int64 `db:"tag_id"` // 模板标签id + } +) + +func newFsMapLibraryModel(conn sqlx.SqlConn) *defaultFsMapLibraryModel { + return &defaultFsMapLibraryModel{ + conn: conn, + table: "`fs_map_library`", + } +} + +func (m *defaultFsMapLibraryModel) Delete(ctx context.Context, id int64) error { + query := fmt.Sprintf("delete from %s where `id` = ?", m.table) + _, err := m.conn.ExecCtx(ctx, query, id) + return err +} + +func (m *defaultFsMapLibraryModel) FindOne(ctx context.Context, id int64) (*FsMapLibrary, error) { + query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", fsMapLibraryRows, m.table) + var resp FsMapLibrary + err := m.conn.QueryRowCtx(ctx, &resp, query, id) + switch err { + case nil: + return &resp, nil + case sqlc.ErrNotFound: + return nil, ErrNotFound + default: + return nil, err + } +} + +func (m *defaultFsMapLibraryModel) Insert(ctx context.Context, data *FsMapLibrary) (sql.Result, error) { + query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?)", m.table, fsMapLibraryRowsExpectAutoSet) + ret, err := m.conn.ExecCtx(ctx, query, data.Title, data.Info, data.Sort, data.Status, data.Ctime, data.TagId) + return ret, err +} + +func (m *defaultFsMapLibraryModel) Update(ctx context.Context, data *FsMapLibrary) error { + query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, fsMapLibraryRowsWithPlaceHolder) + _, err := m.conn.ExecCtx(ctx, query, data.Title, data.Info, data.Sort, data.Status, data.Ctime, data.TagId, data.Id) + return err +} + +func (m *defaultFsMapLibraryModel) tableName() string { + return m.table +} diff --git a/model/gmodel/fsmaplibrarymodel.go b/model/gmodel/fsmaplibrarymodel.go new file mode 100755 index 00000000..1084e333 --- /dev/null +++ b/model/gmodel/fsmaplibrarymodel.go @@ -0,0 +1,31 @@ +package gmodel + +import ( + "context" + "gorm.io/gorm" +) + +type FsMapLibrary struct { + Id int64 `gorm:"primary_key" json:"id"` // Id + Title *string `gorm:"default:''" json:"title"` // 名称 + Info *string `gorm:"default:''" json:"info"` // 贴图数据 + Sort *int64 `gorm:"default:0" json:"sort"` // 排序 + Status *int64 `gorm:"default:1" json:"status"` // 状态 1启用 0未启用 + Ctime *int64 `gorm:"default:0" json:"ctime"` // 创建时间 + TagId *int64 `gorm:"default:0" json:"tag_id"` // 模板标签id +} +type FsMapLibraryModel struct { + db *gorm.DB +} + +func NewFsMapLibraryModel(db *gorm.DB) *FsMapLibraryModel { + return &FsMapLibraryModel{db} +} + +func (ml *FsMapLibraryModel) GetAllEnabledList(ctx context.Context) (resp []FsMapLibrary, err error) { + err = ml.db.WithContext(ctx).Model(&FsMapLibrary{}).Where("`status` = ?", 1).Find(&resp).Error + if err != nil { + return nil, err + } + return +} diff --git a/model/gmodel/fsproducttemplatetagsmodel.go b/model/gmodel/fsproducttemplatetagsmodel.go index 22dc1ebd..81bc121e 100755 --- a/model/gmodel/fsproducttemplatetagsmodel.go +++ b/model/gmodel/fsproducttemplatetagsmodel.go @@ -1,6 +1,9 @@ package gmodel -import "gorm.io/gorm" +import ( + "context" + "gorm.io/gorm" +) type FsProductTemplateTags struct { Id int64 `gorm:"primary_key" json:"id"` // ID @@ -15,3 +18,14 @@ type FsProductTemplateTagsModel struct { func NewFsProductTemplateTagsModel(db *gorm.DB) *FsProductTemplateTagsModel { return &FsProductTemplateTagsModel{db} } + +func (pt *FsProductTemplateTagsModel) GetListByIds(ctx context.Context, ids []int64) (resp []FsProductTemplateTags, err error) { + if len(ids) == 0 { + return + } + err = pt.db.WithContext(ctx).Model(&FsProductTemplateTags{}).Where("`id` in (?)", ids).Find(&resp).Error + if err != nil { + return nil, err + } + return +} diff --git a/server/map_library/internal/handler/getmaplibrarylisthandler.go b/server/map_library/internal/handler/getmaplibrarylisthandler.go index f81221e0..bad5e65d 100644 --- a/server/map_library/internal/handler/getmaplibrarylisthandler.go +++ b/server/map_library/internal/handler/getmaplibrarylisthandler.go @@ -7,16 +7,14 @@ import ( "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/rest/httpx" - "fusenapi/utils/auth" - "fusenapi/utils/basic" - "fusenapi/server/map_library/internal/logic" "fusenapi/server/map_library/internal/svc" + "fusenapi/utils/auth" ) func GetMapLibraryListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - // 解析jwtToken + /*// 解析jwtToken claims, err := svcCtx.ParseJwtToken(r) // 如果解析出错,则返回未授权的JSON响应并记录错误消息 if err != nil { @@ -38,10 +36,10 @@ func GetMapLibraryListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { }) logx.Info("unauthorized:", err.Error()) return - } + }*/ l := logic.NewGetMapLibraryListLogic(r.Context(), svcCtx) - resp := l.GetMapLibraryList(userinfo) + resp := l.GetMapLibraryList(&auth.UserInfo{86}) // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; // 否则,发送500内部服务器错误的JSON响应并记录错误消息logx.Error。 if resp != nil { diff --git a/server/map_library/internal/logic/getmaplibrarylistlogic.go b/server/map_library/internal/logic/getmaplibrarylistlogic.go index 393f59b6..fc393b47 100644 --- a/server/map_library/internal/logic/getmaplibrarylistlogic.go +++ b/server/map_library/internal/logic/getmaplibrarylistlogic.go @@ -1,8 +1,12 @@ package logic import ( + "encoding/json" + "fusenapi/model/gmodel" + "fusenapi/server/map_library/internal/types" "fusenapi/utils/auth" "fusenapi/utils/basic" + "time" "context" @@ -25,5 +29,51 @@ func NewGetMapLibraryListLogic(ctx context.Context, svcCtx *svc.ServiceContext) } func (l *GetMapLibraryListLogic) GetMapLibraryList(userinfo *auth.UserInfo) (resp *basic.Response) { + mapLibraryModel := gmodel.NewFsMapLibraryModel(l.svcCtx.MysqlConn) + mapLibraryList, err := mapLibraryModel.GetAllEnabledList(l.ctx) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get map library list") + } + if len(mapLibraryList) == 0 { + return resp.SetStatus(basic.CodeOK) + } + productTemplateTagIds := make([]int64, 0, len(mapLibraryList)) + for _, v := range mapLibraryList { + productTemplateTagIds = append(productTemplateTagIds, *v.TagId) + } + //获取标签列表 + productTemplateTagsModel := gmodel.NewFsProductTemplateTagsModel(l.svcCtx.MysqlConn) + templateTagList, err := productTemplateTagsModel.GetListByIds(l.ctx, productTemplateTagIds) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product template tags") + } + mapTag := make(map[int64]int) + for k, v := range templateTagList { + mapTag[v.Id] = k + } + list := make([]types.GetMapLibraryListRsp, 0, len(mapLibraryList)) + for _, v := range mapLibraryList { + data := types.GetMapLibraryListRsp{ + Mid: v.Id, + Ctime: time.Unix(*v.Ctime, 0).Format("2006-01-02 15:04:05"), + } + //tag拼装 + if tagIndex, ok := mapTag[*v.TagId]; ok { + data.Tag = types.MapLibraryListTag{ + Id: templateTagList[tagIndex].Id, + Title: *templateTagList[tagIndex].Title, + } + } + //解析info + var info types.MapLibraryListInfo + if err = json.Unmarshal([]byte(*v.Info), &info); err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "json parse info err") + } + data.Info = info + list = append(list, data) + } return resp.SetStatus(basic.CodeOK) } diff --git a/server/map_library/internal/svc/servicecontext.go b/server/map_library/internal/svc/servicecontext.go index b02b6d54..3d6acee1 100644 --- a/server/map_library/internal/svc/servicecontext.go +++ b/server/map_library/internal/svc/servicecontext.go @@ -2,6 +2,7 @@ package svc import ( "errors" + "fmt" "fusenapi/initalize" "fusenapi/server/map_library/internal/config" "github.com/golang-jwt/jwt" @@ -19,7 +20,7 @@ func NewServiceContext(c config.Config) *ServiceContext { return &ServiceContext{ Config: c, - MysqlConn: initalize.InitMysql(c.DataSource), + MysqlConn: initalize.InitMysql(c.SourceMysql), } }