first commit
This commit is contained in:
130
deploy/script/gen_models.ps1
Normal file
130
deploy/script/gen_models.ps1
Normal file
@@ -0,0 +1,130 @@
|
||||
# 设置输出编码为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_role",
|
||||
# "admin_role_api",
|
||||
# "admin_role_menu",
|
||||
# "admin_user",
|
||||
# "admin_user_role",
|
||||
|
||||
# ============================================
|
||||
# 代理系统表(简化后)
|
||||
# ============================================
|
||||
# "agent", # 代理表(简化:移除 level, team_leader_id, invite_code_id)
|
||||
# "agent_commission", # 佣金表(简化:状态只有 1=已发放, 2=已取消)
|
||||
# "agent_config", # 代理配置
|
||||
# "agent_freeze_task", # 冻结任务
|
||||
# "agent_link", # 代理链接
|
||||
# "agent_order", # 代理订单
|
||||
# "agent_product_config", # 产品配置
|
||||
# "agent_short_link", # 短链接
|
||||
# "agent_wallet", # 钱包
|
||||
# "agent_withdraw" # 提现
|
||||
|
||||
# ============================================
|
||||
# 业务功能表
|
||||
# ============================================
|
||||
# "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", # 用户认证
|
||||
# "user_temp" # 临时用户
|
||||
# "yunyin_sign_pay_order" # 云印签签署流程
|
||||
)
|
||||
|
||||
# 为每个表生成模型
|
||||
foreach ($table in $tables) {
|
||||
Write-Host "正在生成表: $table" -ForegroundColor Green
|
||||
goctl model mysql datasource -url="in:5vg67b3UNHu8@tcp(127.0.0.1:21501)/in" -table="$table" -dir="./model" --home="$HOME_DIR" -cache=true --style=goZero
|
||||
|
||||
# 移动生成的文件到目标目录
|
||||
if (Test-Path $OUTPUT_DIR) {
|
||||
$sourceFiles = Get-ChildItem -Path $OUTPUT_DIR -File
|
||||
|
||||
foreach ($file in $sourceFiles) {
|
||||
$fileName = $file.Name
|
||||
$targetPath = Join-Path $TARGET_DIR $fileName
|
||||
$sourcePath = $file.FullName
|
||||
|
||||
# 检查文件类型并决定是否移动
|
||||
$shouldMove = $false
|
||||
$shouldOverwrite = $false
|
||||
|
||||
if ($fileName -eq "vars.go") {
|
||||
# vars.go: 如果目标目录不存在才移动
|
||||
if (-not (Test-Path $targetPath)) {
|
||||
$shouldMove = $true
|
||||
Write-Host " 移动 $fileName (vars.go 不存在于目标目录)" -ForegroundColor Yellow
|
||||
}
|
||||
else {
|
||||
Write-Host " 跳过 $fileName (vars.go 已存在于目标目录,防止覆盖)" -ForegroundColor Cyan
|
||||
}
|
||||
}
|
||||
elseif ($fileName -match "_gen\.go$") {
|
||||
# 带 _gen 后缀的文件: 直接覆盖
|
||||
$shouldMove = $true
|
||||
$shouldOverwrite = $true
|
||||
Write-Host " 移动 $fileName (覆盖 _gen 文件)" -ForegroundColor Yellow
|
||||
}
|
||||
else {
|
||||
# 不带 _gen 后缀的文件: 如果目标目录不存在才移动
|
||||
if (-not (Test-Path $targetPath)) {
|
||||
$shouldMove = $true
|
||||
Write-Host " 移动 $fileName (非 _gen 文件不存在于目标目录)" -ForegroundColor Yellow
|
||||
}
|
||||
else {
|
||||
Write-Host " 跳过 $fileName (非 _gen 文件已存在于目标目录,防止覆盖)" -ForegroundColor Cyan
|
||||
}
|
||||
}
|
||||
|
||||
# 执行移动操作
|
||||
if ($shouldMove) {
|
||||
# 确保目标目录存在
|
||||
if (-not (Test-Path $TARGET_DIR)) {
|
||||
New-Item -ItemType Directory -Path $TARGET_DIR -Force | Out-Null
|
||||
}
|
||||
|
||||
# 如果目标文件存在且需要覆盖,先删除
|
||||
if ($shouldOverwrite -and (Test-Path $targetPath)) {
|
||||
Remove-Item -Path $targetPath -Force
|
||||
}
|
||||
|
||||
# 移动文件
|
||||
Move-Item -Path $sourcePath -Destination $targetPath -Force
|
||||
Write-Host " ✓ 已移动到: $targetPath" -ForegroundColor Green
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Write-Host ""
|
||||
Write-Host '所有模型文件生成并移动完成!' -ForegroundColor Green
|
||||
if (Test-Path $OUTPUT_DIR) {
|
||||
Get-ChildItem -Path $OUTPUT_DIR -File | Remove-Item -Force
|
||||
}
|
||||
320
deploy/sql/README_表结构修改说明.md
Normal file
320
deploy/sql/README_表结构修改说明.md
Normal file
@@ -0,0 +1,320 @@
|
||||
# 表结构修改说明
|
||||
|
||||
本文档说明系统重构需要的所有表结构修改,包括:
|
||||
1. UUID迁移(将所有bigint类型的ID改为CHAR(36)类型的UUID)
|
||||
2. 用户系统重构(删除UserTemp表,优化UserAuth表)
|
||||
|
||||
## 一、UUID迁移
|
||||
|
||||
### 1.1 概述
|
||||
|
||||
将所有表的主键ID从`bigint`类型改为`CHAR(36)`类型的UUID,同时将所有外键关联字段也改为UUID类型。
|
||||
|
||||
**注意**:开发环境使用简化版脚本(`uuid_migration_simple.sql`),直接修改表结构,不保留旧数据。
|
||||
|
||||
### 1.2 涉及的表
|
||||
|
||||
**核心业务表**:
|
||||
- `user` - 用户表
|
||||
- `agent` - 代理表
|
||||
- `product` - 产品表
|
||||
- `order` - 订单表
|
||||
- `query` - 查询报告表
|
||||
- `user_auth` - 用户认证表
|
||||
|
||||
**代理相关表**:
|
||||
- `agent_commission` - 代理佣金表
|
||||
- `agent_invite_code` - 代理邀请码表
|
||||
- `agent_link` - 代理链接表
|
||||
- `agent_order` - 代理订单表
|
||||
- `agent_wallet` - 代理钱包表
|
||||
- `agent_withdrawal` - 代理提现表
|
||||
- `agent_withdrawal_tax` - 代理提现税费表
|
||||
- `agent_rebate` - 代理返利表
|
||||
- `agent_relation` - 代理关系表
|
||||
- `agent_upgrade` - 代理升级表
|
||||
- `agent_real_name` - 代理实名表
|
||||
- `agent_config` - 代理配置表
|
||||
- `agent_product_config` - 代理产品配置表
|
||||
- `agent_short_link` - 代理短链表
|
||||
- `agent_invite_code_usage` - 邀请码使用记录表
|
||||
- `agent_freeze_task` - 代理冻结任务表
|
||||
|
||||
**订单相关表**:
|
||||
- `order_refund` - 订单退款表
|
||||
|
||||
**查询相关表**:
|
||||
- `query_cleanup_log` - 查询清理日志表
|
||||
- `query_cleanup_detail` - 查询清理详情表
|
||||
- `query_cleanup_config` - 查询清理配置表
|
||||
|
||||
**产品相关表**:
|
||||
- `product_feature` - 产品功能表
|
||||
- `feature` - 功能表
|
||||
|
||||
**其他表**:
|
||||
- `authorization_document` - 授权书表
|
||||
- `global_notifications` - 全局通知表
|
||||
|
||||
**管理后台表**:
|
||||
- `admin_user` - 管理员用户表
|
||||
- `admin_role` - 管理员角色表
|
||||
- `admin_menu` - 管理员菜单表
|
||||
- `admin_api` - 管理员API表
|
||||
- `admin_dict_type` - 字典类型表
|
||||
- `admin_dict_data` - 字典数据表
|
||||
- `admin_user_role` - 管理员用户角色关联表
|
||||
- `admin_role_menu` - 管理员角色菜单关联表
|
||||
- `admin_role_api` - 管理员角色API关联表
|
||||
|
||||
### 1.3 修改内容
|
||||
|
||||
**主键字段**:
|
||||
- 所有表的`id`字段:`bigint` → `CHAR(36)`
|
||||
- 移除`AUTO_INCREMENT`属性
|
||||
- 插入时使用`UUID()`函数或应用层生成UUID
|
||||
|
||||
**外键字段**(软关联):
|
||||
- `user_id`:`bigint` → `CHAR(36)`
|
||||
- `agent_id`:`bigint` → `CHAR(36)`
|
||||
- `order_id`:`bigint` → `CHAR(36)`
|
||||
- `product_id`:`bigint` → `CHAR(36)`
|
||||
- `invite_code_id`:`bigint` → `CHAR(36)`
|
||||
- `link_id`:`bigint` → `CHAR(36)`
|
||||
- `commission_id`:`bigint` → `CHAR(36)`
|
||||
- `wallet_id`:`bigint` → `CHAR(36)`
|
||||
- `withdrawal_id`:`bigint` → `CHAR(36)`
|
||||
- `parent_agent_id`:`bigint` → `CHAR(36)`
|
||||
- `team_leader_id`:`bigint` → `CHAR(36)`
|
||||
- `cleanup_log_id`:`bigint` → `CHAR(36)`
|
||||
- `query_id`:`bigint` → `CHAR(36)`
|
||||
- `feature_id`:`bigint` → `CHAR(36)`
|
||||
- `parent_id`(菜单):`bigint` → `CHAR(36)`
|
||||
- `dict_type_id`:`bigint` → `CHAR(36)`
|
||||
- `role_id`:`bigint` → `CHAR(36)`
|
||||
- `menu_id`:`bigint` → `CHAR(36)`
|
||||
- `api_id`:`bigint` → `CHAR(36)`
|
||||
- `used_user_id`:`bigint` → `CHAR(36)`
|
||||
- `used_agent_id`:`bigint` → `CHAR(36)`
|
||||
|
||||
### 1.4 执行脚本
|
||||
|
||||
**开发环境**:执行文件 `uuid_migration_simple.sql`
|
||||
- 直接修改表结构,不保留旧数据
|
||||
- 适用于开发环境,没有重要数据
|
||||
|
||||
**生产环境**:执行文件 `uuid_migration.sql`
|
||||
- 完整的迁移流程,保留旧数据以便回滚
|
||||
- 需要分阶段执行,包含数据迁移和验证
|
||||
|
||||
**开发环境执行步骤**:
|
||||
1. 直接执行 `uuid_migration_simple.sql`
|
||||
2. 所有表的主键和外键字段直接改为CHAR(36)
|
||||
3. 插入新记录时,在应用层生成UUID
|
||||
|
||||
**注意事项**:
|
||||
- 开发环境脚本直接修改表结构,不保留旧数据
|
||||
- 如果表中有数据,需要先清空数据或手动填充UUID
|
||||
- 代码层面需要同步修改所有ID字段类型(int64 → string)
|
||||
|
||||
## 二、用户系统重构
|
||||
|
||||
### 2.1 概述
|
||||
|
||||
删除`UserTemp`表,统一使用`User`表(通过`mobile`字段是否为空区分临时用户和正式用户),优化`UserAuth`表结构。
|
||||
|
||||
### 2.2 涉及的表
|
||||
|
||||
**需要修改的表**:
|
||||
- `user` - 用户表(无需修改结构,只需确保mobile可为空)
|
||||
- `user_auth` - 用户认证表(添加唯一索引)
|
||||
- `user_temp` - 临时用户表(**删除**)
|
||||
|
||||
**需要迁移数据的表**:
|
||||
- `order` - 订单表(更新user_id)
|
||||
- `query` - 查询报告表(更新user_id)
|
||||
|
||||
### 2.3 修改内容
|
||||
|
||||
#### 2.3.1 UserAuth表
|
||||
|
||||
**添加唯一索引**:
|
||||
```sql
|
||||
ALTER TABLE `user_auth`
|
||||
ADD UNIQUE INDEX `uk_auth_type_key` (`auth_type`, `auth_key`);
|
||||
```
|
||||
|
||||
**作用**:
|
||||
- 确保同一个认证方式(auth_type + auth_key)只能绑定一个用户
|
||||
- 防止重复绑定
|
||||
|
||||
#### 2.3.2 UserTemp表
|
||||
|
||||
**删除原因**:
|
||||
- 临时用户可以直接创建User表记录(mobile为空)
|
||||
- 临时用户的认证信息存储在UserAuth表
|
||||
- 通过User.mobile是否为空来区分临时用户和正式用户
|
||||
- 统一使用User表,逻辑更清晰
|
||||
|
||||
**删除步骤**:
|
||||
1. 迁移UserTemp数据到User和UserAuth表
|
||||
2. 更新关联的订单和报告的user_id
|
||||
3. 验证数据完整性
|
||||
4. 删除UserTemp表
|
||||
|
||||
### 2.4 执行脚本
|
||||
|
||||
**开发环境**:执行文件 `user_system_refactor.sql`(已简化)
|
||||
- 直接删除UserTemp表
|
||||
- 添加UserAuth唯一索引
|
||||
|
||||
**生产环境**:如需数据迁移,请参考完整版脚本
|
||||
|
||||
**开发环境执行步骤**:
|
||||
1. 添加UserAuth唯一索引
|
||||
2. 直接删除UserTemp表
|
||||
|
||||
**注意事项**:
|
||||
- 开发环境脚本直接删除UserTemp表,不进行数据迁移
|
||||
- 删除UserTemp表前,确保代码中已移除所有对UserTemp表的引用
|
||||
|
||||
## 三、执行顺序
|
||||
|
||||
### 3.1 推荐执行顺序
|
||||
|
||||
**开发环境**:
|
||||
1. **先执行用户系统重构**(`user_system_refactor.sql`)
|
||||
- 添加UserAuth唯一索引
|
||||
- 删除UserTemp表
|
||||
|
||||
2. **再执行UUID迁移**(`uuid_migration_simple.sql`)
|
||||
- 直接修改所有表结构
|
||||
- 需要同步修改代码
|
||||
|
||||
**生产环境**:
|
||||
1. 先执行用户系统重构(完整版,包含数据迁移)
|
||||
2. 再执行UUID迁移(完整版,包含数据迁移和验证)
|
||||
|
||||
### 3.2 如果先执行UUID迁移
|
||||
|
||||
如果先执行UUID迁移,需要注意:
|
||||
- UserTemp表的ID也会改为UUID
|
||||
- 迁移UserTemp数据时,需要使用UUID映射
|
||||
- 用户系统重构脚本需要相应调整
|
||||
|
||||
## 四、代码修改清单
|
||||
|
||||
### 4.1 UUID迁移需要的代码修改
|
||||
|
||||
**Model层**:
|
||||
- 所有Model的ID字段类型:`int64` → `string`
|
||||
- 所有外键字段类型:`int64` → `string`
|
||||
- 移除`AUTO_INCREMENT`相关逻辑
|
||||
- 插入时生成UUID(使用`uuid.NewString()`)
|
||||
|
||||
**Service层**:
|
||||
- 所有使用ID的地方改为UUID
|
||||
- 查询条件改为UUID
|
||||
- 关联查询改为UUID
|
||||
|
||||
**API层**:
|
||||
- 请求参数中的ID改为UUID
|
||||
- 响应数据中的ID改为UUID
|
||||
|
||||
**数据库操作**:
|
||||
- 插入操作:使用`uuid.NewString()`生成UUID
|
||||
- 查询操作:使用UUID字符串查询
|
||||
- 更新操作:使用UUID字符串更新
|
||||
|
||||
### 4.2 用户系统重构需要的代码修改
|
||||
|
||||
**删除UserTemp相关代码**:
|
||||
- 删除`UserTempModel`
|
||||
- 删除所有使用`UserTemp`的代码
|
||||
- 删除临时用户创建逻辑中的UserTemp相关代码
|
||||
|
||||
**修改用户创建逻辑**:
|
||||
- 临时用户:直接创建User记录(mobile为空)
|
||||
- 创建UserAuth记录(auth_type + auth_key)
|
||||
|
||||
**修改用户查询逻辑**:
|
||||
- 通过UserAuth表查询用户
|
||||
- 通过User.mobile是否为空判断用户类型
|
||||
|
||||
**修改账号合并逻辑**:
|
||||
- 绑定手机号时,检查手机号是否已存在
|
||||
- 如果存在,合并账号(迁移数据)
|
||||
- 如果不存在,更新User.mobile
|
||||
|
||||
## 五、验证清单
|
||||
|
||||
### 5.1 UUID迁移验证
|
||||
|
||||
- [ ] 所有表的主键已改为CHAR(36)
|
||||
- [ ] 所有外键字段已改为CHAR(36)
|
||||
- [ ] 插入新记录时自动生成UUID
|
||||
- [ ] 查询操作使用UUID正常
|
||||
- [ ] 关联查询使用UUID正常
|
||||
- [ ] 索引和约束已更新
|
||||
- [ ] 数据完整性验证通过
|
||||
|
||||
### 5.2 用户系统重构验证
|
||||
|
||||
- [ ] UserAuth表有唯一索引`uk_auth_type_key`
|
||||
- [ ] UserTemp表数据已迁移(如果存在)
|
||||
- [ ] 订单和报告的user_id已更新
|
||||
- [ ] UserTemp表已删除(如果存在)
|
||||
- [ ] 临时用户创建逻辑使用User表
|
||||
- [ ] 账号合并逻辑正常工作
|
||||
- [ ] 数据完整性验证通过
|
||||
|
||||
## 六、回滚方案
|
||||
|
||||
### 6.1 UUID迁移回滚
|
||||
|
||||
如果UUID迁移出现问题,可以:
|
||||
1. 保留原ID字段(迁移脚本中已保留)
|
||||
2. 恢复主键为原ID字段
|
||||
3. 删除UUID字段
|
||||
4. 恢复代码使用int64类型
|
||||
|
||||
### 6.2 用户系统重构回滚
|
||||
|
||||
如果用户系统重构出现问题,可以:
|
||||
1. 恢复UserTemp表(从备份)
|
||||
2. 恢复订单和报告的user_id
|
||||
3. 恢复代码使用UserTemp表
|
||||
|
||||
## 七、常见问题
|
||||
|
||||
### 7.1 UUID性能问题
|
||||
|
||||
**问题**:UUID作为主键可能影响性能
|
||||
|
||||
**解答**:
|
||||
- UUID是字符串类型,索引效率略低于bigint
|
||||
- 但UUID的优势是全局唯一,适合分布式系统
|
||||
- 如果性能问题严重,可以考虑使用BINARY(16)存储UUID(需要转换)
|
||||
|
||||
### 7.2 UUID长度问题
|
||||
|
||||
**问题**:CHAR(36)占用空间较大
|
||||
|
||||
**解答**:
|
||||
- UUID标准格式是36字符(包含连字符)
|
||||
- 如果空间敏感,可以使用BINARY(16)存储(需要转换函数)
|
||||
- 当前使用CHAR(36)便于调试和查看
|
||||
|
||||
### 7.3 UserTemp数据迁移问题
|
||||
|
||||
**问题**:如何确保UserTemp数据正确迁移?
|
||||
|
||||
**解答**:
|
||||
- 通过auth_type和auth_key匹配UserAuth记录
|
||||
- 建立UserTemp.id到User.id的映射关系
|
||||
- 迁移后验证数据完整性
|
||||
- 保留UserTemp表一段时间,确认无误后再删除
|
||||
|
||||
## 八、联系信息
|
||||
|
||||
如有问题,请联系开发团队。
|
||||
46
deploy/sql/add_invite_code_usage_table.sql
Normal file
46
deploy/sql/add_invite_code_usage_table.sql
Normal file
@@ -0,0 +1,46 @@
|
||||
-- ============================================
|
||||
-- 邀请码使用历史表
|
||||
-- 说明:记录每次邀请码的使用情况,支持统计和查询
|
||||
--
|
||||
-- 功能说明:
|
||||
-- 1. 记录每个邀请码每次使用的详细信息
|
||||
-- 2. 支持统计每个邀请码邀请了多少代理
|
||||
-- 3. 支持查询某个代理是通过哪个邀请码成为代理的
|
||||
-- 4. 保留完整的使用历史记录
|
||||
--
|
||||
-- 执行步骤:
|
||||
-- 1. 执行此 SQL 创建表和添加字段
|
||||
-- 2. 使用 goctl 生成 Model:agent_invite_code_usage -> AgentInviteCodeUsageModel
|
||||
-- 3. 在 servicecontext.go 中添加 AgentInviteCodeUsageModel
|
||||
-- ============================================
|
||||
CREATE TABLE `agent_invite_code_usage` (
|
||||
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
|
||||
`invite_code_id` bigint NOT NULL COMMENT '邀请码ID(关联agent_invite_code表)',
|
||||
`code` varchar(50) NOT NULL COMMENT '邀请码(冗余字段,便于查询)',
|
||||
`user_id` bigint NOT NULL COMMENT '使用用户ID',
|
||||
`agent_id` bigint NOT NULL COMMENT '成为的代理ID',
|
||||
`agent_level` tinyint NOT NULL COMMENT '代理等级:1=普通,2=黄金,3=钻石',
|
||||
`used_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '使用时间',
|
||||
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
||||
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
|
||||
`delete_time` datetime DEFAULT NULL COMMENT '删除时间',
|
||||
`del_state` tinyint NOT NULL DEFAULT 0 COMMENT '删除状态:0=未删除,1=已删除',
|
||||
`version` bigint NOT NULL DEFAULT 0 COMMENT '版本号(乐观锁)',
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `idx_invite_code_id` (`invite_code_id`) COMMENT '关联邀请码ID',
|
||||
KEY `idx_code` (`code`) COMMENT '邀请码索引',
|
||||
KEY `idx_user_id` (`user_id`) COMMENT '用户ID索引',
|
||||
KEY `idx_agent_id` (`agent_id`) COMMENT '代理ID索引',
|
||||
KEY `idx_used_time` (`used_time`) COMMENT '使用时间索引',
|
||||
KEY `idx_create_time` (`create_time`) COMMENT '创建时间索引',
|
||||
KEY `idx_code_time` (`code`, `used_time`) COMMENT '邀请码和使用时间复合索引'
|
||||
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '邀请码使用历史表';
|
||||
|
||||
-- ============================================
|
||||
-- 在agent表中添加invite_code_id字段(可选,便于直接查询代理是通过哪个邀请码成为的)
|
||||
-- ============================================
|
||||
ALTER TABLE `agent`
|
||||
ADD COLUMN `invite_code_id` bigint DEFAULT NULL COMMENT '通过哪个邀请码成为代理(关联agent_invite_code表)' AFTER `team_leader_id`;
|
||||
|
||||
ALTER TABLE `agent`
|
||||
ADD KEY `idx_invite_code_id` (`invite_code_id`) COMMENT '邀请码ID索引';
|
||||
5
deploy/sql/add_order_remark_field.sql
Normal file
5
deploy/sql/add_order_remark_field.sql
Normal file
@@ -0,0 +1,5 @@
|
||||
-- 为 order 表添加备注字段
|
||||
-- 用于记录易支付使用的渠道号等信息
|
||||
|
||||
ALTER TABLE `order`
|
||||
ADD COLUMN `remark` VARCHAR(255) NULL COMMENT '备注信息(如易支付渠道号)' AFTER `delete_time`;
|
||||
62
deploy/sql/template.sql
Normal file
62
deploy/sql/template.sql
Normal file
@@ -0,0 +1,62 @@
|
||||
-- ============================================
|
||||
-- 表结构模板(UUID版本)
|
||||
-- ============================================
|
||||
-- 注意:系统已迁移到UUID主键,新表请使用此模板
|
||||
-- ============================================
|
||||
|
||||
CREATE TABLE `表名` (
|
||||
`id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
`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 '版本号',
|
||||
|
||||
/* 业务字段开始 */
|
||||
|
||||
`字段1` 数据类型 [约束条件] [DEFAULT 默认值] [COMMENT '字段说明'],
|
||||
`字段2` 数据类型 [约束条件] [DEFAULT 默认值] [COMMENT '字段说明'],
|
||||
/* 关联字段 - 软关联(使用UUID) */
|
||||
`关联表id` CHAR(36) [NOT NULL] [DEFAULT NULL] COMMENT '关联到XX表的UUID',
|
||||
/* 业务字段结束 */
|
||||
|
||||
PRIMARY KEY (`id`),
|
||||
/* 索引定义 */
|
||||
UNIQUE KEY `索引名称` (`字段名`),
|
||||
KEY `idx_关联字段` (`关联表id`) COMMENT '优化关联查询'
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='表说明';
|
||||
|
||||
-- ============================================
|
||||
-- UUID生成说明
|
||||
-- ============================================
|
||||
-- 1. 应用层生成:使用Go的uuid.NewString()生成UUID
|
||||
-- 示例:id := uuid.NewString()
|
||||
-- 2. 数据库层生成:使用MySQL的UUID()函数(不推荐,性能较差)
|
||||
-- 示例:INSERT INTO table (id, ...) VALUES (UUID(), ...)
|
||||
-- 3. 推荐方式:在应用层生成UUID,然后插入数据库
|
||||
-- ============================================
|
||||
|
||||
-- ============================================
|
||||
-- 旧版本模板(bigint主键,已废弃)
|
||||
-- ============================================
|
||||
-- CREATE TABLE `表名` (
|
||||
-- `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 '版本号',
|
||||
--
|
||||
-- /* 业务字段开始 */
|
||||
-- `字段1` 数据类型 [约束条件] [DEFAULT 默认值] [COMMENT '字段说明'],
|
||||
-- `字段2` 数据类型 [约束条件] [DEFAULT 默认值] [COMMENT '字段说明'],
|
||||
-- /* 关联字段 - 软关联 */
|
||||
-- `关联表id` bigint [NOT NULL] [DEFAULT '0'] COMMENT '关联到XX表的id',
|
||||
-- /* 业务字段结束 */
|
||||
--
|
||||
-- PRIMARY KEY (`id`),
|
||||
-- /* 索引定义 */
|
||||
-- UNIQUE KEY `索引名称` (`字段名`),
|
||||
-- KEY `idx_关联字段` (`关联表id`) COMMENT '优化关联查询'
|
||||
-- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='表说明';
|
||||
-- ============================================
|
||||
48
deploy/sql/user_system_refactor.sql
Normal file
48
deploy/sql/user_system_refactor.sql
Normal file
@@ -0,0 +1,48 @@
|
||||
-- ============================================
|
||||
-- 用户系统重构SQL脚本(简化版 - 开发环境)
|
||||
-- 根据《用户系统重构计划书》执行
|
||||
-- ============================================
|
||||
|
||||
-- ============================================
|
||||
-- 第一部分:UserAuth表唯一索引
|
||||
-- ============================================
|
||||
|
||||
-- 确保user_auth表的(auth_type, auth_key)唯一性
|
||||
-- 如果已存在同名索引,先删除
|
||||
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表(开发环境直接删除)
|
||||
-- ============================================
|
||||
|
||||
-- 注意:开发环境没有数据,直接删除表即可
|
||||
-- 如果表不存在,会报错但可以忽略
|
||||
|
||||
DROP TABLE IF EXISTS `user_temp`;
|
||||
|
||||
-- ============================================
|
||||
-- 第三部分:索引优化(可选)
|
||||
-- ============================================
|
||||
|
||||
-- 为user_auth表添加user_id和auth_type的联合索引(如果查询频繁)
|
||||
-- ALTER TABLE `user_auth`
|
||||
-- ADD INDEX `idx_user_id_auth_type` (`user_id`, `auth_type`) COMMENT '优化按用户查询认证类型';
|
||||
|
||||
-- 为user表添加mobile的唯一索引(如果不存在)
|
||||
-- ALTER TABLE `user`
|
||||
-- ADD UNIQUE INDEX `uk_mobile` (`mobile`) COMMENT '确保手机号唯一性';
|
||||
|
||||
-- ============================================
|
||||
-- 注意事项
|
||||
-- ============================================
|
||||
-- 1. 此脚本适用于开发环境,直接删除UserTemp表
|
||||
-- 2. 如果生产环境有数据,请使用完整版迁移脚本
|
||||
-- 3. 执行后,确保代码中已移除所有对UserTemp表的引用
|
||||
45
deploy/sql/user_temp_migration.sql
Normal file
45
deploy/sql/user_temp_migration.sql
Normal file
@@ -0,0 +1,45 @@
|
||||
-- 将 user_temp 迁移为 user(mobile 为空)与 user_auth
|
||||
|
||||
START TRANSACTION;
|
||||
|
||||
-- 1) 迁移临时用户到 user(mobile 置为空)
|
||||
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`);
|
||||
1831
deploy/sql/uuid_migration.sql
Normal file
1831
deploy/sql/uuid_migration.sql
Normal file
File diff suppressed because it is too large
Load Diff
446
deploy/sql/uuid_migration_simple.sql
Normal file
446
deploy/sql/uuid_migration_simple.sql
Normal file
@@ -0,0 +1,446 @@
|
||||
-- ============================================
|
||||
-- UUID迁移脚本(简化版 - 开发环境)
|
||||
-- 将系统中所有bigint类型的ID字段改为CHAR(36)类型的UUID
|
||||
-- 注意:此脚本直接修改表结构,不保留旧数据,适用于开发环境
|
||||
-- ============================================
|
||||
|
||||
-- 注意:user表和product表已经是CHAR(36)类型,跳过修改
|
||||
|
||||
-- ============================================
|
||||
-- 第一部分:修改核心业务表
|
||||
-- ============================================
|
||||
|
||||
-- user表:已经是CHAR(36),跳过
|
||||
-- product表:已经是CHAR(36),跳过
|
||||
|
||||
-- order表:修改主键和外键字段
|
||||
ALTER TABLE `order`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `user_id` CHAR(36) NOT NULL COMMENT '用户UUID',
|
||||
MODIFY COLUMN `product_id` CHAR(36) NOT NULL COMMENT '产品UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD INDEX `idx_user_id` (`user_id`),
|
||||
ADD INDEX `idx_product_id` (`product_id`);
|
||||
|
||||
-- query表:修改主键和外键字段
|
||||
ALTER TABLE `query`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `order_id` CHAR(36) NOT NULL COMMENT '订单UUID',
|
||||
MODIFY COLUMN `user_id` CHAR(36) NOT NULL COMMENT '用户UUID',
|
||||
MODIFY COLUMN `product_id` CHAR(36) NOT NULL COMMENT '产品UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD INDEX `idx_order_id` (`order_id`),
|
||||
ADD INDEX `idx_user_id` (`user_id`),
|
||||
ADD INDEX `idx_product_id` (`product_id`);
|
||||
|
||||
-- user_auth表:修改主键和外键字段(注意:user_id需要关联到user表的UUID)
|
||||
ALTER TABLE `user_auth`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `user_id` CHAR(36) NOT NULL COMMENT '用户UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD INDEX `idx_user_id` (`user_id`),
|
||||
ADD UNIQUE INDEX `uk_auth_type_key` (`auth_type`, `auth_key`) COMMENT '确保同一个认证方式只能绑定一个用户';
|
||||
|
||||
-- agent表:修改主键和外键字段
|
||||
ALTER TABLE `agent`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `user_id` CHAR(36) NOT NULL COMMENT '用户UUID',
|
||||
MODIFY COLUMN `team_leader_id` CHAR(36) NULL COMMENT '团队首领UUID',
|
||||
MODIFY COLUMN `invite_code_id` CHAR(36) NULL COMMENT '邀请码UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD UNIQUE INDEX `uk_user_id` (`user_id`),
|
||||
ADD INDEX `idx_team_leader_id` (`team_leader_id`),
|
||||
ADD INDEX `idx_invite_code_id` (`invite_code_id`);
|
||||
|
||||
-- ============================================
|
||||
-- 第二部分:修改代理相关表
|
||||
-- ============================================
|
||||
|
||||
-- agent_commission表
|
||||
ALTER TABLE `agent_commission`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `agent_id` CHAR(36) NOT NULL COMMENT '代理UUID',
|
||||
MODIFY COLUMN `order_id` CHAR(36) NOT NULL COMMENT '订单UUID',
|
||||
MODIFY COLUMN `product_id` CHAR(36) NOT NULL COMMENT '产品UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD INDEX `idx_agent_id` (`agent_id`),
|
||||
ADD INDEX `idx_order_id` (`order_id`),
|
||||
ADD INDEX `idx_product_id` (`product_id`);
|
||||
|
||||
-- agent_invite_code表
|
||||
ALTER TABLE `agent_invite_code`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `agent_id` CHAR(36) NULL COMMENT '代理UUID',
|
||||
MODIFY COLUMN `used_user_id` CHAR(36) NULL COMMENT '使用用户UUID',
|
||||
MODIFY COLUMN `used_agent_id` CHAR(36) NULL COMMENT '使用代理UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD INDEX `idx_agent_id` (`agent_id`),
|
||||
ADD INDEX `idx_used_user_id` (`used_user_id`),
|
||||
ADD INDEX `idx_used_agent_id` (`used_agent_id`);
|
||||
|
||||
-- agent_link表
|
||||
ALTER TABLE `agent_link`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `agent_id` CHAR(36) NOT NULL COMMENT '代理UUID',
|
||||
MODIFY COLUMN `user_id` CHAR(36) NOT NULL COMMENT '用户UUID',
|
||||
MODIFY COLUMN `product_id` CHAR(36) NOT NULL COMMENT '产品UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD INDEX `idx_agent_id` (`agent_id`),
|
||||
ADD INDEX `idx_user_id` (`user_id`),
|
||||
ADD INDEX `idx_product_id` (`product_id`);
|
||||
|
||||
-- agent_order表
|
||||
ALTER TABLE `agent_order`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `agent_id` CHAR(36) NOT NULL COMMENT '代理UUID',
|
||||
MODIFY COLUMN `order_id` CHAR(36) NOT NULL COMMENT '订单UUID',
|
||||
MODIFY COLUMN `product_id` CHAR(36) NOT NULL COMMENT '产品UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD INDEX `idx_agent_id` (`agent_id`),
|
||||
ADD INDEX `idx_order_id` (`order_id`),
|
||||
ADD INDEX `idx_product_id` (`product_id`);
|
||||
|
||||
-- agent_wallet表
|
||||
ALTER TABLE `agent_wallet`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `agent_id` CHAR(36) NOT NULL COMMENT '代理UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD UNIQUE INDEX `uk_agent_id` (`agent_id`);
|
||||
|
||||
-- agent_withdrawal表
|
||||
ALTER TABLE `agent_withdrawal`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `agent_id` CHAR(36) NOT NULL COMMENT '代理UUID',
|
||||
MODIFY COLUMN `wallet_id` CHAR(36) NOT NULL COMMENT '钱包UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD INDEX `idx_agent_id` (`agent_id`),
|
||||
ADD INDEX `idx_wallet_id` (`wallet_id`);
|
||||
|
||||
-- agent_withdrawal_tax表
|
||||
ALTER TABLE `agent_withdrawal_tax`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `withdrawal_id` CHAR(36) NOT NULL COMMENT '提现UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD INDEX `idx_withdrawal_id` (`withdrawal_id`);
|
||||
|
||||
-- agent_rebate表
|
||||
ALTER TABLE `agent_rebate`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `agent_id` CHAR(36) NOT NULL COMMENT '代理UUID',
|
||||
MODIFY COLUMN `order_id` CHAR(36) NOT NULL COMMENT '订单UUID',
|
||||
MODIFY COLUMN `product_id` CHAR(36) NOT NULL COMMENT '产品UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD INDEX `idx_agent_id` (`agent_id`),
|
||||
ADD INDEX `idx_order_id` (`order_id`),
|
||||
ADD INDEX `idx_product_id` (`product_id`);
|
||||
|
||||
-- agent_relation表
|
||||
ALTER TABLE `agent_relation`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `agent_id` CHAR(36) NOT NULL COMMENT '代理UUID',
|
||||
MODIFY COLUMN `parent_agent_id` CHAR(36) NULL COMMENT '上级代理UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD INDEX `idx_agent_id` (`agent_id`),
|
||||
ADD INDEX `idx_parent_agent_id` (`parent_agent_id`);
|
||||
|
||||
-- agent_upgrade表
|
||||
ALTER TABLE `agent_upgrade`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `agent_id` CHAR(36) NOT NULL COMMENT '代理UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD INDEX `idx_agent_id` (`agent_id`);
|
||||
|
||||
-- agent_real_name表
|
||||
ALTER TABLE `agent_real_name`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `agent_id` CHAR(36) NOT NULL COMMENT '代理UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD INDEX `idx_agent_id` (`agent_id`);
|
||||
|
||||
-- agent_config表
|
||||
ALTER TABLE `agent_config`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`);
|
||||
|
||||
-- agent_product_config表
|
||||
ALTER TABLE `agent_product_config`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `product_id` CHAR(36) NOT NULL COMMENT '产品UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD INDEX `idx_product_id` (`product_id`);
|
||||
|
||||
-- agent_short_link表
|
||||
ALTER TABLE `agent_short_link`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `invite_code_id` CHAR(36) NULL COMMENT '邀请码UUID',
|
||||
MODIFY COLUMN `link_id` CHAR(36) NULL COMMENT '链接UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD INDEX `idx_invite_code_id` (`invite_code_id`),
|
||||
ADD INDEX `idx_link_id` (`link_id`);
|
||||
|
||||
-- agent_invite_code_usage表
|
||||
ALTER TABLE `agent_invite_code_usage`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `invite_code_id` CHAR(36) NOT NULL COMMENT '邀请码UUID',
|
||||
MODIFY COLUMN `user_id` CHAR(36) NOT NULL COMMENT '用户UUID',
|
||||
MODIFY COLUMN `agent_id` CHAR(36) NOT NULL COMMENT '代理UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD INDEX `idx_invite_code_id` (`invite_code_id`),
|
||||
ADD INDEX `idx_user_id` (`user_id`),
|
||||
ADD INDEX `idx_agent_id` (`agent_id`);
|
||||
|
||||
-- agent_freeze_task表
|
||||
ALTER TABLE `agent_freeze_task`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `agent_id` CHAR(36) NOT NULL COMMENT '代理UUID',
|
||||
MODIFY COLUMN `order_id` CHAR(36) NOT NULL COMMENT '订单UUID',
|
||||
MODIFY COLUMN `commission_id` CHAR(36) NOT NULL COMMENT '佣金UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD INDEX `idx_agent_id` (`agent_id`),
|
||||
ADD INDEX `idx_order_id` (`order_id`),
|
||||
ADD INDEX `idx_commission_id` (`commission_id`);
|
||||
|
||||
-- ============================================
|
||||
-- 第三部分:修改订单相关表
|
||||
-- ============================================
|
||||
|
||||
-- order_refund表
|
||||
ALTER TABLE `order_refund`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `order_id` CHAR(36) NOT NULL COMMENT '订单UUID',
|
||||
MODIFY COLUMN `user_id` CHAR(36) NOT NULL COMMENT '用户UUID',
|
||||
MODIFY COLUMN `product_id` CHAR(36) NOT NULL COMMENT '产品UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD INDEX `idx_order_id` (`order_id`),
|
||||
ADD INDEX `idx_user_id` (`user_id`),
|
||||
ADD INDEX `idx_product_id` (`product_id`);
|
||||
|
||||
-- ============================================
|
||||
-- 第四部分:修改查询相关表
|
||||
-- ============================================
|
||||
|
||||
-- query_cleanup_log表
|
||||
ALTER TABLE `query_cleanup_log`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`);
|
||||
|
||||
-- query_cleanup_detail表
|
||||
ALTER TABLE `query_cleanup_detail`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `cleanup_log_id` CHAR(36) NOT NULL COMMENT '清理日志UUID',
|
||||
MODIFY COLUMN `query_id` CHAR(36) NOT NULL COMMENT '查询UUID',
|
||||
MODIFY COLUMN `order_id` CHAR(36) NOT NULL COMMENT '订单UUID',
|
||||
MODIFY COLUMN `user_id` CHAR(36) NOT NULL COMMENT '用户UUID',
|
||||
MODIFY COLUMN `product_id` CHAR(36) NOT NULL COMMENT '产品UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD INDEX `idx_cleanup_log_id` (`cleanup_log_id`),
|
||||
ADD INDEX `idx_query_id` (`query_id`),
|
||||
ADD INDEX `idx_order_id` (`order_id`),
|
||||
ADD INDEX `idx_user_id` (`user_id`),
|
||||
ADD INDEX `idx_product_id` (`product_id`);
|
||||
|
||||
-- query_cleanup_config表
|
||||
ALTER TABLE `query_cleanup_config`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`);
|
||||
|
||||
-- ============================================
|
||||
-- 第五部分:修改产品相关表
|
||||
-- ============================================
|
||||
|
||||
-- product_feature表
|
||||
ALTER TABLE `product_feature`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `product_id` CHAR(36) NOT NULL COMMENT '产品UUID',
|
||||
MODIFY COLUMN `feature_id` CHAR(36) NOT NULL COMMENT '功能UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD INDEX `idx_product_id` (`product_id`),
|
||||
ADD INDEX `idx_feature_id` (`feature_id`);
|
||||
|
||||
-- feature表
|
||||
ALTER TABLE `feature`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`);
|
||||
|
||||
-- ============================================
|
||||
-- 第六部分:修改其他表
|
||||
-- ============================================
|
||||
|
||||
-- authorization_document表
|
||||
ALTER TABLE `authorization_document`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `user_id` CHAR(36) NOT NULL COMMENT '用户UUID',
|
||||
MODIFY COLUMN `order_id` CHAR(36) NOT NULL COMMENT '订单UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD INDEX `idx_user_id` (`user_id`),
|
||||
ADD INDEX `idx_order_id` (`order_id`);
|
||||
|
||||
-- global_notifications表(注意:原表id是int类型)
|
||||
ALTER TABLE `global_notifications`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`);
|
||||
|
||||
-- example表
|
||||
ALTER TABLE `example`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `feature_id` CHAR(36) NOT NULL COMMENT '功能UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD INDEX `idx_feature_id` (`feature_id`);
|
||||
|
||||
-- ============================================
|
||||
-- 第七部分:修改管理后台表
|
||||
-- ============================================
|
||||
|
||||
-- admin_user表
|
||||
ALTER TABLE `admin_user`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`);
|
||||
|
||||
-- admin_role表
|
||||
ALTER TABLE `admin_role`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`);
|
||||
|
||||
-- admin_menu表(注意:字段名是pid,不是parent_id)
|
||||
ALTER TABLE `admin_menu`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `pid` CHAR(36) NOT NULL DEFAULT '0' COMMENT '父菜单UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD INDEX `idx_pid` (`pid`);
|
||||
|
||||
-- admin_api表
|
||||
ALTER TABLE `admin_api`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`);
|
||||
|
||||
-- admin_dict_type表
|
||||
ALTER TABLE `admin_dict_type`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`);
|
||||
|
||||
-- admin_dict_data表(注意:没有dict_type_id字段,使用dict_type字符串)
|
||||
-- 此表不需要修改外键字段,因为使用的是dict_type字符串而不是ID
|
||||
|
||||
ALTER TABLE `admin_dict_data`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`);
|
||||
|
||||
-- admin_user_role表
|
||||
ALTER TABLE `admin_user_role`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `user_id` CHAR(36) NOT NULL DEFAULT '0' COMMENT '用户UUID',
|
||||
MODIFY COLUMN `role_id` CHAR(36) NOT NULL DEFAULT '0' COMMENT '角色UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD INDEX `idx_user_id` (`user_id`),
|
||||
ADD INDEX `idx_role_id` (`role_id`);
|
||||
|
||||
-- admin_role_menu表
|
||||
ALTER TABLE `admin_role_menu`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `role_id` CHAR(36) NOT NULL DEFAULT '0' COMMENT '角色UUID',
|
||||
MODIFY COLUMN `menu_id` CHAR(36) NOT NULL DEFAULT '0' COMMENT '菜单UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD INDEX `idx_role_id` (`role_id`),
|
||||
ADD INDEX `idx_menu_id` (`menu_id`);
|
||||
|
||||
-- admin_role_api表
|
||||
ALTER TABLE `admin_role_api`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `role_id` CHAR(36) NOT NULL DEFAULT '0' COMMENT '角色UUID',
|
||||
MODIFY COLUMN `api_id` CHAR(36) NOT NULL DEFAULT '0' COMMENT 'API UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD INDEX `idx_role_id` (`role_id`),
|
||||
ADD INDEX `idx_api_id` (`api_id`);
|
||||
|
||||
-- ============================================
|
||||
-- 第八部分:修改推广相关表(新增)
|
||||
-- ============================================
|
||||
|
||||
-- admin_promotion_link表
|
||||
ALTER TABLE `admin_promotion_link`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `admin_user_id` CHAR(36) NOT NULL DEFAULT '0' COMMENT '推广者账号UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD INDEX `idx_admin_user_id` (`admin_user_id`);
|
||||
|
||||
-- admin_promotion_link_stats_history表
|
||||
ALTER TABLE `admin_promotion_link_stats_history`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `link_id` CHAR(36) NOT NULL COMMENT '推广链接UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD INDEX `idx_link_id` (`link_id`);
|
||||
|
||||
-- admin_promotion_link_stats_total表
|
||||
ALTER TABLE `admin_promotion_link_stats_total`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `link_id` CHAR(36) NOT NULL COMMENT '推广链接UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD INDEX `idx_link_id` (`link_id`);
|
||||
|
||||
-- admin_promotion_order表
|
||||
ALTER TABLE `admin_promotion_order`
|
||||
MODIFY COLUMN `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
|
||||
MODIFY COLUMN `link_id` CHAR(36) NOT NULL COMMENT '推广链接UUID',
|
||||
MODIFY COLUMN `order_id` CHAR(36) NOT NULL COMMENT '订单UUID',
|
||||
MODIFY COLUMN `user_id` CHAR(36) NOT NULL COMMENT '下单用户UUID',
|
||||
MODIFY COLUMN `admin_user_id` CHAR(36) NOT NULL COMMENT '推广者账号UUID',
|
||||
DROP PRIMARY KEY,
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD UNIQUE INDEX `uk_order_id` (`order_id`) COMMENT '确保每个订单只有一条推广记录',
|
||||
ADD INDEX `idx_link_id` (`link_id`),
|
||||
ADD INDEX `idx_user_id` (`user_id`) COMMENT '优化用户查询',
|
||||
ADD INDEX `idx_admin_user_id` (`admin_user_id`);
|
||||
|
||||
-- ============================================
|
||||
-- 注意事项
|
||||
-- ============================================
|
||||
-- 1. 此脚本直接修改表结构,不保留旧数据
|
||||
-- 2. user表和product表已经是CHAR(36)类型,已跳过
|
||||
-- 3. 执行后,所有ID字段都是CHAR(36)类型
|
||||
-- 4. 插入新记录时,需要在应用层生成UUID(使用uuid.NewString())
|
||||
-- 5. 如果表中有数据,需要先清空数据或手动填充UUID
|
||||
-- 6. 建议在开发环境先测试,确认无误后再应用到生产环境
|
||||
-- 7. admin_menu表使用pid字段而不是parent_id
|
||||
-- 8. admin_dict_data表使用dict_type字符串字段,不是dict_type_id
|
||||
-- 9. global_notifications表的id原为int类型,现改为CHAR(36)
|
||||
9
deploy/template/api/config.tpl
Normal file
9
deploy/template/api/config.tpl
Normal file
@@ -0,0 +1,9 @@
|
||||
package config
|
||||
|
||||
import {{.authImport}}
|
||||
|
||||
type Config struct {
|
||||
rest.RestConf
|
||||
{{.auth}}
|
||||
{{.jwtTrans}}
|
||||
}
|
||||
17
deploy/template/api/context.tpl
Normal file
17
deploy/template/api/context.tpl
Normal file
@@ -0,0 +1,17 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
{{.configImport}}
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config {{.config}}
|
||||
{{.middleware}}
|
||||
}
|
||||
|
||||
func NewServiceContext(c {{.config}}) *ServiceContext {
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
{{.middlewareAssignment}}
|
||||
}
|
||||
}
|
||||
3
deploy/template/api/etc.tpl
Normal file
3
deploy/template/api/etc.tpl
Normal file
@@ -0,0 +1,3 @@
|
||||
Name: {{.serviceName}}
|
||||
Host: {{.host}}
|
||||
Port: {{.port}}
|
||||
27
deploy/template/api/handler.tpl
Normal file
27
deploy/template/api/handler.tpl
Normal file
@@ -0,0 +1,27 @@
|
||||
package {{.PkgName}}
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"in-server/common/result"
|
||||
"in-server/pkg/lzkit/validator"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
{{.ImportPackages}}
|
||||
)
|
||||
|
||||
func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
{{if .HasRequest}}var req types.{{.RequestType}}
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
result.ParamErrorResult(r,w,err)
|
||||
return
|
||||
}
|
||||
if err := validator.Validate(req); err != nil {
|
||||
result.ParamValidateErrorResult(r, w, err)
|
||||
return
|
||||
}
|
||||
{{end}}l := {{.LogicName}}.New{{.LogicType}}(r.Context(), svcCtx)
|
||||
{{if .HasResp}}resp, {{end}}err := l.{{.Call}}({{if .HasRequest}}&req{{end}})
|
||||
result.HttpResult(r, w, {{if .HasResp}}resp{{else}}nil{{end}}, err)
|
||||
}
|
||||
}
|
||||
25
deploy/template/api/logic.tpl
Normal file
25
deploy/template/api/logic.tpl
Normal file
@@ -0,0 +1,25 @@
|
||||
package {{.pkgName}}
|
||||
|
||||
import (
|
||||
{{.imports}}
|
||||
)
|
||||
|
||||
type {{.logic}} struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func New{{.logic}}(ctx context.Context, svcCtx *svc.ServiceContext) *{{.logic}} {
|
||||
return &{{.logic}}{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *{{.logic}}) {{.function}}({{.request}}) {{.responseType}} {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
{{.returnString}}
|
||||
}
|
||||
27
deploy/template/api/main.tpl
Normal file
27
deploy/template/api/main.tpl
Normal file
@@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
|
||||
{{.importPackages}}
|
||||
"in-server/common/middleware"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "etc/{{.serviceName}}.yaml", "the config file")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var c config.Config
|
||||
conf.MustLoad(*configFile, &c)
|
||||
|
||||
ctx := svc.NewServiceContext(c)
|
||||
server := rest.MustNewServer(c.RestConf)
|
||||
defer server.Stop()
|
||||
|
||||
handler.RegisterHandlers(server, ctx)
|
||||
|
||||
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
|
||||
server.Start()
|
||||
}
|
||||
20
deploy/template/api/middleware.tpl
Normal file
20
deploy/template/api/middleware.tpl
Normal file
@@ -0,0 +1,20 @@
|
||||
|
||||
package middleware
|
||||
|
||||
import "net/http"
|
||||
|
||||
type {{.name}} struct {
|
||||
}
|
||||
|
||||
func New{{.name}}() *{{.name}} {
|
||||
return &{{.name}}{}
|
||||
}
|
||||
|
||||
func (m *{{.name}})Handle(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// TODO generate middleware implement function, delete after code implementation
|
||||
|
||||
// Passthrough to next handler if need
|
||||
next(w, r)
|
||||
}
|
||||
}
|
||||
4
deploy/template/api/route-addition.tpl
Normal file
4
deploy/template/api/route-addition.tpl
Normal file
@@ -0,0 +1,4 @@
|
||||
|
||||
server.AddRoutes(
|
||||
{{.routes}} {{.jwt}}{{.signature}} {{.prefix}} {{.timeout}} {{.maxBytes}}
|
||||
)
|
||||
13
deploy/template/api/routes.tpl
Normal file
13
deploy/template/api/routes.tpl
Normal file
@@ -0,0 +1,13 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"{{if .hasTimeout}}
|
||||
"time"{{end}}
|
||||
|
||||
{{.importPackages}}
|
||||
)
|
||||
|
||||
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
{{.routesAdditions}}
|
||||
}
|
||||
24
deploy/template/api/template.tpl
Normal file
24
deploy/template/api/template.tpl
Normal file
@@ -0,0 +1,24 @@
|
||||
syntax = "v1"
|
||||
|
||||
info (
|
||||
title: // TODO: add title
|
||||
desc: // TODO: add description
|
||||
author: "{{.gitUser}}"
|
||||
email: "{{.gitEmail}}"
|
||||
)
|
||||
|
||||
type request {
|
||||
// TODO: add members here and delete this comment
|
||||
}
|
||||
|
||||
type response {
|
||||
// TODO: add members here and delete this comment
|
||||
}
|
||||
|
||||
service {{.serviceName}} {
|
||||
@handler GetUser // TODO: set handler name and delete this comment
|
||||
get /users/id/:userId(request) returns(response)
|
||||
|
||||
@handler CreateUser // TODO: set handler name and delete this comment
|
||||
post /users/create(request)
|
||||
}
|
||||
6
deploy/template/api/types.tpl
Normal file
6
deploy/template/api/types.tpl
Normal file
@@ -0,0 +1,6 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
package types{{if .containsTime}}
|
||||
import (
|
||||
"time"
|
||||
){{end}}
|
||||
{{.types}}
|
||||
33
deploy/template/docker/docker.tpl
Normal file
33
deploy/template/docker/docker.tpl
Normal file
@@ -0,0 +1,33 @@
|
||||
FROM golang:{{.Version}}alpine AS builder
|
||||
|
||||
LABEL stage=gobuilder
|
||||
|
||||
ENV CGO_ENABLED 0
|
||||
{{if .Chinese}}ENV GOPROXY https://goproxy.cn,direct
|
||||
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
|
||||
{{end}}{{if .HasTimezone}}
|
||||
RUN apk update --no-cache && apk add --no-cache tzdata
|
||||
{{end}}
|
||||
WORKDIR /build
|
||||
|
||||
ADD go.mod .
|
||||
ADD go.sum .
|
||||
RUN go mod download
|
||||
COPY . .
|
||||
{{if .Argument}}COPY {{.GoRelPath}}/etc /app/etc
|
||||
{{end}}RUN go build -ldflags="-s -w" -o /app/{{.ExeFile}} {{.GoMainFrom}}
|
||||
|
||||
|
||||
FROM {{.BaseImage}}
|
||||
|
||||
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
|
||||
{{if .HasTimezone}}COPY --from=builder /usr/share/zoneinfo/{{.Timezone}} /usr/share/zoneinfo/{{.Timezone}}
|
||||
ENV TZ {{.Timezone}}
|
||||
{{end}}
|
||||
WORKDIR /app
|
||||
COPY --from=builder /app/{{.ExeFile}} /app/{{.ExeFile}}{{if .Argument}}
|
||||
COPY --from=builder /app/etc /app/etc{{end}}
|
||||
{{if .HasPort}}
|
||||
EXPOSE {{.Port}}
|
||||
{{end}}
|
||||
CMD ["./{{.ExeFile}}"{{.Argument}}]
|
||||
18
deploy/template/gateway/etc.tpl
Normal file
18
deploy/template/gateway/etc.tpl
Normal file
@@ -0,0 +1,18 @@
|
||||
Name: gateway-example # gateway name
|
||||
Host: localhost # gateway host
|
||||
Port: 8888 # gateway port
|
||||
Upstreams: # upstreams
|
||||
- Grpc: # grpc upstream
|
||||
Target: 0.0.0.0:8080 # grpc target,the direct grpc server address,for only one node
|
||||
# Endpoints: [0.0.0.0:8080,192.168.120.1:8080] # grpc endpoints, the grpc server address list, for multiple nodes
|
||||
# Etcd: # etcd config, if you want to use etcd to discover the grpc server address
|
||||
# Hosts: [127.0.0.1:2378,127.0.0.1:2379] # etcd hosts
|
||||
# Key: greet.grpc # the discovery key
|
||||
# protoset mode
|
||||
ProtoSets:
|
||||
- hello.pb
|
||||
# Mappings can also be written in proto options
|
||||
# Mappings: # routes mapping
|
||||
# - Method: get
|
||||
# Path: /ping
|
||||
# RpcPath: hello.Hello/Ping
|
||||
20
deploy/template/gateway/main.tpl
Normal file
20
deploy/template/gateway/main.tpl
Normal file
@@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/gateway"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "etc/gateway.yaml", "config file")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var c gateway.GatewayConf
|
||||
conf.MustLoad(*configFile, &c)
|
||||
gw := gateway.MustNewServer(c)
|
||||
defer gw.Stop()
|
||||
gw.Start()
|
||||
}
|
||||
117
deploy/template/kube/deployment.tpl
Normal file
117
deploy/template/kube/deployment.tpl
Normal file
@@ -0,0 +1,117 @@
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: {{.Name}}
|
||||
namespace: {{.Namespace}}
|
||||
labels:
|
||||
app: {{.Name}}
|
||||
spec:
|
||||
replicas: {{.Replicas}}
|
||||
revisionHistoryLimit: {{.Revisions}}
|
||||
selector:
|
||||
matchLabels:
|
||||
app: {{.Name}}
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: {{.Name}}
|
||||
spec:{{if .ServiceAccount}}
|
||||
serviceAccountName: {{.ServiceAccount}}{{end}}
|
||||
containers:
|
||||
- name: {{.Name}}
|
||||
image: {{.Image}}
|
||||
{{if .ImagePullPolicy}}imagePullPolicy: {{.ImagePullPolicy}}
|
||||
{{end}}ports:
|
||||
- containerPort: {{.Port}}
|
||||
readinessProbe:
|
||||
tcpSocket:
|
||||
port: {{.Port}}
|
||||
initialDelaySeconds: 5
|
||||
periodSeconds: 10
|
||||
livenessProbe:
|
||||
tcpSocket:
|
||||
port: {{.Port}}
|
||||
initialDelaySeconds: 15
|
||||
periodSeconds: 20
|
||||
resources:
|
||||
requests:
|
||||
cpu: {{.RequestCpu}}m
|
||||
memory: {{.RequestMem}}Mi
|
||||
limits:
|
||||
cpu: {{.LimitCpu}}m
|
||||
memory: {{.LimitMem}}Mi
|
||||
volumeMounts:
|
||||
- name: timezone
|
||||
mountPath: /etc/localtime
|
||||
{{if .Secret}}imagePullSecrets:
|
||||
- name: {{.Secret}}
|
||||
{{end}}volumes:
|
||||
- name: timezone
|
||||
hostPath:
|
||||
path: /usr/share/zoneinfo/Asia/Shanghai
|
||||
|
||||
---
|
||||
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: {{.Name}}-svc
|
||||
namespace: {{.Namespace}}
|
||||
spec:
|
||||
ports:
|
||||
{{if .UseNodePort}}- nodePort: {{.NodePort}}
|
||||
port: {{.Port}}
|
||||
protocol: TCP
|
||||
targetPort: {{.TargetPort}}
|
||||
type: NodePort{{else}}- port: {{.Port}}
|
||||
targetPort: {{.TargetPort}}{{end}}
|
||||
selector:
|
||||
app: {{.Name}}
|
||||
|
||||
---
|
||||
|
||||
apiVersion: autoscaling/v2beta2
|
||||
kind: HorizontalPodAutoscaler
|
||||
metadata:
|
||||
name: {{.Name}}-hpa-c
|
||||
namespace: {{.Namespace}}
|
||||
labels:
|
||||
app: {{.Name}}-hpa-c
|
||||
spec:
|
||||
scaleTargetRef:
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
name: {{.Name}}
|
||||
minReplicas: {{.MinReplicas}}
|
||||
maxReplicas: {{.MaxReplicas}}
|
||||
metrics:
|
||||
- type: Resource
|
||||
resource:
|
||||
name: cpu
|
||||
target:
|
||||
type: Utilization
|
||||
averageUtilization: 80
|
||||
|
||||
---
|
||||
|
||||
apiVersion: autoscaling/v2beta2
|
||||
kind: HorizontalPodAutoscaler
|
||||
metadata:
|
||||
name: {{.Name}}-hpa-m
|
||||
namespace: {{.Namespace}}
|
||||
labels:
|
||||
app: {{.Name}}-hpa-m
|
||||
spec:
|
||||
scaleTargetRef:
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
name: {{.Name}}
|
||||
minReplicas: {{.MinReplicas}}
|
||||
maxReplicas: {{.MaxReplicas}}
|
||||
metrics:
|
||||
- type: Resource
|
||||
resource:
|
||||
name: memory
|
||||
target:
|
||||
type: Utilization
|
||||
averageUtilization: 80
|
||||
37
deploy/template/kube/job.tpl
Normal file
37
deploy/template/kube/job.tpl
Normal file
@@ -0,0 +1,37 @@
|
||||
apiVersion: batch/v1
|
||||
kind: CronJob
|
||||
metadata:
|
||||
name: {{.Name}}
|
||||
namespace: {{.Namespace}}
|
||||
spec:
|
||||
successfulJobsHistoryLimit: {{.SuccessfulJobsHistoryLimit}}
|
||||
schedule: "{{.Schedule}}"
|
||||
jobTemplate:
|
||||
spec:
|
||||
template:
|
||||
spec:{{if .ServiceAccount}}
|
||||
serviceAccountName: {{.ServiceAccount}}{{end}}
|
||||
{{end}}containers:
|
||||
- name: {{.Name}}
|
||||
image: # todo image url
|
||||
resources:
|
||||
requests:
|
||||
cpu: {{.RequestCpu}}m
|
||||
memory: {{.RequestMem}}Mi
|
||||
limits:
|
||||
cpu: {{.LimitCpu}}m
|
||||
memory: {{.LimitMem}}Mi
|
||||
command:
|
||||
- ./{{.ServiceName}}
|
||||
- -f
|
||||
- ./{{.Name}}.yaml
|
||||
volumeMounts:
|
||||
- name: timezone
|
||||
mountPath: /etc/localtime
|
||||
imagePullSecrets:
|
||||
- name: # registry secret, if no, remove this
|
||||
restartPolicy: OnFailure
|
||||
volumes:
|
||||
- name: timezone
|
||||
hostPath:
|
||||
path: /usr/share/zoneinfo/Asia/Shanghai
|
||||
21
deploy/template/model/delete.tpl
Normal file
21
deploy/template/model/delete.tpl
Normal file
@@ -0,0 +1,21 @@
|
||||
func (m *default{{.upperStartCamelObject}}Model) Delete(ctx context.Context, session sqlx.Session, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) error {
|
||||
{{if .withCache}}{{if .containsIndexCache}}data, err:=m.FindOne(ctx, {{.lowerStartCamelPrimaryKey}})
|
||||
if err!=nil{
|
||||
return err
|
||||
}
|
||||
|
||||
{{end}} {{.keys}}
|
||||
_, err {{if .containsIndexCache}}={{else}}:={{end}} m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("delete from %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}}", m.table)
|
||||
if session!=nil{
|
||||
return session.ExecCtx(ctx,query, {{.lowerStartCamelPrimaryKey}})
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, {{.lowerStartCamelPrimaryKey}})
|
||||
}, {{.keyValues}}){{else}}query := fmt.Sprintf("delete from %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}}", m.table)
|
||||
if session!=nil{
|
||||
_,err:= session.ExecCtx(ctx,query, {{.lowerStartCamelPrimaryKey}})
|
||||
return err
|
||||
}
|
||||
_,err:=m.conn.ExecCtx(ctx, query, {{.lowerStartCamelPrimaryKey}}){{end}}
|
||||
return err
|
||||
}
|
||||
9
deploy/template/model/err.tpl
Normal file
9
deploy/template/model/err.tpl
Normal file
@@ -0,0 +1,9 @@
|
||||
package {{.pkg}}
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
)
|
||||
|
||||
var ErrNotFound = sqlx.ErrNotFound
|
||||
var ErrNoRowsUpdate = errors.New("update db no rows change")
|
||||
1
deploy/template/model/field.tpl
Normal file
1
deploy/template/model/field.tpl
Normal file
@@ -0,0 +1 @@
|
||||
{{.name}} {{.type}} {{.tag}} {{if .hasComment}}// {{.comment}}{{end}}
|
||||
7
deploy/template/model/find-one-by-field-extra-method.tpl
Normal file
7
deploy/template/model/find-one-by-field-extra-method.tpl
Normal file
@@ -0,0 +1,7 @@
|
||||
func (m *default{{.upperStartCamelObject}}Model) formatPrimary(primary interface{}) string {
|
||||
return fmt.Sprintf("%s%v", {{.primaryKeyLeft}}, primary)
|
||||
}
|
||||
func (m *default{{.upperStartCamelObject}}Model) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary interface{}) error {
|
||||
query := fmt.Sprintf("select %s from %s where {{.originalPrimaryField}} = {{if .postgreSql}}$1{{else}}?{{end}} and del_state = ? limit 1", {{.lowerStartCamelObject}}Rows, m.table )
|
||||
return conn.QueryRowCtx(ctx, v, query, primary,globalkey.DelStateNo)
|
||||
}
|
||||
32
deploy/template/model/find-one-by-field.tpl
Normal file
32
deploy/template/model/find-one-by-field.tpl
Normal file
@@ -0,0 +1,32 @@
|
||||
|
||||
func (m *default{{.upperStartCamelObject}}Model) FindOneBy{{.upperField}}(ctx context.Context, {{.in}}) (*{{.upperStartCamelObject}}, error) {
|
||||
{{if .withCache}}{{.cacheKey}}
|
||||
var resp {{.upperStartCamelObject}}
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, {{.cacheKeyVariable}}, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
||||
query := fmt.Sprintf("select %s from %s where {{.originalField}} and del_state = ? limit 1", {{.lowerStartCamelObject}}Rows, m.table)
|
||||
if err := conn.QueryRowCtx(ctx, &resp, query, {{.lowerStartCamelField}},globalkey.DelStateNo); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.{{.upperStartCamelPrimaryKey}}, nil
|
||||
}, m.queryPrimary)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}{{else}}var resp {{.upperStartCamelObject}}
|
||||
query := fmt.Sprintf("select %s from %s where {{.originalField}} and del_state = ? limit 1", {{.lowerStartCamelObject}}Rows, m.table )
|
||||
err := m.conn.QueryRowCtx(ctx, &resp, query, {{.lowerStartCamelField}},globalkey.DelStateNo)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}{{end}}
|
||||
|
||||
26
deploy/template/model/find-one.tpl
Normal file
26
deploy/template/model/find-one.tpl
Normal file
@@ -0,0 +1,26 @@
|
||||
func (m *default{{.upperStartCamelObject}}Model) FindOne(ctx context.Context, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error) {
|
||||
{{if .withCache}}{{.cacheKey}}
|
||||
var resp {{.upperStartCamelObject}}
|
||||
err := m.QueryRowCtx(ctx, &resp, {{.cacheKeyVariable}}, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
|
||||
query := fmt.Sprintf("select %s from %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}} and del_state = ? limit 1", {{.lowerStartCamelObject}}Rows, m.table)
|
||||
return conn.QueryRowCtx(ctx, v, query, {{.lowerStartCamelPrimaryKey}},globalkey.DelStateNo)
|
||||
})
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}{{else}}query := fmt.Sprintf("select %s from %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}} and del_state = ? limit 1", {{.lowerStartCamelObject}}Rows, m.table)
|
||||
var resp {{.upperStartCamelObject}}
|
||||
err := m.conn.QueryRowCtx(ctx, &resp, query, {{.lowerStartCamelPrimaryKey}},globalkey.DelStateNo)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}{{end}}
|
||||
}
|
||||
18
deploy/template/model/import-no-cache.tpl
Normal file
18
deploy/template/model/import-no-cache.tpl
Normal file
@@ -0,0 +1,18 @@
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
{{if .time}}"time"{{end}}
|
||||
"reflect"
|
||||
|
||||
"in-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"
|
||||
)
|
||||
19
deploy/template/model/import.tpl
Normal file
19
deploy/template/model/import.tpl
Normal file
@@ -0,0 +1,19 @@
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
{{if .time}}"time"{{end}}
|
||||
"reflect"
|
||||
|
||||
"in-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"
|
||||
)
|
||||
34
deploy/template/model/insert.tpl
Normal file
34
deploy/template/model/insert.tpl
Normal file
@@ -0,0 +1,34 @@
|
||||
|
||||
func (m *default{{.upperStartCamelObject}}Model) Insert(ctx context.Context,session sqlx.Session, data *{{.upperStartCamelObject}}) (sql.Result,error) {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
1
deploy/template/model/interface-delete.tpl
Normal file
1
deploy/template/model/interface-delete.tpl
Normal file
@@ -0,0 +1 @@
|
||||
Delete(ctx context.Context,session sqlx.Session, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) error
|
||||
1
deploy/template/model/interface-find-one-by-field.tpl
Normal file
1
deploy/template/model/interface-find-one-by-field.tpl
Normal file
@@ -0,0 +1 @@
|
||||
FindOneBy{{.upperField}}(ctx context.Context, {{.in}}) (*{{.upperStartCamelObject}}, error)
|
||||
1
deploy/template/model/interface-find-one.tpl
Normal file
1
deploy/template/model/interface-find-one.tpl
Normal file
@@ -0,0 +1 @@
|
||||
FindOne(ctx context.Context, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error)
|
||||
1
deploy/template/model/interface-insert.tpl
Normal file
1
deploy/template/model/interface-insert.tpl
Normal file
@@ -0,0 +1 @@
|
||||
Insert(ctx context.Context, session sqlx.Session,data *{{.upperStartCamelObject}}) (sql.Result,error)
|
||||
12
deploy/template/model/interface-update.tpl
Normal file
12
deploy/template/model/interface-update.tpl
Normal file
@@ -0,0 +1,12 @@
|
||||
Update(ctx context.Context,session sqlx.Session, data *{{.upperStartCamelObject}}) (sql.Result, error)
|
||||
UpdateWithVersion(ctx context.Context,session sqlx.Session,data *{{.upperStartCamelObject}}) 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 *{{.upperStartCamelObject}}) 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) ([]*{{.upperStartCamelObject}},error)
|
||||
FindPageListByPage(ctx context.Context,rowBuilder squirrel.SelectBuilder,page ,pageSize int64,orderBy string) ([]*{{.upperStartCamelObject}},error)
|
||||
FindPageListByPageWithTotal(ctx context.Context, rowBuilder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*{{.upperStartCamelObject}}, int64, error)
|
||||
FindPageListByIdDESC(ctx context.Context,rowBuilder squirrel.SelectBuilder ,preMinId ,pageSize int64) ([]*{{.upperStartCamelObject}},error)
|
||||
FindPageListByIdASC(ctx context.Context,rowBuilder squirrel.SelectBuilder,preMaxId ,pageSize int64) ([]*{{.upperStartCamelObject}},error)
|
||||
13
deploy/template/model/model-gen.tpl
Normal file
13
deploy/template/model/model-gen.tpl
Normal file
@@ -0,0 +1,13 @@
|
||||
// Code generated by goctl. DO NOT EDIT!
|
||||
|
||||
package {{.pkg}}
|
||||
{{.imports}}
|
||||
{{.vars}}
|
||||
{{.types}}
|
||||
{{.new}}
|
||||
{{.insert}}
|
||||
{{.find}}
|
||||
{{.update}}
|
||||
{{.delete}}
|
||||
{{.extraMethod}}
|
||||
{{.tableName}}
|
||||
7
deploy/template/model/model-new.tpl
Normal file
7
deploy/template/model/model-new.tpl
Normal file
@@ -0,0 +1,7 @@
|
||||
|
||||
func new{{.upperStartCamelObject}}Model(conn sqlx.SqlConn{{if .withCache}}, c cache.CacheConf{{end}}) *default{{.upperStartCamelObject}}Model {
|
||||
return &default{{.upperStartCamelObject}}Model{
|
||||
{{if .withCache}}CachedConn: sqlc.NewConn(conn, c){{else}}conn:conn{{end}},
|
||||
table: {{.table}},
|
||||
}
|
||||
}
|
||||
37
deploy/template/model/model.tpl
Normal file
37
deploy/template/model/model.tpl
Normal file
@@ -0,0 +1,37 @@
|
||||
package {{.pkg}}
|
||||
{{if .withCache}}
|
||||
import (
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
|
||||
)
|
||||
{{else}}
|
||||
import (
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
|
||||
)
|
||||
|
||||
{{end}}
|
||||
var _ {{.upperStartCamelObject}}Model = (*custom{{.upperStartCamelObject}}Model)(nil)
|
||||
|
||||
type (
|
||||
// {{.upperStartCamelObject}}Model is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in custom{{.upperStartCamelObject}}Model.
|
||||
{{.upperStartCamelObject}}Model interface {
|
||||
{{.lowerStartCamelObject}}Model
|
||||
|
||||
}
|
||||
|
||||
custom{{.upperStartCamelObject}}Model struct {
|
||||
*default{{.upperStartCamelObject}}Model
|
||||
}
|
||||
)
|
||||
|
||||
// New{{.upperStartCamelObject}}Model returns a model for the database table.
|
||||
func New{{.upperStartCamelObject}}Model(conn sqlx.SqlConn{{if .withCache}}, c cache.CacheConf{{end}}) {{.upperStartCamelObject}}Model {
|
||||
return &custom{{.upperStartCamelObject}}Model{
|
||||
default{{.upperStartCamelObject}}Model: new{{.upperStartCamelObject}}Model(conn{{if .withCache}}, c{{end}}),
|
||||
}
|
||||
}
|
||||
4
deploy/template/model/table-name.tpl
Normal file
4
deploy/template/model/table-name.tpl
Normal file
@@ -0,0 +1,4 @@
|
||||
|
||||
func (m *default{{.upperStartCamelObject}}Model) tableName() string {
|
||||
return m.table
|
||||
}
|
||||
1
deploy/template/model/tag.tpl
Normal file
1
deploy/template/model/tag.tpl
Normal file
@@ -0,0 +1 @@
|
||||
`db:"{{.field}}"`
|
||||
15
deploy/template/model/types.tpl
Normal file
15
deploy/template/model/types.tpl
Normal file
@@ -0,0 +1,15 @@
|
||||
|
||||
type (
|
||||
{{.lowerStartCamelObject}}Model interface{
|
||||
{{.method}}
|
||||
}
|
||||
|
||||
default{{.upperStartCamelObject}}Model struct {
|
||||
{{if .withCache}}sqlc.CachedConn{{else}}conn sqlx.SqlConn{{end}}
|
||||
table string
|
||||
}
|
||||
|
||||
{{.upperStartCamelObject}} struct {
|
||||
{{.fields}}
|
||||
}
|
||||
)
|
||||
286
deploy/template/model/update.tpl
Normal file
286
deploy/template/model/update.tpl
Normal file
@@ -0,0 +1,286 @@
|
||||
|
||||
func (m *default{{.upperStartCamelObject}}Model) Update(ctx context.Context,session sqlx.Session, {{if .containsIndexCache}}newData{{else}}data{{end}} *{{.upperStartCamelObject}}) (sql.Result,error) {
|
||||
{{if .withCache}}{{if .containsIndexCache}}data, err:=m.FindOne(ctx, newData.{{.upperStartCamelPrimaryKey}})
|
||||
if err!=nil{
|
||||
return nil,err
|
||||
}
|
||||
{{end}}{{.keys}}
|
||||
return m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("update %s set %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}}", m.table, {{.lowerStartCamelObject}}RowsWithPlaceHolder)
|
||||
if session != nil{
|
||||
return session.ExecCtx(ctx,query, {{.expressionValues}})
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, {{.expressionValues}})
|
||||
}, {{.keyValues}}){{else}}query := fmt.Sprintf("update %s set %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}}", m.table, {{.lowerStartCamelObject}}RowsWithPlaceHolder)
|
||||
if session != nil{
|
||||
return session.ExecCtx(ctx,query, {{.expressionValues}})
|
||||
}
|
||||
return m.conn.ExecCtx(ctx, query, {{.expressionValues}}){{end}}
|
||||
}
|
||||
|
||||
func (m *default{{.upperStartCamelObject}}Model) UpdateWithVersion(ctx context.Context,session sqlx.Session,{{if .containsIndexCache}}newData{{else}}data{{end}} *{{.upperStartCamelObject}}) error {
|
||||
|
||||
{{if .containsIndexCache}}
|
||||
oldVersion := newData.Version
|
||||
newData.Version += 1
|
||||
{{else}}
|
||||
oldVersion := data.Version
|
||||
data.Version += 1
|
||||
{{end}}
|
||||
|
||||
var sqlResult sql.Result
|
||||
var err error
|
||||
|
||||
{{if .withCache}}{{if .containsIndexCache}}data, err:=m.FindOne(ctx, newData.{{.upperStartCamelPrimaryKey}})
|
||||
if err!=nil{
|
||||
return err
|
||||
}
|
||||
{{end}}{{.keys}}
|
||||
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 {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}} and version = ? ", m.table, {{.lowerStartCamelObject}}RowsWithPlaceHolder)
|
||||
if session != nil{
|
||||
return session.ExecCtx(ctx,query, {{.expressionValues}},oldVersion)
|
||||
}
|
||||
return conn.ExecCtx(ctx,query, {{.expressionValues}},oldVersion)
|
||||
}, {{.keyValues}}){{else}}query := fmt.Sprintf("update %s set %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}} and version = ? ", m.table, {{.lowerStartCamelObject}}RowsWithPlaceHolder)
|
||||
if session != nil{
|
||||
sqlResult,err = session.ExecCtx(ctx,query, {{.expressionValues}},oldVersion)
|
||||
}else{
|
||||
sqlResult,err = m.conn.ExecCtx(ctx,query, {{.expressionValues}},oldVersion)
|
||||
}
|
||||
{{end}}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
updateCount , err := sqlResult.RowsAffected()
|
||||
if err != nil{
|
||||
return err
|
||||
}
|
||||
if updateCount == 0 {
|
||||
return ErrNoRowsUpdate
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *default{{.upperStartCamelObject}}Model) DeleteSoft(ctx context.Context,session sqlx.Session,data *{{.upperStartCamelObject}}) 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 "),"{{.upperStartCamelObject}}Model delete err : %+v",err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *default{{.upperStartCamelObject}}Model) 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
|
||||
{{if .withCache}}err = m.QueryRowNoCacheCtx(ctx,&resp, query, values...){{else}}
|
||||
err = m.conn.QueryRowCtx(ctx,&resp, query, values...)
|
||||
{{end}}
|
||||
switch err {
|
||||
case nil:
|
||||
return resp, nil
|
||||
default:
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *default{{.upperStartCamelObject}}Model) 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
|
||||
{{if .withCache}}err = m.QueryRowNoCacheCtx(ctx,&resp, query, values...){{else}}
|
||||
err = m.conn.QueryRowCtx(ctx,&resp, query, values...)
|
||||
{{end}}
|
||||
switch err {
|
||||
case nil:
|
||||
return resp, nil
|
||||
default:
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *default{{.upperStartCamelObject}}Model) FindAll(ctx context.Context,builder squirrel.SelectBuilder,orderBy string) ([]*{{.upperStartCamelObject}},error) {
|
||||
|
||||
builder = builder.Columns({{.lowerStartCamelObject}}Rows)
|
||||
|
||||
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 []*{{.upperStartCamelObject}}
|
||||
{{if .withCache}}err = m.QueryRowsNoCacheCtx(ctx,&resp, query, values...){{else}}
|
||||
err = m.conn.QueryRowsCtx(ctx,&resp, query, values...)
|
||||
{{end}}
|
||||
switch err {
|
||||
case nil:
|
||||
return resp, nil
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *default{{.upperStartCamelObject}}Model) FindPageListByPage(ctx context.Context,builder squirrel.SelectBuilder,page ,pageSize int64,orderBy string) ([]*{{.upperStartCamelObject}},error) {
|
||||
|
||||
builder = builder.Columns({{.lowerStartCamelObject}}Rows)
|
||||
|
||||
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 []*{{.upperStartCamelObject}}
|
||||
{{if .withCache}}err = m.QueryRowsNoCacheCtx(ctx,&resp, query, values...){{else}}
|
||||
err = m.conn.QueryRowsCtx(ctx,&resp, query, values...)
|
||||
{{end}}
|
||||
switch err {
|
||||
case nil:
|
||||
return resp, nil
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (m *default{{.upperStartCamelObject}}Model) FindPageListByPageWithTotal(ctx context.Context,builder squirrel.SelectBuilder,page ,pageSize int64,orderBy string) ([]*{{.upperStartCamelObject}},int64,error) {
|
||||
|
||||
total, err := m.FindCount(ctx, builder, "id")
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
builder = builder.Columns({{.lowerStartCamelObject}}Rows)
|
||||
|
||||
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 []*{{.upperStartCamelObject}}
|
||||
{{if .withCache}}err = m.QueryRowsNoCacheCtx(ctx,&resp, query, values...){{else}}
|
||||
err = m.conn.QueryRowsCtx(ctx,&resp, query, values...)
|
||||
{{end}}
|
||||
switch err {
|
||||
case nil:
|
||||
return resp,total, nil
|
||||
default:
|
||||
return nil,total, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *default{{.upperStartCamelObject}}Model) FindPageListByIdDESC(ctx context.Context,builder squirrel.SelectBuilder ,preMinId ,pageSize int64) ([]*{{.upperStartCamelObject}},error) {
|
||||
|
||||
builder = builder.Columns({{.lowerStartCamelObject}}Rows)
|
||||
|
||||
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 []*{{.upperStartCamelObject}}
|
||||
{{if .withCache}}err = m.QueryRowsNoCacheCtx(ctx,&resp, query, values...){{else}}
|
||||
err = m.conn.QueryRowsCtx(ctx,&resp, query, values...)
|
||||
{{end}}
|
||||
switch err {
|
||||
case nil:
|
||||
return resp, nil
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *default{{.upperStartCamelObject}}Model) FindPageListByIdASC(ctx context.Context,builder squirrel.SelectBuilder,preMaxId ,pageSize int64) ([]*{{.upperStartCamelObject}},error) {
|
||||
|
||||
builder = builder.Columns({{.lowerStartCamelObject}}Rows)
|
||||
|
||||
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 []*{{.upperStartCamelObject}}
|
||||
{{if .withCache}}err = m.QueryRowsNoCacheCtx(ctx,&resp, query, values...){{else}}
|
||||
err = m.conn.QueryRowsCtx(ctx,&resp, query, values...)
|
||||
{{end}}
|
||||
switch err {
|
||||
case nil:
|
||||
return resp, nil
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *default{{.upperStartCamelObject}}Model) Trans(ctx context.Context,fn func(ctx context.Context,session sqlx.Session) error) error {
|
||||
{{if .withCache}}
|
||||
return m.TransactCtx(ctx,func(ctx context.Context,session sqlx.Session) error {
|
||||
return fn(ctx,session)
|
||||
})
|
||||
{{else}}
|
||||
return m.conn.TransactCtx(ctx,func(ctx context.Context,session sqlx.Session) error {
|
||||
return fn(ctx,session)
|
||||
})
|
||||
{{end}}
|
||||
}
|
||||
|
||||
func(m *default{{.upperStartCamelObject}}Model) SelectBuilder() squirrel.SelectBuilder {
|
||||
return squirrel.Select().From(m.table)
|
||||
}
|
||||
9
deploy/template/model/var.tpl
Normal file
9
deploy/template/model/var.tpl
Normal file
@@ -0,0 +1,9 @@
|
||||
|
||||
var (
|
||||
{{.lowerStartCamelObject}}FieldNames = builder.RawFieldNames(&{{.upperStartCamelObject}}{}{{if .postgreSql}},true{{end}})
|
||||
{{.lowerStartCamelObject}}Rows = strings.Join({{.lowerStartCamelObject}}FieldNames, ",")
|
||||
{{.lowerStartCamelObject}}RowsExpectAutoSet = {{if .postgreSql}}strings.Join(stringx.Remove({{.lowerStartCamelObject}}FieldNames, {{if .autoIncrement}}"{{.originalPrimaryKey}}",{{end}} "create_time", "update_time"), ","){{else}}strings.Join(stringx.Remove({{.lowerStartCamelObject}}FieldNames, {{if .autoIncrement}}"{{.originalPrimaryKey}}",{{end}} "`create_time`", "`update_time`"), ","){{end}}
|
||||
{{.lowerStartCamelObject}}RowsWithPlaceHolder = {{if .postgreSql}}builder.PostgreSqlJoin(stringx.Remove({{.lowerStartCamelObject}}FieldNames, "{{.originalPrimaryKey}}", "create_time", "update_time")){{else}}strings.Join(stringx.Remove({{.lowerStartCamelObject}}FieldNames, "{{.originalPrimaryKey}}", "`create_time`", "`update_time`"), "=?,") + "=?"{{end}}
|
||||
|
||||
{{if .withCache}}{{.cacheKeys}}{{end}}
|
||||
)
|
||||
12
deploy/template/mongo/err.tpl
Normal file
12
deploy/template/mongo/err.tpl
Normal file
@@ -0,0 +1,12 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/mon"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotFound = mon.ErrNotFound
|
||||
ErrInvalidObjectId = errors.New("invalid objectId")
|
||||
)
|
||||
78
deploy/template/mongo/model.tpl
Normal file
78
deploy/template/mongo/model.tpl
Normal file
@@ -0,0 +1,78 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
{{if .Cache}}"github.com/zeromicro/go-zero/core/stores/monc"{{else}}"github.com/zeromicro/go-zero/core/stores/mon"{{end}}
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
{{if .Cache}}var prefix{{.Type}}CacheKey = "cache:{{.lowerType}}:"{{end}}
|
||||
|
||||
type {{.lowerType}}Model interface{
|
||||
Insert(ctx context.Context,data *{{.Type}}) error
|
||||
FindOne(ctx context.Context,id string) (*{{.Type}}, error)
|
||||
Update(ctx context.Context,data *{{.Type}}) (*mongo.UpdateResult, error)
|
||||
Delete(ctx context.Context,id string) (int64, error)
|
||||
}
|
||||
|
||||
type default{{.Type}}Model struct {
|
||||
conn {{if .Cache}}*monc.Model{{else}}*mon.Model{{end}}
|
||||
}
|
||||
|
||||
func newDefault{{.Type}}Model(conn {{if .Cache}}*monc.Model{{else}}*mon.Model{{end}}) *default{{.Type}}Model {
|
||||
return &default{{.Type}}Model{conn: conn}
|
||||
}
|
||||
|
||||
|
||||
func (m *default{{.Type}}Model) Insert(ctx context.Context, data *{{.Type}}) error {
|
||||
if data.ID.IsZero() {
|
||||
data.ID = primitive.NewObjectID()
|
||||
data.CreateAt = time.Now()
|
||||
data.UpdateAt = time.Now()
|
||||
}
|
||||
|
||||
{{if .Cache}}key := prefix{{.Type}}CacheKey + data.ID.Hex(){{end}}
|
||||
_, err := m.conn.InsertOne(ctx, {{if .Cache}}key, {{end}} data)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *default{{.Type}}Model) FindOne(ctx context.Context, id string) (*{{.Type}}, error) {
|
||||
oid, err := primitive.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidObjectId
|
||||
}
|
||||
|
||||
var data {{.Type}}
|
||||
{{if .Cache}}key := prefix{{.Type}}CacheKey + id{{end}}
|
||||
err = m.conn.FindOne(ctx, {{if .Cache}}key, {{end}}&data, bson.M{"_id": oid})
|
||||
switch err {
|
||||
case nil:
|
||||
return &data, nil
|
||||
case {{if .Cache}}monc{{else}}mon{{end}}.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *default{{.Type}}Model) Update(ctx context.Context, data *{{.Type}}) (*mongo.UpdateResult, error) {
|
||||
data.UpdateAt = time.Now()
|
||||
{{if .Cache}}key := prefix{{.Type}}CacheKey + data.ID.Hex(){{end}}
|
||||
res, err := m.conn.UpdateOne(ctx, {{if .Cache}}key, {{end}}bson.M{"_id": data.ID}, bson.M{"$set": data})
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (m *default{{.Type}}Model) Delete(ctx context.Context, id string) (int64, error) {
|
||||
oid, err := primitive.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return 0, ErrInvalidObjectId
|
||||
}
|
||||
{{if .Cache}}key := prefix{{.Type}}CacheKey +id{{end}}
|
||||
res, err := m.conn.DeleteOne(ctx, {{if .Cache}}key, {{end}}bson.M{"_id": oid})
|
||||
return res, err
|
||||
}
|
||||
38
deploy/template/mongo/model_custom.tpl
Normal file
38
deploy/template/mongo/model_custom.tpl
Normal file
@@ -0,0 +1,38 @@
|
||||
package model
|
||||
|
||||
{{if .Cache}}import (
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/monc"
|
||||
){{else}}import "github.com/zeromicro/go-zero/core/stores/mon"{{end}}
|
||||
|
||||
{{if .Easy}}
|
||||
const {{.Type}}CollectionName = "{{.snakeType}}"
|
||||
{{end}}
|
||||
|
||||
var _ {{.Type}}Model = (*custom{{.Type}}Model)(nil)
|
||||
|
||||
type (
|
||||
// {{.Type}}Model is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in custom{{.Type}}Model.
|
||||
{{.Type}}Model interface {
|
||||
{{.lowerType}}Model
|
||||
}
|
||||
|
||||
custom{{.Type}}Model struct {
|
||||
*default{{.Type}}Model
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
// New{{.Type}}Model returns a model for the mongo.
|
||||
{{if .Easy}}func New{{.Type}}Model(url, db string{{if .Cache}}, c cache.CacheConf{{end}}) {{.Type}}Model {
|
||||
conn := {{if .Cache}}monc{{else}}mon{{end}}.MustNewModel(url, db, {{.Type}}CollectionName{{if .Cache}}, c{{end}})
|
||||
return &custom{{.Type}}Model{
|
||||
default{{.Type}}Model: newDefault{{.Type}}Model(conn),
|
||||
}
|
||||
}{{else}}func New{{.Type}}Model(url, db, collection string{{if .Cache}}, c cache.CacheConf{{end}}) {{.Type}}Model {
|
||||
conn := {{if .Cache}}monc{{else}}mon{{end}}.MustNewModel(url, db, collection{{if .Cache}}, c{{end}})
|
||||
return &custom{{.Type}}Model{
|
||||
default{{.Type}}Model: newDefault{{.Type}}Model(conn),
|
||||
}
|
||||
}{{end}}
|
||||
14
deploy/template/mongo/model_types.tpl
Normal file
14
deploy/template/mongo/model_types.tpl
Normal file
@@ -0,0 +1,14 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type {{.Type}} struct {
|
||||
ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
|
||||
// TODO: Fill your own fields
|
||||
UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
|
||||
CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
|
||||
}
|
||||
12
deploy/template/newapi/newtemplate.tpl
Normal file
12
deploy/template/newapi/newtemplate.tpl
Normal file
@@ -0,0 +1,12 @@
|
||||
type Request {
|
||||
Name string `path:"name,options=you|me"`
|
||||
}
|
||||
|
||||
type Response {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
service {{.name}}-api {
|
||||
@handler {{.handler}}Handler
|
||||
get /from/:name(Request) returns (Response)
|
||||
}
|
||||
33
deploy/template/rpc/call.tpl
Normal file
33
deploy/template/rpc/call.tpl
Normal file
@@ -0,0 +1,33 @@
|
||||
{{.head}}
|
||||
|
||||
package {{.filePackage}}
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
{{.pbPackage}}
|
||||
{{if ne .pbPackage .protoGoPackage}}{{.protoGoPackage}}{{end}}
|
||||
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type (
|
||||
{{.alias}}
|
||||
|
||||
{{.serviceName}} interface {
|
||||
{{.interface}}
|
||||
}
|
||||
|
||||
default{{.serviceName}} struct {
|
||||
cli zrpc.Client
|
||||
}
|
||||
)
|
||||
|
||||
func New{{.serviceName}}(cli zrpc.Client) {{.serviceName}} {
|
||||
return &default{{.serviceName}}{
|
||||
cli: cli,
|
||||
}
|
||||
}
|
||||
|
||||
{{.functions}}
|
||||
7
deploy/template/rpc/config.tpl
Normal file
7
deploy/template/rpc/config.tpl
Normal file
@@ -0,0 +1,7 @@
|
||||
package config
|
||||
|
||||
import "github.com/zeromicro/go-zero/zrpc"
|
||||
|
||||
type Config struct {
|
||||
zrpc.RpcServerConf
|
||||
}
|
||||
6
deploy/template/rpc/etc.tpl
Normal file
6
deploy/template/rpc/etc.tpl
Normal file
@@ -0,0 +1,6 @@
|
||||
Name: {{.serviceName}}.rpc
|
||||
ListenOn: 0.0.0.0:8080
|
||||
Etcd:
|
||||
Hosts:
|
||||
- 127.0.0.1:2379
|
||||
Key: {{.serviceName}}.rpc
|
||||
6
deploy/template/rpc/logic-func.tpl
Normal file
6
deploy/template/rpc/logic-func.tpl
Normal file
@@ -0,0 +1,6 @@
|
||||
{{if .hasComment}}{{.comment}}{{end}}
|
||||
func (l *{{.logicName}}) {{.method}} ({{if .hasReq}}in {{.request}}{{if .stream}},stream {{.streamBody}}{{end}}{{else}}stream {{.streamBody}}{{end}}) ({{if .hasReply}}{{.response}},{{end}} error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return {{if .hasReply}}&{{.responseType}}{},{{end}} nil
|
||||
}
|
||||
24
deploy/template/rpc/logic.tpl
Normal file
24
deploy/template/rpc/logic.tpl
Normal file
@@ -0,0 +1,24 @@
|
||||
package {{.packageName}}
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
{{.imports}}
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type {{.logicName}} struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func New{{.logicName}}(ctx context.Context,svcCtx *svc.ServiceContext) *{{.logicName}} {
|
||||
return &{{.logicName}}{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
{{.functions}}
|
||||
42
deploy/template/rpc/main.tpl
Normal file
42
deploy/template/rpc/main.tpl
Normal file
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
|
||||
{{.imports}}
|
||||
"in-server/common/interceptor/rpcserver"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/core/service"
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/reflection"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "etc/{{.serviceName}}.yaml", "the config file")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var c config.Config
|
||||
conf.MustLoad(*configFile, &c)
|
||||
ctx := svc.NewServiceContext(c)
|
||||
|
||||
s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
|
||||
{{range .serviceNames}} {{.Pkg}}.Register{{.Service}}Server(grpcServer, {{.ServerPkg}}.New{{.Service}}Server(ctx))
|
||||
{{end}}
|
||||
if c.Mode == service.DevMode || c.Mode == service.TestMode {
|
||||
reflection.Register(grpcServer)
|
||||
}
|
||||
})
|
||||
|
||||
//rpc log
|
||||
s.AddUnaryInterceptors(rpcserver.LoggerInterceptor)
|
||||
|
||||
defer s.Stop()
|
||||
|
||||
fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
|
||||
s.Start()
|
||||
}
|
||||
|
||||
6
deploy/template/rpc/server-func.tpl
Normal file
6
deploy/template/rpc/server-func.tpl
Normal file
@@ -0,0 +1,6 @@
|
||||
|
||||
{{if .hasComment}}{{.comment}}{{end}}
|
||||
func (s *{{.server}}Server) {{.method}} ({{if .notStream}}ctx context.Context,{{if .hasReq}} in {{.request}}{{end}}{{else}}{{if .hasReq}} in {{.request}},{{end}}stream {{.streamBody}}{{end}}) ({{if .notStream}}{{.response}},{{end}}error) {
|
||||
l := {{.logicPkg}}.New{{.logicName}}({{if .notStream}}ctx,{{else}}stream.Context(),{{end}}s.svcCtx)
|
||||
return l.{{.method}}({{if .hasReq}}in{{if .stream}} ,stream{{end}}{{else}}{{if .stream}}stream{{end}}{{end}})
|
||||
}
|
||||
22
deploy/template/rpc/server.tpl
Normal file
22
deploy/template/rpc/server.tpl
Normal file
@@ -0,0 +1,22 @@
|
||||
{{.head}}
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
{{if .notStream}}"context"{{end}}
|
||||
|
||||
{{.imports}}
|
||||
)
|
||||
|
||||
type {{.server}}Server struct {
|
||||
svcCtx *svc.ServiceContext
|
||||
{{.unimplementedServer}}
|
||||
}
|
||||
|
||||
func New{{.server}}Server(svcCtx *svc.ServiceContext) *{{.server}}Server {
|
||||
return &{{.server}}Server{
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
{{.funcs}}
|
||||
13
deploy/template/rpc/svc.tpl
Normal file
13
deploy/template/rpc/svc.tpl
Normal file
@@ -0,0 +1,13 @@
|
||||
package svc
|
||||
|
||||
import {{.imports}}
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
return &ServiceContext{
|
||||
Config:c,
|
||||
}
|
||||
}
|
||||
16
deploy/template/rpc/template.tpl
Normal file
16
deploy/template/rpc/template.tpl
Normal file
@@ -0,0 +1,16 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package {{.package}};
|
||||
option go_package="./{{.package}}";
|
||||
|
||||
message Request {
|
||||
string ping = 1;
|
||||
}
|
||||
|
||||
message Response {
|
||||
string pong = 1;
|
||||
}
|
||||
|
||||
service {{.serviceName}} {
|
||||
rpc Ping(Request) returns(Response);
|
||||
}
|
||||
Reference in New Issue
Block a user