This commit is contained in:
2025-12-09 18:55:28 +08:00
parent 8d00d67540
commit c23ab8338b
209 changed files with 5445 additions and 3963 deletions

View File

@@ -1,64 +1,68 @@
# 设置输出编码为UTF-8
[Console]::InputEncoding = [System.Text.Encoding]::UTF8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8
chcp.com 65001 | Out-Null
# 目录配置
$OUTPUT_DIR = "./model"
$TARGET_DIR = "../../app/main/model"
$HOME_DIR = Join-Path $PSScriptRoot "..\template"
# 表名列表
$tables = @(
# ============================================
# 新代理系统表
# ============================================
"admin_api",
"admin_dict_data",
"admin_dict_type",
"admin_menu",
"admin_promotion_link",
"admin_promotion_link_stats_history",
"admin_promotion_link_stats_total",
"admin_promotion_order",
"admin_role",
"admin_role_api",
"admin_role_menu",
"admin_user",
"admin_user_role",
"agent",
"agent_commission",
"agent_config",
"agent_freeze_task",
"agent_invite_code",
"agent_invite_code_usage",
"agent_link",
"agent_order",
"agent_product_config",
"agent_real_name",
"agent_rebate",
"agent_relation",
"agent_short_link",
"agent_upgrade",
"agent_wallet",
"agent_withdrawal",
"agent_withdrawal_tax",
"authorization_document",
"example",
"feature",
"global_notifications",
"order",
"order_refund",
"product",
"product_feature",
"query",
"query_cleanup_config",
"query_cleanup_detail",
"query_cleanup_log",
"user",
"user_auth"
# "admin_api",
# "admin_dict_data",
# "admin_dict_type",
# "admin_menu",
# "admin_promotion_link",
# "admin_promotion_link_stats_history",
# "admin_promotion_link_stats_total",
# "admin_promotion_order",
# "admin_role",
# "admin_role_api",
# "admin_role_menu",
# "admin_user",
# "admin_user_role",
# "agent",
# "agent_commission",
"agent_config"
# "agent_freeze_task",
# "agent_invite_code",
# "agent_invite_code_usage",
# "agent_link",
# "agent_order",
# "agent_product_config",
# "agent_real_name",
# "agent_rebate",
# "agent_relation",
# "agent_short_link",
# "agent_upgrade",
# "agent_wallet",
# "agent_withdrawal",
# "agent_withdrawal_tax",
# "authorization_document",
# "example",
# "feature",
# "global_notifications",
# "order",
# "order_refund",
# "product",
# "product_feature",
# "query",
# "query_cleanup_config",
# "query_cleanup_detail",
# "query_cleanup_log",
# "user",
# "user_auth"
)
# 为每个表生成模型
foreach ($table in $tables) {
Write-Host "正在生成表: $table" -ForegroundColor Green
goctl model mysql datasource -url="ycc:5vg67b3UNHu8@tcp(127.0.0.1:21001)/ycc" -table="$table" -dir="./model" --home="../template" -cache=true --style=goZero
goctl model mysql datasource -url="ycc:5vg67b3UNHu8@tcp(127.0.0.1:21001)/ycc" -table="$table" -dir="./model" --home="$HOME_DIR" -cache=true --style=goZero
# 移动生成的文件到目标目录
if (Test-Path $OUTPUT_DIR) {
@@ -122,3 +126,6 @@ foreach ($table in $tables) {
Write-Host ""
Write-Host '所有模型文件生成并移动完成!' -ForegroundColor Green
if (Test-Path $OUTPUT_DIR) {
Get-ChildItem -Path $OUTPUT_DIR -File | Remove-Item -Force
}

View File

@@ -1,9 +0,0 @@
package model
import (
"errors"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
var ErrNotFound = sqlx.ErrNotFound
var ErrNoRowsUpdate = errors.New("update db no rows change")

View File

@@ -15,6 +15,10 @@ ALTER TABLE `user_auth` DROP INDEX IF EXISTS `uk_auth_type_key`;
ALTER TABLE `user_auth`
ADD UNIQUE INDEX `uk_auth_type_key` (`auth_type`, `auth_key`) COMMENT '确保同一个认证方式只能绑定一个用户';
-- 允许一个用户绑定同一认证类型的多个记录例如多个UUID
ALTER TABLE `user_auth` DROP INDEX IF EXISTS `unique_userId_key`;
ALTER TABLE `user_auth` ADD INDEX `idx_user_id_auth_type` (`user_id`, `auth_type`);
-- ============================================
-- 第二部分删除UserTemp表开发环境直接删除
-- ============================================
@@ -41,4 +45,4 @@ DROP TABLE IF EXISTS `user_temp`;
-- ============================================
-- 1. 此脚本适用于开发环境直接删除UserTemp表
-- 2. 如果生产环境有数据,请使用完整版迁移脚本
-- 3. 执行后确保代码中已移除所有对UserTemp表的引用
-- 3. 执行后确保代码中已移除所有对UserTemp表的引用

View File

@@ -0,0 +1,45 @@
-- 将 user_temp 迁移为 usermobile 为空)与 user_auth
START TRANSACTION;
-- 1) 迁移临时用户到 usermobile 置为空)
INSERT INTO
`user` (
`delete_time`,
`del_state`,
`version`,
`mobile`,
`password`,
`nickname`,
`info`,
`inside`
)
SELECT NULL, 0, COALESCE(ut.version, 0), NULL, NULL, NULL, '', 0
FROM `user_temp` ut
WHERE
ut.del_state = 0;
-- 2) 将临时认证迁移到 user_auth按插入顺序关联最近插入的 user.id
INSERT INTO
`user_auth` (
`delete_time`,
`del_state`,
`version`,
`user_id`,
`auth_key`,
`auth_type`
)
SELECT NULL, 0, COALESCE(ut.version, 0), u.id, ut.auth_key, ut.auth_type
FROM `user_temp` ut
JOIN `user` u ON u.del_state = 0
AND u.mobile IS NULL
WHERE
ut.del_state = 0;
-- 注意以上为示意实际生产应通过显式映射如临时ID与新UserID映射表确保一一对应避免笛卡尔匹配。
COMMIT;
-- 唯一索引保障
ALTER TABLE `user_auth`
ADD UNIQUE INDEX `idx_auth_type_key` (`auth_type`, `auth_key`);

View File

@@ -1,16 +1,18 @@
import (
"context"
"database/sql"
"fmt"
"strings"
"context"
"database/sql"
"fmt"
"strings"
{{if .time}}"time"{{end}}
{{if .time}}"time"{{end}}
"reflect"
"ycc-server/common/globalkey"
"github.com/Masterminds/squirrel"
"github.com/pkg/errors"
"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"
"ycc-server/common/globalkey"
"github.com/Masterminds/squirrel"
"github.com/pkg/errors"
"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"
"github.com/google/uuid"
)

View File

@@ -1,17 +1,19 @@
import (
"context"
"database/sql"
"fmt"
"strings"
"context"
"database/sql"
"fmt"
"strings"
{{if .time}}"time"{{end}}
{{if .time}}"time"{{end}}
"reflect"
"ycc-server/common/globalkey"
"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"
"ycc-server/common/globalkey"
"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"
"github.com/google/uuid"
)

View File

@@ -1,17 +1,34 @@
func (m *default{{.upperStartCamelObject}}Model) Insert(ctx context.Context,session sqlx.Session, data *{{.upperStartCamelObject}}) (sql.Result,error) {
data.DelState = globalkey.DelStateNo
{{if .withCache}}{{.keys}}
return m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("insert into %s (%s) values ({{.expression}})", m.table, {{.lowerStartCamelObject}}RowsExpectAutoSet)
if session != nil{
return session.ExecCtx(ctx,query,{{.expressionValues}})
}
return conn.ExecCtx(ctx, query, {{.expressionValues}})
}, {{.keyValues}}){{else}}
query := fmt.Sprintf("insert into %s (%s) values ({{.expression}})", m.table, {{.lowerStartCamelObject}}RowsExpectAutoSet)
if session != nil{
return session.ExecCtx(ctx,query,{{.expressionValues}})
}
return m.conn.ExecCtx(ctx, query, {{.expressionValues}}){{end}}
data.DelState = globalkey.DelStateNo
m.insertUUID(data)
{{if .withCache}}{{.keys}}
return m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("insert into %s (%s) values ({{.expression}})", m.table, {{.lowerStartCamelObject}}RowsExpectAutoSet)
if session != nil{
return session.ExecCtx(ctx,query,{{.expressionValues}})
}
return conn.ExecCtx(ctx, query, {{.expressionValues}})
}, {{.keyValues}}){{else}}
query := fmt.Sprintf("insert into %s (%s) values ({{.expression}})", m.table, {{.lowerStartCamelObject}}RowsExpectAutoSet)
if session != nil{
return session.ExecCtx(ctx,query,{{.expressionValues}})
}
return m.conn.ExecCtx(ctx, query, {{.expressionValues}}){{end}}
}
func (m *default{{.upperStartCamelObject}}Model) insertUUID(data *{{.upperStartCamelObject}}) {
t := reflect.TypeOf(data).Elem()
v := reflect.ValueOf(data).Elem()
for i := 0; i < t.NumField(); i++ {
sf := t.Field(i)
if sf.Tag.Get("db") == "id" {
f := v.Field(i)
if f.IsValid() && f.CanSet() && f.Kind() == reflect.String {
if f.String() == "" {
f.SetString(uuid.NewString())
}
}
break
}
}
}

View File

@@ -1,15 +1,15 @@
type (
{{.lowerStartCamelObject}}Model interface{
{{.method}}
}
{{.lowerStartCamelObject}}Model interface{
{{.method}}
}
default{{.upperStartCamelObject}}Model struct {
{{if .withCache}}sqlc.CachedConn{{else}}conn sqlx.SqlConn{{end}}
table string
}
default{{.upperStartCamelObject}}Model struct {
{{if .withCache}}sqlc.CachedConn{{else}}conn sqlx.SqlConn{{end}}
table string
}
{{.upperStartCamelObject}} struct {
{{.fields}}
}
{{.upperStartCamelObject}} struct {
{{.fields}}
}
)