2023-06-15 11:14:19 +00:00
|
|
|
package gmodel
|
|
|
|
|
2023-07-26 11:23:16 +00:00
|
|
|
import (
|
|
|
|
"context"
|
2023-08-03 09:37:25 +00:00
|
|
|
"fusenapi/utils/handlers"
|
2023-07-28 11:03:36 +00:00
|
|
|
"reflect"
|
2023-07-26 11:23:16 +00:00
|
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
2023-06-15 11:14:19 +00:00
|
|
|
|
|
|
|
func (p *FsPayModel) GetListByOrderNumber(ctx context.Context, sn string) (resp []FsPay, err error) {
|
|
|
|
err = p.db.WithContext(ctx).Model(&FsPay{}).Where("`order_number` = ? ", sn).Find(&resp).Error
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
2023-06-20 11:36:28 +00:00
|
|
|
|
2023-06-16 10:24:31 +00:00
|
|
|
func (p *FsPayModel) GetOrderPayList(ctx context.Context, sn string, payStatus int64, isRefund int64) (resp []FsPay, err error) {
|
|
|
|
err = p.db.WithContext(ctx).Model(&FsPay{}).Where("`order_number` = ? and `pay_status` = ? and `is_refund` = ?", sn, payStatus, isRefund).Find(&resp).Error
|
2023-06-20 11:36:28 +00:00
|
|
|
return resp, err
|
2023-06-16 10:24:31 +00:00
|
|
|
}
|
2023-07-26 03:06:05 +00:00
|
|
|
|
|
|
|
func (p *FsPayModel) GetListByOrderNumberStage(ctx context.Context, sn string, stage int64) (resp *FsPay, err error) {
|
|
|
|
err = p.db.Table(p.name).WithContext(ctx).Model(&FsPay{}).Where("`order_number` = ? ", sn).Where("`pay_stage` = ? ", stage).Take(&resp).Error
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *FsPayModel) CreateOrUpdate(ctx context.Context, req *FsPay) (resp *FsPay, err error) {
|
|
|
|
rowBuilder := p.db.Table(p.name).WithContext(ctx)
|
|
|
|
if req.Id > 0 {
|
|
|
|
err = rowBuilder.Save(req).Error
|
|
|
|
} else {
|
|
|
|
err = rowBuilder.Create(req).Error
|
|
|
|
}
|
|
|
|
return req, err
|
|
|
|
}
|
|
|
|
|
2023-07-26 11:23:16 +00:00
|
|
|
func (m *FsPayModel) RowSelectBuilder(selectData []string) *gorm.DB {
|
|
|
|
var rowBuilder = m.db.Table(m.name)
|
|
|
|
|
|
|
|
if selectData != nil {
|
|
|
|
rowBuilder = rowBuilder.Select(selectData)
|
|
|
|
} else {
|
|
|
|
rowBuilder = rowBuilder.Select("*")
|
|
|
|
}
|
|
|
|
return rowBuilder
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *FsPayModel) FindCount(ctx context.Context, countBuilder *gorm.DB, filterMap map[string]string) (int64, error) {
|
|
|
|
var count int64
|
|
|
|
|
|
|
|
// 过滤
|
|
|
|
if filterMap != nil {
|
2023-08-03 09:37:25 +00:00
|
|
|
countBuilder = countBuilder.Scopes(handlers.FilterData(filterMap))
|
2023-07-26 11:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result := countBuilder.WithContext(ctx).Limit(1).Count(&count)
|
|
|
|
if result.Error != nil {
|
|
|
|
return 0, result.Error
|
|
|
|
} else {
|
|
|
|
return count, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *FsPayModel) FindOneByQuery(ctx context.Context, rowBuilder *gorm.DB, filterMap map[string]string) (*FsPay, error) {
|
|
|
|
var resp FsPay
|
|
|
|
|
|
|
|
if filterMap != nil {
|
2023-08-03 09:37:25 +00:00
|
|
|
rowBuilder = rowBuilder.Scopes(handlers.FilterData(filterMap))
|
2023-07-26 11:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result := rowBuilder.WithContext(ctx).Limit(1).Find(&resp)
|
|
|
|
if result.Error != nil {
|
|
|
|
return nil, result.Error
|
|
|
|
} else {
|
|
|
|
return &resp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-28 11:03:36 +00:00
|
|
|
func (m *FsPayModel) FindAll(ctx context.Context, rowBuilder *gorm.DB, filterMap map[string]string, orderBy string) ([]*FsPay, error) {
|
|
|
|
var resp []*FsPay
|
|
|
|
// 过滤
|
|
|
|
if filterMap != nil {
|
2023-08-03 09:37:25 +00:00
|
|
|
rowBuilder = rowBuilder.Scopes(handlers.FilterData(filterMap))
|
2023-07-28 11:03:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 排序
|
|
|
|
if orderBy != "" {
|
|
|
|
var fieldsMap = make(map[string]struct{})
|
|
|
|
s := reflect.TypeOf(&FsOrder{}).Elem() //通过反射获取type定义
|
|
|
|
for i := 0; i < s.NumField(); i++ {
|
|
|
|
fieldsMap[s.Field(i).Tag.Get("json")] = struct{}{}
|
|
|
|
}
|
2023-08-03 09:37:25 +00:00
|
|
|
rowBuilder = rowBuilder.Scopes(handlers.OrderCheck(orderBy, fieldsMap))
|
2023-07-28 11:03:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result := rowBuilder.WithContext(ctx).Find(&resp)
|
|
|
|
if result.Error != nil {
|
|
|
|
return nil, result.Error
|
|
|
|
} else {
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-26 11:23:16 +00:00
|
|
|
// 事务
|
|
|
|
func (m *FsPayModel) Trans(ctx context.Context, fn func(ctx context.Context, connGorm *gorm.DB) error) error {
|
|
|
|
tx := m.db.Table(m.name).WithContext(ctx).Begin()
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := tx.Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := fn(ctx, tx); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit().Error
|
|
|
|
}
|
|
|
|
|
2023-07-26 03:06:05 +00:00
|
|
|
func (m *FsPayModel) TableName() string {
|
|
|
|
return m.name
|
|
|
|
}
|