1.新增折扣相关接口;

This commit is contained in:
chenlin 2025-03-21 16:40:10 +08:00
parent 7cfbf8fb46
commit 5d93e6227b
7 changed files with 733 additions and 40 deletions

View File

@ -256,3 +256,69 @@ func (e CooperativeApi) SetCooperativeStatus(c *gin.Context) {
app.OK(c, nil, "更新成功")
}
// QueryCooperativeProducts 查询合作商产品列表请求参数
// @Summary 查询合作商产品列表
// @Tags 合作商管理-V1.0.0
// @Produce json
// @Accept json
// @Param request body bus_models.QueryCooperativeProductsReq true "查询合作商产品模型"
// @Success 200 {object} bus_models.QueryCooperativeProductsResp
// @Router /api/v1/cooperative/products [post]
func (e CooperativeApi) QueryCooperativeProducts(c *gin.Context) {
s := bus_service.CooperativeService{}
var req bus_models.QueryCooperativeProductsReq
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
}
// 调用服务层逻辑
result, err := s.QueryCooperativeProducts(req)
if err != nil {
app.Error(c, http.StatusInternalServerError, err, err.Error())
return
}
app.OK(c, result, "查询成功")
}
// UpdateCooperativeProductDiscount 批量更新合作商产品折扣
// @Summary 批量更新合作商产品折扣
// @Tags 合作商管理-V1.0.0
// @Produce json
// @Accept json
// @Param request body bus_models.UpdateProductDiscountReq true "批量更新折扣请求"
// @Success 200 {object} app.Response
// @Router /api/v1/cooperative/update_discount [post]
func (e CooperativeApi) UpdateCooperativeProductDiscount(c *gin.Context) {
s := bus_service.CooperativeService{}
var req bus_models.UpdateProductDiscountReq
err := e.MakeContext(c).
MakeOrm().
Bind(&req, binding.JSON).
MakeService(&s.Service).
Errors
if err != nil {
e.Logger.Error(err)
e.Error(500, err, "参数解析失败")
return
}
// 调用服务层
err = s.UpdateCooperativeProductDiscount(req)
if err != nil {
app.Error(c, http.StatusInternalServerError, err, err.Error())
return
}
app.OK(c, nil, "折扣更新成功")
}

View File

@ -52,8 +52,8 @@ 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"` // 手机号
Contact string `json:"contact,omitempty"` // 联系人
Tel string `json:"tel,omitempty"` // 手机号
Account string `json:"account" binding:"required"` // 账户
Password string `json:"password" binding:"required"` // 密码(前端传输时应加密)
Balance float64 `json:"balance,omitempty"` // 账户余额(可选)
@ -137,8 +137,8 @@ type ProductDetail struct {
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"` // 赠送金额(仅加款时有效)
Amount float64 `json:"amount,omitempty"` // 调整金额
GiftAmount float64 `json:"gift_amount,omitempty"` // 赠送金额
SourceFundingType uint8 `json:"source_funding_type"` // 资金来源,例如 "对公转账"、"支付宝"、"微信"、"现金"等(查询字典值)
Remark string `json:"remark,omitempty"` // 备注信息,例如交易编号、支付凭证等
}
@ -148,3 +148,41 @@ type SetCooperativeStatusReq struct {
CooperativeNumber string `json:"cooperative_number" binding:"required"` // 合作商编号
Status uint8 `json:"status" binding:"required,oneof=1 2"` // 状态 (1 启用, 2 禁用)
}
// 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"`
}

View File

@ -13,12 +13,14 @@ 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("/adjust_account", api.AdjustAccountHandler) // 账户调整,加减款
product.POST("/status", api.SetCooperativeStatus) // 设置账户状态(停用/启用)
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) // 设置账户状态(停用/启用)
product.POST("/products", api.QueryCooperativeProducts) // 查询合作商产品列表
product.POST("/update_discount", api.UpdateCooperativeProductDiscount) // 批量更新合作商产品折扣
}
}

View File

@ -484,9 +484,18 @@ func (e *CooperativeService) AdjustAccount(ctx context.Context, req bus_models.A
return errors.New("合作商账户不存在")
}
if req.Amount == 0 && req.GiftAmount == 0 {
return nil
}
// 确保 transaction_type = 2 (减款) 时,金额是负的
if req.TransactionType == 2 && req.Amount > 0 {
req.Amount = -req.Amount
if req.TransactionType == 2 {
if req.Amount > 0 {
req.Amount = -req.Amount
}
if req.GiftAmount > 0 {
req.GiftAmount = -req.GiftAmount
}
}
// 加款时需要 SourceFundingType但减款时不需要
@ -499,22 +508,21 @@ func (e *CooperativeService) AdjustAccount(ctx context.Context, req bus_models.A
newFree := cooperative.Free
if req.TransactionType == 2 { // 处理减款
totalFunds := cooperative.Balance + cooperative.Free
if totalFunds < -req.Amount { // 确保总余额足够扣款
return errors.New("账户余额不足,无法扣款")
newBalance += req.Amount
newFree += req.GiftAmount
if cooperative.Balance < -req.Amount {
log.Info("账户余额不足,无法扣除普通余额")
newBalance = 0
}
if cooperative.Free < -req.GiftAmount {
log.Info("赠送余额不足,无法扣除赠送余额")
newFree = 0
}
// 先扣普通余额,再扣赠送余额
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 // 仅加款时可增加赠送金额
newFree += req.GiftAmount
}
// 开启事务,确保数据一致性
@ -537,7 +545,7 @@ func (e *CooperativeService) AdjustAccount(ctx context.Context, req bus_models.A
// 记录交易信息
transaction := bus_models.BusCooperativeTransaction{
CooperativeID: cooperative.ID,
Amount: req.Amount, // 直接存入调整后的金额
Amount: req.Amount,
GiftAmount: req.GiftAmount,
SourceFundingType: req.SourceFundingType,
Remark: req.Remark,
@ -558,3 +566,81 @@ func (e *CooperativeService) SetCooperativeStatus(req bus_models.SetCooperativeS
Where("cooperative_number = ?", req.CooperativeNumber).
Update("status", req.Status).Error
}
// QueryCooperativeProducts 查询合作商产品列表
func (e *CooperativeService) QueryCooperativeProducts(req bus_models.QueryCooperativeProductsReq) (*bus_models.QueryCooperativeProductsResp, error) {
var cooperative bus_models.BusCooperative
page := req.Page - 1
if page < 0 {
page = 0
}
if req.PageSize == 0 {
req.PageSize = 10
}
err := e.Orm.Where("cooperative_number = ?", req.CooperativeNumber).First(&cooperative).Error
if err != nil {
return nil, errors.New("合作商不存在")
}
var products []bus_models.ProductInfo
var total int64
query := e.Orm.Table("bus_cooperative_product as cp").
Joins("JOIN bus_product as p ON cp.product_id = p.id").
Where("cp.cooperative_id = ?", cooperative.ID)
// 获取总数
query.Count(&total)
// 查询分页数据
query.Select("p.id as product_id,p.product_code, p.product_name, p.product_type, p.type, p.size, p.price, cp.discount, " +
"p.retail_price, p.description, p.province, p.city, p.platform").
Offset(page * req.PageSize).
Limit(req.PageSize).
Find(&products)
return &bus_models.QueryCooperativeProductsResp{
Total: int(total),
Page: page + 1,
PageSize: req.PageSize,
List: products,
}, nil
}
// UpdateCooperativeProductDiscount 批量更新合作商产品折扣
func (e *CooperativeService) UpdateCooperativeProductDiscount(req bus_models.UpdateProductDiscountReq) error {
if len(req.Products) == 0 {
return errors.New("产品列表不能为空")
}
var cooperative bus_models.BusCooperative
err := e.Orm.Where("cooperative_number = ?", req.CooperativeNumber).First(&cooperative).Error
if err != nil {
return errors.New("合作商不存在")
}
tx := e.Orm.Begin() // 开始事务
for _, p := range req.Products {
if p.Discount < 0 || p.Discount > 1 {
tx.Rollback()
return fmt.Errorf("产品ID %d 的折扣值不合法,必须在 0-1 之间", p.ProductID)
}
err := tx.Model(&bus_models.BusCooperativeProduct{}).
Where("cooperative_id = ? AND product_id = ?", cooperative.ID, p.ProductID).
Update("discount", p.Discount).Error
if err != nil {
tx.Rollback()
return fmt.Errorf("更新产品ID %d 失败: %v", p.ProductID, err)
}
}
if err := tx.Commit().Error; err != nil {
tx.Rollback()
return fmt.Errorf("提交事务失败: %v", err)
}
return nil
}

View File

@ -284,6 +284,39 @@ const docTemplateadmin = `{
}
}
},
"/api/v1/cooperative/products": {
"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.QueryCooperativeProductsReq"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/bus_models.QueryCooperativeProductsResp"
}
}
}
}
},
"/api/v1/cooperative/status": {
"post": {
"consumes": [
@ -317,6 +350,39 @@ const docTemplateadmin = `{
}
}
},
"/api/v1/cooperative/update_discount": {
"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.UpdateProductDiscountReq"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/app.Response"
}
}
}
}
},
"/api/v1/db/columns/page": {
"get": {
"description": "数据库表列分页列表 / database table column page list",
@ -3176,7 +3242,6 @@ const docTemplateadmin = `{
"bus_models.AdjustAccountReq": {
"type": "object",
"required": [
"amount",
"cooperative_number",
"transaction_type"
],
@ -3190,7 +3255,7 @@ const docTemplateadmin = `{
"type": "string"
},
"gift_amount": {
"description": "赠送金额(仅加款时有效)",
"description": "赠送金额",
"type": "number"
},
"remark": {
@ -3504,10 +3569,8 @@ const docTemplateadmin = `{
"type": "object",
"required": [
"account",
"contact",
"cooperative_name",
"password",
"tel"
"password"
],
"properties": {
"account": {
@ -3834,6 +3897,68 @@ const docTemplateadmin = `{
}
}
},
"bus_models.ProductDiscount": {
"type": "object",
"required": [
"discount",
"product_id"
],
"properties": {
"discount": {
"description": "折扣0-1之间",
"type": "number"
},
"product_id": {
"description": "产品ID",
"type": "integer"
}
}
},
"bus_models.ProductInfo": {
"type": "object",
"properties": {
"city": {
"type": "string"
},
"description": {
"type": "string"
},
"discount": {
"type": "number"
},
"platform": {
"type": "integer"
},
"price": {
"type": "number"
},
"product_code": {
"type": "string"
},
"product_id": {
"description": "产品ID",
"type": "integer"
},
"product_name": {
"type": "string"
},
"product_type": {
"type": "integer"
},
"province": {
"type": "string"
},
"retail_price": {
"type": "number"
},
"size": {
"type": "string"
},
"type": {
"type": "integer"
}
}
},
"bus_models.ProductListReq": {
"type": "object",
"properties": {
@ -3895,6 +4020,50 @@ const docTemplateadmin = `{
}
}
},
"bus_models.QueryCooperativeProductsReq": {
"type": "object",
"required": [
"cooperative_number"
],
"properties": {
"cooperative_number": {
"description": "合作商编号",
"type": "string"
},
"page": {
"description": "页码",
"type": "integer"
},
"page_size": {
"description": "每页条数",
"type": "integer"
}
}
},
"bus_models.QueryCooperativeProductsResp": {
"type": "object",
"properties": {
"list": {
"description": "产品信息列表",
"type": "array",
"items": {
"$ref": "#/definitions/bus_models.ProductInfo"
}
},
"page": {
"description": "当前页码",
"type": "integer"
},
"page_size": {
"description": "每页条数",
"type": "integer"
},
"total": {
"description": "总条数",
"type": "integer"
}
}
},
"bus_models.SetCooperativeStatusReq": {
"type": "object",
"required": [
@ -3916,6 +4085,25 @@ const docTemplateadmin = `{
}
}
},
"bus_models.UpdateProductDiscountReq": {
"type": "object",
"required": [
"cooperative_number",
"products"
],
"properties": {
"cooperative_number": {
"description": "合作商编号",
"type": "string"
},
"products": {
"type": "array",
"items": {
"$ref": "#/definitions/bus_models.ProductDiscount"
}
}
}
},
"dto.GetSetSysConfigReq": {
"type": "object",
"properties": {

View File

@ -276,6 +276,39 @@
}
}
},
"/api/v1/cooperative/products": {
"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.QueryCooperativeProductsReq"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/bus_models.QueryCooperativeProductsResp"
}
}
}
}
},
"/api/v1/cooperative/status": {
"post": {
"consumes": [
@ -309,6 +342,39 @@
}
}
},
"/api/v1/cooperative/update_discount": {
"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.UpdateProductDiscountReq"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/app.Response"
}
}
}
}
},
"/api/v1/db/columns/page": {
"get": {
"description": "数据库表列分页列表 / database table column page list",
@ -3168,7 +3234,6 @@
"bus_models.AdjustAccountReq": {
"type": "object",
"required": [
"amount",
"cooperative_number",
"transaction_type"
],
@ -3182,7 +3247,7 @@
"type": "string"
},
"gift_amount": {
"description": "赠送金额(仅加款时有效)",
"description": "赠送金额",
"type": "number"
},
"remark": {
@ -3496,10 +3561,8 @@
"type": "object",
"required": [
"account",
"contact",
"cooperative_name",
"password",
"tel"
"password"
],
"properties": {
"account": {
@ -3826,6 +3889,68 @@
}
}
},
"bus_models.ProductDiscount": {
"type": "object",
"required": [
"discount",
"product_id"
],
"properties": {
"discount": {
"description": "折扣0-1之间",
"type": "number"
},
"product_id": {
"description": "产品ID",
"type": "integer"
}
}
},
"bus_models.ProductInfo": {
"type": "object",
"properties": {
"city": {
"type": "string"
},
"description": {
"type": "string"
},
"discount": {
"type": "number"
},
"platform": {
"type": "integer"
},
"price": {
"type": "number"
},
"product_code": {
"type": "string"
},
"product_id": {
"description": "产品ID",
"type": "integer"
},
"product_name": {
"type": "string"
},
"product_type": {
"type": "integer"
},
"province": {
"type": "string"
},
"retail_price": {
"type": "number"
},
"size": {
"type": "string"
},
"type": {
"type": "integer"
}
}
},
"bus_models.ProductListReq": {
"type": "object",
"properties": {
@ -3887,6 +4012,50 @@
}
}
},
"bus_models.QueryCooperativeProductsReq": {
"type": "object",
"required": [
"cooperative_number"
],
"properties": {
"cooperative_number": {
"description": "合作商编号",
"type": "string"
},
"page": {
"description": "页码",
"type": "integer"
},
"page_size": {
"description": "每页条数",
"type": "integer"
}
}
},
"bus_models.QueryCooperativeProductsResp": {
"type": "object",
"properties": {
"list": {
"description": "产品信息列表",
"type": "array",
"items": {
"$ref": "#/definitions/bus_models.ProductInfo"
}
},
"page": {
"description": "当前页码",
"type": "integer"
},
"page_size": {
"description": "每页条数",
"type": "integer"
},
"total": {
"description": "总条数",
"type": "integer"
}
}
},
"bus_models.SetCooperativeStatusReq": {
"type": "object",
"required": [
@ -3908,6 +4077,25 @@
}
}
},
"bus_models.UpdateProductDiscountReq": {
"type": "object",
"required": [
"cooperative_number",
"products"
],
"properties": {
"cooperative_number": {
"description": "合作商编号",
"type": "string"
},
"products": {
"type": "array",
"items": {
"$ref": "#/definitions/bus_models.ProductDiscount"
}
}
}
},
"dto.GetSetSysConfigReq": {
"type": "object",
"properties": {

View File

@ -23,7 +23,7 @@ definitions:
description: 合作商编号
type: string
gift_amount:
description: 赠送金额(仅加款时有效)
description: 赠送金额
type: number
remark:
description: 备注信息,例如交易编号、支付凭证等
@ -35,7 +35,6 @@ definitions:
description: 交易类型1-加款, 2-减款)
type: integer
required:
- amount
- cooperative_number
- transaction_type
type: object
@ -299,10 +298,8 @@ definitions:
type: string
required:
- account
- contact
- cooperative_name
- password
- tel
type: object
bus_models.CreateCooperativeResp:
properties:
@ -499,6 +496,48 @@ definitions:
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
type: object
bus_models.ProductInfo:
properties:
city:
type: string
description:
type: string
discount:
type: number
platform:
type: integer
price:
type: number
product_code:
type: string
product_id:
description: 产品ID
type: integer
product_name:
type: string
product_type:
type: integer
province:
type: string
retail_price:
type: number
size:
type: string
type:
type: integer
type: object
bus_models.ProductListReq:
properties:
city:
@ -543,6 +582,37 @@ definitions:
description: 总记录数
type: integer
type: object
bus_models.QueryCooperativeProductsReq:
properties:
cooperative_number:
description: 合作商编号
type: string
page:
description: 页码
type: integer
page_size:
description: 每页条数
type: integer
required:
- cooperative_number
type: object
bus_models.QueryCooperativeProductsResp:
properties:
list:
description: 产品信息列表
items:
$ref: '#/definitions/bus_models.ProductInfo'
type: array
page:
description: 当前页码
type: integer
page_size:
description: 每页条数
type: integer
total:
description: 总条数
type: integer
type: object
bus_models.SetCooperativeStatusReq:
properties:
cooperative_number:
@ -558,6 +628,19 @@ definitions:
- cooperative_number
- status
type: object
bus_models.UpdateProductDiscountReq:
properties:
cooperative_number:
description: 合作商编号
type: string
products:
items:
$ref: '#/definitions/bus_models.ProductDiscount'
type: array
required:
- cooperative_number
- products
type: object
dto.GetSetSysConfigReq:
properties:
configKey:
@ -1780,6 +1863,27 @@ paths:
summary: 查询合作商列表
tags:
- 合作商管理-V1.0.0
/api/v1/cooperative/products:
post:
consumes:
- application/json
parameters:
- description: 查询合作商产品模型
in: body
name: request
required: true
schema:
$ref: '#/definitions/bus_models.QueryCooperativeProductsReq'
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/bus_models.QueryCooperativeProductsResp'
summary: 查询合作商产品列表
tags:
- 合作商管理-V1.0.0
/api/v1/cooperative/status:
post:
consumes:
@ -1801,6 +1905,27 @@ paths:
summary: 设置合作商状态
tags:
- 合作商管理-V1.0.0
/api/v1/cooperative/update_discount:
post:
consumes:
- application/json
parameters:
- description: 批量更新折扣请求
in: body
name: request
required: true
schema:
$ref: '#/definitions/bus_models.UpdateProductDiscountReq'
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