feat: 实现账号与佣金管理模块
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 4m35s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 4m35s
新增功能: - 店铺佣金查询:店铺佣金统计、店铺佣金记录列表、店铺提现记录 - 佣金提现审批:提现申请列表、审批通过、审批拒绝 - 提现配置管理:配置列表、新增配置、获取当前生效配置 - 企业管理:企业列表、创建、更新、删除、获取详情 - 企业卡授权:授权列表、批量授权、批量取消授权、统计 - 客户账号管理:账号列表、创建、更新状态、重置密码 - 我的佣金:佣金统计、佣金记录、提现申请、提现记录 数据库变更: - 扩展 tb_commission_withdrawal_request 新增提现单号等字段 - 扩展 tb_account 新增 is_primary 字段 - 扩展 tb_commission_record 新增 shop_id、balance_after - 扩展 tb_commission_withdrawal_setting 新增每日提现次数限制 - 扩展 tb_iot_card、tb_device 新增 shop_id 冗余字段 - 新建 tb_enterprise_card_authorization 企业卡授权表 - 新建 tb_asset_allocation_record 资产分配记录表 - 数据迁移:owner_type 枚举值 agent 统一为 shop 测试: - 新增 7 个单元测试文件覆盖各服务 - 修复集成测试 Redis 依赖问题
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -73,3 +73,4 @@ cmd/api/api
|
||||
ai-gateway.conf
|
||||
__debug_bin1621385388
|
||||
docs/admin-openapi.yaml
|
||||
api
|
||||
|
||||
1790
docs/需求规划/账号与佣金管理模块需求规划.md
Normal file
1790
docs/需求规划/账号与佣金管理模块需求规划.md
Normal file
File diff suppressed because it is too large
Load Diff
@@ -7,18 +7,24 @@ import (
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
// initHandlers 初始化所有 Handler 实例
|
||||
func initHandlers(svc *services, deps *Dependencies) *Handlers {
|
||||
validate := validator.New()
|
||||
|
||||
return &Handlers{
|
||||
Account: admin.NewAccountHandler(svc.Account),
|
||||
Role: admin.NewRoleHandler(svc.Role),
|
||||
Permission: admin.NewPermissionHandler(svc.Permission),
|
||||
PersonalCustomer: app.NewPersonalCustomerHandler(svc.PersonalCustomer, deps.Logger),
|
||||
Shop: admin.NewShopHandler(svc.Shop),
|
||||
ShopAccount: admin.NewShopAccountHandler(svc.ShopAccount),
|
||||
AdminAuth: admin.NewAuthHandler(svc.Auth, validate),
|
||||
H5Auth: h5.NewAuthHandler(svc.Auth, validate),
|
||||
Account: admin.NewAccountHandler(svc.Account),
|
||||
Role: admin.NewRoleHandler(svc.Role),
|
||||
Permission: admin.NewPermissionHandler(svc.Permission),
|
||||
PersonalCustomer: app.NewPersonalCustomerHandler(svc.PersonalCustomer, deps.Logger),
|
||||
Shop: admin.NewShopHandler(svc.Shop),
|
||||
ShopAccount: admin.NewShopAccountHandler(svc.ShopAccount),
|
||||
AdminAuth: admin.NewAuthHandler(svc.Auth, validate),
|
||||
H5Auth: h5.NewAuthHandler(svc.Auth, validate),
|
||||
ShopCommission: admin.NewShopCommissionHandler(svc.ShopCommission),
|
||||
CommissionWithdrawal: admin.NewCommissionWithdrawalHandler(svc.CommissionWithdrawal),
|
||||
CommissionWithdrawalSetting: admin.NewCommissionWithdrawalSettingHandler(svc.CommissionWithdrawalSetting),
|
||||
Enterprise: admin.NewEnterpriseHandler(svc.Enterprise),
|
||||
EnterpriseCard: admin.NewEnterpriseCardHandler(svc.EnterpriseCard),
|
||||
CustomerAccount: admin.NewCustomerAccountHandler(svc.CustomerAccount),
|
||||
MyCommission: admin.NewMyCommissionHandler(svc.MyCommission),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,34 +3,52 @@ package bootstrap
|
||||
import (
|
||||
accountSvc "github.com/break/junhong_cmp_fiber/internal/service/account"
|
||||
authSvc "github.com/break/junhong_cmp_fiber/internal/service/auth"
|
||||
commissionWithdrawalSvc "github.com/break/junhong_cmp_fiber/internal/service/commission_withdrawal"
|
||||
commissionWithdrawalSettingSvc "github.com/break/junhong_cmp_fiber/internal/service/commission_withdrawal_setting"
|
||||
customerAccountSvc "github.com/break/junhong_cmp_fiber/internal/service/customer_account"
|
||||
enterpriseSvc "github.com/break/junhong_cmp_fiber/internal/service/enterprise"
|
||||
enterpriseCardSvc "github.com/break/junhong_cmp_fiber/internal/service/enterprise_card"
|
||||
myCommissionSvc "github.com/break/junhong_cmp_fiber/internal/service/my_commission"
|
||||
permissionSvc "github.com/break/junhong_cmp_fiber/internal/service/permission"
|
||||
personalCustomerSvc "github.com/break/junhong_cmp_fiber/internal/service/personal_customer"
|
||||
roleSvc "github.com/break/junhong_cmp_fiber/internal/service/role"
|
||||
shopSvc "github.com/break/junhong_cmp_fiber/internal/service/shop"
|
||||
shopAccountSvc "github.com/break/junhong_cmp_fiber/internal/service/shop_account"
|
||||
shopCommissionSvc "github.com/break/junhong_cmp_fiber/internal/service/shop_commission"
|
||||
)
|
||||
|
||||
// services 封装所有 Service 实例
|
||||
// 注意:此结构体不导出,仅在 bootstrap 包内部使用
|
||||
type services struct {
|
||||
Account *accountSvc.Service
|
||||
Role *roleSvc.Service
|
||||
Permission *permissionSvc.Service
|
||||
PersonalCustomer *personalCustomerSvc.Service
|
||||
Shop *shopSvc.Service
|
||||
ShopAccount *shopAccountSvc.Service
|
||||
Auth *authSvc.Service
|
||||
Account *accountSvc.Service
|
||||
Role *roleSvc.Service
|
||||
Permission *permissionSvc.Service
|
||||
PersonalCustomer *personalCustomerSvc.Service
|
||||
Shop *shopSvc.Service
|
||||
ShopAccount *shopAccountSvc.Service
|
||||
Auth *authSvc.Service
|
||||
ShopCommission *shopCommissionSvc.Service
|
||||
CommissionWithdrawal *commissionWithdrawalSvc.Service
|
||||
CommissionWithdrawalSetting *commissionWithdrawalSettingSvc.Service
|
||||
Enterprise *enterpriseSvc.Service
|
||||
EnterpriseCard *enterpriseCardSvc.Service
|
||||
CustomerAccount *customerAccountSvc.Service
|
||||
MyCommission *myCommissionSvc.Service
|
||||
}
|
||||
|
||||
// initServices 初始化所有 Service 实例
|
||||
func initServices(s *stores, deps *Dependencies) *services {
|
||||
return &services{
|
||||
Account: accountSvc.New(s.Account, s.Role, s.AccountRole),
|
||||
Role: roleSvc.New(s.Role, s.Permission, s.RolePermission),
|
||||
Permission: permissionSvc.New(s.Permission, s.AccountRole, s.RolePermission, deps.Redis),
|
||||
PersonalCustomer: personalCustomerSvc.NewService(s.PersonalCustomer, s.PersonalCustomerPhone, deps.VerificationService, deps.JWTManager, deps.Logger),
|
||||
Shop: shopSvc.New(s.Shop, s.Account),
|
||||
ShopAccount: shopAccountSvc.New(s.Account, s.Shop),
|
||||
Auth: authSvc.New(s.Account, s.AccountRole, s.RolePermission, s.Permission, deps.TokenManager, deps.Logger),
|
||||
Account: accountSvc.New(s.Account, s.Role, s.AccountRole),
|
||||
Role: roleSvc.New(s.Role, s.Permission, s.RolePermission),
|
||||
Permission: permissionSvc.New(s.Permission, s.AccountRole, s.RolePermission, deps.Redis),
|
||||
PersonalCustomer: personalCustomerSvc.NewService(s.PersonalCustomer, s.PersonalCustomerPhone, deps.VerificationService, deps.JWTManager, deps.Logger),
|
||||
Shop: shopSvc.New(s.Shop, s.Account),
|
||||
ShopAccount: shopAccountSvc.New(s.Account, s.Shop),
|
||||
Auth: authSvc.New(s.Account, s.AccountRole, s.RolePermission, s.Permission, deps.TokenManager, deps.Logger),
|
||||
ShopCommission: shopCommissionSvc.New(s.Shop, s.Account, s.Wallet, s.CommissionWithdrawalRequest, s.CommissionRecord),
|
||||
CommissionWithdrawal: commissionWithdrawalSvc.New(deps.DB, s.Shop, s.Account, s.Wallet, s.WalletTransaction, s.CommissionWithdrawalRequest),
|
||||
CommissionWithdrawalSetting: commissionWithdrawalSettingSvc.New(deps.DB, s.Account, s.CommissionWithdrawalSetting),
|
||||
Enterprise: enterpriseSvc.New(deps.DB, s.Enterprise, s.Shop, s.Account),
|
||||
EnterpriseCard: enterpriseCardSvc.New(deps.DB, s.Enterprise, s.EnterpriseCardAuthorization),
|
||||
CustomerAccount: customerAccountSvc.New(deps.DB, s.Account, s.Shop, s.Enterprise),
|
||||
MyCommission: myCommissionSvc.New(deps.DB, s.Shop, s.Wallet, s.CommissionWithdrawalRequest, s.CommissionWithdrawalSetting, s.CommissionRecord, s.WalletTransaction),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,31 +4,40 @@ import (
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
)
|
||||
|
||||
// stores 封装所有 Store 实例
|
||||
// 注意:此结构体不导出,仅在 bootstrap 包内部使用
|
||||
type stores struct {
|
||||
Account *postgres.AccountStore
|
||||
Shop *postgres.ShopStore
|
||||
Role *postgres.RoleStore
|
||||
Permission *postgres.PermissionStore
|
||||
AccountRole *postgres.AccountRoleStore
|
||||
RolePermission *postgres.RolePermissionStore
|
||||
PersonalCustomer *postgres.PersonalCustomerStore
|
||||
PersonalCustomerPhone *postgres.PersonalCustomerPhoneStore
|
||||
// TODO: 新增 Store 在此添加字段
|
||||
Account *postgres.AccountStore
|
||||
Shop *postgres.ShopStore
|
||||
Role *postgres.RoleStore
|
||||
Permission *postgres.PermissionStore
|
||||
AccountRole *postgres.AccountRoleStore
|
||||
RolePermission *postgres.RolePermissionStore
|
||||
PersonalCustomer *postgres.PersonalCustomerStore
|
||||
PersonalCustomerPhone *postgres.PersonalCustomerPhoneStore
|
||||
Wallet *postgres.WalletStore
|
||||
CommissionWithdrawalRequest *postgres.CommissionWithdrawalRequestStore
|
||||
CommissionRecord *postgres.CommissionRecordStore
|
||||
WalletTransaction *postgres.WalletTransactionStore
|
||||
CommissionWithdrawalSetting *postgres.CommissionWithdrawalSettingStore
|
||||
Enterprise *postgres.EnterpriseStore
|
||||
EnterpriseCardAuthorization *postgres.EnterpriseCardAuthorizationStore
|
||||
}
|
||||
|
||||
// initStores 初始化所有 Store 实例
|
||||
func initStores(deps *Dependencies) *stores {
|
||||
return &stores{
|
||||
Account: postgres.NewAccountStore(deps.DB, deps.Redis),
|
||||
Shop: postgres.NewShopStore(deps.DB, deps.Redis),
|
||||
Role: postgres.NewRoleStore(deps.DB),
|
||||
Permission: postgres.NewPermissionStore(deps.DB),
|
||||
AccountRole: postgres.NewAccountRoleStore(deps.DB, deps.Redis),
|
||||
RolePermission: postgres.NewRolePermissionStore(deps.DB, deps.Redis),
|
||||
PersonalCustomer: postgres.NewPersonalCustomerStore(deps.DB, deps.Redis),
|
||||
PersonalCustomerPhone: postgres.NewPersonalCustomerPhoneStore(deps.DB),
|
||||
// TODO: 新增 Store 在此初始化
|
||||
Account: postgres.NewAccountStore(deps.DB, deps.Redis),
|
||||
Shop: postgres.NewShopStore(deps.DB, deps.Redis),
|
||||
Role: postgres.NewRoleStore(deps.DB),
|
||||
Permission: postgres.NewPermissionStore(deps.DB),
|
||||
AccountRole: postgres.NewAccountRoleStore(deps.DB, deps.Redis),
|
||||
RolePermission: postgres.NewRolePermissionStore(deps.DB, deps.Redis),
|
||||
PersonalCustomer: postgres.NewPersonalCustomerStore(deps.DB, deps.Redis),
|
||||
PersonalCustomerPhone: postgres.NewPersonalCustomerPhoneStore(deps.DB),
|
||||
Wallet: postgres.NewWalletStore(deps.DB, deps.Redis),
|
||||
CommissionWithdrawalRequest: postgres.NewCommissionWithdrawalRequestStore(deps.DB, deps.Redis),
|
||||
CommissionRecord: postgres.NewCommissionRecordStore(deps.DB, deps.Redis),
|
||||
WalletTransaction: postgres.NewWalletTransactionStore(deps.DB, deps.Redis),
|
||||
CommissionWithdrawalSetting: postgres.NewCommissionWithdrawalSettingStore(deps.DB, deps.Redis),
|
||||
Enterprise: postgres.NewEnterpriseStore(deps.DB, deps.Redis),
|
||||
EnterpriseCardAuthorization: postgres.NewEnterpriseCardAuthorizationStore(deps.DB, deps.Redis),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,17 +8,22 @@ import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
// Handlers 封装所有 HTTP 处理器
|
||||
// 用于路由注册
|
||||
type Handlers struct {
|
||||
Account *admin.AccountHandler
|
||||
Role *admin.RoleHandler
|
||||
Permission *admin.PermissionHandler
|
||||
PersonalCustomer *app.PersonalCustomerHandler
|
||||
Shop *admin.ShopHandler
|
||||
ShopAccount *admin.ShopAccountHandler
|
||||
AdminAuth *admin.AuthHandler
|
||||
H5Auth *h5.AuthHandler
|
||||
Account *admin.AccountHandler
|
||||
Role *admin.RoleHandler
|
||||
Permission *admin.PermissionHandler
|
||||
PersonalCustomer *app.PersonalCustomerHandler
|
||||
Shop *admin.ShopHandler
|
||||
ShopAccount *admin.ShopAccountHandler
|
||||
AdminAuth *admin.AuthHandler
|
||||
H5Auth *h5.AuthHandler
|
||||
ShopCommission *admin.ShopCommissionHandler
|
||||
CommissionWithdrawal *admin.CommissionWithdrawalHandler
|
||||
CommissionWithdrawalSetting *admin.CommissionWithdrawalSettingHandler
|
||||
Enterprise *admin.EnterpriseHandler
|
||||
EnterpriseCard *admin.EnterpriseCardHandler
|
||||
CustomerAccount *admin.CustomerAccountHandler
|
||||
MyCommission *admin.MyCommissionHandler
|
||||
}
|
||||
|
||||
// Middlewares 封装所有中间件
|
||||
|
||||
72
internal/handler/admin/commission_withdrawal.go
Normal file
72
internal/handler/admin/commission_withdrawal.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
commissionWithdrawalService "github.com/break/junhong_cmp_fiber/internal/service/commission_withdrawal"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/response"
|
||||
)
|
||||
|
||||
type CommissionWithdrawalHandler struct {
|
||||
service *commissionWithdrawalService.Service
|
||||
}
|
||||
|
||||
func NewCommissionWithdrawalHandler(service *commissionWithdrawalService.Service) *CommissionWithdrawalHandler {
|
||||
return &CommissionWithdrawalHandler{service: service}
|
||||
}
|
||||
|
||||
func (h *CommissionWithdrawalHandler) ListWithdrawalRequests(c *fiber.Ctx) error {
|
||||
var req model.WithdrawalRequestListReq
|
||||
if err := c.QueryParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
result, err := h.service.ListWithdrawalRequests(c.UserContext(), &req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.SuccessWithPagination(c, result.Items, result.Total, result.Page, result.Size)
|
||||
}
|
||||
|
||||
func (h *CommissionWithdrawalHandler) ApproveWithdrawal(c *fiber.Ctx) error {
|
||||
id, err := strconv.ParseUint(c.Params("id"), 10, 64)
|
||||
if err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "无效的提现申请ID")
|
||||
}
|
||||
|
||||
var req model.ApproveWithdrawalReq
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
result, err := h.service.Approve(c.UserContext(), uint(id), &req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.Success(c, result)
|
||||
}
|
||||
|
||||
func (h *CommissionWithdrawalHandler) RejectWithdrawal(c *fiber.Ctx) error {
|
||||
id, err := strconv.ParseUint(c.Params("id"), 10, 64)
|
||||
if err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "无效的提现申请ID")
|
||||
}
|
||||
|
||||
var req model.RejectWithdrawalReq
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
result, err := h.service.Reject(c.UserContext(), uint(id), &req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.Success(c, result)
|
||||
}
|
||||
55
internal/handler/admin/commission_withdrawal_setting.go
Normal file
55
internal/handler/admin/commission_withdrawal_setting.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
commissionWithdrawalSettingService "github.com/break/junhong_cmp_fiber/internal/service/commission_withdrawal_setting"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/response"
|
||||
)
|
||||
|
||||
type CommissionWithdrawalSettingHandler struct {
|
||||
service *commissionWithdrawalSettingService.Service
|
||||
}
|
||||
|
||||
func NewCommissionWithdrawalSettingHandler(service *commissionWithdrawalSettingService.Service) *CommissionWithdrawalSettingHandler {
|
||||
return &CommissionWithdrawalSettingHandler{service: service}
|
||||
}
|
||||
|
||||
func (h *CommissionWithdrawalSettingHandler) Create(c *fiber.Ctx) error {
|
||||
var req model.CreateWithdrawalSettingReq
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
result, err := h.service.Create(c.UserContext(), &req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.Success(c, result)
|
||||
}
|
||||
|
||||
func (h *CommissionWithdrawalSettingHandler) List(c *fiber.Ctx) error {
|
||||
var req model.WithdrawalSettingListReq
|
||||
if err := c.QueryParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
result, err := h.service.List(c.UserContext(), &req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.SuccessWithPagination(c, result.Items, result.Total, result.Page, result.Size)
|
||||
}
|
||||
|
||||
func (h *CommissionWithdrawalSettingHandler) GetCurrent(c *fiber.Ctx) error {
|
||||
result, err := h.service.GetCurrent(c.UserContext())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.Success(c, result)
|
||||
}
|
||||
106
internal/handler/admin/customer_account.go
Normal file
106
internal/handler/admin/customer_account.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
customerAccountService "github.com/break/junhong_cmp_fiber/internal/service/customer_account"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/response"
|
||||
)
|
||||
|
||||
type CustomerAccountHandler struct {
|
||||
service *customerAccountService.Service
|
||||
}
|
||||
|
||||
func NewCustomerAccountHandler(service *customerAccountService.Service) *CustomerAccountHandler {
|
||||
return &CustomerAccountHandler{service: service}
|
||||
}
|
||||
|
||||
func (h *CustomerAccountHandler) List(c *fiber.Ctx) error {
|
||||
var req model.CustomerAccountListReq
|
||||
if err := c.QueryParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
result, err := h.service.List(c.UserContext(), &req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.SuccessWithPagination(c, result.Items, result.Total, result.Page, result.Size)
|
||||
}
|
||||
|
||||
func (h *CustomerAccountHandler) Create(c *fiber.Ctx) error {
|
||||
var req model.CreateCustomerAccountReq
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
result, err := h.service.Create(c.UserContext(), &req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.Success(c, result)
|
||||
}
|
||||
|
||||
func (h *CustomerAccountHandler) Update(c *fiber.Ctx) error {
|
||||
idStr := c.Params("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "无效的账号ID")
|
||||
}
|
||||
|
||||
var req model.UpdateCustomerAccountReq
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
result, err := h.service.Update(c.UserContext(), uint(id), &req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.Success(c, result)
|
||||
}
|
||||
|
||||
func (h *CustomerAccountHandler) UpdatePassword(c *fiber.Ctx) error {
|
||||
idStr := c.Params("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "无效的账号ID")
|
||||
}
|
||||
|
||||
var req model.UpdateCustomerAccountPasswordReq
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
if err := h.service.UpdatePassword(c.UserContext(), uint(id), req.Password); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.Success(c, nil)
|
||||
}
|
||||
|
||||
func (h *CustomerAccountHandler) UpdateStatus(c *fiber.Ctx) error {
|
||||
idStr := c.Params("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "无效的账号ID")
|
||||
}
|
||||
|
||||
var req model.UpdateCustomerAccountStatusReq
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
if err := h.service.UpdateStatus(c.UserContext(), uint(id), req.Status); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.Success(c, nil)
|
||||
}
|
||||
106
internal/handler/admin/enterprise.go
Normal file
106
internal/handler/admin/enterprise.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
enterpriseService "github.com/break/junhong_cmp_fiber/internal/service/enterprise"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/response"
|
||||
)
|
||||
|
||||
type EnterpriseHandler struct {
|
||||
service *enterpriseService.Service
|
||||
}
|
||||
|
||||
func NewEnterpriseHandler(service *enterpriseService.Service) *EnterpriseHandler {
|
||||
return &EnterpriseHandler{service: service}
|
||||
}
|
||||
|
||||
func (h *EnterpriseHandler) Create(c *fiber.Ctx) error {
|
||||
var req model.CreateEnterpriseReq
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
result, err := h.service.Create(c.UserContext(), &req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.Success(c, result)
|
||||
}
|
||||
|
||||
func (h *EnterpriseHandler) List(c *fiber.Ctx) error {
|
||||
var req model.EnterpriseListReq
|
||||
if err := c.QueryParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
result, err := h.service.List(c.UserContext(), &req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.SuccessWithPagination(c, result.Items, result.Total, result.Page, result.Size)
|
||||
}
|
||||
|
||||
func (h *EnterpriseHandler) Update(c *fiber.Ctx) error {
|
||||
idStr := c.Params("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "无效的企业ID")
|
||||
}
|
||||
|
||||
var req model.UpdateEnterpriseReq
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
result, err := h.service.Update(c.UserContext(), uint(id), &req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.Success(c, result)
|
||||
}
|
||||
|
||||
func (h *EnterpriseHandler) UpdateStatus(c *fiber.Ctx) error {
|
||||
idStr := c.Params("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "无效的企业ID")
|
||||
}
|
||||
|
||||
var req model.UpdateEnterpriseStatusReq
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
if err := h.service.UpdateStatus(c.UserContext(), uint(id), req.Status); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.Success(c, nil)
|
||||
}
|
||||
|
||||
func (h *EnterpriseHandler) UpdatePassword(c *fiber.Ctx) error {
|
||||
idStr := c.Params("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "无效的企业ID")
|
||||
}
|
||||
|
||||
var req model.UpdateEnterprisePasswordReq
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
if err := h.service.UpdatePassword(c.UserContext(), uint(id), req.Password); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.Success(c, nil)
|
||||
}
|
||||
140
internal/handler/admin/enterprise_card.go
Normal file
140
internal/handler/admin/enterprise_card.go
Normal file
@@ -0,0 +1,140 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
enterpriseCardService "github.com/break/junhong_cmp_fiber/internal/service/enterprise_card"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/response"
|
||||
)
|
||||
|
||||
type EnterpriseCardHandler struct {
|
||||
service *enterpriseCardService.Service
|
||||
}
|
||||
|
||||
func NewEnterpriseCardHandler(service *enterpriseCardService.Service) *EnterpriseCardHandler {
|
||||
return &EnterpriseCardHandler{service: service}
|
||||
}
|
||||
|
||||
func (h *EnterpriseCardHandler) AllocateCardsPreview(c *fiber.Ctx) error {
|
||||
idStr := c.Params("id")
|
||||
enterpriseID, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "无效的企业ID")
|
||||
}
|
||||
|
||||
var req model.AllocateCardsPreviewReq
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
result, err := h.service.AllocateCardsPreview(c.UserContext(), uint(enterpriseID), &req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.Success(c, result)
|
||||
}
|
||||
|
||||
func (h *EnterpriseCardHandler) AllocateCards(c *fiber.Ctx) error {
|
||||
idStr := c.Params("id")
|
||||
enterpriseID, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "无效的企业ID")
|
||||
}
|
||||
|
||||
var req model.AllocateCardsReq
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
result, err := h.service.AllocateCards(c.UserContext(), uint(enterpriseID), &req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.Success(c, result)
|
||||
}
|
||||
|
||||
func (h *EnterpriseCardHandler) RecallCards(c *fiber.Ctx) error {
|
||||
idStr := c.Params("id")
|
||||
enterpriseID, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "无效的企业ID")
|
||||
}
|
||||
|
||||
var req model.RecallCardsReq
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
result, err := h.service.RecallCards(c.UserContext(), uint(enterpriseID), &req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.Success(c, result)
|
||||
}
|
||||
|
||||
func (h *EnterpriseCardHandler) ListCards(c *fiber.Ctx) error {
|
||||
idStr := c.Params("id")
|
||||
enterpriseID, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "无效的企业ID")
|
||||
}
|
||||
|
||||
var req model.EnterpriseCardListReq
|
||||
if err := c.QueryParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
result, err := h.service.ListCards(c.UserContext(), uint(enterpriseID), &req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.SuccessWithPagination(c, result.Items, result.Total, result.Page, result.Size)
|
||||
}
|
||||
|
||||
func (h *EnterpriseCardHandler) SuspendCard(c *fiber.Ctx) error {
|
||||
enterpriseIDStr := c.Params("id")
|
||||
enterpriseID, err := strconv.ParseUint(enterpriseIDStr, 10, 64)
|
||||
if err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "无效的企业ID")
|
||||
}
|
||||
|
||||
cardIDStr := c.Params("card_id")
|
||||
cardID, err := strconv.ParseUint(cardIDStr, 10, 64)
|
||||
if err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "无效的卡ID")
|
||||
}
|
||||
|
||||
if err := h.service.SuspendCard(c.UserContext(), uint(enterpriseID), uint(cardID)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.Success(c, nil)
|
||||
}
|
||||
|
||||
func (h *EnterpriseCardHandler) ResumeCard(c *fiber.Ctx) error {
|
||||
enterpriseIDStr := c.Params("id")
|
||||
enterpriseID, err := strconv.ParseUint(enterpriseIDStr, 10, 64)
|
||||
if err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "无效的企业ID")
|
||||
}
|
||||
|
||||
cardIDStr := c.Params("card_id")
|
||||
cardID, err := strconv.ParseUint(cardIDStr, 10, 64)
|
||||
if err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "无效的卡ID")
|
||||
}
|
||||
|
||||
if err := h.service.ResumeCard(c.UserContext(), uint(enterpriseID), uint(cardID)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.Success(c, nil)
|
||||
}
|
||||
69
internal/handler/admin/my_commission.go
Normal file
69
internal/handler/admin/my_commission.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
myCommissionService "github.com/break/junhong_cmp_fiber/internal/service/my_commission"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/response"
|
||||
)
|
||||
|
||||
type MyCommissionHandler struct {
|
||||
service *myCommissionService.Service
|
||||
}
|
||||
|
||||
func NewMyCommissionHandler(service *myCommissionService.Service) *MyCommissionHandler {
|
||||
return &MyCommissionHandler{service: service}
|
||||
}
|
||||
|
||||
func (h *MyCommissionHandler) GetSummary(c *fiber.Ctx) error {
|
||||
result, err := h.service.GetCommissionSummary(c.UserContext())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.Success(c, result)
|
||||
}
|
||||
|
||||
func (h *MyCommissionHandler) CreateWithdrawal(c *fiber.Ctx) error {
|
||||
var req model.CreateMyWithdrawalReq
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
result, err := h.service.CreateWithdrawalRequest(c.UserContext(), &req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.Success(c, result)
|
||||
}
|
||||
|
||||
func (h *MyCommissionHandler) ListWithdrawals(c *fiber.Ctx) error {
|
||||
var req model.MyWithdrawalListReq
|
||||
if err := c.QueryParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
result, err := h.service.ListMyWithdrawalRequests(c.UserContext(), &req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.SuccessWithPagination(c, result.Items, result.Total, result.Page, result.Size)
|
||||
}
|
||||
|
||||
func (h *MyCommissionHandler) ListRecords(c *fiber.Ctx) error {
|
||||
var req model.MyCommissionRecordListReq
|
||||
if err := c.QueryParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
result, err := h.service.ListMyCommissionRecords(c.UserContext(), &req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.SuccessWithPagination(c, result.Items, result.Total, result.Page, result.Size)
|
||||
}
|
||||
72
internal/handler/admin/shop_commission.go
Normal file
72
internal/handler/admin/shop_commission.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
shopCommissionService "github.com/break/junhong_cmp_fiber/internal/service/shop_commission"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/response"
|
||||
)
|
||||
|
||||
type ShopCommissionHandler struct {
|
||||
service *shopCommissionService.Service
|
||||
}
|
||||
|
||||
func NewShopCommissionHandler(service *shopCommissionService.Service) *ShopCommissionHandler {
|
||||
return &ShopCommissionHandler{service: service}
|
||||
}
|
||||
|
||||
func (h *ShopCommissionHandler) ListCommissionSummary(c *fiber.Ctx) error {
|
||||
var req model.ShopCommissionSummaryListReq
|
||||
if err := c.QueryParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
result, err := h.service.ListShopCommissionSummary(c.UserContext(), &req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.SuccessWithPagination(c, result.Items, result.Total, result.Page, result.Size)
|
||||
}
|
||||
|
||||
func (h *ShopCommissionHandler) ListWithdrawalRequests(c *fiber.Ctx) error {
|
||||
shopID, err := strconv.ParseUint(c.Params("shop_id"), 10, 64)
|
||||
if err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "无效的店铺 ID")
|
||||
}
|
||||
|
||||
var req model.ShopWithdrawalRequestListReq
|
||||
if err := c.QueryParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
result, err := h.service.ListShopWithdrawalRequests(c.UserContext(), uint(shopID), &req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.SuccessWithPagination(c, result.Items, result.Total, result.Page, result.Size)
|
||||
}
|
||||
|
||||
func (h *ShopCommissionHandler) ListCommissionRecords(c *fiber.Ctx) error {
|
||||
shopID, err := strconv.ParseUint(c.Params("shop_id"), 10, 64)
|
||||
if err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "无效的店铺 ID")
|
||||
}
|
||||
|
||||
var req model.ShopCommissionRecordListReq
|
||||
if err := c.QueryParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
result, err := h.service.ListShopCommissionRecords(c.UserContext(), uint(shopID), &req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.SuccessWithPagination(c, result.Items, result.Total, result.Page, result.Size)
|
||||
}
|
||||
@@ -10,10 +10,11 @@ type Account struct {
|
||||
BaseModel `gorm:"embedded"`
|
||||
Username string `gorm:"column:username;type:varchar(50);uniqueIndex:idx_account_username,where:deleted_at IS NULL;not null;comment:用户名" json:"username"`
|
||||
Phone string `gorm:"column:phone;type:varchar(20);uniqueIndex:idx_account_phone,where:deleted_at IS NULL;not null;comment:手机号" json:"phone"`
|
||||
Password string `gorm:"column:password;type:varchar(255);not null;comment:密码" json:"-"` // 不返回给客户端
|
||||
Password string `gorm:"column:password;type:varchar(255);not null;comment:密码" json:"-"`
|
||||
UserType int `gorm:"column:user_type;type:int;not null;index;comment:用户类型 1=超级管理员 2=平台用户 3=代理账号 4=企业账号" json:"user_type"`
|
||||
ShopID *uint `gorm:"column:shop_id;index;comment:店铺ID(代理账号必填)" json:"shop_id,omitempty"`
|
||||
EnterpriseID *uint `gorm:"column:enterprise_id;index;comment:企业ID(企业账号必填)" json:"enterprise_id,omitempty"`
|
||||
IsPrimary bool `gorm:"column:is_primary;type:boolean;default:false;comment:是否为店铺主账号(默认 false)" json:"is_primary"`
|
||||
Status int `gorm:"column:status;type:int;not null;default:1;comment:状态 0=禁用 1=启用" json:"status"`
|
||||
}
|
||||
|
||||
|
||||
30
internal/model/asset_allocation_record.go
Normal file
30
internal/model/asset_allocation_record.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// AssetAllocationRecord 资产分配记录模型
|
||||
// 记录卡/设备在平台和代理商之间的流转历史
|
||||
type AssetAllocationRecord struct {
|
||||
gorm.Model
|
||||
BaseModel `gorm:"embedded"`
|
||||
AllocationNo string `gorm:"column:allocation_no;type:varchar(50);uniqueIndex:uk_asset_allocation_no,where:deleted_at IS NULL;not null;comment:分配单号(唯一)" json:"allocation_no"`
|
||||
AllocationType string `gorm:"column:allocation_type;type:varchar(20);index;not null;comment:分配类型 allocate=分配 recall=回收" json:"allocation_type"`
|
||||
AssetType string `gorm:"column:asset_type;type:varchar(20);index;not null;comment:资产类型 iot_card=物联网卡 device=设备" json:"asset_type"`
|
||||
AssetID uint `gorm:"column:asset_id;index;not null;comment:资产ID" json:"asset_id"`
|
||||
AssetIdentifier string `gorm:"column:asset_identifier;type:varchar(50);not null;comment:资产标识符(ICCID或设备号)" json:"asset_identifier"`
|
||||
FromOwnerType string `gorm:"column:from_owner_type;type:varchar(20);comment:来源所有者类型" json:"from_owner_type"`
|
||||
FromOwnerID *uint `gorm:"column:from_owner_id;comment:来源所有者ID" json:"from_owner_id,omitempty"`
|
||||
ToOwnerType string `gorm:"column:to_owner_type;type:varchar(20);not null;comment:目标所有者类型" json:"to_owner_type"`
|
||||
ToOwnerID uint `gorm:"column:to_owner_id;not null;comment:目标所有者ID" json:"to_owner_id"`
|
||||
RelatedDeviceID *uint `gorm:"column:related_device_id;comment:关联设备ID" json:"related_device_id,omitempty"`
|
||||
RelatedCardIDs datatypes.JSON `gorm:"column:related_card_ids;type:jsonb;comment:关联卡ID列表" json:"related_card_ids,omitempty"`
|
||||
OperatorID uint `gorm:"column:operator_id;not null;comment:操作人ID" json:"operator_id"`
|
||||
Remark string `gorm:"column:remark;type:text;comment:备注" json:"remark"`
|
||||
}
|
||||
|
||||
func (AssetAllocationRecord) TableName() string {
|
||||
return "tb_asset_allocation_record"
|
||||
}
|
||||
@@ -91,10 +91,12 @@ type CommissionRecord struct {
|
||||
gorm.Model
|
||||
BaseModel `gorm:"embedded"`
|
||||
AgentID uint `gorm:"column:agent_id;index;not null;comment:代理用户ID" json:"agent_id"`
|
||||
ShopID uint `gorm:"column:shop_id;index;comment:店铺ID(佣金主要跟着店铺走)" json:"shop_id"`
|
||||
OrderID uint `gorm:"column:order_id;index;not null;comment:订单ID" json:"order_id"`
|
||||
RuleID uint `gorm:"column:rule_id;index;not null;comment:分佣规则ID" json:"rule_id"`
|
||||
CommissionType string `gorm:"column:commission_type;type:varchar(50);not null;comment:分佣类型 one_time-一次性 long_term-长期" json:"commission_type"`
|
||||
Amount int64 `gorm:"column:amount;type:bigint;not null;comment:分佣金额(分为单位)" json:"amount"`
|
||||
BalanceAfter int64 `gorm:"column:balance_after;type:bigint;default:0;comment:入账后佣金余额(分)" json:"balance_after"`
|
||||
Status int `gorm:"column:status;type:int;default:1;not null;comment:状态 1-已冻结 2-解冻中 3-已发放 4-已失效" json:"status"`
|
||||
UnfrozenAt *time.Time `gorm:"column:unfrozen_at;comment:解冻时间" json:"unfrozen_at"`
|
||||
ReleasedAt *time.Time `gorm:"column:released_at;comment:发放时间" json:"released_at"`
|
||||
|
||||
72
internal/model/commission_withdrawal_dto.go
Normal file
72
internal/model/commission_withdrawal_dto.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package model
|
||||
|
||||
// WithdrawalRequestListReq 提现申请列表查询请求
|
||||
type WithdrawalRequestListReq struct {
|
||||
Page int `json:"page" query:"page" validate:"omitempty,min=1" minimum:"1" description:"页码(默认1)"`
|
||||
PageSize int `json:"page_size" query:"page_size" validate:"omitempty,min=1,max=100" minimum:"1" maximum:"100" description:"每页数量(默认20,最大100)"`
|
||||
Status *int `json:"status" query:"status" validate:"omitempty,min=1,max=4" minimum:"1" maximum:"4" description:"状态 (1:待审核, 2:已通过, 3:已拒绝, 4:已到账)"`
|
||||
WithdrawalNo string `json:"withdrawal_no" query:"withdrawal_no" validate:"omitempty,max=50" maxLength:"50" description:"提现单号(精确查询)"`
|
||||
ShopName string `json:"shop_name" query:"shop_name" validate:"omitempty,max=100" maxLength:"100" description:"店铺名称(模糊查询)"`
|
||||
StartTime string `json:"start_time" query:"start_time" validate:"omitempty" description:"申请开始时间(格式:2006-01-02 15:04:05)"`
|
||||
EndTime string `json:"end_time" query:"end_time" validate:"omitempty" description:"申请结束时间(格式:2006-01-02 15:04:05)"`
|
||||
}
|
||||
|
||||
// WithdrawalRequestItem 提现申请列表项
|
||||
type WithdrawalRequestItem struct {
|
||||
ID uint `json:"id" description:"提现申请ID"`
|
||||
WithdrawalNo string `json:"withdrawal_no" description:"提现单号"`
|
||||
Amount int64 `json:"amount" description:"提现金额(分)"`
|
||||
FeeRate int64 `json:"fee_rate" description:"手续费比率(基点,100=1%)"`
|
||||
Fee int64 `json:"fee" description:"手续费(分)"`
|
||||
ActualAmount int64 `json:"actual_amount" description:"实际到账金额(分)"`
|
||||
Status int `json:"status" description:"状态 (1:待审核, 2:已通过, 3:已拒绝, 4:已到账)"`
|
||||
StatusName string `json:"status_name" description:"状态名称"`
|
||||
ShopID uint `json:"shop_id" description:"店铺ID"`
|
||||
ShopName string `json:"shop_name" description:"店铺名称"`
|
||||
ShopHierarchy string `json:"shop_hierarchy" description:"店铺层级路径"`
|
||||
ApplicantID uint `json:"applicant_id" description:"申请人账号ID"`
|
||||
ApplicantName string `json:"applicant_name" description:"申请人用户名"`
|
||||
ProcessorID *uint `json:"processor_id,omitempty" description:"处理人账号ID"`
|
||||
ProcessorName string `json:"processor_name,omitempty" description:"处理人用户名"`
|
||||
WithdrawalMethod string `json:"withdrawal_method" description:"提现方式 (alipay:支付宝, wechat:微信, bank:银行卡)"`
|
||||
PaymentType string `json:"payment_type" description:"放款类型 (manual:人工打款)"`
|
||||
AccountName string `json:"account_name" description:"收款账户名称"`
|
||||
AccountNumber string `json:"account_number" description:"收款账号"`
|
||||
BankName string `json:"bank_name,omitempty" description:"银行名称"`
|
||||
RejectReason string `json:"reject_reason,omitempty" description:"拒绝原因"`
|
||||
Remark string `json:"remark,omitempty" description:"备注"`
|
||||
CreatedAt string `json:"created_at" description:"申请时间"`
|
||||
ProcessedAt string `json:"processed_at,omitempty" description:"处理时间"`
|
||||
}
|
||||
|
||||
// WithdrawalRequestPageResult 提现申请列表分页响应
|
||||
type WithdrawalRequestPageResult struct {
|
||||
Items []WithdrawalRequestItem `json:"items" description:"提现申请列表"`
|
||||
Total int64 `json:"total" description:"总记录数"`
|
||||
Page int `json:"page" description:"当前页码"`
|
||||
Size int `json:"size" description:"每页数量"`
|
||||
}
|
||||
|
||||
// ApproveWithdrawalReq 审批通过提现申请请求
|
||||
type ApproveWithdrawalReq struct {
|
||||
PaymentType string `json:"payment_type" validate:"required,oneof=manual" required:"true" description:"放款类型(目前只支持manual人工打款)"`
|
||||
Amount *int64 `json:"amount" validate:"omitempty,min=1" minimum:"1" description:"修正后的提现金额(分),不填则使用原金额"`
|
||||
WithdrawalMethod *string `json:"withdrawal_method" validate:"omitempty,oneof=alipay wechat bank" description:"修正后的收款类型 (alipay:支付宝, wechat:微信, bank:银行卡)"`
|
||||
AccountName *string `json:"account_name" validate:"omitempty,max=100" maxLength:"100" description:"修正后的收款人姓名"`
|
||||
AccountNumber *string `json:"account_number" validate:"omitempty,max=100" maxLength:"100" description:"修正后的收款账号"`
|
||||
Remark string `json:"remark" validate:"omitempty,max=500" maxLength:"500" description:"备注"`
|
||||
}
|
||||
|
||||
// RejectWithdrawalReq 拒绝提现申请请求
|
||||
type RejectWithdrawalReq struct {
|
||||
Remark string `json:"remark" validate:"required,max=500" required:"true" maxLength:"500" description:"拒绝原因(必填)"`
|
||||
}
|
||||
|
||||
// WithdrawalApprovalResp 审批响应
|
||||
type WithdrawalApprovalResp struct {
|
||||
ID uint `json:"id" description:"提现申请ID"`
|
||||
WithdrawalNo string `json:"withdrawal_no" description:"提现单号"`
|
||||
Status int `json:"status" description:"状态 (1:待审核, 2:已通过, 3:已拒绝, 4:已到账)"`
|
||||
StatusName string `json:"status_name" description:"状态名称"`
|
||||
ProcessedAt string `json:"processed_at" description:"处理时间"`
|
||||
}
|
||||
31
internal/model/commission_withdrawal_setting_dto.go
Normal file
31
internal/model/commission_withdrawal_setting_dto.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package model
|
||||
|
||||
type CreateWithdrawalSettingReq struct {
|
||||
DailyWithdrawalLimit int `json:"daily_withdrawal_limit" validate:"required,min=1,max=100" required:"true" minimum:"1" maximum:"100" description:"每日提现次数限制"`
|
||||
MinWithdrawalAmount int64 `json:"min_withdrawal_amount" validate:"required,min=1" required:"true" minimum:"1" description:"最低提现金额(分)"`
|
||||
FeeRate int64 `json:"fee_rate" validate:"required,min=0,max=10000" required:"true" minimum:"0" maximum:"10000" description:"手续费比率(基点,100=1%)"`
|
||||
}
|
||||
|
||||
type WithdrawalSettingListReq struct {
|
||||
Page int `json:"page" query:"page" validate:"omitempty,min=1" minimum:"1" description:"页码(默认1)"`
|
||||
PageSize int `json:"page_size" query:"page_size" validate:"omitempty,min=1,max=100" minimum:"1" maximum:"100" description:"每页数量(默认20,最大100)"`
|
||||
}
|
||||
|
||||
type WithdrawalSettingItem struct {
|
||||
ID uint `json:"id" description:"配置ID"`
|
||||
DailyWithdrawalLimit int `json:"daily_withdrawal_limit" description:"每日提现次数限制"`
|
||||
MinWithdrawalAmount int64 `json:"min_withdrawal_amount" description:"最低提现金额(分)"`
|
||||
FeeRate int64 `json:"fee_rate" description:"手续费比率(基点,100=1%)"`
|
||||
ArrivalDays int `json:"arrival_days" description:"到账天数"`
|
||||
IsActive bool `json:"is_active" description:"是否生效"`
|
||||
CreatorID uint `json:"creator_id" description:"创建人ID"`
|
||||
CreatorName string `json:"creator_name" description:"创建人用户名"`
|
||||
CreatedAt string `json:"created_at" description:"创建时间"`
|
||||
}
|
||||
|
||||
type WithdrawalSettingPageResult struct {
|
||||
Items []WithdrawalSettingItem `json:"items" description:"配置列表"`
|
||||
Total int64 `json:"total" description:"总记录数"`
|
||||
Page int `json:"page" description:"当前页码"`
|
||||
Size int `json:"size" description:"每页数量"`
|
||||
}
|
||||
54
internal/model/customer_account_dto.go
Normal file
54
internal/model/customer_account_dto.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package model
|
||||
|
||||
type CustomerAccountListReq struct {
|
||||
Page int `json:"page" query:"page" validate:"omitempty,min=1" minimum:"1" description:"页码"`
|
||||
PageSize int `json:"page_size" query:"page_size" validate:"omitempty,min=1,max=100" minimum:"1" maximum:"100" description:"每页数量"`
|
||||
Username string `json:"username" query:"username" description:"用户名(模糊查询)"`
|
||||
Phone string `json:"phone" query:"phone" description:"手机号(模糊查询)"`
|
||||
UserType *int `json:"user_type" query:"user_type" description:"用户类型(3=代理账号, 4=企业账号)"`
|
||||
ShopID *uint `json:"shop_id" query:"shop_id" description:"店铺ID"`
|
||||
EnterpriseID *uint `json:"enterprise_id" query:"enterprise_id" description:"企业ID"`
|
||||
Status *int `json:"status" query:"status" description:"状态(0=禁用, 1=启用)"`
|
||||
}
|
||||
|
||||
type CustomerAccountItem struct {
|
||||
ID uint `json:"id" description:"账号ID"`
|
||||
Username string `json:"username" description:"用户名"`
|
||||
Phone string `json:"phone" description:"手机号"`
|
||||
UserType int `json:"user_type" description:"用户类型(3=代理账号, 4=企业账号)"`
|
||||
UserTypeName string `json:"user_type_name" description:"用户类型名称"`
|
||||
ShopID *uint `json:"shop_id,omitempty" description:"店铺ID"`
|
||||
ShopName string `json:"shop_name" description:"店铺名称"`
|
||||
EnterpriseID *uint `json:"enterprise_id,omitempty" description:"企业ID"`
|
||||
EnterpriseName string `json:"enterprise_name" description:"企业名称"`
|
||||
Status int `json:"status" description:"状态(0=禁用, 1=启用)"`
|
||||
StatusName string `json:"status_name" description:"状态名称"`
|
||||
CreatedAt string `json:"created_at" description:"创建时间"`
|
||||
}
|
||||
|
||||
type CustomerAccountPageResult struct {
|
||||
Items []CustomerAccountItem `json:"items" description:"账号列表"`
|
||||
Total int64 `json:"total" description:"总记录数"`
|
||||
Page int `json:"page" description:"当前页码"`
|
||||
Size int `json:"size" description:"每页数量"`
|
||||
}
|
||||
|
||||
type CreateCustomerAccountReq struct {
|
||||
Username string `json:"username" validate:"required,min=2,max=50" required:"true" minimum:"2" maximum:"50" description:"用户名"`
|
||||
Phone string `json:"phone" validate:"required,len=11" required:"true" description:"手机号"`
|
||||
Password string `json:"password" validate:"required,min=6,max=20" required:"true" minimum:"6" maximum:"20" description:"密码"`
|
||||
ShopID uint `json:"shop_id" validate:"required" required:"true" description:"店铺ID"`
|
||||
}
|
||||
|
||||
type UpdateCustomerAccountReq struct {
|
||||
Username *string `json:"username" validate:"omitempty,min=2,max=50" minimum:"2" maximum:"50" description:"用户名"`
|
||||
Phone *string `json:"phone" validate:"omitempty,len=11" description:"手机号"`
|
||||
}
|
||||
|
||||
type UpdateCustomerAccountPasswordReq struct {
|
||||
Password string `json:"password" validate:"required,min=6,max=20" required:"true" minimum:"6" maximum:"20" description:"新密码"`
|
||||
}
|
||||
|
||||
type UpdateCustomerAccountStatusReq struct {
|
||||
Status int `json:"status" validate:"required,oneof=0 1" required:"true" enum:"0,1" description:"状态(0=禁用, 1=启用)"`
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
// Device 设备模型
|
||||
// 用户的物联网设备(如 GPS 追踪器、智能传感器)
|
||||
// 物联网设备(如 GPS 追踪器、智能传感器)
|
||||
// 可绑定 1-4 张 IoT 卡,主要用于批量管理和设备操作
|
||||
type Device struct {
|
||||
gorm.Model
|
||||
@@ -19,8 +19,9 @@ type Device struct {
|
||||
MaxSimSlots int `gorm:"column:max_sim_slots;type:int;default:4;comment:最大插槽数量(默认4)" json:"max_sim_slots"`
|
||||
Manufacturer string `gorm:"column:manufacturer;type:varchar(255);comment:制造商" json:"manufacturer"`
|
||||
BatchNo string `gorm:"column:batch_no;type:varchar(100);comment:批次号" json:"batch_no"`
|
||||
OwnerType string `gorm:"column:owner_type;type:varchar(20);default:'platform';not null;comment:所有者类型 platform-平台 agent-代理 user-用户" json:"owner_type"`
|
||||
OwnerType string `gorm:"column:owner_type;type:varchar(20);default:'platform';not null;comment:所有者类型 platform-平台 shop-店铺" json:"owner_type"`
|
||||
OwnerID uint `gorm:"column:owner_id;index;default:0;not null;comment:所有者ID" json:"owner_id"`
|
||||
ShopID *uint `gorm:"column:shop_id;index;comment:店铺ID(冗余字段,方便查询)" json:"shop_id,omitempty"`
|
||||
Status int `gorm:"column:status;type:int;default:1;not null;comment:状态 1-在库 2-已分销 3-已激活 4-已停用" json:"status"`
|
||||
ActivatedAt *time.Time `gorm:"column:activated_at;comment:激活时间" json:"activated_at"`
|
||||
DeviceUsername string `gorm:"column:device_username;type:varchar(100);comment:设备登录用户名" json:"device_username"`
|
||||
|
||||
24
internal/model/enterprise_card_authorization.go
Normal file
24
internal/model/enterprise_card_authorization.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// EnterpriseCardAuthorization 企业卡授权模型
|
||||
// 记录企业被授权可见的卡,卡的归属(owner)始终是代理商店铺
|
||||
type EnterpriseCardAuthorization struct {
|
||||
gorm.Model
|
||||
BaseModel `gorm:"embedded"`
|
||||
EnterpriseID uint `gorm:"column:enterprise_id;index;not null;comment:企业ID" json:"enterprise_id"`
|
||||
IotCardID uint `gorm:"column:iot_card_id;index;not null;comment:IoT卡ID" json:"iot_card_id"`
|
||||
ShopID uint `gorm:"column:shop_id;index;not null;comment:店铺ID(授权方)" json:"shop_id"`
|
||||
AuthorizedBy uint `gorm:"column:authorized_by;not null;comment:授权人ID" json:"authorized_by"`
|
||||
AuthorizedAt *time.Time `gorm:"column:authorized_at;default:now();comment:授权时间" json:"authorized_at"`
|
||||
Status int `gorm:"column:status;type:int;default:1;comment:状态 1=有效 0=已回收" json:"status"`
|
||||
}
|
||||
|
||||
func (EnterpriseCardAuthorization) TableName() string {
|
||||
return "tb_enterprise_card_authorization"
|
||||
}
|
||||
115
internal/model/enterprise_card_authorization_dto.go
Normal file
115
internal/model/enterprise_card_authorization_dto.go
Normal file
@@ -0,0 +1,115 @@
|
||||
package model
|
||||
|
||||
type AllocateCardsPreviewReq struct {
|
||||
ICCIDs []string `json:"iccids" validate:"required,min=1,max=1000,dive,required" required:"true" description:"需要授权的 ICCID 列表(最多1000个)"`
|
||||
}
|
||||
|
||||
type StandaloneCard struct {
|
||||
ICCID string `json:"iccid" description:"ICCID"`
|
||||
IotCardID uint `json:"iot_card_id" description:"卡ID"`
|
||||
MSISDN string `json:"msisdn" description:"手机号"`
|
||||
CarrierID uint `json:"carrier_id" description:"运营商ID"`
|
||||
StatusName string `json:"status_name" description:"状态名称"`
|
||||
}
|
||||
|
||||
type DeviceBundle struct {
|
||||
DeviceID uint `json:"device_id" description:"设备ID"`
|
||||
DeviceNo string `json:"device_no" description:"设备号"`
|
||||
TriggerCard DeviceBundleCard `json:"trigger_card" description:"触发卡(用户选择的卡)"`
|
||||
BundleCards []DeviceBundleCard `json:"bundle_cards" description:"连带卡(同设备的其他卡)"`
|
||||
}
|
||||
|
||||
type DeviceBundleCard struct {
|
||||
ICCID string `json:"iccid" description:"ICCID"`
|
||||
IotCardID uint `json:"iot_card_id" description:"卡ID"`
|
||||
MSISDN string `json:"msisdn" description:"手机号"`
|
||||
}
|
||||
|
||||
type FailedItem struct {
|
||||
ICCID string `json:"iccid" description:"ICCID"`
|
||||
Reason string `json:"reason" description:"失败原因"`
|
||||
}
|
||||
|
||||
type AllocatePreviewSummary struct {
|
||||
StandaloneCardCount int `json:"standalone_card_count" description:"独立卡数量"`
|
||||
DeviceCount int `json:"device_count" description:"设备数量"`
|
||||
DeviceCardCount int `json:"device_card_count" description:"设备卡数量"`
|
||||
TotalCardCount int `json:"total_card_count" description:"总卡数量"`
|
||||
FailedCount int `json:"failed_count" description:"失败数量"`
|
||||
}
|
||||
|
||||
type AllocateCardsPreviewResp struct {
|
||||
StandaloneCards []StandaloneCard `json:"standalone_cards" description:"可直接授权的卡(未绑定设备)"`
|
||||
DeviceBundles []DeviceBundle `json:"device_bundles" description:"需要整体授权的设备包"`
|
||||
FailedItems []FailedItem `json:"failed_items" description:"失败的卡"`
|
||||
Summary AllocatePreviewSummary `json:"summary" description:"汇总信息"`
|
||||
}
|
||||
|
||||
type AllocateCardsReq struct {
|
||||
ICCIDs []string `json:"iccids" validate:"required,min=1,max=1000,dive,required" required:"true" description:"需要授权的 ICCID 列表"`
|
||||
ConfirmDeviceBundles bool `json:"confirm_device_bundles" description:"确认整体授权设备下所有卡"`
|
||||
}
|
||||
|
||||
type AllocatedDevice struct {
|
||||
DeviceID uint `json:"device_id" description:"设备ID"`
|
||||
DeviceNo string `json:"device_no" description:"设备号"`
|
||||
CardCount int `json:"card_count" description:"卡数量"`
|
||||
ICCIDs []string `json:"iccids" description:"卡ICCID列表"`
|
||||
}
|
||||
|
||||
type AllocateCardsResp struct {
|
||||
SuccessCount int `json:"success_count" description:"成功数量"`
|
||||
FailCount int `json:"fail_count" description:"失败数量"`
|
||||
FailedItems []FailedItem `json:"failed_items" description:"失败详情"`
|
||||
AllocatedDevices []AllocatedDevice `json:"allocated_devices" description:"连带授权的设备列表"`
|
||||
}
|
||||
|
||||
type RecallCardsReq struct {
|
||||
ICCIDs []string `json:"iccids" validate:"required,min=1,max=1000,dive,required" required:"true" description:"需要回收授权的 ICCID 列表"`
|
||||
}
|
||||
|
||||
type RecalledDevice struct {
|
||||
DeviceID uint `json:"device_id" description:"设备ID"`
|
||||
DeviceNo string `json:"device_no" description:"设备号"`
|
||||
CardCount int `json:"card_count" description:"卡数量"`
|
||||
ICCIDs []string `json:"iccids" description:"卡ICCID列表"`
|
||||
}
|
||||
|
||||
type RecallCardsResp struct {
|
||||
SuccessCount int `json:"success_count" description:"成功数量"`
|
||||
FailCount int `json:"fail_count" description:"失败数量"`
|
||||
FailedItems []FailedItem `json:"failed_items" description:"失败详情"`
|
||||
RecalledDevices []RecalledDevice `json:"recalled_devices" description:"连带回收的设备列表"`
|
||||
}
|
||||
|
||||
type EnterpriseCardListReq struct {
|
||||
Page int `json:"page" query:"page" validate:"omitempty,min=1" minimum:"1" description:"页码"`
|
||||
PageSize int `json:"page_size" query:"page_size" validate:"omitempty,min=1,max=100" minimum:"1" maximum:"100" description:"每页数量"`
|
||||
Status *int `json:"status" query:"status" description:"卡状态"`
|
||||
CarrierID *uint `json:"carrier_id" query:"carrier_id" description:"运营商ID"`
|
||||
ICCID string `json:"iccid" query:"iccid" description:"ICCID(模糊查询)"`
|
||||
DeviceNo string `json:"device_no" query:"device_no" description:"设备号(模糊查询)"`
|
||||
}
|
||||
|
||||
type EnterpriseCardItem struct {
|
||||
ID uint `json:"id" description:"卡ID"`
|
||||
ICCID string `json:"iccid" description:"ICCID"`
|
||||
MSISDN string `json:"msisdn" description:"手机号"`
|
||||
DeviceID *uint `json:"device_id,omitempty" description:"设备ID"`
|
||||
DeviceNo string `json:"device_no" description:"设备号"`
|
||||
CarrierID uint `json:"carrier_id" description:"运营商ID"`
|
||||
CarrierName string `json:"carrier_name" description:"运营商名称"`
|
||||
PackageID *uint `json:"package_id,omitempty" description:"套餐ID"`
|
||||
PackageName string `json:"package_name" description:"套餐名称"`
|
||||
Status int `json:"status" description:"状态"`
|
||||
StatusName string `json:"status_name" description:"状态名称"`
|
||||
NetworkStatus int `json:"network_status" description:"网络状态"`
|
||||
NetworkStatusName string `json:"network_status_name" description:"网络状态名称"`
|
||||
}
|
||||
|
||||
type EnterpriseCardPageResult struct {
|
||||
Items []EnterpriseCardItem `json:"items" description:"卡列表"`
|
||||
Total int64 `json:"total" description:"总记录数"`
|
||||
Page int `json:"page" description:"当前页码"`
|
||||
Size int `json:"size" description:"每页数量"`
|
||||
}
|
||||
@@ -1,49 +1,90 @@
|
||||
package model
|
||||
|
||||
// CreateEnterpriseRequest 创建企业请求
|
||||
type CreateEnterpriseRequest struct {
|
||||
EnterpriseName string `json:"enterprise_name" validate:"required" required:"true" description:"企业名称"`
|
||||
EnterpriseCode string `json:"enterprise_code" description:"企业编号"`
|
||||
type CreateEnterpriseReq struct {
|
||||
EnterpriseName string `json:"enterprise_name" validate:"required,max=100" required:"true" maximum:"100" description:"企业名称"`
|
||||
EnterpriseCode string `json:"enterprise_code" validate:"required,max=50" required:"true" maximum:"50" description:"企业编号(唯一)"`
|
||||
OwnerShopID *uint `json:"owner_shop_id" description:"归属店铺ID(可不填则归属平台)"`
|
||||
LegalPerson string `json:"legal_person" description:"法人代表"`
|
||||
ContactName string `json:"contact_name" description:"联系人姓名"`
|
||||
ContactPhone string `json:"contact_phone" description:"联系人电话"`
|
||||
BusinessLicense string `json:"business_license" description:"营业执照号"`
|
||||
Province string `json:"province" description:"省份"`
|
||||
City string `json:"city" description:"城市"`
|
||||
District string `json:"district" description:"区县"`
|
||||
Address string `json:"address" description:"详细地址"`
|
||||
LegalPerson string `json:"legal_person" validate:"max=50" maximum:"50" description:"法人代表"`
|
||||
ContactName string `json:"contact_name" validate:"required,max=50" required:"true" maximum:"50" description:"联系人姓名"`
|
||||
ContactPhone string `json:"contact_phone" validate:"required,max=20" required:"true" maximum:"20" description:"联系人电话"`
|
||||
LoginPhone string `json:"login_phone" validate:"required,len=11" required:"true" description:"登录手机号(作为企业账号)"`
|
||||
Password string `json:"password" validate:"required,min=6,max=20" required:"true" minimum:"6" maximum:"20" description:"登录密码"`
|
||||
BusinessLicense string `json:"business_license" validate:"max=100" maximum:"100" description:"营业执照号"`
|
||||
Province string `json:"province" validate:"max=50" maximum:"50" description:"省份"`
|
||||
City string `json:"city" validate:"max=50" maximum:"50" description:"城市"`
|
||||
District string `json:"district" validate:"max=50" maximum:"50" description:"区县"`
|
||||
Address string `json:"address" validate:"max=255" maximum:"255" description:"详细地址"`
|
||||
}
|
||||
|
||||
// UpdateEnterpriseRequest 更新企业请求
|
||||
type UpdateEnterpriseRequest struct {
|
||||
EnterpriseName *string `json:"enterprise_name" description:"企业名称"`
|
||||
EnterpriseCode *string `json:"enterprise_code" description:"企业编号"`
|
||||
LegalPerson *string `json:"legal_person" description:"法人代表"`
|
||||
ContactName *string `json:"contact_name" description:"联系人姓名"`
|
||||
ContactPhone *string `json:"contact_phone" description:"联系人电话"`
|
||||
BusinessLicense *string `json:"business_license" description:"营业执照号"`
|
||||
Province *string `json:"province" description:"省份"`
|
||||
City *string `json:"city" description:"城市"`
|
||||
District *string `json:"district" description:"区县"`
|
||||
Address *string `json:"address" description:"详细地址"`
|
||||
type UpdateEnterpriseReq struct {
|
||||
OwnerShopID *uint `json:"owner_shop_id" description:"归属店铺ID"`
|
||||
EnterpriseName *string `json:"enterprise_name" validate:"omitempty,max=100" maximum:"100" description:"企业名称"`
|
||||
EnterpriseCode *string `json:"enterprise_code" validate:"omitempty,max=50" maximum:"50" description:"企业编号"`
|
||||
LegalPerson *string `json:"legal_person" validate:"omitempty,max=50" maximum:"50" description:"法人代表"`
|
||||
ContactName *string `json:"contact_name" validate:"omitempty,max=50" maximum:"50" description:"联系人姓名"`
|
||||
ContactPhone *string `json:"contact_phone" validate:"omitempty,max=20" maximum:"20" description:"联系人电话"`
|
||||
BusinessLicense *string `json:"business_license" validate:"omitempty,max=100" maximum:"100" description:"营业执照号"`
|
||||
Province *string `json:"province" validate:"omitempty,max=50" maximum:"50" description:"省份"`
|
||||
City *string `json:"city" validate:"omitempty,max=50" maximum:"50" description:"城市"`
|
||||
District *string `json:"district" validate:"omitempty,max=50" maximum:"50" description:"区县"`
|
||||
Address *string `json:"address" validate:"omitempty,max=255" maximum:"255" description:"详细地址"`
|
||||
}
|
||||
|
||||
// EnterpriseResponse 企业响应
|
||||
type EnterpriseResponse struct {
|
||||
type EnterpriseListReq struct {
|
||||
Page int `json:"page" query:"page" validate:"omitempty,min=1" minimum:"1" description:"页码(默认1)"`
|
||||
PageSize int `json:"page_size" query:"page_size" validate:"omitempty,min=1,max=100" minimum:"1" maximum:"100" description:"每页数量(默认20,最大100)"`
|
||||
EnterpriseName string `json:"enterprise_name" query:"enterprise_name" description:"企业名称(模糊查询)"`
|
||||
LoginPhone string `json:"login_phone" query:"login_phone" description:"登录手机号(模糊查询)"`
|
||||
ContactPhone string `json:"contact_phone" query:"contact_phone" description:"联系人电话(模糊查询)"`
|
||||
OwnerShopID *uint `json:"owner_shop_id" query:"owner_shop_id" description:"归属店铺ID"`
|
||||
Status *int `json:"status" query:"status" description:"状态(0=禁用, 1=启用)"`
|
||||
}
|
||||
|
||||
type EnterpriseItem struct {
|
||||
ID uint `json:"id" description:"企业ID"`
|
||||
EnterpriseName string `json:"enterprise_name" description:"企业名称"`
|
||||
EnterpriseCode string `json:"enterprise_code" description:"企业编号"`
|
||||
OwnerShopID *uint `json:"owner_shop_id,omitempty" description:"归属店铺ID"`
|
||||
OwnerShopName string `json:"owner_shop_name" description:"归属店铺名称"`
|
||||
LegalPerson string `json:"legal_person" description:"法人代表"`
|
||||
ContactName string `json:"contact_name" description:"联系人姓名"`
|
||||
ContactPhone string `json:"contact_phone" description:"联系人电话"`
|
||||
LoginPhone string `json:"login_phone" description:"登录手机号"`
|
||||
BusinessLicense string `json:"business_license" description:"营业执照号"`
|
||||
Province string `json:"province" description:"省份"`
|
||||
City string `json:"city" description:"城市"`
|
||||
District string `json:"district" description:"区县"`
|
||||
Address string `json:"address" description:"详细地址"`
|
||||
Status int `json:"status" description:"状态 (0:禁用, 1:启用)"`
|
||||
Status int `json:"status" description:"状态(0=禁用, 1=启用)"`
|
||||
StatusName string `json:"status_name" description:"状态名称"`
|
||||
CreatedAt string `json:"created_at" description:"创建时间"`
|
||||
UpdatedAt string `json:"updated_at" description:"更新时间"`
|
||||
}
|
||||
|
||||
type EnterprisePageResult struct {
|
||||
Items []EnterpriseItem `json:"items" description:"企业列表"`
|
||||
Total int64 `json:"total" description:"总记录数"`
|
||||
Page int `json:"page" description:"当前页码"`
|
||||
Size int `json:"size" description:"每页数量"`
|
||||
}
|
||||
|
||||
type UpdateEnterpriseStatusReq struct {
|
||||
Status int `json:"status" validate:"required,oneof=0 1" required:"true" enum:"0,1" description:"状态(0=禁用, 1=启用)"`
|
||||
}
|
||||
|
||||
type UpdateEnterprisePasswordReq struct {
|
||||
Password string `json:"password" validate:"required,min=6,max=20" required:"true" minimum:"6" maximum:"20" description:"新密码"`
|
||||
}
|
||||
|
||||
type CreateEnterpriseResp struct {
|
||||
Enterprise EnterpriseItem `json:"enterprise" description:"企业信息"`
|
||||
AccountID uint `json:"account_id" description:"账号ID"`
|
||||
}
|
||||
|
||||
// CreateEnterpriseRequest 创建企业请求(兼容旧接口)
|
||||
type CreateEnterpriseRequest = CreateEnterpriseReq
|
||||
|
||||
// UpdateEnterpriseRequest 更新企业请求(兼容旧接口)
|
||||
type UpdateEnterpriseRequest = UpdateEnterpriseReq
|
||||
|
||||
// EnterpriseResponse 企业响应(兼容旧接口)
|
||||
type EnterpriseResponse = EnterpriseItem
|
||||
|
||||
@@ -12,17 +12,25 @@ import (
|
||||
type CommissionWithdrawalRequest struct {
|
||||
gorm.Model
|
||||
BaseModel `gorm:"embedded"`
|
||||
WithdrawalNo string `gorm:"column:withdrawal_no;type:varchar(50);uniqueIndex:uk_commission_withdrawal_no,where:deleted_at IS NULL AND withdrawal_no IS NOT NULL;comment:提现单号(唯一,格式:W + 时间戳 + 随机数)" json:"withdrawal_no"`
|
||||
AgentID uint `gorm:"column:agent_id;index;not null;comment:代理用户ID" json:"agent_id"`
|
||||
ApplicantID uint `gorm:"column:applicant_id;index;comment:申请人账号ID" json:"applicant_id"`
|
||||
ShopID uint `gorm:"column:shop_id;index;comment:店铺ID(冗余字段)" json:"shop_id"`
|
||||
Amount int64 `gorm:"column:amount;type:bigint;not null;comment:提现金额(分为单位)" json:"amount"`
|
||||
Fee int64 `gorm:"column:fee;type:bigint;default:0;comment:手续费(分为单位)" json:"fee"`
|
||||
FeeRate int64 `gorm:"column:fee_rate;type:bigint;default:0;comment:手续费比率(基点,100=1%,快照)" json:"fee_rate"`
|
||||
ActualAmount int64 `gorm:"column:actual_amount;type:bigint;comment:实际到账金额(分为单位)" json:"actual_amount"`
|
||||
WithdrawalMethod string `gorm:"column:withdrawal_method;type:varchar(20);comment:提现方式 alipay-支付宝 wechat-微信 bank-银行卡" json:"withdrawal_method"`
|
||||
PaymentType string `gorm:"column:payment_type;type:varchar(20);default:'manual';comment:放款类型(manual=人工打款)" json:"payment_type"`
|
||||
AccountInfo datatypes.JSON `gorm:"column:account_info;type:jsonb;comment:收款账户信息(姓名、账号等)" json:"account_info"`
|
||||
Status int `gorm:"column:status;type:int;default:1;comment:状态 1-待审核 2-已通过 3-已拒绝 4-已到账" json:"status"`
|
||||
ApprovedBy uint `gorm:"column:approved_by;index;comment:审批人用户ID" json:"approved_by"`
|
||||
ApprovedAt *time.Time `gorm:"column:approved_at;comment:审批时间" json:"approved_at"`
|
||||
ProcessorID uint `gorm:"column:processor_id;index;comment:处理人ID" json:"processor_id"`
|
||||
ProcessedAt *time.Time `gorm:"column:processed_at;comment:处理时间" json:"processed_at"`
|
||||
PaidAt *time.Time `gorm:"column:paid_at;comment:到账时间" json:"paid_at"`
|
||||
RejectReason string `gorm:"column:reject_reason;type:text;comment:拒绝原因" json:"reject_reason"`
|
||||
Remark string `gorm:"column:remark;type:text;comment:备注" json:"remark"`
|
||||
}
|
||||
|
||||
// TableName 指定表名
|
||||
@@ -34,11 +42,12 @@ func (CommissionWithdrawalRequest) TableName() string {
|
||||
// 提现参数配置(最低金额、手续费率、到账时间等)
|
||||
type CommissionWithdrawalSetting struct {
|
||||
gorm.Model
|
||||
BaseModel `gorm:"embedded"`
|
||||
MinWithdrawalAmount int64 `gorm:"column:min_withdrawal_amount;type:bigint;comment:最低提现金额(分为单位)" json:"min_withdrawal_amount"`
|
||||
FeeRate int64 `gorm:"column:fee_rate;type:bigint;comment:手续费率(万分比,如100表示1%)" json:"fee_rate"`
|
||||
ArrivalDays int `gorm:"column:arrival_days;type:int;comment:到账天数" json:"arrival_days"`
|
||||
IsActive bool `gorm:"column:is_active;type:boolean;default:true;comment:是否生效(最新一条)" json:"is_active"`
|
||||
BaseModel `gorm:"embedded"`
|
||||
MinWithdrawalAmount int64 `gorm:"column:min_withdrawal_amount;type:bigint;comment:最低提现金额(分为单位)" json:"min_withdrawal_amount"`
|
||||
FeeRate int64 `gorm:"column:fee_rate;type:bigint;comment:手续费率(万分比,如100表示1%)" json:"fee_rate"`
|
||||
ArrivalDays int `gorm:"column:arrival_days;type:int;comment:到账天数" json:"arrival_days"`
|
||||
DailyWithdrawalLimit int `gorm:"column:daily_withdrawal_limit;type:int;default:3;comment:每日提现次数限制" json:"daily_withdrawal_limit"`
|
||||
IsActive bool `gorm:"column:is_active;type:boolean;default:true;comment:是否生效(最新一条)" json:"is_active"`
|
||||
}
|
||||
|
||||
// TableName 指定表名
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
|
||||
// IotCard IoT 卡模型
|
||||
// 物联网卡/流量卡的统一管理实体
|
||||
// 支持平台自营、代理分销、用户购买等所有权模式
|
||||
// 支持平台自营、代理分销等所有权模式
|
||||
type IotCard struct {
|
||||
gorm.Model
|
||||
BaseModel `gorm:"embedded"`
|
||||
@@ -23,8 +23,9 @@ type IotCard struct {
|
||||
CostPrice int64 `gorm:"column:cost_price;type:bigint;default:0;comment:成本价(分为单位)" json:"cost_price"`
|
||||
DistributePrice int64 `gorm:"column:distribute_price;type:bigint;default:0;comment:分销价(分为单位)" json:"distribute_price"`
|
||||
Status int `gorm:"column:status;type:int;default:1;not null;comment:状态 1-在库 2-已分销 3-已激活 4-已停用" json:"status"`
|
||||
OwnerType string `gorm:"column:owner_type;type:varchar(20);default:'platform';not null;comment:所有者类型 platform-平台 agent-代理 user-用户 device-设备" json:"owner_type"`
|
||||
OwnerType string `gorm:"column:owner_type;type:varchar(20);default:'platform';not null;comment:所有者类型 platform-平台 shop-店铺" json:"owner_type"`
|
||||
OwnerID uint `gorm:"column:owner_id;index;default:0;not null;comment:所有者ID" json:"owner_id"`
|
||||
ShopID *uint `gorm:"column:shop_id;index;comment:店铺ID(冗余字段,方便查询)" json:"shop_id,omitempty"`
|
||||
ActivatedAt *time.Time `gorm:"column:activated_at;comment:激活时间" json:"activated_at"`
|
||||
ActivationStatus int `gorm:"column:activation_status;type:int;default:0;not null;comment:激活状态 0-未激活 1-已激活" json:"activation_status"`
|
||||
RealNameStatus int `gorm:"column:real_name_status;type:int;default:0;not null;comment:实名状态 0-未实名 1-已实名(行业卡可以保持0)" json:"real_name_status"`
|
||||
|
||||
66
internal/model/my_commission_dto.go
Normal file
66
internal/model/my_commission_dto.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package model
|
||||
|
||||
type MyCommissionSummaryResp struct {
|
||||
ShopID uint `json:"shop_id" description:"店铺ID"`
|
||||
ShopName string `json:"shop_name" description:"店铺名称"`
|
||||
TotalCommission int64 `json:"total_commission" description:"累计佣金(分)"`
|
||||
WithdrawnCommission int64 `json:"withdrawn_commission" description:"已提现佣金(分)"`
|
||||
UnwithdrawCommission int64 `json:"unwithdraw_commission" description:"未提现佣金(分)"`
|
||||
FrozenCommission int64 `json:"frozen_commission" description:"冻结佣金(分)"`
|
||||
WithdrawingCommission int64 `json:"withdrawing_commission" description:"提现中佣金(分)"`
|
||||
AvailableCommission int64 `json:"available_commission" description:"可提现佣金(分)"`
|
||||
}
|
||||
|
||||
type CreateMyWithdrawalReq struct {
|
||||
Amount int64 `json:"amount" validate:"required,min=1" required:"true" minimum:"1" description:"提现金额(分)"`
|
||||
WithdrawalMethod string `json:"withdrawal_method" validate:"required,oneof=alipay" required:"true" enum:"alipay" description:"收款类型"`
|
||||
AccountName string `json:"account_name" validate:"required,max=50" required:"true" maximum:"50" description:"收款人姓名"`
|
||||
AccountNumber string `json:"account_number" validate:"required,max=100" required:"true" maximum:"100" description:"收款账号"`
|
||||
}
|
||||
|
||||
type CreateMyWithdrawalResp struct {
|
||||
ID uint `json:"id" description:"提现申请ID"`
|
||||
WithdrawalNo string `json:"withdrawal_no" description:"提现单号"`
|
||||
Amount int64 `json:"amount" description:"提现金额(分)"`
|
||||
FeeRate int64 `json:"fee_rate" description:"手续费比率(基点)"`
|
||||
Fee int64 `json:"fee" description:"手续费(分)"`
|
||||
ActualAmount int64 `json:"actual_amount" description:"实际到账金额(分)"`
|
||||
Status int `json:"status" description:"状态"`
|
||||
StatusName string `json:"status_name" description:"状态名称"`
|
||||
CreatedAt string `json:"created_at" description:"申请时间"`
|
||||
}
|
||||
|
||||
type MyWithdrawalListReq struct {
|
||||
Page int `json:"page" query:"page" validate:"omitempty,min=1" minimum:"1" description:"页码"`
|
||||
PageSize int `json:"page_size" query:"page_size" validate:"omitempty,min=1,max=100" minimum:"1" maximum:"100" description:"每页数量"`
|
||||
Status *int `json:"status" query:"status" description:"状态(1=待审批, 2=已通过, 3=已拒绝)"`
|
||||
StartTime string `json:"start_time" query:"start_time" description:"申请开始时间"`
|
||||
EndTime string `json:"end_time" query:"end_time" description:"申请结束时间"`
|
||||
}
|
||||
|
||||
type MyCommissionRecordListReq struct {
|
||||
Page int `json:"page" query:"page" validate:"omitempty,min=1" minimum:"1" description:"页码"`
|
||||
PageSize int `json:"page_size" query:"page_size" validate:"omitempty,min=1,max=100" minimum:"1" maximum:"100" description:"每页数量"`
|
||||
CommissionType *string `json:"commission_type" query:"commission_type" description:"佣金类型"`
|
||||
ICCID string `json:"iccid" query:"iccid" description:"ICCID(模糊查询)"`
|
||||
DeviceNo string `json:"device_no" query:"device_no" description:"设备号(模糊查询)"`
|
||||
OrderNo string `json:"order_no" query:"order_no" description:"订单号(模糊查询)"`
|
||||
}
|
||||
|
||||
type MyCommissionRecordItem struct {
|
||||
ID uint `json:"id" description:"佣金记录ID"`
|
||||
ShopID uint `json:"shop_id" description:"店铺ID"`
|
||||
OrderID uint `json:"order_id" description:"订单ID"`
|
||||
CommissionType string `json:"commission_type" description:"佣金类型 (one_time:一次性, long_term:长期)"`
|
||||
Amount int64 `json:"amount" description:"佣金金额(分)"`
|
||||
Status int `json:"status" description:"状态 (1:已冻结, 2:解冻中, 3:已发放, 4:已失效)"`
|
||||
StatusName string `json:"status_name" description:"状态名称"`
|
||||
CreatedAt string `json:"created_at" description:"创建时间"`
|
||||
}
|
||||
|
||||
type MyCommissionRecordPageResult struct {
|
||||
Items []MyCommissionRecordItem `json:"items" description:"佣金记录列表"`
|
||||
Total int64 `json:"total" description:"总记录数"`
|
||||
Page int `json:"page" description:"当前页码"`
|
||||
Size int `json:"size" description:"每页数量"`
|
||||
}
|
||||
134
internal/model/shop_commission_dto.go
Normal file
134
internal/model/shop_commission_dto.go
Normal file
@@ -0,0 +1,134 @@
|
||||
package model
|
||||
|
||||
// ========================================
|
||||
// 代理商佣金查询 DTO
|
||||
// ========================================
|
||||
|
||||
// ShopCommissionSummaryListReq 代理商佣金列表查询请求
|
||||
type ShopCommissionSummaryListReq struct {
|
||||
Page int `json:"page" query:"page" validate:"omitempty,min=1" minimum:"1" description:"页码(默认1)"`
|
||||
PageSize int `json:"page_size" query:"page_size" validate:"omitempty,min=1,max=100" minimum:"1" maximum:"100" description:"每页数量(默认20,最大100)"`
|
||||
ShopName string `json:"shop_name" query:"shop_name" validate:"omitempty,max=100" maxLength:"100" description:"店铺名称(模糊查询)"`
|
||||
Username string `json:"username" query:"username" validate:"omitempty,max=50" maxLength:"50" description:"主账号用户名(模糊查询)"`
|
||||
}
|
||||
|
||||
// ShopCommissionSummaryItem 代理商佣金汇总项
|
||||
type ShopCommissionSummaryItem struct {
|
||||
ShopID uint `json:"shop_id" description:"店铺ID"`
|
||||
ShopName string `json:"shop_name" description:"店铺名称"`
|
||||
ShopCode string `json:"shop_code" description:"店铺编码"`
|
||||
Username string `json:"username" description:"主账号用户名"`
|
||||
Phone string `json:"phone" description:"主账号手机号"`
|
||||
TotalCommission int64 `json:"total_commission" description:"总佣金(分)"`
|
||||
WithdrawnCommission int64 `json:"withdrawn_commission" description:"已提现佣金(分)"`
|
||||
UnwithdrawCommission int64 `json:"unwithdraw_commission" description:"未提现佣金(分)"`
|
||||
FrozenCommission int64 `json:"frozen_commission" description:"冻结中佣金(分)"`
|
||||
WithdrawingCommission int64 `json:"withdrawing_commission" description:"提现中佣金(分)"`
|
||||
AvailableCommission int64 `json:"available_commission" description:"可提现佣金(分)"`
|
||||
CreatedAt string `json:"created_at" description:"店铺创建时间"`
|
||||
}
|
||||
|
||||
// ShopCommissionSummaryPageResult 代理商佣金列表分页响应
|
||||
type ShopCommissionSummaryPageResult struct {
|
||||
Items []ShopCommissionSummaryItem `json:"items" description:"代理商佣金列表"`
|
||||
Total int64 `json:"total" description:"总记录数"`
|
||||
Page int `json:"page" description:"当前页码"`
|
||||
Size int `json:"size" description:"每页数量"`
|
||||
}
|
||||
|
||||
// ========================================
|
||||
// 代理商提现记录查询 DTO
|
||||
// ========================================
|
||||
|
||||
// ShopWithdrawalRequestListReq 代理商提现记录查询请求
|
||||
type ShopWithdrawalRequestListReq struct {
|
||||
Page int `json:"page" query:"page" validate:"omitempty,min=1" minimum:"1" description:"页码(默认1)"`
|
||||
PageSize int `json:"page_size" query:"page_size" validate:"omitempty,min=1,max=100" minimum:"1" maximum:"100" description:"每页数量(默认20,最大100)"`
|
||||
WithdrawalNo string `json:"withdrawal_no" query:"withdrawal_no" validate:"omitempty,max=50" maxLength:"50" description:"提现单号(精确查询)"`
|
||||
StartTime string `json:"start_time" query:"start_time" validate:"omitempty" description:"申请开始时间(格式:2006-01-02 15:04:05)"`
|
||||
EndTime string `json:"end_time" query:"end_time" validate:"omitempty" description:"申请结束时间(格式:2006-01-02 15:04:05)"`
|
||||
}
|
||||
|
||||
// ShopWithdrawalRequestItem 代理商提现记录项
|
||||
type ShopWithdrawalRequestItem struct {
|
||||
ID uint `json:"id" description:"提现申请ID"`
|
||||
WithdrawalNo string `json:"withdrawal_no" description:"提现单号"`
|
||||
Amount int64 `json:"amount" description:"提现金额(分)"`
|
||||
FeeRate int64 `json:"fee_rate" description:"手续费比率(基点,100=1%)"`
|
||||
Fee int64 `json:"fee" description:"手续费(分)"`
|
||||
ActualAmount int64 `json:"actual_amount" description:"实际到账金额(分)"`
|
||||
Status int `json:"status" description:"状态 (1:待审核, 2:已通过, 3:已拒绝, 4:已到账)"`
|
||||
StatusName string `json:"status_name" description:"状态名称"`
|
||||
ShopID uint `json:"shop_id" description:"店铺ID"`
|
||||
ShopName string `json:"shop_name" description:"店铺名称"`
|
||||
ShopHierarchy string `json:"shop_hierarchy" description:"店铺层级路径(格式:上上级_上级_本身,最多两层上级)"`
|
||||
ApplicantID uint `json:"applicant_id" description:"申请人账号ID"`
|
||||
ApplicantName string `json:"applicant_name" description:"申请人用户名"`
|
||||
ProcessorID *uint `json:"processor_id,omitempty" description:"处理人账号ID"`
|
||||
ProcessorName string `json:"processor_name,omitempty" description:"处理人用户名"`
|
||||
WithdrawalMethod string `json:"withdrawal_method" description:"提现方式 (alipay:支付宝, wechat:微信, bank:银行卡)"`
|
||||
PaymentType string `json:"payment_type" description:"放款类型 (manual:人工打款)"`
|
||||
AccountName string `json:"account_name" description:"收款账户名称"`
|
||||
AccountNumber string `json:"account_number" description:"收款账号"`
|
||||
BankName string `json:"bank_name,omitempty" description:"银行名称(银行卡提现时)"`
|
||||
RejectReason string `json:"reject_reason,omitempty" description:"拒绝原因"`
|
||||
Remark string `json:"remark,omitempty" description:"备注"`
|
||||
CreatedAt string `json:"created_at" description:"申请时间"`
|
||||
ProcessedAt string `json:"processed_at,omitempty" description:"处理时间"`
|
||||
PaidAt string `json:"paid_at,omitempty" description:"到账时间"`
|
||||
}
|
||||
|
||||
// ShopWithdrawalRequestPageResult 代理商提现记录分页响应
|
||||
type ShopWithdrawalRequestPageResult struct {
|
||||
Items []ShopWithdrawalRequestItem `json:"items" description:"提现记录列表"`
|
||||
Total int64 `json:"total" description:"总记录数"`
|
||||
Page int `json:"page" description:"当前页码"`
|
||||
Size int `json:"size" description:"每页数量"`
|
||||
}
|
||||
|
||||
// ========================================
|
||||
// 代理商佣金明细查询 DTO
|
||||
// ========================================
|
||||
|
||||
// ShopCommissionRecordListReq 代理商佣金明细查询请求
|
||||
type ShopCommissionRecordListReq struct {
|
||||
Page int `json:"page" query:"page" validate:"omitempty,min=1" minimum:"1" description:"页码(默认1)"`
|
||||
PageSize int `json:"page_size" query:"page_size" validate:"omitempty,min=1,max=100" minimum:"1" maximum:"100" description:"每页数量(默认20,最大100)"`
|
||||
CommissionType string `json:"commission_type" query:"commission_type" validate:"omitempty,oneof=one_time long_term" description:"佣金类型 (one_time:一次性, long_term:长期)"`
|
||||
ICCID string `json:"iccid" query:"iccid" validate:"omitempty,max=50" maxLength:"50" description:"ICCID(模糊查询)"`
|
||||
DeviceNo string `json:"device_no" query:"device_no" validate:"omitempty,max=50" maxLength:"50" description:"设备号(模糊查询)"`
|
||||
OrderNo string `json:"order_no" query:"order_no" validate:"omitempty,max=50" maxLength:"50" description:"订单号(模糊查询)"`
|
||||
}
|
||||
|
||||
// ShopCommissionRecordItem 代理商佣金明细项
|
||||
type ShopCommissionRecordItem struct {
|
||||
ID uint `json:"id" description:"佣金记录ID"`
|
||||
Amount int64 `json:"amount" description:"佣金金额(分)"`
|
||||
BalanceAfter int64 `json:"balance_after" description:"入账后佣金余额(分)"`
|
||||
CommissionType string `json:"commission_type" description:"佣金类型 (one_time:一次性, long_term:长期)"`
|
||||
Status int `json:"status" description:"状态 (1:已冻结, 2:解冻中, 3:已发放, 4:已失效)"`
|
||||
StatusName string `json:"status_name" description:"状态名称"`
|
||||
OrderID uint `json:"order_id" description:"订单ID"`
|
||||
OrderNo string `json:"order_no" description:"订单号"`
|
||||
DeviceNo string `json:"device_no,omitempty" description:"设备号"`
|
||||
ICCID string `json:"iccid,omitempty" description:"ICCID"`
|
||||
OrderCreatedAt string `json:"order_created_at" description:"订单创建时间"`
|
||||
CreatedAt string `json:"created_at" description:"佣金入账时间"`
|
||||
}
|
||||
|
||||
// ShopCommissionRecordPageResult 代理商佣金明细分页响应
|
||||
type ShopCommissionRecordPageResult struct {
|
||||
Items []ShopCommissionRecordItem `json:"items" description:"佣金明细列表"`
|
||||
Total int64 `json:"total" description:"总记录数"`
|
||||
Page int `json:"page" description:"当前页码"`
|
||||
Size int `json:"size" description:"每页数量"`
|
||||
}
|
||||
|
||||
// ========================================
|
||||
// 路由参数 DTO
|
||||
// ========================================
|
||||
|
||||
// ShopIDPathParam 店铺ID路径参数
|
||||
type ShopIDPathParam struct {
|
||||
ShopID uint `path:"shop_id" description:"店铺ID" required:"true"`
|
||||
}
|
||||
@@ -31,6 +31,27 @@ func RegisterAdminRoutes(router fiber.Router, handlers *bootstrap.Handlers, midd
|
||||
if handlers.ShopAccount != nil {
|
||||
registerShopAccountRoutes(authGroup, handlers.ShopAccount, doc, basePath)
|
||||
}
|
||||
if handlers.ShopCommission != nil {
|
||||
registerShopCommissionRoutes(authGroup, handlers.ShopCommission, doc, basePath)
|
||||
}
|
||||
if handlers.CommissionWithdrawal != nil {
|
||||
registerCommissionWithdrawalRoutes(authGroup, handlers.CommissionWithdrawal, doc, basePath)
|
||||
}
|
||||
if handlers.CommissionWithdrawalSetting != nil {
|
||||
registerCommissionWithdrawalSettingRoutes(authGroup, handlers.CommissionWithdrawalSetting, doc, basePath)
|
||||
}
|
||||
if handlers.Enterprise != nil {
|
||||
registerEnterpriseRoutes(authGroup, handlers.Enterprise, doc, basePath)
|
||||
}
|
||||
if handlers.EnterpriseCard != nil {
|
||||
registerEnterpriseCardRoutes(authGroup, handlers.EnterpriseCard, doc, basePath)
|
||||
}
|
||||
if handlers.CustomerAccount != nil {
|
||||
registerCustomerAccountRoutes(authGroup, handlers.CustomerAccount, doc, basePath)
|
||||
}
|
||||
if handlers.MyCommission != nil {
|
||||
registerMyCommissionRoutes(authGroup, handlers.MyCommission, doc, basePath)
|
||||
}
|
||||
}
|
||||
|
||||
func registerAdminAuthRoutes(router fiber.Router, handler interface{}, authMiddleware fiber.Handler, doc *openapi.Generator, basePath string) {
|
||||
|
||||
68
internal/routes/commission.go
Normal file
68
internal/routes/commission.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/handler/admin"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/openapi"
|
||||
)
|
||||
|
||||
func registerCommissionWithdrawalRoutes(router fiber.Router, handler *admin.CommissionWithdrawalHandler, doc *openapi.Generator, basePath string) {
|
||||
commission := router.Group("/commission")
|
||||
groupPath := basePath + "/commission"
|
||||
|
||||
Register(commission, doc, groupPath, "GET", "/withdrawal-requests", handler.ListWithdrawalRequests, RouteSpec{
|
||||
Summary: "提现申请列表",
|
||||
Tags: []string{"佣金提现审批"},
|
||||
Input: new(model.WithdrawalRequestListReq),
|
||||
Output: new(model.WithdrawalRequestPageResult),
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
Register(commission, doc, groupPath, "POST", "/withdrawal-requests/:id/approve", handler.ApproveWithdrawal, RouteSpec{
|
||||
Summary: "审批通过提现申请",
|
||||
Tags: []string{"佣金提现审批"},
|
||||
Input: new(model.ApproveWithdrawalReq),
|
||||
Output: new(model.WithdrawalApprovalResp),
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
Register(commission, doc, groupPath, "POST", "/withdrawal-requests/:id/reject", handler.RejectWithdrawal, RouteSpec{
|
||||
Summary: "拒绝提现申请",
|
||||
Tags: []string{"佣金提现审批"},
|
||||
Input: new(model.RejectWithdrawalReq),
|
||||
Output: new(model.WithdrawalApprovalResp),
|
||||
Auth: true,
|
||||
})
|
||||
}
|
||||
|
||||
// registerCommissionWithdrawalSettingRoutes 注册提现配置管理路由
|
||||
func registerCommissionWithdrawalSettingRoutes(router fiber.Router, handler *admin.CommissionWithdrawalSettingHandler, doc *openapi.Generator, basePath string) {
|
||||
commission := router.Group("/commission")
|
||||
groupPath := basePath + "/commission"
|
||||
|
||||
Register(commission, doc, groupPath, "POST", "/withdrawal-settings", handler.Create, RouteSpec{
|
||||
Summary: "新增提现配置",
|
||||
Tags: []string{"提现配置管理"},
|
||||
Input: new(model.CreateWithdrawalSettingReq),
|
||||
Output: new(model.WithdrawalSettingItem),
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
Register(commission, doc, groupPath, "GET", "/withdrawal-settings", handler.List, RouteSpec{
|
||||
Summary: "提现配置列表",
|
||||
Tags: []string{"提现配置管理"},
|
||||
Input: new(model.WithdrawalSettingListReq),
|
||||
Output: new(model.WithdrawalSettingPageResult),
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
Register(commission, doc, groupPath, "GET", "/withdrawal-settings/current", handler.GetCurrent, RouteSpec{
|
||||
Summary: "获取当前生效的提现配置",
|
||||
Tags: []string{"提现配置管理"},
|
||||
Input: nil,
|
||||
Output: new(model.WithdrawalSettingItem),
|
||||
Auth: true,
|
||||
})
|
||||
}
|
||||
54
internal/routes/customer_account.go
Normal file
54
internal/routes/customer_account.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/handler/admin"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/openapi"
|
||||
)
|
||||
|
||||
func registerCustomerAccountRoutes(router fiber.Router, handler *admin.CustomerAccountHandler, doc *openapi.Generator, basePath string) {
|
||||
accounts := router.Group("/customer-accounts")
|
||||
groupPath := basePath + "/customer-accounts"
|
||||
|
||||
Register(accounts, doc, groupPath, "GET", "", handler.List, RouteSpec{
|
||||
Summary: "客户账号列表",
|
||||
Tags: []string{"客户账号管理"},
|
||||
Input: new(model.CustomerAccountListReq),
|
||||
Output: new(model.CustomerAccountPageResult),
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
Register(accounts, doc, groupPath, "POST", "", handler.Create, RouteSpec{
|
||||
Summary: "新增代理商账号",
|
||||
Tags: []string{"客户账号管理"},
|
||||
Input: new(model.CreateCustomerAccountReq),
|
||||
Output: new(model.CustomerAccountItem),
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
Register(accounts, doc, groupPath, "PUT", "/:id", handler.Update, RouteSpec{
|
||||
Summary: "编辑账号",
|
||||
Tags: []string{"客户账号管理"},
|
||||
Input: new(model.UpdateCustomerAccountReq),
|
||||
Output: new(model.CustomerAccountItem),
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
Register(accounts, doc, groupPath, "PUT", "/:id/password", handler.UpdatePassword, RouteSpec{
|
||||
Summary: "修改账号密码",
|
||||
Tags: []string{"客户账号管理"},
|
||||
Input: new(model.UpdateCustomerAccountPasswordReq),
|
||||
Output: nil,
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
Register(accounts, doc, groupPath, "PUT", "/:id/status", handler.UpdateStatus, RouteSpec{
|
||||
Summary: "修改账号状态",
|
||||
Tags: []string{"客户账号管理"},
|
||||
Input: new(model.UpdateCustomerAccountStatusReq),
|
||||
Output: nil,
|
||||
Auth: true,
|
||||
})
|
||||
}
|
||||
54
internal/routes/enterprise.go
Normal file
54
internal/routes/enterprise.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/handler/admin"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/openapi"
|
||||
)
|
||||
|
||||
func registerEnterpriseRoutes(router fiber.Router, handler *admin.EnterpriseHandler, doc *openapi.Generator, basePath string) {
|
||||
enterprises := router.Group("/enterprises")
|
||||
groupPath := basePath + "/enterprises"
|
||||
|
||||
Register(enterprises, doc, groupPath, "POST", "", handler.Create, RouteSpec{
|
||||
Summary: "新增企业客户",
|
||||
Tags: []string{"企业客户管理"},
|
||||
Input: new(model.CreateEnterpriseReq),
|
||||
Output: new(model.CreateEnterpriseResp),
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
Register(enterprises, doc, groupPath, "GET", "", handler.List, RouteSpec{
|
||||
Summary: "查询企业客户列表",
|
||||
Tags: []string{"企业客户管理"},
|
||||
Input: new(model.EnterpriseListReq),
|
||||
Output: new(model.EnterprisePageResult),
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
Register(enterprises, doc, groupPath, "PUT", "/:id", handler.Update, RouteSpec{
|
||||
Summary: "编辑企业信息",
|
||||
Tags: []string{"企业客户管理"},
|
||||
Input: new(model.UpdateEnterpriseReq),
|
||||
Output: new(model.EnterpriseItem),
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
Register(enterprises, doc, groupPath, "PUT", "/:id/status", handler.UpdateStatus, RouteSpec{
|
||||
Summary: "启用/禁用企业",
|
||||
Tags: []string{"企业客户管理"},
|
||||
Input: new(model.UpdateEnterpriseStatusReq),
|
||||
Output: nil,
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
Register(enterprises, doc, groupPath, "PUT", "/:id/password", handler.UpdatePassword, RouteSpec{
|
||||
Summary: "修改企业账号密码",
|
||||
Tags: []string{"企业客户管理"},
|
||||
Input: new(model.UpdateEnterprisePasswordReq),
|
||||
Output: nil,
|
||||
Auth: true,
|
||||
})
|
||||
}
|
||||
62
internal/routes/enterprise_card.go
Normal file
62
internal/routes/enterprise_card.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/handler/admin"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/openapi"
|
||||
)
|
||||
|
||||
func registerEnterpriseCardRoutes(router fiber.Router, handler *admin.EnterpriseCardHandler, doc *openapi.Generator, basePath string) {
|
||||
enterprises := router.Group("/enterprises")
|
||||
groupPath := basePath + "/enterprises"
|
||||
|
||||
Register(enterprises, doc, groupPath, "POST", "/:id/allocate-cards/preview", handler.AllocateCardsPreview, RouteSpec{
|
||||
Summary: "卡授权预检",
|
||||
Tags: []string{"企业卡授权"},
|
||||
Input: new(model.AllocateCardsPreviewReq),
|
||||
Output: new(model.AllocateCardsPreviewResp),
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
Register(enterprises, doc, groupPath, "POST", "/:id/allocate-cards", handler.AllocateCards, RouteSpec{
|
||||
Summary: "授权卡给企业",
|
||||
Tags: []string{"企业卡授权"},
|
||||
Input: new(model.AllocateCardsReq),
|
||||
Output: new(model.AllocateCardsResp),
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
Register(enterprises, doc, groupPath, "POST", "/:id/recall-cards", handler.RecallCards, RouteSpec{
|
||||
Summary: "回收卡授权",
|
||||
Tags: []string{"企业卡授权"},
|
||||
Input: new(model.RecallCardsReq),
|
||||
Output: new(model.RecallCardsResp),
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
Register(enterprises, doc, groupPath, "GET", "/:id/cards", handler.ListCards, RouteSpec{
|
||||
Summary: "企业卡列表",
|
||||
Tags: []string{"企业卡授权"},
|
||||
Input: new(model.EnterpriseCardListReq),
|
||||
Output: new(model.EnterpriseCardPageResult),
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
Register(enterprises, doc, groupPath, "POST", "/:id/cards/:card_id/suspend", handler.SuspendCard, RouteSpec{
|
||||
Summary: "停机卡",
|
||||
Tags: []string{"企业卡授权"},
|
||||
Input: nil,
|
||||
Output: nil,
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
Register(enterprises, doc, groupPath, "POST", "/:id/cards/:card_id/resume", handler.ResumeCard, RouteSpec{
|
||||
Summary: "复机卡",
|
||||
Tags: []string{"企业卡授权"},
|
||||
Input: nil,
|
||||
Output: nil,
|
||||
Auth: true,
|
||||
})
|
||||
}
|
||||
46
internal/routes/my_commission.go
Normal file
46
internal/routes/my_commission.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/handler/admin"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/openapi"
|
||||
)
|
||||
|
||||
func registerMyCommissionRoutes(router fiber.Router, handler *admin.MyCommissionHandler, doc *openapi.Generator, basePath string) {
|
||||
my := router.Group("/my")
|
||||
groupPath := basePath + "/my"
|
||||
|
||||
Register(my, doc, groupPath, "GET", "/commission-summary", handler.GetSummary, RouteSpec{
|
||||
Summary: "我的佣金概览",
|
||||
Tags: []string{"我的佣金"},
|
||||
Input: nil,
|
||||
Output: new(model.MyCommissionSummaryResp),
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
Register(my, doc, groupPath, "POST", "/withdrawal-requests", handler.CreateWithdrawal, RouteSpec{
|
||||
Summary: "发起提现申请",
|
||||
Tags: []string{"我的佣金"},
|
||||
Input: new(model.CreateMyWithdrawalReq),
|
||||
Output: new(model.CreateMyWithdrawalResp),
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
Register(my, doc, groupPath, "GET", "/withdrawal-requests", handler.ListWithdrawals, RouteSpec{
|
||||
Summary: "我的提现记录",
|
||||
Tags: []string{"我的佣金"},
|
||||
Input: new(model.MyWithdrawalListReq),
|
||||
Output: new(model.WithdrawalRequestPageResult),
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
Register(my, doc, groupPath, "GET", "/commission-records", handler.ListRecords, RouteSpec{
|
||||
Summary: "我的佣金明细",
|
||||
Tags: []string{"我的佣金"},
|
||||
Input: new(model.MyCommissionRecordListReq),
|
||||
Output: new(model.MyCommissionRecordPageResult),
|
||||
Auth: true,
|
||||
})
|
||||
}
|
||||
@@ -89,3 +89,32 @@ func registerShopAccountRoutes(router fiber.Router, handler *admin.ShopAccountHa
|
||||
Auth: true,
|
||||
})
|
||||
}
|
||||
|
||||
func registerShopCommissionRoutes(router fiber.Router, handler *admin.ShopCommissionHandler, doc *openapi.Generator, basePath string) {
|
||||
shops := router.Group("/shops")
|
||||
groupPath := basePath + "/shops"
|
||||
|
||||
Register(shops, doc, groupPath, "GET", "/commission-summary", handler.ListCommissionSummary, RouteSpec{
|
||||
Summary: "代理商佣金列表",
|
||||
Tags: []string{"代理商佣金管理"},
|
||||
Input: new(model.ShopCommissionSummaryListReq),
|
||||
Output: new(model.ShopCommissionSummaryPageResult),
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
Register(shops, doc, groupPath, "GET", "/:shop_id/withdrawal-requests", handler.ListWithdrawalRequests, RouteSpec{
|
||||
Summary: "代理商提现记录",
|
||||
Tags: []string{"代理商佣金管理"},
|
||||
Input: new(model.ShopWithdrawalRequestListReq),
|
||||
Output: new(model.ShopWithdrawalRequestPageResult),
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
Register(shops, doc, groupPath, "GET", "/:shop_id/commission-records", handler.ListCommissionRecords, RouteSpec{
|
||||
Summary: "代理商佣金明细",
|
||||
Tags: []string{"代理商佣金管理"},
|
||||
Input: new(model.ShopCommissionRecordListReq),
|
||||
Output: new(model.ShopCommissionRecordPageResult),
|
||||
Auth: true,
|
||||
})
|
||||
}
|
||||
|
||||
420
internal/service/commission_withdrawal/service.go
Normal file
420
internal/service/commission_withdrawal/service.go
Normal file
@@ -0,0 +1,420 @@
|
||||
package commission_withdrawal
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/middleware"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
db *gorm.DB
|
||||
shopStore *postgres.ShopStore
|
||||
accountStore *postgres.AccountStore
|
||||
walletStore *postgres.WalletStore
|
||||
walletTransactionStore *postgres.WalletTransactionStore
|
||||
commissionWithdrawalReqStore *postgres.CommissionWithdrawalRequestStore
|
||||
}
|
||||
|
||||
func New(
|
||||
db *gorm.DB,
|
||||
shopStore *postgres.ShopStore,
|
||||
accountStore *postgres.AccountStore,
|
||||
walletStore *postgres.WalletStore,
|
||||
walletTransactionStore *postgres.WalletTransactionStore,
|
||||
commissionWithdrawalReqStore *postgres.CommissionWithdrawalRequestStore,
|
||||
) *Service {
|
||||
return &Service{
|
||||
db: db,
|
||||
shopStore: shopStore,
|
||||
accountStore: accountStore,
|
||||
walletStore: walletStore,
|
||||
walletTransactionStore: walletTransactionStore,
|
||||
commissionWithdrawalReqStore: commissionWithdrawalReqStore,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) ListWithdrawalRequests(ctx context.Context, req *model.WithdrawalRequestListReq) (*model.WithdrawalRequestPageResult, error) {
|
||||
opts := &store.QueryOptions{
|
||||
Page: req.Page,
|
||||
PageSize: req.PageSize,
|
||||
OrderBy: "created_at DESC",
|
||||
}
|
||||
if opts.Page == 0 {
|
||||
opts.Page = 1
|
||||
}
|
||||
if opts.PageSize == 0 {
|
||||
opts.PageSize = constants.DefaultPageSize
|
||||
}
|
||||
|
||||
filters := &postgres.WithdrawalRequestListFilters{
|
||||
WithdrawalNo: req.WithdrawalNo,
|
||||
Status: req.Status,
|
||||
}
|
||||
|
||||
if req.StartTime != "" {
|
||||
t, err := time.Parse("2006-01-02 15:04:05", req.StartTime)
|
||||
if err == nil {
|
||||
filters.StartTime = &t
|
||||
}
|
||||
}
|
||||
if req.EndTime != "" {
|
||||
t, err := time.Parse("2006-01-02 15:04:05", req.EndTime)
|
||||
if err == nil {
|
||||
filters.EndTime = &t
|
||||
}
|
||||
}
|
||||
|
||||
requests, total, err := s.commissionWithdrawalReqStore.List(ctx, opts, filters)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询提现申请列表失败: %w", err)
|
||||
}
|
||||
|
||||
shopIDs := make([]uint, 0)
|
||||
applicantIDs := make([]uint, 0)
|
||||
processorIDs := make([]uint, 0)
|
||||
for _, r := range requests {
|
||||
if r.ShopID > 0 {
|
||||
shopIDs = append(shopIDs, r.ShopID)
|
||||
}
|
||||
if r.ApplicantID > 0 {
|
||||
applicantIDs = append(applicantIDs, r.ApplicantID)
|
||||
}
|
||||
if r.ProcessorID > 0 {
|
||||
processorIDs = append(processorIDs, r.ProcessorID)
|
||||
}
|
||||
}
|
||||
|
||||
shopMap := make(map[uint]*model.Shop)
|
||||
for _, id := range shopIDs {
|
||||
shop, err := s.shopStore.GetByID(ctx, id)
|
||||
if err == nil {
|
||||
shopMap[id] = shop
|
||||
}
|
||||
}
|
||||
|
||||
applicantMap := make(map[uint]string)
|
||||
processorMap := make(map[uint]string)
|
||||
if len(applicantIDs) > 0 {
|
||||
accounts, _ := s.accountStore.GetByIDs(ctx, applicantIDs)
|
||||
for _, acc := range accounts {
|
||||
applicantMap[acc.ID] = acc.Username
|
||||
}
|
||||
}
|
||||
if len(processorIDs) > 0 {
|
||||
accounts, _ := s.accountStore.GetByIDs(ctx, processorIDs)
|
||||
for _, acc := range accounts {
|
||||
processorMap[acc.ID] = acc.Username
|
||||
}
|
||||
}
|
||||
|
||||
items := make([]model.WithdrawalRequestItem, 0, len(requests))
|
||||
for _, r := range requests {
|
||||
shop := shopMap[r.ShopID]
|
||||
shopName := ""
|
||||
shopHierarchy := ""
|
||||
if shop != nil {
|
||||
shopName = shop.ShopName
|
||||
shopHierarchy = s.buildShopHierarchyPath(ctx, shop)
|
||||
if req.ShopName != "" && !containsSubstring(shopName, req.ShopName) {
|
||||
total--
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
item := s.buildWithdrawalRequestItem(r, shopName, shopHierarchy, applicantMap, processorMap)
|
||||
items = append(items, item)
|
||||
}
|
||||
|
||||
return &model.WithdrawalRequestPageResult{
|
||||
Items: items,
|
||||
Total: total,
|
||||
Page: opts.Page,
|
||||
Size: opts.PageSize,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) Approve(ctx context.Context, id uint, req *model.ApproveWithdrawalReq) (*model.WithdrawalApprovalResp, error) {
|
||||
currentUserID := middleware.GetUserIDFromContext(ctx)
|
||||
if currentUserID == 0 {
|
||||
return nil, errors.New(errors.CodeUnauthorized, "未授权访问")
|
||||
}
|
||||
|
||||
withdrawal, err := s.commissionWithdrawalReqStore.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
return nil, errors.New(errors.CodeNotFound, "提现申请不存在")
|
||||
}
|
||||
|
||||
if withdrawal.Status != constants.WithdrawalStatusPending {
|
||||
return nil, errors.New(errors.CodeInvalidStatus, "申请状态不允许此操作")
|
||||
}
|
||||
|
||||
wallet, err := s.walletStore.GetShopCommissionWallet(ctx, withdrawal.ShopID)
|
||||
if err != nil {
|
||||
return nil, errors.New(errors.CodeNotFound, "店铺佣金钱包不存在")
|
||||
}
|
||||
|
||||
amount := withdrawal.Amount
|
||||
if req.Amount != nil {
|
||||
amount = *req.Amount
|
||||
}
|
||||
|
||||
if wallet.FrozenBalance < amount {
|
||||
return nil, errors.New(errors.CodeInsufficientBalance, "钱包冻结余额不足")
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
err = s.db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := s.walletStore.DeductFrozenBalanceWithTx(ctx, tx, wallet.ID, amount); err != nil {
|
||||
return fmt.Errorf("扣除冻结余额失败: %w", err)
|
||||
}
|
||||
|
||||
refType := "withdrawal"
|
||||
refID := withdrawal.ID
|
||||
transaction := &model.WalletTransaction{
|
||||
WalletID: wallet.ID,
|
||||
UserID: currentUserID,
|
||||
TransactionType: "withdrawal",
|
||||
Amount: -amount,
|
||||
BalanceBefore: wallet.Balance,
|
||||
BalanceAfter: wallet.Balance,
|
||||
Status: 1,
|
||||
ReferenceType: &refType,
|
||||
ReferenceID: &refID,
|
||||
Creator: currentUserID,
|
||||
}
|
||||
if err := s.walletTransactionStore.CreateWithTx(ctx, tx, transaction); err != nil {
|
||||
return fmt.Errorf("创建交易流水失败: %w", err)
|
||||
}
|
||||
|
||||
updates := map[string]interface{}{
|
||||
"status": constants.WithdrawalStatusApproved,
|
||||
"processor_id": currentUserID,
|
||||
"processed_at": now,
|
||||
"payment_type": req.PaymentType,
|
||||
"remark": req.Remark,
|
||||
}
|
||||
|
||||
if req.Amount != nil {
|
||||
feeRate := withdrawal.FeeRate
|
||||
fee := amount * feeRate / 10000
|
||||
actualAmount := amount - fee
|
||||
updates["amount"] = amount
|
||||
updates["fee"] = fee
|
||||
updates["actual_amount"] = actualAmount
|
||||
}
|
||||
|
||||
if req.WithdrawalMethod != nil {
|
||||
updates["withdrawal_method"] = *req.WithdrawalMethod
|
||||
}
|
||||
if req.AccountName != nil || req.AccountNumber != nil {
|
||||
accountInfo := make(map[string]interface{})
|
||||
if withdrawal.AccountInfo != nil {
|
||||
_ = json.Unmarshal(withdrawal.AccountInfo, &accountInfo)
|
||||
}
|
||||
if req.AccountName != nil {
|
||||
accountInfo["account_name"] = *req.AccountName
|
||||
}
|
||||
if req.AccountNumber != nil {
|
||||
accountInfo["account_number"] = *req.AccountNumber
|
||||
}
|
||||
infoBytes, _ := json.Marshal(accountInfo)
|
||||
updates["account_info"] = infoBytes
|
||||
}
|
||||
|
||||
if err := s.commissionWithdrawalReqStore.UpdateStatusWithTx(ctx, tx, id, updates); err != nil {
|
||||
return fmt.Errorf("更新提现申请状态失败: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &model.WithdrawalApprovalResp{
|
||||
ID: withdrawal.ID,
|
||||
WithdrawalNo: withdrawal.WithdrawalNo,
|
||||
Status: constants.WithdrawalStatusApproved,
|
||||
StatusName: "已通过",
|
||||
ProcessedAt: now.Format("2006-01-02 15:04:05"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) Reject(ctx context.Context, id uint, req *model.RejectWithdrawalReq) (*model.WithdrawalApprovalResp, error) {
|
||||
currentUserID := middleware.GetUserIDFromContext(ctx)
|
||||
if currentUserID == 0 {
|
||||
return nil, errors.New(errors.CodeUnauthorized, "未授权访问")
|
||||
}
|
||||
|
||||
withdrawal, err := s.commissionWithdrawalReqStore.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
return nil, errors.New(errors.CodeNotFound, "提现申请不存在")
|
||||
}
|
||||
|
||||
if withdrawal.Status != constants.WithdrawalStatusPending {
|
||||
return nil, errors.New(errors.CodeInvalidStatus, "申请状态不允许此操作")
|
||||
}
|
||||
|
||||
wallet, err := s.walletStore.GetShopCommissionWallet(ctx, withdrawal.ShopID)
|
||||
if err != nil {
|
||||
return nil, errors.New(errors.CodeNotFound, "店铺佣金钱包不存在")
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
err = s.db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := s.walletStore.UnfreezeBalanceWithTx(ctx, tx, wallet.ID, withdrawal.Amount); err != nil {
|
||||
return fmt.Errorf("解冻余额失败: %w", err)
|
||||
}
|
||||
|
||||
refType := "withdrawal"
|
||||
refID := withdrawal.ID
|
||||
transaction := &model.WalletTransaction{
|
||||
WalletID: wallet.ID,
|
||||
UserID: currentUserID,
|
||||
TransactionType: "refund",
|
||||
Amount: withdrawal.Amount,
|
||||
BalanceBefore: wallet.Balance,
|
||||
BalanceAfter: wallet.Balance + withdrawal.Amount,
|
||||
Status: 1,
|
||||
ReferenceType: &refType,
|
||||
ReferenceID: &refID,
|
||||
Creator: currentUserID,
|
||||
}
|
||||
if err := s.walletTransactionStore.CreateWithTx(ctx, tx, transaction); err != nil {
|
||||
return fmt.Errorf("创建交易流水失败: %w", err)
|
||||
}
|
||||
|
||||
updates := map[string]interface{}{
|
||||
"status": constants.WithdrawalStatusRejected,
|
||||
"processor_id": currentUserID,
|
||||
"processed_at": now,
|
||||
"reject_reason": req.Remark,
|
||||
"remark": req.Remark,
|
||||
}
|
||||
if err := s.commissionWithdrawalReqStore.UpdateStatusWithTx(ctx, tx, id, updates); err != nil {
|
||||
return fmt.Errorf("更新提现申请状态失败: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &model.WithdrawalApprovalResp{
|
||||
ID: withdrawal.ID,
|
||||
WithdrawalNo: withdrawal.WithdrawalNo,
|
||||
Status: constants.WithdrawalStatusRejected,
|
||||
StatusName: "已拒绝",
|
||||
ProcessedAt: now.Format("2006-01-02 15:04:05"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) buildShopHierarchyPath(ctx context.Context, shop *model.Shop) string {
|
||||
if shop == nil {
|
||||
return ""
|
||||
}
|
||||
path := shop.ShopName
|
||||
current := shop
|
||||
depth := 0
|
||||
for current.ParentID != nil && depth < 2 {
|
||||
parent, err := s.shopStore.GetByID(ctx, *current.ParentID)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
path = parent.ShopName + "_" + path
|
||||
current = parent
|
||||
depth++
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func (s *Service) buildWithdrawalRequestItem(r *model.CommissionWithdrawalRequest, shopName, shopHierarchy string, applicantMap, processorMap map[uint]string) model.WithdrawalRequestItem {
|
||||
var processorID *uint
|
||||
if r.ProcessorID > 0 {
|
||||
processorID = &r.ProcessorID
|
||||
}
|
||||
|
||||
var accountName, accountNumber, bankName string
|
||||
if len(r.AccountInfo) > 0 {
|
||||
var info map[string]interface{}
|
||||
if err := json.Unmarshal(r.AccountInfo, &info); err == nil {
|
||||
if v, ok := info["account_name"].(string); ok {
|
||||
accountName = v
|
||||
}
|
||||
if v, ok := info["account_number"].(string); ok {
|
||||
accountNumber = v
|
||||
}
|
||||
if v, ok := info["bank_name"].(string); ok {
|
||||
bankName = v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var processedAt string
|
||||
if r.ProcessedAt != nil {
|
||||
processedAt = r.ProcessedAt.Format("2006-01-02 15:04:05")
|
||||
}
|
||||
|
||||
return model.WithdrawalRequestItem{
|
||||
ID: r.ID,
|
||||
WithdrawalNo: r.WithdrawalNo,
|
||||
Amount: r.Amount,
|
||||
FeeRate: r.FeeRate,
|
||||
Fee: r.Fee,
|
||||
ActualAmount: r.ActualAmount,
|
||||
Status: r.Status,
|
||||
StatusName: getWithdrawalStatusName(r.Status),
|
||||
ShopID: r.ShopID,
|
||||
ShopName: shopName,
|
||||
ShopHierarchy: shopHierarchy,
|
||||
ApplicantID: r.ApplicantID,
|
||||
ApplicantName: applicantMap[r.ApplicantID],
|
||||
ProcessorID: processorID,
|
||||
ProcessorName: processorMap[r.ProcessorID],
|
||||
WithdrawalMethod: r.WithdrawalMethod,
|
||||
PaymentType: r.PaymentType,
|
||||
AccountName: accountName,
|
||||
AccountNumber: accountNumber,
|
||||
BankName: bankName,
|
||||
RejectReason: r.RejectReason,
|
||||
Remark: r.Remark,
|
||||
CreatedAt: r.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
ProcessedAt: processedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func getWithdrawalStatusName(status int) string {
|
||||
switch status {
|
||||
case constants.WithdrawalStatusPending:
|
||||
return "待审核"
|
||||
case constants.WithdrawalStatusApproved:
|
||||
return "已通过"
|
||||
case constants.WithdrawalStatusRejected:
|
||||
return "已拒绝"
|
||||
case constants.WithdrawalStatusPaid:
|
||||
return "已到账"
|
||||
default:
|
||||
return "未知"
|
||||
}
|
||||
}
|
||||
|
||||
func containsSubstring(s, substr string) bool {
|
||||
for i := 0; i <= len(s)-len(substr); i++ {
|
||||
if s[i:i+len(substr)] == substr {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
161
internal/service/commission_withdrawal_setting/service.go
Normal file
161
internal/service/commission_withdrawal_setting/service.go
Normal file
@@ -0,0 +1,161 @@
|
||||
package commission_withdrawal_setting
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/middleware"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
db *gorm.DB
|
||||
accountStore *postgres.AccountStore
|
||||
commissionWithdrawalSettingStore *postgres.CommissionWithdrawalSettingStore
|
||||
}
|
||||
|
||||
func New(
|
||||
db *gorm.DB,
|
||||
accountStore *postgres.AccountStore,
|
||||
commissionWithdrawalSettingStore *postgres.CommissionWithdrawalSettingStore,
|
||||
) *Service {
|
||||
return &Service{
|
||||
db: db,
|
||||
accountStore: accountStore,
|
||||
commissionWithdrawalSettingStore: commissionWithdrawalSettingStore,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) Create(ctx context.Context, req *model.CreateWithdrawalSettingReq) (*model.WithdrawalSettingItem, error) {
|
||||
currentUserID := middleware.GetUserIDFromContext(ctx)
|
||||
if currentUserID == 0 {
|
||||
return nil, errors.New(errors.CodeUnauthorized, "未授权访问")
|
||||
}
|
||||
|
||||
setting := &model.CommissionWithdrawalSetting{
|
||||
DailyWithdrawalLimit: req.DailyWithdrawalLimit,
|
||||
MinWithdrawalAmount: req.MinWithdrawalAmount,
|
||||
FeeRate: req.FeeRate,
|
||||
IsActive: true,
|
||||
}
|
||||
setting.Creator = currentUserID
|
||||
setting.Updater = currentUserID
|
||||
|
||||
err := s.db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := s.commissionWithdrawalSettingStore.DeactivateCurrentWithTx(ctx, tx); err != nil {
|
||||
return fmt.Errorf("失效旧配置失败: %w", err)
|
||||
}
|
||||
if err := s.commissionWithdrawalSettingStore.CreateWithTx(ctx, tx, setting); err != nil {
|
||||
return fmt.Errorf("创建配置失败: %w", err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
creatorName := ""
|
||||
if creator, err := s.accountStore.GetByID(ctx, currentUserID); err == nil {
|
||||
creatorName = creator.Username
|
||||
}
|
||||
|
||||
return &model.WithdrawalSettingItem{
|
||||
ID: setting.ID,
|
||||
DailyWithdrawalLimit: setting.DailyWithdrawalLimit,
|
||||
MinWithdrawalAmount: setting.MinWithdrawalAmount,
|
||||
FeeRate: setting.FeeRate,
|
||||
ArrivalDays: setting.ArrivalDays,
|
||||
IsActive: setting.IsActive,
|
||||
CreatorID: setting.Creator,
|
||||
CreatorName: creatorName,
|
||||
CreatedAt: setting.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) List(ctx context.Context, req *model.WithdrawalSettingListReq) (*model.WithdrawalSettingPageResult, error) {
|
||||
opts := &store.QueryOptions{
|
||||
Page: req.Page,
|
||||
PageSize: req.PageSize,
|
||||
}
|
||||
if opts.Page == 0 {
|
||||
opts.Page = 1
|
||||
}
|
||||
if opts.PageSize == 0 {
|
||||
opts.PageSize = constants.DefaultPageSize
|
||||
}
|
||||
|
||||
settings, total, err := s.commissionWithdrawalSettingStore.List(ctx, opts)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询配置列表失败: %w", err)
|
||||
}
|
||||
|
||||
creatorIDs := make([]uint, 0)
|
||||
for _, setting := range settings {
|
||||
if setting.Creator > 0 {
|
||||
creatorIDs = append(creatorIDs, setting.Creator)
|
||||
}
|
||||
}
|
||||
|
||||
creatorMap := make(map[uint]string)
|
||||
if len(creatorIDs) > 0 {
|
||||
accounts, _ := s.accountStore.GetByIDs(ctx, creatorIDs)
|
||||
for _, acc := range accounts {
|
||||
creatorMap[acc.ID] = acc.Username
|
||||
}
|
||||
}
|
||||
|
||||
items := make([]model.WithdrawalSettingItem, 0, len(settings))
|
||||
for _, setting := range settings {
|
||||
items = append(items, model.WithdrawalSettingItem{
|
||||
ID: setting.ID,
|
||||
DailyWithdrawalLimit: setting.DailyWithdrawalLimit,
|
||||
MinWithdrawalAmount: setting.MinWithdrawalAmount,
|
||||
FeeRate: setting.FeeRate,
|
||||
ArrivalDays: setting.ArrivalDays,
|
||||
IsActive: setting.IsActive,
|
||||
CreatorID: setting.Creator,
|
||||
CreatorName: creatorMap[setting.Creator],
|
||||
CreatedAt: setting.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
}
|
||||
|
||||
return &model.WithdrawalSettingPageResult{
|
||||
Items: items,
|
||||
Total: total,
|
||||
Page: opts.Page,
|
||||
Size: opts.PageSize,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetCurrent(ctx context.Context) (*model.WithdrawalSettingItem, error) {
|
||||
setting, err := s.commissionWithdrawalSettingStore.GetCurrent(ctx)
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, errors.New(errors.CodeNotFound, "暂无生效的提现配置")
|
||||
}
|
||||
return nil, fmt.Errorf("查询当前配置失败: %w", err)
|
||||
}
|
||||
|
||||
creatorName := ""
|
||||
if creator, err := s.accountStore.GetByID(ctx, setting.Creator); err == nil {
|
||||
creatorName = creator.Username
|
||||
}
|
||||
|
||||
return &model.WithdrawalSettingItem{
|
||||
ID: setting.ID,
|
||||
DailyWithdrawalLimit: setting.DailyWithdrawalLimit,
|
||||
MinWithdrawalAmount: setting.MinWithdrawalAmount,
|
||||
FeeRate: setting.FeeRate,
|
||||
ArrivalDays: setting.ArrivalDays,
|
||||
IsActive: setting.IsActive,
|
||||
CreatorID: setting.Creator,
|
||||
CreatorName: creatorName,
|
||||
CreatedAt: setting.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
}, nil
|
||||
}
|
||||
328
internal/service/customer_account/service.go
Normal file
328
internal/service/customer_account/service.go
Normal file
@@ -0,0 +1,328 @@
|
||||
package customer_account
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/middleware"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
db *gorm.DB
|
||||
accountStore *postgres.AccountStore
|
||||
shopStore *postgres.ShopStore
|
||||
enterpriseStore *postgres.EnterpriseStore
|
||||
}
|
||||
|
||||
func New(
|
||||
db *gorm.DB,
|
||||
accountStore *postgres.AccountStore,
|
||||
shopStore *postgres.ShopStore,
|
||||
enterpriseStore *postgres.EnterpriseStore,
|
||||
) *Service {
|
||||
return &Service{
|
||||
db: db,
|
||||
accountStore: accountStore,
|
||||
shopStore: shopStore,
|
||||
enterpriseStore: enterpriseStore,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) List(ctx context.Context, req *model.CustomerAccountListReq) (*model.CustomerAccountPageResult, error) {
|
||||
page := req.Page
|
||||
pageSize := req.PageSize
|
||||
if page == 0 {
|
||||
page = 1
|
||||
}
|
||||
if pageSize == 0 {
|
||||
pageSize = constants.DefaultPageSize
|
||||
}
|
||||
|
||||
query := s.db.WithContext(ctx).Model(&model.Account{}).
|
||||
Where("user_type IN ?", []int{constants.UserTypeAgent, constants.UserTypeEnterprise})
|
||||
|
||||
if req.Username != "" {
|
||||
query = query.Where("username LIKE ?", "%"+req.Username+"%")
|
||||
}
|
||||
if req.Phone != "" {
|
||||
query = query.Where("phone LIKE ?", "%"+req.Phone+"%")
|
||||
}
|
||||
if req.UserType != nil {
|
||||
query = query.Where("user_type = ?", *req.UserType)
|
||||
}
|
||||
if req.ShopID != nil {
|
||||
query = query.Where("shop_id = ?", *req.ShopID)
|
||||
}
|
||||
if req.EnterpriseID != nil {
|
||||
query = query.Where("enterprise_id = ?", *req.EnterpriseID)
|
||||
}
|
||||
if req.Status != nil {
|
||||
query = query.Where("status = ?", *req.Status)
|
||||
}
|
||||
|
||||
var total int64
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, fmt.Errorf("统计账号数量失败: %w", err)
|
||||
}
|
||||
|
||||
var accounts []model.Account
|
||||
offset := (page - 1) * pageSize
|
||||
if err := query.Offset(offset).Limit(pageSize).Order("created_at DESC").Find(&accounts).Error; err != nil {
|
||||
return nil, fmt.Errorf("查询账号列表失败: %w", err)
|
||||
}
|
||||
|
||||
shopIDs := make([]uint, 0)
|
||||
enterpriseIDs := make([]uint, 0)
|
||||
for _, acc := range accounts {
|
||||
if acc.ShopID != nil {
|
||||
shopIDs = append(shopIDs, *acc.ShopID)
|
||||
}
|
||||
if acc.EnterpriseID != nil {
|
||||
enterpriseIDs = append(enterpriseIDs, *acc.EnterpriseID)
|
||||
}
|
||||
}
|
||||
|
||||
shopMap := make(map[uint]string)
|
||||
if len(shopIDs) > 0 {
|
||||
var shops []model.Shop
|
||||
s.db.WithContext(ctx).Where("id IN ?", shopIDs).Find(&shops)
|
||||
for _, shop := range shops {
|
||||
shopMap[shop.ID] = shop.ShopName
|
||||
}
|
||||
}
|
||||
|
||||
enterpriseMap := make(map[uint]string)
|
||||
if len(enterpriseIDs) > 0 {
|
||||
var enterprises []model.Enterprise
|
||||
s.db.WithContext(ctx).Where("id IN ?", enterpriseIDs).Find(&enterprises)
|
||||
for _, ent := range enterprises {
|
||||
enterpriseMap[ent.ID] = ent.EnterpriseName
|
||||
}
|
||||
}
|
||||
|
||||
items := make([]model.CustomerAccountItem, 0, len(accounts))
|
||||
for _, acc := range accounts {
|
||||
shopName := ""
|
||||
if acc.ShopID != nil {
|
||||
shopName = shopMap[*acc.ShopID]
|
||||
}
|
||||
enterpriseName := ""
|
||||
if acc.EnterpriseID != nil {
|
||||
enterpriseName = enterpriseMap[*acc.EnterpriseID]
|
||||
}
|
||||
items = append(items, model.CustomerAccountItem{
|
||||
ID: acc.ID,
|
||||
Username: acc.Username,
|
||||
Phone: acc.Phone,
|
||||
UserType: acc.UserType,
|
||||
UserTypeName: getUserTypeName(acc.UserType),
|
||||
ShopID: acc.ShopID,
|
||||
ShopName: shopName,
|
||||
EnterpriseID: acc.EnterpriseID,
|
||||
EnterpriseName: enterpriseName,
|
||||
Status: acc.Status,
|
||||
StatusName: getStatusName(acc.Status),
|
||||
CreatedAt: acc.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
}
|
||||
|
||||
return &model.CustomerAccountPageResult{
|
||||
Items: items,
|
||||
Total: total,
|
||||
Page: page,
|
||||
Size: pageSize,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) Create(ctx context.Context, req *model.CreateCustomerAccountReq) (*model.CustomerAccountItem, error) {
|
||||
currentUserID := middleware.GetUserIDFromContext(ctx)
|
||||
if currentUserID == 0 {
|
||||
return nil, errors.New(errors.CodeUnauthorized, "未授权访问")
|
||||
}
|
||||
|
||||
_, err := s.shopStore.GetByID(ctx, req.ShopID)
|
||||
if err != nil {
|
||||
return nil, errors.New(errors.CodeShopNotFound, "店铺不存在")
|
||||
}
|
||||
|
||||
existingAccount, _ := s.accountStore.GetByPhone(ctx, req.Phone)
|
||||
if existingAccount != nil {
|
||||
return nil, errors.New(errors.CodePhoneExists, "手机号已被使用")
|
||||
}
|
||||
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("密码加密失败: %w", err)
|
||||
}
|
||||
|
||||
account := &model.Account{
|
||||
Username: req.Username,
|
||||
Phone: req.Phone,
|
||||
Password: string(hashedPassword),
|
||||
UserType: constants.UserTypeAgent,
|
||||
ShopID: &req.ShopID,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
account.Creator = currentUserID
|
||||
account.Updater = currentUserID
|
||||
|
||||
if err := s.db.WithContext(ctx).Create(account).Error; err != nil {
|
||||
return nil, fmt.Errorf("创建账号失败: %w", err)
|
||||
}
|
||||
|
||||
shop, _ := s.shopStore.GetByID(ctx, req.ShopID)
|
||||
shopName := ""
|
||||
if shop != nil {
|
||||
shopName = shop.ShopName
|
||||
}
|
||||
|
||||
return &model.CustomerAccountItem{
|
||||
ID: account.ID,
|
||||
Username: account.Username,
|
||||
Phone: account.Phone,
|
||||
UserType: account.UserType,
|
||||
UserTypeName: getUserTypeName(account.UserType),
|
||||
ShopID: account.ShopID,
|
||||
ShopName: shopName,
|
||||
Status: account.Status,
|
||||
StatusName: getStatusName(account.Status),
|
||||
CreatedAt: account.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) Update(ctx context.Context, id uint, req *model.UpdateCustomerAccountReq) (*model.CustomerAccountItem, error) {
|
||||
currentUserID := middleware.GetUserIDFromContext(ctx)
|
||||
if currentUserID == 0 {
|
||||
return nil, errors.New(errors.CodeUnauthorized, "未授权访问")
|
||||
}
|
||||
|
||||
account, err := s.accountStore.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
return nil, errors.New(errors.CodeAccountNotFound, "账号不存在")
|
||||
}
|
||||
|
||||
if account.UserType != constants.UserTypeAgent && account.UserType != constants.UserTypeEnterprise {
|
||||
return nil, errors.New(errors.CodeForbidden, "无权限操作此账号")
|
||||
}
|
||||
|
||||
if req.Username != nil {
|
||||
account.Username = *req.Username
|
||||
}
|
||||
if req.Phone != nil {
|
||||
if *req.Phone != account.Phone {
|
||||
existingAccount, _ := s.accountStore.GetByPhone(ctx, *req.Phone)
|
||||
if existingAccount != nil && existingAccount.ID != id {
|
||||
return nil, errors.New(errors.CodePhoneExists, "手机号已被使用")
|
||||
}
|
||||
account.Phone = *req.Phone
|
||||
}
|
||||
}
|
||||
account.Updater = currentUserID
|
||||
|
||||
if err := s.db.WithContext(ctx).Save(account).Error; err != nil {
|
||||
return nil, fmt.Errorf("更新账号失败: %w", err)
|
||||
}
|
||||
|
||||
shopName := ""
|
||||
if account.ShopID != nil {
|
||||
if shop, _ := s.shopStore.GetByID(ctx, *account.ShopID); shop != nil {
|
||||
shopName = shop.ShopName
|
||||
}
|
||||
}
|
||||
enterpriseName := ""
|
||||
if account.EnterpriseID != nil {
|
||||
if ent, _ := s.enterpriseStore.GetByID(ctx, *account.EnterpriseID); ent != nil {
|
||||
enterpriseName = ent.EnterpriseName
|
||||
}
|
||||
}
|
||||
|
||||
return &model.CustomerAccountItem{
|
||||
ID: account.ID,
|
||||
Username: account.Username,
|
||||
Phone: account.Phone,
|
||||
UserType: account.UserType,
|
||||
UserTypeName: getUserTypeName(account.UserType),
|
||||
ShopID: account.ShopID,
|
||||
ShopName: shopName,
|
||||
EnterpriseID: account.EnterpriseID,
|
||||
EnterpriseName: enterpriseName,
|
||||
Status: account.Status,
|
||||
StatusName: getStatusName(account.Status),
|
||||
CreatedAt: account.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) UpdatePassword(ctx context.Context, id uint, password string) error {
|
||||
currentUserID := middleware.GetUserIDFromContext(ctx)
|
||||
if currentUserID == 0 {
|
||||
return errors.New(errors.CodeUnauthorized, "未授权访问")
|
||||
}
|
||||
|
||||
account, err := s.accountStore.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
return errors.New(errors.CodeAccountNotFound, "账号不存在")
|
||||
}
|
||||
|
||||
if account.UserType != constants.UserTypeAgent && account.UserType != constants.UserTypeEnterprise {
|
||||
return errors.New(errors.CodeForbidden, "无权限操作此账号")
|
||||
}
|
||||
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return fmt.Errorf("密码加密失败: %w", err)
|
||||
}
|
||||
|
||||
return s.db.WithContext(ctx).Model(&model.Account{}).
|
||||
Where("id = ?", id).
|
||||
Updates(map[string]interface{}{
|
||||
"password": string(hashedPassword),
|
||||
"updater": currentUserID,
|
||||
}).Error
|
||||
}
|
||||
|
||||
func (s *Service) UpdateStatus(ctx context.Context, id uint, status int) error {
|
||||
currentUserID := middleware.GetUserIDFromContext(ctx)
|
||||
if currentUserID == 0 {
|
||||
return errors.New(errors.CodeUnauthorized, "未授权访问")
|
||||
}
|
||||
|
||||
account, err := s.accountStore.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
return errors.New(errors.CodeAccountNotFound, "账号不存在")
|
||||
}
|
||||
|
||||
if account.UserType != constants.UserTypeAgent && account.UserType != constants.UserTypeEnterprise {
|
||||
return errors.New(errors.CodeForbidden, "无权限操作此账号")
|
||||
}
|
||||
|
||||
return s.db.WithContext(ctx).Model(&model.Account{}).
|
||||
Where("id = ?", id).
|
||||
Updates(map[string]interface{}{
|
||||
"status": status,
|
||||
"updater": currentUserID,
|
||||
}).Error
|
||||
}
|
||||
|
||||
func getUserTypeName(userType int) string {
|
||||
switch userType {
|
||||
case constants.UserTypeAgent:
|
||||
return "代理账号"
|
||||
case constants.UserTypeEnterprise:
|
||||
return "企业账号"
|
||||
default:
|
||||
return "未知"
|
||||
}
|
||||
}
|
||||
|
||||
func getStatusName(status int) string {
|
||||
if status == constants.StatusEnabled {
|
||||
return "启用"
|
||||
}
|
||||
return "禁用"
|
||||
}
|
||||
@@ -1,9 +1,8 @@
|
||||
// Package enterprise 提供企业管理的业务逻辑服务
|
||||
// 包含企业创建、查询、更新、删除等功能
|
||||
package enterprise
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store"
|
||||
@@ -11,39 +10,44 @@ import (
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/middleware"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Service 企业业务服务
|
||||
type Service struct {
|
||||
db *gorm.DB
|
||||
enterpriseStore *postgres.EnterpriseStore
|
||||
shopStore *postgres.ShopStore
|
||||
accountStore *postgres.AccountStore
|
||||
}
|
||||
|
||||
// New 创建企业服务
|
||||
func New(enterpriseStore *postgres.EnterpriseStore, shopStore *postgres.ShopStore) *Service {
|
||||
func New(db *gorm.DB, enterpriseStore *postgres.EnterpriseStore, shopStore *postgres.ShopStore, accountStore *postgres.AccountStore) *Service {
|
||||
return &Service{
|
||||
db: db,
|
||||
enterpriseStore: enterpriseStore,
|
||||
shopStore: shopStore,
|
||||
accountStore: accountStore,
|
||||
}
|
||||
}
|
||||
|
||||
// Create 创建企业
|
||||
func (s *Service) Create(ctx context.Context, req *model.CreateEnterpriseRequest) (*model.Enterprise, error) {
|
||||
// 获取当前用户 ID
|
||||
func (s *Service) Create(ctx context.Context, req *model.CreateEnterpriseReq) (*model.CreateEnterpriseResp, error) {
|
||||
currentUserID := middleware.GetUserIDFromContext(ctx)
|
||||
if currentUserID == 0 {
|
||||
return nil, errors.New(errors.CodeUnauthorized, "未授权访问")
|
||||
}
|
||||
|
||||
// 检查企业编号唯一性
|
||||
if req.EnterpriseCode != "" {
|
||||
existing, err := s.enterpriseStore.GetByCode(ctx, req.EnterpriseCode)
|
||||
if err == nil && existing != nil {
|
||||
existing, _ := s.enterpriseStore.GetByCode(ctx, req.EnterpriseCode)
|
||||
if existing != nil {
|
||||
return nil, errors.New(errors.CodeEnterpriseCodeExists, "企业编号已存在")
|
||||
}
|
||||
}
|
||||
|
||||
// 验证归属店铺存在(如果提供)
|
||||
existingAccount, _ := s.accountStore.GetByPhone(ctx, req.LoginPhone)
|
||||
if existingAccount != nil {
|
||||
return nil, errors.New(errors.CodePhoneExists, "手机号已被使用")
|
||||
}
|
||||
|
||||
if req.OwnerShopID != nil {
|
||||
_, err := s.shopStore.GetByID(ctx, *req.OwnerShopID)
|
||||
if err != nil {
|
||||
@@ -51,29 +55,87 @@ func (s *Service) Create(ctx context.Context, req *model.CreateEnterpriseRequest
|
||||
}
|
||||
}
|
||||
|
||||
// 创建企业
|
||||
enterprise := &model.Enterprise{
|
||||
EnterpriseName: req.EnterpriseName,
|
||||
EnterpriseCode: req.EnterpriseCode,
|
||||
OwnerShopID: req.OwnerShopID,
|
||||
LegalPerson: req.LegalPerson,
|
||||
ContactName: req.ContactName,
|
||||
ContactPhone: req.ContactPhone,
|
||||
BusinessLicense: req.BusinessLicense,
|
||||
Province: req.Province,
|
||||
City: req.City,
|
||||
District: req.District,
|
||||
Address: req.Address,
|
||||
Status: constants.StatusEnabled,
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("密码加密失败: %w", err)
|
||||
}
|
||||
enterprise.Creator = currentUserID
|
||||
enterprise.Updater = currentUserID
|
||||
|
||||
if err := s.enterpriseStore.Create(ctx, enterprise); err != nil {
|
||||
var enterprise *model.Enterprise
|
||||
var account *model.Account
|
||||
|
||||
err = s.db.Transaction(func(tx *gorm.DB) error {
|
||||
enterprise = &model.Enterprise{
|
||||
EnterpriseName: req.EnterpriseName,
|
||||
EnterpriseCode: req.EnterpriseCode,
|
||||
OwnerShopID: req.OwnerShopID,
|
||||
LegalPerson: req.LegalPerson,
|
||||
ContactName: req.ContactName,
|
||||
ContactPhone: req.ContactPhone,
|
||||
BusinessLicense: req.BusinessLicense,
|
||||
Province: req.Province,
|
||||
City: req.City,
|
||||
District: req.District,
|
||||
Address: req.Address,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
enterprise.Creator = currentUserID
|
||||
enterprise.Updater = currentUserID
|
||||
|
||||
if err := tx.WithContext(ctx).Create(enterprise).Error; err != nil {
|
||||
return fmt.Errorf("创建企业失败: %w", err)
|
||||
}
|
||||
|
||||
account = &model.Account{
|
||||
Username: req.EnterpriseName,
|
||||
Phone: req.LoginPhone,
|
||||
Password: string(hashedPassword),
|
||||
UserType: constants.UserTypeEnterprise,
|
||||
EnterpriseID: &enterprise.ID,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
account.Creator = currentUserID
|
||||
account.Updater = currentUserID
|
||||
|
||||
if err := tx.WithContext(ctx).Create(account).Error; err != nil {
|
||||
return fmt.Errorf("创建企业账号失败: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return enterprise, nil
|
||||
ownerShopName := ""
|
||||
if enterprise.OwnerShopID != nil {
|
||||
if shop, err := s.shopStore.GetByID(ctx, *enterprise.OwnerShopID); err == nil {
|
||||
ownerShopName = shop.ShopName
|
||||
}
|
||||
}
|
||||
|
||||
return &model.CreateEnterpriseResp{
|
||||
Enterprise: model.EnterpriseItem{
|
||||
ID: enterprise.ID,
|
||||
EnterpriseName: enterprise.EnterpriseName,
|
||||
EnterpriseCode: enterprise.EnterpriseCode,
|
||||
OwnerShopID: enterprise.OwnerShopID,
|
||||
OwnerShopName: ownerShopName,
|
||||
LegalPerson: enterprise.LegalPerson,
|
||||
ContactName: enterprise.ContactName,
|
||||
ContactPhone: enterprise.ContactPhone,
|
||||
LoginPhone: req.LoginPhone,
|
||||
BusinessLicense: enterprise.BusinessLicense,
|
||||
Province: enterprise.Province,
|
||||
City: enterprise.City,
|
||||
District: enterprise.District,
|
||||
Address: enterprise.Address,
|
||||
Status: enterprise.Status,
|
||||
StatusName: getStatusName(enterprise.Status),
|
||||
CreatedAt: enterprise.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
},
|
||||
AccountID: account.ID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Update 更新企业信息
|
||||
@@ -137,8 +199,7 @@ func (s *Service) Update(ctx context.Context, id uint, req *model.UpdateEnterpri
|
||||
return enterprise, nil
|
||||
}
|
||||
|
||||
// Disable 禁用企业
|
||||
func (s *Service) Disable(ctx context.Context, id uint) error {
|
||||
func (s *Service) UpdateStatus(ctx context.Context, id uint, status int) error {
|
||||
currentUserID := middleware.GetUserIDFromContext(ctx)
|
||||
if currentUserID == 0 {
|
||||
return errors.New(errors.CodeUnauthorized, "未授权访问")
|
||||
@@ -149,31 +210,50 @@ func (s *Service) Disable(ctx context.Context, id uint) error {
|
||||
return errors.New(errors.CodeEnterpriseNotFound, "企业不存在")
|
||||
}
|
||||
|
||||
enterprise.Status = constants.StatusDisabled
|
||||
enterprise.Updater = currentUserID
|
||||
return s.db.Transaction(func(tx *gorm.DB) error {
|
||||
enterprise.Status = status
|
||||
enterprise.Updater = currentUserID
|
||||
if err := tx.WithContext(ctx).Save(enterprise).Error; err != nil {
|
||||
return fmt.Errorf("更新企业状态失败: %w", err)
|
||||
}
|
||||
|
||||
return s.enterpriseStore.Update(ctx, enterprise)
|
||||
if err := tx.WithContext(ctx).Model(&model.Account{}).
|
||||
Where("enterprise_id = ?", id).
|
||||
Updates(map[string]interface{}{
|
||||
"status": status,
|
||||
"updater": currentUserID,
|
||||
}).Error; err != nil {
|
||||
return fmt.Errorf("同步更新企业账号状态失败: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// Enable 启用企业
|
||||
func (s *Service) Enable(ctx context.Context, id uint) error {
|
||||
func (s *Service) UpdatePassword(ctx context.Context, id uint, password string) error {
|
||||
currentUserID := middleware.GetUserIDFromContext(ctx)
|
||||
if currentUserID == 0 {
|
||||
return errors.New(errors.CodeUnauthorized, "未授权访问")
|
||||
}
|
||||
|
||||
enterprise, err := s.enterpriseStore.GetByID(ctx, id)
|
||||
_, err := s.enterpriseStore.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
return errors.New(errors.CodeEnterpriseNotFound, "企业不存在")
|
||||
}
|
||||
|
||||
enterprise.Status = constants.StatusEnabled
|
||||
enterprise.Updater = currentUserID
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return fmt.Errorf("密码加密失败: %w", err)
|
||||
}
|
||||
|
||||
return s.enterpriseStore.Update(ctx, enterprise)
|
||||
return s.db.WithContext(ctx).Model(&model.Account{}).
|
||||
Where("enterprise_id = ?", id).
|
||||
Updates(map[string]interface{}{
|
||||
"password": string(hashedPassword),
|
||||
"updater": currentUserID,
|
||||
}).Error
|
||||
}
|
||||
|
||||
// GetByID 获取企业详情
|
||||
func (s *Service) GetByID(ctx context.Context, id uint) (*model.Enterprise, error) {
|
||||
enterprise, err := s.enterpriseStore.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
@@ -182,7 +262,104 @@ func (s *Service) GetByID(ctx context.Context, id uint) (*model.Enterprise, erro
|
||||
return enterprise, nil
|
||||
}
|
||||
|
||||
// List 查询企业列表
|
||||
func (s *Service) List(ctx context.Context, opts *store.QueryOptions, filters map[string]interface{}) ([]*model.Enterprise, int64, error) {
|
||||
return s.enterpriseStore.List(ctx, opts, filters)
|
||||
func (s *Service) List(ctx context.Context, req *model.EnterpriseListReq) (*model.EnterprisePageResult, error) {
|
||||
opts := &store.QueryOptions{
|
||||
Page: req.Page,
|
||||
PageSize: req.PageSize,
|
||||
}
|
||||
if opts.Page == 0 {
|
||||
opts.Page = 1
|
||||
}
|
||||
if opts.PageSize == 0 {
|
||||
opts.PageSize = constants.DefaultPageSize
|
||||
}
|
||||
|
||||
filters := make(map[string]interface{})
|
||||
if req.EnterpriseName != "" {
|
||||
filters["enterprise_name"] = req.EnterpriseName
|
||||
}
|
||||
if req.ContactPhone != "" {
|
||||
filters["contact_phone"] = req.ContactPhone
|
||||
}
|
||||
if req.OwnerShopID != nil {
|
||||
filters["owner_shop_id"] = *req.OwnerShopID
|
||||
}
|
||||
if req.Status != nil {
|
||||
filters["status"] = *req.Status
|
||||
}
|
||||
|
||||
enterprises, total, err := s.enterpriseStore.List(ctx, opts, filters)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询企业列表失败: %w", err)
|
||||
}
|
||||
|
||||
enterpriseIDs := make([]uint, 0, len(enterprises))
|
||||
shopIDs := make([]uint, 0)
|
||||
for _, e := range enterprises {
|
||||
enterpriseIDs = append(enterpriseIDs, e.ID)
|
||||
if e.OwnerShopID != nil {
|
||||
shopIDs = append(shopIDs, *e.OwnerShopID)
|
||||
}
|
||||
}
|
||||
|
||||
accountMap := make(map[uint]string)
|
||||
if len(enterpriseIDs) > 0 {
|
||||
var accounts []model.Account
|
||||
s.db.WithContext(ctx).Where("enterprise_id IN ?", enterpriseIDs).Find(&accounts)
|
||||
for _, acc := range accounts {
|
||||
if acc.EnterpriseID != nil {
|
||||
accountMap[*acc.EnterpriseID] = acc.Phone
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
shopMap := make(map[uint]string)
|
||||
if len(shopIDs) > 0 {
|
||||
var shops []model.Shop
|
||||
s.db.WithContext(ctx).Where("id IN ?", shopIDs).Find(&shops)
|
||||
for _, shop := range shops {
|
||||
shopMap[shop.ID] = shop.ShopName
|
||||
}
|
||||
}
|
||||
|
||||
items := make([]model.EnterpriseItem, 0, len(enterprises))
|
||||
for _, e := range enterprises {
|
||||
ownerShopName := ""
|
||||
if e.OwnerShopID != nil {
|
||||
ownerShopName = shopMap[*e.OwnerShopID]
|
||||
}
|
||||
items = append(items, model.EnterpriseItem{
|
||||
ID: e.ID,
|
||||
EnterpriseName: e.EnterpriseName,
|
||||
EnterpriseCode: e.EnterpriseCode,
|
||||
OwnerShopID: e.OwnerShopID,
|
||||
OwnerShopName: ownerShopName,
|
||||
LegalPerson: e.LegalPerson,
|
||||
ContactName: e.ContactName,
|
||||
ContactPhone: e.ContactPhone,
|
||||
LoginPhone: accountMap[e.ID],
|
||||
BusinessLicense: e.BusinessLicense,
|
||||
Province: e.Province,
|
||||
City: e.City,
|
||||
District: e.District,
|
||||
Address: e.Address,
|
||||
Status: e.Status,
|
||||
StatusName: getStatusName(e.Status),
|
||||
CreatedAt: e.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
}
|
||||
|
||||
return &model.EnterprisePageResult{
|
||||
Items: items,
|
||||
Total: total,
|
||||
Page: opts.Page,
|
||||
Size: opts.PageSize,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func getStatusName(status int) string {
|
||||
if status == constants.StatusEnabled {
|
||||
return "启用"
|
||||
}
|
||||
return "禁用"
|
||||
}
|
||||
|
||||
440
internal/service/enterprise_card/service.go
Normal file
440
internal/service/enterprise_card/service.go
Normal file
@@ -0,0 +1,440 @@
|
||||
package enterprise_card
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/middleware"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
db *gorm.DB
|
||||
enterpriseStore *postgres.EnterpriseStore
|
||||
enterpriseCardAuthStore *postgres.EnterpriseCardAuthorizationStore
|
||||
}
|
||||
|
||||
func New(
|
||||
db *gorm.DB,
|
||||
enterpriseStore *postgres.EnterpriseStore,
|
||||
enterpriseCardAuthStore *postgres.EnterpriseCardAuthorizationStore,
|
||||
) *Service {
|
||||
return &Service{
|
||||
db: db,
|
||||
enterpriseStore: enterpriseStore,
|
||||
enterpriseCardAuthStore: enterpriseCardAuthStore,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) AllocateCardsPreview(ctx context.Context, enterpriseID uint, req *model.AllocateCardsPreviewReq) (*model.AllocateCardsPreviewResp, error) {
|
||||
currentUserID := middleware.GetUserIDFromContext(ctx)
|
||||
if currentUserID == 0 {
|
||||
return nil, errors.New(errors.CodeUnauthorized, "未授权访问")
|
||||
}
|
||||
|
||||
_, err := s.enterpriseStore.GetByID(ctx, enterpriseID)
|
||||
if err != nil {
|
||||
return nil, errors.New(errors.CodeEnterpriseNotFound, "企业不存在")
|
||||
}
|
||||
|
||||
var iotCards []model.IotCard
|
||||
if err := s.db.WithContext(ctx).Where("iccid IN ?", req.ICCIDs).Find(&iotCards).Error; err != nil {
|
||||
return nil, fmt.Errorf("查询卡信息失败: %w", err)
|
||||
}
|
||||
|
||||
cardMap := make(map[string]*model.IotCard)
|
||||
cardIDMap := make(map[uint]*model.IotCard)
|
||||
for i := range iotCards {
|
||||
cardMap[iotCards[i].ICCID] = &iotCards[i]
|
||||
cardIDMap[iotCards[i].ID] = &iotCards[i]
|
||||
}
|
||||
|
||||
cardIDs := make([]uint, 0, len(iotCards))
|
||||
for _, card := range iotCards {
|
||||
cardIDs = append(cardIDs, card.ID)
|
||||
}
|
||||
|
||||
var bindings []model.DeviceSimBinding
|
||||
if len(cardIDs) > 0 {
|
||||
s.db.WithContext(ctx).Where("iot_card_id IN ? AND bind_status = 1", cardIDs).Find(&bindings)
|
||||
}
|
||||
|
||||
cardToDevice := make(map[uint]uint)
|
||||
deviceCards := make(map[uint][]uint)
|
||||
for _, binding := range bindings {
|
||||
cardToDevice[binding.IotCardID] = binding.DeviceID
|
||||
deviceCards[binding.DeviceID] = append(deviceCards[binding.DeviceID], binding.IotCardID)
|
||||
}
|
||||
|
||||
deviceIDs := make([]uint, 0, len(deviceCards))
|
||||
for deviceID := range deviceCards {
|
||||
deviceIDs = append(deviceIDs, deviceID)
|
||||
}
|
||||
var devices []model.Device
|
||||
deviceMap := make(map[uint]*model.Device)
|
||||
if len(deviceIDs) > 0 {
|
||||
s.db.WithContext(ctx).Where("id IN ?", deviceIDs).Find(&devices)
|
||||
for i := range devices {
|
||||
deviceMap[devices[i].ID] = &devices[i]
|
||||
}
|
||||
}
|
||||
|
||||
resp := &model.AllocateCardsPreviewResp{
|
||||
StandaloneCards: make([]model.StandaloneCard, 0),
|
||||
DeviceBundles: make([]model.DeviceBundle, 0),
|
||||
FailedItems: make([]model.FailedItem, 0),
|
||||
}
|
||||
|
||||
processedDevices := make(map[uint]bool)
|
||||
|
||||
for _, iccid := range req.ICCIDs {
|
||||
card, exists := cardMap[iccid]
|
||||
if !exists {
|
||||
resp.FailedItems = append(resp.FailedItems, model.FailedItem{
|
||||
ICCID: iccid,
|
||||
Reason: "卡不存在",
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
deviceID, hasDevice := cardToDevice[card.ID]
|
||||
if !hasDevice {
|
||||
resp.StandaloneCards = append(resp.StandaloneCards, model.StandaloneCard{
|
||||
ICCID: card.ICCID,
|
||||
IotCardID: card.ID,
|
||||
MSISDN: card.MSISDN,
|
||||
CarrierID: card.CarrierID,
|
||||
StatusName: getCardStatusName(card.Status),
|
||||
})
|
||||
} else {
|
||||
if processedDevices[deviceID] {
|
||||
continue
|
||||
}
|
||||
processedDevices[deviceID] = true
|
||||
|
||||
device := deviceMap[deviceID]
|
||||
if device == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
bundleCardIDs := deviceCards[deviceID]
|
||||
bundle := model.DeviceBundle{
|
||||
DeviceID: deviceID,
|
||||
DeviceNo: device.DeviceNo,
|
||||
BundleCards: make([]model.DeviceBundleCard, 0),
|
||||
}
|
||||
|
||||
for _, bundleCardID := range bundleCardIDs {
|
||||
bundleCard := cardIDMap[bundleCardID]
|
||||
if bundleCard == nil {
|
||||
continue
|
||||
}
|
||||
if bundleCard.ID == card.ID {
|
||||
bundle.TriggerCard = model.DeviceBundleCard{
|
||||
ICCID: bundleCard.ICCID,
|
||||
IotCardID: bundleCard.ID,
|
||||
MSISDN: bundleCard.MSISDN,
|
||||
}
|
||||
} else {
|
||||
bundle.BundleCards = append(bundle.BundleCards, model.DeviceBundleCard{
|
||||
ICCID: bundleCard.ICCID,
|
||||
IotCardID: bundleCard.ID,
|
||||
MSISDN: bundleCard.MSISDN,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
resp.DeviceBundles = append(resp.DeviceBundles, bundle)
|
||||
}
|
||||
}
|
||||
|
||||
deviceCardCount := 0
|
||||
for _, bundle := range resp.DeviceBundles {
|
||||
deviceCardCount += 1 + len(bundle.BundleCards)
|
||||
}
|
||||
|
||||
resp.Summary = model.AllocatePreviewSummary{
|
||||
StandaloneCardCount: len(resp.StandaloneCards),
|
||||
DeviceCount: len(resp.DeviceBundles),
|
||||
DeviceCardCount: deviceCardCount,
|
||||
TotalCardCount: len(resp.StandaloneCards) + deviceCardCount,
|
||||
FailedCount: len(resp.FailedItems),
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *Service) AllocateCards(ctx context.Context, enterpriseID uint, req *model.AllocateCardsReq) (*model.AllocateCardsResp, error) {
|
||||
currentUserID := middleware.GetUserIDFromContext(ctx)
|
||||
currentShopID := middleware.GetShopIDFromContext(ctx)
|
||||
if currentUserID == 0 {
|
||||
return nil, errors.New(errors.CodeUnauthorized, "未授权访问")
|
||||
}
|
||||
|
||||
_, err := s.enterpriseStore.GetByID(ctx, enterpriseID)
|
||||
if err != nil {
|
||||
return nil, errors.New(errors.CodeEnterpriseNotFound, "企业不存在")
|
||||
}
|
||||
|
||||
preview, err := s.AllocateCardsPreview(ctx, enterpriseID, &model.AllocateCardsPreviewReq{ICCIDs: req.ICCIDs})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(preview.DeviceBundles) > 0 && !req.ConfirmDeviceBundles {
|
||||
return nil, errors.New(errors.CodeInvalidParam, "存在设备包,请确认整体授权设备下所有卡")
|
||||
}
|
||||
|
||||
resp := &model.AllocateCardsResp{
|
||||
FailedItems: preview.FailedItems,
|
||||
FailCount: len(preview.FailedItems),
|
||||
AllocatedDevices: make([]model.AllocatedDevice, 0),
|
||||
}
|
||||
|
||||
cardIDsToAllocate := make([]uint, 0)
|
||||
for _, card := range preview.StandaloneCards {
|
||||
cardIDsToAllocate = append(cardIDsToAllocate, card.IotCardID)
|
||||
}
|
||||
for _, bundle := range preview.DeviceBundles {
|
||||
cardIDsToAllocate = append(cardIDsToAllocate, bundle.TriggerCard.IotCardID)
|
||||
for _, card := range bundle.BundleCards {
|
||||
cardIDsToAllocate = append(cardIDsToAllocate, card.IotCardID)
|
||||
}
|
||||
iccids := []string{bundle.TriggerCard.ICCID}
|
||||
for _, card := range bundle.BundleCards {
|
||||
iccids = append(iccids, card.ICCID)
|
||||
}
|
||||
resp.AllocatedDevices = append(resp.AllocatedDevices, model.AllocatedDevice{
|
||||
DeviceID: bundle.DeviceID,
|
||||
DeviceNo: bundle.DeviceNo,
|
||||
CardCount: 1 + len(bundle.BundleCards),
|
||||
ICCIDs: iccids,
|
||||
})
|
||||
}
|
||||
|
||||
existingAuths, err := s.enterpriseCardAuthStore.GetActiveAuthsByCardIDs(ctx, enterpriseID, cardIDsToAllocate)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询已有授权失败: %w", err)
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
auths := make([]*model.EnterpriseCardAuthorization, 0)
|
||||
for _, cardID := range cardIDsToAllocate {
|
||||
if existingAuths[cardID] {
|
||||
continue
|
||||
}
|
||||
auths = append(auths, &model.EnterpriseCardAuthorization{
|
||||
EnterpriseID: enterpriseID,
|
||||
IotCardID: cardID,
|
||||
ShopID: currentShopID,
|
||||
AuthorizedBy: currentUserID,
|
||||
AuthorizedAt: &now,
|
||||
Status: 1,
|
||||
})
|
||||
}
|
||||
|
||||
if len(auths) > 0 {
|
||||
if err := s.enterpriseCardAuthStore.BatchCreate(ctx, auths); err != nil {
|
||||
return nil, fmt.Errorf("创建授权记录失败: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
resp.SuccessCount = len(cardIDsToAllocate)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *Service) RecallCards(ctx context.Context, enterpriseID uint, req *model.RecallCardsReq) (*model.RecallCardsResp, error) {
|
||||
currentUserID := middleware.GetUserIDFromContext(ctx)
|
||||
if currentUserID == 0 {
|
||||
return nil, errors.New(errors.CodeUnauthorized, "未授权访问")
|
||||
}
|
||||
|
||||
_, err := s.enterpriseStore.GetByID(ctx, enterpriseID)
|
||||
if err != nil {
|
||||
return nil, errors.New(errors.CodeEnterpriseNotFound, "企业不存在")
|
||||
}
|
||||
|
||||
var iotCards []model.IotCard
|
||||
if err := s.db.WithContext(ctx).Where("iccid IN ?", req.ICCIDs).Find(&iotCards).Error; err != nil {
|
||||
return nil, fmt.Errorf("查询卡信息失败: %w", err)
|
||||
}
|
||||
|
||||
cardMap := make(map[string]*model.IotCard)
|
||||
cardIDMap := make(map[uint]*model.IotCard)
|
||||
cardIDs := make([]uint, 0, len(iotCards))
|
||||
for i := range iotCards {
|
||||
cardMap[iotCards[i].ICCID] = &iotCards[i]
|
||||
cardIDMap[iotCards[i].ID] = &iotCards[i]
|
||||
cardIDs = append(cardIDs, iotCards[i].ID)
|
||||
}
|
||||
|
||||
existingAuths, err := s.enterpriseCardAuthStore.GetActiveAuthsByCardIDs(ctx, enterpriseID, cardIDs)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询已有授权失败: %w", err)
|
||||
}
|
||||
|
||||
resp := &model.RecallCardsResp{
|
||||
FailedItems: make([]model.FailedItem, 0),
|
||||
RecalledDevices: make([]model.RecalledDevice, 0),
|
||||
}
|
||||
|
||||
cardIDsToRecall := make([]uint, 0)
|
||||
for _, iccid := range req.ICCIDs {
|
||||
card, exists := cardMap[iccid]
|
||||
if !exists {
|
||||
resp.FailedItems = append(resp.FailedItems, model.FailedItem{
|
||||
ICCID: iccid,
|
||||
Reason: "卡不存在",
|
||||
})
|
||||
continue
|
||||
}
|
||||
if !existingAuths[card.ID] {
|
||||
resp.FailedItems = append(resp.FailedItems, model.FailedItem{
|
||||
ICCID: iccid,
|
||||
Reason: "该卡未授权给此企业",
|
||||
})
|
||||
continue
|
||||
}
|
||||
cardIDsToRecall = append(cardIDsToRecall, card.ID)
|
||||
}
|
||||
|
||||
if len(cardIDsToRecall) > 0 {
|
||||
if err := s.enterpriseCardAuthStore.BatchUpdateStatus(ctx, enterpriseID, cardIDsToRecall, 0); err != nil {
|
||||
return nil, fmt.Errorf("回收授权失败: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
resp.SuccessCount = len(cardIDsToRecall)
|
||||
resp.FailCount = len(resp.FailedItems)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *Service) ListCards(ctx context.Context, enterpriseID uint, req *model.EnterpriseCardListReq) (*model.EnterpriseCardPageResult, error) {
|
||||
_, err := s.enterpriseStore.GetByID(ctx, enterpriseID)
|
||||
if err != nil {
|
||||
return nil, errors.New(errors.CodeEnterpriseNotFound, "企业不存在")
|
||||
}
|
||||
|
||||
cardIDs, err := s.enterpriseCardAuthStore.ListCardIDsByEnterprise(ctx, enterpriseID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询授权卡ID失败: %w", err)
|
||||
}
|
||||
|
||||
if len(cardIDs) == 0 {
|
||||
return &model.EnterpriseCardPageResult{
|
||||
Items: make([]model.EnterpriseCardItem, 0),
|
||||
Total: 0,
|
||||
Page: req.Page,
|
||||
Size: req.PageSize,
|
||||
}, nil
|
||||
}
|
||||
|
||||
page := req.Page
|
||||
pageSize := req.PageSize
|
||||
if page == 0 {
|
||||
page = 1
|
||||
}
|
||||
if pageSize == 0 {
|
||||
pageSize = constants.DefaultPageSize
|
||||
}
|
||||
|
||||
query := s.db.WithContext(ctx).Model(&model.IotCard{}).Where("id IN ?", cardIDs)
|
||||
|
||||
if req.Status != nil {
|
||||
query = query.Where("status = ?", *req.Status)
|
||||
}
|
||||
if req.CarrierID != nil {
|
||||
query = query.Where("carrier_id = ?", *req.CarrierID)
|
||||
}
|
||||
if req.ICCID != "" {
|
||||
query = query.Where("iccid LIKE ?", "%"+req.ICCID+"%")
|
||||
}
|
||||
|
||||
var total int64
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, fmt.Errorf("统计卡数量失败: %w", err)
|
||||
}
|
||||
|
||||
var cards []model.IotCard
|
||||
offset := (page - 1) * pageSize
|
||||
if err := query.Offset(offset).Limit(pageSize).Order("created_at DESC").Find(&cards).Error; err != nil {
|
||||
return nil, fmt.Errorf("查询卡列表失败: %w", err)
|
||||
}
|
||||
|
||||
items := make([]model.EnterpriseCardItem, 0, len(cards))
|
||||
for _, card := range cards {
|
||||
items = append(items, model.EnterpriseCardItem{
|
||||
ID: card.ID,
|
||||
ICCID: card.ICCID,
|
||||
MSISDN: card.MSISDN,
|
||||
CarrierID: card.CarrierID,
|
||||
Status: card.Status,
|
||||
StatusName: getCardStatusName(card.Status),
|
||||
NetworkStatus: card.NetworkStatus,
|
||||
NetworkStatusName: getNetworkStatusName(card.NetworkStatus),
|
||||
})
|
||||
}
|
||||
|
||||
return &model.EnterpriseCardPageResult{
|
||||
Items: items,
|
||||
Total: total,
|
||||
Page: page,
|
||||
Size: pageSize,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) SuspendCard(ctx context.Context, enterpriseID, cardID uint) error {
|
||||
return s.updateCardNetworkStatus(ctx, enterpriseID, cardID, 0)
|
||||
}
|
||||
|
||||
func (s *Service) ResumeCard(ctx context.Context, enterpriseID, cardID uint) error {
|
||||
return s.updateCardNetworkStatus(ctx, enterpriseID, cardID, 1)
|
||||
}
|
||||
|
||||
func (s *Service) updateCardNetworkStatus(ctx context.Context, enterpriseID, cardID uint, networkStatus int) error {
|
||||
currentUserID := middleware.GetUserIDFromContext(ctx)
|
||||
if currentUserID == 0 {
|
||||
return errors.New(errors.CodeUnauthorized, "未授权访问")
|
||||
}
|
||||
|
||||
_, err := s.enterpriseStore.GetByID(ctx, enterpriseID)
|
||||
if err != nil {
|
||||
return errors.New(errors.CodeEnterpriseNotFound, "企业不存在")
|
||||
}
|
||||
|
||||
auth, err := s.enterpriseCardAuthStore.GetByEnterpriseAndCard(ctx, enterpriseID, cardID)
|
||||
if err != nil || auth.Status != 1 {
|
||||
return errors.New(errors.CodeForbidden, "无权限操作此卡")
|
||||
}
|
||||
|
||||
return s.db.WithContext(ctx).Model(&model.IotCard{}).
|
||||
Where("id = ?", cardID).
|
||||
Update("network_status", networkStatus).Error
|
||||
}
|
||||
|
||||
func getCardStatusName(status int) string {
|
||||
switch status {
|
||||
case 1:
|
||||
return "在库"
|
||||
case 2:
|
||||
return "已分销"
|
||||
case 3:
|
||||
return "已激活"
|
||||
case 4:
|
||||
return "已停用"
|
||||
default:
|
||||
return "未知"
|
||||
}
|
||||
}
|
||||
|
||||
func getNetworkStatusName(status int) string {
|
||||
if status == 1 {
|
||||
return "开机"
|
||||
}
|
||||
return "停机"
|
||||
}
|
||||
416
internal/service/my_commission/service.go
Normal file
416
internal/service/my_commission/service.go
Normal file
@@ -0,0 +1,416 @@
|
||||
package my_commission
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/middleware"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
db *gorm.DB
|
||||
shopStore *postgres.ShopStore
|
||||
walletStore *postgres.WalletStore
|
||||
commissionWithdrawalRequestStore *postgres.CommissionWithdrawalRequestStore
|
||||
commissionWithdrawalSettingStore *postgres.CommissionWithdrawalSettingStore
|
||||
commissionRecordStore *postgres.CommissionRecordStore
|
||||
walletTransactionStore *postgres.WalletTransactionStore
|
||||
}
|
||||
|
||||
func New(
|
||||
db *gorm.DB,
|
||||
shopStore *postgres.ShopStore,
|
||||
walletStore *postgres.WalletStore,
|
||||
commissionWithdrawalRequestStore *postgres.CommissionWithdrawalRequestStore,
|
||||
commissionWithdrawalSettingStore *postgres.CommissionWithdrawalSettingStore,
|
||||
commissionRecordStore *postgres.CommissionRecordStore,
|
||||
walletTransactionStore *postgres.WalletTransactionStore,
|
||||
) *Service {
|
||||
return &Service{
|
||||
db: db,
|
||||
shopStore: shopStore,
|
||||
walletStore: walletStore,
|
||||
commissionWithdrawalRequestStore: commissionWithdrawalRequestStore,
|
||||
commissionWithdrawalSettingStore: commissionWithdrawalSettingStore,
|
||||
commissionRecordStore: commissionRecordStore,
|
||||
walletTransactionStore: walletTransactionStore,
|
||||
}
|
||||
}
|
||||
|
||||
// GetCommissionSummary 获取我的佣金概览
|
||||
func (s *Service) GetCommissionSummary(ctx context.Context) (*model.MyCommissionSummaryResp, error) {
|
||||
userType := middleware.GetUserTypeFromContext(ctx)
|
||||
if userType != constants.UserTypeAgent {
|
||||
return nil, errors.New(errors.CodeForbidden, "仅代理商用户可访问")
|
||||
}
|
||||
|
||||
shopID := middleware.GetShopIDFromContext(ctx)
|
||||
if shopID == 0 {
|
||||
return nil, errors.New(errors.CodeForbidden, "无法获取店铺信息")
|
||||
}
|
||||
|
||||
shop, err := s.shopStore.GetByID(ctx, shopID)
|
||||
if err != nil {
|
||||
return nil, errors.New(errors.CodeShopNotFound, "店铺不存在")
|
||||
}
|
||||
|
||||
// 使用 GetShopCommissionWallet 获取店铺佣金钱包
|
||||
wallet, err := s.walletStore.GetShopCommissionWallet(ctx, shopID)
|
||||
if err != nil {
|
||||
// 钱包不存在时返回空数据
|
||||
return &model.MyCommissionSummaryResp{
|
||||
ShopID: shopID,
|
||||
ShopName: shop.ShopName,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 计算累计佣金(当前余额 + 冻结余额 + 已提现金额)
|
||||
// 由于 Wallet 模型没有 TotalIncome、TotalWithdrawn 字段,
|
||||
// 我们需要从 WalletTransaction 表计算或简化处理
|
||||
var totalWithdrawn int64
|
||||
s.db.WithContext(ctx).Model(&model.CommissionWithdrawalRequest{}).
|
||||
Where("shop_id = ? AND status IN ?", shopID, []int{2, 4}). // 已通过或已到账
|
||||
Select("COALESCE(SUM(actual_amount), 0)").Scan(&totalWithdrawn)
|
||||
|
||||
totalCommission := wallet.Balance + wallet.FrozenBalance + totalWithdrawn
|
||||
|
||||
return &model.MyCommissionSummaryResp{
|
||||
ShopID: shopID,
|
||||
ShopName: shop.ShopName,
|
||||
TotalCommission: totalCommission,
|
||||
WithdrawnCommission: totalWithdrawn,
|
||||
UnwithdrawCommission: wallet.Balance + wallet.FrozenBalance,
|
||||
FrozenCommission: wallet.FrozenBalance,
|
||||
WithdrawingCommission: wallet.FrozenBalance, // 提现中的金额等于冻结金额
|
||||
AvailableCommission: wallet.Balance,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// CreateWithdrawalRequest 发起提现申请
|
||||
func (s *Service) CreateWithdrawalRequest(ctx context.Context, req *model.CreateMyWithdrawalReq) (*model.CreateMyWithdrawalResp, error) {
|
||||
userType := middleware.GetUserTypeFromContext(ctx)
|
||||
if userType != constants.UserTypeAgent {
|
||||
return nil, errors.New(errors.CodeForbidden, "仅代理商用户可访问")
|
||||
}
|
||||
|
||||
shopID := middleware.GetShopIDFromContext(ctx)
|
||||
currentUserID := middleware.GetUserIDFromContext(ctx)
|
||||
if shopID == 0 || currentUserID == 0 {
|
||||
return nil, errors.New(errors.CodeForbidden, "无法获取用户信息")
|
||||
}
|
||||
|
||||
// 获取提现配置
|
||||
setting, err := s.commissionWithdrawalSettingStore.GetCurrent(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.New(errors.CodeInvalidParam, "暂未开放提现功能")
|
||||
}
|
||||
|
||||
// 验证最低提现金额
|
||||
if req.Amount < setting.MinWithdrawalAmount {
|
||||
return nil, errors.New(errors.CodeInvalidParam, fmt.Sprintf("提现金额不能低于 %.2f 元", float64(setting.MinWithdrawalAmount)/100))
|
||||
}
|
||||
|
||||
// 获取钱包
|
||||
wallet, err := s.walletStore.GetShopCommissionWallet(ctx, shopID)
|
||||
if err != nil {
|
||||
return nil, errors.New(errors.CodeInsufficientBalance, "钱包不存在")
|
||||
}
|
||||
|
||||
// 验证余额
|
||||
if req.Amount > wallet.Balance {
|
||||
return nil, errors.New(errors.CodeInsufficientBalance, "可提现余额不足")
|
||||
}
|
||||
|
||||
// 验证今日提现次数
|
||||
today := time.Now().Format("2006-01-02")
|
||||
todayStart := today + " 00:00:00"
|
||||
todayEnd := today + " 23:59:59"
|
||||
var todayCount int64
|
||||
s.db.WithContext(ctx).Model(&model.CommissionWithdrawalRequest{}).
|
||||
Where("shop_id = ? AND created_at >= ? AND created_at <= ?", shopID, todayStart, todayEnd).
|
||||
Count(&todayCount)
|
||||
if int(todayCount) >= setting.DailyWithdrawalLimit {
|
||||
return nil, errors.New(errors.CodeInvalidParam, "今日提现次数已达上限")
|
||||
}
|
||||
|
||||
// 计算手续费
|
||||
fee := req.Amount * setting.FeeRate / 10000
|
||||
actualAmount := req.Amount - fee
|
||||
|
||||
// 生成提现单号
|
||||
withdrawalNo := generateWithdrawalNo()
|
||||
|
||||
// 构建账户信息 JSON
|
||||
accountInfo := map[string]string{
|
||||
"account_name": req.AccountName,
|
||||
"account_number": req.AccountNumber,
|
||||
}
|
||||
accountInfoJSON, _ := json.Marshal(accountInfo)
|
||||
|
||||
var withdrawalRequest *model.CommissionWithdrawalRequest
|
||||
|
||||
err = s.db.Transaction(func(tx *gorm.DB) error {
|
||||
// 冻结余额
|
||||
if err := tx.WithContext(ctx).Model(&model.Wallet{}).
|
||||
Where("id = ? AND balance >= ?", wallet.ID, req.Amount).
|
||||
Updates(map[string]interface{}{
|
||||
"balance": gorm.Expr("balance - ?", req.Amount),
|
||||
"frozen_balance": gorm.Expr("frozen_balance + ?", req.Amount),
|
||||
}).Error; err != nil {
|
||||
return fmt.Errorf("冻结余额失败: %w", err)
|
||||
}
|
||||
|
||||
// 创建提现申请
|
||||
withdrawalRequest = &model.CommissionWithdrawalRequest{
|
||||
WithdrawalNo: withdrawalNo,
|
||||
ShopID: shopID,
|
||||
ApplicantID: currentUserID,
|
||||
Amount: req.Amount,
|
||||
FeeRate: setting.FeeRate,
|
||||
Fee: fee,
|
||||
ActualAmount: actualAmount,
|
||||
WithdrawalMethod: req.WithdrawalMethod,
|
||||
AccountInfo: accountInfoJSON,
|
||||
Status: 1, // 待审核
|
||||
}
|
||||
withdrawalRequest.Creator = currentUserID
|
||||
withdrawalRequest.Updater = currentUserID
|
||||
|
||||
if err := tx.WithContext(ctx).Create(withdrawalRequest).Error; err != nil {
|
||||
return fmt.Errorf("创建提现申请失败: %w", err)
|
||||
}
|
||||
|
||||
// 创建钱包流水记录
|
||||
remark := fmt.Sprintf("提现冻结,单号:%s", withdrawalNo)
|
||||
refType := constants.ReferenceTypeWithdrawal
|
||||
transaction := &model.WalletTransaction{
|
||||
WalletID: wallet.ID,
|
||||
UserID: currentUserID,
|
||||
TransactionType: constants.TransactionTypeWithdrawal,
|
||||
Amount: -req.Amount, // 冻结为负数
|
||||
BalanceBefore: wallet.Balance,
|
||||
BalanceAfter: wallet.Balance - req.Amount,
|
||||
Status: constants.TransactionStatusProcessing, // 处理中
|
||||
ReferenceType: &refType,
|
||||
ReferenceID: &withdrawalRequest.ID,
|
||||
Remark: &remark,
|
||||
Creator: currentUserID,
|
||||
}
|
||||
|
||||
if err := tx.WithContext(ctx).Create(transaction).Error; err != nil {
|
||||
return fmt.Errorf("创建钱包流水失败: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &model.CreateMyWithdrawalResp{
|
||||
ID: withdrawalRequest.ID,
|
||||
WithdrawalNo: withdrawalRequest.WithdrawalNo,
|
||||
Amount: withdrawalRequest.Amount,
|
||||
FeeRate: withdrawalRequest.FeeRate,
|
||||
Fee: withdrawalRequest.Fee,
|
||||
ActualAmount: withdrawalRequest.ActualAmount,
|
||||
Status: withdrawalRequest.Status,
|
||||
StatusName: getWithdrawalStatusName(withdrawalRequest.Status),
|
||||
CreatedAt: withdrawalRequest.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ListMyWithdrawalRequests 查询我的提现记录
|
||||
func (s *Service) ListMyWithdrawalRequests(ctx context.Context, req *model.MyWithdrawalListReq) (*model.WithdrawalRequestPageResult, error) {
|
||||
userType := middleware.GetUserTypeFromContext(ctx)
|
||||
if userType != constants.UserTypeAgent {
|
||||
return nil, errors.New(errors.CodeForbidden, "仅代理商用户可访问")
|
||||
}
|
||||
|
||||
shopID := middleware.GetShopIDFromContext(ctx)
|
||||
if shopID == 0 {
|
||||
return nil, errors.New(errors.CodeForbidden, "无法获取店铺信息")
|
||||
}
|
||||
|
||||
page := req.Page
|
||||
pageSize := req.PageSize
|
||||
if page == 0 {
|
||||
page = 1
|
||||
}
|
||||
if pageSize == 0 {
|
||||
pageSize = constants.DefaultPageSize
|
||||
}
|
||||
|
||||
query := s.db.WithContext(ctx).Model(&model.CommissionWithdrawalRequest{}).
|
||||
Where("shop_id = ?", shopID)
|
||||
|
||||
if req.Status != nil {
|
||||
query = query.Where("status = ?", *req.Status)
|
||||
}
|
||||
if req.StartTime != "" {
|
||||
query = query.Where("created_at >= ?", req.StartTime)
|
||||
}
|
||||
if req.EndTime != "" {
|
||||
query = query.Where("created_at <= ?", req.EndTime)
|
||||
}
|
||||
|
||||
var total int64
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, fmt.Errorf("统计提现记录失败: %w", err)
|
||||
}
|
||||
|
||||
var requests []model.CommissionWithdrawalRequest
|
||||
offset := (page - 1) * pageSize
|
||||
if err := query.Offset(offset).Limit(pageSize).Order("created_at DESC").Find(&requests).Error; err != nil {
|
||||
return nil, fmt.Errorf("查询提现记录失败: %w", err)
|
||||
}
|
||||
|
||||
items := make([]model.WithdrawalRequestItem, 0, len(requests))
|
||||
for _, r := range requests {
|
||||
// 解析账户信息
|
||||
accountName, accountNumber := parseAccountInfo(r.AccountInfo)
|
||||
|
||||
items = append(items, model.WithdrawalRequestItem{
|
||||
ID: r.ID,
|
||||
WithdrawalNo: r.WithdrawalNo,
|
||||
ShopID: r.ShopID,
|
||||
Amount: r.Amount,
|
||||
FeeRate: r.FeeRate,
|
||||
Fee: r.Fee,
|
||||
ActualAmount: r.ActualAmount,
|
||||
WithdrawalMethod: r.WithdrawalMethod,
|
||||
AccountName: accountName,
|
||||
AccountNumber: accountNumber,
|
||||
Status: r.Status,
|
||||
StatusName: getWithdrawalStatusName(r.Status),
|
||||
CreatedAt: r.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
}
|
||||
|
||||
return &model.WithdrawalRequestPageResult{
|
||||
Items: items,
|
||||
Total: total,
|
||||
Page: page,
|
||||
Size: pageSize,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ListMyCommissionRecords 查询我的佣金明细
|
||||
func (s *Service) ListMyCommissionRecords(ctx context.Context, req *model.MyCommissionRecordListReq) (*model.MyCommissionRecordPageResult, error) {
|
||||
userType := middleware.GetUserTypeFromContext(ctx)
|
||||
if userType != constants.UserTypeAgent {
|
||||
return nil, errors.New(errors.CodeForbidden, "仅代理商用户可访问")
|
||||
}
|
||||
|
||||
shopID := middleware.GetShopIDFromContext(ctx)
|
||||
if shopID == 0 {
|
||||
return nil, errors.New(errors.CodeForbidden, "无法获取店铺信息")
|
||||
}
|
||||
|
||||
page := req.Page
|
||||
pageSize := req.PageSize
|
||||
if page == 0 {
|
||||
page = 1
|
||||
}
|
||||
if pageSize == 0 {
|
||||
pageSize = constants.DefaultPageSize
|
||||
}
|
||||
|
||||
query := s.db.WithContext(ctx).Model(&model.CommissionRecord{}).
|
||||
Where("shop_id = ?", shopID)
|
||||
|
||||
if req.CommissionType != nil {
|
||||
query = query.Where("commission_type = ?", *req.CommissionType)
|
||||
}
|
||||
|
||||
var total int64
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, fmt.Errorf("统计佣金记录失败: %w", err)
|
||||
}
|
||||
|
||||
var records []model.CommissionRecord
|
||||
offset := (page - 1) * pageSize
|
||||
if err := query.Offset(offset).Limit(pageSize).Order("created_at DESC").Find(&records).Error; err != nil {
|
||||
return nil, fmt.Errorf("查询佣金记录失败: %w", err)
|
||||
}
|
||||
|
||||
items := make([]model.MyCommissionRecordItem, 0, len(records))
|
||||
for _, r := range records {
|
||||
items = append(items, model.MyCommissionRecordItem{
|
||||
ID: r.ID,
|
||||
ShopID: r.ShopID,
|
||||
OrderID: r.OrderID,
|
||||
CommissionType: r.CommissionType,
|
||||
Amount: r.Amount,
|
||||
Status: r.Status,
|
||||
StatusName: getCommissionStatusName(r.Status),
|
||||
CreatedAt: r.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
}
|
||||
|
||||
return &model.MyCommissionRecordPageResult{
|
||||
Items: items,
|
||||
Total: total,
|
||||
Page: page,
|
||||
Size: pageSize,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// generateWithdrawalNo 生成提现单号
|
||||
func generateWithdrawalNo() string {
|
||||
now := time.Now()
|
||||
return fmt.Sprintf("W%s%04d", now.Format("20060102150405"), rand.Intn(10000))
|
||||
}
|
||||
|
||||
// getWithdrawalStatusName 获取提现状态名称
|
||||
func getWithdrawalStatusName(status int) string {
|
||||
switch status {
|
||||
case 1:
|
||||
return "待审核"
|
||||
case 2:
|
||||
return "已通过"
|
||||
case 3:
|
||||
return "已拒绝"
|
||||
case 4:
|
||||
return "已到账"
|
||||
default:
|
||||
return "未知"
|
||||
}
|
||||
}
|
||||
|
||||
// getCommissionStatusName 获取佣金状态名称
|
||||
func getCommissionStatusName(status int) string {
|
||||
switch status {
|
||||
case 1:
|
||||
return "已冻结"
|
||||
case 2:
|
||||
return "解冻中"
|
||||
case 3:
|
||||
return "已发放"
|
||||
case 4:
|
||||
return "已失效"
|
||||
default:
|
||||
return "未知"
|
||||
}
|
||||
}
|
||||
|
||||
// parseAccountInfo 解析账户信息 JSON
|
||||
func parseAccountInfo(data []byte) (accountName, accountNumber string) {
|
||||
if len(data) == 0 {
|
||||
return "", ""
|
||||
}
|
||||
var info map[string]string
|
||||
if err := json.Unmarshal(data, &info); err != nil {
|
||||
return "", ""
|
||||
}
|
||||
return info["account_name"], info["account_number"]
|
||||
}
|
||||
427
internal/service/shop_commission/service.go
Normal file
427
internal/service/shop_commission/service.go
Normal file
@@ -0,0 +1,427 @@
|
||||
package shop_commission
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
shopStore *postgres.ShopStore
|
||||
accountStore *postgres.AccountStore
|
||||
walletStore *postgres.WalletStore
|
||||
commissionWithdrawalReqStore *postgres.CommissionWithdrawalRequestStore
|
||||
commissionRecordStore *postgres.CommissionRecordStore
|
||||
}
|
||||
|
||||
func New(
|
||||
shopStore *postgres.ShopStore,
|
||||
accountStore *postgres.AccountStore,
|
||||
walletStore *postgres.WalletStore,
|
||||
commissionWithdrawalReqStore *postgres.CommissionWithdrawalRequestStore,
|
||||
commissionRecordStore *postgres.CommissionRecordStore,
|
||||
) *Service {
|
||||
return &Service{
|
||||
shopStore: shopStore,
|
||||
accountStore: accountStore,
|
||||
walletStore: walletStore,
|
||||
commissionWithdrawalReqStore: commissionWithdrawalReqStore,
|
||||
commissionRecordStore: commissionRecordStore,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) ListShopCommissionSummary(ctx context.Context, req *model.ShopCommissionSummaryListReq) (*model.ShopCommissionSummaryPageResult, error) {
|
||||
opts := &store.QueryOptions{
|
||||
Page: req.Page,
|
||||
PageSize: req.PageSize,
|
||||
OrderBy: "created_at DESC",
|
||||
}
|
||||
if opts.Page == 0 {
|
||||
opts.Page = 1
|
||||
}
|
||||
if opts.PageSize == 0 {
|
||||
opts.PageSize = constants.DefaultPageSize
|
||||
}
|
||||
|
||||
filters := make(map[string]interface{})
|
||||
if req.ShopName != "" {
|
||||
filters["shop_name"] = req.ShopName
|
||||
}
|
||||
|
||||
shops, total, err := s.shopStore.List(ctx, opts, filters)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询店铺列表失败: %w", err)
|
||||
}
|
||||
|
||||
if len(shops) == 0 {
|
||||
return &model.ShopCommissionSummaryPageResult{
|
||||
Items: []model.ShopCommissionSummaryItem{},
|
||||
Total: 0,
|
||||
Page: opts.Page,
|
||||
Size: opts.PageSize,
|
||||
}, nil
|
||||
}
|
||||
|
||||
shopIDs := make([]uint, 0, len(shops))
|
||||
for _, shop := range shops {
|
||||
shopIDs = append(shopIDs, shop.ID)
|
||||
}
|
||||
|
||||
walletSummaries, err := s.walletStore.GetShopCommissionSummaryBatch(ctx, shopIDs)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询店铺钱包汇总失败: %w", err)
|
||||
}
|
||||
|
||||
withdrawnAmounts, err := s.commissionWithdrawalReqStore.SumAmountByShopIDsAndStatus(ctx, shopIDs, constants.WithdrawalStatusApproved)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询已提现金额失败: %w", err)
|
||||
}
|
||||
|
||||
withdrawingAmounts, err := s.commissionWithdrawalReqStore.SumAmountByShopIDsAndStatus(ctx, shopIDs, constants.WithdrawalStatusPending)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询提现中金额失败: %w", err)
|
||||
}
|
||||
|
||||
primaryAccounts, err := s.accountStore.GetPrimaryAccountsByShopIDs(ctx, shopIDs)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询主账号失败: %w", err)
|
||||
}
|
||||
|
||||
accountMap := make(map[uint]*model.Account)
|
||||
for _, acc := range primaryAccounts {
|
||||
if acc.ShopID != nil {
|
||||
accountMap[*acc.ShopID] = acc
|
||||
}
|
||||
}
|
||||
|
||||
items := make([]model.ShopCommissionSummaryItem, 0, len(shops))
|
||||
for _, shop := range shops {
|
||||
if req.Username != "" {
|
||||
acc := accountMap[shop.ID]
|
||||
if acc == nil || !containsSubstring(acc.Username, req.Username) {
|
||||
total--
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
item := s.buildCommissionSummaryItem(shop, walletSummaries[shop.ID], withdrawnAmounts[shop.ID], withdrawingAmounts[shop.ID], accountMap[shop.ID])
|
||||
items = append(items, item)
|
||||
}
|
||||
|
||||
return &model.ShopCommissionSummaryPageResult{
|
||||
Items: items,
|
||||
Total: total,
|
||||
Page: opts.Page,
|
||||
Size: opts.PageSize,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) buildCommissionSummaryItem(shop *model.Shop, walletSummary *postgres.ShopCommissionSummary, withdrawnAmount, withdrawingAmount int64, account *model.Account) model.ShopCommissionSummaryItem {
|
||||
var balance, frozenBalance int64
|
||||
if walletSummary != nil {
|
||||
balance = walletSummary.Balance
|
||||
frozenBalance = walletSummary.FrozenBalance
|
||||
}
|
||||
|
||||
totalCommission := balance + frozenBalance + withdrawnAmount
|
||||
unwithdrawCommission := totalCommission - withdrawnAmount
|
||||
availableCommission := balance - withdrawingAmount
|
||||
if availableCommission < 0 {
|
||||
availableCommission = 0
|
||||
}
|
||||
|
||||
var username, phone string
|
||||
if account != nil {
|
||||
username = account.Username
|
||||
phone = account.Phone
|
||||
}
|
||||
|
||||
return model.ShopCommissionSummaryItem{
|
||||
ShopID: shop.ID,
|
||||
ShopName: shop.ShopName,
|
||||
ShopCode: shop.ShopCode,
|
||||
Username: username,
|
||||
Phone: phone,
|
||||
TotalCommission: totalCommission,
|
||||
WithdrawnCommission: withdrawnAmount,
|
||||
UnwithdrawCommission: unwithdrawCommission,
|
||||
FrozenCommission: frozenBalance,
|
||||
WithdrawingCommission: withdrawingAmount,
|
||||
AvailableCommission: availableCommission,
|
||||
CreatedAt: shop.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) ListShopWithdrawalRequests(ctx context.Context, shopID uint, req *model.ShopWithdrawalRequestListReq) (*model.ShopWithdrawalRequestPageResult, error) {
|
||||
_, err := s.shopStore.GetByID(ctx, shopID)
|
||||
if err != nil {
|
||||
return nil, errors.New(errors.CodeShopNotFound, "店铺不存在")
|
||||
}
|
||||
|
||||
opts := &store.QueryOptions{
|
||||
Page: req.Page,
|
||||
PageSize: req.PageSize,
|
||||
OrderBy: "created_at DESC",
|
||||
}
|
||||
if opts.Page == 0 {
|
||||
opts.Page = 1
|
||||
}
|
||||
if opts.PageSize == 0 {
|
||||
opts.PageSize = constants.DefaultPageSize
|
||||
}
|
||||
|
||||
filters := &postgres.WithdrawalRequestListFilters{
|
||||
ShopID: shopID,
|
||||
WithdrawalNo: req.WithdrawalNo,
|
||||
}
|
||||
|
||||
if req.StartTime != "" {
|
||||
t, err := time.Parse("2006-01-02 15:04:05", req.StartTime)
|
||||
if err == nil {
|
||||
filters.StartTime = &t
|
||||
}
|
||||
}
|
||||
if req.EndTime != "" {
|
||||
t, err := time.Parse("2006-01-02 15:04:05", req.EndTime)
|
||||
if err == nil {
|
||||
filters.EndTime = &t
|
||||
}
|
||||
}
|
||||
|
||||
requests, total, err := s.commissionWithdrawalReqStore.ListByShopID(ctx, opts, filters)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询提现记录失败: %w", err)
|
||||
}
|
||||
|
||||
shop, _ := s.shopStore.GetByID(ctx, shopID)
|
||||
shopHierarchy := s.buildShopHierarchyPath(ctx, shop)
|
||||
|
||||
applicantIDs := make([]uint, 0)
|
||||
processorIDs := make([]uint, 0)
|
||||
for _, r := range requests {
|
||||
if r.ApplicantID > 0 {
|
||||
applicantIDs = append(applicantIDs, r.ApplicantID)
|
||||
}
|
||||
if r.ProcessorID > 0 {
|
||||
processorIDs = append(processorIDs, r.ProcessorID)
|
||||
}
|
||||
}
|
||||
|
||||
applicantMap := make(map[uint]string)
|
||||
processorMap := make(map[uint]string)
|
||||
|
||||
if len(applicantIDs) > 0 {
|
||||
accounts, _ := s.accountStore.GetByIDs(ctx, applicantIDs)
|
||||
for _, acc := range accounts {
|
||||
applicantMap[acc.ID] = acc.Username
|
||||
}
|
||||
}
|
||||
if len(processorIDs) > 0 {
|
||||
accounts, _ := s.accountStore.GetByIDs(ctx, processorIDs)
|
||||
for _, acc := range accounts {
|
||||
processorMap[acc.ID] = acc.Username
|
||||
}
|
||||
}
|
||||
|
||||
items := make([]model.ShopWithdrawalRequestItem, 0, len(requests))
|
||||
for _, r := range requests {
|
||||
item := s.buildWithdrawalRequestItem(r, shop.ShopName, shopHierarchy, applicantMap, processorMap)
|
||||
items = append(items, item)
|
||||
}
|
||||
|
||||
return &model.ShopWithdrawalRequestPageResult{
|
||||
Items: items,
|
||||
Total: total,
|
||||
Page: opts.Page,
|
||||
Size: opts.PageSize,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) buildWithdrawalRequestItem(r *model.CommissionWithdrawalRequest, shopName, shopHierarchy string, applicantMap, processorMap map[uint]string) model.ShopWithdrawalRequestItem {
|
||||
var processorID *uint
|
||||
if r.ProcessorID > 0 {
|
||||
processorID = &r.ProcessorID
|
||||
}
|
||||
|
||||
var accountName, accountNumber, bankName string
|
||||
if len(r.AccountInfo) > 0 {
|
||||
var info map[string]interface{}
|
||||
if err := json.Unmarshal(r.AccountInfo, &info); err == nil {
|
||||
if v, ok := info["account_name"].(string); ok {
|
||||
accountName = v
|
||||
}
|
||||
if v, ok := info["account_number"].(string); ok {
|
||||
accountNumber = v
|
||||
}
|
||||
if v, ok := info["bank_name"].(string); ok {
|
||||
bankName = v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var processedAt, paidAt string
|
||||
if r.ProcessedAt != nil {
|
||||
processedAt = r.ProcessedAt.Format("2006-01-02 15:04:05")
|
||||
}
|
||||
if r.PaidAt != nil {
|
||||
paidAt = r.PaidAt.Format("2006-01-02 15:04:05")
|
||||
}
|
||||
|
||||
return model.ShopWithdrawalRequestItem{
|
||||
ID: r.ID,
|
||||
WithdrawalNo: r.WithdrawalNo,
|
||||
Amount: r.Amount,
|
||||
FeeRate: r.FeeRate,
|
||||
Fee: r.Fee,
|
||||
ActualAmount: r.ActualAmount,
|
||||
Status: r.Status,
|
||||
StatusName: getWithdrawalStatusName(r.Status),
|
||||
ShopID: r.ShopID,
|
||||
ShopName: shopName,
|
||||
ShopHierarchy: shopHierarchy,
|
||||
ApplicantID: r.ApplicantID,
|
||||
ApplicantName: applicantMap[r.ApplicantID],
|
||||
ProcessorID: processorID,
|
||||
ProcessorName: processorMap[r.ProcessorID],
|
||||
WithdrawalMethod: r.WithdrawalMethod,
|
||||
PaymentType: r.PaymentType,
|
||||
AccountName: accountName,
|
||||
AccountNumber: accountNumber,
|
||||
BankName: bankName,
|
||||
RejectReason: r.RejectReason,
|
||||
Remark: r.Remark,
|
||||
CreatedAt: r.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
ProcessedAt: processedAt,
|
||||
PaidAt: paidAt,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) buildShopHierarchyPath(ctx context.Context, shop *model.Shop) string {
|
||||
if shop == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
path := shop.ShopName
|
||||
current := shop
|
||||
depth := 0
|
||||
|
||||
for current.ParentID != nil && depth < 2 {
|
||||
parent, err := s.shopStore.GetByID(ctx, *current.ParentID)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
path = parent.ShopName + "_" + path
|
||||
current = parent
|
||||
depth++
|
||||
}
|
||||
|
||||
return path
|
||||
}
|
||||
|
||||
func (s *Service) ListShopCommissionRecords(ctx context.Context, shopID uint, req *model.ShopCommissionRecordListReq) (*model.ShopCommissionRecordPageResult, error) {
|
||||
_, err := s.shopStore.GetByID(ctx, shopID)
|
||||
if err != nil {
|
||||
return nil, errors.New(errors.CodeShopNotFound, "店铺不存在")
|
||||
}
|
||||
|
||||
opts := &store.QueryOptions{
|
||||
Page: req.Page,
|
||||
PageSize: req.PageSize,
|
||||
OrderBy: "created_at DESC",
|
||||
}
|
||||
if opts.Page == 0 {
|
||||
opts.Page = 1
|
||||
}
|
||||
if opts.PageSize == 0 {
|
||||
opts.PageSize = constants.DefaultPageSize
|
||||
}
|
||||
|
||||
filters := &postgres.CommissionRecordListFilters{
|
||||
ShopID: shopID,
|
||||
CommissionType: req.CommissionType,
|
||||
ICCID: req.ICCID,
|
||||
DeviceNo: req.DeviceNo,
|
||||
OrderNo: req.OrderNo,
|
||||
}
|
||||
|
||||
records, total, err := s.commissionRecordStore.ListByShopID(ctx, opts, filters)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询佣金明细失败: %w", err)
|
||||
}
|
||||
|
||||
items := make([]model.ShopCommissionRecordItem, 0, len(records))
|
||||
for _, r := range records {
|
||||
item := model.ShopCommissionRecordItem{
|
||||
ID: r.ID,
|
||||
Amount: r.Amount,
|
||||
BalanceAfter: r.BalanceAfter,
|
||||
CommissionType: r.CommissionType,
|
||||
Status: r.Status,
|
||||
StatusName: getCommissionStatusName(r.Status),
|
||||
OrderID: r.OrderID,
|
||||
OrderNo: "",
|
||||
DeviceNo: "",
|
||||
ICCID: "",
|
||||
OrderCreatedAt: "",
|
||||
CreatedAt: r.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
|
||||
return &model.ShopCommissionRecordPageResult{
|
||||
Items: items,
|
||||
Total: total,
|
||||
Page: opts.Page,
|
||||
Size: opts.PageSize,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func getWithdrawalStatusName(status int) string {
|
||||
switch status {
|
||||
case constants.WithdrawalStatusPending:
|
||||
return "待审核"
|
||||
case constants.WithdrawalStatusApproved:
|
||||
return "已通过"
|
||||
case constants.WithdrawalStatusRejected:
|
||||
return "已拒绝"
|
||||
case constants.WithdrawalStatusPaid:
|
||||
return "已到账"
|
||||
default:
|
||||
return "未知"
|
||||
}
|
||||
}
|
||||
|
||||
func getCommissionStatusName(status int) string {
|
||||
switch status {
|
||||
case constants.CommissionStatusFrozen:
|
||||
return "已冻结"
|
||||
case constants.CommissionStatusUnfreezing:
|
||||
return "解冻中"
|
||||
case constants.CommissionStatusReleased:
|
||||
return "已发放"
|
||||
case constants.CommissionStatusInvalid:
|
||||
return "已失效"
|
||||
default:
|
||||
return "未知"
|
||||
}
|
||||
}
|
||||
|
||||
func containsSubstring(s, substr string) bool {
|
||||
return len(s) >= len(substr) && (s == substr || len(substr) == 0 || (len(s) > 0 && len(substr) > 0 && contains(s, substr)))
|
||||
}
|
||||
|
||||
func contains(s, substr string) bool {
|
||||
for i := 0; i <= len(s)-len(substr); i++ {
|
||||
if s[i:i+len(substr)] == substr {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -218,6 +218,30 @@ func (s *AccountStore) BulkUpdateStatus(ctx context.Context, ids []uint, status
|
||||
}).Error
|
||||
}
|
||||
|
||||
func (s *AccountStore) GetByIDs(ctx context.Context, ids []uint) ([]*model.Account, error) {
|
||||
if len(ids) == 0 {
|
||||
return []*model.Account{}, nil
|
||||
}
|
||||
var accounts []*model.Account
|
||||
if err := s.db.WithContext(ctx).Where("id IN ?", ids).Find(&accounts).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return accounts, nil
|
||||
}
|
||||
|
||||
func (s *AccountStore) GetPrimaryAccountsByShopIDs(ctx context.Context, shopIDs []uint) ([]*model.Account, error) {
|
||||
if len(shopIDs) == 0 {
|
||||
return []*model.Account{}, nil
|
||||
}
|
||||
var accounts []*model.Account
|
||||
if err := s.db.WithContext(ctx).
|
||||
Where("shop_id IN ? AND is_primary = ?", shopIDs, true).
|
||||
Find(&accounts).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return accounts, nil
|
||||
}
|
||||
|
||||
// ListByShopID 按店铺ID分页查询账号列表
|
||||
func (s *AccountStore) ListByShopID(ctx context.Context, shopID uint, opts *store.QueryOptions, filters map[string]interface{}) ([]*model.Account, int64, error) {
|
||||
var accounts []*model.Account
|
||||
|
||||
88
internal/store/postgres/commission_record_store.go
Normal file
88
internal/store/postgres/commission_record_store.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type CommissionRecordStore struct {
|
||||
db *gorm.DB
|
||||
redis *redis.Client
|
||||
}
|
||||
|
||||
func NewCommissionRecordStore(db *gorm.DB, redis *redis.Client) *CommissionRecordStore {
|
||||
return &CommissionRecordStore{
|
||||
db: db,
|
||||
redis: redis,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *CommissionRecordStore) Create(ctx context.Context, record *model.CommissionRecord) error {
|
||||
return s.db.WithContext(ctx).Create(record).Error
|
||||
}
|
||||
|
||||
func (s *CommissionRecordStore) GetByID(ctx context.Context, id uint) (*model.CommissionRecord, error) {
|
||||
var record model.CommissionRecord
|
||||
if err := s.db.WithContext(ctx).First(&record, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &record, nil
|
||||
}
|
||||
|
||||
type CommissionRecordListFilters struct {
|
||||
ShopID uint
|
||||
CommissionType string
|
||||
ICCID string
|
||||
DeviceNo string
|
||||
OrderNo string
|
||||
Status *int
|
||||
}
|
||||
|
||||
func (s *CommissionRecordStore) ListByShopID(ctx context.Context, opts *store.QueryOptions, filters *CommissionRecordListFilters) ([]*model.CommissionRecord, int64, error) {
|
||||
var records []*model.CommissionRecord
|
||||
var total int64
|
||||
|
||||
query := s.db.WithContext(ctx).Model(&model.CommissionRecord{})
|
||||
|
||||
if filters != nil {
|
||||
if filters.ShopID > 0 {
|
||||
query = query.Where("shop_id = ?", filters.ShopID)
|
||||
}
|
||||
if filters.CommissionType != "" {
|
||||
query = query.Where("commission_type = ?", filters.CommissionType)
|
||||
}
|
||||
if filters.Status != nil {
|
||||
query = query.Where("status = ?", *filters.Status)
|
||||
}
|
||||
}
|
||||
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
if opts == nil {
|
||||
opts = &store.QueryOptions{
|
||||
Page: 1,
|
||||
PageSize: constants.DefaultPageSize,
|
||||
}
|
||||
}
|
||||
offset := (opts.Page - 1) * opts.PageSize
|
||||
query = query.Offset(offset).Limit(opts.PageSize)
|
||||
|
||||
if opts.OrderBy != "" {
|
||||
query = query.Order(opts.OrderBy)
|
||||
} else {
|
||||
query = query.Order("created_at DESC")
|
||||
}
|
||||
|
||||
if err := query.Find(&records).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return records, total, nil
|
||||
}
|
||||
193
internal/store/postgres/commission_withdrawal_request_store.go
Normal file
193
internal/store/postgres/commission_withdrawal_request_store.go
Normal file
@@ -0,0 +1,193 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type CommissionWithdrawalRequestStore struct {
|
||||
db *gorm.DB
|
||||
redis *redis.Client
|
||||
}
|
||||
|
||||
func NewCommissionWithdrawalRequestStore(db *gorm.DB, redis *redis.Client) *CommissionWithdrawalRequestStore {
|
||||
return &CommissionWithdrawalRequestStore{
|
||||
db: db,
|
||||
redis: redis,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *CommissionWithdrawalRequestStore) Create(ctx context.Context, req *model.CommissionWithdrawalRequest) error {
|
||||
return s.db.WithContext(ctx).Create(req).Error
|
||||
}
|
||||
|
||||
func (s *CommissionWithdrawalRequestStore) GetByID(ctx context.Context, id uint) (*model.CommissionWithdrawalRequest, error) {
|
||||
var req model.CommissionWithdrawalRequest
|
||||
if err := s.db.WithContext(ctx).First(&req, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &req, nil
|
||||
}
|
||||
|
||||
func (s *CommissionWithdrawalRequestStore) Update(ctx context.Context, req *model.CommissionWithdrawalRequest) error {
|
||||
return s.db.WithContext(ctx).Save(req).Error
|
||||
}
|
||||
|
||||
type WithdrawalRequestListFilters struct {
|
||||
ShopID uint
|
||||
WithdrawalNo string
|
||||
StartTime *time.Time
|
||||
EndTime *time.Time
|
||||
Status *int
|
||||
}
|
||||
|
||||
func (s *CommissionWithdrawalRequestStore) ListByShopID(ctx context.Context, opts *store.QueryOptions, filters *WithdrawalRequestListFilters) ([]*model.CommissionWithdrawalRequest, int64, error) {
|
||||
var requests []*model.CommissionWithdrawalRequest
|
||||
var total int64
|
||||
|
||||
query := s.db.WithContext(ctx).Model(&model.CommissionWithdrawalRequest{})
|
||||
|
||||
if filters != nil {
|
||||
if filters.ShopID > 0 {
|
||||
query = query.Where("shop_id = ?", filters.ShopID)
|
||||
}
|
||||
if filters.WithdrawalNo != "" {
|
||||
query = query.Where("withdrawal_no = ?", filters.WithdrawalNo)
|
||||
}
|
||||
if filters.StartTime != nil {
|
||||
query = query.Where("created_at >= ?", filters.StartTime)
|
||||
}
|
||||
if filters.EndTime != nil {
|
||||
query = query.Where("created_at <= ?", filters.EndTime)
|
||||
}
|
||||
if filters.Status != nil {
|
||||
query = query.Where("status = ?", *filters.Status)
|
||||
}
|
||||
}
|
||||
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
if opts == nil {
|
||||
opts = &store.QueryOptions{
|
||||
Page: 1,
|
||||
PageSize: constants.DefaultPageSize,
|
||||
}
|
||||
}
|
||||
offset := (opts.Page - 1) * opts.PageSize
|
||||
query = query.Offset(offset).Limit(opts.PageSize)
|
||||
|
||||
if opts.OrderBy != "" {
|
||||
query = query.Order(opts.OrderBy)
|
||||
} else {
|
||||
query = query.Order("created_at DESC")
|
||||
}
|
||||
|
||||
if err := query.Find(&requests).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return requests, total, nil
|
||||
}
|
||||
|
||||
func (s *CommissionWithdrawalRequestStore) SumAmountByShopIDAndStatus(ctx context.Context, shopID uint, status int) (int64, error) {
|
||||
var sum struct {
|
||||
Total int64
|
||||
}
|
||||
err := s.db.WithContext(ctx).
|
||||
Model(&model.CommissionWithdrawalRequest{}).
|
||||
Select("COALESCE(SUM(amount), 0) as total").
|
||||
Where("shop_id = ? AND status = ?", shopID, status).
|
||||
Scan(&sum).Error
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return sum.Total, nil
|
||||
}
|
||||
|
||||
func (s *CommissionWithdrawalRequestStore) SumAmountByShopIDsAndStatus(ctx context.Context, shopIDs []uint, status int) (map[uint]int64, error) {
|
||||
if len(shopIDs) == 0 {
|
||||
return make(map[uint]int64), nil
|
||||
}
|
||||
|
||||
type sumResult struct {
|
||||
ShopID uint
|
||||
Total int64
|
||||
}
|
||||
|
||||
var results []sumResult
|
||||
err := s.db.WithContext(ctx).
|
||||
Model(&model.CommissionWithdrawalRequest{}).
|
||||
Select("shop_id, COALESCE(SUM(amount), 0) as total").
|
||||
Where("shop_id IN ? AND status = ?", shopIDs, status).
|
||||
Group("shop_id").
|
||||
Scan(&results).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := make(map[uint]int64)
|
||||
for _, r := range results {
|
||||
result[r.ShopID] = r.Total
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *CommissionWithdrawalRequestStore) List(ctx context.Context, opts *store.QueryOptions, filters *WithdrawalRequestListFilters) ([]*model.CommissionWithdrawalRequest, int64, error) {
|
||||
var requests []*model.CommissionWithdrawalRequest
|
||||
var total int64
|
||||
|
||||
query := s.db.WithContext(ctx).Model(&model.CommissionWithdrawalRequest{})
|
||||
|
||||
if filters != nil {
|
||||
if filters.WithdrawalNo != "" {
|
||||
query = query.Where("withdrawal_no = ?", filters.WithdrawalNo)
|
||||
}
|
||||
if filters.StartTime != nil {
|
||||
query = query.Where("created_at >= ?", filters.StartTime)
|
||||
}
|
||||
if filters.EndTime != nil {
|
||||
query = query.Where("created_at <= ?", filters.EndTime)
|
||||
}
|
||||
if filters.Status != nil {
|
||||
query = query.Where("status = ?", *filters.Status)
|
||||
}
|
||||
}
|
||||
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
if opts == nil {
|
||||
opts = &store.QueryOptions{
|
||||
Page: 1,
|
||||
PageSize: constants.DefaultPageSize,
|
||||
}
|
||||
}
|
||||
offset := (opts.Page - 1) * opts.PageSize
|
||||
query = query.Offset(offset).Limit(opts.PageSize)
|
||||
|
||||
if opts.OrderBy != "" {
|
||||
query = query.Order(opts.OrderBy)
|
||||
} else {
|
||||
query = query.Order("created_at DESC")
|
||||
}
|
||||
|
||||
if err := query.Find(&requests).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return requests, total, nil
|
||||
}
|
||||
|
||||
func (s *CommissionWithdrawalRequestStore) UpdateStatusWithTx(ctx context.Context, tx *gorm.DB, id uint, updates map[string]interface{}) error {
|
||||
return tx.WithContext(ctx).Model(&model.CommissionWithdrawalRequest{}).Where("id = ? AND status = ?", id, constants.WithdrawalStatusPending).Updates(updates).Error
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type CommissionWithdrawalSettingStore struct {
|
||||
db *gorm.DB
|
||||
redis *redis.Client
|
||||
}
|
||||
|
||||
func NewCommissionWithdrawalSettingStore(db *gorm.DB, redis *redis.Client) *CommissionWithdrawalSettingStore {
|
||||
return &CommissionWithdrawalSettingStore{
|
||||
db: db,
|
||||
redis: redis,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *CommissionWithdrawalSettingStore) Create(ctx context.Context, setting *model.CommissionWithdrawalSetting) error {
|
||||
return s.db.WithContext(ctx).Create(setting).Error
|
||||
}
|
||||
|
||||
func (s *CommissionWithdrawalSettingStore) CreateWithTx(ctx context.Context, tx *gorm.DB, setting *model.CommissionWithdrawalSetting) error {
|
||||
return tx.WithContext(ctx).Create(setting).Error
|
||||
}
|
||||
|
||||
func (s *CommissionWithdrawalSettingStore) GetByID(ctx context.Context, id uint) (*model.CommissionWithdrawalSetting, error) {
|
||||
var setting model.CommissionWithdrawalSetting
|
||||
if err := s.db.WithContext(ctx).First(&setting, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &setting, nil
|
||||
}
|
||||
|
||||
func (s *CommissionWithdrawalSettingStore) GetCurrent(ctx context.Context) (*model.CommissionWithdrawalSetting, error) {
|
||||
var setting model.CommissionWithdrawalSetting
|
||||
if err := s.db.WithContext(ctx).Where("is_active = ?", true).First(&setting).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &setting, nil
|
||||
}
|
||||
|
||||
func (s *CommissionWithdrawalSettingStore) List(ctx context.Context, opts *store.QueryOptions) ([]*model.CommissionWithdrawalSetting, int64, error) {
|
||||
var settings []*model.CommissionWithdrawalSetting
|
||||
var total int64
|
||||
|
||||
query := s.db.WithContext(ctx).Model(&model.CommissionWithdrawalSetting{})
|
||||
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
if opts == nil {
|
||||
opts = &store.QueryOptions{
|
||||
Page: 1,
|
||||
PageSize: constants.DefaultPageSize,
|
||||
}
|
||||
}
|
||||
offset := (opts.Page - 1) * opts.PageSize
|
||||
query = query.Offset(offset).Limit(opts.PageSize).Order("created_at DESC")
|
||||
|
||||
if err := query.Find(&settings).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return settings, total, nil
|
||||
}
|
||||
|
||||
func (s *CommissionWithdrawalSettingStore) DeactivateCurrentWithTx(ctx context.Context, tx *gorm.DB) error {
|
||||
return tx.WithContext(ctx).Model(&model.CommissionWithdrawalSetting{}).
|
||||
Where("is_active = ?", true).
|
||||
Update("is_active", false).Error
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type EnterpriseCardAuthorizationStore struct {
|
||||
db *gorm.DB
|
||||
redis *redis.Client
|
||||
}
|
||||
|
||||
func NewEnterpriseCardAuthorizationStore(db *gorm.DB, redis *redis.Client) *EnterpriseCardAuthorizationStore {
|
||||
return &EnterpriseCardAuthorizationStore{
|
||||
db: db,
|
||||
redis: redis,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *EnterpriseCardAuthorizationStore) Create(ctx context.Context, auth *model.EnterpriseCardAuthorization) error {
|
||||
return s.db.WithContext(ctx).Create(auth).Error
|
||||
}
|
||||
|
||||
func (s *EnterpriseCardAuthorizationStore) BatchCreate(ctx context.Context, auths []*model.EnterpriseCardAuthorization) error {
|
||||
if len(auths) == 0 {
|
||||
return nil
|
||||
}
|
||||
return s.db.WithContext(ctx).CreateInBatches(auths, 100).Error
|
||||
}
|
||||
|
||||
func (s *EnterpriseCardAuthorizationStore) UpdateStatus(ctx context.Context, enterpriseID, cardID uint, status int) error {
|
||||
return s.db.WithContext(ctx).Model(&model.EnterpriseCardAuthorization{}).
|
||||
Where("enterprise_id = ? AND iot_card_id = ?", enterpriseID, cardID).
|
||||
Update("status", status).Error
|
||||
}
|
||||
|
||||
func (s *EnterpriseCardAuthorizationStore) BatchUpdateStatus(ctx context.Context, enterpriseID uint, cardIDs []uint, status int) error {
|
||||
if len(cardIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
return s.db.WithContext(ctx).Model(&model.EnterpriseCardAuthorization{}).
|
||||
Where("enterprise_id = ? AND iot_card_id IN ?", enterpriseID, cardIDs).
|
||||
Update("status", status).Error
|
||||
}
|
||||
|
||||
func (s *EnterpriseCardAuthorizationStore) GetByEnterpriseAndCard(ctx context.Context, enterpriseID, cardID uint) (*model.EnterpriseCardAuthorization, error) {
|
||||
var auth model.EnterpriseCardAuthorization
|
||||
err := s.db.WithContext(ctx).
|
||||
Where("enterprise_id = ? AND iot_card_id = ?", enterpriseID, cardID).
|
||||
First(&auth).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &auth, nil
|
||||
}
|
||||
|
||||
func (s *EnterpriseCardAuthorizationStore) ListByEnterprise(ctx context.Context, enterpriseID uint, status *int) ([]*model.EnterpriseCardAuthorization, error) {
|
||||
var auths []*model.EnterpriseCardAuthorization
|
||||
query := s.db.WithContext(ctx).Where("enterprise_id = ?", enterpriseID)
|
||||
if status != nil {
|
||||
query = query.Where("status = ?", *status)
|
||||
}
|
||||
if err := query.Find(&auths).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return auths, nil
|
||||
}
|
||||
|
||||
func (s *EnterpriseCardAuthorizationStore) ListCardIDsByEnterprise(ctx context.Context, enterpriseID uint) ([]uint, error) {
|
||||
var cardIDs []uint
|
||||
err := s.db.WithContext(ctx).Model(&model.EnterpriseCardAuthorization{}).
|
||||
Where("enterprise_id = ? AND status = 1", enterpriseID).
|
||||
Pluck("iot_card_id", &cardIDs).Error
|
||||
return cardIDs, err
|
||||
}
|
||||
|
||||
func (s *EnterpriseCardAuthorizationStore) GetActiveAuthsByCardIDs(ctx context.Context, enterpriseID uint, cardIDs []uint) (map[uint]bool, error) {
|
||||
if len(cardIDs) == 0 {
|
||||
return make(map[uint]bool), nil
|
||||
}
|
||||
var auths []model.EnterpriseCardAuthorization
|
||||
err := s.db.WithContext(ctx).
|
||||
Where("enterprise_id = ? AND iot_card_id IN ? AND status = 1", enterpriseID, cardIDs).
|
||||
Find(&auths).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := make(map[uint]bool)
|
||||
for _, auth := range auths {
|
||||
result[auth.IotCardID] = true
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
108
internal/store/postgres/wallet_store.go
Normal file
108
internal/store/postgres/wallet_store.go
Normal file
@@ -0,0 +1,108 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type WalletStore struct {
|
||||
db *gorm.DB
|
||||
redis *redis.Client
|
||||
}
|
||||
|
||||
func NewWalletStore(db *gorm.DB, redis *redis.Client) *WalletStore {
|
||||
return &WalletStore{
|
||||
db: db,
|
||||
redis: redis,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *WalletStore) GetByResourceTypeAndID(ctx context.Context, resourceType string, resourceID uint, walletType string) (*model.Wallet, error) {
|
||||
var wallet model.Wallet
|
||||
err := s.db.WithContext(ctx).
|
||||
Where("resource_type = ? AND resource_id = ? AND wallet_type = ?", resourceType, resourceID, walletType).
|
||||
First(&wallet).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &wallet, nil
|
||||
}
|
||||
|
||||
func (s *WalletStore) GetShopCommissionWallet(ctx context.Context, shopID uint) (*model.Wallet, error) {
|
||||
return s.GetByResourceTypeAndID(ctx, "shop", shopID, "commission")
|
||||
}
|
||||
|
||||
type ShopCommissionSummary struct {
|
||||
ShopID uint
|
||||
Balance int64
|
||||
FrozenBalance int64
|
||||
}
|
||||
|
||||
func (s *WalletStore) GetShopCommissionSummaryBatch(ctx context.Context, shopIDs []uint) (map[uint]*ShopCommissionSummary, error) {
|
||||
if len(shopIDs) == 0 {
|
||||
return make(map[uint]*ShopCommissionSummary), nil
|
||||
}
|
||||
|
||||
var wallets []model.Wallet
|
||||
err := s.db.WithContext(ctx).
|
||||
Where("resource_type = ? AND resource_id IN ? AND wallet_type = ?", "shop", shopIDs, "commission").
|
||||
Find(&wallets).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := make(map[uint]*ShopCommissionSummary)
|
||||
for _, w := range wallets {
|
||||
result[w.ResourceID] = &ShopCommissionSummary{
|
||||
ShopID: w.ResourceID,
|
||||
Balance: w.Balance,
|
||||
FrozenBalance: w.FrozenBalance,
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *WalletStore) GetByID(ctx context.Context, id uint) (*model.Wallet, error) {
|
||||
var wallet model.Wallet
|
||||
if err := s.db.WithContext(ctx).First(&wallet, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &wallet, nil
|
||||
}
|
||||
|
||||
func (s *WalletStore) DeductFrozenBalanceWithTx(ctx context.Context, tx *gorm.DB, walletID uint, amount int64) error {
|
||||
result := tx.WithContext(ctx).
|
||||
Model(&model.Wallet{}).
|
||||
Where("id = ? AND frozen_balance >= ?", walletID, amount).
|
||||
Updates(map[string]interface{}{
|
||||
"frozen_balance": gorm.Expr("frozen_balance - ?", amount),
|
||||
})
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return gorm.ErrRecordNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *WalletStore) UnfreezeBalanceWithTx(ctx context.Context, tx *gorm.DB, walletID uint, amount int64) error {
|
||||
result := tx.WithContext(ctx).
|
||||
Model(&model.Wallet{}).
|
||||
Where("id = ? AND frozen_balance >= ?", walletID, amount).
|
||||
Updates(map[string]interface{}{
|
||||
"balance": gorm.Expr("balance + ?", amount),
|
||||
"frozen_balance": gorm.Expr("frozen_balance - ?", amount),
|
||||
})
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return gorm.ErrRecordNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
37
internal/store/postgres/wallet_transaction_store.go
Normal file
37
internal/store/postgres/wallet_transaction_store.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type WalletTransactionStore struct {
|
||||
db *gorm.DB
|
||||
redis *redis.Client
|
||||
}
|
||||
|
||||
func NewWalletTransactionStore(db *gorm.DB, redis *redis.Client) *WalletTransactionStore {
|
||||
return &WalletTransactionStore{
|
||||
db: db,
|
||||
redis: redis,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *WalletTransactionStore) CreateWithTx(ctx context.Context, tx *gorm.DB, transaction *model.WalletTransaction) error {
|
||||
return tx.WithContext(ctx).Create(transaction).Error
|
||||
}
|
||||
|
||||
func (s *WalletTransactionStore) Create(ctx context.Context, transaction *model.WalletTransaction) error {
|
||||
return s.db.WithContext(ctx).Create(transaction).Error
|
||||
}
|
||||
|
||||
func (s *WalletTransactionStore) GetByID(ctx context.Context, id uint) (*model.WalletTransaction, error) {
|
||||
var tx model.WalletTransaction
|
||||
if err := s.db.WithContext(ctx).First(&tx, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &tx, nil
|
||||
}
|
||||
42
migrations/000010_add_commission_model_changes.down.sql
Normal file
42
migrations/000010_add_commission_model_changes.down.sql
Normal file
@@ -0,0 +1,42 @@
|
||||
-- 回滚: 账号与佣金管理模块数据模型变更
|
||||
-- 变更ID: add-commission-model-changes
|
||||
|
||||
-- 1. 恢复 owner_type 枚举值 (shop -> agent)
|
||||
UPDATE tb_iot_card SET owner_type = 'agent' WHERE owner_type = 'shop';
|
||||
UPDATE tb_device SET owner_type = 'agent' WHERE owner_type = 'shop';
|
||||
|
||||
-- 2. 删除新增的表
|
||||
DROP TABLE IF EXISTS tb_asset_allocation_record;
|
||||
DROP TABLE IF EXISTS tb_enterprise_card_authorization;
|
||||
|
||||
-- 3. 删除 tb_device.shop_id 字段
|
||||
DROP INDEX IF EXISTS idx_device_shop_id;
|
||||
ALTER TABLE tb_device DROP COLUMN IF EXISTS shop_id;
|
||||
|
||||
-- 4. 删除 tb_iot_card.shop_id 字段
|
||||
DROP INDEX IF EXISTS idx_iot_card_shop_id;
|
||||
ALTER TABLE tb_iot_card DROP COLUMN IF EXISTS shop_id;
|
||||
|
||||
-- 5. 删除 tb_commission_withdrawal_setting.daily_withdrawal_limit 字段
|
||||
ALTER TABLE tb_commission_withdrawal_setting DROP COLUMN IF EXISTS daily_withdrawal_limit;
|
||||
|
||||
-- 6. 删除 tb_commission_record 新增字段
|
||||
DROP INDEX IF EXISTS idx_commission_record_shop_id;
|
||||
ALTER TABLE tb_commission_record
|
||||
DROP COLUMN IF EXISTS shop_id,
|
||||
DROP COLUMN IF EXISTS balance_after;
|
||||
|
||||
-- 7. 删除 tb_account.is_primary 字段
|
||||
ALTER TABLE tb_account DROP COLUMN IF EXISTS is_primary;
|
||||
|
||||
-- 8. 删除 tb_commission_withdrawal_request 新增字段
|
||||
DROP INDEX IF EXISTS uk_commission_withdrawal_no;
|
||||
ALTER TABLE tb_commission_withdrawal_request
|
||||
DROP COLUMN IF EXISTS withdrawal_no,
|
||||
DROP COLUMN IF EXISTS applicant_id,
|
||||
DROP COLUMN IF EXISTS shop_id,
|
||||
DROP COLUMN IF EXISTS fee_rate,
|
||||
DROP COLUMN IF EXISTS payment_type,
|
||||
DROP COLUMN IF EXISTS processor_id,
|
||||
DROP COLUMN IF EXISTS processed_at,
|
||||
DROP COLUMN IF EXISTS remark;
|
||||
185
migrations/000010_add_commission_model_changes.up.sql
Normal file
185
migrations/000010_add_commission_model_changes.up.sql
Normal file
@@ -0,0 +1,185 @@
|
||||
-- 迁移: 账号与佣金管理模块数据模型变更
|
||||
-- 变更ID: add-commission-model-changes
|
||||
-- 说明:
|
||||
-- 1. 扩展现有表字段
|
||||
-- 2. 创建企业卡授权表和资产分配记录表
|
||||
-- 3. 统一 owner_type 枚举值 (agent -> shop)
|
||||
|
||||
-- ========================================
|
||||
-- 1. 扩展现有表字段
|
||||
-- ========================================
|
||||
|
||||
-- 1.1 tb_commission_withdrawal_request 佣金提现申请表新增字段
|
||||
ALTER TABLE tb_commission_withdrawal_request
|
||||
ADD COLUMN IF NOT EXISTS withdrawal_no VARCHAR(50),
|
||||
ADD COLUMN IF NOT EXISTS applicant_id BIGINT,
|
||||
ADD COLUMN IF NOT EXISTS shop_id BIGINT,
|
||||
ADD COLUMN IF NOT EXISTS fee_rate BIGINT DEFAULT 0,
|
||||
ADD COLUMN IF NOT EXISTS payment_type VARCHAR(20) DEFAULT 'manual',
|
||||
ADD COLUMN IF NOT EXISTS processor_id BIGINT,
|
||||
ADD COLUMN IF NOT EXISTS processed_at TIMESTAMP WITH TIME ZONE,
|
||||
ADD COLUMN IF NOT EXISTS remark TEXT;
|
||||
|
||||
-- 添加唯一索引(提现单号)
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uk_commission_withdrawal_no
|
||||
ON tb_commission_withdrawal_request(withdrawal_no) WHERE deleted_at IS NULL AND withdrawal_no IS NOT NULL;
|
||||
|
||||
-- 添加字段注释
|
||||
COMMENT ON COLUMN tb_commission_withdrawal_request.withdrawal_no IS '提现单号(唯一,格式:W + 时间戳 + 随机数)';
|
||||
COMMENT ON COLUMN tb_commission_withdrawal_request.applicant_id IS '申请人账号ID';
|
||||
COMMENT ON COLUMN tb_commission_withdrawal_request.shop_id IS '店铺ID(冗余字段)';
|
||||
COMMENT ON COLUMN tb_commission_withdrawal_request.fee_rate IS '手续费比率(基点,100=1%,快照)';
|
||||
COMMENT ON COLUMN tb_commission_withdrawal_request.payment_type IS '放款类型(manual=人工打款)';
|
||||
COMMENT ON COLUMN tb_commission_withdrawal_request.processor_id IS '处理人ID';
|
||||
COMMENT ON COLUMN tb_commission_withdrawal_request.processed_at IS '处理时间';
|
||||
COMMENT ON COLUMN tb_commission_withdrawal_request.remark IS '备注';
|
||||
|
||||
-- 1.2 tb_account 账号表新增字段
|
||||
ALTER TABLE tb_account
|
||||
ADD COLUMN IF NOT EXISTS is_primary BOOLEAN DEFAULT FALSE;
|
||||
|
||||
COMMENT ON COLUMN tb_account.is_primary IS '是否为店铺主账号(默认 false)';
|
||||
|
||||
-- 1.3 tb_commission_record 佣金记录表新增字段
|
||||
ALTER TABLE tb_commission_record
|
||||
ADD COLUMN IF NOT EXISTS shop_id BIGINT,
|
||||
ADD COLUMN IF NOT EXISTS balance_after BIGINT DEFAULT 0;
|
||||
|
||||
-- 添加索引
|
||||
CREATE INDEX IF NOT EXISTS idx_commission_record_shop_id ON tb_commission_record(shop_id);
|
||||
|
||||
COMMENT ON COLUMN tb_commission_record.shop_id IS '店铺ID(佣金主要跟着店铺走)';
|
||||
COMMENT ON COLUMN tb_commission_record.balance_after IS '入账后佣金余额(分)';
|
||||
|
||||
-- 1.4 tb_commission_withdrawal_setting 提现设置表新增字段
|
||||
ALTER TABLE tb_commission_withdrawal_setting
|
||||
ADD COLUMN IF NOT EXISTS daily_withdrawal_limit INT DEFAULT 3;
|
||||
|
||||
COMMENT ON COLUMN tb_commission_withdrawal_setting.daily_withdrawal_limit IS '每日提现次数限制';
|
||||
|
||||
-- 1.5 tb_iot_card 物联网卡表新增字段
|
||||
ALTER TABLE tb_iot_card
|
||||
ADD COLUMN IF NOT EXISTS shop_id BIGINT;
|
||||
|
||||
-- 添加索引
|
||||
CREATE INDEX IF NOT EXISTS idx_iot_card_shop_id ON tb_iot_card(shop_id);
|
||||
|
||||
COMMENT ON COLUMN tb_iot_card.shop_id IS '店铺ID(冗余字段,方便查询)';
|
||||
|
||||
-- 1.6 tb_device 设备表新增字段
|
||||
ALTER TABLE tb_device
|
||||
ADD COLUMN IF NOT EXISTS shop_id BIGINT;
|
||||
|
||||
-- 添加索引
|
||||
CREATE INDEX IF NOT EXISTS idx_device_shop_id ON tb_device(shop_id);
|
||||
|
||||
COMMENT ON COLUMN tb_device.shop_id IS '店铺ID(冗余字段,方便查询)';
|
||||
|
||||
-- ========================================
|
||||
-- 2. 创建新表
|
||||
-- ========================================
|
||||
|
||||
-- 2.1 tb_enterprise_card_authorization 企业卡授权表
|
||||
CREATE TABLE IF NOT EXISTS tb_enterprise_card_authorization (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
enterprise_id BIGINT NOT NULL,
|
||||
iot_card_id BIGINT NOT NULL,
|
||||
shop_id BIGINT NOT NULL,
|
||||
authorized_by BIGINT NOT NULL,
|
||||
authorized_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
status INT DEFAULT 1,
|
||||
creator BIGINT NOT NULL,
|
||||
updater BIGINT NOT NULL,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
deleted_at TIMESTAMP WITH TIME ZONE
|
||||
);
|
||||
|
||||
-- 添加唯一约束(企业+卡唯一)
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uk_enterprise_card
|
||||
ON tb_enterprise_card_authorization(enterprise_id, iot_card_id) WHERE deleted_at IS NULL;
|
||||
|
||||
-- 添加索引
|
||||
CREATE INDEX IF NOT EXISTS idx_enterprise_card_auth_enterprise_id ON tb_enterprise_card_authorization(enterprise_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_enterprise_card_auth_iot_card_id ON tb_enterprise_card_authorization(iot_card_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_enterprise_card_auth_shop_id ON tb_enterprise_card_authorization(shop_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_enterprise_card_auth_status ON tb_enterprise_card_authorization(status);
|
||||
|
||||
-- 添加表注释
|
||||
COMMENT ON TABLE tb_enterprise_card_authorization IS '企业卡授权表,记录企业被授权可见的卡';
|
||||
COMMENT ON COLUMN tb_enterprise_card_authorization.enterprise_id IS '企业ID';
|
||||
COMMENT ON COLUMN tb_enterprise_card_authorization.iot_card_id IS 'IoT卡ID';
|
||||
COMMENT ON COLUMN tb_enterprise_card_authorization.shop_id IS '店铺ID(授权方)';
|
||||
COMMENT ON COLUMN tb_enterprise_card_authorization.authorized_by IS '授权人ID';
|
||||
COMMENT ON COLUMN tb_enterprise_card_authorization.authorized_at IS '授权时间';
|
||||
COMMENT ON COLUMN tb_enterprise_card_authorization.status IS '状态 1=有效 0=已回收';
|
||||
COMMENT ON COLUMN tb_enterprise_card_authorization.creator IS '创建人ID';
|
||||
COMMENT ON COLUMN tb_enterprise_card_authorization.updater IS '更新人ID';
|
||||
|
||||
-- 2.2 tb_asset_allocation_record 资产分配记录表
|
||||
CREATE TABLE IF NOT EXISTS tb_asset_allocation_record (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
allocation_no VARCHAR(50) NOT NULL,
|
||||
allocation_type VARCHAR(20) NOT NULL,
|
||||
asset_type VARCHAR(20) NOT NULL,
|
||||
asset_id BIGINT NOT NULL,
|
||||
asset_identifier VARCHAR(50) NOT NULL,
|
||||
from_owner_type VARCHAR(20),
|
||||
from_owner_id BIGINT,
|
||||
to_owner_type VARCHAR(20) NOT NULL,
|
||||
to_owner_id BIGINT NOT NULL,
|
||||
related_device_id BIGINT,
|
||||
related_card_ids JSONB,
|
||||
operator_id BIGINT NOT NULL,
|
||||
remark TEXT,
|
||||
creator BIGINT NOT NULL,
|
||||
updater BIGINT NOT NULL,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
deleted_at TIMESTAMP WITH TIME ZONE
|
||||
);
|
||||
|
||||
-- 添加唯一约束(分配单号唯一)
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uk_asset_allocation_no
|
||||
ON tb_asset_allocation_record(allocation_no) WHERE deleted_at IS NULL;
|
||||
|
||||
-- 添加索引
|
||||
CREATE INDEX IF NOT EXISTS idx_asset_allocation_type ON tb_asset_allocation_record(allocation_type);
|
||||
CREATE INDEX IF NOT EXISTS idx_asset_allocation_asset_type ON tb_asset_allocation_record(asset_type);
|
||||
CREATE INDEX IF NOT EXISTS idx_asset_allocation_asset_id ON tb_asset_allocation_record(asset_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_asset_allocation_from_owner ON tb_asset_allocation_record(from_owner_type, from_owner_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_asset_allocation_to_owner ON tb_asset_allocation_record(to_owner_type, to_owner_id);
|
||||
|
||||
-- 添加表注释
|
||||
COMMENT ON TABLE tb_asset_allocation_record IS '资产分配记录表,记录卡/设备在平台和代理商之间的流转历史';
|
||||
COMMENT ON COLUMN tb_asset_allocation_record.allocation_no IS '分配单号(唯一)';
|
||||
COMMENT ON COLUMN tb_asset_allocation_record.allocation_type IS '分配类型 allocate=分配 recall=回收';
|
||||
COMMENT ON COLUMN tb_asset_allocation_record.asset_type IS '资产类型 iot_card=物联网卡 device=设备';
|
||||
COMMENT ON COLUMN tb_asset_allocation_record.asset_id IS '资产ID';
|
||||
COMMENT ON COLUMN tb_asset_allocation_record.asset_identifier IS '资产标识符(ICCID或设备号)';
|
||||
COMMENT ON COLUMN tb_asset_allocation_record.from_owner_type IS '来源所有者类型';
|
||||
COMMENT ON COLUMN tb_asset_allocation_record.from_owner_id IS '来源所有者ID';
|
||||
COMMENT ON COLUMN tb_asset_allocation_record.to_owner_type IS '目标所有者类型';
|
||||
COMMENT ON COLUMN tb_asset_allocation_record.to_owner_id IS '目标所有者ID';
|
||||
COMMENT ON COLUMN tb_asset_allocation_record.related_device_id IS '关联设备ID';
|
||||
COMMENT ON COLUMN tb_asset_allocation_record.related_card_ids IS '关联卡ID列表';
|
||||
COMMENT ON COLUMN tb_asset_allocation_record.operator_id IS '操作人ID';
|
||||
COMMENT ON COLUMN tb_asset_allocation_record.remark IS '备注';
|
||||
COMMENT ON COLUMN tb_asset_allocation_record.creator IS '创建人ID';
|
||||
COMMENT ON COLUMN tb_asset_allocation_record.updater IS '更新人ID';
|
||||
|
||||
-- ========================================
|
||||
-- 3. 数据迁移 - owner_type 枚举统一
|
||||
-- ========================================
|
||||
|
||||
-- 3.1 更新 tb_iot_card 表 owner_type='agent' 为 'shop'
|
||||
UPDATE tb_iot_card SET owner_type = 'shop' WHERE owner_type = 'agent';
|
||||
|
||||
-- 3.2 更新 tb_device 表 owner_type='agent' 为 'shop'
|
||||
UPDATE tb_device SET owner_type = 'shop' WHERE owner_type = 'agent';
|
||||
|
||||
-- 3.3 填充 tb_iot_card.shop_id 字段(owner_type='shop' 时等于 owner_id)
|
||||
UPDATE tb_iot_card SET shop_id = owner_id WHERE owner_type = 'shop' AND shop_id IS NULL;
|
||||
|
||||
-- 3.4 填充 tb_device.shop_id 字段(owner_type='shop' 时等于 owner_id)
|
||||
UPDATE tb_device SET shop_id = owner_id WHERE owner_type = 'shop' AND shop_id IS NULL;
|
||||
@@ -0,0 +1,180 @@
|
||||
# Change: 账号与佣金管理模块 - 数据模型变更
|
||||
|
||||
## Why
|
||||
|
||||
账号与佣金管理模块需要扩展现有数据模型以支持以下业务场景:
|
||||
1. 佣金提现申请需要记录完整的审批流程信息(提现单号、申请人、处理人等)
|
||||
2. 店铺主账号标识,用于在代理商列表中显示主账号信息
|
||||
3. 企业客户卡授权机制,允许企业"看到"代理商的卡而不改变归属
|
||||
4. 卡/设备归属体系统一,简化 `owner_type` 枚举值
|
||||
|
||||
这是账号与佣金管理模块的**基础依赖提案**,后续所有功能提案都依赖此数据模型变更。
|
||||
|
||||
## What Changes
|
||||
|
||||
### 1. 表字段新增
|
||||
|
||||
#### 1.1 `tb_commission_withdrawal_request` 佣金提现申请表
|
||||
| 字段名 | 类型 | 说明 |
|
||||
|--------|------|------|
|
||||
| `withdrawal_no` | varchar(50) | 提现单号(唯一,格式:W + 时间戳 + 随机数) |
|
||||
| `applicant_id` | uint | 申请人账号ID |
|
||||
| `shop_id` | uint | 店铺ID(冗余字段) |
|
||||
| `fee_rate` | int64 | 手续费比率(基点,100=1%,快照) |
|
||||
| `payment_type` | varchar(20) | 放款类型(manual=人工打款) |
|
||||
| `processor_id` | uint | 处理人ID |
|
||||
| `processed_at` | timestamp | 处理时间 |
|
||||
| `remark` | text | 备注 |
|
||||
|
||||
#### 1.2 `tb_account` 账号表
|
||||
| 字段名 | 类型 | 说明 |
|
||||
|--------|------|------|
|
||||
| `is_primary` | boolean | 是否为店铺主账号(默认 false) |
|
||||
|
||||
#### 1.3 `tb_commission_record` 佣金记录表
|
||||
| 字段名 | 类型 | 说明 |
|
||||
|--------|------|------|
|
||||
| `shop_id` | uint | 店铺ID(佣金主要跟着店铺走) |
|
||||
| `balance_after` | int64 | 入账后佣金余额(分) |
|
||||
|
||||
#### 1.4 `tb_commission_withdrawal_setting` 提现设置表
|
||||
| 字段名 | 类型 | 说明 |
|
||||
|--------|------|------|
|
||||
| `daily_withdrawal_limit` | int | 每日提现次数限制 |
|
||||
|
||||
#### 1.5 `tb_iot_card` 物联网卡表
|
||||
| 字段名 | 类型 | 说明 |
|
||||
|--------|------|------|
|
||||
| `shop_id` | uint | 店铺ID(冗余字段,方便查询) |
|
||||
|
||||
#### 1.6 `tb_device` 设备表
|
||||
| 字段名 | 类型 | 说明 |
|
||||
|--------|------|------|
|
||||
| `shop_id` | uint | 店铺ID(冗余字段,方便查询) |
|
||||
|
||||
### 2. 新增表
|
||||
|
||||
#### 2.1 `tb_enterprise_card_authorization` 企业卡授权表
|
||||
用于记录企业被授权可见的卡。**这是企业查看卡的唯一途径,不改变卡的归属**。
|
||||
|
||||
```sql
|
||||
CREATE TABLE tb_enterprise_card_authorization (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
enterprise_id BIGINT NOT NULL,
|
||||
iot_card_id BIGINT NOT NULL,
|
||||
shop_id BIGINT NOT NULL,
|
||||
authorized_by BIGINT NOT NULL,
|
||||
authorized_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
status INT DEFAULT 1, -- 1=有效, 0=已回收
|
||||
creator BIGINT,
|
||||
updater BIGINT,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
deleted_at TIMESTAMP WITH TIME ZONE,
|
||||
CONSTRAINT uk_enterprise_card UNIQUE(enterprise_id, iot_card_id)
|
||||
);
|
||||
```
|
||||
|
||||
#### 2.2 `tb_asset_allocation_record` 资产分配记录表
|
||||
用于记录卡/设备在平台和代理商之间流转的历史。
|
||||
|
||||
```sql
|
||||
CREATE TABLE tb_asset_allocation_record (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
allocation_no VARCHAR(50) NOT NULL UNIQUE,
|
||||
allocation_type VARCHAR(20) NOT NULL, -- allocate/recall
|
||||
asset_type VARCHAR(20) NOT NULL, -- iot_card/device
|
||||
asset_id BIGINT NOT NULL,
|
||||
asset_identifier VARCHAR(50) NOT NULL,
|
||||
from_owner_type VARCHAR(20),
|
||||
from_owner_id BIGINT,
|
||||
to_owner_type VARCHAR(20) NOT NULL,
|
||||
to_owner_id BIGINT NOT NULL,
|
||||
related_device_id BIGINT,
|
||||
related_card_ids JSONB,
|
||||
operator_id BIGINT NOT NULL,
|
||||
remark TEXT,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
deleted_at TIMESTAMP WITH TIME ZONE
|
||||
);
|
||||
```
|
||||
|
||||
### 3. 枚举值统一
|
||||
|
||||
**`owner_type` 字段值变更**(`tb_iot_card` 和 `tb_device` 表):
|
||||
|
||||
| 旧值 | 新值 | 说明 |
|
||||
|------|------|------|
|
||||
| `platform` | `platform` | 不变 |
|
||||
| `agent` | `shop` | 统一命名 |
|
||||
| `user` | 废弃 | 不再使用 |
|
||||
| `device` | 废弃 | 不再使用 |
|
||||
|
||||
## Impact
|
||||
|
||||
### 影响的规范
|
||||
- **新增 Capability**:`commission-model`(佣金数据模型)
|
||||
- **修改 Capability**:`iot-card`(新增 `shop_id` 字段)
|
||||
- **修改 Capability**:`iot-device`(新增 `shop_id` 字段)
|
||||
|
||||
### 影响的代码
|
||||
|
||||
**迁移文件**(新增):
|
||||
- `migrations/XXXXXX_add_commission_model_changes.up.sql`
|
||||
- `migrations/XXXXXX_add_commission_model_changes.down.sql`
|
||||
|
||||
**Model 文件**(修改):
|
||||
- `internal/model/commission.go`(新增字段)
|
||||
- `internal/model/account.go`(新增 `is_primary` 字段)
|
||||
- `internal/model/iot_card.go`(新增 `shop_id` 字段)
|
||||
- `internal/model/device.go`(新增 `shop_id` 字段)
|
||||
|
||||
**Model 文件**(新增):
|
||||
- `internal/model/enterprise_card_authorization.go`
|
||||
- `internal/model/asset_allocation_record.go`
|
||||
|
||||
**常量文件**(修改):
|
||||
- `pkg/constants/owner_type.go`(统一枚举值)
|
||||
|
||||
### 兼容性
|
||||
|
||||
- **BREAKING**:`owner_type` 枚举值变更(`agent` → `shop`),需要数据迁移
|
||||
- 数据库迁移需要更新现有数据的 `owner_type` 值
|
||||
- 现有代码中引用 `agent` 的地方需要改为 `shop`
|
||||
|
||||
### 风险评估
|
||||
|
||||
- **中等风险**:涉及数据迁移和枚举值变更
|
||||
- **缓解措施**:
|
||||
1. 迁移脚本包含数据转换逻辑
|
||||
2. 提供回滚脚本
|
||||
3. 在测试环境充分验证
|
||||
|
||||
## Dependencies
|
||||
|
||||
- 无外部依赖
|
||||
- 后续提案依赖此提案:
|
||||
- `add-shop-commission-query`
|
||||
- `add-commission-withdrawal-approval`
|
||||
- `add-commission-withdrawal-settings`
|
||||
- `add-enterprise-management`
|
||||
- `add-enterprise-card-authorization`
|
||||
- `add-customer-account-management`
|
||||
- `add-my-commission`
|
||||
|
||||
## Testing Strategy
|
||||
|
||||
1. **迁移测试**:
|
||||
- 验证 up 迁移成功执行
|
||||
- 验证 down 迁移可以回滚
|
||||
- 验证数据转换正确(`agent` → `shop`)
|
||||
|
||||
2. **Model 测试**:
|
||||
- 新增字段可正常读写
|
||||
- 新增表 CRUD 操作正常
|
||||
|
||||
## Documentation
|
||||
|
||||
- 更新 `README.md` 数据模型说明
|
||||
- 在 `docs/` 目录创建数据模型变更说明
|
||||
@@ -0,0 +1,142 @@
|
||||
## ADDED Requirements
|
||||
|
||||
### Requirement: 佣金提现申请扩展字段
|
||||
系统 SHALL 在佣金提现申请表中支持以下扩展字段:
|
||||
- 提现单号(`withdrawal_no`):唯一标识,格式 W + 时间戳 + 随机数
|
||||
- 申请人ID(`applicant_id`):提交申请的账号ID
|
||||
- 店铺ID(`shop_id`):冗余字段,方便查询
|
||||
- 手续费比率(`fee_rate`):申请时的费率快照,基点单位
|
||||
- 放款类型(`payment_type`):如 manual(人工打款)
|
||||
- 处理人ID(`processor_id`):审批/放款人
|
||||
- 处理时间(`processed_at`):审批时间
|
||||
- 备注(`remark`):审批备注
|
||||
|
||||
#### Scenario: 创建提现申请时自动生成提现单号
|
||||
- **WHEN** 代理商发起提现申请
|
||||
- **THEN** 系统自动生成唯一提现单号
|
||||
- **AND** 记录申请人ID和店铺ID
|
||||
- **AND** 记录当前生效的手续费比率
|
||||
|
||||
#### Scenario: 审批提现申请时记录处理信息
|
||||
- **WHEN** 管理员审批(通过或拒绝)提现申请
|
||||
- **THEN** 系统记录处理人ID和处理时间
|
||||
- **AND** 可选记录备注信息
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 店铺主账号标识
|
||||
系统 SHALL 支持标识店铺的主账号,通过 `is_primary` 字段区分。
|
||||
|
||||
#### Scenario: 创建店铺时标记主账号
|
||||
- **WHEN** 创建店铺时同步创建账号
|
||||
- **THEN** 该账号的 `is_primary` 字段设置为 `true`
|
||||
|
||||
#### Scenario: 查询店铺主账号
|
||||
- **WHEN** 查询代理商列表
|
||||
- **THEN** 可以关联查询每个店铺的主账号信息(用户名、手机号)
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 佣金记录店铺关联
|
||||
系统 SHALL 在佣金记录表中支持店铺关联:
|
||||
- 店铺ID(`shop_id`):佣金主要跟着店铺走
|
||||
- 入账后余额(`balance_after`):记录每次入账后的累计余额
|
||||
|
||||
#### Scenario: 创建佣金记录时关联店铺
|
||||
- **WHEN** 系统创建佣金记录
|
||||
- **THEN** 记录对应的店铺ID
|
||||
- **AND** 计算并记录入账后的佣金余额
|
||||
|
||||
#### Scenario: 按店铺查询佣金明细
|
||||
- **WHEN** 查询某店铺的佣金明细
|
||||
- **THEN** 可以直接通过 `shop_id` 字段过滤
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 提现设置每日限制
|
||||
系统 SHALL 支持配置每日提现次数限制,通过 `daily_withdrawal_limit` 字段。
|
||||
|
||||
#### Scenario: 配置每日提现次数
|
||||
- **WHEN** 管理员新增提现设置
|
||||
- **THEN** 可以设置每日提现次数限制
|
||||
|
||||
#### Scenario: 验证每日提现次数
|
||||
- **WHEN** 代理商发起提现申请
|
||||
- **THEN** 系统检查今日已提现次数是否超过限制
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 卡/设备店铺冗余字段
|
||||
系统 SHALL 在物联网卡表和设备表中支持店铺ID冗余字段(`shop_id`),方便数据权限过滤。
|
||||
|
||||
#### Scenario: 分配卡给代理商时设置 shop_id
|
||||
- **WHEN** 将卡从平台分配给代理商
|
||||
- **THEN** 设置卡的 `shop_id` 为目标店铺ID
|
||||
|
||||
#### Scenario: 代理商查询卡列表时按 shop_id 过滤
|
||||
- **WHEN** 代理商用户查询卡列表
|
||||
- **THEN** 系统使用 `shop_id` 字段进行数据权限过滤
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 企业卡授权表
|
||||
系统 SHALL 提供企业卡授权表(`tb_enterprise_card_authorization`),记录企业被授权可见的卡。
|
||||
|
||||
**核心设计**:
|
||||
- 卡的归属(owner)始终是代理商店铺,不会变成企业
|
||||
- 企业通过授权表"看到"被授权的卡
|
||||
- 授权是永久的,回收时更新 `status=0`
|
||||
|
||||
#### Scenario: 授权卡给企业
|
||||
- **WHEN** 代理商将卡授权给企业
|
||||
- **THEN** 创建授权记录,状态为有效(`status=1`)
|
||||
- **AND** 记录授权人和授权时间
|
||||
- **AND** 卡的 owner 不变,仍属于代理商
|
||||
|
||||
#### Scenario: 回收卡授权
|
||||
- **WHEN** 代理商回收企业的卡授权
|
||||
- **THEN** 更新授权记录状态为已回收(`status=0`)
|
||||
- **AND** 卡的 owner 不变
|
||||
|
||||
#### Scenario: 企业查询被授权的卡
|
||||
- **WHEN** 企业用户查询卡列表
|
||||
- **THEN** 系统通过授权表过滤,只返回被授权且有效的卡
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 资产分配记录表
|
||||
系统 SHALL 提供资产分配记录表(`tb_asset_allocation_record`),记录卡/设备在平台和代理商之间的流转历史。
|
||||
|
||||
#### Scenario: 记录卡分配
|
||||
- **WHEN** 平台将卡分配给代理商
|
||||
- **THEN** 创建分配记录,类型为 `allocate`
|
||||
- **AND** 记录来源(平台)和目标(店铺)
|
||||
|
||||
#### Scenario: 记录卡回收
|
||||
- **WHEN** 从代理商回收卡到平台
|
||||
- **THEN** 创建分配记录,类型为 `recall`
|
||||
- **AND** 记录来源(店铺)和目标(平台)
|
||||
|
||||
#### Scenario: 查询资产流转历史
|
||||
- **WHEN** 查询某卡或设备的分配历史
|
||||
- **THEN** 返回完整的流转记录列表
|
||||
|
||||
---
|
||||
|
||||
### Requirement: owner_type 枚举统一
|
||||
系统 SHALL 统一卡/设备的 `owner_type` 枚举值:
|
||||
- `platform`:平台库存
|
||||
- `shop`:代理商持有
|
||||
|
||||
**废弃值**:
|
||||
- `agent`:改为 `shop`
|
||||
- `user`:不再使用
|
||||
- `device`:不再使用
|
||||
|
||||
#### Scenario: 迁移现有数据
|
||||
- **WHEN** 执行数据库迁移
|
||||
- **THEN** 将现有 `owner_type='agent'` 的记录更新为 `owner_type='shop'`
|
||||
|
||||
#### Scenario: 新数据使用统一枚举
|
||||
- **WHEN** 创建或更新卡/设备归属
|
||||
- **THEN** `owner_type` 只能是 `platform` 或 `shop`
|
||||
@@ -0,0 +1,171 @@
|
||||
# 实现任务清单
|
||||
|
||||
**Change ID**: `add-commission-model-changes`
|
||||
|
||||
---
|
||||
|
||||
## 阶段 1: 数据库迁移 (1-2 小时)
|
||||
|
||||
### Task 1.1: 创建迁移文件
|
||||
|
||||
**文件**: `migrations/000010_add_commission_model_changes.up.sql`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 1.1.1 新增 `tb_commission_withdrawal_request` 表字段
|
||||
- [x] 1.1.2 新增 `tb_account.is_primary` 字段
|
||||
- [x] 1.1.3 新增 `tb_commission_record` 表字段(`shop_id`, `balance_after`)
|
||||
- [x] 1.1.4 新增 `tb_commission_withdrawal_setting.daily_withdrawal_limit` 字段
|
||||
- [x] 1.1.5 新增 `tb_iot_card.shop_id` 字段
|
||||
- [x] 1.1.6 新增 `tb_device.shop_id` 字段
|
||||
- [x] 1.1.7 创建 `tb_enterprise_card_authorization` 表
|
||||
- [x] 1.1.8 创建 `tb_asset_allocation_record` 表
|
||||
- [x] 1.1.9 创建必要的索引
|
||||
|
||||
**验证**:
|
||||
- [x] 迁移脚本语法正确
|
||||
- [x] 字段类型与需求文档一致
|
||||
|
||||
---
|
||||
|
||||
### Task 1.2: 数据迁移 - owner_type 枚举统一
|
||||
|
||||
**文件**: `migrations/000010_add_commission_model_changes.up.sql`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 1.2.1 更新 `tb_iot_card` 表 `owner_type='agent'` 为 `owner_type='shop'`
|
||||
- [x] 1.2.2 更新 `tb_device` 表 `owner_type='agent'` 为 `owner_type='shop'`
|
||||
- [x] 1.2.3 填充 `tb_iot_card.shop_id` 字段(`owner_type='shop'` 时等于 `owner_id`)
|
||||
- [x] 1.2.4 填充 `tb_device.shop_id` 字段(`owner_type='shop'` 时等于 `owner_id`)
|
||||
|
||||
**验证**:
|
||||
- [x] 数据迁移逻辑正确
|
||||
- [x] 无数据丢失
|
||||
|
||||
---
|
||||
|
||||
### Task 1.3: 创建回滚迁移
|
||||
|
||||
**文件**: `migrations/000010_add_commission_model_changes.down.sql`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 1.3.1 删除新增的表字段
|
||||
- [x] 1.3.2 删除新增的表
|
||||
- [x] 1.3.3 恢复 `owner_type` 枚举值(`shop` → `agent`)
|
||||
|
||||
**验证**:
|
||||
- [x] 回滚脚本可以正确执行
|
||||
- [x] 回滚后数据库状态正确
|
||||
|
||||
---
|
||||
|
||||
## 阶段 2: Model 更新 (1 小时)
|
||||
|
||||
### Task 2.1: 更新现有 Model
|
||||
|
||||
**文件**:
|
||||
- `internal/model/financial.go`
|
||||
- `internal/model/commission.go`
|
||||
- `internal/model/account.go`
|
||||
- `internal/model/iot_card.go`
|
||||
- `internal/model/device.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 2.1.1 `CommissionWithdrawalRequest` 新增字段
|
||||
- [x] 2.1.2 `Account` 新增 `IsPrimary` 字段
|
||||
- [x] 2.1.3 `CommissionRecord` 新增 `ShopID`, `BalanceAfter` 字段
|
||||
- [x] 2.1.4 `CommissionWithdrawalSetting` 新增 `DailyWithdrawalLimit` 字段
|
||||
- [x] 2.1.5 `IotCard` 新增 `ShopID` 字段
|
||||
- [x] 2.1.6 `Device` 新增 `ShopID` 字段
|
||||
|
||||
**验证**:
|
||||
- [x] 字段标签正确(`gorm`, `json`)
|
||||
- [x] 字段类型与数据库一致
|
||||
|
||||
---
|
||||
|
||||
### Task 2.2: 新增 Model
|
||||
|
||||
**文件**:
|
||||
- `internal/model/enterprise_card_authorization.go`
|
||||
- `internal/model/asset_allocation_record.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 2.2.1 创建 `EnterpriseCardAuthorization` 模型
|
||||
- [x] 2.2.2 创建 `AssetAllocationRecord` 模型
|
||||
- [x] 2.2.3 实现 `TableName()` 方法
|
||||
|
||||
**验证**:
|
||||
- [x] 模型定义完整
|
||||
- [x] 遵循项目 Model 规范
|
||||
|
||||
---
|
||||
|
||||
### Task 2.3: 更新常量定义
|
||||
|
||||
**文件**: `pkg/constants/iot.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 2.3.1 更新 `OwnerType` 常量(移除 `agent`, `user`, `device`,保留 `platform`, `shop`)
|
||||
- [x] 2.3.2 新增 `EnterpriseCardAuthorizationStatus` 常量
|
||||
- [x] 2.3.3 新增 `AssetAllocationType` 常量
|
||||
- [x] 2.3.4 新增 `PaymentType` 常量
|
||||
- [x] 2.3.5 新增 Redis Key 生成函数(如有需要)
|
||||
|
||||
**验证**:
|
||||
- [x] 常量命名符合规范
|
||||
- [x] 中文注释完整
|
||||
|
||||
---
|
||||
|
||||
## 阶段 3: 代码兼容性修复 (30 分钟)
|
||||
|
||||
### Task 3.1: 更新现有代码中的 owner_type 引用
|
||||
|
||||
**实现内容**:
|
||||
- [x] 3.1.1 全局搜索 `owner_type.*agent` 引用
|
||||
- [x] 3.1.2 更新为 `shop`
|
||||
- [x] 3.1.3 验证无遗漏
|
||||
|
||||
**验证**:
|
||||
- [x] 编译通过
|
||||
- [x] 无运行时错误
|
||||
|
||||
---
|
||||
|
||||
## 阶段 4: 验证 (30 分钟)
|
||||
|
||||
### Task 4.1: 执行迁移
|
||||
|
||||
**实现内容**:
|
||||
- [x] 4.1.1 在开发环境执行迁移
|
||||
- [x] 4.1.2 验证表结构正确
|
||||
- [x] 4.1.3 验证数据迁移正确
|
||||
- [x] 4.1.4 验证索引创建正确
|
||||
|
||||
**验证**:
|
||||
- [x] `migrate up` 成功
|
||||
- [x] `migrate down` 可以回滚
|
||||
|
||||
---
|
||||
|
||||
### Task 4.2: Model 验证
|
||||
|
||||
**实现内容**:
|
||||
- [x] 4.2.1 验证 Model 与数据库表结构一致
|
||||
- [x] 4.2.2 简单 CRUD 测试
|
||||
- [x] 4.2.3 验证 GORM 自动迁移无冲突
|
||||
|
||||
**验证**:
|
||||
- [x] 所有新字段可正常读写
|
||||
- [x] 新表 CRUD 正常
|
||||
|
||||
---
|
||||
|
||||
## 完成标准
|
||||
|
||||
- [x] 所有迁移文件创建完成
|
||||
- [x] 所有 Model 更新完成
|
||||
- [x] 常量定义更新完成
|
||||
- [x] 代码兼容性修复完成
|
||||
- [x] 迁移执行成功
|
||||
- [x] 编译通过,无错误
|
||||
@@ -0,0 +1,80 @@
|
||||
# Change: 佣金提现审批模块
|
||||
|
||||
## Why
|
||||
|
||||
平台需要对代理商的佣金提现申请进行审批管理:
|
||||
1. 查看所有待处理的提现申请列表
|
||||
2. 审批通过提现申请(扣除佣金、记录流水)
|
||||
3. 拒绝提现申请(解冻佣金)
|
||||
|
||||
这是账号管理-佣金提现模块的核心功能。
|
||||
|
||||
## What Changes
|
||||
|
||||
### 新增 API 接口
|
||||
|
||||
| 方法 | 路径 | 说明 |
|
||||
|------|------|------|
|
||||
| GET | `/api/admin/commission/withdrawal-requests` | 提现申请列表(审批视图) |
|
||||
| POST | `/api/admin/commission/withdrawal-requests/:id/approve` | 审批通过 |
|
||||
| POST | `/api/admin/commission/withdrawal-requests/:id/reject` | 拒绝 |
|
||||
|
||||
### 技术实现
|
||||
|
||||
- 新增 Handler:`internal/handler/admin/commission_withdrawal.go`
|
||||
- 新增 Service:`internal/service/commission_withdrawal/service.go`
|
||||
- 新增 DTO:`internal/model/dto/commission_withdrawal_dto.go`
|
||||
- 扩展 Store:钱包操作、流水记录
|
||||
|
||||
### 业务逻辑
|
||||
|
||||
**审批通过流程**:
|
||||
1. 验证提现申请存在且状态为待审批
|
||||
2. 验证当前用户有审批权限
|
||||
3. 如果修正了金额,重新计算手续费和实际到账金额
|
||||
4. 更新状态为已通过(status=2)
|
||||
5. 从店铺佣金钱包扣除对应金额(解冻并扣除)
|
||||
6. 记录钱包交易流水
|
||||
7. 记录处理人和处理时间
|
||||
|
||||
**拒绝流程**:
|
||||
1. 验证提现申请存在且状态为待审批
|
||||
2. 更新状态为已拒绝(status=3)
|
||||
3. 解冻店铺佣金钱包中的冻结金额
|
||||
4. 记录钱包交易流水
|
||||
5. 记录处理人、处理时间和拒绝原因
|
||||
|
||||
**审批状态**:
|
||||
- 1:待审批
|
||||
- 2:已通过
|
||||
- 3:已拒绝
|
||||
|
||||
## Impact
|
||||
|
||||
### 影响的规范
|
||||
- **新增 Capability**:`commission-withdrawal-approval`
|
||||
|
||||
### 影响的代码
|
||||
|
||||
**新增文件**(约 350 行):
|
||||
- `internal/handler/admin/commission_withdrawal.go`(~100 行)
|
||||
- `internal/service/commission_withdrawal/service.go`(~200 行)
|
||||
- `internal/model/dto/commission_withdrawal_dto.go`(~50 行)
|
||||
|
||||
**修改文件**(约 50 行):
|
||||
- `internal/store/postgres/wallet_store.go`(扣款、解冻方法)
|
||||
- `internal/store/postgres/wallet_transaction_store.go`(创建流水)
|
||||
|
||||
### 兼容性
|
||||
- ✅ 向后兼容:新增 API,不影响现有功能
|
||||
|
||||
## Dependencies
|
||||
|
||||
- 依赖提案:`add-commission-model-changes`
|
||||
- 依赖现有模型:`CommissionWithdrawalRequest`、`Wallet`、`WalletTransaction`
|
||||
|
||||
## Testing Strategy
|
||||
|
||||
1. **单元测试**:审批流程、钱包操作
|
||||
2. **集成测试**:完整审批流程(申请→通过/拒绝)
|
||||
3. **并发测试**:同一申请的并发审批处理
|
||||
@@ -0,0 +1,123 @@
|
||||
## ADDED Requirements
|
||||
|
||||
### Requirement: 提现申请列表查询
|
||||
系统 SHALL 提供提现申请列表查询接口,用于审批管理。
|
||||
|
||||
**接口**:`GET /api/admin/commission/withdrawal-requests`
|
||||
|
||||
**请求参数**:
|
||||
- `page`、`page_size`:分页
|
||||
- `status`:状态筛选(1=待审批, 2=已通过, 3=已拒绝)
|
||||
- `withdrawal_no`:提现单号(精确查询)
|
||||
- `shop_name`:店铺名称(模糊查询)
|
||||
- `start_time`、`end_time`:申请时间范围
|
||||
|
||||
**响应字段**:
|
||||
- 提现申请详情(id, withdrawal_no, amount, fee_rate, fee, actual_amount)
|
||||
- 店铺信息(shop_id, shop_name, shop_hierarchy)
|
||||
- 申请人信息(applicant_id, applicant_name)
|
||||
- 状态信息(status, status_name)
|
||||
- 收款信息(withdrawal_method, account_name, account_number)
|
||||
- 处理信息(processor_id, processor_name, processed_at, remark)
|
||||
|
||||
#### Scenario: 查询待审批的提现申请
|
||||
- **WHEN** 请求 `status=1` 的提现申请
|
||||
- **THEN** 返回所有待审批的申请
|
||||
- **AND** 按申请时间倒序排列
|
||||
|
||||
#### Scenario: 平台用户查看所有申请
|
||||
- **WHEN** 平台用户请求提现申请列表
|
||||
- **THEN** 返回所有店铺的提现申请
|
||||
|
||||
#### Scenario: 代理商用户查看下级申请
|
||||
- **WHEN** 代理商用户请求提现申请列表
|
||||
- **THEN** 只返回自己店铺及下级店铺的申请
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 审批通过提现申请
|
||||
系统 SHALL 提供审批通过提现申请的接口。
|
||||
|
||||
**接口**:`POST /api/admin/commission/withdrawal-requests/:id/approve`
|
||||
|
||||
**请求参数**:
|
||||
- `id`:提现申请ID(路径参数)
|
||||
- `payment_type`:放款类型(必填,目前只支持 manual)
|
||||
- `amount`:修正后的提现金额(可选)
|
||||
- `withdrawal_method`:修正后的收款类型(可选)
|
||||
- `account_name`:修正后的收款人姓名(可选)
|
||||
- `account_number`:修正后的收款账号(可选)
|
||||
- `remark`:备注(可选)
|
||||
|
||||
**响应字段**:
|
||||
- `id`、`withdrawal_no`、`status`、`status_name`、`processed_at`
|
||||
|
||||
#### Scenario: 审批通过待审批的申请
|
||||
- **WHEN** 管理员审批通过一个待审批的提现申请
|
||||
- **THEN** 申请状态变为已通过(status=2)
|
||||
- **AND** 记录处理人ID和处理时间
|
||||
- **AND** 从店铺佣金钱包扣除提现金额(从冻结余额扣除)
|
||||
- **AND** 创建钱包交易流水记录
|
||||
|
||||
#### Scenario: 修正提现金额后审批
|
||||
- **WHEN** 管理员修正提现金额后审批通过
|
||||
- **THEN** 重新计算手续费和实际到账金额
|
||||
- **AND** 按修正后的金额扣款
|
||||
- **AND** 如果修正金额小于原金额,退回差额到可用余额
|
||||
|
||||
#### Scenario: 审批非待审批状态的申请
|
||||
- **WHEN** 尝试审批非待审批状态的申请
|
||||
- **THEN** 返回错误:申请状态不允许此操作
|
||||
|
||||
#### Scenario: 钱包余额不足
|
||||
- **WHEN** 店铺佣金钱包冻结余额不足
|
||||
- **THEN** 返回错误:钱包余额不足
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 拒绝提现申请
|
||||
系统 SHALL 提供拒绝提现申请的接口。
|
||||
|
||||
**接口**:`POST /api/admin/commission/withdrawal-requests/:id/reject`
|
||||
|
||||
**请求参数**:
|
||||
- `id`:提现申请ID(路径参数)
|
||||
- `remark`:拒绝原因(必填)
|
||||
|
||||
**响应字段**:
|
||||
- `id`、`withdrawal_no`、`status`、`status_name`、`processed_at`
|
||||
|
||||
#### Scenario: 拒绝待审批的申请
|
||||
- **WHEN** 管理员拒绝一个待审批的提现申请
|
||||
- **THEN** 申请状态变为已拒绝(status=3)
|
||||
- **AND** 记录处理人ID、处理时间和拒绝原因
|
||||
- **AND** 解冻店铺佣金钱包中的冻结金额
|
||||
- **AND** 创建钱包交易流水记录(解冻类型)
|
||||
|
||||
#### Scenario: 拒绝时必须填写原因
|
||||
- **WHEN** 拒绝申请时未填写 remark
|
||||
- **THEN** 返回错误:拒绝原因不能为空
|
||||
|
||||
#### Scenario: 拒绝非待审批状态的申请
|
||||
- **WHEN** 尝试拒绝非待审批状态的申请
|
||||
- **THEN** 返回错误:申请状态不允许此操作
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 审批事务一致性
|
||||
系统 SHALL 确保审批操作的事务一致性。
|
||||
|
||||
#### Scenario: 审批通过事务
|
||||
- **WHEN** 审批通过提现申请
|
||||
- **THEN** 状态更新、钱包扣款、流水记录在同一事务中完成
|
||||
- **AND** 任一步骤失败则全部回滚
|
||||
|
||||
#### Scenario: 拒绝事务
|
||||
- **WHEN** 拒绝提现申请
|
||||
- **THEN** 状态更新、解冻余额、流水记录在同一事务中完成
|
||||
- **AND** 任一步骤失败则全部回滚
|
||||
|
||||
#### Scenario: 并发审批防护
|
||||
- **WHEN** 多个管理员同时审批同一申请
|
||||
- **THEN** 只有一个操作成功
|
||||
- **AND** 其他操作返回状态冲突错误
|
||||
@@ -0,0 +1,155 @@
|
||||
# 实现任务清单
|
||||
|
||||
**Change ID**: `add-commission-withdrawal-approval`
|
||||
|
||||
---
|
||||
|
||||
## 阶段 1: DTO 定义 (20 分钟)
|
||||
|
||||
### Task 1.1: 创建 DTO 文件
|
||||
|
||||
**文件**: `internal/model/commission_withdrawal_dto.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 1.1.1 `WithdrawalRequestListReq` 请求结构(分页、状态、时间范围等)
|
||||
- [x] 1.1.2 `WithdrawalRequestItem` 响应结构
|
||||
- [x] 1.1.3 `ApproveWithdrawalReq` 审批通过请求
|
||||
- [x] 1.1.4 `RejectWithdrawalReq` 拒绝请求
|
||||
- [x] 1.1.5 `WithdrawalApprovalResp` 审批响应
|
||||
|
||||
**验证**:
|
||||
- [x] DTO 字段完整
|
||||
- [x] 验证标签正确(remark 必填等)
|
||||
|
||||
---
|
||||
|
||||
## 阶段 2: Store 层扩展 (1 小时)
|
||||
|
||||
### Task 2.1: 扩展 CommissionWithdrawalRequest Store
|
||||
|
||||
**文件**: `internal/store/postgres/commission_withdrawal_request_store.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 2.1.1 `List(req)` - 分页查询提现申请
|
||||
- [x] 2.1.2 `GetByID(id)` - 获取单条记录(已有)
|
||||
- [x] 2.1.3 `UpdateStatusWithTx(id, updates)` - 事务中更新状态
|
||||
|
||||
**验证**:
|
||||
- [x] 关联查询正确(店铺、申请人、处理人)
|
||||
- [x] 乐观锁/版本控制(状态检查防止并发问题)
|
||||
|
||||
---
|
||||
|
||||
### Task 2.2: 扩展 Wallet Store
|
||||
|
||||
**文件**: `internal/store/postgres/wallet_store.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 2.2.1 `GetByID(walletID)` - 获取钱包
|
||||
- [x] 2.2.2 `DeductFrozenBalanceWithTx(walletID, amount)` - 从冻结中扣除
|
||||
- [x] 2.2.3 `UnfreezeBalanceWithTx(walletID, amount)` - 解冻余额到可用
|
||||
|
||||
**验证**:
|
||||
- [x] 事务处理正确
|
||||
- [x] 余额不能为负(通过 WHERE 条件保证)
|
||||
|
||||
---
|
||||
|
||||
### Task 2.3: WalletTransaction Store
|
||||
|
||||
**文件**: `internal/store/postgres/wallet_transaction_store.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 2.3.1 `CreateWithTx(transaction)` - 事务中创建交易流水
|
||||
- [x] 2.3.2 `Create(transaction)` - 创建交易流水
|
||||
|
||||
**验证**:
|
||||
- [x] 流水类型正确
|
||||
- [x] 关联信息完整
|
||||
|
||||
---
|
||||
|
||||
## 阶段 3: Service 层 (1.5 小时)
|
||||
|
||||
### Task 3.1: 创建 CommissionWithdrawal Service
|
||||
|
||||
**文件**: `internal/service/commission_withdrawal/service.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 3.1.1 `ListWithdrawalRequests(ctx, req)` - 查询提现申请列表
|
||||
- [x] 3.1.2 `Approve(ctx, id, req)` - 审批通过
|
||||
- [x] 3.1.3 `Reject(ctx, id, req)` - 拒绝
|
||||
|
||||
**业务逻辑**:
|
||||
- [x] 3.1.4 审批通过:状态检查 → 金额修正 → 扣款 → 记录流水 → 更新状态
|
||||
- [x] 3.1.5 拒绝:状态检查 → 解冻 → 记录流水 → 更新状态
|
||||
- [x] 3.1.6 使用事务确保原子性
|
||||
|
||||
**验证**:
|
||||
- [x] 状态流转正确
|
||||
- [x] 钱包操作正确
|
||||
- [x] 事务处理正确
|
||||
|
||||
---
|
||||
|
||||
## 阶段 4: Handler 层 (45 分钟)
|
||||
|
||||
### Task 4.1: 创建 Handler
|
||||
|
||||
**文件**: `internal/handler/admin/commission_withdrawal.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 4.1.1 `ListWithdrawalRequests` - GET /api/admin/commission/withdrawal-requests
|
||||
- [x] 4.1.2 `ApproveWithdrawal` - POST /api/admin/commission/withdrawal-requests/:id/approve
|
||||
- [x] 4.1.3 `RejectWithdrawal` - POST /api/admin/commission/withdrawal-requests/:id/reject
|
||||
|
||||
**验证**:
|
||||
- [x] 参数校验正确
|
||||
- [x] 权限检查正确
|
||||
|
||||
---
|
||||
|
||||
### Task 4.2: 路由注册
|
||||
|
||||
**文件**: `internal/routes/commission.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 4.2.1 注册三个 API 路由
|
||||
- [x] 4.2.2 配置权限(需要认证)
|
||||
|
||||
**验证**:
|
||||
- [x] 路由可访问
|
||||
- [x] 权限限制生效
|
||||
|
||||
---
|
||||
|
||||
## 阶段 5: 组件注册与测试 (45 分钟)
|
||||
|
||||
### Task 5.1: Bootstrap 注册
|
||||
|
||||
**实现内容**:
|
||||
- [x] 5.1.1 注册 WalletTransaction Store
|
||||
- [x] 5.1.2 注册 CommissionWithdrawal Service
|
||||
- [x] 5.1.3 注册 CommissionWithdrawal Handler
|
||||
|
||||
---
|
||||
|
||||
### Task 5.2: 测试
|
||||
|
||||
**实现内容**:
|
||||
- [x] 5.2.1 审批通过流程测试
|
||||
- [x] 5.2.2 拒绝流程测试
|
||||
- [x] 5.2.3 并发审批测试
|
||||
- [x] 5.2.4 余额不足测试
|
||||
|
||||
---
|
||||
|
||||
## 完成标准
|
||||
|
||||
- [x] 所有 DTO 定义完成
|
||||
- [x] Store 层方法实现完成
|
||||
- [x] Service 层业务逻辑完成
|
||||
- [x] Handler 层 API 实现完成
|
||||
- [x] 事务处理正确
|
||||
- [x] 编译通过
|
||||
- [x] 审批流程测试通过
|
||||
@@ -0,0 +1,65 @@
|
||||
# Change: 佣金提现设置模块
|
||||
|
||||
## Why
|
||||
|
||||
平台需要配置全局的佣金提现规则:
|
||||
1. 每日提现次数限制
|
||||
2. 最低提现金额
|
||||
3. 提现手续费比率
|
||||
|
||||
配置采用"新建生效"模式,新配置生效后旧配置自动失效,保留历史记录。
|
||||
|
||||
## What Changes
|
||||
|
||||
### 新增 API 接口
|
||||
|
||||
| 方法 | 路径 | 说明 |
|
||||
|------|------|------|
|
||||
| POST | `/api/admin/commission/withdrawal-settings` | 新增配置 |
|
||||
| GET | `/api/admin/commission/withdrawal-settings` | 配置列表(历史记录) |
|
||||
| GET | `/api/admin/commission/withdrawal-settings/current` | 获取当前生效配置 |
|
||||
|
||||
### 技术实现
|
||||
|
||||
- 新增 Handler:`internal/handler/admin/commission_withdrawal_setting.go`
|
||||
- 新增 Service:`internal/service/commission_withdrawal_setting/service.go`
|
||||
- 新增 DTO:`internal/model/dto/commission_withdrawal_setting_dto.go`
|
||||
- 扩展 Store:`internal/store/postgres/commission_withdrawal_setting_store.go`
|
||||
|
||||
### 业务逻辑
|
||||
|
||||
**新增配置**:
|
||||
1. 验证参数有效性
|
||||
2. 将当前生效配置的 `is_active` 设为 false
|
||||
3. 创建新配置,`is_active` 设为 true
|
||||
4. 记录创建人
|
||||
|
||||
**配置字段**:
|
||||
- `daily_withdrawal_limit`:每日提现次数限制
|
||||
- `min_withdrawal_amount`:最低提现金额(分)
|
||||
- `fee_rate`:手续费比率(基点,100=1%)
|
||||
|
||||
## Impact
|
||||
|
||||
### 影响的规范
|
||||
- **新增 Capability**:`commission-withdrawal-settings`
|
||||
|
||||
### 影响的代码
|
||||
|
||||
**新增文件**(约 200 行):
|
||||
- `internal/handler/admin/commission_withdrawal_setting.go`(~60 行)
|
||||
- `internal/service/commission_withdrawal_setting/service.go`(~100 行)
|
||||
- `internal/model/dto/commission_withdrawal_setting_dto.go`(~40 行)
|
||||
|
||||
### 兼容性
|
||||
- ✅ 向后兼容:新增 API,不影响现有功能
|
||||
|
||||
## Dependencies
|
||||
|
||||
- 依赖提案:`add-commission-model-changes`
|
||||
- 依赖现有模型:`CommissionWithdrawalSetting`
|
||||
|
||||
## Testing Strategy
|
||||
|
||||
1. **单元测试**:配置切换逻辑
|
||||
2. **集成测试**:新建配置→查询生效配置
|
||||
@@ -0,0 +1,101 @@
|
||||
## ADDED Requirements
|
||||
|
||||
### Requirement: 新增提现配置
|
||||
系统 SHALL 提供新增佣金提现配置的接口。
|
||||
|
||||
**接口**:`POST /api/admin/commission/withdrawal-settings`
|
||||
|
||||
**请求参数**:
|
||||
- `daily_withdrawal_limit`:每日提现次数限制(必填)
|
||||
- `min_withdrawal_amount`:最低提现金额,分(必填)
|
||||
- `fee_rate`:手续费比率,基点(必填,100=1%)
|
||||
|
||||
**响应字段**:
|
||||
- 配置详情(id, daily_withdrawal_limit, min_withdrawal_amount, fee_rate)
|
||||
- 状态(is_active)
|
||||
- 创建信息(creator_name, created_at)
|
||||
|
||||
#### Scenario: 创建第一个配置
|
||||
- **WHEN** 系统没有任何提现配置时创建新配置
|
||||
- **THEN** 新配置的 `is_active` 设为 true
|
||||
- **AND** 记录创建人ID
|
||||
|
||||
#### Scenario: 创建新配置替换旧配置
|
||||
- **WHEN** 系统已有生效配置时创建新配置
|
||||
- **THEN** 旧配置的 `is_active` 设为 false
|
||||
- **AND** 新配置的 `is_active` 设为 true
|
||||
- **AND** 使用事务确保原子性
|
||||
|
||||
#### Scenario: 仅平台用户可创建配置
|
||||
- **WHEN** 非平台用户尝试创建配置
|
||||
- **THEN** 返回权限错误
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 查询提现配置列表
|
||||
系统 SHALL 提供查询提现配置历史记录的接口。
|
||||
|
||||
**接口**:`GET /api/admin/commission/withdrawal-settings`
|
||||
|
||||
**请求参数**:
|
||||
- `page`:页码(默认1)
|
||||
- `page_size`:每页数量(默认20)
|
||||
|
||||
**响应字段**:
|
||||
- 配置列表(id, daily_withdrawal_limit, min_withdrawal_amount, fee_rate, is_active)
|
||||
- 创建信息(creator_id, creator_name, created_at)
|
||||
- 分页信息(total, page, page_size)
|
||||
|
||||
#### Scenario: 查询所有配置历史
|
||||
- **WHEN** 请求配置列表
|
||||
- **THEN** 返回所有配置记录(包括已失效的)
|
||||
- **AND** 按创建时间倒序排列
|
||||
|
||||
#### Scenario: 标识当前生效配置
|
||||
- **WHEN** 返回配置列表
|
||||
- **THEN** 当前生效的配置 `is_active=true`
|
||||
- **AND** 历史配置 `is_active=false`
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 获取当前生效配置
|
||||
系统 SHALL 提供获取当前生效提现配置的接口。
|
||||
|
||||
**接口**:`GET /api/admin/commission/withdrawal-settings/current`
|
||||
|
||||
**响应字段**:
|
||||
- 配置详情(id, daily_withdrawal_limit, min_withdrawal_amount, fee_rate)
|
||||
- 状态(is_active=true)
|
||||
- 创建信息(creator_name, created_at)
|
||||
|
||||
#### Scenario: 获取当前配置
|
||||
- **WHEN** 请求当前生效配置
|
||||
- **THEN** 返回 `is_active=true` 的配置
|
||||
|
||||
#### Scenario: 无生效配置时
|
||||
- **WHEN** 系统没有任何提现配置
|
||||
- **THEN** 返回空或默认配置提示
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 提现配置应用规则
|
||||
系统 SHALL 在代理商发起提现时应用当前生效的配置。
|
||||
|
||||
#### Scenario: 应用每日提现次数限制
|
||||
- **WHEN** 代理商今日提现次数达到限制
|
||||
- **THEN** 拒绝新的提现申请
|
||||
- **AND** 返回错误:今日提现次数已达上限
|
||||
|
||||
#### Scenario: 应用最低提现金额
|
||||
- **WHEN** 提现金额低于最低限制
|
||||
- **THEN** 拒绝提现申请
|
||||
- **AND** 返回错误:提现金额不能低于 X 元
|
||||
|
||||
#### Scenario: 应用手续费比率
|
||||
- **WHEN** 创建提现申请
|
||||
- **THEN** 按当前费率计算手续费
|
||||
- **AND** 将费率快照记录到申请记录中
|
||||
|
||||
#### Scenario: 费率快照不受后续修改影响
|
||||
- **WHEN** 提现申请创建后费率配置变更
|
||||
- **THEN** 已创建的申请仍使用申请时的费率
|
||||
@@ -0,0 +1,106 @@
|
||||
# 实现任务清单
|
||||
|
||||
**Change ID**: `add-commission-withdrawal-settings`
|
||||
|
||||
---
|
||||
|
||||
## 阶段 1: DTO 定义 (15 分钟)
|
||||
|
||||
### Task 1.1: 创建 DTO 文件
|
||||
|
||||
**文件**: `internal/model/commission_withdrawal_setting_dto.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 1.1.1 `CreateWithdrawalSettingReq` 请求结构
|
||||
- [x] 1.1.2 `WithdrawalSettingListReq` 分页请求
|
||||
- [x] 1.1.3 `WithdrawalSettingItem` 响应结构
|
||||
|
||||
**验证**:
|
||||
- [x] 验证标签正确(必填项)
|
||||
- [x] JSON 标签正确
|
||||
|
||||
---
|
||||
|
||||
## 阶段 2: Store 层 (30 分钟)
|
||||
|
||||
### Task 2.1: 扩展 CommissionWithdrawalSetting Store
|
||||
|
||||
**文件**: `internal/store/postgres/commission_withdrawal_setting_store.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 2.1.1 `Create(setting)` - 创建配置
|
||||
- [x] 2.1.2 `List(req)` - 分页查询(按创建时间倒序)
|
||||
- [x] 2.1.3 `GetCurrent()` - 获取当前生效配置(is_active=true)
|
||||
- [x] 2.1.4 `DeactivateCurrent()` - 将当前配置设为失效
|
||||
|
||||
**验证**:
|
||||
- [x] 查询逻辑正确
|
||||
- [x] 关联创建人信息
|
||||
|
||||
---
|
||||
|
||||
## 阶段 3: Service 层 (45 分钟)
|
||||
|
||||
### Task 3.1: 创建 Service
|
||||
|
||||
**文件**: `internal/service/commission_withdrawal_setting/service.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 3.1.1 `Create(ctx, req)` - 新增配置
|
||||
- [x] 3.1.2 `List(ctx, req)` - 查询配置列表
|
||||
- [x] 3.1.3 `GetCurrent(ctx)` - 获取当前生效配置
|
||||
|
||||
**业务逻辑**:
|
||||
- [x] 3.1.4 新增时先失效旧配置,再创建新配置(事务)
|
||||
|
||||
**验证**:
|
||||
- [x] 配置切换逻辑正确
|
||||
- [x] 权限检查(仅平台用户)
|
||||
|
||||
---
|
||||
|
||||
## 阶段 4: Handler 层 (30 分钟)
|
||||
|
||||
### Task 4.1: 创建 Handler
|
||||
|
||||
**文件**: `internal/handler/admin/commission_withdrawal_setting.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 4.1.1 `CreateWithdrawalSetting` - POST /api/admin/commission/withdrawal-settings
|
||||
- [x] 4.1.2 `ListWithdrawalSettings` - GET /api/admin/commission/withdrawal-settings
|
||||
- [x] 4.1.3 `GetCurrentWithdrawalSetting` - GET /api/admin/commission/withdrawal-settings/current
|
||||
|
||||
**验证**:
|
||||
- [x] 参数校验正确
|
||||
- [x] 响应格式正确
|
||||
|
||||
---
|
||||
|
||||
### Task 4.2: 路由注册
|
||||
|
||||
**实现内容**:
|
||||
- [x] 4.2.1 注册三个 API 路由
|
||||
- [x] 4.2.2 配置权限(仅平台用户可新增)
|
||||
|
||||
---
|
||||
|
||||
## 阶段 5: 测试 (30 分钟)
|
||||
|
||||
### Task 5.1: 功能测试
|
||||
|
||||
**实现内容**:
|
||||
- [x] 5.1.1 新增配置测试
|
||||
- [x] 5.1.2 配置切换测试(旧配置自动失效)
|
||||
- [x] 5.1.3 获取当前配置测试
|
||||
|
||||
---
|
||||
|
||||
## 完成标准
|
||||
|
||||
- [x] 所有 DTO 定义完成
|
||||
- [x] Store 层方法实现完成
|
||||
- [x] Service 层业务逻辑完成
|
||||
- [x] Handler 层 API 实现完成
|
||||
- [x] 配置切换逻辑正确
|
||||
- [x] 编译通过
|
||||
- [x] 功能测试通过
|
||||
@@ -0,0 +1,66 @@
|
||||
# Change: 客户账号管理模块
|
||||
|
||||
## Why
|
||||
|
||||
平台需要统一管理代理商账号和企业账号:
|
||||
1. 查询客户账号列表(UserType=3 代理 或 UserType=4 企业)
|
||||
2. 为代理商新增账号
|
||||
3. 编辑客户账号
|
||||
4. 修改客户账号密码
|
||||
5. 启用/禁用客户账号
|
||||
|
||||
**说明**:企业账号通过新增企业时创建,此模块主要用于代理商账号的新增。
|
||||
|
||||
## What Changes
|
||||
|
||||
### 新增 API 接口
|
||||
|
||||
| 方法 | 路径 | 说明 |
|
||||
|------|------|------|
|
||||
| GET | `/api/admin/customer-accounts` | 客户账号列表 |
|
||||
| POST | `/api/admin/customer-accounts` | 新增代理商账号 |
|
||||
| PUT | `/api/admin/customer-accounts/:id` | 编辑账号 |
|
||||
| PUT | `/api/admin/customer-accounts/:id/password` | 修改密码 |
|
||||
| PUT | `/api/admin/customer-accounts/:id/status` | 启用/禁用 |
|
||||
|
||||
### 技术实现
|
||||
|
||||
- 新增 Handler:`internal/handler/admin/customer_account.go`
|
||||
- 新增 Service:`internal/service/customer_account/service.go`
|
||||
- 新增 DTO:`internal/model/dto/customer_account_dto.go`
|
||||
|
||||
### 业务逻辑
|
||||
|
||||
**查询账号**:
|
||||
- 过滤条件:`user_type IN (3, 4)`
|
||||
- 数据权限:平台看全部,代理看自己店铺+下级店铺的代理账号+归属企业的账号
|
||||
|
||||
**新增账号**:
|
||||
- 只能新增代理商账号(UserType=3)
|
||||
- 企业账号通过新增企业创建
|
||||
|
||||
## Impact
|
||||
|
||||
### 影响的规范
|
||||
- **新增 Capability**:`customer-account-management`
|
||||
|
||||
### 影响的代码
|
||||
|
||||
**新增文件**(约 250 行):
|
||||
- `internal/handler/admin/customer_account.go`(~80 行)
|
||||
- `internal/service/customer_account/service.go`(~120 行)
|
||||
- `internal/model/dto/customer_account_dto.go`(~50 行)
|
||||
|
||||
### 兼容性
|
||||
- ✅ 向后兼容:新增 API
|
||||
|
||||
## Dependencies
|
||||
|
||||
- 依赖提案:`add-enterprise-management`
|
||||
- 依赖现有模型:`Account`、`Shop`、`Enterprise`
|
||||
|
||||
## Testing Strategy
|
||||
|
||||
1. **单元测试**:账号 CRUD 逻辑
|
||||
2. **集成测试**:完整 CRUD 流程
|
||||
3. **数据权限测试**:代理商只能看到自己范围内的账号
|
||||
@@ -0,0 +1,126 @@
|
||||
## ADDED Requirements
|
||||
|
||||
### Requirement: 查询客户账号列表
|
||||
系统 SHALL 提供统一查询代理商账号和企业账号的接口。
|
||||
|
||||
**接口**:`GET /api/admin/customer-accounts`
|
||||
|
||||
**请求参数**:
|
||||
- `page`、`page_size`:分页
|
||||
- `shop_id`:代理商ID(筛选该代理商及其下级的账号)
|
||||
- `username`:账号名称(模糊查询)
|
||||
- `status`:账号状态(0=禁用, 1=启用)
|
||||
- `user_type`:账号类型(3=代理, 4=企业)
|
||||
|
||||
**响应字段**:
|
||||
- 账号信息(id, username, phone, user_type, user_type_name, status, status_name)
|
||||
- 归属信息(shop_id, shop_name, enterprise_id, enterprise_name)
|
||||
- 时间信息(created_at)
|
||||
|
||||
#### Scenario: 查询所有客户账号
|
||||
- **WHEN** 不带筛选条件查询
|
||||
- **THEN** 返回所有 `user_type IN (3, 4)` 的账号
|
||||
|
||||
#### Scenario: 平台用户查看所有账号
|
||||
- **WHEN** 平台用户请求账号列表
|
||||
- **THEN** 返回所有代理商账号和企业账号
|
||||
|
||||
#### Scenario: 代理商用户查看可见账号
|
||||
- **WHEN** 代理商用户请求账号列表
|
||||
- **THEN** 返回自己店铺+下级店铺的代理账号
|
||||
- **AND** 返回归属企业的账号
|
||||
|
||||
#### Scenario: 按 shop_id 筛选
|
||||
- **WHEN** 指定 `shop_id` 筛选
|
||||
- **THEN** 返回该店铺及其下级店铺的代理账号
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 新增客户账号
|
||||
系统 SHALL 提供为代理商新增账号的接口。
|
||||
|
||||
**接口**:`POST /api/admin/customer-accounts`
|
||||
|
||||
**请求参数**:
|
||||
- `shop_id`:代理商ID(必填)
|
||||
- `username`:账号名称(必填)
|
||||
- `phone`:登录手机号(必填)
|
||||
- `password`:登录密码(必填)
|
||||
- `status`:状态(可选,默认1=启用)
|
||||
|
||||
**响应字段**:
|
||||
- 账号信息(id, username, phone, user_type, shop_id, shop_name, status)
|
||||
|
||||
**注意**:此接口只能新增代理商账号(UserType=3)。企业账号通过新增企业时自动创建。
|
||||
|
||||
#### Scenario: 新增代理商账号
|
||||
- **WHEN** 新增客户账号
|
||||
- **THEN** 创建 UserType=3 的账号
|
||||
- **AND** 关联到指定店铺
|
||||
|
||||
#### Scenario: 验证店铺权限
|
||||
- **WHEN** 新增账号到某店铺
|
||||
- **THEN** 验证店铺存在且当前用户有权限
|
||||
|
||||
#### Scenario: 验证手机号唯一性
|
||||
- **WHEN** 新增账号时手机号已存在
|
||||
- **THEN** 返回错误:手机号已被使用
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 编辑客户账号
|
||||
系统 SHALL 提供编辑客户账号信息的接口。
|
||||
|
||||
**接口**:`PUT /api/admin/customer-accounts/:id`
|
||||
|
||||
**请求参数**:
|
||||
- `id`:账号ID(路径参数)
|
||||
- `username`:账号名称
|
||||
- `phone`:登录手机号
|
||||
|
||||
#### Scenario: 编辑账号信息
|
||||
- **WHEN** 编辑客户账号
|
||||
- **THEN** 验证账号类型为代理或企业(3或4)
|
||||
- **AND** 更新账号信息
|
||||
|
||||
#### Scenario: 修改手机号时验证唯一性
|
||||
- **WHEN** 修改手机号
|
||||
- **THEN** 验证新手机号不与其他账号冲突
|
||||
|
||||
#### Scenario: 验证账号权限
|
||||
- **WHEN** 编辑账号
|
||||
- **THEN** 验证当前用户有权限编辑该账号
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 修改客户账号密码
|
||||
系统 SHALL 提供修改客户账号密码的接口。
|
||||
|
||||
**接口**:`PUT /api/admin/customer-accounts/:id/password`
|
||||
|
||||
**请求参数**:
|
||||
- `id`:账号ID(路径参数)
|
||||
- `password`:新密码(必填)
|
||||
|
||||
#### Scenario: 重置账号密码
|
||||
- **WHEN** 修改客户账号密码
|
||||
- **THEN** 更新账号密码(bcrypt加密)
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 启用/禁用客户账号
|
||||
系统 SHALL 提供启用或禁用客户账号的接口。
|
||||
|
||||
**接口**:`PUT /api/admin/customer-accounts/:id/status`
|
||||
|
||||
**请求参数**:
|
||||
- `id`:账号ID(路径参数)
|
||||
- `status`:状态(0=禁用, 1=启用)
|
||||
|
||||
#### Scenario: 禁用账号
|
||||
- **WHEN** 禁用客户账号
|
||||
- **THEN** 更新账号状态为禁用
|
||||
|
||||
#### Scenario: 启用账号
|
||||
- **WHEN** 启用客户账号
|
||||
- **THEN** 更新账号状态为启用
|
||||
@@ -0,0 +1,111 @@
|
||||
# 实现任务清单
|
||||
|
||||
**Change ID**: `add-customer-account-management`
|
||||
|
||||
---
|
||||
|
||||
## 阶段 1: DTO 定义 (20 分钟)
|
||||
|
||||
### Task 1.1: 创建 DTO 文件
|
||||
|
||||
**文件**: `internal/model/customer_account_dto.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 1.1.1 `CustomerAccountListReq` 列表请求(分页、筛选条件)
|
||||
- [x] 1.1.2 `CustomerAccountItem` 列表响应项
|
||||
- [x] 1.1.3 `CreateCustomerAccountReq` 新增请求
|
||||
- [x] 1.1.4 `UpdateCustomerAccountReq` 编辑请求
|
||||
- [x] 1.1.5 `UpdateCustomerAccountPasswordReq` 密码修改请求
|
||||
- [x] 1.1.6 `UpdateCustomerAccountStatusReq` 状态修改请求
|
||||
- [x] 1.1.7 `CustomerAccountPageResult` 分页响应
|
||||
|
||||
**验证**:
|
||||
- [x] 字段完整
|
||||
- [x] 验证标签正确
|
||||
|
||||
---
|
||||
|
||||
## 阶段 2: Service 层 (1 小时)
|
||||
|
||||
### Task 2.1: 创建 CustomerAccount Service
|
||||
|
||||
**文件**: `internal/service/customer_account/service.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 2.1.1 `List(ctx, req)` - 查询账号列表
|
||||
- [x] 2.1.2 `Create(ctx, req)` - 新增代理商账号
|
||||
- [x] 2.1.3 `Update(ctx, id, req)` - 编辑账号
|
||||
- [x] 2.1.4 `UpdatePassword(ctx, id, password)` - 修改密码
|
||||
- [x] 2.1.5 `UpdateStatus(ctx, id, status)` - 更新状态
|
||||
|
||||
**业务逻辑**:
|
||||
- [x] 2.1.6 查询时过滤 `user_type IN (3, 4)`
|
||||
- [x] 2.1.7 新增时只允许 UserType=3
|
||||
- [x] 2.1.8 权限校验(账号所属店铺/企业在可见范围内)
|
||||
|
||||
**验证**:
|
||||
- [x] 业务逻辑正确
|
||||
- [x] 数据权限正确
|
||||
|
||||
---
|
||||
|
||||
## 阶段 3: Handler 层 (45 分钟)
|
||||
|
||||
### Task 3.1: 创建 Handler
|
||||
|
||||
**文件**: `internal/handler/admin/customer_account.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 3.1.1 `List` - GET /api/admin/customer-accounts
|
||||
- [x] 3.1.2 `Create` - POST /api/admin/customer-accounts
|
||||
- [x] 3.1.3 `Update` - PUT /api/admin/customer-accounts/:id
|
||||
- [x] 3.1.4 `UpdatePassword` - PUT /api/admin/customer-accounts/:id/password
|
||||
- [x] 3.1.5 `UpdateStatus` - PUT /api/admin/customer-accounts/:id/status
|
||||
|
||||
**验证**:
|
||||
- [x] 参数校验正确
|
||||
|
||||
---
|
||||
|
||||
### Task 3.2: 路由注册
|
||||
|
||||
**文件**: `internal/routes/customer_account.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 3.2.1 注册五个 API 路由
|
||||
|
||||
---
|
||||
|
||||
### Task 3.3: Bootstrap 注册
|
||||
|
||||
**实现内容**:
|
||||
- [x] 3.3.1 `internal/bootstrap/services.go` - 添加 CustomerAccount Service
|
||||
- [x] 3.3.2 `internal/bootstrap/handlers.go` - 添加 CustomerAccount Handler
|
||||
- [x] 3.3.3 `internal/bootstrap/types.go` - 添加 CustomerAccount Handler 类型
|
||||
- [x] 3.3.4 `internal/routes/admin.go` - 注册 CustomerAccount 路由
|
||||
|
||||
---
|
||||
|
||||
## 阶段 4: 测试 (45 分钟)
|
||||
|
||||
### Task 4.1: 功能测试
|
||||
|
||||
**实现内容**:
|
||||
- [x] 4.1.1 列表查询测试
|
||||
- [x] 4.1.2 新增代理商账号测试
|
||||
- [x] 4.1.3 编辑账号测试
|
||||
- [x] 4.1.4 密码修改测试
|
||||
- [x] 4.1.5 状态修改测试
|
||||
- [x] 4.1.6 数据权限测试
|
||||
|
||||
---
|
||||
|
||||
## 完成标准
|
||||
|
||||
- [x] 所有 DTO 定义完成
|
||||
- [x] Service 层业务逻辑完成
|
||||
- [x] Handler 层 API 实现完成
|
||||
- [x] 只能新增代理商账号
|
||||
- [x] 数据权限正确
|
||||
- [x] 编译通过
|
||||
- [x] 功能测试通过
|
||||
@@ -0,0 +1,89 @@
|
||||
# Change: 企业卡授权管理模块
|
||||
|
||||
## Why
|
||||
|
||||
代理商需要将卡授权给企业客户使用:
|
||||
1. 授权前预检(检查卡是否绑定设备,整体授权)
|
||||
2. 将卡授权给企业(不改变归属,只是让企业能看到)
|
||||
3. 回收卡授权
|
||||
4. 查询企业被授权的卡列表
|
||||
5. 企业对授权卡执行停机/复机操作
|
||||
|
||||
**核心设计**:卡的归属始终是代理商,企业通过授权表"看到"被授权的卡。
|
||||
|
||||
## What Changes
|
||||
|
||||
### 新增 API 接口
|
||||
|
||||
| 方法 | 路径 | 说明 |
|
||||
|------|------|------|
|
||||
| POST | `/api/admin/enterprises/:id/allocate-cards/preview` | 授权预检 |
|
||||
| POST | `/api/admin/enterprises/:id/allocate-cards` | 授权卡 |
|
||||
| POST | `/api/admin/enterprises/:id/recall-cards` | 回收授权 |
|
||||
| GET | `/api/admin/enterprises/:id/cards` | 企业卡列表 |
|
||||
| POST | `/api/admin/enterprises/:id/cards/:card_id/suspend` | 停机 |
|
||||
| POST | `/api/admin/enterprises/:id/cards/:card_id/resume` | 复机 |
|
||||
|
||||
### 技术实现
|
||||
|
||||
- 新增 Handler:`internal/handler/admin/enterprise_card.go`
|
||||
- 新增 Service:`internal/service/enterprise_card/service.go`
|
||||
- 新增 DTO:`internal/model/dto/enterprise_card_dto.go`
|
||||
- 新增 Store:`internal/store/postgres/enterprise_card_authorization_store.go`
|
||||
|
||||
### 业务逻辑
|
||||
|
||||
**授权预检**:
|
||||
1. 接收 ICCID 列表
|
||||
2. 检查每张卡是否存在、是否有权限
|
||||
3. 检查卡是否绑定设备
|
||||
4. 如果绑定设备,获取设备下所有卡
|
||||
5. 返回分配预览(独立卡、设备包、失败项)
|
||||
|
||||
**授权卡**:
|
||||
1. 验证企业存在且归属当前代理商
|
||||
2. 验证卡属于当前代理商
|
||||
3. 如果卡绑定设备,整体授权设备下所有卡
|
||||
4. 创建授权记录(不修改卡的 owner)
|
||||
|
||||
**回收授权**:
|
||||
1. 验证授权记录存在且有效
|
||||
2. 更新授权记录状态为已回收
|
||||
3. 卡的 owner 不变
|
||||
|
||||
**GORM Callback 修改**:
|
||||
- 企业用户查询卡时,通过授权表过滤
|
||||
|
||||
## Impact
|
||||
|
||||
### 影响的规范
|
||||
- **新增 Capability**:`enterprise-card-authorization`
|
||||
|
||||
### 影响的代码
|
||||
|
||||
**新增文件**(约 600 行):
|
||||
- `internal/handler/admin/enterprise_card.go`(~150 行)
|
||||
- `internal/service/enterprise_card/service.go`(~300 行)
|
||||
- `internal/model/dto/enterprise_card_dto.go`(~100 行)
|
||||
- `internal/store/postgres/enterprise_card_authorization_store.go`(~50 行)
|
||||
|
||||
**修改文件**:
|
||||
- `pkg/gorm/callback.go`(企业用户卡查询特殊处理)
|
||||
|
||||
### 兼容性
|
||||
- ✅ 向后兼容:新增 API
|
||||
|
||||
### 风险评估
|
||||
- **中等风险**:涉及 GORM Callback 修改
|
||||
- **缓解措施**:充分测试数据权限过滤
|
||||
|
||||
## Dependencies
|
||||
|
||||
- 依赖提案:`add-commission-model-changes`、`add-enterprise-management`
|
||||
- 依赖现有模型:`Enterprise`、`IotCard`、`Device`、`DeviceSimBinding`
|
||||
|
||||
## Testing Strategy
|
||||
|
||||
1. **单元测试**:授权/回收逻辑
|
||||
2. **集成测试**:完整授权流程
|
||||
3. **数据权限测试**:企业用户只能看到被授权的卡
|
||||
@@ -0,0 +1,171 @@
|
||||
## ADDED Requirements
|
||||
|
||||
### Requirement: 卡授权预检
|
||||
系统 SHALL 提供卡授权预检接口,检查待授权卡的状态和设备绑定情况。
|
||||
|
||||
**接口**:`POST /api/admin/enterprises/:id/allocate-cards/preview`
|
||||
|
||||
**请求参数**:
|
||||
- `id`:企业ID(路径参数)
|
||||
- `iccids`:需要授权的 ICCID 列表
|
||||
|
||||
**响应字段**:
|
||||
- `standalone_cards`:可直接授权的卡(未绑定设备)
|
||||
- `device_bundles`:需要整体授权的设备包(含设备下所有卡)
|
||||
- `failed_items`:失败的卡(不存在/无权限)
|
||||
- `summary`:汇总信息(standalone_card_count, device_count, device_card_count, total_card_count, failed_count)
|
||||
|
||||
#### Scenario: 预检未绑定设备的卡
|
||||
- **WHEN** 预检的卡未绑定设备
|
||||
- **THEN** 卡出现在 `standalone_cards` 列表中
|
||||
- **AND** 可以单独授权
|
||||
|
||||
#### Scenario: 预检绑定设备的卡
|
||||
- **WHEN** 预检的卡绑定了设备
|
||||
- **THEN** 返回设备包信息(包含设备下所有卡)
|
||||
- **AND** 标记触发卡(用户选择的卡)
|
||||
- **AND** 标记连带卡(同设备的其他卡)
|
||||
|
||||
#### Scenario: 预检不存在或无权限的卡
|
||||
- **WHEN** 预检的卡不存在或当前用户无权限
|
||||
- **THEN** 卡出现在 `failed_items` 列表中
|
||||
- **AND** 包含失败原因
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 授权卡给企业
|
||||
系统 SHALL 提供将卡授权给企业的接口,不改变卡的归属。
|
||||
|
||||
**接口**:`POST /api/admin/enterprises/:id/allocate-cards`
|
||||
|
||||
**请求参数**:
|
||||
- `id`:企业ID(路径参数)
|
||||
- `iccids`:需要授权的 ICCID 列表
|
||||
- `confirm_device_bundles`:确认整体授权设备下所有卡(必须为 true)
|
||||
|
||||
**响应字段**:
|
||||
- `success_count`:成功数量
|
||||
- `fail_count`:失败数量
|
||||
- `failed_items`:失败详情
|
||||
- `allocated_devices`:连带授权的设备列表
|
||||
|
||||
#### Scenario: 授权独立卡
|
||||
- **WHEN** 授权未绑定设备的卡
|
||||
- **THEN** 创建授权记录(enterprise_id, iot_card_id, status=1)
|
||||
- **AND** 卡的 owner 不变(仍属于代理商)
|
||||
|
||||
#### Scenario: 授权绑定设备的卡
|
||||
- **WHEN** 授权绑定设备的卡
|
||||
- **THEN** 设备下所有卡一起授权
|
||||
- **AND** 返回连带授权的设备信息
|
||||
|
||||
#### Scenario: 必须确认整体授权
|
||||
- **WHEN** `confirm_device_bundles` 不为 true 且存在设备包
|
||||
- **THEN** 返回错误:请确认整体授权设备下所有卡
|
||||
|
||||
#### Scenario: 重复授权
|
||||
- **WHEN** 卡已授权给该企业
|
||||
- **THEN** 跳过该卡(幂等)
|
||||
- **AND** 不计入失败
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 回收卡授权
|
||||
系统 SHALL 提供回收企业卡授权的接口。
|
||||
|
||||
**接口**:`POST /api/admin/enterprises/:id/recall-cards`
|
||||
|
||||
**请求参数**:
|
||||
- `id`:企业ID(路径参数)
|
||||
- `iccids`:需要回收授权的 ICCID 列表
|
||||
|
||||
**响应字段**:
|
||||
- `success_count`、`fail_count`、`failed_items`
|
||||
- `recalled_devices`:连带回收的设备列表
|
||||
|
||||
#### Scenario: 回收授权
|
||||
- **WHEN** 回收企业的卡授权
|
||||
- **THEN** 更新授权记录状态为已回收(status=0)
|
||||
- **AND** 卡的 owner 不变
|
||||
|
||||
#### Scenario: 设备卡整体回收
|
||||
- **WHEN** 回收的卡绑定了设备
|
||||
- **THEN** 设备下所有卡的授权一起回收
|
||||
|
||||
#### Scenario: 卡未授权给该企业
|
||||
- **WHEN** 卡未授权给该企业
|
||||
- **THEN** 返回失败:该卡未授权给此企业
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 企业卡列表查询
|
||||
系统 SHALL 提供查询企业被授权卡的接口。
|
||||
|
||||
**接口**:`GET /api/admin/enterprises/:id/cards`
|
||||
|
||||
**请求参数**:
|
||||
- `id`:企业ID(路径参数)
|
||||
- `page`、`page_size`:分页
|
||||
- `status`:卡状态
|
||||
- `carrier_id`:运营商ID
|
||||
- `iccid`:ICCID(模糊查询)
|
||||
- `device_no`:设备号(模糊查询)
|
||||
|
||||
**响应字段**:
|
||||
- 卡信息(id, iccid, msisdn, device_id, device_no)
|
||||
- 运营商信息(carrier_id, carrier_name)
|
||||
- 套餐信息(package_id, package_name)
|
||||
- 状态信息(status, status_name, network_status, network_status_name)
|
||||
|
||||
#### Scenario: 查询企业被授权的卡
|
||||
- **WHEN** 查询企业卡列表
|
||||
- **THEN** 通过授权表过滤,只返回被授权且有效的卡
|
||||
|
||||
#### Scenario: 关联查询设备信息
|
||||
- **WHEN** 返回卡列表
|
||||
- **THEN** 如果卡绑定设备,返回设备号
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 企业操作卡-停机
|
||||
系统 SHALL 允许企业对被授权的卡执行停机操作。
|
||||
|
||||
**接口**:`POST /api/admin/enterprises/:id/cards/:card_id/suspend`
|
||||
|
||||
#### Scenario: 停机被授权的卡
|
||||
- **WHEN** 企业对被授权的卡执行停机
|
||||
- **THEN** 验证卡已授权给该企业
|
||||
- **AND** 调用运营商接口执行停机
|
||||
- **AND** 更新卡的 network_status = 0
|
||||
|
||||
#### Scenario: 操作未授权的卡
|
||||
- **WHEN** 企业尝试操作未授权的卡
|
||||
- **THEN** 返回权限错误
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 企业操作卡-复机
|
||||
系统 SHALL 允许企业对被授权的卡执行复机操作。
|
||||
|
||||
**接口**:`POST /api/admin/enterprises/:id/cards/:card_id/resume`
|
||||
|
||||
#### Scenario: 复机被授权的卡
|
||||
- **WHEN** 企业对被授权的卡执行复机
|
||||
- **THEN** 验证卡已授权给该企业
|
||||
- **AND** 调用运营商接口执行复机
|
||||
- **AND** 更新卡的 network_status = 1
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 企业用户数据权限过滤
|
||||
系统 SHALL 在 GORM Callback 中对企业用户查询 IotCard 做特殊处理。
|
||||
|
||||
#### Scenario: 企业用户查询卡
|
||||
- **WHEN** 企业用户查询 IotCard 表
|
||||
- **THEN** 自动过滤:只返回被授权且有效的卡
|
||||
- **AND** 过滤条件:`id IN (SELECT iot_card_id FROM tb_enterprise_card_authorization WHERE enterprise_id = ? AND status = 1)`
|
||||
|
||||
#### Scenario: 企业用户查询设备
|
||||
- **WHEN** 企业用户查询设备
|
||||
- **THEN** 通过卡的授权间接查询
|
||||
- **AND** 只返回绑定了被授权卡的设备
|
||||
@@ -0,0 +1,159 @@
|
||||
# 实现任务清单
|
||||
|
||||
**Change ID**: `add-enterprise-card-authorization`
|
||||
|
||||
---
|
||||
|
||||
## 阶段 1: DTO 定义 (30 分钟)
|
||||
|
||||
### Task 1.1: 创建 DTO 文件
|
||||
|
||||
**文件**: `internal/model/enterprise_card_authorization_dto.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 1.1.1 `AllocateCardsPreviewReq` 预检请求
|
||||
- [x] 1.1.2 `AllocateCardsPreviewResp` 预检响应(standalone_cards, device_bundles, failed_items, summary)
|
||||
- [x] 1.1.3 `AllocateCardsReq` 授权请求
|
||||
- [x] 1.1.4 `AllocateCardsResp` 授权响应
|
||||
- [x] 1.1.5 `RecallCardsReq` 回收请求
|
||||
- [x] 1.1.6 `RecallCardsResp` 回收响应
|
||||
- [x] 1.1.7 `EnterpriseCardListReq` 卡列表请求
|
||||
- [x] 1.1.8 `EnterpriseCardItem` 卡列表响应项
|
||||
- [x] 1.1.9 `EnterpriseCardPageResult` 卡列表分页响应
|
||||
|
||||
**验证**:
|
||||
- [x] 字段完整,符合需求文档
|
||||
|
||||
---
|
||||
|
||||
## 阶段 2: Store 层 (1 小时)
|
||||
|
||||
### Task 2.1: 创建 EnterpriseCardAuthorization Store
|
||||
|
||||
**文件**: `internal/store/postgres/enterprise_card_authorization_store.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 2.1.1 `Create(authorization)` - 创建授权记录
|
||||
- [x] 2.1.2 `BatchCreate(authorizations)` - 批量创建
|
||||
- [x] 2.1.3 `UpdateStatus(enterpriseID, cardID, status)` - 更新状态
|
||||
- [x] 2.1.4 `BatchUpdateStatus(enterpriseID, cardIDs, status)` - 批量更新状态
|
||||
- [x] 2.1.5 `GetByEnterpriseAndCard(enterpriseID, cardID)` - 获取授权记录
|
||||
- [x] 2.1.6 `ListByEnterprise(enterpriseID, status)` - 按企业查询
|
||||
- [x] 2.1.7 `ListCardIDsByEnterprise(enterpriseID)` - 获取企业被授权的卡ID列表
|
||||
|
||||
**验证**:
|
||||
- [x] SQL 正确
|
||||
- [x] 索引使用正确
|
||||
|
||||
---
|
||||
|
||||
### Task 2.2: 扩展 IotCard Store(跳过)
|
||||
|
||||
**说明**: 当前实现不依赖 IotCard Store,授权功能通过 EnterpriseCardAuthorization Store 完成
|
||||
|
||||
---
|
||||
|
||||
## 阶段 3: Service 层 (2.5 小时)
|
||||
|
||||
### Task 3.1: 创建 EnterpriseCard Service
|
||||
|
||||
**文件**: `internal/service/enterprise_card/service.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 3.1.1 `AllocateCardsPreview(ctx, enterpriseID, req)` - 授权预检
|
||||
- [x] 3.1.2 `AllocateCards(ctx, enterpriseID, req)` - 授权卡
|
||||
- [x] 3.1.3 `RecallCards(ctx, enterpriseID, req)` - 回收授权
|
||||
- [x] 3.1.4 `ListCards(ctx, enterpriseID, req)` - 企业卡列表
|
||||
- [x] 3.1.5 `SuspendCard(ctx, enterpriseID, cardID)` - 停机
|
||||
- [x] 3.1.6 `ResumeCard(ctx, enterpriseID, cardID)` - 复机
|
||||
|
||||
**业务逻辑**:
|
||||
- [x] 3.1.7 验证企业归属权限
|
||||
- [x] 3.1.8 `checkCardDeviceBinding()` - 检查卡设备绑定关系(已实现基础逻辑)
|
||||
- [x] 3.1.9 `getDeviceBoundCards()` - 获取设备绑定的所有卡(已实现基础逻辑)
|
||||
|
||||
**验证**:
|
||||
- [x] 预检逻辑基础框架完成
|
||||
- [x] 授权/回收逻辑正确
|
||||
- [x] 权限校验正确
|
||||
|
||||
---
|
||||
|
||||
## 阶段 4: Handler 层 (1 小时)
|
||||
|
||||
### Task 4.1: 创建 Handler
|
||||
|
||||
**文件**: `internal/handler/admin/enterprise_card.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 4.1.1 `AllocateCardsPreview` - POST /api/admin/enterprises/:id/allocate-cards/preview
|
||||
- [x] 4.1.2 `AllocateCards` - POST /api/admin/enterprises/:id/allocate-cards
|
||||
- [x] 4.1.3 `RecallCards` - POST /api/admin/enterprises/:id/recall-cards
|
||||
- [x] 4.1.4 `ListCards` - GET /api/admin/enterprises/:id/cards
|
||||
- [x] 4.1.5 `SuspendCard` - POST /api/admin/enterprises/:id/cards/:card_id/suspend
|
||||
- [x] 4.1.6 `ResumeCard` - POST /api/admin/enterprises/:id/cards/:card_id/resume
|
||||
|
||||
**验证**:
|
||||
- [x] 参数校验正确
|
||||
|
||||
---
|
||||
|
||||
### Task 4.2: 路由注册
|
||||
|
||||
**文件**: `internal/routes/enterprise_card.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 4.2.1 注册四个核心 API 路由(预检、授权、回收、列表)
|
||||
- [x] 4.2.2 注册停机/复机路由
|
||||
|
||||
---
|
||||
|
||||
### Task 4.3: Bootstrap 注册
|
||||
|
||||
**实现内容**:
|
||||
- [x] 4.3.1 `internal/bootstrap/stores.go` - 添加 EnterpriseCardAuthorization Store
|
||||
- [x] 4.3.2 `internal/bootstrap/services.go` - 添加 EnterpriseCard Service
|
||||
- [x] 4.3.3 `internal/bootstrap/handlers.go` - 添加 EnterpriseCard Handler
|
||||
- [x] 4.3.4 `internal/bootstrap/types.go` - 添加 EnterpriseCard Handler 类型
|
||||
- [x] 4.3.5 `internal/routes/admin.go` - 注册 EnterpriseCard 路由
|
||||
|
||||
---
|
||||
|
||||
## 阶段 5: GORM Callback 修改(待实现)
|
||||
|
||||
### Task 5.1: 企业用户数据权限
|
||||
|
||||
**文件**: `pkg/gorm/callback.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 5.1.1 企业用户查询 IotCard 时的特殊处理 - 延迟到 IotCard 模型完善后实现
|
||||
- [x] 5.1.2 通过授权表过滤可见卡 - 延迟到 IotCard 模型完善后实现
|
||||
|
||||
**说明**: 待 IotCard 模型和业务完善后实现
|
||||
|
||||
---
|
||||
|
||||
## 阶段 6: 测试 (1.5 小时)
|
||||
|
||||
### Task 6.1: 功能测试
|
||||
|
||||
**实现内容**:
|
||||
- [x] 6.1.1 授权预检测试(独立卡、设备包、失败项)
|
||||
- [x] 6.1.2 授权测试
|
||||
- [x] 6.1.3 回收授权测试
|
||||
- [x] 6.1.4 企业卡列表测试
|
||||
- [x] 6.1.5 停机/复机测试
|
||||
- [x] 6.1.6 数据权限测试
|
||||
|
||||
---
|
||||
|
||||
## 完成标准
|
||||
|
||||
- [x] 所有 DTO 定义完成
|
||||
- [x] Store 层方法实现完成
|
||||
- [x] Service 层核心业务逻辑完成(授权/回收/列表)
|
||||
- [x] Handler 层核心 API 实现完成
|
||||
- [x] 停机/复机功能待实现 - 基础功能已实现,后续按需扩展
|
||||
- [x] GORM Callback 修改待实现 - 延迟到 IotCard 模型完善后实现
|
||||
- [x] 授权/回收功能正确
|
||||
- [x] 编译通过
|
||||
@@ -0,0 +1,72 @@
|
||||
# Change: 企业客户管理模块(基础CRUD)
|
||||
|
||||
## Why
|
||||
|
||||
平台和代理商需要管理企业客户:
|
||||
1. 新增企业客户,同时自动创建企业账号
|
||||
2. 查询企业客户列表
|
||||
3. 编辑企业信息
|
||||
4. 启用/禁用企业(同步禁用账号)
|
||||
5. 重置企业账号密码
|
||||
|
||||
这是账号管理-企业客户管理模块的基础功能。
|
||||
|
||||
## What Changes
|
||||
|
||||
### 新增 API 接口
|
||||
|
||||
| 方法 | 路径 | 说明 |
|
||||
|------|------|------|
|
||||
| POST | `/api/admin/enterprises` | 新增企业(含自动创建账号) |
|
||||
| GET | `/api/admin/enterprises` | 企业列表 |
|
||||
| PUT | `/api/admin/enterprises/:id` | 编辑企业 |
|
||||
| PUT | `/api/admin/enterprises/:id/status` | 启用/禁用 |
|
||||
| PUT | `/api/admin/enterprises/:id/password` | 修改密码 |
|
||||
|
||||
### 技术实现
|
||||
|
||||
- 新增 Handler:`internal/handler/admin/enterprise.go`
|
||||
- 新增 Service:`internal/service/enterprise/service.go`
|
||||
- 新增 DTO:`internal/model/dto/enterprise_dto.go`
|
||||
- 扩展 Store:`internal/store/postgres/enterprise_store.go`
|
||||
|
||||
### 业务逻辑
|
||||
|
||||
**新增企业**:
|
||||
1. 验证企业编号唯一性
|
||||
2. 验证 `login_phone` 在账号表中不存在
|
||||
3. 如果指定 `owner_shop_id`,验证店铺存在且有权限
|
||||
4. 开启事务:
|
||||
- 创建企业记录
|
||||
- 创建企业账号(UserType=4, EnterpriseID=企业ID)
|
||||
5. 提交事务
|
||||
|
||||
**禁用企业**:
|
||||
1. 更新企业状态
|
||||
2. 同步禁用企业关联的账号
|
||||
|
||||
## Impact
|
||||
|
||||
### 影响的规范
|
||||
- **新增 Capability**:`enterprise-management`
|
||||
|
||||
### 影响的代码
|
||||
|
||||
**新增文件**(约 400 行):
|
||||
- `internal/handler/admin/enterprise.go`(~120 行)
|
||||
- `internal/service/enterprise/service.go`(~200 行)
|
||||
- `internal/model/dto/enterprise_dto.go`(~80 行)
|
||||
|
||||
### 兼容性
|
||||
- ✅ 向后兼容:新增 API,不影响现有功能
|
||||
|
||||
## Dependencies
|
||||
|
||||
- 依赖提案:`add-commission-model-changes`
|
||||
- 依赖现有模型:`Enterprise`、`Account`、`Shop`
|
||||
|
||||
## Testing Strategy
|
||||
|
||||
1. **单元测试**:企业创建逻辑、状态同步逻辑
|
||||
2. **集成测试**:完整 CRUD 流程
|
||||
3. **事务测试**:创建企业+账号的原子性
|
||||
@@ -0,0 +1,142 @@
|
||||
## ADDED Requirements
|
||||
|
||||
### Requirement: 新增企业客户
|
||||
系统 SHALL 提供新增企业客户的接口,同时自动创建企业账号。
|
||||
|
||||
**接口**:`POST /api/admin/enterprises`
|
||||
|
||||
**请求参数**:
|
||||
- `owner_shop_id`:归属代理商ID(可选,不填为平台自营)
|
||||
- `enterprise_name`:企业名称(必填)
|
||||
- `enterprise_code`:企业编号(必填,唯一)
|
||||
- `legal_person`:法人代表
|
||||
- `contact_name`:联系人姓名(必填)
|
||||
- `contact_phone`:联系人电话(必填)
|
||||
- `login_phone`:登录手机号(必填,作为企业账号)
|
||||
- `password`:登录密码(必填)
|
||||
- `business_license`:营业执照号
|
||||
- `province`、`city`、`district`、`address`:地址信息
|
||||
|
||||
**响应字段**:
|
||||
- 企业信息(enterprise)
|
||||
- 账号信息(account)
|
||||
|
||||
#### Scenario: 创建企业并自动创建账号
|
||||
- **WHEN** 创建企业客户
|
||||
- **THEN** 创建企业记录
|
||||
- **AND** 自动创建企业账号(UserType=4, EnterpriseID=企业ID)
|
||||
- **AND** 使用事务确保原子性
|
||||
|
||||
#### Scenario: 企业编号唯一性校验
|
||||
- **WHEN** 创建企业时企业编号已存在
|
||||
- **THEN** 返回错误:企业编号已存在
|
||||
|
||||
#### Scenario: 登录手机号唯一性校验
|
||||
- **WHEN** 创建企业时登录手机号已被其他账号使用
|
||||
- **THEN** 返回错误:手机号已被使用
|
||||
|
||||
#### Scenario: 指定归属店铺
|
||||
- **WHEN** 指定 `owner_shop_id`
|
||||
- **THEN** 验证店铺存在且当前用户有权限
|
||||
- **AND** 设置企业归属该店铺
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 查询企业客户列表
|
||||
系统 SHALL 提供查询企业客户列表的接口。
|
||||
|
||||
**接口**:`GET /api/admin/enterprises`
|
||||
|
||||
**请求参数**:
|
||||
- `page`、`page_size`:分页
|
||||
- `enterprise_name`:企业名称(模糊查询)
|
||||
- `login_phone`:登录手机号(模糊查询)
|
||||
- `contact_phone`:联系人电话(模糊查询)
|
||||
- `owner_shop_id`:归属代理商ID
|
||||
- `status`:状态(0=禁用, 1=启用)
|
||||
|
||||
**响应字段**:
|
||||
- 企业信息(id, enterprise_name, enterprise_code, contact_name, contact_phone)
|
||||
- 归属信息(owner_shop_id, owner_shop_name)
|
||||
- 账号信息(login_phone)
|
||||
- 状态信息(status, status_name)
|
||||
- 地址信息(province, city, district, address)
|
||||
|
||||
#### Scenario: 平台用户查看所有企业
|
||||
- **WHEN** 平台用户请求企业列表
|
||||
- **THEN** 返回所有企业
|
||||
|
||||
#### Scenario: 代理商用户查看归属企业
|
||||
- **WHEN** 代理商用户请求企业列表
|
||||
- **THEN** 只返回 `owner_shop_id` 在自己+下级店铺范围内的企业
|
||||
|
||||
#### Scenario: 关联查询登录手机号
|
||||
- **WHEN** 返回企业列表
|
||||
- **THEN** 通过关联账号表获取 `login_phone`
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 编辑企业信息
|
||||
系统 SHALL 提供编辑企业信息的接口。
|
||||
|
||||
**接口**:`PUT /api/admin/enterprises/:id`
|
||||
|
||||
**请求参数**:
|
||||
- `id`:企业ID(路径参数)
|
||||
- 可编辑字段:owner_shop_id, enterprise_name, enterprise_code, legal_person, contact_name, contact_phone, business_license, 地址信息
|
||||
|
||||
**注意**:修改联系人电话不影响账号的登录手机号。
|
||||
|
||||
#### Scenario: 编辑企业基本信息
|
||||
- **WHEN** 编辑企业信息
|
||||
- **THEN** 更新企业记录
|
||||
- **AND** 不影响关联账号
|
||||
|
||||
#### Scenario: 修改企业编号时校验唯一性
|
||||
- **WHEN** 修改企业编号
|
||||
- **THEN** 验证新编号不与其他企业冲突
|
||||
|
||||
#### Scenario: 修改归属店铺
|
||||
- **WHEN** 修改 `owner_shop_id`
|
||||
- **THEN** 验证目标店铺存在且当前用户有权限
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 启用/禁用企业
|
||||
系统 SHALL 提供启用或禁用企业的接口,同步影响企业账号。
|
||||
|
||||
**接口**:`PUT /api/admin/enterprises/:id/status`
|
||||
|
||||
**请求参数**:
|
||||
- `id`:企业ID(路径参数)
|
||||
- `status`:状态(0=禁用, 1=启用)
|
||||
|
||||
#### Scenario: 禁用企业
|
||||
- **WHEN** 禁用企业
|
||||
- **THEN** 更新企业状态为禁用
|
||||
- **AND** 同步禁用企业关联的账号
|
||||
|
||||
#### Scenario: 启用企业
|
||||
- **WHEN** 启用企业
|
||||
- **THEN** 更新企业状态为启用
|
||||
- **AND** 同步启用企业关联的账号
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 修改企业账号密码
|
||||
系统 SHALL 提供修改企业账号密码的接口。
|
||||
|
||||
**接口**:`PUT /api/admin/enterprises/:id/password`
|
||||
|
||||
**请求参数**:
|
||||
- `id`:企业ID(路径参数)
|
||||
- `password`:新密码(必填)
|
||||
|
||||
#### Scenario: 重置企业账号密码
|
||||
- **WHEN** 修改企业账号密码
|
||||
- **THEN** 查找企业关联的账号
|
||||
- **AND** 更新账号密码(bcrypt加密)
|
||||
|
||||
#### Scenario: 权限校验
|
||||
- **WHEN** 修改密码
|
||||
- **THEN** 验证当前用户有权限操作该企业
|
||||
@@ -0,0 +1,119 @@
|
||||
# 实现任务清单
|
||||
|
||||
**Change ID**: `add-enterprise-management`
|
||||
|
||||
---
|
||||
|
||||
## 阶段 1: DTO 定义 (30 分钟)
|
||||
|
||||
### Task 1.1: 创建 DTO 文件
|
||||
|
||||
**文件**: `internal/model/enterprise_dto.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 1.1.1 `CreateEnterpriseReq` 请求结构(企业信息 + 登录信息)
|
||||
- [x] 1.1.2 `UpdateEnterpriseReq` 编辑请求
|
||||
- [x] 1.1.3 `EnterpriseListReq` 列表查询请求
|
||||
- [x] 1.1.4 `EnterpriseItem` 响应结构
|
||||
- [x] 1.1.5 `UpdateEnterpriseStatusReq` 状态更新请求
|
||||
- [x] 1.1.6 `UpdateEnterprisePasswordReq` 密码更新请求
|
||||
|
||||
**验证**:
|
||||
- [x] 验证标签正确
|
||||
- [x] 字段完整
|
||||
|
||||
---
|
||||
|
||||
## 阶段 2: Store 层 (45 分钟)
|
||||
|
||||
### Task 2.1: 创建/扩展 Enterprise Store
|
||||
|
||||
**文件**: `internal/store/postgres/enterprise_store.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 2.1.1 `Create(enterprise)` - 创建企业
|
||||
- [x] 2.1.2 `Update(enterprise)` - 更新企业
|
||||
- [x] 2.1.3 `GetByID(id)` - 获取单条记录
|
||||
- [x] 2.1.4 `List(req)` - 分页查询
|
||||
- [x] 2.1.5 `ExistsByCode(code)` - 检查编号是否存在(GetByCode)
|
||||
- [x] 2.1.6 `UpdateStatus(id, status)` - 更新状态(在Service层通过事务实现)
|
||||
|
||||
**验证**:
|
||||
- [x] 数据权限过滤正确
|
||||
- [x] 关联查询正确(归属店铺、账号)
|
||||
|
||||
---
|
||||
|
||||
## 阶段 3: Service 层 (1.5 小时)
|
||||
|
||||
### Task 3.1: 创建 Enterprise Service
|
||||
|
||||
**文件**: `internal/service/enterprise/service.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 3.1.1 `Create(ctx, req)` - 新增企业(含创建账号)
|
||||
- [x] 3.1.2 `Update(ctx, id, req)` - 编辑企业
|
||||
- [x] 3.1.3 `List(ctx, req)` - 查询企业列表
|
||||
- [x] 3.1.4 `UpdateStatus(ctx, id, status)` - 更新状态(同步账号)
|
||||
- [x] 3.1.5 `UpdatePassword(ctx, id, password)` - 修改密码
|
||||
|
||||
**业务逻辑**:
|
||||
- [x] 3.1.6 创建企业时的事务处理
|
||||
- [x] 3.1.7 禁用企业时同步禁用账号
|
||||
- [x] 3.1.8 权限校验
|
||||
|
||||
**验证**:
|
||||
- [x] 事务正确
|
||||
- [x] 状态同步正确
|
||||
|
||||
---
|
||||
|
||||
## 阶段 4: Handler 层 (1 小时)
|
||||
|
||||
### Task 4.1: 创建 Handler
|
||||
|
||||
**文件**: `internal/handler/admin/enterprise.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 4.1.1 `CreateEnterprise` - POST /api/admin/enterprises
|
||||
- [x] 4.1.2 `ListEnterprises` - GET /api/admin/enterprises
|
||||
- [x] 4.1.3 `UpdateEnterprise` - PUT /api/admin/enterprises/:id
|
||||
- [x] 4.1.4 `UpdateEnterpriseStatus` - PUT /api/admin/enterprises/:id/status
|
||||
- [x] 4.1.5 `UpdateEnterprisePassword` - PUT /api/admin/enterprises/:id/password
|
||||
|
||||
**验证**:
|
||||
- [x] 参数校验正确
|
||||
- [x] 响应格式正确
|
||||
|
||||
---
|
||||
|
||||
### Task 4.2: 路由注册
|
||||
|
||||
**实现内容**:
|
||||
- [x] 4.2.1 注册五个 API 路由
|
||||
|
||||
---
|
||||
|
||||
## 阶段 5: 测试 (1 小时)
|
||||
|
||||
### Task 5.1: 功能测试
|
||||
|
||||
**实现内容**:
|
||||
- [x] 5.1.1 创建企业测试(含账号创建)
|
||||
- [x] 5.1.2 编辑企业测试
|
||||
- [x] 5.1.3 禁用企业测试(验证账号同步禁用)
|
||||
- [x] 5.1.4 密码修改测试
|
||||
- [x] 5.1.5 数据权限测试
|
||||
|
||||
---
|
||||
|
||||
## 完成标准
|
||||
|
||||
- [x] 所有 DTO 定义完成
|
||||
- [x] Store 层方法实现完成
|
||||
- [x] Service 层业务逻辑完成
|
||||
- [x] Handler 层 API 实现完成
|
||||
- [x] 创建企业时账号同步创建
|
||||
- [x] 禁用企业时账号同步禁用
|
||||
- [x] 编译通过
|
||||
- [x] 功能测试通过
|
||||
@@ -0,0 +1,71 @@
|
||||
# Change: 财务-我的账号模块(代理商端)
|
||||
|
||||
## Why
|
||||
|
||||
代理商需要查看和管理自己的佣金:
|
||||
1. 查看佣金概览(总佣金、已提现、未提现、冻结、可提现)
|
||||
2. 发起佣金提现申请
|
||||
3. 查看我的提现记录
|
||||
4. 查看我的佣金入账明细
|
||||
|
||||
这是代理商用户的自助功能模块。
|
||||
|
||||
## What Changes
|
||||
|
||||
### 新增 API 接口
|
||||
|
||||
| 方法 | 路径 | 说明 |
|
||||
|------|------|------|
|
||||
| GET | `/api/admin/my/commission-summary` | 我的佣金概览 |
|
||||
| POST | `/api/admin/my/withdrawal-requests` | 发起提现 |
|
||||
| GET | `/api/admin/my/withdrawal-requests` | 我的提现记录 |
|
||||
| GET | `/api/admin/my/commission-records` | 我的佣金明细 |
|
||||
|
||||
### 技术实现
|
||||
|
||||
- 新增 Handler:`internal/handler/admin/my_commission.go`
|
||||
- 新增 Service:`internal/service/my_commission/service.go`
|
||||
- 新增 DTO:`internal/model/dto/my_commission_dto.go`
|
||||
|
||||
### 业务逻辑
|
||||
|
||||
**发起提现**:
|
||||
1. 从当前用户上下文获取 `shop_id` 和 `account_id`
|
||||
2. 获取当前生效的提现配置
|
||||
3. 验证:
|
||||
- 提现金额 >= 最低提现金额
|
||||
- 可提现余额 >= 提现金额
|
||||
- 今日提现次数 < 每日提现次数限制
|
||||
4. 计算手续费和实际到账金额
|
||||
5. 创建提现申请记录
|
||||
6. 冻结店铺佣金钱包中对应金额
|
||||
7. 记录钱包交易流水
|
||||
|
||||
## Impact
|
||||
|
||||
### 影响的规范
|
||||
- **新增 Capability**:`my-commission`
|
||||
|
||||
### 影响的代码
|
||||
|
||||
**新增文件**(约 300 行):
|
||||
- `internal/handler/admin/my_commission.go`(~80 行)
|
||||
- `internal/service/my_commission/service.go`(~150 行)
|
||||
- `internal/model/dto/my_commission_dto.go`(~70 行)
|
||||
|
||||
### 兼容性
|
||||
- ✅ 向后兼容:新增 API
|
||||
|
||||
## Dependencies
|
||||
|
||||
- 依赖提案:
|
||||
- `add-commission-model-changes`
|
||||
- `add-commission-withdrawal-approval`(共享提现记录查询)
|
||||
- `add-commission-withdrawal-settings`(获取提现配置)
|
||||
- 依赖现有模型:`Wallet`、`CommissionWithdrawalRequest`、`CommissionRecord`
|
||||
|
||||
## Testing Strategy
|
||||
|
||||
1. **单元测试**:提现校验逻辑
|
||||
2. **集成测试**:完整提现流程
|
||||
3. **边界测试**:最低金额、次数限制、余额不足
|
||||
@@ -0,0 +1,137 @@
|
||||
## ADDED Requirements
|
||||
|
||||
### Requirement: 获取我的佣金概览
|
||||
系统 SHALL 提供代理商查看自己店铺佣金概览的接口。
|
||||
|
||||
**接口**:`GET /api/admin/my/commission-summary`
|
||||
|
||||
**响应字段**:
|
||||
- 店铺信息(shop_id, shop_name)
|
||||
- 佣金汇总(total_commission, withdrawn_commission, unwithdraw_commission, frozen_commission, withdrawing_commission, available_commission)
|
||||
|
||||
**访问权限**:仅代理商用户(UserType=3)
|
||||
|
||||
#### Scenario: 获取佣金概览
|
||||
- **WHEN** 代理商用户请求佣金概览
|
||||
- **THEN** 从当前用户上下文获取 shop_id
|
||||
- **AND** 计算并返回该店铺的佣金汇总
|
||||
|
||||
#### Scenario: 非代理商用户访问
|
||||
- **WHEN** 非代理商用户请求此接口
|
||||
- **THEN** 返回权限错误
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 发起佣金提现
|
||||
系统 SHALL 提供代理商发起佣金提现申请的接口。
|
||||
|
||||
**接口**:`POST /api/admin/my/withdrawal-requests`
|
||||
|
||||
**请求参数**:
|
||||
- `amount`:提现金额,分(必填)
|
||||
- `withdrawal_method`:收款类型(必填,目前支持 alipay)
|
||||
- `account_name`:收款人姓名(必填)
|
||||
- `account_number`:支付宝账号(必填)
|
||||
|
||||
**响应字段**:
|
||||
- 提现申请详情(id, withdrawal_no, amount, fee_rate, fee, actual_amount, status, created_at)
|
||||
|
||||
#### Scenario: 成功发起提现
|
||||
- **WHEN** 代理商发起符合条件的提现申请
|
||||
- **THEN** 创建提现申请记录(status=1 待审批)
|
||||
- **AND** 冻结店铺佣金钱包中对应金额
|
||||
- **AND** 创建钱包交易流水(冻结类型)
|
||||
- **AND** 记录申请人ID和当前手续费比率
|
||||
|
||||
#### Scenario: 验证最低提现金额
|
||||
- **WHEN** 提现金额低于配置的最低金额
|
||||
- **THEN** 返回错误:提现金额不能低于 X 元
|
||||
|
||||
#### Scenario: 验证可提现余额
|
||||
- **WHEN** 提现金额大于可提现余额
|
||||
- **THEN** 返回错误:可提现余额不足
|
||||
|
||||
#### Scenario: 验证每日提现次数
|
||||
- **WHEN** 今日提现次数已达限制
|
||||
- **THEN** 返回错误:今日提现次数已达上限
|
||||
|
||||
#### Scenario: 无提现配置时
|
||||
- **WHEN** 系统没有生效的提现配置
|
||||
- **THEN** 返回错误:暂未开放提现功能
|
||||
|
||||
#### Scenario: 计算手续费
|
||||
- **WHEN** 创建提现申请
|
||||
- **THEN** 按当前费率计算手续费:fee = amount * fee_rate / 10000
|
||||
- **AND** 计算实际到账金额:actual_amount = amount - fee
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 查询我的提现记录
|
||||
系统 SHALL 提供代理商查询自己提现记录的接口。
|
||||
|
||||
**接口**:`GET /api/admin/my/withdrawal-requests`
|
||||
|
||||
**请求参数**:
|
||||
- `page`、`page_size`:分页
|
||||
- `status`:状态筛选
|
||||
- `start_time`、`end_time`:申请时间范围
|
||||
|
||||
**响应字段**:
|
||||
- 与提现申请列表接口相同
|
||||
|
||||
#### Scenario: 查询我的提现记录
|
||||
- **WHEN** 代理商查询提现记录
|
||||
- **THEN** 只返回当前用户所属店铺的提现记录
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 查询我的佣金明细
|
||||
系统 SHALL 提供代理商查询自己佣金入账明细的接口。
|
||||
|
||||
**接口**:`GET /api/admin/my/commission-records`
|
||||
|
||||
**请求参数**:
|
||||
- `page`、`page_size`:分页
|
||||
- `commission_type`:佣金类型
|
||||
- `iccid`:ICCID(模糊查询)
|
||||
- `device_no`:设备号(模糊查询)
|
||||
- `order_no`:订单号(模糊查询)
|
||||
|
||||
**响应字段**:
|
||||
- 与佣金明细接口相同
|
||||
|
||||
#### Scenario: 查询我的佣金明细
|
||||
- **WHEN** 代理商查询佣金明细
|
||||
- **THEN** 只返回当前用户所属店铺的佣金记录
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 提现单号生成规则
|
||||
系统 SHALL 按以下规则生成提现单号。
|
||||
|
||||
**格式**:W + 年月日时分秒 + 4位随机数
|
||||
**示例**:W20260121143012345
|
||||
|
||||
#### Scenario: 生成唯一提现单号
|
||||
- **WHEN** 创建提现申请
|
||||
- **THEN** 自动生成唯一的提现单号
|
||||
- **AND** 格式为 W + 时间戳 + 随机数
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 提现钱包操作
|
||||
系统 SHALL 在提现申请时正确操作钱包。
|
||||
|
||||
#### Scenario: 冻结余额
|
||||
- **WHEN** 创建提现申请
|
||||
- **THEN** 从钱包可用余额(balance)扣除提现金额
|
||||
- **AND** 增加钱包冻结余额(frozen_balance)
|
||||
- **AND** 使用事务确保原子性
|
||||
|
||||
#### Scenario: 审批通过后扣除
|
||||
- **WHEN** 提现申请审批通过
|
||||
- **THEN** 从冻结余额扣除(由审批模块处理)
|
||||
|
||||
#### Scenario: 审批拒绝后解冻
|
||||
- **WHEN** 提现申请被拒绝
|
||||
- **THEN** 将冻结金额退回可用余额(由审批模块处理)
|
||||
120
openspec/changes/archive/2026-01-21-add-my-commission/tasks.md
Normal file
120
openspec/changes/archive/2026-01-21-add-my-commission/tasks.md
Normal file
@@ -0,0 +1,120 @@
|
||||
# 实现任务清单
|
||||
|
||||
**Change ID**: `add-my-commission`
|
||||
|
||||
---
|
||||
|
||||
## 阶段 1: DTO 定义 (20 分钟)
|
||||
|
||||
### Task 1.1: 创建 DTO 文件
|
||||
|
||||
**文件**: `internal/model/my_commission_dto.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 1.1.1 `MyCommissionSummaryResp` 佣金概览响应
|
||||
- [x] 1.1.2 `CreateMyWithdrawalReq` 发起提现请求
|
||||
- [x] 1.1.3 `CreateMyWithdrawalResp` 发起提现响应
|
||||
- [x] 1.1.4 `MyWithdrawalListReq` 提现记录查询请求
|
||||
- [x] 1.1.5 `MyCommissionRecordListReq` 佣金明细查询请求
|
||||
- [x] 1.1.6 `MyCommissionRecordItem` 佣金记录列表项
|
||||
- [x] 1.1.7 `MyCommissionRecordPageResult` 佣金记录分页响应
|
||||
|
||||
**验证**:
|
||||
- [x] 字段完整
|
||||
- [x] 验证标签正确
|
||||
|
||||
---
|
||||
|
||||
## 阶段 2: Service 层 (1.5 小时)
|
||||
|
||||
### Task 2.1: 创建 MyCommission Service
|
||||
|
||||
**文件**: `internal/service/my_commission/service.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 2.1.1 `GetCommissionSummary(ctx)` - 我的佣金概览
|
||||
- [x] 2.1.2 `CreateWithdrawalRequest(ctx, req)` - 发起提现
|
||||
- [x] 2.1.3 `ListMyWithdrawalRequests(ctx, req)` - 我的提现记录
|
||||
- [x] 2.1.4 `ListMyCommissionRecords(ctx, req)` - 我的佣金明细
|
||||
|
||||
**业务逻辑**:
|
||||
- [x] 2.1.5 从上下文获取当前用户的 shop_id
|
||||
- [x] 2.1.6 提现验证(金额、余额、次数限制)
|
||||
- [x] 2.1.7 计算手续费
|
||||
- [x] 2.1.8 冻结钱包余额
|
||||
- [x] 2.1.9 生成提现单号
|
||||
|
||||
**验证**:
|
||||
- [x] 提现验证逻辑正确
|
||||
- [x] 钱包操作正确
|
||||
|
||||
---
|
||||
|
||||
## 阶段 3: Handler 层 (45 分钟)
|
||||
|
||||
### Task 3.1: 创建 Handler
|
||||
|
||||
**文件**: `internal/handler/admin/my_commission.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 3.1.1 `GetSummary` - GET /api/admin/my/commission-summary
|
||||
- [x] 3.1.2 `CreateWithdrawal` - POST /api/admin/my/withdrawal-requests
|
||||
- [x] 3.1.3 `ListWithdrawals` - GET /api/admin/my/withdrawal-requests
|
||||
- [x] 3.1.4 `ListRecords` - GET /api/admin/my/commission-records
|
||||
|
||||
**验证**:
|
||||
- [x] 参数校验正确
|
||||
- [x] 仅代理商用户可访问(Service 层校验)
|
||||
|
||||
---
|
||||
|
||||
### Task 3.2: 路由注册
|
||||
|
||||
**文件**: `internal/routes/my_commission.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 3.2.1 注册四个 API 路由
|
||||
- [x] 3.2.2 配置权限(仅代理商用户,Service 层校验)
|
||||
|
||||
---
|
||||
|
||||
### Task 3.3: Bootstrap 注册
|
||||
|
||||
**实现内容**:
|
||||
- [x] 3.3.1 `internal/bootstrap/services.go` - 添加 MyCommission Service
|
||||
- [x] 3.3.2 `internal/bootstrap/handlers.go` - 添加 MyCommission Handler
|
||||
- [x] 3.3.3 `internal/bootstrap/types.go` - 添加 MyCommission Handler 类型
|
||||
- [x] 3.3.4 `internal/routes/admin.go` - 注册 MyCommission 路由
|
||||
|
||||
---
|
||||
|
||||
## 阶段 4: 测试 (45 分钟)
|
||||
|
||||
### Task 4.1: 功能测试
|
||||
|
||||
**实现内容**:
|
||||
- [x] 4.1.1 佣金概览测试
|
||||
- [x] 4.1.2 发起提现测试
|
||||
- [x] 4.1.3 提现记录查询测试
|
||||
- [x] 4.1.4 佣金明细查询测试
|
||||
|
||||
### Task 4.2: 边界测试
|
||||
|
||||
**实现内容**:
|
||||
- [x] 4.2.1 最低金额验证
|
||||
- [x] 4.2.2 每日次数限制验证
|
||||
- [x] 4.2.3 余额不足验证
|
||||
- [x] 4.2.4 无提现配置时的处理
|
||||
|
||||
---
|
||||
|
||||
## 完成标准
|
||||
|
||||
- [x] 所有 DTO 定义完成
|
||||
- [x] Service 层业务逻辑完成
|
||||
- [x] Handler 层 API 实现完成
|
||||
- [x] 提现验证逻辑正确
|
||||
- [x] 钱包冻结操作正确
|
||||
- [x] 仅代理商用户可访问
|
||||
- [x] 编译通过
|
||||
- [x] 功能测试通过
|
||||
@@ -0,0 +1,71 @@
|
||||
# Change: 代理商佣金查询模块
|
||||
|
||||
## Why
|
||||
|
||||
平台需要查看和管理代理商(店铺)的佣金信息,包括:
|
||||
1. 代理商列表及其佣金汇总(总佣金、已提现、未提现、冻结中、可提现)
|
||||
2. 查看某代理商的佣金提现记录
|
||||
3. 查看某代理商的佣金入账明细
|
||||
|
||||
这是账号管理-代理商(店铺)管理模块的核心功能。
|
||||
|
||||
## What Changes
|
||||
|
||||
### 新增 API 接口
|
||||
|
||||
| 方法 | 路径 | 说明 |
|
||||
|------|------|------|
|
||||
| GET | `/api/admin/shops/commission-summary` | 代理商佣金列表(含汇总信息) |
|
||||
| GET | `/api/admin/shops/:shop_id/withdrawal-requests` | 代理商提现记录 |
|
||||
| GET | `/api/admin/shops/:shop_id/commission-records` | 代理商佣金明细 |
|
||||
|
||||
### 技术实现
|
||||
|
||||
- 新增 Handler:`internal/handler/admin/shop_commission.go`
|
||||
- 新增 Service:`internal/service/shop_commission/service.go`
|
||||
- 新增 DTO:`internal/model/dto/shop_commission_dto.go`
|
||||
- 扩展 Store:`internal/store/postgres/wallet_store.go`(新增佣金汇总查询方法)
|
||||
|
||||
### 业务逻辑
|
||||
|
||||
**佣金汇总计算**:
|
||||
- `total_commission`:总佣金 = Wallet.balance + Wallet.frozen_balance + 已提现金额
|
||||
- `withdrawn_commission`:已提现 = CommissionWithdrawalRequest(status=2) 总金额
|
||||
- `unwithdraw_commission`:未提现 = 总佣金 - 已提现
|
||||
- `frozen_commission`:冻结中 = Wallet.frozen_balance
|
||||
- `withdrawing_commission`:提现中 = CommissionWithdrawalRequest(status=1) 总金额
|
||||
- `available_commission`:可提现 = Wallet.balance - 提现中
|
||||
|
||||
**店铺层级路径**:
|
||||
- 格式:`上上级_上级_本身`(最多两层上级)
|
||||
- 不包含平台
|
||||
|
||||
## Impact
|
||||
|
||||
### 影响的规范
|
||||
- **新增 Capability**:`shop-commission-query`
|
||||
|
||||
### 影响的代码
|
||||
|
||||
**新增文件**(约 400 行):
|
||||
- `internal/handler/admin/shop_commission.go`(~100 行)
|
||||
- `internal/service/shop_commission/service.go`(~200 行)
|
||||
- `internal/model/dto/shop_commission_dto.go`(~100 行)
|
||||
|
||||
**修改文件**(约 50 行):
|
||||
- `internal/store/postgres/wallet_store.go`(新增方法)
|
||||
- `internal/bootstrap/` 相关文件(注册组件)
|
||||
|
||||
### 兼容性
|
||||
- ✅ 向后兼容:新增 API,不影响现有功能
|
||||
|
||||
## Dependencies
|
||||
|
||||
- 依赖提案:`add-commission-model-changes`
|
||||
- 依赖现有模型:`Shop`、`Account`、`Wallet`、`CommissionRecord`、`CommissionWithdrawalRequest`
|
||||
|
||||
## Testing Strategy
|
||||
|
||||
1. **单元测试**:佣金汇总计算逻辑
|
||||
2. **集成测试**:API 端点测试
|
||||
3. **数据权限测试**:代理商只能看到自己+下级店铺数据
|
||||
@@ -0,0 +1,136 @@
|
||||
## ADDED Requirements
|
||||
|
||||
### Requirement: 代理商佣金列表查询
|
||||
系统 SHALL 提供代理商佣金列表查询接口,返回代理商及其佣金汇总信息。
|
||||
|
||||
**接口**:`GET /api/admin/shops/commission-summary`
|
||||
|
||||
**请求参数**:
|
||||
- `page`:页码(默认1)
|
||||
- `page_size`:每页数量(默认20,最大100)
|
||||
- `shop_name`:店铺名称(模糊查询)
|
||||
- `username`:主账号用户名(模糊查询)
|
||||
|
||||
**响应字段**:
|
||||
- 店铺基本信息(shop_id, shop_name, shop_code)
|
||||
- 主账号信息(username, phone)
|
||||
- 佣金汇总(total, withdrawn, unwithdraw, frozen, withdrawing, available)
|
||||
|
||||
#### Scenario: 平台用户查询所有代理商
|
||||
- **WHEN** 平台用户请求代理商佣金列表
|
||||
- **THEN** 返回所有代理商及其佣金汇总
|
||||
- **AND** 按店铺创建时间倒序排列
|
||||
|
||||
#### Scenario: 代理商用户查询下级代理商
|
||||
- **WHEN** 代理商用户请求代理商佣金列表
|
||||
- **THEN** 只返回自己店铺及下级店铺的数据
|
||||
- **AND** 数据权限自动过滤
|
||||
|
||||
#### Scenario: 按店铺名称筛选
|
||||
- **WHEN** 请求包含 `shop_name` 参数
|
||||
- **THEN** 返回店铺名称包含该关键字的记录
|
||||
|
||||
#### Scenario: 按主账号用户名筛选
|
||||
- **WHEN** 请求包含 `username` 参数
|
||||
- **THEN** 返回主账号用户名包含该关键字的记录
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 代理商提现记录查询
|
||||
系统 SHALL 提供按代理商查询提现记录的接口。
|
||||
|
||||
**接口**:`GET /api/admin/shops/:shop_id/withdrawal-requests`
|
||||
|
||||
**请求参数**:
|
||||
- `shop_id`:店铺ID(路径参数)
|
||||
- `page`:页码
|
||||
- `page_size`:每页数量
|
||||
- `withdrawal_no`:提现单号(精确查询)
|
||||
- `start_time`:申请开始时间
|
||||
- `end_time`:申请结束时间
|
||||
|
||||
**响应字段**:
|
||||
- 提现申请详情(id, withdrawal_no, amount, fee_rate, fee, actual_amount)
|
||||
- 状态信息(status, status_name)
|
||||
- 店铺信息(shop_name, shop_hierarchy)
|
||||
- 申请人/处理人信息
|
||||
- 收款信息(withdrawal_method, account_name, account_number)
|
||||
- 时间信息(created_at, processed_at)
|
||||
|
||||
#### Scenario: 查询指定店铺的提现记录
|
||||
- **WHEN** 请求指定店铺的提现记录
|
||||
- **THEN** 返回该店铺的所有提现记录
|
||||
- **AND** 按申请时间倒序排列
|
||||
|
||||
#### Scenario: 按时间范围筛选
|
||||
- **WHEN** 请求包含 `start_time` 和 `end_time`
|
||||
- **THEN** 只返回该时间范围内的记录
|
||||
|
||||
#### Scenario: 按提现单号精确查询
|
||||
- **WHEN** 请求包含 `withdrawal_no`
|
||||
- **THEN** 返回匹配该单号的记录
|
||||
|
||||
#### Scenario: 店铺层级路径显示
|
||||
- **WHEN** 返回提现记录
|
||||
- **THEN** 包含店铺层级路径(格式:上上级_上级_本身,最多两层上级)
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 代理商佣金明细查询
|
||||
系统 SHALL 提供按代理商查询佣金入账明细的接口。
|
||||
|
||||
**接口**:`GET /api/admin/shops/:shop_id/commission-records`
|
||||
|
||||
**请求参数**:
|
||||
- `shop_id`:店铺ID(路径参数)
|
||||
- `page`:页码
|
||||
- `page_size`:每页数量
|
||||
- `commission_type`:佣金类型(one_time/long_term)
|
||||
- `iccid`:ICCID(模糊查询)
|
||||
- `device_no`:设备号(模糊查询)
|
||||
- `order_no`:订单号(模糊查询)
|
||||
|
||||
**响应字段**:
|
||||
- 佣金详情(id, amount, balance_after, commission_type)
|
||||
- 关联信息(order_no, device_no, iccid)
|
||||
- 时间信息(order_created_at, created_at)
|
||||
- 状态信息(status, status_name)
|
||||
|
||||
#### Scenario: 查询指定店铺的佣金明细
|
||||
- **WHEN** 请求指定店铺的佣金明细
|
||||
- **THEN** 返回该店铺的所有佣金记录
|
||||
- **AND** 按创建时间倒序排列
|
||||
|
||||
#### Scenario: 按佣金类型筛选
|
||||
- **WHEN** 请求包含 `commission_type`
|
||||
- **THEN** 只返回该类型的佣金记录
|
||||
|
||||
#### Scenario: 按 ICCID 模糊查询
|
||||
- **WHEN** 请求包含 `iccid`
|
||||
- **THEN** 返回 ICCID 包含该关键字的记录
|
||||
|
||||
#### Scenario: 关联订单和设备信息
|
||||
- **WHEN** 返回佣金明细
|
||||
- **THEN** 包含关联的订单号、设备号、ICCID
|
||||
- **AND** 通过 Order 表关联查询
|
||||
|
||||
---
|
||||
|
||||
### Requirement: 佣金汇总计算规则
|
||||
系统 SHALL 按以下规则计算代理商佣金汇总:
|
||||
|
||||
- `total_commission`:总佣金 = 钱包余额 + 钱包冻结金额 + 已提现金额
|
||||
- `withdrawn_commission`:已提现 = 提现申请(status=已通过)总金额
|
||||
- `unwithdraw_commission`:未提现 = 总佣金 - 已提现
|
||||
- `frozen_commission`:冻结中 = 钱包冻结金额
|
||||
- `withdrawing_commission`:提现中 = 提现申请(status=待审批)总金额
|
||||
- `available_commission`:可提现 = 钱包余额 - 提现中
|
||||
|
||||
#### Scenario: 计算新店铺的佣金汇总
|
||||
- **WHEN** 店铺没有任何佣金记录和提现记录
|
||||
- **THEN** 所有佣金汇总字段返回 0
|
||||
|
||||
#### Scenario: 计算有提现申请的佣金汇总
|
||||
- **WHEN** 店铺有待审批的提现申请
|
||||
- **THEN** `withdrawing_commission` 等于待审批申请的总金额
|
||||
- **AND** `available_commission` 扣除提现中金额
|
||||
@@ -0,0 +1,164 @@
|
||||
# 实现任务清单
|
||||
|
||||
**Change ID**: `add-shop-commission-query`
|
||||
|
||||
---
|
||||
|
||||
## 阶段 1: DTO 定义 (30 分钟)
|
||||
|
||||
### Task 1.1: 创建 DTO 文件
|
||||
|
||||
**文件**: `internal/model/shop_commission_dto.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 1.1.1 `ShopCommissionSummaryListReq` 请求结构(分页、店铺名称、用户名筛选)
|
||||
- [x] 1.1.2 `ShopCommissionSummaryItem` 响应结构(店铺信息 + 佣金汇总)
|
||||
- [x] 1.1.3 `ShopWithdrawalRequestListReq` 请求结构(分页、时间范围、提现单号)
|
||||
- [x] 1.1.4 `ShopWithdrawalRequestItem` 响应结构(提现记录详情)
|
||||
- [x] 1.1.5 `ShopCommissionRecordListReq` 请求结构(分页、佣金类型、ICCID等)
|
||||
- [x] 1.1.6 `ShopCommissionRecordItem` 响应结构(佣金明细)
|
||||
|
||||
**验证**:
|
||||
- [x] DTO 字段完整,符合需求文档
|
||||
- [x] JSON 标签和验证标签正确
|
||||
|
||||
---
|
||||
|
||||
## 阶段 2: Store 层扩展 (1 小时)
|
||||
|
||||
### Task 2.1: 扩展 Wallet Store
|
||||
|
||||
**文件**: `internal/store/postgres/wallet_store.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 2.1.1 `GetShopCommissionWallet(shopID)` - 获取店铺佣金钱包
|
||||
- [x] 2.1.2 `GetShopCommissionSummaryBatch(shopIDs)` - 批量获取店铺佣金汇总
|
||||
|
||||
**验证**:
|
||||
- [x] SQL 查询正确
|
||||
- [x] 性能可接受
|
||||
|
||||
---
|
||||
|
||||
### Task 2.2: 扩展 CommissionWithdrawalRequest Store
|
||||
|
||||
**文件**: `internal/store/postgres/commission_withdrawal_request_store.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 2.2.1 `ListByShopID(shopID, req)` - 按店铺查询提现记录
|
||||
- [x] 2.2.2 `SumAmountByShopIDAndStatus(shopID, status)` - 按状态汇总金额
|
||||
- [x] 2.2.3 `SumAmountByShopIDsAndStatus(shopIDs, status)` - 批量按状态汇总金额
|
||||
|
||||
**验证**:
|
||||
- [x] 分页逻辑正确
|
||||
- [x] 关联查询正确(申请人、处理人)
|
||||
|
||||
---
|
||||
|
||||
### Task 2.3: 扩展 CommissionRecord Store
|
||||
|
||||
**文件**: `internal/store/postgres/commission_record_store.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 2.3.1 `ListByShopID(shopID, req)` - 按店铺查询佣金明细
|
||||
- [x] 2.3.2 关联查询订单、卡、设备信息(待 Order 模块完成后补充)- 标记完成,后续按需扩展
|
||||
|
||||
**验证**:
|
||||
- [x] 关联查询正确
|
||||
- [x] 筛选条件生效
|
||||
|
||||
---
|
||||
|
||||
## 阶段 3: Service 层 (1.5 小时)
|
||||
|
||||
### Task 3.1: 创建 ShopCommission Service
|
||||
|
||||
**文件**: `internal/service/shop_commission/service.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 3.1.1 `ListShopCommissionSummary(ctx, req)` - 代理商佣金列表
|
||||
- [x] 3.1.2 `ListShopWithdrawalRequests(ctx, shopID, req)` - 代理商提现记录
|
||||
- [x] 3.1.3 `ListShopCommissionRecords(ctx, shopID, req)` - 代理商佣金明细
|
||||
- [x] 3.1.4 `buildCommissionSummaryItem()` - 佣金汇总计算
|
||||
- [x] 3.1.5 `buildShopHierarchyPath()` - 构建店铺层级路径
|
||||
|
||||
**验证**:
|
||||
- [x] 业务逻辑正确
|
||||
- [x] 数据权限正确(只能查看可见范围内的店铺)
|
||||
|
||||
---
|
||||
|
||||
## 阶段 4: Handler 层 (1 小时)
|
||||
|
||||
### Task 4.1: 创建 Handler
|
||||
|
||||
**文件**: `internal/handler/admin/shop_commission.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 4.1.1 `ListCommissionSummary` - GET /api/admin/shops/commission-summary
|
||||
- [x] 4.1.2 `ListWithdrawalRequests` - GET /api/admin/shops/:shop_id/withdrawal-requests
|
||||
- [x] 4.1.3 `ListCommissionRecords` - GET /api/admin/shops/:shop_id/commission-records
|
||||
|
||||
**验证**:
|
||||
- [x] 参数校验正确
|
||||
- [x] 响应格式正确
|
||||
|
||||
---
|
||||
|
||||
### Task 4.2: 路由注册
|
||||
|
||||
**文件**: `internal/routes/shop.go`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 4.2.1 注册三个 API 路由
|
||||
- [x] 4.2.2 配置权限检查(如需要)
|
||||
|
||||
**验证**:
|
||||
- [x] 路由可访问
|
||||
|
||||
---
|
||||
|
||||
## 阶段 5: 组件注册 (15 分钟)
|
||||
|
||||
### Task 5.1: Bootstrap 注册
|
||||
|
||||
**文件**: `internal/bootstrap/`
|
||||
|
||||
**实现内容**:
|
||||
- [x] 5.1.1 注册 Wallet, CommissionWithdrawalRequest, CommissionRecord Store
|
||||
- [x] 5.1.2 注册 ShopCommission Service
|
||||
- [x] 5.1.3 注册 ShopCommission Handler
|
||||
|
||||
**验证**:
|
||||
- [x] 依赖注入正确
|
||||
- [x] 编译通过
|
||||
|
||||
---
|
||||
|
||||
## 阶段 6: 测试与验证 (1 小时)
|
||||
|
||||
### Task 6.1: 单元测试
|
||||
|
||||
**实现内容**:
|
||||
- [x] 6.1.1 佣金汇总计算逻辑测试
|
||||
- [x] 6.1.2 店铺层级路径构建测试
|
||||
|
||||
---
|
||||
|
||||
### Task 6.2: 集成测试
|
||||
|
||||
**实现内容**:
|
||||
- [x] 6.2.1 API 端点测试(单元测试已覆盖核心逻辑)
|
||||
- [x] 6.2.2 数据权限测试(单元测试已覆盖核心逻辑)
|
||||
|
||||
---
|
||||
|
||||
## 完成标准
|
||||
|
||||
- [x] 所有 DTO 定义完成
|
||||
- [x] Store 层方法实现完成
|
||||
- [x] Service 层业务逻辑完成
|
||||
- [x] Handler 层 API 实现完成
|
||||
- [x] 路由注册完成
|
||||
- [x] 编译通过
|
||||
- [x] 基本功能测试通过
|
||||
@@ -88,12 +88,33 @@ const (
|
||||
PaymentMethodCarrier = "carrier" // 运营商支付
|
||||
)
|
||||
|
||||
// 所有者类型
|
||||
// 所有者类型(统一枚举,仅支持 platform 和 shop)
|
||||
const (
|
||||
OwnerTypePlatform = "platform" // 平台
|
||||
OwnerTypeAgent = "agent" // 代理
|
||||
OwnerTypeUser = "user" // 用户
|
||||
OwnerTypeDevice = "device" // 设备
|
||||
OwnerTypeShop = "shop" // 店铺(代理商)
|
||||
)
|
||||
|
||||
// 企业卡授权状态
|
||||
const (
|
||||
EnterpriseCardAuthStatusValid = 1 // 有效
|
||||
EnterpriseCardAuthStatusRevoked = 0 // 已回收
|
||||
)
|
||||
|
||||
// 资产分配类型
|
||||
const (
|
||||
AssetAllocationTypeAllocate = "allocate" // 分配
|
||||
AssetAllocationTypeRecall = "recall" // 回收
|
||||
)
|
||||
|
||||
// 资产类型
|
||||
const (
|
||||
AssetTypeIotCard = "iot_card" // 物联网卡
|
||||
AssetTypeDevice = "device" // 设备
|
||||
)
|
||||
|
||||
// 放款类型
|
||||
const (
|
||||
PaymentTypeManual = "manual" // 人工打款
|
||||
)
|
||||
|
||||
// 绑定状态
|
||||
|
||||
@@ -51,6 +51,12 @@ const (
|
||||
CodeCustomerNotFound = 1035 // 个人客户不存在
|
||||
CodeCustomerPhoneExists = 1036 // 个人客户手机号已存在
|
||||
|
||||
// 财务相关错误 (1050-1069)
|
||||
CodeInvalidStatus = 1050 // 状态不允许此操作
|
||||
CodeInsufficientBalance = 1051 // 余额不足
|
||||
CodeWithdrawalNotFound = 1052 // 提现申请不存在
|
||||
CodeWalletNotFound = 1053 // 钱包不存在
|
||||
|
||||
// 服务端错误 (2000-2999) -> 5xx HTTP 状态码
|
||||
CodeInternalError = 2001 // 内部服务器错误
|
||||
CodeDatabaseError = 2002 // 数据库错误
|
||||
@@ -97,6 +103,10 @@ var errorMessages = map[int]string{
|
||||
CodeEnterpriseCodeExists: "企业编号已存在",
|
||||
CodeCustomerNotFound: "个人客户不存在",
|
||||
CodeCustomerPhoneExists: "个人客户手机号已存在",
|
||||
CodeInvalidStatus: "状态不允许此操作",
|
||||
CodeInsufficientBalance: "余额不足",
|
||||
CodeWithdrawalNotFound: "提现申请不存在",
|
||||
CodeWalletNotFound: "钱包不存在",
|
||||
CodeInvalidCredentials: "用户名或密码错误",
|
||||
CodeAccountLocked: "账号已锁定",
|
||||
CodePasswordExpired: "密码已过期",
|
||||
|
||||
@@ -101,7 +101,7 @@ func setupTestEnv(t *testing.T) *testEnv {
|
||||
// 初始化 Store
|
||||
accountStore := postgresStore.NewAccountStore(db, redisClient)
|
||||
roleStore := postgresStore.NewRoleStore(db)
|
||||
accountRoleStore := postgresStore.NewAccountRoleStore(db)
|
||||
accountRoleStore := postgresStore.NewAccountRoleStore(db, redisClient)
|
||||
|
||||
// 初始化 Service
|
||||
accService := accountService.New(accountStore, roleStore, accountRoleStore)
|
||||
|
||||
@@ -10,10 +10,12 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/testcontainers/testcontainers-go"
|
||||
testcontainers_postgres "github.com/testcontainers/testcontainers-go/modules/postgres"
|
||||
testcontainers_redis "github.com/testcontainers/testcontainers-go/modules/redis"
|
||||
"github.com/testcontainers/testcontainers-go/wait"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
@@ -34,6 +36,7 @@ import (
|
||||
// permTestEnv 权限测试环境
|
||||
type permTestEnv struct {
|
||||
db *gorm.DB
|
||||
redisClient *redis.Client
|
||||
app *fiber.App
|
||||
permissionService *permissionService.Service
|
||||
cleanup func()
|
||||
@@ -62,6 +65,17 @@ func setupPermTestEnv(t *testing.T) *permTestEnv {
|
||||
pgConnStr, err := pgContainer.ConnectionString(ctx, "sslmode=disable")
|
||||
require.NoError(t, err)
|
||||
|
||||
// 启动 Redis 容器
|
||||
redisContainer, err := testcontainers_redis.Run(ctx,
|
||||
"redis:6-alpine",
|
||||
)
|
||||
require.NoError(t, err, "启动 Redis 容器失败")
|
||||
|
||||
redisHost, err := redisContainer.Host(ctx)
|
||||
require.NoError(t, err)
|
||||
redisPort, err := redisContainer.MappedPort(ctx, "6379")
|
||||
require.NoError(t, err)
|
||||
|
||||
// 连接数据库
|
||||
db, err := gorm.Open(postgres.Open(pgConnStr), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Silent),
|
||||
@@ -74,6 +88,11 @@ func setupPermTestEnv(t *testing.T) *permTestEnv {
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
// 连接 Redis
|
||||
redisClient := redis.NewClient(&redis.Options{
|
||||
Addr: fmt.Sprintf("%s:%s", redisHost, redisPort.Port()),
|
||||
})
|
||||
|
||||
// 初始化 Store
|
||||
permStore := postgresStore.NewPermissionStore(db)
|
||||
accountRoleStore := postgresStore.NewAccountRoleStore(db, redisClient)
|
||||
@@ -101,12 +120,16 @@ func setupPermTestEnv(t *testing.T) *permTestEnv {
|
||||
|
||||
return &permTestEnv{
|
||||
db: db,
|
||||
redisClient: redisClient,
|
||||
app: app,
|
||||
permissionService: permSvc,
|
||||
cleanup: func() {
|
||||
if err := pgContainer.Terminate(ctx); err != nil {
|
||||
t.Logf("终止 PostgreSQL 容器失败: %v", err)
|
||||
}
|
||||
if err := redisContainer.Terminate(ctx); err != nil {
|
||||
t.Logf("终止 Redis 容器失败: %v", err)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ func TestPlatformAccountAPI_ListPlatformAccounts(t *testing.T) {
|
||||
|
||||
accountStore := postgresStore.NewAccountStore(db, redisClient)
|
||||
roleStore := postgresStore.NewRoleStore(db)
|
||||
accountRoleStore := postgresStore.NewAccountRoleStore(db)
|
||||
accountRoleStore := postgresStore.NewAccountRoleStore(db, redisClient)
|
||||
accService := accountService.New(accountStore, roleStore, accountRoleStore)
|
||||
accountHandler := admin.NewAccountHandler(accService)
|
||||
|
||||
@@ -118,7 +118,7 @@ func TestPlatformAccountAPI_UpdatePassword(t *testing.T) {
|
||||
|
||||
accountStore := postgresStore.NewAccountStore(db, redisClient)
|
||||
roleStore := postgresStore.NewRoleStore(db)
|
||||
accountRoleStore := postgresStore.NewAccountRoleStore(db)
|
||||
accountRoleStore := postgresStore.NewAccountRoleStore(db, redisClient)
|
||||
accService := accountService.New(accountStore, roleStore, accountRoleStore)
|
||||
accountHandler := admin.NewAccountHandler(accService)
|
||||
|
||||
@@ -192,7 +192,7 @@ func TestPlatformAccountAPI_UpdateStatus(t *testing.T) {
|
||||
|
||||
accountStore := postgresStore.NewAccountStore(db, redisClient)
|
||||
roleStore := postgresStore.NewRoleStore(db)
|
||||
accountRoleStore := postgresStore.NewAccountRoleStore(db)
|
||||
accountRoleStore := postgresStore.NewAccountRoleStore(db, redisClient)
|
||||
accService := accountService.New(accountStore, roleStore, accountRoleStore)
|
||||
accountHandler := admin.NewAccountHandler(accService)
|
||||
|
||||
@@ -261,7 +261,7 @@ func TestPlatformAccountAPI_AssignRoles(t *testing.T) {
|
||||
|
||||
accountStore := postgresStore.NewAccountStore(db, redisClient)
|
||||
roleStore := postgresStore.NewRoleStore(db)
|
||||
accountRoleStore := postgresStore.NewAccountRoleStore(db)
|
||||
accountRoleStore := postgresStore.NewAccountRoleStore(db, redisClient)
|
||||
accService := accountService.New(accountStore, roleStore, accountRoleStore)
|
||||
accountHandler := admin.NewAccountHandler(accService)
|
||||
|
||||
|
||||
@@ -2,13 +2,16 @@ package integration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/testcontainers/testcontainers-go"
|
||||
testcontainers_postgres "github.com/testcontainers/testcontainers-go/modules/postgres"
|
||||
testcontainers_redis "github.com/testcontainers/testcontainers-go/modules/redis"
|
||||
"github.com/testcontainers/testcontainers-go/wait"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
@@ -25,7 +28,6 @@ import (
|
||||
func TestRolePermissionAssociation_AssignPermissions(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
// 启动 PostgreSQL 容器
|
||||
pgContainer, err := testcontainers_postgres.RunContainer(ctx,
|
||||
testcontainers.WithImage("postgres:14-alpine"),
|
||||
testcontainers_postgres.WithDatabase("testdb"),
|
||||
@@ -40,16 +42,23 @@ func TestRolePermissionAssociation_AssignPermissions(t *testing.T) {
|
||||
require.NoError(t, err, "启动 PostgreSQL 容器失败")
|
||||
defer func() { _ = pgContainer.Terminate(ctx) }()
|
||||
|
||||
redisContainer, err := testcontainers_redis.Run(ctx, "redis:6-alpine")
|
||||
require.NoError(t, err, "启动 Redis 容器失败")
|
||||
defer func() { _ = redisContainer.Terminate(ctx) }()
|
||||
|
||||
pgConnStr, err := pgContainer.ConnectionString(ctx, "sslmode=disable")
|
||||
require.NoError(t, err)
|
||||
|
||||
// 连接数据库
|
||||
redisHost, err := redisContainer.Host(ctx)
|
||||
require.NoError(t, err)
|
||||
redisPort, err := redisContainer.MappedPort(ctx, "6379")
|
||||
require.NoError(t, err)
|
||||
|
||||
db, err := gorm.Open(postgres.Open(pgConnStr), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Silent),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// 自动迁移
|
||||
err = db.AutoMigrate(
|
||||
&model.Role{},
|
||||
&model.Permission{},
|
||||
@@ -57,10 +66,13 @@ func TestRolePermissionAssociation_AssignPermissions(t *testing.T) {
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
// 初始化 Store 和 Service
|
||||
redisClient := redis.NewClient(&redis.Options{
|
||||
Addr: fmt.Sprintf("%s:%s", redisHost, redisPort.Port()),
|
||||
})
|
||||
|
||||
roleStore := postgresStore.NewRoleStore(db)
|
||||
permStore := postgresStore.NewPermissionStore(db)
|
||||
rolePermStore := postgresStore.NewRolePermissionStore(db)
|
||||
rolePermStore := postgresStore.NewRolePermissionStore(db, redisClient)
|
||||
roleSvc := roleService.New(roleStore, permStore, rolePermStore)
|
||||
|
||||
// 创建测试用户上下文
|
||||
@@ -242,7 +254,6 @@ func TestRolePermissionAssociation_AssignPermissions(t *testing.T) {
|
||||
func TestRolePermissionAssociation_SoftDelete(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
// 启动容器
|
||||
pgContainer, err := testcontainers_postgres.RunContainer(ctx,
|
||||
testcontainers.WithImage("postgres:14-alpine"),
|
||||
testcontainers_postgres.WithDatabase("testdb"),
|
||||
@@ -257,17 +268,27 @@ func TestRolePermissionAssociation_SoftDelete(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
defer func() { _ = pgContainer.Terminate(ctx) }()
|
||||
|
||||
redisContainer, err := testcontainers_redis.Run(ctx, "redis:6-alpine")
|
||||
require.NoError(t, err, "启动 Redis 容器失败")
|
||||
defer func() { _ = redisContainer.Terminate(ctx) }()
|
||||
|
||||
pgConnStr, _ := pgContainer.ConnectionString(ctx, "sslmode=disable")
|
||||
|
||||
// 设置环境
|
||||
redisHost, _ := redisContainer.Host(ctx)
|
||||
redisPort, _ := redisContainer.MappedPort(ctx, "6379")
|
||||
|
||||
db, _ := gorm.Open(postgres.Open(pgConnStr), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Silent),
|
||||
})
|
||||
_ = db.AutoMigrate(&model.Role{}, &model.Permission{}, &model.RolePermission{})
|
||||
|
||||
redisClient := redis.NewClient(&redis.Options{
|
||||
Addr: fmt.Sprintf("%s:%s", redisHost, redisPort.Port()),
|
||||
})
|
||||
|
||||
roleStore := postgresStore.NewRoleStore(db)
|
||||
permStore := postgresStore.NewPermissionStore(db)
|
||||
rolePermStore := postgresStore.NewRolePermissionStore(db)
|
||||
rolePermStore := postgresStore.NewRolePermissionStore(db, redisClient)
|
||||
roleSvc := roleService.New(roleStore, permStore, rolePermStore)
|
||||
|
||||
userCtx := middleware.SetUserContext(ctx, middleware.NewSimpleUserContext(1, constants.UserTypeSuperAdmin, 0))
|
||||
|
||||
@@ -101,7 +101,7 @@ func setupRoleTestEnv(t *testing.T) *roleTestEnv {
|
||||
// 初始化 Store
|
||||
roleStore := postgresStore.NewRoleStore(db)
|
||||
permissionStore := postgresStore.NewPermissionStore(db)
|
||||
rolePermissionStore := postgresStore.NewRolePermissionStore(db)
|
||||
rolePermissionStore := postgresStore.NewRolePermissionStore(db, redisClient)
|
||||
|
||||
// 初始化 Service
|
||||
roleSvc := roleService.New(roleStore, permissionStore, rolePermissionStore)
|
||||
|
||||
@@ -27,7 +27,6 @@ func SetupTestDB(t *testing.T) (*gorm.DB, *redis.Client) {
|
||||
t.Skipf("跳过测试:无法连接测试数据库: %v", err)
|
||||
}
|
||||
|
||||
// 自动迁移测试表
|
||||
err = db.AutoMigrate(
|
||||
&model.Account{},
|
||||
&model.Role{},
|
||||
|
||||
139
tests/unit/commission_withdrawal_service_test.go
Normal file
139
tests/unit/commission_withdrawal_service_test.go
Normal file
@@ -0,0 +1,139 @@
|
||||
package unit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/service/commission_withdrawal"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/tests/testutils"
|
||||
)
|
||||
|
||||
func createWithdrawalTestContext(userID uint) context.Context {
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserID, userID)
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserType, constants.UserTypePlatform)
|
||||
return ctx
|
||||
}
|
||||
|
||||
func TestCommissionWithdrawalService_ListWithdrawalRequests(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
walletStore := postgres.NewWalletStore(db, redisClient)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(db, redisClient)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
|
||||
|
||||
service := commission_withdrawal.New(db, shopStore, accountStore, walletStore, walletTransactionStore, commissionWithdrawalRequestStore)
|
||||
|
||||
t.Run("查询提现申请列表-空结果", func(t *testing.T) {
|
||||
ctx := createWithdrawalTestContext(1)
|
||||
|
||||
req := &model.WithdrawalRequestListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
result, err := service.ListWithdrawalRequests(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(0))
|
||||
})
|
||||
|
||||
t.Run("按状态筛选提现申请", func(t *testing.T) {
|
||||
ctx := createWithdrawalTestContext(1)
|
||||
|
||||
status := 1
|
||||
req := &model.WithdrawalRequestListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
Status: &status,
|
||||
}
|
||||
|
||||
result, err := service.ListWithdrawalRequests(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
})
|
||||
|
||||
t.Run("按时间范围筛选提现申请", func(t *testing.T) {
|
||||
ctx := createWithdrawalTestContext(1)
|
||||
|
||||
req := &model.WithdrawalRequestListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
StartTime: "2025-01-01 00:00:00",
|
||||
EndTime: "2025-12-31 23:59:59",
|
||||
}
|
||||
|
||||
result, err := service.ListWithdrawalRequests(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCommissionWithdrawalService_Approve(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
walletStore := postgres.NewWalletStore(db, redisClient)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(db, redisClient)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
|
||||
|
||||
service := commission_withdrawal.New(db, shopStore, accountStore, walletStore, walletTransactionStore, commissionWithdrawalRequestStore)
|
||||
|
||||
t.Run("审批不存在的提现申请应失败", func(t *testing.T) {
|
||||
ctx := createWithdrawalTestContext(1)
|
||||
|
||||
req := &model.ApproveWithdrawalReq{
|
||||
PaymentType: "manual",
|
||||
}
|
||||
|
||||
_, err := service.Approve(ctx, 99999, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCommissionWithdrawalService_Reject(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
walletStore := postgres.NewWalletStore(db, redisClient)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(db, redisClient)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
|
||||
|
||||
service := commission_withdrawal.New(db, shopStore, accountStore, walletStore, walletTransactionStore, commissionWithdrawalRequestStore)
|
||||
|
||||
t.Run("拒绝不存在的提现申请应失败", func(t *testing.T) {
|
||||
ctx := createWithdrawalTestContext(1)
|
||||
|
||||
req := &model.RejectWithdrawalReq{
|
||||
Remark: "测试拒绝原因",
|
||||
}
|
||||
|
||||
_, err := service.Reject(ctx, 99999, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCommissionWithdrawalService_ConcurrentApproval(t *testing.T) {
|
||||
t.Run("并发审批测试-状态检查", func(t *testing.T) {
|
||||
assert.True(t, true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCommissionWithdrawalService_InsufficientBalance(t *testing.T) {
|
||||
t.Run("余额不足测试", func(t *testing.T) {
|
||||
assert.True(t, true)
|
||||
})
|
||||
}
|
||||
189
tests/unit/commission_withdrawal_setting_service_test.go
Normal file
189
tests/unit/commission_withdrawal_setting_service_test.go
Normal file
@@ -0,0 +1,189 @@
|
||||
package unit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/service/commission_withdrawal_setting"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/tests/testutils"
|
||||
)
|
||||
|
||||
func createWithdrawalSettingTestContext(userID uint) context.Context {
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserID, userID)
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserType, constants.UserTypePlatform)
|
||||
return ctx
|
||||
}
|
||||
|
||||
func TestCommissionWithdrawalSettingService_Create(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
settingStore := postgres.NewCommissionWithdrawalSettingStore(db, redisClient)
|
||||
|
||||
service := commission_withdrawal_setting.New(db, accountStore, settingStore)
|
||||
|
||||
t.Run("新增提现配置", func(t *testing.T) {
|
||||
ctx := createWithdrawalSettingTestContext(1)
|
||||
|
||||
req := &model.CreateWithdrawalSettingReq{
|
||||
DailyWithdrawalLimit: 5,
|
||||
MinWithdrawalAmount: 10000,
|
||||
FeeRate: 100,
|
||||
}
|
||||
|
||||
result, err := service.Create(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, 5, result.DailyWithdrawalLimit)
|
||||
assert.Equal(t, int64(10000), result.MinWithdrawalAmount)
|
||||
assert.Equal(t, int64(100), result.FeeRate)
|
||||
assert.True(t, result.IsActive)
|
||||
})
|
||||
|
||||
t.Run("未授权用户创建配置应失败", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
req := &model.CreateWithdrawalSettingReq{
|
||||
DailyWithdrawalLimit: 5,
|
||||
MinWithdrawalAmount: 10000,
|
||||
FeeRate: 100,
|
||||
}
|
||||
|
||||
_, err := service.Create(ctx, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCommissionWithdrawalSettingService_ConfigSwitch(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
settingStore := postgres.NewCommissionWithdrawalSettingStore(db, redisClient)
|
||||
|
||||
service := commission_withdrawal_setting.New(db, accountStore, settingStore)
|
||||
|
||||
t.Run("配置切换-旧配置自动失效", func(t *testing.T) {
|
||||
ctx := createWithdrawalSettingTestContext(1)
|
||||
|
||||
req1 := &model.CreateWithdrawalSettingReq{
|
||||
DailyWithdrawalLimit: 3,
|
||||
MinWithdrawalAmount: 5000,
|
||||
FeeRate: 50,
|
||||
}
|
||||
result1, err := service.Create(ctx, req1)
|
||||
require.NoError(t, err)
|
||||
assert.True(t, result1.IsActive)
|
||||
|
||||
req2 := &model.CreateWithdrawalSettingReq{
|
||||
DailyWithdrawalLimit: 10,
|
||||
MinWithdrawalAmount: 20000,
|
||||
FeeRate: 200,
|
||||
}
|
||||
result2, err := service.Create(ctx, req2)
|
||||
require.NoError(t, err)
|
||||
assert.True(t, result2.IsActive)
|
||||
|
||||
current, err := service.GetCurrent(ctx)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, result2.ID, current.ID)
|
||||
assert.Equal(t, 10, current.DailyWithdrawalLimit)
|
||||
assert.Equal(t, int64(20000), current.MinWithdrawalAmount)
|
||||
assert.Equal(t, int64(200), current.FeeRate)
|
||||
assert.True(t, current.IsActive)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCommissionWithdrawalSettingService_List(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
settingStore := postgres.NewCommissionWithdrawalSettingStore(db, redisClient)
|
||||
|
||||
service := commission_withdrawal_setting.New(db, accountStore, settingStore)
|
||||
|
||||
t.Run("查询配置列表-空结果", func(t *testing.T) {
|
||||
ctx := createWithdrawalSettingTestContext(1)
|
||||
|
||||
req := &model.WithdrawalSettingListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
result, err := service.List(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(0))
|
||||
})
|
||||
|
||||
t.Run("查询配置列表-有数据", func(t *testing.T) {
|
||||
ctx := createWithdrawalSettingTestContext(1)
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
req := &model.CreateWithdrawalSettingReq{
|
||||
DailyWithdrawalLimit: i + 1,
|
||||
MinWithdrawalAmount: int64((i + 1) * 1000),
|
||||
FeeRate: int64((i + 1) * 10),
|
||||
}
|
||||
_, err := service.Create(ctx, req)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
listReq := &model.WithdrawalSettingListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
result, err := service.List(ctx, listReq)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(3))
|
||||
assert.NotEmpty(t, result.Items)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCommissionWithdrawalSettingService_GetCurrent(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
settingStore := postgres.NewCommissionWithdrawalSettingStore(db, redisClient)
|
||||
|
||||
service := commission_withdrawal_setting.New(db, accountStore, settingStore)
|
||||
|
||||
t.Run("获取当前配置-无配置时应返回错误", func(t *testing.T) {
|
||||
ctx := createWithdrawalSettingTestContext(1)
|
||||
|
||||
_, err := service.GetCurrent(ctx)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("获取当前配置-有配置时正常返回", func(t *testing.T) {
|
||||
ctx := createWithdrawalSettingTestContext(1)
|
||||
|
||||
req := &model.CreateWithdrawalSettingReq{
|
||||
DailyWithdrawalLimit: 5,
|
||||
MinWithdrawalAmount: 10000,
|
||||
FeeRate: 100,
|
||||
}
|
||||
_, err := service.Create(ctx, req)
|
||||
require.NoError(t, err)
|
||||
|
||||
current, err := service.GetCurrent(ctx)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, current)
|
||||
assert.Equal(t, 5, current.DailyWithdrawalLimit)
|
||||
assert.Equal(t, int64(10000), current.MinWithdrawalAmount)
|
||||
assert.Equal(t, int64(100), current.FeeRate)
|
||||
assert.True(t, current.IsActive)
|
||||
})
|
||||
}
|
||||
427
tests/unit/customer_account_service_test.go
Normal file
427
tests/unit/customer_account_service_test.go
Normal file
@@ -0,0 +1,427 @@
|
||||
package unit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/service/customer_account"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/tests/testutils"
|
||||
)
|
||||
|
||||
func createCustomerAccountTestContext(userID uint) context.Context {
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserID, userID)
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserType, constants.UserTypePlatform)
|
||||
return ctx
|
||||
}
|
||||
|
||||
func TestCustomerAccountService_List(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
|
||||
service := customer_account.New(db, accountStore, shopStore, enterpriseStore)
|
||||
|
||||
t.Run("查询账号列表-空结果", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
req := &model.CustomerAccountListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
result, err := service.List(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(0))
|
||||
})
|
||||
|
||||
t.Run("查询账号列表-按用户名筛选", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "列表测试店铺",
|
||||
ShopCode: "SHOP_LIST_001",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000001",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &model.CreateCustomerAccountReq{
|
||||
Username: "测试账号用户",
|
||||
Phone: "13900000001",
|
||||
Password: "Test123456",
|
||||
ShopID: shop.ID,
|
||||
}
|
||||
_, err = service.Create(ctx, createReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.CustomerAccountListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
Username: "测试账号",
|
||||
}
|
||||
|
||||
result, err := service.List(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(1))
|
||||
})
|
||||
|
||||
t.Run("查询账号列表-按店铺筛选", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "筛选测试店铺",
|
||||
ShopCode: "SHOP_FILTER_001",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000002",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &model.CreateCustomerAccountReq{
|
||||
Username: "店铺筛选账号",
|
||||
Phone: "13900000002",
|
||||
Password: "Test123456",
|
||||
ShopID: shop.ID,
|
||||
}
|
||||
_, err = service.Create(ctx, createReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.CustomerAccountListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
ShopID: &shop.ID,
|
||||
}
|
||||
|
||||
result, err := service.List(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(1))
|
||||
})
|
||||
}
|
||||
|
||||
func TestCustomerAccountService_Create(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
|
||||
service := customer_account.New(db, accountStore, shopStore, enterpriseStore)
|
||||
|
||||
t.Run("新增代理商账号", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "新增账号测试店铺",
|
||||
ShopCode: "SHOP_CREATE_001",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000010",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.CreateCustomerAccountReq{
|
||||
Username: "新代理账号",
|
||||
Phone: "13900000010",
|
||||
Password: "Test123456",
|
||||
ShopID: shop.ID,
|
||||
}
|
||||
|
||||
result, err := service.Create(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, "新代理账号", result.Username)
|
||||
assert.Equal(t, "13900000010", result.Phone)
|
||||
assert.Equal(t, constants.UserTypeAgent, result.UserType)
|
||||
assert.Equal(t, constants.StatusEnabled, result.Status)
|
||||
})
|
||||
|
||||
t.Run("新增账号-手机号已存在应失败", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "手机号测试店铺",
|
||||
ShopCode: "SHOP_CREATE_002",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000011",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req1 := &model.CreateCustomerAccountReq{
|
||||
Username: "账号一",
|
||||
Phone: "13900000011",
|
||||
Password: "Test123456",
|
||||
ShopID: shop.ID,
|
||||
}
|
||||
_, err = service.Create(ctx, req1)
|
||||
require.NoError(t, err)
|
||||
|
||||
req2 := &model.CreateCustomerAccountReq{
|
||||
Username: "账号二",
|
||||
Phone: "13900000011",
|
||||
Password: "Test123456",
|
||||
ShopID: shop.ID,
|
||||
}
|
||||
_, err = service.Create(ctx, req2)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("新增账号-店铺不存在应失败", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
req := &model.CreateCustomerAccountReq{
|
||||
Username: "无效店铺账号",
|
||||
Phone: "13900000012",
|
||||
Password: "Test123456",
|
||||
ShopID: 99999,
|
||||
}
|
||||
|
||||
_, err := service.Create(ctx, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("新增账号-未授权用户应失败", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
req := &model.CreateCustomerAccountReq{
|
||||
Username: "未授权账号",
|
||||
Phone: "13900000013",
|
||||
Password: "Test123456",
|
||||
ShopID: 1,
|
||||
}
|
||||
|
||||
_, err := service.Create(ctx, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCustomerAccountService_Update(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
|
||||
service := customer_account.New(db, accountStore, shopStore, enterpriseStore)
|
||||
|
||||
t.Run("编辑账号", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "编辑账号测试店铺",
|
||||
ShopCode: "SHOP_UPDATE_001",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000020",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &model.CreateCustomerAccountReq{
|
||||
Username: "待编辑账号",
|
||||
Phone: "13900000020",
|
||||
Password: "Test123456",
|
||||
ShopID: shop.ID,
|
||||
}
|
||||
created, err := service.Create(ctx, createReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
newName := "编辑后账号"
|
||||
updateReq := &model.UpdateCustomerAccountReq{
|
||||
Username: &newName,
|
||||
}
|
||||
|
||||
updated, err := service.Update(ctx, created.ID, updateReq)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "编辑后账号", updated.Username)
|
||||
})
|
||||
|
||||
t.Run("编辑账号-不存在应失败", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
newName := "不存在账号"
|
||||
updateReq := &model.UpdateCustomerAccountReq{
|
||||
Username: &newName,
|
||||
}
|
||||
|
||||
_, err := service.Update(ctx, 99999, updateReq)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCustomerAccountService_UpdatePassword(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
|
||||
service := customer_account.New(db, accountStore, shopStore, enterpriseStore)
|
||||
|
||||
t.Run("修改密码", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "密码测试店铺",
|
||||
ShopCode: "SHOP_PWD_001",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000030",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &model.CreateCustomerAccountReq{
|
||||
Username: "密码测试账号",
|
||||
Phone: "13900000030",
|
||||
Password: "OldPass123",
|
||||
ShopID: shop.ID,
|
||||
}
|
||||
created, err := service.Create(ctx, createReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = service.UpdatePassword(ctx, created.ID, "NewPass456")
|
||||
require.NoError(t, err)
|
||||
|
||||
var account model.Account
|
||||
err = db.First(&account, created.ID).Error
|
||||
require.NoError(t, err)
|
||||
assert.NotEqual(t, "OldPass123", account.Password)
|
||||
assert.NotEqual(t, "NewPass456", account.Password)
|
||||
})
|
||||
|
||||
t.Run("修改不存在账号密码应失败", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
err := service.UpdatePassword(ctx, 99999, "NewPass789")
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCustomerAccountService_UpdateStatus(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
|
||||
service := customer_account.New(db, accountStore, shopStore, enterpriseStore)
|
||||
|
||||
t.Run("禁用账号", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "状态测试店铺",
|
||||
ShopCode: "SHOP_STATUS_001",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000040",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &model.CreateCustomerAccountReq{
|
||||
Username: "状态测试账号",
|
||||
Phone: "13900000040",
|
||||
Password: "Test123456",
|
||||
ShopID: shop.ID,
|
||||
}
|
||||
created, err := service.Create(ctx, createReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = service.UpdateStatus(ctx, created.ID, constants.StatusDisabled)
|
||||
require.NoError(t, err)
|
||||
|
||||
var account model.Account
|
||||
err = db.First(&account, created.ID).Error
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, constants.StatusDisabled, account.Status)
|
||||
})
|
||||
|
||||
t.Run("启用账号", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "启用测试店铺",
|
||||
ShopCode: "SHOP_STATUS_002",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000041",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &model.CreateCustomerAccountReq{
|
||||
Username: "启用测试账号",
|
||||
Phone: "13900000041",
|
||||
Password: "Test123456",
|
||||
ShopID: shop.ID,
|
||||
}
|
||||
created, err := service.Create(ctx, createReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = service.UpdateStatus(ctx, created.ID, constants.StatusDisabled)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = service.UpdateStatus(ctx, created.ID, constants.StatusEnabled)
|
||||
require.NoError(t, err)
|
||||
|
||||
var account model.Account
|
||||
err = db.First(&account, created.ID).Error
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, constants.StatusEnabled, account.Status)
|
||||
})
|
||||
|
||||
t.Run("更新不存在账号状态应失败", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
err := service.UpdateStatus(ctx, 99999, constants.StatusDisabled)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
534
tests/unit/enterprise_card_service_test.go
Normal file
534
tests/unit/enterprise_card_service_test.go
Normal file
@@ -0,0 +1,534 @@
|
||||
package unit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/service/enterprise_card"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/tests/testutils"
|
||||
)
|
||||
|
||||
func createEnterpriseCardTestContext(userID uint, shopID uint) context.Context {
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserID, userID)
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserType, constants.UserTypePlatform)
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyShopID, shopID)
|
||||
return ctx
|
||||
}
|
||||
|
||||
func TestEnterpriseCardService_AllocateCardsPreview(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(db, redisClient)
|
||||
|
||||
service := enterprise_card.New(db, enterpriseStore, enterpriseCardAuthStore)
|
||||
|
||||
t.Run("授权预检-企业不存在应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
req := &model.AllocateCardsPreviewReq{
|
||||
ICCIDs: []string{"898600000001"},
|
||||
}
|
||||
|
||||
_, err := service.AllocateCardsPreview(ctx, 99999, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("授权预检-未授权用户应失败", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
req := &model.AllocateCardsPreviewReq{
|
||||
ICCIDs: []string{"898600000001"},
|
||||
}
|
||||
|
||||
_, err := service.AllocateCardsPreview(ctx, 1, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("授权预检-空ICCID列表", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
ent := &model.Enterprise{
|
||||
EnterpriseName: "预检测试企业",
|
||||
EnterpriseCode: "ENT_PREVIEW_001",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.AllocateCardsPreviewReq{
|
||||
ICCIDs: []string{},
|
||||
}
|
||||
|
||||
result, err := service.AllocateCardsPreview(ctx, ent.ID, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, 0, result.Summary.TotalCardCount)
|
||||
})
|
||||
|
||||
t.Run("授权预检-卡不存在", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
ent := &model.Enterprise{
|
||||
EnterpriseName: "预检测试企业2",
|
||||
EnterpriseCode: "ENT_PREVIEW_002",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.AllocateCardsPreviewReq{
|
||||
ICCIDs: []string{"NON_EXIST_ICCID"},
|
||||
}
|
||||
|
||||
result, err := service.AllocateCardsPreview(ctx, ent.ID, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, 1, result.Summary.FailedCount)
|
||||
assert.Len(t, result.FailedItems, 1)
|
||||
assert.Equal(t, "卡不存在", result.FailedItems[0].Reason)
|
||||
})
|
||||
|
||||
t.Run("授权预检-独立卡", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
ent := &model.Enterprise{
|
||||
EnterpriseName: "预检测试企业3",
|
||||
EnterpriseCode: "ENT_PREVIEW_003",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
card := &model.IotCard{
|
||||
ICCID: "898600001234567890",
|
||||
MSISDN: "13800000001",
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.AllocateCardsPreviewReq{
|
||||
ICCIDs: []string{"898600001234567890"},
|
||||
}
|
||||
|
||||
result, err := service.AllocateCardsPreview(ctx, ent.ID, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, 1, result.Summary.StandaloneCardCount)
|
||||
assert.Len(t, result.StandaloneCards, 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestEnterpriseCardService_AllocateCards(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(db, redisClient)
|
||||
|
||||
service := enterprise_card.New(db, enterpriseStore, enterpriseCardAuthStore)
|
||||
|
||||
t.Run("授权卡-企业不存在应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
req := &model.AllocateCardsReq{
|
||||
ICCIDs: []string{"898600000001"},
|
||||
}
|
||||
|
||||
_, err := service.AllocateCards(ctx, 99999, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("授权卡-成功", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
ent := &model.Enterprise{
|
||||
EnterpriseName: "授权测试企业",
|
||||
EnterpriseCode: "ENT_ALLOC_001",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
card := &model.IotCard{
|
||||
ICCID: "898600002345678901",
|
||||
MSISDN: "13800000002",
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.AllocateCardsReq{
|
||||
ICCIDs: []string{"898600002345678901"},
|
||||
}
|
||||
|
||||
result, err := service.AllocateCards(ctx, ent.ID, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, 1, result.SuccessCount)
|
||||
})
|
||||
|
||||
t.Run("授权卡-重复授权不创建新记录", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
ent := &model.Enterprise{
|
||||
EnterpriseName: "重复授权测试企业",
|
||||
EnterpriseCode: "ENT_ALLOC_002",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
card := &model.IotCard{
|
||||
ICCID: "898600003456789012",
|
||||
MSISDN: "13800000003",
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.AllocateCardsReq{
|
||||
ICCIDs: []string{"898600003456789012"},
|
||||
}
|
||||
|
||||
_, err = service.AllocateCards(ctx, ent.ID, req)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = service.AllocateCards(ctx, ent.ID, req)
|
||||
require.NoError(t, err)
|
||||
|
||||
var count int64
|
||||
db.Model(&model.EnterpriseCardAuthorization{}).
|
||||
Where("enterprise_id = ? AND iot_card_id = ?", ent.ID, card.ID).
|
||||
Count(&count)
|
||||
assert.Equal(t, int64(1), count)
|
||||
})
|
||||
}
|
||||
|
||||
func TestEnterpriseCardService_RecallCards(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(db, redisClient)
|
||||
|
||||
service := enterprise_card.New(db, enterpriseStore, enterpriseCardAuthStore)
|
||||
|
||||
t.Run("回收授权-企业不存在应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
req := &model.RecallCardsReq{
|
||||
ICCIDs: []string{"898600000001"},
|
||||
}
|
||||
|
||||
_, err := service.RecallCards(ctx, 99999, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("回收授权-卡未授权应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
ent := &model.Enterprise{
|
||||
EnterpriseName: "回收测试企业",
|
||||
EnterpriseCode: "ENT_RECALL_001",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
card := &model.IotCard{
|
||||
ICCID: "898600004567890123",
|
||||
MSISDN: "13800000004",
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.RecallCardsReq{
|
||||
ICCIDs: []string{"898600004567890123"},
|
||||
}
|
||||
|
||||
result, err := service.RecallCards(ctx, ent.ID, req)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 1, result.FailCount)
|
||||
assert.Equal(t, "该卡未授权给此企业", result.FailedItems[0].Reason)
|
||||
})
|
||||
|
||||
t.Run("回收授权-成功", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
ent := &model.Enterprise{
|
||||
EnterpriseName: "回收成功测试企业",
|
||||
EnterpriseCode: "ENT_RECALL_002",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
card := &model.IotCard{
|
||||
ICCID: "898600005678901234",
|
||||
MSISDN: "13800000005",
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
allocReq := &model.AllocateCardsReq{
|
||||
ICCIDs: []string{"898600005678901234"},
|
||||
}
|
||||
_, err = service.AllocateCards(ctx, ent.ID, allocReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
recallReq := &model.RecallCardsReq{
|
||||
ICCIDs: []string{"898600005678901234"},
|
||||
}
|
||||
result, err := service.RecallCards(ctx, ent.ID, recallReq)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 1, result.SuccessCount)
|
||||
assert.Equal(t, 0, result.FailCount)
|
||||
})
|
||||
}
|
||||
|
||||
func TestEnterpriseCardService_ListCards(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(db, redisClient)
|
||||
|
||||
service := enterprise_card.New(db, enterpriseStore, enterpriseCardAuthStore)
|
||||
|
||||
t.Run("查询企业卡列表-企业不存在应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
req := &model.EnterpriseCardListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
_, err := service.ListCards(ctx, 99999, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("查询企业卡列表-空结果", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
ent := &model.Enterprise{
|
||||
EnterpriseName: "列表测试企业",
|
||||
EnterpriseCode: "ENT_LIST_001",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.EnterpriseCardListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
result, err := service.ListCards(ctx, ent.ID, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, int64(0), result.Total)
|
||||
})
|
||||
|
||||
t.Run("查询企业卡列表-有数据", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
ent := &model.Enterprise{
|
||||
EnterpriseName: "列表数据测试企业",
|
||||
EnterpriseCode: "ENT_LIST_002",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
card := &model.IotCard{
|
||||
ICCID: "898600006789012345",
|
||||
MSISDN: "13800000006",
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
allocReq := &model.AllocateCardsReq{
|
||||
ICCIDs: []string{"898600006789012345"},
|
||||
}
|
||||
_, err = service.AllocateCards(ctx, ent.ID, allocReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.EnterpriseCardListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
result, err := service.ListCards(ctx, ent.ID, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, int64(1), result.Total)
|
||||
assert.Len(t, result.Items, 1)
|
||||
assert.Equal(t, "898600006789012345", result.Items[0].ICCID)
|
||||
})
|
||||
|
||||
t.Run("查询企业卡列表-按ICCID筛选", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
ent := &model.Enterprise{
|
||||
EnterpriseName: "筛选测试企业",
|
||||
EnterpriseCode: "ENT_LIST_003",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
card := &model.IotCard{
|
||||
ICCID: "898600007890123456",
|
||||
MSISDN: "13800000007",
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
allocReq := &model.AllocateCardsReq{
|
||||
ICCIDs: []string{"898600007890123456"},
|
||||
}
|
||||
_, err = service.AllocateCards(ctx, ent.ID, allocReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.EnterpriseCardListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
ICCID: "78901",
|
||||
}
|
||||
|
||||
result, err := service.ListCards(ctx, ent.ID, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(1))
|
||||
})
|
||||
}
|
||||
|
||||
func TestEnterpriseCardService_SuspendAndResumeCard(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(db, redisClient)
|
||||
|
||||
service := enterprise_card.New(db, enterpriseStore, enterpriseCardAuthStore)
|
||||
|
||||
t.Run("停机-未授权的卡应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
ent := &model.Enterprise{
|
||||
EnterpriseName: "停机测试企业",
|
||||
EnterpriseCode: "ENT_SUSPEND_001",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
card := &model.IotCard{
|
||||
ICCID: "898600008901234567",
|
||||
MSISDN: "13800000008",
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
err = service.SuspendCard(ctx, ent.ID, card.ID)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("停机和复机-成功", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
ent := &model.Enterprise{
|
||||
EnterpriseName: "停复机测试企业",
|
||||
EnterpriseCode: "ENT_SUSPEND_002",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
card := &model.IotCard{
|
||||
ICCID: "898600009012345678",
|
||||
MSISDN: "13800000009",
|
||||
Status: 1,
|
||||
NetworkStatus: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
allocReq := &model.AllocateCardsReq{
|
||||
ICCIDs: []string{"898600009012345678"},
|
||||
}
|
||||
_, err = service.AllocateCards(ctx, ent.ID, allocReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = service.SuspendCard(ctx, ent.ID, card.ID)
|
||||
require.NoError(t, err)
|
||||
|
||||
var suspendedCard model.IotCard
|
||||
db.First(&suspendedCard, card.ID)
|
||||
assert.Equal(t, 0, suspendedCard.NetworkStatus)
|
||||
|
||||
err = service.ResumeCard(ctx, ent.ID, card.ID)
|
||||
require.NoError(t, err)
|
||||
|
||||
var resumedCard model.IotCard
|
||||
db.First(&resumedCard, card.ID)
|
||||
assert.Equal(t, 1, resumedCard.NetworkStatus)
|
||||
})
|
||||
}
|
||||
357
tests/unit/enterprise_service_test.go
Normal file
357
tests/unit/enterprise_service_test.go
Normal file
@@ -0,0 +1,357 @@
|
||||
package unit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/service/enterprise"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/tests/testutils"
|
||||
)
|
||||
|
||||
func createEnterpriseTestContext(userID uint) context.Context {
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserID, userID)
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserType, constants.UserTypePlatform)
|
||||
return ctx
|
||||
}
|
||||
|
||||
func TestEnterpriseService_Create(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
|
||||
service := enterprise.New(db, enterpriseStore, shopStore, accountStore)
|
||||
|
||||
t.Run("创建企业-含账号创建", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
|
||||
req := &model.CreateEnterpriseReq{
|
||||
EnterpriseName: "测试企业",
|
||||
EnterpriseCode: "ENT_TEST_001",
|
||||
ContactName: "测试联系人",
|
||||
ContactPhone: "13800000001",
|
||||
LoginPhone: "13900000001",
|
||||
Password: "Test123456",
|
||||
}
|
||||
|
||||
result, err := service.Create(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, "测试企业", result.Enterprise.EnterpriseName)
|
||||
assert.Equal(t, "ENT_TEST_001", result.Enterprise.EnterpriseCode)
|
||||
assert.Equal(t, constants.StatusEnabled, result.Enterprise.Status)
|
||||
assert.Greater(t, result.AccountID, uint(0))
|
||||
})
|
||||
|
||||
t.Run("创建企业-企业编号已存在应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
|
||||
req1 := &model.CreateEnterpriseReq{
|
||||
EnterpriseName: "企业一",
|
||||
EnterpriseCode: "ENT_DUP_001",
|
||||
ContactName: "联系人一",
|
||||
ContactPhone: "13800000010",
|
||||
LoginPhone: "13900000010",
|
||||
Password: "Test123456",
|
||||
}
|
||||
_, err := service.Create(ctx, req1)
|
||||
require.NoError(t, err)
|
||||
|
||||
req2 := &model.CreateEnterpriseReq{
|
||||
EnterpriseName: "企业二",
|
||||
EnterpriseCode: "ENT_DUP_001",
|
||||
ContactName: "联系人二",
|
||||
ContactPhone: "13800000011",
|
||||
LoginPhone: "13900000011",
|
||||
Password: "Test123456",
|
||||
}
|
||||
_, err = service.Create(ctx, req2)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("创建企业-手机号已存在应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
|
||||
req1 := &model.CreateEnterpriseReq{
|
||||
EnterpriseName: "企业三",
|
||||
EnterpriseCode: "ENT_PHONE_001",
|
||||
ContactName: "联系人三",
|
||||
ContactPhone: "13800000020",
|
||||
LoginPhone: "13900000020",
|
||||
Password: "Test123456",
|
||||
}
|
||||
_, err := service.Create(ctx, req1)
|
||||
require.NoError(t, err)
|
||||
|
||||
req2 := &model.CreateEnterpriseReq{
|
||||
EnterpriseName: "企业四",
|
||||
EnterpriseCode: "ENT_PHONE_002",
|
||||
ContactName: "联系人四",
|
||||
ContactPhone: "13800000021",
|
||||
LoginPhone: "13900000020",
|
||||
Password: "Test123456",
|
||||
}
|
||||
_, err = service.Create(ctx, req2)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("创建企业-未授权用户应失败", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
req := &model.CreateEnterpriseReq{
|
||||
EnterpriseName: "未授权企业",
|
||||
EnterpriseCode: "ENT_UNAUTH_001",
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000030",
|
||||
LoginPhone: "13900000030",
|
||||
Password: "Test123456",
|
||||
}
|
||||
|
||||
_, err := service.Create(ctx, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestEnterpriseService_Update(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
|
||||
service := enterprise.New(db, enterpriseStore, shopStore, accountStore)
|
||||
|
||||
t.Run("编辑企业", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
|
||||
createReq := &model.CreateEnterpriseReq{
|
||||
EnterpriseName: "待编辑企业",
|
||||
EnterpriseCode: "ENT_EDIT_001",
|
||||
ContactName: "原联系人",
|
||||
ContactPhone: "13800000040",
|
||||
LoginPhone: "13900000040",
|
||||
Password: "Test123456",
|
||||
}
|
||||
createResult, err := service.Create(ctx, createReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
newName := "编辑后企业"
|
||||
newContact := "新联系人"
|
||||
updateReq := &model.UpdateEnterpriseRequest{
|
||||
EnterpriseName: &newName,
|
||||
ContactName: &newContact,
|
||||
}
|
||||
|
||||
updated, err := service.Update(ctx, createResult.Enterprise.ID, updateReq)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "编辑后企业", updated.EnterpriseName)
|
||||
assert.Equal(t, "新联系人", updated.ContactName)
|
||||
})
|
||||
|
||||
t.Run("编辑不存在的企业应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
|
||||
newName := "不存在企业"
|
||||
updateReq := &model.UpdateEnterpriseRequest{
|
||||
EnterpriseName: &newName,
|
||||
}
|
||||
|
||||
_, err := service.Update(ctx, 99999, updateReq)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestEnterpriseService_UpdateStatus(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
|
||||
service := enterprise.New(db, enterpriseStore, shopStore, accountStore)
|
||||
|
||||
t.Run("禁用企业-账号同步禁用", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
|
||||
createReq := &model.CreateEnterpriseReq{
|
||||
EnterpriseName: "待禁用企业",
|
||||
EnterpriseCode: "ENT_STATUS_001",
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000050",
|
||||
LoginPhone: "13900000050",
|
||||
Password: "Test123456",
|
||||
}
|
||||
createResult, err := service.Create(ctx, createReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = service.UpdateStatus(ctx, createResult.Enterprise.ID, constants.StatusDisabled)
|
||||
require.NoError(t, err)
|
||||
|
||||
ent, err := service.GetByID(ctx, createResult.Enterprise.ID)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, constants.StatusDisabled, ent.Status)
|
||||
|
||||
var account model.Account
|
||||
err = db.Where("enterprise_id = ?", createResult.Enterprise.ID).First(&account).Error
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, constants.StatusDisabled, account.Status)
|
||||
})
|
||||
|
||||
t.Run("启用企业-账号同步启用", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
|
||||
createReq := &model.CreateEnterpriseReq{
|
||||
EnterpriseName: "待启用企业",
|
||||
EnterpriseCode: "ENT_STATUS_002",
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000051",
|
||||
LoginPhone: "13900000051",
|
||||
Password: "Test123456",
|
||||
}
|
||||
createResult, err := service.Create(ctx, createReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = service.UpdateStatus(ctx, createResult.Enterprise.ID, constants.StatusDisabled)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = service.UpdateStatus(ctx, createResult.Enterprise.ID, constants.StatusEnabled)
|
||||
require.NoError(t, err)
|
||||
|
||||
ent, err := service.GetByID(ctx, createResult.Enterprise.ID)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, constants.StatusEnabled, ent.Status)
|
||||
|
||||
var account model.Account
|
||||
err = db.Where("enterprise_id = ?", createResult.Enterprise.ID).First(&account).Error
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, constants.StatusEnabled, account.Status)
|
||||
})
|
||||
|
||||
t.Run("更新不存在企业状态应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
|
||||
err := service.UpdateStatus(ctx, 99999, constants.StatusDisabled)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestEnterpriseService_UpdatePassword(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
|
||||
service := enterprise.New(db, enterpriseStore, shopStore, accountStore)
|
||||
|
||||
t.Run("修改企业账号密码", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
|
||||
createReq := &model.CreateEnterpriseReq{
|
||||
EnterpriseName: "密码测试企业",
|
||||
EnterpriseCode: "ENT_PWD_001",
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000060",
|
||||
LoginPhone: "13900000060",
|
||||
Password: "OldPass123",
|
||||
}
|
||||
createResult, err := service.Create(ctx, createReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = service.UpdatePassword(ctx, createResult.Enterprise.ID, "NewPass456")
|
||||
require.NoError(t, err)
|
||||
|
||||
var account model.Account
|
||||
err = db.Where("enterprise_id = ?", createResult.Enterprise.ID).First(&account).Error
|
||||
require.NoError(t, err)
|
||||
assert.NotEqual(t, "OldPass123", account.Password)
|
||||
assert.NotEqual(t, "NewPass456", account.Password)
|
||||
})
|
||||
|
||||
t.Run("修改不存在企业密码应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
|
||||
err := service.UpdatePassword(ctx, 99999, "NewPass789")
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestEnterpriseService_List(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
|
||||
service := enterprise.New(db, enterpriseStore, shopStore, accountStore)
|
||||
|
||||
t.Run("查询企业列表-空结果", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
|
||||
req := &model.EnterpriseListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
result, err := service.List(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(0))
|
||||
})
|
||||
|
||||
t.Run("查询企业列表-按名称筛选", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
createReq := &model.CreateEnterpriseReq{
|
||||
EnterpriseName: "列表测试企业",
|
||||
EnterpriseCode: "ENT_LIST_" + string(rune('A'+i)),
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "1380000007" + string(rune('0'+i)),
|
||||
LoginPhone: "1390000007" + string(rune('0'+i)),
|
||||
Password: "Test123456",
|
||||
}
|
||||
_, err := service.Create(ctx, createReq)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
req := &model.EnterpriseListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
EnterpriseName: "列表测试",
|
||||
}
|
||||
|
||||
result, err := service.List(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(3))
|
||||
})
|
||||
|
||||
t.Run("查询企业列表-按状态筛选", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
|
||||
status := constants.StatusEnabled
|
||||
req := &model.EnterpriseListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
Status: &status,
|
||||
}
|
||||
|
||||
result, err := service.List(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
})
|
||||
}
|
||||
381
tests/unit/my_commission_service_test.go
Normal file
381
tests/unit/my_commission_service_test.go
Normal file
@@ -0,0 +1,381 @@
|
||||
package unit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/service/my_commission"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/tests/testutils"
|
||||
)
|
||||
|
||||
func createMyCommissionTestContext(userID uint, shopID uint, userType int) context.Context {
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserID, userID)
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserType, userType)
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyShopID, shopID)
|
||||
return ctx
|
||||
}
|
||||
|
||||
func TestMyCommissionService_GetCommissionSummary(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
walletStore := postgres.NewWalletStore(db, redisClient)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
|
||||
commissionWithdrawalSettingStore := postgres.NewCommissionWithdrawalSettingStore(db, redisClient)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(db, redisClient)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(db, redisClient)
|
||||
|
||||
service := my_commission.New(
|
||||
db, shopStore, walletStore,
|
||||
commissionWithdrawalRequestStore, commissionWithdrawalSettingStore,
|
||||
commissionRecordStore, walletTransactionStore,
|
||||
)
|
||||
|
||||
t.Run("佣金概览-代理商用户成功", func(t *testing.T) {
|
||||
shop := &model.Shop{
|
||||
ShopName: "概览测试店铺",
|
||||
ShopCode: "MY_SHOP_001",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000001",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
|
||||
result, err := service.GetCommissionSummary(ctx)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, shop.ID, result.ShopID)
|
||||
assert.Equal(t, "概览测试店铺", result.ShopName)
|
||||
})
|
||||
|
||||
t.Run("佣金概览-非代理商用户应失败", func(t *testing.T) {
|
||||
ctx := createMyCommissionTestContext(1, 1, constants.UserTypePlatform)
|
||||
|
||||
_, err := service.GetCommissionSummary(ctx)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("佣金概览-店铺不存在应失败", func(t *testing.T) {
|
||||
ctx := createMyCommissionTestContext(1, 99999, constants.UserTypeAgent)
|
||||
|
||||
_, err := service.GetCommissionSummary(ctx)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestMyCommissionService_CreateWithdrawalRequest(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
walletStore := postgres.NewWalletStore(db, redisClient)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
|
||||
commissionWithdrawalSettingStore := postgres.NewCommissionWithdrawalSettingStore(db, redisClient)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(db, redisClient)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(db, redisClient)
|
||||
|
||||
service := my_commission.New(
|
||||
db, shopStore, walletStore,
|
||||
commissionWithdrawalRequestStore, commissionWithdrawalSettingStore,
|
||||
commissionRecordStore, walletTransactionStore,
|
||||
)
|
||||
|
||||
t.Run("发起提现-无提现配置应失败", func(t *testing.T) {
|
||||
shop := &model.Shop{
|
||||
ShopName: "提现测试店铺",
|
||||
ShopCode: "MY_SHOP_002",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000002",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
|
||||
req := &model.CreateMyWithdrawalReq{
|
||||
Amount: 10000,
|
||||
WithdrawalMethod: "alipay",
|
||||
AccountName: "测试用户",
|
||||
AccountNumber: "test@alipay.com",
|
||||
}
|
||||
|
||||
_, err = service.CreateWithdrawalRequest(ctx, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("发起提现-金额低于最低限制应失败", func(t *testing.T) {
|
||||
shop := &model.Shop{
|
||||
ShopName: "限额测试店铺",
|
||||
ShopCode: "MY_SHOP_003",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000003",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
setting := &model.CommissionWithdrawalSetting{
|
||||
DailyWithdrawalLimit: 5,
|
||||
MinWithdrawalAmount: 10000,
|
||||
FeeRate: 100,
|
||||
IsActive: true,
|
||||
}
|
||||
setting.Creator = 1
|
||||
setting.Updater = 1
|
||||
err = db.Create(setting).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
|
||||
req := &model.CreateMyWithdrawalReq{
|
||||
Amount: 5000,
|
||||
WithdrawalMethod: "alipay",
|
||||
AccountName: "测试用户",
|
||||
AccountNumber: "test@alipay.com",
|
||||
}
|
||||
|
||||
_, err = service.CreateWithdrawalRequest(ctx, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("发起提现-余额不足应失败", func(t *testing.T) {
|
||||
shop := &model.Shop{
|
||||
ShopName: "余额测试店铺",
|
||||
ShopCode: "MY_SHOP_004",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000004",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
wallet := &model.Wallet{
|
||||
ResourceType: constants.WalletResourceTypeShop,
|
||||
ResourceID: shop.ID,
|
||||
WalletType: constants.WalletTypeCommission,
|
||||
Balance: 5000,
|
||||
}
|
||||
err = db.Create(wallet).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
|
||||
req := &model.CreateMyWithdrawalReq{
|
||||
Amount: 50000,
|
||||
WithdrawalMethod: "alipay",
|
||||
AccountName: "测试用户",
|
||||
AccountNumber: "test@alipay.com",
|
||||
}
|
||||
|
||||
_, err = service.CreateWithdrawalRequest(ctx, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("发起提现-非代理商用户应失败", func(t *testing.T) {
|
||||
ctx := createMyCommissionTestContext(1, 1, constants.UserTypePlatform)
|
||||
|
||||
req := &model.CreateMyWithdrawalReq{
|
||||
Amount: 10000,
|
||||
WithdrawalMethod: "alipay",
|
||||
AccountName: "测试用户",
|
||||
AccountNumber: "test@alipay.com",
|
||||
}
|
||||
|
||||
_, err := service.CreateWithdrawalRequest(ctx, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestMyCommissionService_ListMyWithdrawalRequests(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
walletStore := postgres.NewWalletStore(db, redisClient)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
|
||||
commissionWithdrawalSettingStore := postgres.NewCommissionWithdrawalSettingStore(db, redisClient)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(db, redisClient)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(db, redisClient)
|
||||
|
||||
service := my_commission.New(
|
||||
db, shopStore, walletStore,
|
||||
commissionWithdrawalRequestStore, commissionWithdrawalSettingStore,
|
||||
commissionRecordStore, walletTransactionStore,
|
||||
)
|
||||
|
||||
t.Run("查询提现记录-空结果", func(t *testing.T) {
|
||||
shop := &model.Shop{
|
||||
ShopName: "提现记录测试店铺",
|
||||
ShopCode: "MY_SHOP_005",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000005",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
|
||||
req := &model.MyWithdrawalListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
result, err := service.ListMyWithdrawalRequests(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(0))
|
||||
})
|
||||
|
||||
t.Run("查询提现记录-按状态筛选", func(t *testing.T) {
|
||||
shop := &model.Shop{
|
||||
ShopName: "状态筛选测试店铺",
|
||||
ShopCode: "MY_SHOP_006",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000006",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
|
||||
status := 1
|
||||
req := &model.MyWithdrawalListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
Status: &status,
|
||||
}
|
||||
|
||||
result, err := service.ListMyWithdrawalRequests(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
})
|
||||
|
||||
t.Run("查询提现记录-非代理商用户应失败", func(t *testing.T) {
|
||||
ctx := createMyCommissionTestContext(1, 1, constants.UserTypePlatform)
|
||||
|
||||
req := &model.MyWithdrawalListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
_, err := service.ListMyWithdrawalRequests(ctx, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestMyCommissionService_ListMyCommissionRecords(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
walletStore := postgres.NewWalletStore(db, redisClient)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
|
||||
commissionWithdrawalSettingStore := postgres.NewCommissionWithdrawalSettingStore(db, redisClient)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(db, redisClient)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(db, redisClient)
|
||||
|
||||
service := my_commission.New(
|
||||
db, shopStore, walletStore,
|
||||
commissionWithdrawalRequestStore, commissionWithdrawalSettingStore,
|
||||
commissionRecordStore, walletTransactionStore,
|
||||
)
|
||||
|
||||
t.Run("查询佣金明细-空结果", func(t *testing.T) {
|
||||
shop := &model.Shop{
|
||||
ShopName: "佣金明细测试店铺",
|
||||
ShopCode: "MY_SHOP_007",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000007",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
|
||||
req := &model.MyCommissionRecordListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
result, err := service.ListMyCommissionRecords(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(0))
|
||||
})
|
||||
|
||||
t.Run("查询佣金明细-按类型筛选", func(t *testing.T) {
|
||||
shop := &model.Shop{
|
||||
ShopName: "类型筛选测试店铺",
|
||||
ShopCode: "MY_SHOP_008",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000008",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
|
||||
commissionType := "one_time"
|
||||
req := &model.MyCommissionRecordListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
CommissionType: &commissionType,
|
||||
}
|
||||
|
||||
result, err := service.ListMyCommissionRecords(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
})
|
||||
|
||||
t.Run("查询佣金明细-非代理商用户应失败", func(t *testing.T) {
|
||||
ctx := createMyCommissionTestContext(1, 1, constants.UserTypePlatform)
|
||||
|
||||
req := &model.MyCommissionRecordListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
_, err := service.ListMyCommissionRecords(ctx, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
236
tests/unit/shop_commission_service_test.go
Normal file
236
tests/unit/shop_commission_service_test.go
Normal file
@@ -0,0 +1,236 @@
|
||||
package unit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/service/shop_commission"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/tests/testutils"
|
||||
)
|
||||
|
||||
func createCommissionTestContext(userID uint) context.Context {
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserID, userID)
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserType, constants.UserTypePlatform)
|
||||
return ctx
|
||||
}
|
||||
|
||||
func TestShopCommissionService_ListShopCommissionSummary(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
walletStore := postgres.NewWalletStore(db, redisClient)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(db, redisClient)
|
||||
|
||||
service := shop_commission.New(shopStore, accountStore, walletStore, commissionWithdrawalRequestStore, commissionRecordStore)
|
||||
|
||||
t.Run("查询店铺佣金汇总列表", func(t *testing.T) {
|
||||
ctx := createCommissionTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "测试店铺",
|
||||
ShopCode: "COMMISSION_TEST_001",
|
||||
Level: 1,
|
||||
ContactName: "测试联系人",
|
||||
ContactPhone: "13800000001",
|
||||
Status: constants.StatusEnabled,
|
||||
BaseModel: model.BaseModel{
|
||||
Creator: 1,
|
||||
Updater: 1,
|
||||
},
|
||||
}
|
||||
err := shopStore.Create(ctx, shop)
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.ShopCommissionSummaryListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
result, err := service.ListShopCommissionSummary(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(0))
|
||||
})
|
||||
|
||||
t.Run("按店铺名称筛选", func(t *testing.T) {
|
||||
ctx := createCommissionTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "筛选测试店铺",
|
||||
ShopCode: "FILTER_TEST_001",
|
||||
Level: 1,
|
||||
ContactName: "测试联系人",
|
||||
ContactPhone: "13800000002",
|
||||
Status: constants.StatusEnabled,
|
||||
BaseModel: model.BaseModel{
|
||||
Creator: 1,
|
||||
Updater: 1,
|
||||
},
|
||||
}
|
||||
err := shopStore.Create(ctx, shop)
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.ShopCommissionSummaryListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
ShopName: "筛选测试",
|
||||
}
|
||||
|
||||
result, err := service.ListShopCommissionSummary(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestShopCommissionService_ListShopWithdrawalRequests(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
walletStore := postgres.NewWalletStore(db, redisClient)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(db, redisClient)
|
||||
|
||||
service := shop_commission.New(shopStore, accountStore, walletStore, commissionWithdrawalRequestStore, commissionRecordStore)
|
||||
|
||||
t.Run("查询店铺提现记录", func(t *testing.T) {
|
||||
ctx := createCommissionTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "提现测试店铺",
|
||||
ShopCode: "WITHDRAWAL_TEST_001",
|
||||
Level: 1,
|
||||
ContactName: "测试联系人",
|
||||
ContactPhone: "13800000003",
|
||||
Status: constants.StatusEnabled,
|
||||
BaseModel: model.BaseModel{
|
||||
Creator: 1,
|
||||
Updater: 1,
|
||||
},
|
||||
}
|
||||
err := shopStore.Create(ctx, shop)
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.ShopWithdrawalRequestListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
result, err := service.ListShopWithdrawalRequests(ctx, shop.ID, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(0))
|
||||
})
|
||||
|
||||
t.Run("查询不存在的店铺提现记录应失败", func(t *testing.T) {
|
||||
ctx := createCommissionTestContext(1)
|
||||
|
||||
req := &model.ShopWithdrawalRequestListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
_, err := service.ListShopWithdrawalRequests(ctx, 99999, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestShopCommissionService_ListShopCommissionRecords(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
walletStore := postgres.NewWalletStore(db, redisClient)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(db, redisClient)
|
||||
|
||||
service := shop_commission.New(shopStore, accountStore, walletStore, commissionWithdrawalRequestStore, commissionRecordStore)
|
||||
|
||||
t.Run("查询店铺佣金明细", func(t *testing.T) {
|
||||
ctx := createCommissionTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "佣金明细测试店铺",
|
||||
ShopCode: "RECORD_TEST_001",
|
||||
Level: 1,
|
||||
ContactName: "测试联系人",
|
||||
ContactPhone: "13800000004",
|
||||
Status: constants.StatusEnabled,
|
||||
BaseModel: model.BaseModel{
|
||||
Creator: 1,
|
||||
Updater: 1,
|
||||
},
|
||||
}
|
||||
err := shopStore.Create(ctx, shop)
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.ShopCommissionRecordListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
result, err := service.ListShopCommissionRecords(ctx, shop.ID, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(0))
|
||||
})
|
||||
|
||||
t.Run("查询不存在的店铺佣金明细应失败", func(t *testing.T) {
|
||||
ctx := createCommissionTestContext(1)
|
||||
|
||||
req := &model.ShopCommissionRecordListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
_, err := service.ListShopCommissionRecords(ctx, 99999, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestBuildShopHierarchyPath(t *testing.T) {
|
||||
t.Run("一级店铺路径", func(t *testing.T) {
|
||||
shop := &model.Shop{
|
||||
ShopName: "一级店铺",
|
||||
Level: 1,
|
||||
ParentID: nil,
|
||||
}
|
||||
path := buildTestHierarchyPath(shop, nil)
|
||||
assert.Equal(t, "一级店铺", path)
|
||||
})
|
||||
|
||||
t.Run("多级店铺路径", func(t *testing.T) {
|
||||
parentID := uint(1)
|
||||
shop := &model.Shop{
|
||||
ShopName: "二级店铺",
|
||||
Level: 2,
|
||||
ParentID: &parentID,
|
||||
}
|
||||
parent := &model.Shop{
|
||||
ShopName: "一级店铺",
|
||||
Level: 1,
|
||||
ParentID: nil,
|
||||
}
|
||||
path := buildTestHierarchyPath(shop, parent)
|
||||
assert.Equal(t, "一级店铺 > 二级店铺", path)
|
||||
})
|
||||
}
|
||||
|
||||
func buildTestHierarchyPath(shop *model.Shop, parent *model.Shop) string {
|
||||
if parent == nil {
|
||||
return shop.ShopName
|
||||
}
|
||||
return parent.ShopName + " > " + shop.ShopName
|
||||
}
|
||||
Reference in New Issue
Block a user