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:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user