Browse Source

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

k.zhang 1 năm trước cách đây
mục cha
commit
bf77dd7b6e

+ 47 - 0
apis/shanghu/account.go

@@ -0,0 +1,47 @@
+package shanghu
+
+import (
+	"duoduo/apis/shanghu/models"
+	"duoduo/models/shanghu"
+	"duoduo/tools/app"
+	"github.com/gin-gonic/gin"
+)
+
+func GetAccountAmount(c *gin.Context) {
+	var inData models.GetAccountAmountRequest
+	var outData models.GetAccountAmountReply
+	var sqlMerchantData shanghu.MerchantAccount
+	var sqlClientData shanghu.MerchantClientAccount
+
+	err := c.ShouldBindJSON(&inData)
+	if err != nil {
+		app.Error(c, 400, err, err.Error())
+		return
+	}
+	if inData.Type == "merchant" {
+		sqlMerchantData.MerchantOpenID = inData.OpenId
+		merchantAmount, err := sqlMerchantData.GetMerchantAccount()
+		if err != nil {
+			app.Error(c, 500, err, err.Error())
+			return
+		}
+		outData.Amount = merchantAmount.Amount
+		app.OK(c, outData, app.Success)
+		return
+
+	} else if inData.Type == "client" {
+		sqlClientData.ClientOpenID = inData.OpenId
+		clientAccount, err := sqlClientData.GetClientAccount()
+		if err != nil {
+			app.Error(c, 500, err, err.Error())
+			return
+		}
+		outData.Amount = clientAccount.Amount
+		app.OK(c, outData, app.Success)
+		return
+	} else {
+		app.Error(c, 400, err, err.Error())
+		return
+	}
+
+}

+ 1 - 0
apis/shanghu/base.go

@@ -35,5 +35,6 @@ func InitShangHuRouter(engine *gin.RouterGroup) {
 		v1.POST("/merchant/cancel", CancelNumber)                           //核销
 		v1.POST("/merchant/update/whxy", UpdateMerchantCardWXYZ)            //xyz                               //更新坐标
 		v1.POST("/merchant/user/code", MerchantUserCode)                    //
+		v1.POST("/account/amount", GetAccountAmount)                        //获取金额
 	}
 }

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

@@ -40,6 +40,7 @@ func CreateMerchantCard(c *gin.Context) {
 
 	sqlData.CardProjectData = string(jsStr)
 	sqlData.CardPrice = inData.CardPrice
+	sqlData.CardTotalPrice = inData.CardTotalPrice
 	sqlData.ActivityEndTime, err = tools.TimeToInt64(inData.ActivityEndTime, "2006-01-02")
 	if err != nil {
 		app.Error(c, 400, err, err.Error())

+ 12 - 0
apis/shanghu/models/account.go

@@ -0,0 +1,12 @@
+package models
+
+import "github.com/shopspring/decimal"
+
+type GetAccountAmountRequest struct {
+	Type   string `json:"type"`
+	OpenId string `json:"open_id"`
+}
+
+type GetAccountAmountReply struct {
+	Amount decimal.Decimal `json:"amount"`
+}

+ 1 - 0
apis/shanghu/models/card.go

@@ -10,6 +10,7 @@ type CreateMerchantCardRequest struct {
 	MerchantOpenID    string  `json:"open_id"`             // openid
 	CardProjectData   []int64 `json:"card_project_data"`   // 项目
 	CardPrice         string  `json:"card_price"`          // 单价
+	CardTotalPrice    string  `json:"card_total_price"`    // 价格
 	ActivityEndTime   string  `json:"activity_end_time"`   // 活动结束时间
 	ActivityStartTime string  `json:"activity_start_time"` // 活动开始时间
 	MerchantCardTime  string  `json:"merchant_card_time"`  // 商户卡有效期

+ 1 - 0
apis/shanghu/pay.go

@@ -190,6 +190,7 @@ func PayCallBack(c *gin.Context) {
 	payTrans.ThirdTradeNo = wxNotify.Get("transaction_id")
 	payTrans.Status = 2 //支付成功
 	payTrans.PayTime = timeEnd
+	payTrans.AccountStatus = 1 //未分账
 
 	err = payTrans.UpdatePayTransByTradeNo()
 	if err != nil {

+ 25 - 10
models/shanghu/client.account.go

@@ -1,23 +1,38 @@
 package shanghu
 
 import (
+	orm "duoduo/database"
 	"github.com/shopspring/decimal"
 	"time"
 )
 
 type MerchantClientAccount struct {
-	ID           int64           `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"` // 主键
-	ClientOpenID string          `gorm:"column:client_open_id;type:varchar(255)" json:"client_open_id"`  // openid
-	ReviewAmount decimal.Decimal `gorm:"column:review_amount;type:decimal(10,2)" json:"review_amount"`   // 审核金额
-	Amount       decimal.Decimal `gorm:"column:amount;type:decimal(10,2)" json:"amount"`                 // 可提现金额
-	Version      int             `gorm:"column:version;type:int(11)" json:"version"`                     // 创建者
-	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)" json:"deleted_at"`           // 删除时间
+	ID           int64           `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"`    // 主键
+	ClientOpenID string          `gorm:"column:client_open_id;type:varchar(255)" json:"client_open_id"`     // openid
+	ReviewAmount decimal.Decimal `gorm:"column:review_amount;type:decimal(10,2)" json:"review_amount"`      // 审核金额
+	Amount       decimal.Decimal `gorm:"column:amount;type:decimal(10,2)" json:"amount"`                    // 可提现金额
+	Version      int             `gorm:"column:version;type:int(11)" json:"version"`                        // 创建者
+	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 *MerchantClientAccount) TableName() string {
 	return "merchant_client_account"
 }
+
+func (m *MerchantClientAccount) GetClientAccount() (MerchantClientAccount, error) {
+	var doc MerchantClientAccount
+
+	table := orm.ShMysql.Table(m.TableName())
+	table = table.Where("client_open_id = ?  ", m.ClientOpenID)
+
+	if err := table.Select("*").First(&doc).Error; err != nil {
+		return doc, err
+	}
+
+	return doc, nil
+
+}

+ 1 - 1
models/shanghu/client.account.log.go

@@ -17,7 +17,7 @@ type MerchantClientAccountLog struct {
 	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)" json:"deleted_at"`                     // 删除时间
+	DeletedAt         time.Time       `gorm:"column:deleted_at;type:datetime(3);default:null" json:"deleted_at"`        // 删除时间
 }
 
 func (m *MerchantClientAccountLog) TableName() string {

+ 1 - 1
models/shanghu/client.card.go

@@ -19,7 +19,7 @@ type MerchantClientCard struct {
 	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)" json:"deleted_at"`                  // 删除时间
+	DeletedAt        time.Time `gorm:"column:deleted_at;type:datetime(3);default:null" json:"deleted_at"`     // 删除时间
 }
 
 func (m *MerchantClientCard) TableName() string {

+ 8 - 8
models/shanghu/client.card.use.log.go

@@ -6,14 +6,14 @@ import (
 
 type MerchantClientCardUseLog struct {
 	ID             int64     `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"`
-	MerchantCardID int64     `gorm:"column:merchant_card_id;type:bigint(20)" json:"merchant_card_id"` // 商户卡id
-	MerchantOpenID int64     `gorm:"column:merchant_open_id;type:bigint(20)" json:"merchant_open_id"` // 核销人
-	ClinetOpenID   string    `gorm:"column:clinet_open_id;type:varchar(255)" json:"clinet_open_id"`   // Openid
-	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)" json:"deleted_at"`            // 删除时间
+	MerchantCardID int64     `gorm:"column:merchant_card_id;type:bigint(20)" json:"merchant_card_id"`   // 商户卡id
+	MerchantOpenID int64     `gorm:"column:merchant_open_id;type:bigint(20)" json:"merchant_open_id"`   // 核销人
+	ClinetOpenID   string    `gorm:"column:clinet_open_id;type:varchar(255)" json:"clinet_open_id"`     // Openid
+	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 *MerchantClientCardUseLog) TableName() string {

+ 16 - 1
models/shanghu/merchant.account.go

@@ -1,6 +1,7 @@
 package shanghu
 
 import (
+	orm "duoduo/database"
 	"github.com/shopspring/decimal"
 	"time"
 )
@@ -15,9 +16,23 @@ type MerchantAccount struct {
 	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)" json:"deleted_at"`              // 删除时间
+	DeletedAt      time.Time       `gorm:"column:deleted_at;type:datetime(3);default:null" json:"deleted_at"` // 删除时间
 }
 
 func (m *MerchantAccount) TableName() string {
 	return "merchant_account"
 }
+
+func (m *MerchantAccount) GetMerchantAccount() (MerchantAccount, error) {
+	var doc MerchantAccount
+
+	table := orm.ShMysql.Table(m.TableName())
+	table = table.Where("merchant_open_id = ?  ", m.MerchantOpenID)
+
+	if err := table.Select("*").First(&doc).Error; err != nil {
+		return doc, err
+	}
+
+	return doc, nil
+
+}

+ 1 - 1
models/shanghu/merchant.account.log.go

@@ -17,7 +17,7 @@ type MerchantAccountLog struct {
 	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)" json:"deleted_at"`                     // 删除时间
+	DeletedAt         time.Time       `gorm:"column:deleted_at;type:datetime(3);default:null" json:"deleted_at"`        // 删除时间
 }
 
 func (m *MerchantAccountLog) TableName() string {

+ 1 - 0
models/shanghu/merchant_card.go

@@ -31,6 +31,7 @@ type MerchantCard struct {
 	H                 string    `gorm:"column:h;type:varchar(255);default:null" json:"h"`                           // h
 	X                 string    `gorm:"column:x;type:varchar(255);default:null" json:"x"`                           // x
 	Y                 string    `gorm:"column:y;type:varchar(255);default:null" json:"y"`                           // y
+	CardTotalPrice    string    `gorm:"column:card_total_price;type:decimal(10,2)" json:"card_total_price"`         // 总价
 }
 
 func (m *MerchantCard) TableName() string {

+ 2 - 1
models/shanghu/pay.go

@@ -98,6 +98,7 @@ func (m *ClientPayTrans) UpdatePayTransByTradeNo() error {
 			"third_trade_no": m.ThirdTradeNo,
 			"pay_time":       m.PayTime,
 			"status":         m.Status,
+			"account_status": m.AccountStatus,
 			"updated_at":     time.Now()}).Error; err != nil {
 		return err
 	}
@@ -261,7 +262,7 @@ func (m *ClientPayTrans) SettleAdd(merchantAmount, clientAmount decimal.Decimal,
 	clientAccountLog.ReviewAmountAfter = clientAccount.ReviewAmount
 	clientAccountLog.ReviewAmountPre = clientAccount.ReviewAmount
 
-	err = tx.Table("merchant_client_account").Create(&clientAccountLog).Error
+	err = tx.Table("merchant_client_account_log").Create(&clientAccountLog).Error
 	if err != nil {
 		return err
 	}

+ 8 - 6
report/report.go

@@ -1,13 +1,15 @@
 package report
 
+import "github.com/robfig/cron"
+
 const (
-	cronSettDateCutoff = "0 0 1 * * *"    //每天凌晨1:00:00
-	cronRunStart       = "0 0 17 * * *"   //凌晨一点
-	cronMinuteStart    = "0 */20 * * * *" //每10分钟
+	cronSettDateCutoff = "0 0 1 * * *"   //每天凌晨1:00:00
+	cronRunStart       = "0 0 17 * * *"  //凌晨一点
+	cronMinuteStart    = "0 */1 * * * *" //每10分钟
 )
 
 func SysCronStart() {
-	//c := cron.New()
-	//c.AddFunc(cronMinuteStart, OrderList) //每分钟同步一下订单
-	//c.Start()
+	c := cron.New()
+	c.AddFunc(cronMinuteStart, ClientAccount) //每分钟同步一下订单
+	c.Start()
 }

+ 20 - 3
report/shang.hu.ka.go

@@ -2,6 +2,7 @@ package report
 
 import (
 	"duoduo/models/shanghu"
+	"fmt"
 	"github.com/shopspring/decimal"
 	"time"
 )
@@ -49,9 +50,25 @@ func ClientAccount() {
 		//分账
 		//按比例商户一部分,分销一部分
 		//计算金额
-		clientAmount := account.Amount.Mul(decimal.NewFromInt(merchantCard.RebateRate)).Div(decimal.NewFromInt(100)).Round(2)
-		//merchantAmount := account.Amount.Sub(clientAmount).Round(2)
-		account.Amount.Sub(clientAmount).Round(2)
+		fmt.Println(merchantCard.RebateRate)
+		fmt.Println(clientTrans.Amount)
+		clientAmount := clientTrans.Amount.Mul(decimal.NewFromInt(merchantCard.RebateRate)).Div(decimal.NewFromInt(100)).Round(2)
+		fmt.Println(clientAmount)
+		merchantAmount := clientTrans.Amount.Sub(clientAmount).Round(2)
+		fmt.Println(merchantAmount)
+		//乐观锁
+		err = account.SettleAdd(merchantAmount, clientAmount, merchantCard.MerchantOpenID, clientTrans.ClientOpenID)
+		if err != nil {
+			dataErr := make(map[string]interface{})
+			dataErr["account_status"] = 2            //分账失败
+			dataErr["account_err_log"] = err.Error() //分账失败日志
+			dataErr["updated_at"] = time.Now()
+			err = clientTrans.UpdateById(dataErr)
+			if err != nil {
+				break
+			}
+			continue
+		}
 
 	}