123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- package shanghu
- import (
- orm "duoduo/database"
- "errors"
- "fmt"
- "github.com/shopspring/decimal"
- "time"
- )
- type MerchantAccount struct {
- ID int64 `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"` // 主键
- MerchantOpenID string `gorm:"column:merchant_open_id;type:varchar(255)" json:"merchant_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 *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
- }
- func (m *MerchantAccount) Create() (MerchantAccount, error) {
- var doc MerchantAccount
- var err error
- doc = *m
- err = orm.ShMysql.Table(m.TableName()).Create(&doc).Error
- if err != nil {
- return doc, err
- }
- return doc, nil
- }
- // 拼团核销与分账
- func (m *MerchantAccount) ActiveCancelSettle(log []ActiveCancelLog, merchantAccount MerchantAccount, activeClientAccount ActiveClientAccount, payTrans ClientActivePayTrans) error {
- var err error
- var merchantAccountLog MerchantAccountLog
- var clientAccountLog ActiveClientAccountLog
- fmt.Println("merchantAccount =", merchantAccount)
- fmt.Println("activeClientAccount =", activeClientAccount)
- err = orm.ShMysql.Table("merchant_account").Select("*").Where("merchant_open_id = ?", merchantAccount.MerchantOpenID).First(&merchantAccount).Error
- if err != nil {
- return err
- }
- if payTrans.InvitationCode != YuanShiMa {
- err = orm.ShMysql.Table("active_client_account").Select("*").Where("client_open_id = ?", activeClientAccount.ClientOpenID).First(&activeClientAccount).Error
- if err != nil {
- return err
- }
- }
- fmt.Println("merchantAccount =", merchantAccount)
- fmt.Println("activeClientAccount =", activeClientAccount)
- tx := orm.ShMysql.Begin()
- defer func() {
- if err != nil {
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- //核销日志
- for i := 0; i < len(log); i++ {
- err = tx.Table(log[i].TableName()).Create(&log[i]).Error
- if err != nil {
- return err
- }
- }
- //分账
- //商家加款
- merchantAmountAdd := merchantAccount.Amount.Add(payTrans.MerchantAmount)
- result := tx.Table("merchant_account").Model(&merchantAccount).Where("merchant_open_id = ? and version = ?", merchantAccount.MerchantOpenID, merchantAccount.Version).Updates(
- map[string]interface{}{
- "amount": merchantAmountAdd,
- "version": merchantAccount.Version + 1,
- "updated_at": time.Now()})
- if result.Error != nil {
- err = result.Error
- return err
- }
- if result.RowsAffected <= 0 {
- err = errors.New("rows is zero")
- return err
- }
- merchantAccountLog.MerchantOpenID = merchantAccount.MerchantOpenID
- merchantAccountLog.UpdatedAt = time.Now()
- merchantAccountLog.AmountPre = merchantAccount.Amount
- merchantAccountLog.AmountAfter = merchantAmountAdd
- merchantAccountLog.ReviewAmountAfter = merchantAccount.ReviewAmount
- merchantAccountLog.ReviewAmountPre = merchantAccount.ReviewAmount
- merchantAccountLog.Amount = payTrans.MerchantAmount
- merchantAccountLog.PayTransId = payTrans.ID
- merchantAccountLog.TransType = MerchantAccountLogTransTypeGroupBuy
- err = tx.Table("merchant_account_log").Create(&merchantAccountLog).Error
- if err != nil {
- return err
- }
- //err = tx.Table("merchant_account_log").Create(&merchantAccountLog).Error
- //if err != nil {
- // return err
- //}
- // 客户加款
- if payTrans.InvitationCode != YuanShiMa && payTrans.InvitationCode != "" {
- clientAmountAdd := activeClientAccount.Amount.Add(payTrans.ClientAmount)
- clientResult := tx.Table("active_client_account").Model(&activeClientAccount).Where("client_open_id = ? and version = ?", activeClientAccount.ClientOpenID, activeClientAccount.Version).Updates(
- map[string]interface{}{
- "amount": clientAmountAdd,
- "version": activeClientAccount.Version + 1,
- "updated_at": time.Now()})
- if clientResult.Error != nil {
- err = clientResult.Error
- return err
- }
- if clientResult.RowsAffected <= 0 {
- err = errors.New("rows is zero")
- return err
- }
- clientAccountLog.ClientOpenID = activeClientAccount.ClientOpenID
- clientAccountLog.UpdatedAt = time.Now()
- clientAccountLog.AmountPre = activeClientAccount.Amount
- clientAccountLog.AmountAfter = clientAmountAdd
- clientAccountLog.ReviewAmountAfter = activeClientAccount.ReviewAmount
- clientAccountLog.ReviewAmountPre = activeClientAccount.ReviewAmount
- clientAccountLog.Amount = payTrans.ClientAmount
- clientAccountLog.PayTransID = payTrans.ID
- clientAccountLog.TransType = ActiveClientAccountLogTransTypeGroupBuy
- err = tx.Table("active_client_account_log").Create(&clientAccountLog).Error
- if err != nil {
- return err
- }
- }
- //更新状态
- var activePay ClientActivePayTrans
- resultPay := tx.Table("client_active_pay_trans").Model(&activePay).Where("id = ? ", payTrans.ID).Updates(
- map[string]interface{}{
- "account_status": ClientActivePayTransAccountStatusSettleSuccess,
- "updated_at": time.Now()})
- if resultPay.Error != nil {
- err = resultPay.Error
- return err
- }
- if resultPay.RowsAffected <= 0 {
- err = errors.New("pay rows is zero")
- return err
- }
- return nil
- }
|