1.优化合作商相关接口;

This commit is contained in:
chenlin 2025-03-20 18:51:02 +08:00
parent df7076fc11
commit 7cfbf8fb46
11 changed files with 847 additions and 227 deletions

View File

@ -78,6 +78,10 @@ func (e CooperativeApi) CreateCooperative(c *gin.Context) {
app.Error(c, http.StatusInternalServerError, err, err.Error())
return
}
if resp.CooperativeNumber == "" {
app.Error(c, http.StatusInternalServerError, err, "新增失败")
return
}
// 返回创建结果
app.OK(c, resp, "新增成功")
@ -188,3 +192,67 @@ func (e CooperativeApi) CooperativeDetail(c *gin.Context) {
app.OK(c, resp, "查询成功")
return
}
// AdjustAccountHandler 账户调整接口
// @Summary 账户调整(加款/减款)
// @Tags 合作商管理-V1.0.0
// @Accept json
// @Produce json
// @Param request body bus_models.AdjustAccountReq true "账户调整请求"
// @Success 200 {object} app.Response
// @Router /api/v1/cooperative/adjust_account [post]
func (e CooperativeApi) AdjustAccountHandler(c *gin.Context) {
s := bus_service.CooperativeService{}
var req bus_models.AdjustAccountReq
err := e.MakeContext(c).
MakeOrm().
Bind(&req, binding.JSON).
MakeService(&s.Service).
Errors
if err != nil {
e.Logger.Error(err)
e.Error(500, err, err.Error())
return
}
// 调用服务层方法
if err := s.AdjustAccount(c.Request.Context(), req); err != nil {
app.Error(c, http.StatusInternalServerError, err, err.Error())
return
}
app.OK(c, nil, "账户调整成功")
}
// SetCooperativeStatus 设置合作商状态
// @Summary 设置合作商状态
// @Tags 合作商管理-V1.0.0
// @Produce json
// @Accept json
// @Param request body bus_models.SetCooperativeStatusReq true "设置合作商状态"
// @Success 200 {object} app.Response
// @Router /api/v1/cooperative/status [post]
func (e CooperativeApi) SetCooperativeStatus(c *gin.Context) {
s := bus_service.CooperativeService{}
var req bus_models.SetCooperativeStatusReq
if err := c.ShouldBindJSON(&req); err != nil {
app.Error(c, http.StatusBadRequest, err, "参数错误")
return
}
err := e.MakeContext(c).MakeOrm().MakeService(&s.Service).Errors
if err != nil {
e.Logger.Error(err)
app.Error(c, http.StatusInternalServerError, err, "内部错误")
return
}
if err := s.SetCooperativeStatus(req); err != nil {
app.Error(c, http.StatusInternalServerError, err, "更新失败")
return
}
app.OK(c, nil, "更新成功")
}

View File

@ -5,21 +5,22 @@ import "go-admin/app/admin/models"
type BusCooperative struct {
models.Model
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 int8 `gorm:"type:tinyint(1);not null;default:1" json:"status"` // 账户状态1启用 0禁用
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"` // 税号
Products []BusCooperativeProduct `json:"products" gorm:"-"` // 产品信息
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:"-"` // 产品信息
}
// BusCooperativeProduct 合作商与产品关联表
@ -33,11 +34,11 @@ type BusCooperativeProduct struct {
// CooperativeListReq 查询合作商列表请求
type CooperativeListReq struct {
CooperativeNumber string `form:"cooperative_number,omitempty"` // 合作商编号(可选)
CooperativeName string `form:"cooperative_name,omitempty"` // 合作商名称(支持模糊查询)
Status int8 `form:"status,omitempty"` // 账户状态1启用 0禁用)
Page int `form:"page" binding:"required,min=1"` // 页码
PageSize int `form:"page_size" binding:"required,min=1,max=100"` // 每页条数
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"` // 每页条数
}
// CooperativeListResp 查询合作商列表响应
@ -50,19 +51,19 @@ type CooperativeListResp struct {
// CreateCooperativeReq 创建合作商请求
type CreateCooperativeReq struct {
CooperativeName string `json:"cooperative_name" binding:"required"` // 合作商名称
Contact string `json:"contact" binding:"required"` // 联系人
Tel string `json:"tel" binding:"required"` // 手机号
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 []ProductDiscount `json:"products"` // 关联产品及折扣信息
CooperativeName string `json:"cooperative_name" binding:"required"` // 合作商名称
Contact string `json:"contact" binding:"required"` // 联系人
Tel string `json:"tel" binding:"required"` // 手机号
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"` // 关联产品及折扣信息
}
// ProductDiscount 结构体用于存储产品ID及对应折扣
@ -78,31 +79,31 @@ type CreateCooperativeResp struct {
// EditCooperativeReq 编辑合作商请求
type EditCooperativeReq struct {
CooperativeNumber string `form:"cooperative_number,required"` // 合作商编号
CooperativeName string `json:"cooperative_name,omitempty"` // 合作商名称(可选)
Contact string `json:"contact,omitempty"` // 联系人(可选)
Tel string `json:"tel,omitempty"` // 手机号(可选)
Status int8 `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 []ProductDiscount `json:"products"` // 关联产品及折扣信息
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"` // 关联产品及折扣信息
}
// DeleteCooperativeReq 删除合作商请求
type DeleteCooperativeReq struct {
CooperativeNumber string `form:"cooperative_number,required"` // 合作商编号
CooperativeNumber string `json:"cooperative_number,required"` // 合作商编号
}
// CooperativeDetailReq 查询合作商详情请求参数
type CooperativeDetailReq struct {
CooperativeID uint64 `json:"cooperative_id" binding:"required"` // 合作商ID
CooperativeNumber string `json:"cooperative_number" binding:"required"` // 合作商编号
}
// CooperativeDetailResp 查询合作商详情响应参数
@ -111,17 +112,39 @@ type CooperativeDetailResp struct {
CooperativeName string `json:"cooperative_name"` // 合作商名称
Contact string `json:"contact"` // 联系人
Tel string `json:"tel"` // 手机号
Status int8 `json:"status"` // 账户状态1启用 0禁用)
Status uint8 `json:"status"` // 账户状态1启用 2禁用)
Account string `json:"account"` // 账户
Password string `json:"password"` // 密码(建议存储加密哈希值)
Balance float64 `json:"balance"` // 账户余额
Free float64 `json:"free"` // 赠送余额
Bond float64 `json:"bond"` // 保证金
CardHolder string `json:"card_holder"` // 开户人
Bank string `json:"bank"` // 开户行
CardID string `json:"card_id"` // 银行帐号
TaxID string `json:"tax_id"` // 税号
Products []ProductDetail `json:"products"` // 产品信息
}
// ProductDetail 产品信息
type ProductDetail struct {
ProductID uint64 `json:"product_id"` // 产品ID
ProductCode string `json:"product_code"` // 产品编码
ProductName string `json:"product_name"` // 产品名称
Discount float64 `json:"discount"` // 折扣0-1
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"` // 合作商编号
Amount float64 `json:"amount" binding:"required"` // 调整金额
GiftAmount float64 `json:"gift_amount,omitempty"` // 赠送金额(仅加款时有效)
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 禁用)
}

View File

@ -0,0 +1,16 @@
package bus_models
import "go-admin/app/admin/models"
type BusCooperativeTransaction struct {
models.Model
CooperativeID uint64 `gorm:"not null;index" json:"cooperative_id"` // 关联的合作商ID
TransactionType uint8 `gorm:"type:tinyint(1);not null" json:"transaction_type"` // 交易类型1-加款, 2-减款)
Amount float64 `gorm:"type:decimal(10,3);not null" json:"amount"` // 交易金额
SourceFundingType uint8 `gorm:"not null;index" json:"source_funding_type"` // 资金来源ID关联 BusFundSource
GiftAmount float64 `gorm:"type:decimal(10,2);not null;default:0.00" json:"gift_amount"` // 赠送金额
BalanceBefore float64 `gorm:"type:decimal(10,3);not null" json:"balance_before"` // 交易前余额
BalanceAfter float64 `gorm:"type:decimal(10,3);not null" json:"balance_after"` // 交易后余额
Remark string `gorm:"type:varchar(255)" json:"remark,omitempty"` // 备注
}

View File

@ -13,10 +13,12 @@ func registerCooperativeManageRouter(v1 *gin.RouterGroup, authMiddleware *jwt.Gi
product := v1.Group("/cooperative").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole())
{
product.POST("/list", api.CooperativeList) // 合作商列表
product.POST("/detail", api.CooperativeDetail) // 合作商详情
product.POST("/create", api.CreateCooperative) // 创建合作商
product.POST("/edit", api.EditCooperative) // 编辑合作商
product.POST("/delete", api.DeleteCooperative) // 删除合作商
product.POST("/list", api.CooperativeList) // 合作商列表
product.POST("/detail", api.CooperativeDetail) // 合作商详情
product.POST("/create", api.CreateCooperative) // 创建合作商
product.POST("/edit", api.EditCooperative) // 编辑合作商
product.POST("/delete", api.DeleteCooperative) // 删除合作商
product.POST("/adjust_account", api.AdjustAccountHandler) // 账户调整,加减款
product.POST("/status", api.SetCooperativeStatus) // 设置账户状态(停用/启用)
}
}

View File

@ -4,7 +4,9 @@ import (
"context"
"errors"
"fmt"
log "github.com/go-admin-team/go-admin-core/logger"
"github.com/go-admin-team/go-admin-core/sdk/service"
"go-admin/app/admin/models"
"go-admin/app/admin/models/bus_models"
"go-admin/common/global"
"gorm.io/gorm"
@ -29,10 +31,8 @@ func (e *CooperativeService) GetCooperativeList(ctx context.Context, req bus_mod
req.PageSize = 10
}
// 构建查询条件,假设我们支持根据 product_code 查询
// 构建查询条件
db := e.Orm.WithContext(ctx)
// 如果需要支持分页,可以根据请求条件调整查询
if req.CooperativeNumber != "" {
db = db.Where("cooperative_number = ?", req.CooperativeNumber)
}
@ -43,11 +43,66 @@ func (e *CooperativeService) GetCooperativeList(ctx context.Context, req bus_mod
db = db.Where("status = ?", req.Status)
}
// 查询数据库
if err := db.Offset(page * req.Page).Limit(req.PageSize).Find(&cooperativeList).Error; err != nil {
// 查询合作商基本信息
if err := db.Offset(page * req.PageSize).Limit(req.PageSize).Find(&cooperativeList).Error; err != nil {
return resp, errors.New("查询合作商列表失败")
}
// 获取所有合作商ID
var cooperativeIDs []uint64
for _, c := range cooperativeList {
cooperativeIDs = append(cooperativeIDs, c.ID)
}
// 查询所有关联的产品信息
var cooperativeProducts []bus_models.BusCooperativeProduct
if len(cooperativeIDs) > 0 {
err := e.Orm.WithContext(ctx).
Where("cooperative_id IN (?)", cooperativeIDs).
Find(&cooperativeProducts).Error
if err != nil {
return resp, errors.New("查询合作商产品信息失败")
}
}
// 获取所有产品ID
var productIDs []uint64
for _, cp := range cooperativeProducts {
productIDs = append(productIDs, cp.ProductID)
}
// 查询产品详细信息
var products []bus_models.BusProduct
if len(productIDs) > 0 {
err := e.Orm.WithContext(ctx).
Where("id IN (?)", productIDs).
Find(&products).Error
if err != nil {
return resp, errors.New("查询产品信息失败")
}
}
// 构建合作商ID到产品信息的映射
productMap := make(map[uint64][]bus_models.ProductDetail)
for _, cp := range cooperativeProducts {
for _, p := range products {
if p.ID == cp.ProductID {
productMap[cp.CooperativeID] = append(productMap[cp.CooperativeID], bus_models.ProductDetail{
ProductCode: p.ProductCode,
ProductName: p.ProductName,
Discount: cp.Discount,
ProductID: p.ID,
})
break
}
}
}
// 组装返回数据
for i := range cooperativeList {
cooperativeList[i].Products = productMap[cooperativeList[i].ID]
}
resp.List = cooperativeList
resp.Total = len(cooperativeList)
resp.Page = page + 1
@ -64,11 +119,68 @@ func (e *CooperativeService) CreateCooperative(req bus_models.CreateCooperativeR
tx := e.Orm.Begin()
defer func() {
if r := recover(); r != nil {
// 打印 panic 错误信息
log.Error("Panic recovered: %v", r)
tx.Rollback()
}
}()
// 创建合作商
// 1-新建系统用户
// 查询 role_id
var roleID int
err := tx.Model(&models.SysRole{}).
Select("role_id").
Where("role_name = ?", global.CooperativeRoleName).
Where("status = ?", global.SysRoleStatusOnUse). // 确保角色状态为正常
First(&roleID).Error
if err != nil || roleID == 0 {
tx.Rollback()
return resp, errors.New("系统未创建合作商角色")
}
// **调用创建用户接口**
var userPhone string
if req.Tel == "" {
userPhone = "13966668888"
} else {
userPhone = req.Tel
}
var sysUser models.SysUser
var count int64
// 检查用户名是否已存在
err = tx.Model(&sysUser).Where("username = ?", req.Account).Count(&count).Error
if err != nil {
tx.Rollback() // 出现错误时回滚
return resp, errors.New("数据库错误:" + err.Error())
}
if count > 0 {
tx.Rollback() // 用户名已存在,回滚事务
return resp, errors.New("用户名已存在")
}
// 创建SysUser对象并插入
userReq := models.SysUser{
Username: req.Account, // 使用合作商账户作为用户名
Password: req.Password, // 直接使用合作商密码(前端已加密)
NickName: req.CooperativeName, // 用合作商名称作为昵称
Phone: userPhone, // 绑定手机号
Email: userPhone + "@mail.com", // 默认邮箱
DeptId: global.DefaultDeptId, // 默认部门ID
Status: global.SysUserStatusOnUse, // 默认状态
RoleId: roleID, // 默认角色ID
}
err = tx.Create(&userReq).Error
if err != nil {
tx.Rollback() // 创建用户失败,回滚事务
return resp, errors.New("创建用户失败:" + err.Error())
}
// 2-创建合作商
cooperative := bus_models.BusCooperative{
CooperativeName: req.CooperativeName,
Contact: req.Contact,
@ -83,8 +195,8 @@ func (e *CooperativeService) CreateCooperative(req bus_models.CreateCooperativeR
Bank: req.Bank,
CardID: req.CardID,
TaxID: req.TaxID,
UserId: userReq.UserId,
}
var err error
cooperative.CooperativeNumber, err = GenerateCooperativeNumber(e.Orm)
if err != nil {
return resp, err
@ -217,10 +329,39 @@ func (e *CooperativeService) EditCooperative(req bus_models.EditCooperativeReq)
// DeleteCooperative 删除合作商
func (e *CooperativeService) DeleteCooperative(req bus_models.DeleteCooperativeReq) error {
// 执行删除操作
if err := e.Orm.Where("cooperative_number = ?", req.CooperativeNumber).
Delete(&bus_models.BusCooperative{}).Error; err != nil {
return errors.New("删除产品失败")
// 开始事务
tx := e.Orm.Begin()
// 1. 查询合作商的user_id
var cooperative bus_models.BusCooperative
err := tx.Where("cooperative_number = ?", req.CooperativeNumber).First(&cooperative).Error
if err != nil {
tx.Rollback() // 如果查询失败,回滚事务
return errors.New("查询合作商失败:" + err.Error())
}
// 2. 如果user_id != 0删除sys_user中的用户
if cooperative.UserId != 0 {
var sysUser models.SysUser
err = tx.Where("user_id = ?", cooperative.UserId).Delete(&sysUser).Error
if err != nil {
tx.Rollback() // 删除用户失败,回滚事务
return errors.New("删除用户失败:" + err.Error())
}
}
// 3. 删除合作商
err = tx.Where("cooperative_number = ?", req.CooperativeNumber).Delete(&bus_models.BusCooperative{}).Error
if err != nil {
tx.Rollback() // 删除合作商失败,回滚事务
return errors.New("删除合作商失败:" + err.Error())
}
// 4. 提交事务
err = tx.Commit().Error
if err != nil {
tx.Rollback() // 提交事务失败,回滚
return errors.New("提交事务失败:" + err.Error())
}
return nil
@ -233,7 +374,7 @@ func (e *CooperativeService) GetCooperativeDetail(ctx context.Context, req bus_m
var cooperative bus_models.BusCooperative
// 查询合作商基本信息
db := e.Orm.WithContext(ctx).Where("id = ?", req.CooperativeID)
db := e.Orm.WithContext(ctx).Where("cooperative_number = ?", req.CooperativeNumber)
if err := db.First(&cooperative).Error; err != nil {
return resp, errors.New("查询合作商详情失败")
@ -242,7 +383,7 @@ func (e *CooperativeService) GetCooperativeDetail(ctx context.Context, req bus_m
// 查询该合作商的产品信息
var cooperativeProducts []bus_models.BusCooperativeProduct
err := e.Orm.WithContext(ctx).
Where("cooperative_id = ?", req.CooperativeID).
Where("cooperative_id = ?", cooperative.ID).
Find(&cooperativeProducts).Error
if err != nil {
@ -293,6 +434,11 @@ func (e *CooperativeService) GetCooperativeDetail(ctx context.Context, req bus_m
resp.Balance = cooperative.Balance
resp.Free = cooperative.Free
resp.Bond = cooperative.Bond
resp.CardHolder = cooperative.CardHolder
resp.Bank = cooperative.Bank
resp.CardID = cooperative.CardID
resp.TaxID = cooperative.TaxID
resp.Products = productDetails
return resp, nil
@ -327,3 +473,88 @@ func GenerateCooperativeNumber(db *gorm.DB) (string, error) {
return cooperativeNumber, nil
}
// AdjustAccount 账户调整(加款/减款)
func (e *CooperativeService) AdjustAccount(ctx context.Context, req bus_models.AdjustAccountReq) error {
var cooperative bus_models.BusCooperative
db := e.Orm.WithContext(ctx)
// 查询合作商账户信息
if err := db.Where("cooperative_number = ?", req.CooperativeNumber).First(&cooperative).Error; err != nil {
return errors.New("合作商账户不存在")
}
// 确保 transaction_type = 2 (减款) 时,金额是负的
if req.TransactionType == 2 && req.Amount > 0 {
req.Amount = -req.Amount
}
// 加款时需要 SourceFundingType但减款时不需要
if req.TransactionType == 1 && req.SourceFundingType == 0 {
return errors.New("加款时必须填写资金来源")
}
// 计算新的余额
newBalance := cooperative.Balance
newFree := cooperative.Free
if req.TransactionType == 2 { // 处理减款
totalFunds := cooperative.Balance + cooperative.Free
if totalFunds < -req.Amount { // 确保总余额足够扣款
return errors.New("账户余额不足,无法扣款")
}
// 先扣普通余额,再扣赠送余额
if cooperative.Balance >= -req.Amount {
newBalance += req.Amount // 这里 req.Amount 是负数,所以相当于减去
} else {
remaining := -req.Amount - cooperative.Balance
newBalance = 0
newFree -= remaining
}
} else { // 处理加款
newBalance += req.Amount
newFree += req.GiftAmount // 仅加款时可增加赠送金额
}
// 开启事务,确保数据一致性
tx := db.Begin()
defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()
// 更新合作商账户余额
if err := tx.Model(&cooperative).Updates(map[string]interface{}{
"balance": newBalance,
"free": newFree,
}).Error; err != nil {
tx.Rollback()
return errors.New("更新账户余额失败")
}
// 记录交易信息
transaction := bus_models.BusCooperativeTransaction{
CooperativeID: cooperative.ID,
Amount: req.Amount, // 直接存入调整后的金额
GiftAmount: req.GiftAmount,
SourceFundingType: req.SourceFundingType,
Remark: req.Remark,
}
if err := tx.Create(&transaction).Error; err != nil {
tx.Rollback()
return errors.New("记录交易失败")
}
tx.Commit()
return nil
}
// SetCooperativeStatus 在服务层更新合作商状态
func (e *CooperativeService) SetCooperativeStatus(req bus_models.SetCooperativeStatusReq) error {
return e.Orm.Model(&bus_models.BusCooperative{}).
Where("cooperative_number = ?", req.CooperativeNumber).
Update("status", req.Status).Error
}

View File

@ -47,7 +47,7 @@ func setupSimpleDatabase(host string, c *toolsConfig.Database) {
Logger: New(
logger.Config{
SlowThreshold: time.Second,
Colorful: true,
Colorful: false,
LogLevel: logger.LogLevel(
log.DefaultLogger.Options().Level.LevelForGorm()),
},

View File

@ -2,4 +2,8 @@ package global
const (
CooperativeStatusOnUse = 1 // 合作商状态-启用
CooperativeRoleName = "合作商"
SysRoleStatusOnUse = 2 // 系统角色状态-正常
SysUserStatusOnUse = "2" // 系统用户状态-正常
DefaultDeptId = 1 // 默认部门id
)

View File

@ -25,7 +25,7 @@ settings:
# 日志存放路径
path: temp/logs
# 日志输出file文件default命令行其他命令行
stdout: '' #控制台日志,启用后,不输出到文件
stdout: default
# 日志等级, trace, debug, info, warn, error, fatal
level: trace
# 数据库日志开关

View File

@ -86,6 +86,39 @@ const docTemplateadmin = `{
}
}
},
"/api/v1/cooperative/adjust_account": {
"post": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"合作商管理-V1.0.0"
],
"summary": "账户调整(加款/减款)",
"parameters": [
{
"description": "账户调整请求",
"name": "request",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/bus_models.AdjustAccountReq"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/app.Response"
}
}
}
}
},
"/api/v1/cooperative/create": {
"post": {
"consumes": [
@ -251,6 +284,39 @@ const docTemplateadmin = `{
}
}
},
"/api/v1/cooperative/status": {
"post": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"合作商管理-V1.0.0"
],
"summary": "设置合作商状态",
"parameters": [
{
"description": "设置合作商状态",
"name": "request",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/bus_models.SetCooperativeStatusReq"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/app.Response"
}
}
}
}
},
"/api/v1/db/columns/page": {
"get": {
"description": "数据库表列分页列表 / database table column page list",
@ -3107,6 +3173,40 @@ const docTemplateadmin = `{
}
}
},
"bus_models.AdjustAccountReq": {
"type": "object",
"required": [
"amount",
"cooperative_number",
"transaction_type"
],
"properties": {
"amount": {
"description": "调整金额",
"type": "number"
},
"cooperative_number": {
"description": "合作商编号",
"type": "string"
},
"gift_amount": {
"description": "赠送金额(仅加款时有效)",
"type": "number"
},
"remark": {
"description": "备注信息,例如交易编号、支付凭证等",
"type": "string"
},
"source_funding_type": {
"description": "资金来源,例如 \"对公转账\"、\"支付宝\"、\"微信\"、\"现金\"等(查询字典值)",
"type": "integer"
},
"transaction_type": {
"description": "交易类型1-加款, 2-减款)",
"type": "integer"
}
}
},
"bus_models.BusCooperative": {
"type": "object",
"properties": {
@ -3166,11 +3266,11 @@ const docTemplateadmin = `{
"description": "产品信息",
"type": "array",
"items": {
"$ref": "#/definitions/bus_models.BusCooperativeProduct"
"$ref": "#/definitions/bus_models.ProductDetail"
}
},
"status": {
"description": "账户状态1启用 0禁用)",
"description": "账户状态1启用 2禁用)",
"type": "integer"
},
"tax_id": {
@ -3184,35 +3284,10 @@ const docTemplateadmin = `{
"updatedAt": {
"description": "更新时间",
"type": "string"
}
}
},
"bus_models.BusCooperativeProduct": {
"type": "object",
"properties": {
"cooperative_id": {
"description": "合作商编号",
},
"userId": {
"description": "用户ID重置密码时需要",
"type": "integer"
},
"createdAt": {
"description": "创建时间",
"type": "string"
},
"discount": {
"description": "折扣0-1",
"type": "number"
},
"id": {
"description": "数据库记录编号",
"type": "integer"
},
"product_id": {
"description": "产品ID",
"type": "integer"
},
"updatedAt": {
"description": "更新时间",
"type": "string"
}
}
},
@ -3292,12 +3367,12 @@ const docTemplateadmin = `{
"bus_models.CooperativeDetailReq": {
"type": "object",
"required": [
"cooperative_id"
"cooperative_number"
],
"properties": {
"cooperative_id": {
"description": "合作商ID",
"type": "integer"
"cooperative_number": {
"description": "合作商编号",
"type": "string"
}
}
},
@ -3312,10 +3387,22 @@ const docTemplateadmin = `{
"description": "账户余额",
"type": "number"
},
"bank": {
"description": "开户行",
"type": "string"
},
"bond": {
"description": "保证金",
"type": "number"
},
"card_holder": {
"description": "开户人",
"type": "string"
},
"card_id": {
"description": "银行帐号",
"type": "string"
},
"contact": {
"description": "联系人",
"type": "string"
@ -3332,6 +3419,10 @@ const docTemplateadmin = `{
"description": "赠送余额",
"type": "number"
},
"password": {
"description": "密码(建议存储加密哈希值)",
"type": "string"
},
"products": {
"description": "产品信息",
"type": "array",
@ -3340,9 +3431,13 @@ const docTemplateadmin = `{
}
},
"status": {
"description": "账户状态1启用 0禁用)",
"description": "账户状态1启用 2禁用)",
"type": "integer"
},
"tax_id": {
"description": "税号",
"type": "string"
},
"tel": {
"description": "手机号",
"type": "string"
@ -3376,7 +3471,7 @@ const docTemplateadmin = `{
"minimum": 1
},
"status": {
"description": "账户状态1启用 0禁用)",
"description": "账户状态1启用 2禁用)",
"type": "integer"
}
}
@ -3459,7 +3554,7 @@ const docTemplateadmin = `{
"description": "关联产品及折扣信息",
"type": "array",
"items": {
"$ref": "#/definitions/bus_models.ProductDiscount"
"$ref": "#/definitions/bus_models.ProductDetail"
}
},
"tax_id": {
@ -3631,7 +3726,7 @@ const docTemplateadmin = `{
"description": "关联产品及折扣信息",
"type": "array",
"items": {
"$ref": "#/definitions/bus_models.ProductDiscount"
"$ref": "#/definitions/bus_models.ProductDetail"
}
},
"status": {
@ -3722,33 +3817,20 @@ const docTemplateadmin = `{
"type": "object",
"properties": {
"discount": {
"description": "折扣0-1",
"description": "折扣0-1:给到合作商的折扣,不是产品本身的折扣",
"type": "number"
},
"product_code": {
"description": "产品编码",
"type": "string"
},
"product_name": {
"description": "产品名称",
"type": "string"
}
}
},
"bus_models.ProductDiscount": {
"type": "object",
"required": [
"discount",
"product_id"
],
"properties": {
"discount": {
"description": "折扣0-1之间",
"type": "number"
},
"product_id": {
"description": "产品ID",
"type": "integer"
},
"product_name": {
"description": "产品名称",
"type": "string"
}
}
},
@ -3813,6 +3895,27 @@ const docTemplateadmin = `{
}
}
},
"bus_models.SetCooperativeStatusReq": {
"type": "object",
"required": [
"cooperative_number",
"status"
],
"properties": {
"cooperative_number": {
"description": "合作商编号",
"type": "string"
},
"status": {
"description": "状态 (1 启用, 2 禁用)",
"type": "integer",
"enum": [
1,
2
]
}
}
},
"dto.GetSetSysConfigReq": {
"type": "object",
"properties": {

View File

@ -78,6 +78,39 @@
}
}
},
"/api/v1/cooperative/adjust_account": {
"post": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"合作商管理-V1.0.0"
],
"summary": "账户调整(加款/减款)",
"parameters": [
{
"description": "账户调整请求",
"name": "request",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/bus_models.AdjustAccountReq"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/app.Response"
}
}
}
}
},
"/api/v1/cooperative/create": {
"post": {
"consumes": [
@ -243,6 +276,39 @@
}
}
},
"/api/v1/cooperative/status": {
"post": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"合作商管理-V1.0.0"
],
"summary": "设置合作商状态",
"parameters": [
{
"description": "设置合作商状态",
"name": "request",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/bus_models.SetCooperativeStatusReq"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/app.Response"
}
}
}
}
},
"/api/v1/db/columns/page": {
"get": {
"description": "数据库表列分页列表 / database table column page list",
@ -3099,6 +3165,40 @@
}
}
},
"bus_models.AdjustAccountReq": {
"type": "object",
"required": [
"amount",
"cooperative_number",
"transaction_type"
],
"properties": {
"amount": {
"description": "调整金额",
"type": "number"
},
"cooperative_number": {
"description": "合作商编号",
"type": "string"
},
"gift_amount": {
"description": "赠送金额(仅加款时有效)",
"type": "number"
},
"remark": {
"description": "备注信息,例如交易编号、支付凭证等",
"type": "string"
},
"source_funding_type": {
"description": "资金来源,例如 \"对公转账\"、\"支付宝\"、\"微信\"、\"现金\"等(查询字典值)",
"type": "integer"
},
"transaction_type": {
"description": "交易类型1-加款, 2-减款)",
"type": "integer"
}
}
},
"bus_models.BusCooperative": {
"type": "object",
"properties": {
@ -3158,11 +3258,11 @@
"description": "产品信息",
"type": "array",
"items": {
"$ref": "#/definitions/bus_models.BusCooperativeProduct"
"$ref": "#/definitions/bus_models.ProductDetail"
}
},
"status": {
"description": "账户状态1启用 0禁用)",
"description": "账户状态1启用 2禁用)",
"type": "integer"
},
"tax_id": {
@ -3176,35 +3276,10 @@
"updatedAt": {
"description": "更新时间",
"type": "string"
}
}
},
"bus_models.BusCooperativeProduct": {
"type": "object",
"properties": {
"cooperative_id": {
"description": "合作商编号",
},
"userId": {
"description": "用户ID重置密码时需要",
"type": "integer"
},
"createdAt": {
"description": "创建时间",
"type": "string"
},
"discount": {
"description": "折扣0-1",
"type": "number"
},
"id": {
"description": "数据库记录编号",
"type": "integer"
},
"product_id": {
"description": "产品ID",
"type": "integer"
},
"updatedAt": {
"description": "更新时间",
"type": "string"
}
}
},
@ -3284,12 +3359,12 @@
"bus_models.CooperativeDetailReq": {
"type": "object",
"required": [
"cooperative_id"
"cooperative_number"
],
"properties": {
"cooperative_id": {
"description": "合作商ID",
"type": "integer"
"cooperative_number": {
"description": "合作商编号",
"type": "string"
}
}
},
@ -3304,10 +3379,22 @@
"description": "账户余额",
"type": "number"
},
"bank": {
"description": "开户行",
"type": "string"
},
"bond": {
"description": "保证金",
"type": "number"
},
"card_holder": {
"description": "开户人",
"type": "string"
},
"card_id": {
"description": "银行帐号",
"type": "string"
},
"contact": {
"description": "联系人",
"type": "string"
@ -3324,6 +3411,10 @@
"description": "赠送余额",
"type": "number"
},
"password": {
"description": "密码(建议存储加密哈希值)",
"type": "string"
},
"products": {
"description": "产品信息",
"type": "array",
@ -3332,9 +3423,13 @@
}
},
"status": {
"description": "账户状态1启用 0禁用)",
"description": "账户状态1启用 2禁用)",
"type": "integer"
},
"tax_id": {
"description": "税号",
"type": "string"
},
"tel": {
"description": "手机号",
"type": "string"
@ -3368,7 +3463,7 @@
"minimum": 1
},
"status": {
"description": "账户状态1启用 0禁用)",
"description": "账户状态1启用 2禁用)",
"type": "integer"
}
}
@ -3451,7 +3546,7 @@
"description": "关联产品及折扣信息",
"type": "array",
"items": {
"$ref": "#/definitions/bus_models.ProductDiscount"
"$ref": "#/definitions/bus_models.ProductDetail"
}
},
"tax_id": {
@ -3623,7 +3718,7 @@
"description": "关联产品及折扣信息",
"type": "array",
"items": {
"$ref": "#/definitions/bus_models.ProductDiscount"
"$ref": "#/definitions/bus_models.ProductDetail"
}
},
"status": {
@ -3714,33 +3809,20 @@
"type": "object",
"properties": {
"discount": {
"description": "折扣0-1",
"description": "折扣0-1:给到合作商的折扣,不是产品本身的折扣",
"type": "number"
},
"product_code": {
"description": "产品编码",
"type": "string"
},
"product_name": {
"description": "产品名称",
"type": "string"
}
}
},
"bus_models.ProductDiscount": {
"type": "object",
"required": [
"discount",
"product_id"
],
"properties": {
"discount": {
"description": "折扣0-1之间",
"type": "number"
},
"product_id": {
"description": "产品ID",
"type": "integer"
},
"product_name": {
"description": "产品名称",
"type": "string"
}
}
},
@ -3805,6 +3887,27 @@
}
}
},
"bus_models.SetCooperativeStatusReq": {
"type": "object",
"required": [
"cooperative_number",
"status"
],
"properties": {
"cooperative_number": {
"description": "合作商编号",
"type": "string"
},
"status": {
"description": "状态 (1 启用, 2 禁用)",
"type": "integer",
"enum": [
1,
2
]
}
}
},
"dto.GetSetSysConfigReq": {
"type": "object",
"properties": {

View File

@ -14,6 +14,31 @@ definitions:
description: 请求id
type: string
type: object
bus_models.AdjustAccountReq:
properties:
amount:
description: 调整金额
type: number
cooperative_number:
description: 合作商编号
type: string
gift_amount:
description: 赠送金额(仅加款时有效)
type: number
remark:
description: 备注信息,例如交易编号、支付凭证等
type: string
source_funding_type:
description: 资金来源,例如 "对公转账"、"支付宝"、"微信"、"现金"等(查询字典值)
type: integer
transaction_type:
description: 交易类型1-加款, 2-减款)
type: integer
required:
- amount
- cooperative_number
- transaction_type
type: object
bus_models.BusCooperative:
properties:
account:
@ -58,10 +83,10 @@ definitions:
products:
description: 产品信息
items:
$ref: '#/definitions/bus_models.BusCooperativeProduct'
$ref: '#/definitions/bus_models.ProductDetail'
type: array
status:
description: 账户状态1启用 0禁用)
description: 账户状态1启用 2禁用)
type: integer
tax_id:
description: 税号
@ -72,27 +97,9 @@ definitions:
updatedAt:
description: 更新时间
type: string
type: object
bus_models.BusCooperativeProduct:
properties:
cooperative_id:
description: 合作商编号
userId:
description: 用户ID重置密码时需要
type: integer
createdAt:
description: 创建时间
type: string
discount:
description: 折扣0-1
type: number
id:
description: 数据库记录编号
type: integer
product_id:
description: 产品ID
type: integer
updatedAt:
description: 更新时间
type: string
type: object
bus_models.BusProduct:
properties:
@ -150,11 +157,11 @@ definitions:
type: object
bus_models.CooperativeDetailReq:
properties:
cooperative_id:
description: 合作商ID
type: integer
cooperative_number:
description: 合作商编号
type: string
required:
- cooperative_id
- cooperative_number
type: object
bus_models.CooperativeDetailResp:
properties:
@ -164,9 +171,18 @@ definitions:
balance:
description: 账户余额
type: number
bank:
description: 开户行
type: string
bond:
description: 保证金
type: number
card_holder:
description: 开户人
type: string
card_id:
description: 银行帐号
type: string
contact:
description: 联系人
type: string
@ -179,14 +195,20 @@ definitions:
free:
description: 赠送余额
type: number
password:
description: 密码(建议存储加密哈希值)
type: string
products:
description: 产品信息
items:
$ref: '#/definitions/bus_models.ProductDetail'
type: array
status:
description: 账户状态1启用 0禁用)
description: 账户状态1启用 2禁用)
type: integer
tax_id:
description: 税号
type: string
tel:
description: 手机号
type: string
@ -209,7 +231,7 @@ definitions:
minimum: 1
type: integer
status:
description: 账户状态1启用 0禁用)
description: 账户状态1启用 2禁用)
type: integer
required:
- page
@ -267,7 +289,7 @@ definitions:
products:
description: 关联产品及折扣信息
items:
$ref: '#/definitions/bus_models.ProductDiscount'
$ref: '#/definitions/bus_models.ProductDetail'
type: array
tax_id:
description: 税号(可选)
@ -398,7 +420,7 @@ definitions:
products:
description: 关联产品及折扣信息
items:
$ref: '#/definitions/bus_models.ProductDiscount'
$ref: '#/definitions/bus_models.ProductDetail'
type: array
status:
description: 账户状态(可选)
@ -465,26 +487,17 @@ definitions:
bus_models.ProductDetail:
properties:
discount:
description: 折扣0-1
description: 折扣0-1:给到合作商的折扣,不是产品本身的折扣
type: number
product_code:
description: 产品编码
type: string
product_name:
description: 产品名称
type: string
type: object
bus_models.ProductDiscount:
properties:
discount:
description: 折扣0-1之间
type: number
product_id:
description: 产品ID
type: integer
required:
- discount
- product_id
product_name:
description: 产品名称
type: string
type: object
bus_models.ProductListReq:
properties:
@ -530,6 +543,21 @@ definitions:
description: 总记录数
type: integer
type: object
bus_models.SetCooperativeStatusReq:
properties:
cooperative_number:
description: 合作商编号
type: string
status:
description: 状态 (1 启用, 2 禁用)
enum:
- 1
- 2
type: integer
required:
- cooperative_number
- status
type: object
dto.GetSetSysConfigReq:
properties:
configKey:
@ -1626,6 +1654,27 @@ paths:
summary: 获取验证码
tags:
- 登陆
/api/v1/cooperative/adjust_account:
post:
consumes:
- application/json
parameters:
- description: 账户调整请求
in: body
name: request
required: true
schema:
$ref: '#/definitions/bus_models.AdjustAccountReq'
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/app.Response'
summary: 账户调整(加款/减款)
tags:
- 合作商管理-V1.0.0
/api/v1/cooperative/create:
post:
consumes:
@ -1731,6 +1780,27 @@ paths:
summary: 查询合作商列表
tags:
- 合作商管理-V1.0.0
/api/v1/cooperative/status:
post:
consumes:
- application/json
parameters:
- description: 设置合作商状态
in: body
name: request
required: true
schema:
$ref: '#/definitions/bus_models.SetCooperativeStatusReq'
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/app.Response'
summary: 设置合作商状态
tags:
- 合作商管理-V1.0.0
/api/v1/db/columns/page:
get:
description: 数据库表列分页列表 / database table column page list