diff --git a/data-transfer/data-transfer.go b/data-transfer/data-transfer.go new file mode 100644 index 00000000..396cd2b2 --- /dev/null +++ b/data-transfer/data-transfer.go @@ -0,0 +1,31 @@ +package main + +import ( + "flag" + "fmt" + + "fusenapi/data-transfer/internal/config" + "fusenapi/data-transfer/internal/handler" + "fusenapi/data-transfer/internal/svc" + + "github.com/zeromicro/go-zero/core/conf" + "github.com/zeromicro/go-zero/rest" +) + +var configFile = flag.String("f", "etc/data-transfer.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/data-transfer/etc/data-transfer.yaml b/data-transfer/etc/data-transfer.yaml new file mode 100644 index 00000000..4a2111b7 --- /dev/null +++ b/data-transfer/etc/data-transfer.yaml @@ -0,0 +1,8 @@ +Name: data-transfer +Host: 0.0.0.0 +Port: 8890 +SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest +Auth: + AccessSecret: fusen2023 + AccessExpire: 60 + RefreshAfter: 60 \ No newline at end of file diff --git a/data-transfer/internal/config/config.go b/data-transfer/internal/config/config.go new file mode 100644 index 00000000..b96d5f2f --- /dev/null +++ b/data-transfer/internal/config/config.go @@ -0,0 +1,12 @@ +package config + +import ( + "fusenapi/data-transfer/internal/types" + "github.com/zeromicro/go-zero/rest" +) + +type Config struct { + rest.RestConf + SourceMysql string + Auth types.Auth +} diff --git a/data-transfer/internal/handler/getstandardlogolisthandler.go b/data-transfer/internal/handler/getstandardlogolisthandler.go new file mode 100644 index 00000000..8c9741c3 --- /dev/null +++ b/data-transfer/internal/handler/getstandardlogolisthandler.go @@ -0,0 +1,26 @@ +package handler + +import ( + "errors" + "net/http" + + "github.com/zeromicro/go-zero/core/logx" + "github.com/zeromicro/go-zero/rest/httpx" + + "fusenapi/data-transfer/internal/logic" + "fusenapi/data-transfer/internal/svc" +) + +func GetStandardLogoListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + l := logic.NewGetStandardLogoListLogic(r.Context(), svcCtx) + resp := l.GetStandardLogoList() + if resp != nil { + httpx.OkJsonCtx(r.Context(), w, resp) + } else { + err := errors.New("server logic is error, resp must not be nil") + httpx.ErrorCtx(r.Context(), w, err) + logx.Error(err) + } + } +} diff --git a/data-transfer/internal/handler/routes.go b/data-transfer/internal/handler/routes.go new file mode 100644 index 00000000..260df581 --- /dev/null +++ b/data-transfer/internal/handler/routes.go @@ -0,0 +1,23 @@ +// Code generated by goctl. DO NOT EDIT. +package handler + +import ( + "net/http" + + "fusenapi/data-transfer/internal/svc" + + "github.com/zeromicro/go-zero/rest" +) + +func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { + server.AddRoutes( + []rest.Route{ + { + Method: http.MethodGet, + Path: "/standard-logo/list", + Handler: GetStandardLogoListHandler(serverCtx), + }, + }, + rest.WithJwt(serverCtx.Config.Auth.AccessSecret), + ) +} diff --git a/data-transfer/internal/logic/getstandardlogolistlogic.go b/data-transfer/internal/logic/getstandardlogolistlogic.go new file mode 100644 index 00000000..89e7ad32 --- /dev/null +++ b/data-transfer/internal/logic/getstandardlogolistlogic.go @@ -0,0 +1,44 @@ +package logic + +import ( + "context" + "fusenapi/data-transfer/internal/svc" + "fusenapi/data-transfer/internal/types" + "fusenapi/model" + "fusenapi/utils/basic" + + "github.com/zeromicro/go-zero/core/logx" +) + +type GetStandardLogoListLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewGetStandardLogoListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetStandardLogoListLogic { + return &GetStandardLogoListLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 获取标准logo列表 +func (l *GetStandardLogoListLogic) GetStandardLogoList() (resp *types.Response) { + standardLogoModel := model.NewFsStandardLogoModel(l.svcCtx.MysqlConn) + logoList, err := standardLogoModel.GetAll(l.ctx) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get standard logo list") + } + list := make([]types.GetStandardLogoListRsp, 0, len(logoList)) + for _, v := range logoList { + list = append(list, types.GetStandardLogoListRsp{ + Id: v.Id, + Name: v.Name, + Url: v.Image, + }) + } + return resp.SetStatusWithMessage(basic.CodeOK, "success", list) +} diff --git a/data-transfer/internal/svc/servicecontext.go b/data-transfer/internal/svc/servicecontext.go new file mode 100644 index 00000000..db1b3062 --- /dev/null +++ b/data-transfer/internal/svc/servicecontext.go @@ -0,0 +1,19 @@ +package svc + +import ( + "fusenapi/data-transfer/internal/config" + "github.com/zeromicro/go-zero/core/stores/sqlx" +) + +type ServiceContext struct { + Config config.Config + + MysqlConn sqlx.SqlConn +} + +func NewServiceContext(c config.Config) *ServiceContext { + return &ServiceContext{ + Config: c, + MysqlConn: sqlx.NewMysql(c.SourceMysql), + } +} diff --git a/data-transfer/internal/types/types.go b/data-transfer/internal/types/types.go new file mode 100644 index 00000000..ecd5ec58 --- /dev/null +++ b/data-transfer/internal/types/types.go @@ -0,0 +1,72 @@ +// Code generated by goctl. DO NOT EDIT. +package types + +import ( + "fusenapi/utils/basic" +) + +type GetStandardLogoListRsp struct { + Id int64 `json:"id"` + Name string `json:"name"` + Url string `json:"url"` +} + +type Response struct { + Code int `json:"code"` + Message string `json:"msg"` + Data interface{} `json:"data"` +} + +type ResponseJwt struct { + Code int `json:"code"` + Message string `json:"msg"` + Data interface{} `json:"data"` + AccessSecret string `json:"accessSecret"` + AccessExpire int64 `json:"accessExpire"` +} + +type Auth struct { + AccessSecret string `json:"accessSecret"` + AccessExpire int64 `json:"accessExpire"` + RefreshAfter int64 `json:"refreshAfter"` +} + +// Set 设置Response的Code和Message值 +func (resp *Response) Set(Code int, Message string) *Response { + return &Response{ + Code: Code, + Message: Message, + } +} + +// Set 设置整个Response +func (resp *Response) SetWithData(Code int, Message string, Data interface{}) *Response { + return &Response{ + Code: Code, + Message: Message, + Data: Data, + } +} + +// SetStatus 设置默认StatusResponse(内部自定义) 默认msg, 可以带data, data只使用一个参数 +func (resp *Response) SetStatus(sr *basic.StatusResponse, data ...interface{}) *Response { + newResp := &Response{ + Code: sr.Code, + } + if len(data) == 1 { + newResp.Data = data[0] + } + return newResp +} + +// SetStatusWithMessage 设置默认StatusResponse(内部自定义) 非默认msg, 可以带data, data只使用一个参数 +func (resp *Response) SetStatusWithMessage(sr *basic.StatusResponse, msg string, data ...interface{}) *Response { + newResp := &Response{ + Code: sr.Code, + Message: msg, + } + if len(data) == 1 { + newResp.Data = data[0] + } + return newResp +} diff --git a/ddl/fs_standard_logo.sql b/ddl/fs_standard_logo.sql new file mode 100644 index 00000000..a8c21181 --- /dev/null +++ b/ddl/fs_standard_logo.sql @@ -0,0 +1,10 @@ +-- fusentest.fs_standard_logo definition + +CREATE TABLE `fs_standard_logo` ( + `id` int(10) NOT NULL AUTO_INCREMENT COMMENT 'ID', + `name` varchar(20) CHARACTER SET utf8mb4 NOT NULL COMMENT 'logo名称', + `image` varchar(255) CHARACTER SET utf8mb4 NOT NULL COMMENT '图片地址', + `ctime` int(10) NOT NULL DEFAULT '0' COMMENT '添加时间', + `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '状态 1正常 0删除', + PRIMARY KEY (`id`) +) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COMMENT='标准logo'; \ No newline at end of file diff --git a/model/fsstandardlogomodel.go b/model/fsstandardlogomodel.go new file mode 100755 index 00000000..6a6c05b8 --- /dev/null +++ b/model/fsstandardlogomodel.go @@ -0,0 +1,38 @@ +package model + +import ( + "context" + "fmt" + "github.com/zeromicro/go-zero/core/stores/sqlx" +) + +var _ FsStandardLogoModel = (*customFsStandardLogoModel)(nil) + +type ( + // FsStandardLogoModel is an interface to be customized, add more methods here, + // and implement the added methods in customFsStandardLogoModel. + FsStandardLogoModel interface { + fsStandardLogoModel + GetAll(ctx context.Context) (resp []FsStandardLogo, err error) + } + + customFsStandardLogoModel struct { + *defaultFsStandardLogoModel + } +) + +// NewFsStandardLogoModel returns a model for the database table. +func NewFsStandardLogoModel(conn sqlx.SqlConn) FsStandardLogoModel { + return &customFsStandardLogoModel{ + defaultFsStandardLogoModel: newFsStandardLogoModel(conn), + } +} + +func (m *defaultFsStandardLogoModel) GetAll(ctx context.Context) (resp []FsStandardLogo, err error) { + query := fmt.Sprintf("select %s from %s where `status` = ? ", fsStandardLogoRows, m.table) + err = m.conn.QueryRowsCtx(ctx, &resp, query, 1) + if err != nil { + return nil, err + } + return +} diff --git a/model/fsstandardlogomodel_gen.go b/model/fsstandardlogomodel_gen.go new file mode 100755 index 00000000..df8aaecd --- /dev/null +++ b/model/fsstandardlogomodel_gen.go @@ -0,0 +1,87 @@ +// 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 ( + fsStandardLogoFieldNames = builder.RawFieldNames(&FsStandardLogo{}) + fsStandardLogoRows = strings.Join(fsStandardLogoFieldNames, ",") + fsStandardLogoRowsExpectAutoSet = strings.Join(stringx.Remove(fsStandardLogoFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",") + fsStandardLogoRowsWithPlaceHolder = strings.Join(stringx.Remove(fsStandardLogoFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?" +) + +type ( + fsStandardLogoModel interface { + Insert(ctx context.Context, data *FsStandardLogo) (sql.Result, error) + FindOne(ctx context.Context, id int64) (*FsStandardLogo, error) + Update(ctx context.Context, data *FsStandardLogo) error + Delete(ctx context.Context, id int64) error + } + + defaultFsStandardLogoModel struct { + conn sqlx.SqlConn + table string + } + + FsStandardLogo struct { + Id int64 `db:"id"` // ID + Name string `db:"name"` // logo名称 + Image string `db:"image"` // 图片地址 + Ctime int64 `db:"ctime"` // 添加时间 + Status int64 `db:"status"` // 状态 1正常 0删除 + } +) + +func newFsStandardLogoModel(conn sqlx.SqlConn) *defaultFsStandardLogoModel { + return &defaultFsStandardLogoModel{ + conn: conn, + table: "`fs_standard_logo`", + } +} + +func (m *defaultFsStandardLogoModel) 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 *defaultFsStandardLogoModel) FindOne(ctx context.Context, id int64) (*FsStandardLogo, error) { + query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", fsStandardLogoRows, m.table) + var resp FsStandardLogo + 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 *defaultFsStandardLogoModel) Insert(ctx context.Context, data *FsStandardLogo) (sql.Result, error) { + query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?)", m.table, fsStandardLogoRowsExpectAutoSet) + ret, err := m.conn.ExecCtx(ctx, query, data.Name, data.Image, data.Ctime, data.Status) + return ret, err +} + +func (m *defaultFsStandardLogoModel) Update(ctx context.Context, data *FsStandardLogo) error { + query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, fsStandardLogoRowsWithPlaceHolder) + _, err := m.conn.ExecCtx(ctx, query, data.Name, data.Image, data.Ctime, data.Status, data.Id) + return err +} + +func (m *defaultFsStandardLogoModel) tableName() string { + return m.table +} diff --git a/server/product/internal/handler/routes.go b/server/product/internal/handler/routes.go index b936c6a0..5d55deee 100644 --- a/server/product/internal/handler/routes.go +++ b/server/product/internal/handler/routes.go @@ -22,17 +22,12 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/product/success-recommand", Handler: GetSuccessRecommandHandler(serverCtx), }, - }, - rest.WithJwt(serverCtx.Config.Auth.AccessSecret), - ) - - server.AddRoutes( - []rest.Route{ { Method: http.MethodGet, Path: "/product/get-size-by-product", Handler: GetSizeByProductHandler(serverCtx), }, }, + rest.WithJwt(serverCtx.Config.Auth.AccessSecret), ) } diff --git a/server/product/internal/logic/getsizebyproductlogic.go b/server/product/internal/logic/getsizebyproductlogic.go index 63fbd3f8..9ec5193d 100644 --- a/server/product/internal/logic/getsizebyproductlogic.go +++ b/server/product/internal/logic/getsizebyproductlogic.go @@ -157,22 +157,18 @@ func (l *GetSizeByProductLogic) GetSecondChildrenList(tag model.FsTags, product if err != nil { return nil, err } - if len(stepNum) > len(stepPrice) { - return nil, errors.New(fmt.Sprintf("stepNum count not eq stepPrice count: product size id :%d ,product price id :%d", productSize.Id, price.Id)) + if len(stepNum) == 0 || len(stepPrice) == 0 { + return nil, errors.New(fmt.Sprintf("stepNum count or stepPrice count is empty: product size id :%d ,product price id :%d", productSize.Id, price.Id)) } - for i := 0; i < 3; i++ { - // 最小购买数量小于 最大阶梯数量+5 - if int(price.MinBuyNum) < (stepNum[len(stepNum)-1] + 5) { - priceList = append(priceList, types.PriceObj{ - Num: int(price.MinBuyNum * price.EachBoxNum), - Price: l.GetPrice(int(price.MinBuyNum), stepNum, stepPrice), - }) - } + index := 0 + // 最小购买数量小于 最大阶梯数量+5 + for int(price.MinBuyNum) < (stepNum[len(stepNum)-1]+5) && index < 3 { + priceList = append(priceList, types.PriceObj{ + Num: int(price.MinBuyNum * price.EachBoxNum), + Price: l.GetPrice(int(price.MinBuyNum), stepNum, stepPrice), + }) price.MinBuyNum++ - } - //如果不够三个则追加伪数据 - for len(priceList) < 3 { - priceList = append(priceList, types.PriceObj{Num: 1, Price: 0}) + index++ } data := types.ChildrenObj{ Id: productSize.Id, @@ -184,9 +180,15 @@ func (l *GetSizeByProductLogic) GetSecondChildrenList(tag model.FsTags, product return } func (l *GetSizeByProductLogic) GetPrice(minBuyNum int, stepNum []int, stepPrice []int) float64 { + if minBuyNum > stepNum[len(stepNum)-1] { + return float64(stepPrice[len(stepPrice)-1]) / float64(100) + } for k, v := range stepNum { if minBuyNum <= v { - return float64(stepPrice[k]) / float64(100) + if k <= (len(stepPrice) - 1) { + return float64(stepPrice[k]) / float64(100) + } + return float64(stepPrice[len(stepPrice)-1]) / float64(100) } } return float64(stepPrice[len(stepPrice)-1]) / float64(100) diff --git a/server_api/data-transfer.api b/server_api/data-transfer.api index 12da54af..fbcb090d 100644 --- a/server_api/data-transfer.api +++ b/server_api/data-transfer.api @@ -7,17 +7,20 @@ info ( email: "" ) -type request { - // TODO: add members here and delete this comment - Name string `path:"name,options=you|me"` // parameters are auto validated +import "basic.api" + +@server( + jwt: Auth +) +service data-transfer { + //获取标准logo列表 + @handler GetStandardLogoListHandler + get /standard-logo/list ( ) returns (response); } -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); +//获取标准logo列表 +type GetStandardLogoListRsp { + Id int64 `json:"id"` + Name string `json:"name"` + Url string `json:"url"` } \ No newline at end of file diff --git a/server_api/product.api b/server_api/product.api index 181ec2b0..af4dc351 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -18,10 +18,6 @@ service product { //获取成功后的推荐产品 @handler GetSuccessRecommand get /product/success-recommand (GetSuccessRecommandReq) returns (response); -} - -//非登录接口 -service product { //获取分类下的产品以及尺寸 @handler GetSizeByProduct get /product/get-size-by-product () returns (response);