Browse Source

Merge branch 'yun-group-buy' of k.zhang/duoduo into yun-test

k.zhang 2 months ago
parent
commit
5d42a34a6d

+ 119 - 0
apis/shanghu/active.pay.go

@@ -0,0 +1,119 @@
+package shanghu
+
+import (
+	"duoduo/apis/shanghu/models"
+	"duoduo/models/shanghu"
+	"duoduo/tools/app"
+	"errors"
+	"github.com/gin-gonic/gin"
+	"time"
+)
+
+// 待核销金额
+func PendingCancelAmount(c *gin.Context) {
+	var inData models.PendingCancelAmountRequest
+	var activePayTransSql shanghu.ClientActivePayTrans
+	var outData models.PendingCancelAmountReply
+
+	err := c.ShouldBindJSON(&inData)
+	if err != nil {
+		app.Error(c, 400, err, err.Error())
+		return
+	}
+
+	if inData.Type == 1 { //商家
+		activePayTransSql.MerchantOpenID = inData.OpenId
+		activeAmount, err := activePayTransSql.GetPendingCancelByMerchant()
+		if err != nil {
+			app.Error(c, 500, err, err.Error())
+			return
+		}
+		outData.Amount = activeAmount.MerchantAmount.String()
+
+	} else if inData.Type == 2 { //个人
+		activePayTransSql.ClientOpenID = inData.OpenId
+		activeAmount, err := activePayTransSql.GetPendingCancelByClient()
+		if err != nil {
+			app.Error(c, 500, err, err.Error())
+			return
+		}
+		outData.Amount = activeAmount.ClientAmount.String()
+
+	} else {
+		app.Error(c, 500, errors.New("type 类型错误"), err.Error())
+		return
+	}
+
+	app.OK(c, outData, app.Success)
+
+}
+
+// 拼团购买
+func GetGroupBuyPay(c *gin.Context) {
+	var inData models.GetGroupBuyPayRequest
+	var sqlData shanghu.ClientActivePayTrans
+	var outData []models.GetGroupBuyPayReply
+
+	err := c.ShouldBindJSON(&inData)
+	if err != nil {
+		app.Error(c, 400, err, err.Error())
+		return
+	}
+
+	// 不是商家也不是c端客户
+	if inData.UserType != 1 && inData.UserType != 2 {
+		app.Error(c, 500, errors.New("用户类型错误"), err.Error())
+		return
+	}
+
+	// 核销类型错误
+	if inData.GroupBuyType != 1 && inData.GroupBuyType != 2 {
+		app.Error(c, 500, errors.New("核销类型错误"), err.Error())
+		return
+	}
+
+	var pageSize = 10
+	var pageIndex = 1
+
+	if inData.PageSize != 0 {
+		pageSize = inData.PageSize
+	}
+	if inData.PageIndex != 0 {
+		pageIndex = inData.PageIndex
+	}
+
+	activeDrawLogList, count, err := sqlData.GetActivePayTransList(pageSize, pageIndex, inData.UserType, inData.GroupBuyType, inData.OpenId)
+	if err != nil {
+		app.Error(c, 500, err, err.Error())
+		return
+	}
+
+	for _, v := range activeDrawLogList {
+		var data models.GetGroupBuyPayReply
+		var activeSql shanghu.MerchantActiveConfig
+		var groupBuySql shanghu.MerchantActiveGroupBuy
+		data.MerchantAmount = v.MerchantAmount.String()
+		data.Amount = v.Amount.String()
+		data.ClientAmount = v.ClientAmount.String()
+		data.PayTime = v.PayTime.Format(time.DateTime)
+
+		activeSql.ID = v.ActiveConfigID
+		activeInfo, err := activeSql.GetConfigInfoById()
+		if err != nil {
+			app.Error(c, 500, err, err.Error())
+			return
+		}
+		data.ActiveName = activeInfo.ActiveName
+		groupBuySql.ID = v.GroupBuyID
+		groupBuyInfo, err := groupBuySql.GetMerchantActiveGroupBuyById()
+		if err != nil {
+			app.Error(c, 500, err, err.Error())
+			return
+		}
+		data.GroupBuyName = groupBuyInfo.GroupBuyName
+		data.PayTime = v.PayTime.Format(time.DateTime)
+		outData = append(outData, data)
+	}
+	app.PageOK(c, outData, count, pageIndex, pageSize, app.Success)
+
+}

+ 23 - 19
apis/shanghu/base.go

@@ -74,25 +74,29 @@ func InitShangHuRouter(engine *gin.RouterGroup) {
 
 	activeV1 := engine.Group("activeV1")
 	{
-		activeV1.POST("/active/draw/product/create", DrawProductCreate)   //抽奖奖品创建
-		activeV1.POST("/active/group/buy/create", GroupBuyCreate)         //拼团创建
-		activeV1.POST("/active/config/create", ActiveConfigCreate)        //创建活动
-		activeV1.POST("/active/config/list", ActiveConfigList)            //活动列表
-		activeV1.POST("/active/config/info", ActiveConfigInfo)            //活动详情
-		activeV1.POST("/active/draw/product/list", DrawProductList)       //奖品列表
-		activeV1.POST("/active/group/buy/list", GroupBuyList)             //拼团列表
-		activeV1.POST("/active/draw", Draw)                               //抽奖
-		activeV1.POST("/active/draw/list", DrawLog)                       //中奖列表
-		activeV1.POST("/draw/verification/code", DrawLogVerificationCode) //奖品核销码                                           //中奖核销码
-		activeV1.POST("/draw/cancel", DrawCancel)                         //核销 奖品与拼团
-		activeV1.POST("/active/config/whxy", UpdateActiveConfigWHXY)      //二维码坐标
-		activeV1.POST("/active/qr", GetClientActiveQR)                    //获取二维码
-		activeV1.POST("/active/unified/order", GroupByUnifiedOrder)       //拼团购买
-		activeV1.POST("/active/pay/callback", ActivePayCallBack)          //支付回调
-		activeV1.POST("/active/draw/num", InvitedDrawNum)                 //邀请次数
-		activeV1.POST("/client/user/set", ActiveOpenIdSet)                //活动 创建用户
-		//待核销金额
-		//
+		activeV1.POST("/active/draw/product/create", DrawProductCreate)     //抽奖奖品创建
+		activeV1.POST("/active/group/buy/create", GroupBuyCreate)           //拼团创建
+		activeV1.POST("/active/config/create", ActiveConfigCreate)          //创建活动
+		activeV1.POST("/active/config/list", ActiveConfigList)              //活动列表
+		activeV1.POST("/active/config/info", ActiveConfigInfo)              //活动详情
+		activeV1.POST("/active/draw/product/list", DrawProductList)         //奖品列表
+		activeV1.POST("/active/group/buy/list", GroupBuyList)               //拼团列表
+		activeV1.POST("/active/draw", Draw)                                 //抽奖
+		activeV1.POST("/active/draw/list", DrawLog)                         //中奖列表
+		activeV1.POST("/draw/verification/code", DrawLogVerificationCode)   //奖品核销码                                           //中奖核销码
+		activeV1.POST("/draw/cancel", DrawCancel)                           //核销 奖品与拼团
+		activeV1.POST("/active/config/whxy", UpdateActiveConfigWHXY)        //二维码坐标
+		activeV1.POST("/active/qr", GetClientActiveQR)                      //获取二维码
+		activeV1.POST("/active/unified/order", GroupByUnifiedOrder)         //拼团购买
+		activeV1.POST("/active/pay/callback", ActivePayCallBack)            //支付回调
+		activeV1.POST("/active/draw/num", InvitedDrawNum)                   //邀请次数
+		activeV1.POST("/client/user/set", ActiveOpenIdSet)                  //活动 创建用户
+		activeV1.POST("/active/pending.cancel/amount", PendingCancelAmount) //待核销金额  从交易记录里面求和, 扫码后核销,进入可提现余额
+		//拼团核销
+		//拼团核销码
+		activeV1.POST("/active/group.buy/pay", GetGroupBuyPay) //购买拼团列表
+		//提现
+		activeV1.POST("/active/draw/count", ActiveDrawCount) //邀请客户数量统计
 
 	}
 }

+ 25 - 1
apis/shanghu/client.active.draw.num.go

@@ -8,7 +8,7 @@ import (
 	"time"
 )
 
-// 邀请次数
+// 增加邀请次数
 func InvitedDrawNum(c *gin.Context) {
 	var inData models.InvitedDrawNumRequest
 	var sqlData shanghu.ClientActiveDrawNum
@@ -74,3 +74,27 @@ func InvitedDrawNum(c *gin.Context) {
 	app.OK(c, nil, app.Success)
 
 }
+
+func ActiveDrawCount(c *gin.Context) {
+	var inData models.ActiveDrawCountRequest
+	var sqlData shanghu.ClientActiveDrawNum
+	var outData models.ActiveDrawCountReply
+
+	err := c.ShouldBindJSON(&inData)
+	if err != nil {
+		app.Error(c, 400, err, err.Error())
+		return
+	}
+
+	sqlData.ClientOpenID = inData.OpenId
+	sqlData.ActiveConfigID = inData.ActiveConfigId
+	count, err := sqlData.GetClientDrawByOpenID()
+	if err != nil {
+		app.Error(c, 500, err, err.Error())
+		return
+	}
+
+	outData.Num = count
+	app.OK(c, outData, app.Success)
+
+}

+ 19 - 1
apis/shanghu/merchant.active.config.go

@@ -148,7 +148,7 @@ func ActiveConfigInfo(c *gin.Context) {
 		return
 	}
 
-	sqlData.MerchantOpenID = inData.MerchantOpenID
+	//sqlData.MerchantOpenID = inData.MerchantOpenID
 	sqlData.ID = inData.ActiveConfigId
 
 	configInfo, err := sqlData.GetConfigInfoById()
@@ -730,6 +730,7 @@ func GroupByUnifiedOrder(c *gin.Context) {
 	var sqlData shanghu.ClientActivePayTrans
 	var activeConfig shanghu.MerchantActiveConfig
 	var outData models.GroupBuyUnifiedOrderReply
+	var groupBuySql shanghu.MerchantActiveGroupBuy
 
 	err := c.ShouldBindJSON(&inData)
 	if err != nil {
@@ -773,6 +774,21 @@ func GroupByUnifiedOrder(c *gin.Context) {
 		return
 	}
 
+	//计算待核销金额
+	if inData.InvitationCode == "yuanshima" {
+		sqlData.MerchantAmount = inData.Amount
+		sqlData.ClientAmount = decimal.NewFromInt32(0)
+	} else {
+		groupBuySql.ID = inData.GroupBuyId
+		groupBuyInfo, err := groupBuySql.GetMerchantActiveGroupBuyById()
+		if err != nil {
+			app.Error(c, 400, err, err.Error())
+			return
+		}
+		sqlData.ClientAmount = inData.Amount.Mul(decimal.NewFromInt32(int32(groupBuyInfo.RebateRate))).Div(decimal.NewFromInt32(100))
+		sqlData.MerchantAmount = inData.Amount.Sub(sqlData.ClientAmount)
+	}
+
 	//创建支付记录
 	sqlData.ClientOpenID = inData.ClientOpenId
 	sqlData.RequestID = inData.RequestId
@@ -783,6 +799,8 @@ func GroupByUnifiedOrder(c *gin.Context) {
 	sqlData.Status = 1 //未支付
 	sqlData.ActiveConfigID = inData.ActiveConfigId
 	sqlData.InvitationCode = inData.InvitationCode
+	sqlData.GroupBuyID = inData.GroupBuyId //团购id
+	sqlData.MerchantOpenID = activeConfigInfo.MerchantOpenID
 	_, err = sqlData.Create()
 	if err != nil {
 		app.Error(c, 400, err, "创建支付失败")

+ 2 - 1
apis/shanghu/models/active.config.go

@@ -121,7 +121,8 @@ type ClientActiveQRRequest struct {
 type GroupBuyUnifiedOrderRequest struct {
 	RequestId      string          `json:"request_id"`      //request id
 	OutTradeNo     string          `json:"out_trade_no"`    //交易id
-	ActiveConfigId int64           `json:"activeConfigId"`  //
+	ActiveConfigId int64           `json:"activeConfigId"`  //活动id
+	GroupBuyId     int64           `json:"group_buy_id"`    //团购id
 	ClientOpenId   string          `json:"client_open_id"`  //openid
 	Amount         decimal.Decimal `json:"amount"`          //交易金额
 	InvitationCode string          `json:"invitation_code"` //邀请码

+ 28 - 0
apis/shanghu/models/active.pay.go

@@ -0,0 +1,28 @@
+package models
+
+type PendingCancelAmountRequest struct {
+	OpenId string `json:"open_id"`
+	Type   int    `json:"type"` //1-商家 2-客户
+}
+
+type PendingCancelAmountReply struct {
+	Amount string `json:"amount"`
+}
+
+type GetGroupBuyPayRequest struct {
+	OpenId       string `json:"open_id"`
+	UserType     int    `json:"user_type"` //1-商家 2-c端客户
+	GroupBuyType int    `json:"pay_type"`  //1-待核销 2-已核销
+	PageSize     int    `json:"page_size"`
+	PageIndex    int    `json:"page_index"`
+}
+
+type GetGroupBuyPayReply struct {
+	ActiveName     string `json:"active_name"`     //活动名称
+	GroupBuyName   string `json:"group_buy_name"`  //拼团名称
+	Amount         string `json:"amount"`          //交易金额
+	MerchantAmount string `json:"merchant_amount"` //商家金额
+	ClientAmount   string `json:"client_amount"`   //客户金额
+	PayTime        string `json:"pay_time"`        //支付时间
+
+}

+ 9 - 0
apis/shanghu/models/client.active.draw.num.go

@@ -5,3 +5,12 @@ type InvitedDrawNumRequest struct {
 	InvitedCode    string `json:"invited_code"`     //邀请码
 	ActiveConfigId int64  `json:"active_config_id"` //活动id
 }
+
+type ActiveDrawCountRequest struct {
+	OpenId         string `json:"open_id"`          //open-id
+	ActiveConfigId int64  `json:"active_config_id"` //活动id
+}
+
+type ActiveDrawCountReply struct {
+	Num int `json:"num"`
+}

+ 9 - 0
models/shanghu/client.active.draw.num.go

@@ -54,3 +54,12 @@ func (m *ClientActiveDrawNum) GetClientDrawNum() (int, error) {
 	table.Count(&count)
 	return count, nil
 }
+
+func (m *ClientActiveDrawNum) GetClientDrawByOpenID() (int, error) {
+	table := orm.ShMysql.Table(m.TableName())
+
+	table = table.Where("client_open_id = ? and active_config_id = ?  ", m.ClientOpenID, m.ActiveConfigID)
+	var count int
+	table.Count(&count)
+	return count, nil
+}

+ 62 - 0
models/shanghu/client.active.pay.trans.go

@@ -2,6 +2,7 @@ package shanghu
 
 import (
 	orm "duoduo/database"
+	"errors"
 	"github.com/shopspring/decimal"
 	"time"
 )
@@ -26,6 +27,10 @@ type ClientActivePayTrans struct {
 	CreatedAt      time.Time       `gorm:"column:created_at;type:datetime(3)" json:"created_at"`              // 创建时间
 	UpdatedAt      time.Time       `gorm:"column:updated_at;type:datetime(3)" json:"updated_at"`              // 最后更新时间
 	DeletedAt      time.Time       `gorm:"column:deleted_at;type:datetime(3);default:null" json:"deleted_at"` // 删除时间
+	MerchantAmount decimal.Decimal `gorm:"column:merchant_amount;type:decimal(10,2)" json:"merchant_amount"`  // 商家金额
+	ClientAmount   decimal.Decimal `gorm:"column:client_amount;type:decimal(10,2)" json:"client_amount"`      // 客户分销金额
+	MerchantOpenID string          `gorm:"column:merchant_open_id;type:varchar(255)" json:"merchant_open_id"` // 商家openid
+
 }
 
 func (m *ClientActivePayTrans) TableName() string {
@@ -80,3 +85,60 @@ func (m *ClientActivePayTrans) UpdatePayTransByTradeNo() error {
 	}
 	return nil
 }
+
+func (m *ClientActivePayTrans) GetPendingCancelByMerchant() (ClientActivePayTrans, error) {
+	var doc ClientActivePayTrans
+
+	table := orm.ShMysql.Table(m.TableName())
+	table = table.Where("status = 2 and  account_status = 1 and merchant_open_id = ?", m.MerchantOpenID)
+
+	if err := table.Select("sum(merchant_amount) as merchant_amount").First(&doc).Error; err != nil {
+		return doc, err
+	}
+
+	return doc, nil
+}
+
+func (m *ClientActivePayTrans) GetPendingCancelByClient() (ClientActivePayTrans, error) {
+	var doc ClientActivePayTrans
+
+	table := orm.ShMysql.Table(m.TableName())
+	table = table.Where("status = 2 and  account_status = 1 and client_open_id = ?", m.ClientOpenID)
+
+	if err := table.Select("sum(client_amount) as client_amount").First(&doc).Error; err != nil {
+		return doc, err
+	}
+
+	return doc, nil
+}
+
+// 交易记录
+func (m *ClientActivePayTrans) GetActivePayTransList(pageSize int, pageIndex int, userType int, groupBuyType int, openId string) ([]ClientActivePayTrans, int, error) {
+	var doc []ClientActivePayTrans
+
+	table := orm.ShMysql.Table(m.TableName())
+
+	if userType == 1 { // 商家
+		table = table.Where("merchant_open_id = ?", openId)
+
+	} else if userType == 2 { // c端用户
+		table = table.Where("client_open_id = ?", openId)
+	} else {
+		return doc, 0, errors.New("用户类型错误")
+	}
+
+	if groupBuyType == 1 { // 待核销
+		table = table.Where("status = 2 and account_status in (1,2)")
+	} else if groupBuyType == 2 { //已核销
+		table = table.Where("status = 2 and account_status = 99")
+	} else {
+		return doc, 0, errors.New("核销类型错误")
+	}
+
+	var count int
+	if err := table.Select("*").Order("id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
+		return nil, 0, err
+	}
+	table.Count(&count)
+	return doc, count, nil
+}

+ 13 - 0
models/shanghu/merchant.active.group.buy.go

@@ -92,3 +92,16 @@ func (m *MerchantActiveGroupBuy) GetGroupBuyListByOpenId(pageSize int, pageIndex
 	table.Count(&count)
 	return doc, count, nil
 }
+
+func (m *MerchantActiveGroupBuy) GetMerchantActiveGroupBuyById() (MerchantActiveGroupBuy, error) {
+	var doc MerchantActiveGroupBuy
+
+	table := orm.ShMysql.Table(m.TableName())
+
+	table = table.Where("id = ? ", m.ID)
+	if err := table.Select("*").Order("id desc").First(&doc).Error; err != nil {
+		return doc, err
+	}
+
+	return doc, nil
+}