| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 | 
							- package shanghu
 
- import (
 
- 	orm "duoduo/database"
 
- 	"github.com/shopspring/decimal"
 
- 	"time"
 
- )
 
- type ClientActivePayTrans struct {
 
- 	ID             int64           `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"`    // 主键
 
- 	RequestID      string          `gorm:"column:request_id;type:varchar(255)" json:"request_id"`             // 请求id,幂等性
 
- 	OutTradeNo     string          `gorm:"column:out_trade_no;type:varchar(255)" json:"out_trade_no"`         // 交易id
 
- 	ActiveConfigID int64           `gorm:"column:active_config_id;type:bigint(20)" json:"active_config_id"`   // 活动id
 
- 	ClientOpenID   string          `gorm:"column:client_open_id;type:varchar(255)" json:"client_open_id"`     // 客户端openid
 
- 	Status         int             `gorm:"column:status;type:int(11)" json:"status"`                          // 1-未支付 2-支付成功 3-取消支付 4-退款
 
- 	ThirdTradeNo   string          `gorm:"column:third_trade_no;type:varchar(255)" json:"third_trade_no"`     // 微信交易id
 
- 	AccountStatus  int             `gorm:"column:account_status;type:int(11)" json:"account_status"`          // 分账状态 99-分账成功 2-分账失败 1-未分账 3-分账中
 
- 	PayTime        time.Time       `gorm:"column:pay_time;type:datetime(3);default:null" json:"pay_time"`     // 支付时间
 
- 	InvitationCode string          `gorm:"column:invitation_code;type:varchar(25)" json:"invitation_code"`    // 邀请码
 
- 	Amount         decimal.Decimal `gorm:"column:amount;type:decimal(10,2)" json:"amount"`                    // 交易金额
 
- 	AccountErrLog  string          `gorm:"column:account_err_log;type:varchar(255)" json:"account_err_log"`   // 分账err日志
 
- 	GroupBuyID     int64           `gorm:"column:group_buy_id;type:bigint(20)" json:"group_buy_id"`           // 团购id
 
- 	SeckillID      int64           `gorm:"column:seckill_id;type:bigint(20)" json:"seckill_id"`               // 秒杀id
 
- 	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"` // 删除时间
 
- 	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
 
- }
 
- const (
 
- 	ClientActivePayTransAccountStatusDef           = 0
 
- 	ClientActivePayTransAccountStatusUnSettle      = 1  //未分账
 
- 	ClientActivePayTransAccountStatusSettleFail    = 2  //分账失败
 
- 	ClientActivePayTransAccountStatusSettlePending = 3  //分账中
 
- 	ClientActivePayTransAccountStatusSettleSuccess = 99 //分账成功
 
- )
 
- const (
 
- 	ClientActivePayTransStatusDef        = 0
 
- 	ClientActivePayTransStatusUnPay      = 1 // 未支付
 
- 	ClientActivePayTransStatusPaySuccess = 2 //支付成功
 
- 	ClientActivePayTransStatusPayCancel  = 3 //取消支付
 
- 	ClientActivePayTransStatusRefund     = 4 // 退款
 
- )
 
- const (
 
- 	YuanShiMa = "yuanshima" //
 
- )
 
- func (m *ClientActivePayTrans) TableName() string {
 
- 	return "client_active_pay_trans"
 
- }
 
- func (m *ClientActivePayTrans) GetRequestNum() int {
 
- 	var count int
 
- 	tableCount := orm.ShMysql.Table(m.TableName()).Where("request_id = ? ", m.RequestID)
 
- 	tableCount.Count(&count)
 
- 	return count
 
- }
 
- func (m *ClientActivePayTrans) Create() (ClientActivePayTrans, error) {
 
- 	var doc ClientActivePayTrans
 
- 	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 *ClientActivePayTrans) GetPayTransByTradeNo() (ClientActivePayTrans, error) {
 
- 	var doc ClientActivePayTrans
 
- 	table := orm.ShMysql.Table(m.TableName())
 
- 	table = table.Where("out_trade_no = ?  ", m.OutTradeNo)
 
- 	if err := table.Select("*").First(&doc).Error; err != nil {
 
- 		return doc, err
 
- 	}
 
- 	return doc, nil
 
- }
 
- func (m *ClientActivePayTrans) UpdatePayTransByTradeNo() error {
 
- 	if err := orm.ShMysql.Table(m.TableName()).Model(&m).Where("out_trade_no = ? ", m.OutTradeNo).Updates(
 
- 		map[string]interface{}{
 
- 			"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
 
- 	}
 
- 	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, 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 { // 待核销 核销次数大于0
 
- 	//	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("核销类型错误")
 
- 	//}
 
- 	table = table.Where("client_open_id = ? and status = 2 ", openId)
 
- 	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
 
- }
 
- func (m *ClientActivePayTrans) GetPayTransByThirdTradeNo() (ClientActivePayTrans, error) {
 
- 	var doc ClientActivePayTrans
 
- 	table := orm.ShMysql.Table(m.TableName())
 
- 	table = table.Where("third_trade_no = ?  ", m.ThirdTradeNo)
 
- 	if err := table.Select("*").First(&doc).Error; err != nil {
 
- 		return doc, err
 
- 	}
 
- 	return doc, nil
 
- }
 
- // 分账
 
- func (m *ClientActivePayTrans) TXSettle(payInfo ClientActivePayTrans) error {
 
- 	var err error
 
- 	tx := orm.ShMysql.Begin()
 
- 	defer func() {
 
- 		if err != nil {
 
- 			tx.Rollback()
 
- 		} else {
 
- 			tx.Commit()
 
- 		}
 
- 	}()
 
- 	//判断是否有这个商家账号,c端用户账号,没有用户需要创建
 
- 	//商家加款 c端用户加款
 
- 	return nil
 
- }
 
- func (m *ClientActivePayTrans) UpdateAccountStatusByID() error {
 
- 	if err := orm.ShMysql.Table(m.TableName()).Model(&m).Where("id = ? ", m.ID).Updates(
 
- 		map[string]interface{}{
 
- 			"account_status": m.Status,
 
- 			"updated_at":     time.Now()}).Error; err != nil {
 
- 		return err
 
- 	}
 
- 	return nil
 
- }
 
- func (m *ClientActivePayTrans) UpdateById(data map[string]interface{}) error {
 
- 	err := orm.ShMysql.Table(m.TableName()).Where("id = ?", m.ID).Updates(data).Error
 
- 	if err != nil {
 
- 		return err
 
- 	}
 
- 	return nil
 
- }
 
 
  |