From d26586c794e0b902d3ca9793c947eec7ace5d152 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Thu, 1 Jun 2023 15:32:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=A7=E5=93=81=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 ++ .idea/fusenapi.iml | 9 ++ .idea/modules.xml | 8 ++ .idea/sqldialects.xml | 7 + .idea/vcs.xml | 6 + .idea/watcherTasks.xml | 29 ++++ ddl/fs_product.sql | 35 +++++ model/fsproductmodel.go | 24 ++++ model/fsproductmodel_gen.go | 126 ++++++++++++++++++ product.api | 20 ++- product/etc/product.yaml | 4 + product/internal/config/config.go | 8 ++ .../internal/handler/getproductlisthandler.go | 28 ++++ product/internal/handler/routes.go | 22 +++ product/internal/logic/getproductlistlogic.go | 35 +++++ product/internal/svc/servicecontext.go | 19 +++ product/internal/types/types.go | 15 +++ product/product.go | 31 +++++ utils/image/image_size.go | 28 ++++ 19 files changed, 451 insertions(+), 11 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/fusenapi.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/sqldialects.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/watcherTasks.xml create mode 100644 ddl/fs_product.sql create mode 100755 model/fsproductmodel.go create mode 100755 model/fsproductmodel_gen.go create mode 100644 product/etc/product.yaml create mode 100644 product/internal/config/config.go create mode 100644 product/internal/handler/getproductlisthandler.go create mode 100644 product/internal/handler/routes.go create mode 100644 product/internal/logic/getproductlistlogic.go create mode 100644 product/internal/svc/servicecontext.go create mode 100644 product/internal/types/types.go create mode 100644 product/product.go create mode 100644 utils/image/image_size.go diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..73f69e09 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/fusenapi.iml b/.idea/fusenapi.iml new file mode 100644 index 00000000..5e764c4f --- /dev/null +++ b/.idea/fusenapi.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..f0926055 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml new file mode 100644 index 00000000..495fcf38 --- /dev/null +++ b/.idea/sqldialects.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..94a25f7f --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/watcherTasks.xml b/.idea/watcherTasks.xml new file mode 100644 index 00000000..97ad6d2d --- /dev/null +++ b/.idea/watcherTasks.xml @@ -0,0 +1,29 @@ + + + + + + + + \ No newline at end of file diff --git a/ddl/fs_product.sql b/ddl/fs_product.sql new file mode 100644 index 00000000..0302c8b6 --- /dev/null +++ b/ddl/fs_product.sql @@ -0,0 +1,35 @@ +-- fusentest.fs_product definition + +CREATE TABLE `fs_product` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `sn` char(8) NOT NULL COMMENT '商品编号 P98f087j', + `type` tinyint(4) NOT NULL COMMENT '分类ID', + `title` varchar(64) NOT NULL COMMENT '名称', + `title_cn` varchar(64) NOT NULL COMMENT '中文名称', + `cover` varchar(128) NOT NULL COMMENT '封面图', + `imgs` text NOT NULL COMMENT '一个或多个介绍图或视频', + `keywords` varchar(128) NOT NULL COMMENT '关键字', + `intro` text COMMENT '简要描述', + `sort` smallint(6) DEFAULT '0' COMMENT '排序', + `selled_num` int(11) DEFAULT '0' COMMENT '已卖数量', + `ctime` int(11) DEFAULT NULL COMMENT '添加时间', + `view` int(11) DEFAULT '0' COMMENT '浏览量', + `size_ids` varchar(128) DEFAULT NULL COMMENT '尺寸 1,2,3,4', + `material_ids` varchar(128) NOT NULL DEFAULT '' COMMENT '材质 1,2,3', + `tag_ids` varchar(128) DEFAULT NULL COMMENT '标签 逗号间隔', + `status` tinyint(4) DEFAULT '0' COMMENT '状态位 弃用', + `produce_days` mediumint(8) unsigned NOT NULL DEFAULT '0' COMMENT '生产天数', + `delivery_days` mediumint(8) unsigned NOT NULL DEFAULT '0' COMMENT '运送天数', + `cover_img` varchar(128) NOT NULL DEFAULT '' COMMENT '背景图', + `is_shelf` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否上架', + `is_recommend` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否推荐', + `is_hot` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否热销', + `is_protection` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否环保', + `is_microwave` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否可微波炉', + `is_del` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除', + `recommend_product` varchar(500) DEFAULT NULL COMMENT '推荐产品id例如: 1,3,4,5', + `recommend_product_sort` varchar(500) DEFAULT NULL COMMENT '推荐排序例如:1324', + `scene_ids` varchar(500) DEFAULT NULL COMMENT '关联的场景id', + PRIMARY KEY (`id`) USING BTREE, + UNIQUE KEY `sn` (`sn`) USING BTREE +) ENGINE=InnoDB AUTO_INCREMENT=60 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='产品表'; \ No newline at end of file diff --git a/model/fsproductmodel.go b/model/fsproductmodel.go new file mode 100755 index 00000000..b92b1d3b --- /dev/null +++ b/model/fsproductmodel.go @@ -0,0 +1,24 @@ +package model + +import "github.com/zeromicro/go-zero/core/stores/sqlx" + +var _ FsProductModel = (*customFsProductModel)(nil) + +type ( + // FsProductModel is an interface to be customized, add more methods here, + // and implement the added methods in customFsProductModel. + FsProductModel interface { + fsProductModel + } + + customFsProductModel struct { + *defaultFsProductModel + } +) + +// NewFsProductModel returns a model for the database table. +func NewFsProductModel(conn sqlx.SqlConn) FsProductModel { + return &customFsProductModel{ + defaultFsProductModel: newFsProductModel(conn), + } +} diff --git a/model/fsproductmodel_gen.go b/model/fsproductmodel_gen.go new file mode 100755 index 00000000..bff1a42e --- /dev/null +++ b/model/fsproductmodel_gen.go @@ -0,0 +1,126 @@ +// 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 ( + fsProductFieldNames = builder.RawFieldNames(&FsProduct{}) + fsProductRows = strings.Join(fsProductFieldNames, ",") + fsProductRowsExpectAutoSet = strings.Join(stringx.Remove(fsProductFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",") + fsProductRowsWithPlaceHolder = strings.Join(stringx.Remove(fsProductFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?" +) + +type ( + fsProductModel interface { + Insert(ctx context.Context, data *FsProduct) (sql.Result, error) + FindOne(ctx context.Context, id int64) (*FsProduct, error) + FindOneBySn(ctx context.Context, sn string) (*FsProduct, error) + Update(ctx context.Context, data *FsProduct) error + Delete(ctx context.Context, id int64) error + } + + defaultFsProductModel struct { + conn sqlx.SqlConn + table string + } + + FsProduct struct { + Id int64 `db:"id"` + Sn string `db:"sn"` // 商品编号 P98f087j + Type int64 `db:"type"` // 分类ID + Title string `db:"title"` // 名称 + TitleCn string `db:"title_cn"` // 中文名称 + Cover string `db:"cover"` // 封面图 + Imgs string `db:"imgs"` // 一个或多个介绍图或视频 + Keywords string `db:"keywords"` // 关键字 + Intro sql.NullString `db:"intro"` // 简要描述 + Sort int64 `db:"sort"` // 排序 + SelledNum int64 `db:"selled_num"` // 已卖数量 + Ctime sql.NullInt64 `db:"ctime"` // 添加时间 + View int64 `db:"view"` // 浏览量 + SizeIds sql.NullString `db:"size_ids"` // 尺寸 1,2,3,4 + MaterialIds string `db:"material_ids"` // 材质 1,2,3 + TagIds sql.NullString `db:"tag_ids"` // 标签 逗号间隔 + Status int64 `db:"status"` // 状态位 弃用 + ProduceDays int64 `db:"produce_days"` // 生产天数 + DeliveryDays int64 `db:"delivery_days"` // 运送天数 + CoverImg string `db:"cover_img"` // 背景图 + IsShelf int64 `db:"is_shelf"` // 是否上架 + IsRecommend int64 `db:"is_recommend"` // 是否推荐 + IsHot int64 `db:"is_hot"` // 是否热销 + IsProtection int64 `db:"is_protection"` // 是否环保 + IsMicrowave int64 `db:"is_microwave"` // 是否可微波炉 + IsDel int64 `db:"is_del"` // 是否删除 + RecommendProduct sql.NullString `db:"recommend_product"` // 推荐产品id例如: 1,3,4,5 + RecommendProductSort sql.NullString `db:"recommend_product_sort"` // 推荐排序例如:1324 + SceneIds sql.NullString `db:"scene_ids"` // 关联的场景id + } +) + +func newFsProductModel(conn sqlx.SqlConn) *defaultFsProductModel { + return &defaultFsProductModel{ + conn: conn, + table: "`fs_product`", + } +} + +func (m *defaultFsProductModel) 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 *defaultFsProductModel) FindOne(ctx context.Context, id int64) (*FsProduct, error) { + query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", fsProductRows, m.table) + var resp FsProduct + 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 *defaultFsProductModel) FindOneBySn(ctx context.Context, sn string) (*FsProduct, error) { + var resp FsProduct + query := fmt.Sprintf("select %s from %s where `sn` = ? limit 1", fsProductRows, m.table) + err := m.conn.QueryRowCtx(ctx, &resp, query, sn) + switch err { + case nil: + return &resp, nil + case sqlc.ErrNotFound: + return nil, ErrNotFound + default: + return nil, err + } +} + +func (m *defaultFsProductModel) Insert(ctx context.Context, data *FsProduct) (sql.Result, error) { + query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, fsProductRowsExpectAutoSet) + ret, err := m.conn.ExecCtx(ctx, query, data.Sn, data.Type, data.Title, data.TitleCn, data.Cover, data.Imgs, data.Keywords, data.Intro, data.Sort, data.SelledNum, data.Ctime, data.View, data.SizeIds, data.MaterialIds, data.TagIds, data.Status, data.ProduceDays, data.DeliveryDays, data.CoverImg, data.IsShelf, data.IsRecommend, data.IsHot, data.IsProtection, data.IsMicrowave, data.IsDel, data.RecommendProduct, data.RecommendProductSort, data.SceneIds) + return ret, err +} + +func (m *defaultFsProductModel) Update(ctx context.Context, newData *FsProduct) error { + query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, fsProductRowsWithPlaceHolder) + _, err := m.conn.ExecCtx(ctx, query, newData.Sn, newData.Type, newData.Title, newData.TitleCn, newData.Cover, newData.Imgs, newData.Keywords, newData.Intro, newData.Sort, newData.SelledNum, newData.Ctime, newData.View, newData.SizeIds, newData.MaterialIds, newData.TagIds, newData.Status, newData.ProduceDays, newData.DeliveryDays, newData.CoverImg, newData.IsShelf, newData.IsRecommend, newData.IsHot, newData.IsProtection, newData.IsMicrowave, newData.IsDel, newData.RecommendProduct, newData.RecommendProductSort, newData.SceneIds, newData.Id) + return err +} + +func (m *defaultFsProductModel) tableName() string { + return m.table +} diff --git a/product.api b/product.api index 99683571..fbb01de1 100644 --- a/product.api +++ b/product.api @@ -6,18 +6,16 @@ info ( author: "" email: "" ) - -type request { +import "basic.api" +type GetProductListReq { // TODO: add members here and delete this comment - Name string `path:"name,options=you|me"` // parameters are auto validated + Cid uint32 `form:"cid"` + Size uint32 `form:"size"` + Page uint32 `form:"page"` + IsDemo uint32 `form:"is_demo" , options=0|1"` } -type response { - // TODO: add members here and delete this comment - Message string `json:"message"` -} - -service user-auth { - @handler GreetHandler - get /greet/from/:name(request) returns (response); +service product { + @handler GetProductListHandler + get /product/list(GetProductListReq) returns (response); } \ No newline at end of file diff --git a/product/etc/product.yaml b/product/etc/product.yaml new file mode 100644 index 00000000..43aeb8a3 --- /dev/null +++ b/product/etc/product.yaml @@ -0,0 +1,4 @@ +Name: product +Host: 0.0.0.0 +Port: 8888 +DataSource: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest \ No newline at end of file diff --git a/product/internal/config/config.go b/product/internal/config/config.go new file mode 100644 index 00000000..0e4c60b5 --- /dev/null +++ b/product/internal/config/config.go @@ -0,0 +1,8 @@ +package config + +import "github.com/zeromicro/go-zero/rest" + +type Config struct { + rest.RestConf + DataSource string +} diff --git a/product/internal/handler/getproductlisthandler.go b/product/internal/handler/getproductlisthandler.go new file mode 100644 index 00000000..efbe0594 --- /dev/null +++ b/product/internal/handler/getproductlisthandler.go @@ -0,0 +1,28 @@ +package handler + +import ( + "net/http" + + "fusenapi/product/internal/logic" + "fusenapi/product/internal/svc" + "fusenapi/product/internal/types" + "github.com/zeromicro/go-zero/rest/httpx" +) + +func GetProductListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.GetProductListReq + if err := httpx.Parse(r, &req); err != nil { + httpx.ErrorCtx(r.Context(), w, err) + return + } + + l := logic.NewGetProductListLogic(r.Context(), svcCtx) + resp, err := l.GetProductList(&req) + if err != nil { + httpx.ErrorCtx(r.Context(), w, err) + } else { + httpx.OkJsonCtx(r.Context(), w, resp) + } + } +} diff --git a/product/internal/handler/routes.go b/product/internal/handler/routes.go new file mode 100644 index 00000000..18f67cbc --- /dev/null +++ b/product/internal/handler/routes.go @@ -0,0 +1,22 @@ +// Code generated by goctl. DO NOT EDIT. +package handler + +import ( + "net/http" + + "fusenapi/product/internal/svc" + + "github.com/zeromicro/go-zero/rest" +) + +func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { + server.AddRoutes( + []rest.Route{ + { + Method: http.MethodGet, + Path: "/product/list", + Handler: GetProductListHandler(serverCtx), + }, + }, + ) +} diff --git a/product/internal/logic/getproductlistlogic.go b/product/internal/logic/getproductlistlogic.go new file mode 100644 index 00000000..b15c9e34 --- /dev/null +++ b/product/internal/logic/getproductlistlogic.go @@ -0,0 +1,35 @@ +package logic + +import ( + "context" + "fusenapi/utils/image" + + "fusenapi/product/internal/svc" + "fusenapi/product/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type GetProductListLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewGetProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetProductListLogic { + return &GetProductListLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 获取产品列表 +func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp *types.Response, err error) { + //获取合适尺寸 + if req.Size > 0 { + req.Size = image.GetCurrentSize(req.Size) + } + //获取是否存在千人千面 + return +} diff --git a/product/internal/svc/servicecontext.go b/product/internal/svc/servicecontext.go new file mode 100644 index 00000000..d1c7fc03 --- /dev/null +++ b/product/internal/svc/servicecontext.go @@ -0,0 +1,19 @@ +package svc + +import ( + "fusenapi/model" + "fusenapi/product/internal/config" + "github.com/zeromicro/go-zero/core/stores/sqlx" +) + +type ServiceContext struct { + Config config.Config + FsProductModel model.FsProductModel +} + +func NewServiceContext(c config.Config) *ServiceContext { + return &ServiceContext{ + Config: c, + FsProductModel: model.NewFsProductModel(sqlx.NewMysql(c.DataSource)), + } +} diff --git a/product/internal/types/types.go b/product/internal/types/types.go new file mode 100644 index 00000000..a94c8c07 --- /dev/null +++ b/product/internal/types/types.go @@ -0,0 +1,15 @@ +// Code generated by goctl. DO NOT EDIT. +package types + +type GetProductListReq struct { + Cid uint32 `form:"cid"` + Size uint32 `form:"size"` + Page uint32 `form:"page"` + IsDemo uint32 `form:"is_demo" , options=0|1"` +} + +type Response struct { + Code int `json:"code"` + Message string `json:"msg"` + Data interface{} `json:"data"` +} diff --git a/product/product.go b/product/product.go new file mode 100644 index 00000000..e5afc4c9 --- /dev/null +++ b/product/product.go @@ -0,0 +1,31 @@ +package main + +import ( + "flag" + "fmt" + + "fusenapi/product/internal/config" + "fusenapi/product/internal/handler" + "fusenapi/product/internal/svc" + + "github.com/zeromicro/go-zero/core/conf" + "github.com/zeromicro/go-zero/rest" +) + +var configFile = flag.String("f", "etc/product.yaml", "the config file") + +func main() { + flag.Parse() + + var c config.Config + conf.MustLoad(*configFile, &c) + + server := rest.MustNewServer(c.RestConf) + defer server.Stop() + + ctx := svc.NewServiceContext(c) + handler.RegisterHandlers(server, ctx) + + fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port) + server.Start() +} diff --git a/utils/image/image_size.go b/utils/image/image_size.go new file mode 100644 index 00000000..9421cc8e --- /dev/null +++ b/utils/image/image_size.go @@ -0,0 +1,28 @@ +package image + +// 定义尺寸规则 +var sizeArray = []uint32{1200, 1000, 750, 500, 128} + +// 裁剪尺寸阶梯 +var newSizeArray = []uint32{200, 400, 600, 800} + +func GetCurrentSize(clientSize uint32) uint32 { + lenNewSize := len(newSizeArray) + //大于最大尺寸则返回规则最大尺寸 + if clientSize >= newSizeArray[lenNewSize-1] { + return newSizeArray[lenNewSize-1] + } + //小于最小尺寸则返回规则最小尺寸 + if clientSize < newSizeArray[0] { + return clientSize + } + for k, v := range newSizeArray { + if v == clientSize { + return v + } + if clientSize > v && k < lenNewSize-1 && clientSize < newSizeArray[k+1] { + return newSizeArray[k+1] + } + } + return clientSize +}