init
This commit is contained in:
92
model/goods_status/goodsstatusmodel.go
Normal file
92
model/goods_status/goodsstatusmodel.go
Normal file
@ -0,0 +1,92 @@
|
||||
package goods_status
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var _ GoodsStatusModel = (*customGoodsStatusModel)(nil)
|
||||
|
||||
type (
|
||||
// GoodsStatusModel is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in customGoodsStatusModel.
|
||||
GoodsStatusModel interface {
|
||||
goodsStatusModel
|
||||
withSession(session sqlx.Session) GoodsStatusModel
|
||||
DeleteByApiId(ctx context.Context, id int64) error
|
||||
FindUpList(ctx context.Context, apiId int64) ([]int64, error)
|
||||
UpdateDownByIds(ctx context.Context, ids []int64) error
|
||||
FindNotIdOne(ctx context.Context, apiId int64) (int64, error)
|
||||
}
|
||||
|
||||
customGoodsStatusModel struct {
|
||||
*defaultGoodsStatusModel
|
||||
}
|
||||
)
|
||||
|
||||
func (m *customGoodsStatusModel) FindNotIdOne(ctx context.Context, apiId int64) (int64, error) {
|
||||
query := fmt.Sprintf("select gs.`api_product_id` from %s gs left join `goods_info` gi on gs.`api_id` = gi.`api_id` where gs.`api_id` = ? and gs.`api_product_id` not in (select gs.api_product_id from `goods_status` gs join `goods_info` gi on gs.`api_id` = gi.`api_id` and gi.api_product_id = gs.api_product_id where gs.`api_id` = ?) limit 1", m.table)
|
||||
var resp int64
|
||||
err := m.conn.QueryRowCtx(ctx, &resp, query, apiId, apiId)
|
||||
switch err {
|
||||
case nil:
|
||||
return resp, nil
|
||||
case sqlx.ErrNotFound:
|
||||
return 0, ErrNotFound
|
||||
default:
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *customGoodsStatusModel) UpdateDownByIds(ctx context.Context, ids []int64) error {
|
||||
// 动态构造 IN 子句中的占位符
|
||||
placeholders := make([]string, len(ids))
|
||||
for i := range ids {
|
||||
placeholders[i] = "?"
|
||||
}
|
||||
query := fmt.Sprintf("update %s set `status` = 0 where `id` in (%s)", m.table, strings.Join(placeholders, ","))
|
||||
_, err := m.conn.ExecCtx(ctx, query, convertToInterface(ids)...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *customGoodsStatusModel) FindUpList(ctx context.Context, apiId int64) ([]int64, error) {
|
||||
query := fmt.Sprintf("select g.`id` from %s g join `config_api` c on g.`api_id` = c.id where g.status = 0 and c.id = ?", m.table)
|
||||
var resp []int64
|
||||
err := m.conn.QueryRowsCtx(ctx, &resp, query, apiId)
|
||||
switch err {
|
||||
case nil:
|
||||
return resp, nil
|
||||
case sqlx.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *customGoodsStatusModel) DeleteByApiId(ctx context.Context, id int64) error {
|
||||
query := fmt.Sprintf("delete from %s where `api_id` = ?", m.table)
|
||||
_, err := m.conn.ExecCtx(ctx, query, id)
|
||||
return err
|
||||
}
|
||||
|
||||
// NewGoodsStatusModel returns a model for the database table.
|
||||
func NewGoodsStatusModel(conn sqlx.SqlConn) GoodsStatusModel {
|
||||
return &customGoodsStatusModel{
|
||||
defaultGoodsStatusModel: newGoodsStatusModel(conn),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *customGoodsStatusModel) withSession(session sqlx.Session) GoodsStatusModel {
|
||||
return NewGoodsStatusModel(sqlx.NewSqlConnFromSession(session))
|
||||
}
|
||||
|
||||
// convertToInterface 将 int64 切片转换为接口切片
|
||||
func convertToInterface(ids []int64) []interface{} {
|
||||
result := make([]interface{}, len(ids))
|
||||
for i, id := range ids {
|
||||
result[i] = id
|
||||
}
|
||||
return result
|
||||
}
|
87
model/goods_status/goodsstatusmodel_gen.go
Normal file
87
model/goods_status/goodsstatusmodel_gen.go
Normal file
@ -0,0 +1,87 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// versions:
|
||||
// goctl version: 1.7.6
|
||||
|
||||
package goods_status
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/builder"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
)
|
||||
|
||||
var (
|
||||
goodsStatusFieldNames = builder.RawFieldNames(&GoodsStatus{})
|
||||
goodsStatusRows = strings.Join(goodsStatusFieldNames, ",")
|
||||
goodsStatusRowsExpectAutoSet = strings.Join(stringx.Remove(goodsStatusFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
||||
goodsStatusRowsWithPlaceHolder = strings.Join(stringx.Remove(goodsStatusFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
||||
)
|
||||
|
||||
type (
|
||||
goodsStatusModel interface {
|
||||
Insert(ctx context.Context, data *GoodsStatus) (sql.Result, error)
|
||||
FindOne(ctx context.Context, id int64) (*GoodsStatus, error)
|
||||
Update(ctx context.Context, data *GoodsStatus) error
|
||||
Delete(ctx context.Context, id int64) error
|
||||
}
|
||||
|
||||
defaultGoodsStatusModel struct {
|
||||
conn sqlx.SqlConn
|
||||
table string
|
||||
}
|
||||
|
||||
GoodsStatus struct {
|
||||
Id int64 `db:"id"` // id
|
||||
ApiId sql.NullInt64 `db:"api_id"` // 三方接口
|
||||
ApiProductId sql.NullInt64 `db:"api_product_id"` // 三方商品id
|
||||
Status sql.NullInt64 `db:"status"` // 商品状态 0=上架 1=下架
|
||||
}
|
||||
)
|
||||
|
||||
func newGoodsStatusModel(conn sqlx.SqlConn) *defaultGoodsStatusModel {
|
||||
return &defaultGoodsStatusModel{
|
||||
conn: conn,
|
||||
table: "`goods_status`",
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultGoodsStatusModel) 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 *defaultGoodsStatusModel) FindOne(ctx context.Context, id int64) (*GoodsStatus, error) {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", goodsStatusRows, m.table)
|
||||
var resp GoodsStatus
|
||||
err := m.conn.QueryRowCtx(ctx, &resp, query, id)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlx.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultGoodsStatusModel) Insert(ctx context.Context, data *GoodsStatus) (sql.Result, error) {
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?)", m.table, goodsStatusRowsExpectAutoSet)
|
||||
ret, err := m.conn.ExecCtx(ctx, query, data.ApiId, data.ApiProductId, data.Status)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (m *defaultGoodsStatusModel) Update(ctx context.Context, data *GoodsStatus) error {
|
||||
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, goodsStatusRowsWithPlaceHolder)
|
||||
_, err := m.conn.ExecCtx(ctx, query, data.ApiId, data.ApiProductId, data.Status, data.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultGoodsStatusModel) tableName() string {
|
||||
return m.table
|
||||
}
|
5
model/goods_status/vars.go
Normal file
5
model/goods_status/vars.go
Normal file
@ -0,0 +1,5 @@
|
||||
package goods_status
|
||||
|
||||
import "github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
|
||||
var ErrNotFound = sqlx.ErrNotFound
|
Reference in New Issue
Block a user