telco_server/app/admin/models/bus_models/m_cooperative_manage.go

343 lines
17 KiB
Go
Raw Normal View History

2025-03-18 11:45:22 +00:00
package bus_models
import (
"errors"
"fmt"
"go-admin/app/admin/models"
"gorm.io/gorm"
)
2025-03-18 11:45:22 +00:00
type BusCooperative struct {
models.Model
2025-03-20 10:51:02 +00:00
CooperativeNumber string `gorm:"type:varchar(32);not null" json:"cooperative_number"` // 合作商编号
CooperativeName string `gorm:"type:varchar(64);not null" json:"cooperative_name"` // 合作商名称
Contact string `gorm:"type:varchar(32);not null" json:"contact"` // 联系人
Tel string `gorm:"type:varchar(20);not null" json:"tel"` // 手机号
Status uint8 `gorm:"type:tinyint(1);not null;default:1" json:"status"` // 账户状态1启用 2禁用
Account string `gorm:"type:varchar(32);not null;unique" json:"account"` // 账户
Password string `gorm:"type:varchar(64);not null" json:"password"` // 密码(建议存储加密哈希值)
Balance float64 `gorm:"type:decimal(10,3);not null;default:0.000" json:"balance"` // 账户余额
Free float64 `gorm:"type:decimal(10,2);not null;default:0.00" json:"free"` // 赠送余额
Bond float64 `gorm:"type:decimal(10,2);not null;default:0.00" json:"bond"` // 保证金
CardHolder string `gorm:"type:varchar(32)" json:"card_holder,omitempty"` // 开户人
Bank string `gorm:"type:varchar(64)" json:"bank,omitempty"` // 开户行
CardID string `gorm:"type:varchar(32)" json:"card_id,omitempty"` // 银行帐号
TaxID string `gorm:"type:varchar(32)" json:"tax_id,omitempty"` // 税号
UserId int `gorm:"type:int;not null" json:"userId"` // 用户ID重置密码时需要
Products []ProductDetail `json:"products" gorm:"-"` // 产品信息
2025-03-18 11:45:22 +00:00
}
// BusCooperativeProduct 合作商与产品关联表
type BusCooperativeProduct struct {
models.Model
CooperativeID uint64 `gorm:"not null" json:"cooperative_id"` // 合作商编号
ProductID uint64 `gorm:"not null" json:"product_id"` // 产品ID
Discount float64 `gorm:"type:decimal(5,2);not null;default:0.00" json:"discount"` // 折扣0-1
}
// BusCooperativeConsumptionLog 合作商消耗日志表
type BusCooperativeConsumptionLog struct {
models.Model
CooperativeID uint64 `gorm:"not null;index" json:"cooperative_id"` // 合作商ID
ConsumptionType uint8 `gorm:"type:tinyint(1);not null" json:"consumption_type"` // 消耗类型1-短信发送2-话费充值3-流量充值4-其他)
BusinessID uint64 `gorm:"default:0" json:"business_id"` // 关联业务ID
Amount float64 `gorm:"type:decimal(10,3);not null" json:"amount"` // 总消耗金额(正式余额+赠送余额)
UseBalanceAmount float64 `gorm:"type:decimal(10,3);not null;default:0.000" json:"use_balance_amount"` // 本次消耗的正式余额
UseFreeAmount float64 `gorm:"type:decimal(10,3);not null;default:0.000" json:"use_free_amount"` // 本次消耗的赠送余额
BalanceBefore float64 `gorm:"type:decimal(10,3);not null" json:"balance_before"` // 消耗前 正式余额
FreeBefore float64 `gorm:"type:decimal(10,3);not null" json:"free_before"` // 消耗前 赠送余额
BalanceAfter float64 `gorm:"type:decimal(10,3);not null" json:"balance_after"` // 消耗后 正式余额
FreeAfter float64 `gorm:"type:decimal(10,3);not null" json:"free_after"` // 消耗后 赠送余额
SourceDesc string `gorm:"type:varchar(255)" json:"source_desc,omitempty"` // 消耗来源描述
Remark string `gorm:"type:varchar(255)" json:"remark,omitempty"` // 备注
}
2025-03-18 11:45:22 +00:00
// CooperativeListReq 查询合作商列表请求
type CooperativeListReq struct {
2025-03-20 10:51:02 +00:00
CooperativeNumber string `json:"cooperative_number,omitempty"` // 合作商编号(可选)
CooperativeName string `json:"cooperative_name,omitempty"` // 合作商名称(支持模糊查询)
Status uint8 `json:"status,omitempty"` // 账户状态1启用 2禁用
Page int `json:"page" binding:"required,min=1"` // 页码
PageSize int `json:"page_size" binding:"required,min=1,max=100"` // 每页条数
2025-03-18 11:45:22 +00:00
}
// CooperativeListResp 查询合作商列表响应
type CooperativeListResp struct {
List []BusCooperative `json:"list"` // 合作商列表
Total int `json:"total"` // 总记录数
Page int `json:"page"` // 页码
PageSize int `json:"page_size"` // 每页大小
}
// CreateCooperativeReq 创建合作商请求
type CreateCooperativeReq struct {
2025-03-20 10:51:02 +00:00
CooperativeName string `json:"cooperative_name" binding:"required"` // 合作商名称
2025-03-21 08:40:10 +00:00
Contact string `json:"contact,omitempty"` // 联系人
Tel string `json:"tel,omitempty"` // 手机号
2025-03-20 10:51:02 +00:00
Account string `json:"account" binding:"required"` // 账户
Password string `json:"password" binding:"required"` // 密码(前端传输时应加密)
Balance float64 `json:"balance,omitempty"` // 账户余额(可选)
Free float64 `json:"free,omitempty"` // 赠送余额(可选)
Bond float64 `json:"bond,omitempty"` // 保证金(可选)
CardHolder string `json:"card_holder,omitempty"` // 开户人(可选)
Bank string `json:"bank,omitempty"` // 开户行(可选)
CardID string `json:"card_id,omitempty"` // 银行帐号(可选)
TaxID string `json:"tax_id,omitempty"` // 税号(可选)
Products []ProductDetail `json:"products"` // 关联产品及折扣信息
2025-03-18 11:45:22 +00:00
}
// ProductDiscount 结构体用于存储产品ID及对应折扣
type ProductDiscount struct {
ProductID uint64 `json:"product_id" binding:"required"` // 产品ID
Discount float64 `json:"discount" binding:"required"` // 折扣0-1之间
}
// CreateCooperativeResp 创建合作商响应
type CreateCooperativeResp struct {
CooperativeNumber string `form:"cooperative_number"` // 合作商编号
}
// EditCooperativeReq 编辑合作商请求
type EditCooperativeReq struct {
2025-03-20 10:51:02 +00:00
CooperativeNumber string `json:"cooperative_number,required"` // 合作商编号
CooperativeName string `json:"cooperative_name,omitempty"` // 合作商名称(可选)
Contact string `json:"contact,omitempty"` // 联系人(可选)
Tel string `json:"tel,omitempty"` // 手机号(可选)
Status uint8 `json:"status,omitempty"` // 账户状态(可选)
Account string `json:"account,omitempty"` // 账户(可选)
Password string `json:"password,omitempty"` // 密码(可选,前端加密传输)
Balance float64 `json:"balance,omitempty"` // 账户余额(可选)
Free float64 `json:"free,omitempty"` // 赠送余额(可选)
Bond float64 `json:"bond,omitempty"` // 保证金(可选)
CardHolder string `json:"card_holder,omitempty"` // 开户人(可选)
Bank string `json:"bank,omitempty"` // 开户行(可选)
CardID string `json:"card_id,omitempty"` // 银行帐号(可选)
TaxID string `json:"tax_id,omitempty"` // 税号(可选)
Products []ProductDetail `json:"products"` // 关联产品及折扣信息
2025-03-18 11:45:22 +00:00
}
// DeleteCooperativeReq 删除合作商请求
type DeleteCooperativeReq struct {
2025-03-20 10:51:02 +00:00
CooperativeNumber string `json:"cooperative_number,required"` // 合作商编号
2025-03-18 11:45:22 +00:00
}
// CooperativeDetailReq 查询合作商详情请求参数
type CooperativeDetailReq struct {
2025-03-20 10:51:02 +00:00
CooperativeNumber string `json:"cooperative_number" binding:"required"` // 合作商编号
2025-03-18 11:45:22 +00:00
}
// CooperativeDetailResp 查询合作商详情响应参数
type CooperativeDetailResp struct {
CooperativeNumber string `json:"cooperative_number"` // 合作商编号
CooperativeName string `json:"cooperative_name"` // 合作商名称
Contact string `json:"contact"` // 联系人
Tel string `json:"tel"` // 手机号
2025-03-20 10:51:02 +00:00
Status uint8 `json:"status"` // 账户状态1启用 2禁用
2025-03-18 11:45:22 +00:00
Account string `json:"account"` // 账户
2025-03-20 10:51:02 +00:00
Password string `json:"password"` // 密码(建议存储加密哈希值)
2025-03-18 11:45:22 +00:00
Balance float64 `json:"balance"` // 账户余额
Free float64 `json:"free"` // 赠送余额
Bond float64 `json:"bond"` // 保证金
2025-03-20 10:51:02 +00:00
CardHolder string `json:"card_holder"` // 开户人
Bank string `json:"bank"` // 开户行
CardID string `json:"card_id"` // 银行帐号
TaxID string `json:"tax_id"` // 税号
2025-03-18 11:45:22 +00:00
Products []ProductDetail `json:"products"` // 产品信息
}
// ProductDetail 产品信息
type ProductDetail struct {
2025-03-20 10:51:02 +00:00
ProductID uint64 `json:"product_id"` // 产品ID
2025-03-18 11:45:22 +00:00
ProductCode string `json:"product_code"` // 产品编码
ProductName string `json:"product_name"` // 产品名称
2025-03-20 10:51:02 +00:00
Discount float64 `json:"discount"` // 折扣0-1给到合作商的折扣不是产品本身的折扣
}
// AdjustAccountReq 账户调整入参
type AdjustAccountReq struct {
TransactionType uint8 `json:"transaction_type" binding:"required"` // 交易类型1-加款, 2-减款)
CooperativeNumber string `json:"cooperative_number" binding:"required"` // 合作商编号
2025-03-21 08:40:10 +00:00
Amount float64 `json:"amount,omitempty"` // 调整金额
GiftAmount float64 `json:"gift_amount,omitempty"` // 赠送金额
2025-03-20 10:51:02 +00:00
SourceFundingType uint8 `json:"source_funding_type"` // 资金来源,例如 "对公转账"、"支付宝"、"微信"、"现金"等(查询字典值)
Remark string `json:"remark,omitempty"` // 备注信息,例如交易编号、支付凭证等
}
// SetCooperativeStatusReq 设置合作商状态
type SetCooperativeStatusReq struct {
CooperativeNumber string `json:"cooperative_number" binding:"required"` // 合作商编号
Status uint8 `json:"status" binding:"required,oneof=1 2"` // 状态 (1 启用, 2 禁用)
2025-03-18 11:45:22 +00:00
}
2025-03-21 08:40:10 +00:00
// QueryCooperativeProductsReq 请求参数
type QueryCooperativeProductsReq struct {
CooperativeNumber string `json:"cooperative_number" binding:"required"` // 合作商编号
Page int `json:"page"` // 页码
PageSize int `json:"page_size"` // 每页条数
}
// QueryCooperativeProductsResp 响应参数
type QueryCooperativeProductsResp struct {
List []ProductInfo `json:"list"` // 产品信息列表
Total int `json:"total"` // 总条数
Page int `json:"page"` // 当前页码
PageSize int `json:"page_size"` // 每页条数
}
// ProductInfo 产品详情
type ProductInfo struct {
ProductID uint64 `json:"product_id"` // 产品ID
ProductCode string `json:"product_code"`
ProductName string `json:"product_name"`
ProductType uint8 `json:"product_type"`
Type uint8 `json:"type"`
Size string `json:"size"`
Price float64 `json:"price"`
Discount float64 `json:"discount"`
RetailPrice float64 `json:"retail_price"`
Description string `json:"description"`
Province string `json:"province"`
City string `json:"city"`
Platform uint8 `json:"platform"`
}
// UpdateProductDiscountReq 批量更新折扣请求
type UpdateProductDiscountReq struct {
CooperativeNumber string `json:"cooperative_number" binding:"required"` // 合作商编号
Products []ProductDiscount `json:"products" binding:"required,dive"`
}
// GetCooperativeInfoByAccount 根据账号查询合作商编号和名称
func GetCooperativeInfoByAccount(db *gorm.DB, account string) (cooperativeNumber string, cooperativeName string, err error) {
if account == "" {
err = errors.New("account 不能为空")
return
}
if account == "admin" {
return "admin", "系统管理员", nil
}
var coop BusCooperative
err = db.Model(&BusCooperative{}).Select("cooperative_number", "cooperative_name").
Where("account = ?", account).
First(&coop).Error
if err != nil {
return "", "", err
}
return coop.CooperativeNumber, coop.CooperativeName, nil
}
// TransactionListReq 查询充值日志请求结构体
type TransactionListReq struct {
CooperativeID uint64 `json:"cooperative_id"` // 合作商ID
TransactionType uint8 `json:"transaction_type"` // 交易类型1-加款, 2-减款)
SourceFundingType uint8 `json:"source_funding_type"` // 资金来源类型
Page int `json:"page"`
PageSize int `json:"page_size"`
}
// TransactionListResp 查询充值日志响应结构体
type TransactionListResp struct {
List []BusCooperativeTransaction `json:"list"`
Total int64 `json:"total"`
Page int `json:"page"`
PageSize int `json:"page_size"`
TotalPage int64 `json:"total_page"`
}
// RecordCooperativeConsumptionLog 记录合作商消耗日志
func RecordCooperativeConsumptionLog(db *gorm.DB, account string, consumptionType uint8, businessID uint64,
amount float64, sourceDesc string, remark string) error {
// 参数校验
if account == "" {
return errors.New("account不能为空")
}
if amount <= 0 {
return errors.New("amount必须大于0")
}
// 查询合作商信息
var coop BusCooperative
if err := db.Model(&BusCooperative{}).Where("account = ?", account).First(&coop).Error; err != nil {
return fmt.Errorf("查询合作商失败: %w", err)
}
// 记录交易前余额
balanceBefore := coop.Balance
freeBefore := coop.Free
// 初始化使用金额
var useBalanceAmount float64
var useFreeAmount float64
// 计算优先消耗逻辑:先扣正式余额,再扣赠送余额
if balanceBefore >= amount {
useBalanceAmount = amount
useFreeAmount = 0
} else {
useBalanceAmount = balanceBefore
useFreeAmount = amount - balanceBefore
}
// 计算交易后余额
balanceAfter := balanceBefore - useBalanceAmount
if balanceAfter < 0 {
balanceAfter = 0
}
freeAfter := freeBefore - useFreeAmount
if freeAfter < 0 {
freeAfter = 0
}
// 保存消耗日志
log := BusCooperativeConsumptionLog{
CooperativeID: coop.ID,
ConsumptionType: consumptionType,
BusinessID: businessID,
Amount: amount,
UseBalanceAmount: useBalanceAmount,
UseFreeAmount: useFreeAmount,
BalanceBefore: balanceBefore,
FreeBefore: freeBefore,
BalanceAfter: balanceAfter,
FreeAfter: freeAfter,
SourceDesc: sourceDesc,
Remark: remark,
}
if err := db.Create(&log).Error; err != nil {
return fmt.Errorf("记录消耗日志失败: %w", err)
}
// 更新合作商账户余额
updateFields := map[string]interface{}{
"balance": balanceAfter,
"free": freeAfter,
}
if err := db.Model(&BusCooperative{}).Where("id = ?", coop.ID).Updates(updateFields).Error; err != nil {
return fmt.Errorf("更新合作商余额失败: %w", err)
}
return nil
}
// ConsumptionLogListReq 查询消耗日志请求结构体
type ConsumptionLogListReq struct {
CooperativeID uint64 `json:"cooperative_id"` // 合作商ID
ConsumptionType uint8 `json:"consumption_type"` // 消耗类型1-短信发送2-话费充值3-流量充值4-其他)
Page int `json:"page"`
PageSize int `json:"page_size"`
}
// ConsumptionLogListResp 查询消耗日志响应结构体
type ConsumptionLogListResp struct {
List []BusCooperativeConsumptionLog `json:"list"`
Total int64 `json:"total"`
Page int `json:"page"`
PageSize int `json:"page_size"`
}