Переглянути джерело

Merge branch 'develop' of k.zhang/duoduo into master

k.zhang 1 рік тому
батько
коміт
82f7786fef

+ 1 - 0
apis/shanghu/base.go

@@ -32,5 +32,6 @@ func InitShangHuRouter(engine *gin.RouterGroup) {
 		v1.POST("/client/pay/list", GetPayTransList)                        //交易列表
 		v1.POST("/verification/code", GetVerificationCode)                  //核销码
 		v1.POST("/merchant/sale/list", GetMerchantPayTransList)             //销售记录
+		v1.POST("/merchant/cancel", CancelNumber)                           //核销
 	}
 }

+ 1 - 0
apis/shanghu/client.trans.go

@@ -92,6 +92,7 @@ func GetVerificationCode(c *gin.Context) {
 
 	qrData.MerchantId = transInfo.MerchantCardID
 	qrData.Key = key
+	qrData.ClientOpenId = transInfo.ClientOpenID
 	str, err := json.Marshal(&qrData)
 	if err != nil {
 		app.Error(c, 500, err, err.Error())

+ 69 - 0
apis/shanghu/merchant.card.go

@@ -9,6 +9,7 @@ import (
 	"duoduo/tools/app"
 	"encoding/base64"
 	"encoding/json"
+	"errors"
 	"fmt"
 	"github.com/gin-gonic/gin"
 	"github.com/shopspring/decimal"
@@ -445,3 +446,71 @@ func UpdateMerchantCard(c *gin.Context) {
 
 	app.OK(c, nil, app.Success)
 }
+
+// 核销次数
+func CancelNumber(c *gin.Context) {
+	var inData models.CancelCardRequest
+	var sqlData shanghu.MerchantCard
+	var log shanghu.CancelLog
+	var qrMessage models.QRData
+	var clientPay shanghu.ClientPayTrans
+
+	err := c.ShouldBindJSON(&inData)
+	if err != nil {
+		app.Error(c, 400, err, err.Error())
+		return
+	}
+
+	err = json.Unmarshal([]byte(inData.QRMessage), &qrMessage)
+	if err != nil {
+		app.Error(c, 400, err, err.Error())
+		return
+	}
+
+	clientPay.MerchantCardID = qrMessage.MerchantId
+	clientPay.ClientOpenID = qrMessage.ClientOpenId
+	clientInfo, err := clientPay.GetPayTransByOpenid()
+	if err != nil {
+		app.Error(c, 400, err, err.Error())
+		return
+	}
+
+	if clientInfo.ID == 0 {
+		app.Error(c, 200, errors.New("用户未购买此卡"), "用户未购买此卡")
+		return
+	}
+
+	sqlData.ID = qrMessage.MerchantId
+	merchantInfo, err := sqlData.GetMerchantCard()
+	if err != nil {
+		app.Error(c, 500, err, err.Error())
+		return
+	}
+
+	log.MerchantID = qrMessage.MerchantId
+	log.ClientOpenID = qrMessage.ClientOpenId
+	if log.GetNumber() >= merchantInfo.CancelNumber {
+		app.Error(c, 200, errors.New("卡已使用完,请重新购买"), "卡已使用完,请重新购买")
+		return
+	}
+
+	if merchantInfo.MerchantOpenID != inData.MerchantOpenId {
+		app.Error(c, 200, errors.New("核销商家与发卡商家不一致"), "核销商家与发卡商家不一致")
+		return
+	}
+
+	log.MerchantID = qrMessage.MerchantId
+	log.ClientOpenID = qrMessage.ClientOpenId
+	log.MerchantOpenID = inData.MerchantOpenId
+	log.CreatedAt = time.Now()
+	log.UpdatedAt = time.Now()
+
+	_, err = log.Create()
+	if err != nil {
+		app.Error(c, 500, err, err.Error())
+		return
+	}
+
+	app.OK(c, nil, "核销成功")
+
+}

+ 3 - 2
apis/shanghu/models/client.trans.go

@@ -25,8 +25,9 @@ type VerificationCodeRequest struct {
 }
 
 type QRData struct {
-	MerchantId int64  `json:"merchant_id"`
-	Key        string `json:"key"`
+	MerchantId   int64  `json:"merchant_id"`
+	Key          string `json:"key"`
+	ClientOpenId string `json:"client_open_id"`
 }
 
 type VerificationCodeReply struct {

+ 5 - 0
apis/shanghu/models/merchant.go

@@ -42,3 +42,8 @@ type UpdateMerchantRequest struct {
 	BusinessLicenseUrl string `json:"business_license_url"` // 营业执照 url
 	DoorHeaderUrl      string `json:"door_header_url"`      // 门头照 url
 }
+
+type CancelCardRequest struct {
+	MerchantOpenId string `json:"merchant_open_id"`
+	QRMessage      string `json:"qr_message"`
+}

+ 43 - 0
models/shanghu/cancel_log.go

@@ -0,0 +1,43 @@
+package shanghu
+
+import (
+	orm "duoduo/database"
+	"time"
+)
+
+type CancelLog struct {
+	ID             int64     `gorm:"column:id;type:bigint(20)" json:"id"` // 主键
+	ClientOpenID   string    `gorm:"column:client_open_id;type:varchar(255)" json:"client_open_id"`
+	MerchantOpenID string    `gorm:"column:merchant_open_id;type:varchar(255)" json:"merchant_open_id"`
+	MerchantID     int64     `gorm:"column:merchant_id;type:bigint(20)" json:"merchant_id"` // 商户卡id
+	UpdateBy       int64     `gorm:"column:update_by;type:bigint(20)" json:"update_by"`
+	CreateBy       int64     `gorm:"column:create_by;type:bigint(20)" json:"create_by"`
+	CreatedAt      time.Time `gorm:"column:created_at;type:datetime" json:"created_at"`
+	UpdatedAt      time.Time `gorm:"column:updated_at;type:datetime" json:"updated_at"`
+	DeletedAt      time.Time `gorm:"column:deleted_at;type:datetime" json:"deleted_at"`
+}
+
+func (m *CancelLog) TableName() string {
+	return "cancel_log"
+}
+
+func (u *CancelLog) Create() (CancelLog, error) {
+	var doc CancelLog
+	var err error
+
+	doc = *u
+	err = orm.ShMysql.Table(u.TableName()).Create(&doc).Error
+	if err != nil {
+		return doc, err
+	}
+
+	return doc, nil
+}
+
+func (u *CancelLog) GetNumber() int {
+	var count int
+
+	tableCount := orm.ShMysql.Table(u.TableName()).Where("client_open_id = ? and merchant_id = ?", u.ClientOpenID, u.MerchantID)
+	tableCount.Count(&count)
+	return count
+}

+ 6 - 5
models/shanghu/merchant_card.go

@@ -20,11 +20,12 @@ type MerchantCard struct {
 	UseRule           string    `gorm:"column:use_rule;type:varchar(255)" json:"use_rule"`                          // 使用规则
 	Picture           string    `gorm:"column:picture;type:varchar(255)" json:"picture"`                            // 图片
 	RebateRate        int64     `gorm:"column:rebate_rate;type:int(11)" json:"rebate_rate"`                         // 佣金比例
-	CreateBy          int64     `gorm:"column:create_by;type:bigint(20)" json:"create_by"`                          // 创建者
-	UpdateBy          int64     `gorm:"column:update_by;type:bigint(20)" json:"update_by"`                          // 更新者
-	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"`          // 删除时间
+	CancelNumber      int       `gorm:"column:cancel_number;type:int(11)" json:"cancel_number"`
+	CreateBy          int64     `gorm:"column:create_by;type:bigint(20)" json:"create_by"`                 // 创建者
+	UpdateBy          int64     `gorm:"column:update_by;type:bigint(20)" json:"update_by"`                 // 更新者
+	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"` // 删除时间
 }
 
 func (m *MerchantCard) TableName() string {