123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- package mysql
- import (
- orm "duoduo/database"
- "duoduo/tools"
- "github.com/shopspring/decimal"
- )
- type TransRecords struct {
- Amount decimal.Decimal `gorm:"column:amount" json:"amount"`
- CreateTime string `gorm:"column:create_time" json:"createTime"`
- OpenID string `gorm:"column:open_id" json:"openId"`
- TransStatus int `gorm:"column:trans_status" json:"transStatus"`
- UpdateTime string `gorm:"column:update_time" json:"updateTime"`
- Pid string `gorm:"column:pid" json:"pid"`
- PidStatus int `gorm:"column:pid_status" json:"pid_status"`
- Pay string `gorm:"column:pay" json:"pay"`
- UserId int `gorm:"column:user_id" json:"userId"`
- PayName string `gorm:"column:pay_name" json:"payName"`
- Id int64 `gorm:"column:id" json:"id"`
- }
- // TableName sets the insert table name for this struct type
- func (t *TransRecords) TableName() string {
- return "trans_records"
- }
- func (t *TransRecords) Create() (TransRecords, error) {
- var doc TransRecords
- result := orm.Eloquent.Table(t.TableName()).Create(&t)
- if result.Error != nil {
- err := result.Error
- return doc, err
- }
- doc = *t
- return doc, nil
- }
- func (t *TransRecords) GetAmount() (TransRecords, error) {
- var trans TransRecords
- //可体现金额:5求和-已体现金额
- //已体现金额:提现记录求和
- //历史总收益
- err := orm.Eloquent.Select("SUM(amount) as amount").Where("open_id = ? and trans_status = ? ", t.OpenID, t.TransStatus).Table(t.TableName()).First(&trans).Error
- if err != nil {
- return trans, err
- }
- return trans, nil
- }
- func (t *TransRecords) GetTransNum() int64 {
- var count int64
- tableCount := orm.Eloquent.Table(t.TableName()).Where("open_id = ? and trans_status = ?", t.OpenID, t.TransStatus)
- tableCount.Count(&count)
- return count
- }
- func (t *TransRecords) GetTransSum() (TransRecords, error) {
- var trans TransRecords
- err := orm.Eloquent.Select("SUM(amount) as amount").Where("open_id = ? and trans_status = ? and pid = ? and pid_status = ?", t.OpenID, t.TransStatus, t.Pid, t.PidStatus).Table(t.TableName()).First(&trans).Error
- if err != nil {
- return trans, err
- }
- return trans, nil
- }
- //
- func (t *TransRecords) GetTrans() (TransRecords, error) {
- var trans TransRecords
- err := orm.Eloquent.Select("SUM(amount) as amount").Where("open_id = ? and trans_status in (1,3) and pid = ? and pid_status = ?", t.OpenID, t.Pid, t.PidStatus).Table(t.TableName()).First(&trans).Error
- if err != nil {
- return trans, err
- }
- return trans, nil
- }
- //已发放金额求和
- func (t *TransRecords) GetTransStatus() (TransRecords, error) {
- var trans TransRecords
- err := orm.Eloquent.Select("SUM(amount) as amount").Where("open_id = ? and trans_status = 3 and pid = ? and pid_status = ?", t.OpenID, t.Pid, t.PidStatus).Table(t.TableName()).First(&trans).Error
- if err != nil {
- return trans, err
- }
- return trans, nil
- }
- func (t *TransRecords) Update(id int) (update TransRecords, err error) {
- if err := orm.Eloquent.Table(t.TableName()).Model(&update).Where("id = ? ", id).Updates(
- map[string]interface{}{
- "trans_status": t.TransStatus,
- "pay": t.Pay,
- "pay_name": t.PayName,
- "user_id": t.UserId,
- "update_time": tools.GetCurrntTimeStr()}).Error; err != nil {
- return update, err
- }
- return update, nil
- }
- //list 接口使用
- func (t *TransRecords) GetTransList(pageSize int, pageIndex int) ([]TransRecords, int, error) {
- var doc []TransRecords
- table := orm.Eloquent.Table(t.TableName())
- if t.OpenID != "" {
- table = table.Where("open_id = ? ", t.OpenID)
- }
- if t.PidStatus != 0 {
- table = table.Where("pid_status = ? ", t.PidStatus)
- }
- 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
- }
|