promote
This commit is contained in:
@@ -1,25 +0,0 @@
|
||||
# 使用方法:
|
||||
# .\genModel.ps1 user user
|
||||
# .\genModel.ps1 user user_auth
|
||||
# 再将 .\genModel 下的文件剪切到对应服务的 model 目录里面,记得改 package
|
||||
# goctl model mysql datasource -url="tyc:5vg67b3UNHu8@tcp(127.0.0.1:20001)/tyc" -table="product" -dir="./model" --home="../template" -cache=true --style=goZero
|
||||
param (
|
||||
[string]$database,
|
||||
[string]$tables
|
||||
)
|
||||
|
||||
# 生成的表名
|
||||
$modeldir = "./genModel"
|
||||
$templateDir = Join-Path -Path (Resolve-Path "$PSScriptRoot/..") -ChildPath "template"
|
||||
# 数据库配置
|
||||
$host = "127.0.0.1"
|
||||
$port = "20001"
|
||||
$dbname = "$database"
|
||||
$username = "tyc"
|
||||
$passwd = "5vg67b3UNHu8"
|
||||
|
||||
Write-Output "开始创建库:$dbname 的表:$tables"
|
||||
|
||||
# 执行 goctl 命令生成 model
|
||||
$command = "goctl model mysql datasource -url=`"$username`:$passwd`@tcp($host`:$port)/$dbname`" -table=`"$tables`" -dir=`"$modeldir`" --home=`"$templateDir`" -cache=true --style=goZero"
|
||||
Invoke-Expression $command
|
||||
@@ -7,7 +7,7 @@ $TEMPLATE_DIR = "../template"
|
||||
|
||||
# 表名列表
|
||||
$tables = @(
|
||||
"example_params"
|
||||
# "example_params"
|
||||
# "feature",
|
||||
# "global_notifications",
|
||||
# "order",
|
||||
@@ -17,6 +17,19 @@ $tables = @(
|
||||
# "query",
|
||||
# "user",
|
||||
# "user_auth"
|
||||
# "admin_user"
|
||||
# "admin_user_role"
|
||||
# "admin_api",
|
||||
# "admin_menu"
|
||||
# "admin_role",
|
||||
# "admin_role_api",
|
||||
# "admin_role_menu",
|
||||
# "admin_dict_data"
|
||||
# "admin_dict_type"
|
||||
# "admin_promotion_link"
|
||||
# "admin_promotion_link_stats_total"
|
||||
# "admin_promotion_link_stats_history"
|
||||
"admin_promotion_order"
|
||||
)
|
||||
|
||||
# 为每个表生成模型
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
)
|
||||
|
||||
var _ ExampleModel = (*customExampleModel)(nil)
|
||||
|
||||
type (
|
||||
// ExampleModel is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in customExampleModel.
|
||||
ExampleModel interface {
|
||||
exampleModel
|
||||
}
|
||||
|
||||
customExampleModel struct {
|
||||
*defaultExampleModel
|
||||
}
|
||||
)
|
||||
|
||||
// NewExampleModel returns a model for the database table.
|
||||
func NewExampleModel(conn sqlx.SqlConn, c cache.CacheConf) ExampleModel {
|
||||
return &customExampleModel{
|
||||
defaultExampleModel: newExampleModel(conn, c),
|
||||
}
|
||||
}
|
||||
@@ -1,434 +0,0 @@
|
||||
// Code generated by goctl. DO NOT EDIT!
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"time"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/stores/builder"
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
"tyc-server/common/globalkey"
|
||||
)
|
||||
|
||||
var (
|
||||
exampleFieldNames = builder.RawFieldNames(&Example{})
|
||||
exampleRows = strings.Join(exampleFieldNames, ",")
|
||||
exampleRowsExpectAutoSet = strings.Join(stringx.Remove(exampleFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
|
||||
exampleRowsWithPlaceHolder = strings.Join(stringx.Remove(exampleFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
|
||||
|
||||
cacheTycExampleIdPrefix = "cache:tyc:example:id:"
|
||||
cacheTycExampleApiIdPrefix = "cache:tyc:example:apiId:"
|
||||
cacheTycExampleFeatureIdPrefix = "cache:tyc:example:featureId:"
|
||||
)
|
||||
|
||||
type (
|
||||
exampleModel interface {
|
||||
Insert(ctx context.Context, session sqlx.Session, data *Example) (sql.Result, error)
|
||||
FindOne(ctx context.Context, id int64) (*Example, error)
|
||||
FindOneByApiId(ctx context.Context, apiId string) (*Example, error)
|
||||
FindOneByFeatureId(ctx context.Context, featureId int64) (*Example, error)
|
||||
Update(ctx context.Context, session sqlx.Session, data *Example) (sql.Result, error)
|
||||
UpdateWithVersion(ctx context.Context, session sqlx.Session, data *Example) error
|
||||
Trans(ctx context.Context, fn func(context context.Context, session sqlx.Session) error) error
|
||||
SelectBuilder() squirrel.SelectBuilder
|
||||
DeleteSoft(ctx context.Context, session sqlx.Session, data *Example) error
|
||||
FindSum(ctx context.Context, sumBuilder squirrel.SelectBuilder, field string) (float64, error)
|
||||
FindCount(ctx context.Context, countBuilder squirrel.SelectBuilder, field string) (int64, error)
|
||||
FindAll(ctx context.Context, rowBuilder squirrel.SelectBuilder, orderBy string) ([]*Example, error)
|
||||
FindPageListByPage(ctx context.Context, rowBuilder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*Example, error)
|
||||
FindPageListByPageWithTotal(ctx context.Context, rowBuilder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*Example, int64, error)
|
||||
FindPageListByIdDESC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMinId, pageSize int64) ([]*Example, error)
|
||||
FindPageListByIdASC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMaxId, pageSize int64) ([]*Example, error)
|
||||
Delete(ctx context.Context, session sqlx.Session, id int64) error
|
||||
}
|
||||
|
||||
defaultExampleModel struct {
|
||||
sqlc.CachedConn
|
||||
table string
|
||||
}
|
||||
|
||||
Example struct {
|
||||
Id int64 `db:"id"` // 主键ID
|
||||
CreateTime time.Time `db:"create_time"` // 创建时间
|
||||
UpdateTime time.Time `db:"update_time"` // 更新时间
|
||||
DeleteTime sql.NullTime `db:"delete_time"` // 删除时间
|
||||
DelState int64 `db:"del_state"` // 删除状态
|
||||
Version int64 `db:"version"` // 版本号
|
||||
ApiId string `db:"api_id"` // API标识
|
||||
FeatureId int64 `db:"feature_id"` // 关联feature表的ID
|
||||
Content string `db:"content"` // 内容
|
||||
}
|
||||
)
|
||||
|
||||
func newExampleModel(conn sqlx.SqlConn, c cache.CacheConf) *defaultExampleModel {
|
||||
return &defaultExampleModel{
|
||||
CachedConn: sqlc.NewConn(conn, c),
|
||||
table: "`example`",
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultExampleModel) Insert(ctx context.Context, session sqlx.Session, data *Example) (sql.Result, error) {
|
||||
data.DelState = globalkey.DelStateNo
|
||||
tycExampleApiIdKey := fmt.Sprintf("%s%v", cacheTycExampleApiIdPrefix, data.ApiId)
|
||||
tycExampleFeatureIdKey := fmt.Sprintf("%s%v", cacheTycExampleFeatureIdPrefix, data.FeatureId)
|
||||
tycExampleIdKey := fmt.Sprintf("%s%v", cacheTycExampleIdPrefix, data.Id)
|
||||
return m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?)", m.table, exampleRowsExpectAutoSet)
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.ApiId, data.FeatureId, data.Content)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.ApiId, data.FeatureId, data.Content)
|
||||
}, tycExampleApiIdKey, tycExampleFeatureIdKey, tycExampleIdKey)
|
||||
}
|
||||
|
||||
func (m *defaultExampleModel) FindOne(ctx context.Context, id int64) (*Example, error) {
|
||||
tycExampleIdKey := fmt.Sprintf("%s%v", cacheTycExampleIdPrefix, id)
|
||||
var resp Example
|
||||
err := m.QueryRowCtx(ctx, &resp, tycExampleIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", exampleRows, m.table)
|
||||
return conn.QueryRowCtx(ctx, v, query, id, globalkey.DelStateNo)
|
||||
})
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultExampleModel) FindOneByApiId(ctx context.Context, apiId string) (*Example, error) {
|
||||
tycExampleApiIdKey := fmt.Sprintf("%s%v", cacheTycExampleApiIdPrefix, apiId)
|
||||
var resp Example
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, tycExampleApiIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
||||
query := fmt.Sprintf("select %s from %s where `api_id` = ? and del_state = ? limit 1", exampleRows, m.table)
|
||||
if err := conn.QueryRowCtx(ctx, &resp, query, apiId, globalkey.DelStateNo); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.Id, nil
|
||||
}, m.queryPrimary)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultExampleModel) FindOneByFeatureId(ctx context.Context, featureId int64) (*Example, error) {
|
||||
tycExampleFeatureIdKey := fmt.Sprintf("%s%v", cacheTycExampleFeatureIdPrefix, featureId)
|
||||
var resp Example
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, tycExampleFeatureIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
||||
query := fmt.Sprintf("select %s from %s where `feature_id` = ? and del_state = ? limit 1", exampleRows, m.table)
|
||||
if err := conn.QueryRowCtx(ctx, &resp, query, featureId, globalkey.DelStateNo); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.Id, nil
|
||||
}, m.queryPrimary)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultExampleModel) Update(ctx context.Context, session sqlx.Session, newData *Example) (sql.Result, error) {
|
||||
data, err := m.FindOne(ctx, newData.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tycExampleApiIdKey := fmt.Sprintf("%s%v", cacheTycExampleApiIdPrefix, data.ApiId)
|
||||
tycExampleFeatureIdKey := fmt.Sprintf("%s%v", cacheTycExampleFeatureIdPrefix, data.FeatureId)
|
||||
tycExampleIdKey := fmt.Sprintf("%s%v", cacheTycExampleIdPrefix, data.Id)
|
||||
return m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, exampleRowsWithPlaceHolder)
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.ApiId, newData.FeatureId, newData.Content, newData.Id)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.ApiId, newData.FeatureId, newData.Content, newData.Id)
|
||||
}, tycExampleApiIdKey, tycExampleFeatureIdKey, tycExampleIdKey)
|
||||
}
|
||||
|
||||
func (m *defaultExampleModel) UpdateWithVersion(ctx context.Context, session sqlx.Session, newData *Example) error {
|
||||
|
||||
oldVersion := newData.Version
|
||||
newData.Version += 1
|
||||
|
||||
var sqlResult sql.Result
|
||||
var err error
|
||||
|
||||
data, err := m.FindOne(ctx, newData.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tycExampleApiIdKey := fmt.Sprintf("%s%v", cacheTycExampleApiIdPrefix, data.ApiId)
|
||||
tycExampleFeatureIdKey := fmt.Sprintf("%s%v", cacheTycExampleFeatureIdPrefix, data.FeatureId)
|
||||
tycExampleIdKey := fmt.Sprintf("%s%v", cacheTycExampleIdPrefix, data.Id)
|
||||
sqlResult, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("update %s set %s where `id` = ? and version = ? ", m.table, exampleRowsWithPlaceHolder)
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.ApiId, newData.FeatureId, newData.Content, newData.Id, oldVersion)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.ApiId, newData.FeatureId, newData.Content, newData.Id, oldVersion)
|
||||
}, tycExampleApiIdKey, tycExampleFeatureIdKey, tycExampleIdKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
updateCount, err := sqlResult.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if updateCount == 0 {
|
||||
return ErrNoRowsUpdate
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *defaultExampleModel) DeleteSoft(ctx context.Context, session sqlx.Session, data *Example) error {
|
||||
data.DelState = globalkey.DelStateYes
|
||||
data.DeleteTime = sql.NullTime{Time: time.Now(), Valid: true}
|
||||
if err := m.UpdateWithVersion(ctx, session, data); err != nil {
|
||||
return errors.Wrapf(errors.New("delete soft failed "), "ExampleModel delete err : %+v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *defaultExampleModel) FindSum(ctx context.Context, builder squirrel.SelectBuilder, field string) (float64, error) {
|
||||
|
||||
if len(field) == 0 {
|
||||
return 0, errors.Wrapf(errors.New("FindSum Least One Field"), "FindSum Least One Field")
|
||||
}
|
||||
|
||||
builder = builder.Columns("IFNULL(SUM(" + field + "),0)")
|
||||
|
||||
query, values, err := builder.Where("del_state = ?", globalkey.DelStateNo).ToSql()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var resp float64
|
||||
err = m.QueryRowNoCacheCtx(ctx, &resp, query, values...)
|
||||
switch err {
|
||||
case nil:
|
||||
return resp, nil
|
||||
default:
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultExampleModel) FindCount(ctx context.Context, builder squirrel.SelectBuilder, field string) (int64, error) {
|
||||
|
||||
if len(field) == 0 {
|
||||
return 0, errors.Wrapf(errors.New("FindCount Least One Field"), "FindCount Least One Field")
|
||||
}
|
||||
|
||||
builder = builder.Columns("COUNT(" + field + ")")
|
||||
|
||||
query, values, err := builder.Where("del_state = ?", globalkey.DelStateNo).ToSql()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var resp int64
|
||||
err = m.QueryRowNoCacheCtx(ctx, &resp, query, values...)
|
||||
switch err {
|
||||
case nil:
|
||||
return resp, nil
|
||||
default:
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultExampleModel) FindAll(ctx context.Context, builder squirrel.SelectBuilder, orderBy string) ([]*Example, error) {
|
||||
|
||||
builder = builder.Columns(exampleRows)
|
||||
|
||||
if orderBy == "" {
|
||||
builder = builder.OrderBy("id DESC")
|
||||
} else {
|
||||
builder = builder.OrderBy(orderBy)
|
||||
}
|
||||
|
||||
query, values, err := builder.Where("del_state = ?", globalkey.DelStateNo).ToSql()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var resp []*Example
|
||||
err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...)
|
||||
switch err {
|
||||
case nil:
|
||||
return resp, nil
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultExampleModel) FindPageListByPage(ctx context.Context, builder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*Example, error) {
|
||||
|
||||
builder = builder.Columns(exampleRows)
|
||||
|
||||
if orderBy == "" {
|
||||
builder = builder.OrderBy("id DESC")
|
||||
} else {
|
||||
builder = builder.OrderBy(orderBy)
|
||||
}
|
||||
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
offset := (page - 1) * pageSize
|
||||
|
||||
query, values, err := builder.Where("del_state = ?", globalkey.DelStateNo).Offset(uint64(offset)).Limit(uint64(pageSize)).ToSql()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var resp []*Example
|
||||
err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...)
|
||||
switch err {
|
||||
case nil:
|
||||
return resp, nil
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultExampleModel) FindPageListByPageWithTotal(ctx context.Context, builder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*Example, int64, error) {
|
||||
|
||||
total, err := m.FindCount(ctx, builder, "id")
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
builder = builder.Columns(exampleRows)
|
||||
|
||||
if orderBy == "" {
|
||||
builder = builder.OrderBy("id DESC")
|
||||
} else {
|
||||
builder = builder.OrderBy(orderBy)
|
||||
}
|
||||
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
offset := (page - 1) * pageSize
|
||||
|
||||
query, values, err := builder.Where("del_state = ?", globalkey.DelStateNo).Offset(uint64(offset)).Limit(uint64(pageSize)).ToSql()
|
||||
if err != nil {
|
||||
return nil, total, err
|
||||
}
|
||||
|
||||
var resp []*Example
|
||||
err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...)
|
||||
switch err {
|
||||
case nil:
|
||||
return resp, total, nil
|
||||
default:
|
||||
return nil, total, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultExampleModel) FindPageListByIdDESC(ctx context.Context, builder squirrel.SelectBuilder, preMinId, pageSize int64) ([]*Example, error) {
|
||||
|
||||
builder = builder.Columns(exampleRows)
|
||||
|
||||
if preMinId > 0 {
|
||||
builder = builder.Where(" id < ? ", preMinId)
|
||||
}
|
||||
|
||||
query, values, err := builder.Where("del_state = ?", globalkey.DelStateNo).OrderBy("id DESC").Limit(uint64(pageSize)).ToSql()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var resp []*Example
|
||||
err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...)
|
||||
switch err {
|
||||
case nil:
|
||||
return resp, nil
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultExampleModel) FindPageListByIdASC(ctx context.Context, builder squirrel.SelectBuilder, preMaxId, pageSize int64) ([]*Example, error) {
|
||||
|
||||
builder = builder.Columns(exampleRows)
|
||||
|
||||
if preMaxId > 0 {
|
||||
builder = builder.Where(" id > ? ", preMaxId)
|
||||
}
|
||||
|
||||
query, values, err := builder.Where("del_state = ?", globalkey.DelStateNo).OrderBy("id ASC").Limit(uint64(pageSize)).ToSql()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var resp []*Example
|
||||
err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...)
|
||||
switch err {
|
||||
case nil:
|
||||
return resp, nil
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultExampleModel) Trans(ctx context.Context, fn func(ctx context.Context, session sqlx.Session) error) error {
|
||||
|
||||
return m.TransactCtx(ctx, func(ctx context.Context, session sqlx.Session) error {
|
||||
return fn(ctx, session)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func (m *defaultExampleModel) SelectBuilder() squirrel.SelectBuilder {
|
||||
return squirrel.Select().From(m.table)
|
||||
}
|
||||
func (m *defaultExampleModel) Delete(ctx context.Context, session sqlx.Session, id int64) error {
|
||||
data, err := m.FindOne(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tycExampleApiIdKey := fmt.Sprintf("%s%v", cacheTycExampleApiIdPrefix, data.ApiId)
|
||||
tycExampleFeatureIdKey := fmt.Sprintf("%s%v", cacheTycExampleFeatureIdPrefix, data.FeatureId)
|
||||
tycExampleIdKey := fmt.Sprintf("%s%v", cacheTycExampleIdPrefix, id)
|
||||
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, id)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, id)
|
||||
}, tycExampleApiIdKey, tycExampleFeatureIdKey, tycExampleIdKey)
|
||||
return err
|
||||
}
|
||||
func (m *defaultExampleModel) formatPrimary(primary interface{}) string {
|
||||
return fmt.Sprintf("%s%v", cacheTycExampleIdPrefix, primary)
|
||||
}
|
||||
func (m *defaultExampleModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary interface{}) error {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", exampleRows, m.table)
|
||||
return conn.QueryRowCtx(ctx, v, query, primary, globalkey.DelStateNo)
|
||||
}
|
||||
|
||||
func (m *defaultExampleModel) tableName() string {
|
||||
return m.table
|
||||
}
|
||||
@@ -10,10 +10,12 @@ CREATE TABLE `admin_user` (
|
||||
/* 业务字段开始 */
|
||||
`username` varchar(50) NOT NULL DEFAULT '' COMMENT '用户名',
|
||||
`password` varchar(255) NOT NULL DEFAULT '' COMMENT '密码',
|
||||
`real_name` varchar(50) NOT NULL DEFAULT '' COMMENT '真实姓名',
|
||||
`status` tinyint NOT NULL DEFAULT '1' COMMENT '状态:0-禁用,1-启用',
|
||||
/* 业务字段结束 */
|
||||
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `uk_real_name` (`real_name`),
|
||||
UNIQUE KEY `uk_username` (`username`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='管理员用户表';
|
||||
|
||||
@@ -48,21 +50,24 @@ CREATE TABLE `admin_menu` (
|
||||
`version` bigint NOT NULL DEFAULT '0' COMMENT '版本号',
|
||||
|
||||
/* 业务字段开始 */
|
||||
`parent_id` bigint NOT NULL DEFAULT '0' COMMENT '父菜单ID',
|
||||
`menu_name` varchar(50) NOT NULL DEFAULT '' COMMENT '菜单名称',
|
||||
`menu_code` varchar(100) NOT NULL DEFAULT '' COMMENT '菜单编码',
|
||||
`pid` bigint NOT NULL DEFAULT '0' COMMENT '父菜单ID',
|
||||
`name` varchar(100) NOT NULL DEFAULT '' COMMENT '路由名称',
|
||||
`path` varchar(255) NOT NULL DEFAULT '' COMMENT '路由路径',
|
||||
`component` varchar(255) NOT NULL DEFAULT '' COMMENT '组件路径',
|
||||
`icon` varchar(100) NOT NULL DEFAULT '' COMMENT '图标',
|
||||
`sort` int NOT NULL DEFAULT '0' COMMENT '排序',
|
||||
`visible` tinyint NOT NULL DEFAULT '1' COMMENT '是否可见:0-隐藏,1-显示',
|
||||
`status` tinyint NOT NULL DEFAULT '1' COMMENT '状态:0-禁用,1-启用',
|
||||
`is_cache` tinyint NOT NULL DEFAULT '0' COMMENT '是否缓存:0-不缓存,1-缓存',
|
||||
`redirect` varchar(255) DEFAULT NULL COMMENT '重定向路径',
|
||||
|
||||
/* Meta配置JSON字段 */
|
||||
`meta` json NOT NULL COMMENT '路由元数据配置',
|
||||
|
||||
/* 附加信息 */
|
||||
`status` tinyint NOT NULL DEFAULT '1' COMMENT '状态: 0-禁用, 1-启用',
|
||||
`type` tinyint NOT NULL DEFAULT '0' COMMENT '类型: 0-目录, 1-菜单, 2-按钮',
|
||||
`sort` int NOT NULL DEFAULT '0' COMMENT '排序号',
|
||||
/* 业务字段结束 */
|
||||
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `uk_menu_code` (`menu_code`),
|
||||
KEY `idx_parent_id` (`parent_id`) COMMENT '优化层级查询'
|
||||
UNIQUE KEY `uk_name_path` (`name`, `path`),
|
||||
KEY `idx_pid` (`pid`) COMMENT '优化层级查询'
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='菜单表';
|
||||
|
||||
-- 接口表
|
||||
@@ -149,4 +154,43 @@ CREATE TABLE `admin_role_api` (
|
||||
UNIQUE KEY `uk_role_api` (`role_id`, `api_id`),
|
||||
KEY `idx_role_id` (`role_id`) COMMENT '优化角色查询',
|
||||
KEY `idx_api_id` (`api_id`) COMMENT '优化接口查询'
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='角色接口关联表';
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='角色接口关联表';
|
||||
-- 字典类型表
|
||||
CREATE TABLE `admin_dict_type` (
|
||||
`id` bigint NOT NULL AUTO_INCREMENT,
|
||||
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
`delete_time` datetime DEFAULT NULL COMMENT '删除时间',
|
||||
`del_state` tinyint NOT NULL DEFAULT '0',
|
||||
`version` bigint NOT NULL DEFAULT '0' COMMENT '版本号',
|
||||
|
||||
`dict_type` varchar(50) NOT NULL COMMENT '字典类型编码',
|
||||
`dict_name` varchar(100) NOT NULL COMMENT '字典类型名称',
|
||||
`status` tinyint NOT NULL DEFAULT '1' COMMENT '状态:0-禁用,1-启用',
|
||||
`remark` varchar(255) DEFAULT NULL COMMENT '备注',
|
||||
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `uk_dict_type` (`dict_type`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='字典类型表';
|
||||
|
||||
-- 字典数据表
|
||||
CREATE TABLE `admin_dict_data` (
|
||||
`id` bigint NOT NULL AUTO_INCREMENT,
|
||||
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
`delete_time` datetime DEFAULT NULL COMMENT '删除时间',
|
||||
`del_state` tinyint NOT NULL DEFAULT '0',
|
||||
`version` bigint NOT NULL DEFAULT '0' COMMENT '版本号',
|
||||
|
||||
`dict_type` varchar(50) NOT NULL COMMENT '字典类型编码',
|
||||
`dict_label` varchar(100) NOT NULL COMMENT '字典标签',
|
||||
`dict_value` tinyint NOT NULL COMMENT '字典键值',
|
||||
`dict_sort` int NOT NULL DEFAULT '0' COMMENT '字典排序',
|
||||
`status` tinyint NOT NULL DEFAULT '1' COMMENT '状态:0-禁用,1-启用',
|
||||
`remark` varchar(255) DEFAULT NULL COMMENT '备注',
|
||||
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `uk_type_value` (`dict_type`, `dict_value`),
|
||||
UNIQUE KEY `uk_type_label` (`dict_type`, `dict_label`),
|
||||
KEY `idx_dict_type` (`dict_type`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='字典数据表';
|
||||
92
deploy/sql/promotion.sql
Normal file
92
deploy/sql/promotion.sql
Normal file
@@ -0,0 +1,92 @@
|
||||
CREATE TABLE `admin_promotion_link` (
|
||||
`id` bigint NOT NULL AUTO_INCREMENT,
|
||||
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
`delete_time` datetime DEFAULT NULL COMMENT '删除时间',
|
||||
`del_state` tinyint NOT NULL DEFAULT '0',
|
||||
`version` bigint NOT NULL DEFAULT '0' COMMENT '版本号',
|
||||
|
||||
/* 业务字段开始 */
|
||||
`url` varchar(1024) NOT NULL COMMENT '推广链接URL',
|
||||
`name` varchar(100) NOT NULL COMMENT '推广链接名称',
|
||||
`admin_user_id` bigint NOT NULL DEFAULT '0' COMMENT '推广者账号ID',
|
||||
/* 业务字段结束 */
|
||||
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `uk_url` (`url`(255)) COMMENT '确保URL唯一',
|
||||
KEY `idx_name` (`name`) COMMENT '优化名称查询',
|
||||
KEY `idx_admin_user_id` (`admin_user_id`) COMMENT '优化推广者查询'
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='推广链接表';
|
||||
|
||||
-- 总统计表(缓存汇总数据,提升查询性能)
|
||||
CREATE TABLE `admin_promotion_link_stats_total` (
|
||||
`id` bigint NOT NULL AUTO_INCREMENT,
|
||||
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
`delete_time` datetime DEFAULT NULL COMMENT '删除时间',
|
||||
`del_state` tinyint NOT NULL DEFAULT '0',
|
||||
`version` bigint NOT NULL DEFAULT '0' COMMENT '版本号',
|
||||
|
||||
/* 业务字段开始 */
|
||||
`link_id` bigint NOT NULL COMMENT '推广链接ID',
|
||||
`click_count` bigint NOT NULL DEFAULT '0' COMMENT '总点击数',
|
||||
`pay_count` bigint NOT NULL DEFAULT '0' COMMENT '总付费次数',
|
||||
`pay_amount` decimal(20,2) NOT NULL DEFAULT '0.00' COMMENT '总付费金额',
|
||||
`last_click_time` datetime DEFAULT NULL COMMENT '最后点击时间',
|
||||
`last_pay_time` datetime DEFAULT NULL COMMENT '最后付费时间',
|
||||
/* 业务字段结束 */
|
||||
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `uk_link_id` (`link_id`) COMMENT '确保每个链接只有一条总统计记录'
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='推广链接总统计表(缓存汇总数据,提升查询性能)';
|
||||
|
||||
-- 历史统计表(按天统计,包含当天实时统计)
|
||||
CREATE TABLE `admin_promotion_link_stats_history` (
|
||||
`id` bigint NOT NULL AUTO_INCREMENT,
|
||||
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
`delete_time` datetime DEFAULT NULL COMMENT '删除时间',
|
||||
`del_state` tinyint NOT NULL DEFAULT '0',
|
||||
`version` bigint NOT NULL DEFAULT '0' COMMENT '版本号',
|
||||
|
||||
/* 业务字段开始 */
|
||||
`link_id` bigint NOT NULL COMMENT '推广链接ID',
|
||||
`stats_date` date NOT NULL COMMENT '统计日期',
|
||||
`click_count` bigint NOT NULL DEFAULT '0' COMMENT '点击数',
|
||||
`pay_count` bigint NOT NULL DEFAULT '0' COMMENT '付费次数',
|
||||
`pay_amount` decimal(20,2) NOT NULL DEFAULT '0.00' COMMENT '付费金额',
|
||||
`last_click_time` datetime DEFAULT NULL COMMENT '最后点击时间',
|
||||
`last_pay_time` datetime DEFAULT NULL COMMENT '最后付费时间',
|
||||
/* 业务字段结束 */
|
||||
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `uk_link_date` (`link_id`, `stats_date`) COMMENT '确保每个链接每天只有一条统计记录',
|
||||
KEY `idx_stats_date` (`stats_date`) COMMENT '优化日期查询',
|
||||
KEY `idx_link_id` (`link_id`) COMMENT '优化链接查询',
|
||||
KEY `idx_link_date` (`link_id`, `stats_date`) COMMENT '优化链接日期查询'
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='推广链接历史统计表';
|
||||
|
||||
-- 推广订单表
|
||||
CREATE TABLE `admin_promotion_order` (
|
||||
`id` bigint NOT NULL AUTO_INCREMENT,
|
||||
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
`delete_time` datetime DEFAULT NULL COMMENT '删除时间',
|
||||
`del_state` tinyint NOT NULL DEFAULT '0',
|
||||
`version` bigint NOT NULL DEFAULT '0' COMMENT '版本号',
|
||||
|
||||
/* 业务字段开始 */
|
||||
`link_id` bigint NOT NULL COMMENT '推广链接ID',
|
||||
`order_id` bigint NOT NULL COMMENT '订单ID',
|
||||
`user_id` bigint NOT NULL COMMENT '下单用户ID',
|
||||
`admin_user_id` bigint NOT NULL COMMENT '推广者账号ID',
|
||||
/* 业务字段结束 */
|
||||
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `uk_order_id` (`order_id`) COMMENT '确保每个订单只有一条推广记录',
|
||||
UNIQUE KEY `uk_url` (`url`(255)) COMMENT '确保URL唯一',
|
||||
KEY `idx_link_id` (`link_id`) COMMENT '优化链接查询',
|
||||
KEY `idx_user_id` (`user_id`) COMMENT '优化用户查询',
|
||||
KEY `idx_admin_user_id` (`admin_user_id`) COMMENT '优化推广者查询'
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='推广订单表';
|
||||
|
||||
Reference in New Issue
Block a user