This commit is contained in:
laodaming 2023-06-14 15:45:56 +08:00
parent 5b2eef4822
commit 83e45f26b2
9 changed files with 228 additions and 9 deletions

12
ddl/fs_map_library.sql Normal file
View File

@ -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='贴图库';

View File

@ -19,7 +19,7 @@ func NewServiceContext(c {{.config}}) *ServiceContext {
return &ServiceContext{
Config: c,
MysqlConn: initalize.InitMysql(c.DataSource),
MysqlConn: initalize.InitMysql(c.SourceMysql),
{{.middlewareAssignment}}
}
}

24
model/fsmaplibrarymodel.go Executable file
View File

@ -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),
}
}

89
model/fsmaplibrarymodel_gen.go Executable file
View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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 {

View File

@ -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)
}

View File

@ -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),
}
}