fusenapi/model/gmodel/fs_order_logic.go
2023-08-03 17:37:25 +08:00

178 lines
5.1 KiB
Go
Executable File

package gmodel
import (
"context"
"fusenapi/constants"
"reflect"
"time"
"fusenapi/utils/handlers"
"gorm.io/gorm"
)
func (o *FsOrderModel) FindOneBySn(ctx context.Context, userId int64, sn string) (resp *FsOrder, err error) {
err = o.db.WithContext(ctx).Model(&FsOrder{}).Where(" `user_id` = ? and `sn` = ? ", userId, sn).Take(&resp).Error
return resp, err
}
func (o *FsOrderModel) FindOne(ctx context.Context, userId int64, OrderId int64) (order *FsOrder, err error) {
err = o.db.WithContext(ctx).Model(&FsOrder{}).Where("`user_id` = ? and `id` = ?", userId, OrderId).Take(&order).Error
if err != nil {
return nil, err
}
return
}
func (o *FsOrderModel) Update(ctx context.Context, data *FsOrder) error {
return o.db.WithContext(ctx).Model(&FsOrder{}).Where("`id` = ?", data.Id).Updates(&data).Error
}
func (o *FsOrderModel) Create(ctx context.Context, data *FsOrder) error {
return o.db.WithContext(ctx).Model(&FsOrder{}).Create(&data).Error
}
func (o *FsOrderModel) FindOneAndCreateServiceContact(ctx context.Context, userId int64, OrderId int64, cs *FsContactService) (order *FsOrder, err error) {
err = o.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
err = tx.Model(order).Select("id").Limit(1).Where("`user_id` = ? and `id` = ?", userId, OrderId).Take(&order).Error
if err != nil {
return err
}
ctime := time.Now().Unix()
cs.Ctime = &ctime
if constants.ConcactService(*cs.Type) == constants.TYPE_DEFAULT {
*cs.RelationId = 0
}
return tx.Model(cs).Create(cs).Error
})
return order, err
}
// 获取用户最近下单成功的订单
func (o *FsOrderModel) FindLastSuccessOneOrder(ctx context.Context, userId int64, statusGt int64) (order *FsOrder, err error) {
err = o.db.WithContext(ctx).Model(&FsOrder{}).Where("`user_id` = ? and `status` > ?", userId, statusGt).Order("id DESC").Take(&order).Error
return order, err
}
// 分页查询的订单
func (o *FsOrderModel) FindPageListByPage(ctx context.Context, rowBuilder *gorm.DB, page *int64, pageSize *int64, filterMap map[string]string, orderBy string) ([]*FsOrderRel, error) {
var resp []*FsOrderRel
// 过滤
if filterMap != nil {
rowBuilder = rowBuilder.Scopes(handlers.FilterData(filterMap))
}
// 排序
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{}{}
}
rowBuilder = rowBuilder.Scopes(handlers.OrderCheck(orderBy, fieldsMap))
}
// 分页
rowBuilder = rowBuilder.Scopes(handlers.Paginate(page, pageSize))
// 结果
result := rowBuilder.Debug().WithContext(ctx).Find(&resp)
if result.Error != nil {
return nil, result.Error
} else {
return resp, nil
}
}
type FsOrderRel struct {
FsOrder
FsOrderDetails []FsOrderDetails `gorm:"foreignKey:order_id;references:id"`
FsOrderAffiliateInfo FsOrderAffiliate `gorm:"foreignKey:order_id;references:id"`
}
type FsOrderDetails struct {
FsOrderDetail
FsOrderDetailTemplateInfo FsOrderDetailTemplateInfo `gorm:"foreignKey:id;references:order_detail_template_id"`
FsProductInfo FsProduct `gorm:"foreignKey:id;references:product_id"`
}
type FsOrderDetailTemplateInfo struct {
FsOrderDetailTemplate
FsProductDesignInfo FsProductDesignInfo `gorm:"foreignKey:id;references:design_id"` //获取设计数据
FsProductSizeInfo FsProductSize `gorm:"foreignKey:id;references:size_id"`
}
type FsProductDesignInfo struct {
FsProductDesign
OptionData FsProduct `gorm:"foreignKey:id;references:optional_id"` //获取配件信息
TemplateData FsProductTemplateV2 `gorm:"foreignKey:id;references:template_id"` //获取模板信息
}
func (m *FsOrderModel) 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 *FsOrderModel) FindCount(ctx context.Context, countBuilder *gorm.DB, filterMap map[string]string) (int64, error) {
var count int64
// 过滤
if filterMap != nil {
countBuilder = countBuilder.Scopes(handlers.FilterData(filterMap))
}
result := countBuilder.WithContext(ctx).Limit(1).Count(&count)
if result.Error != nil {
return 0, result.Error
} else {
return count, nil
}
}
func (m *FsOrderModel) FindOneByQuery(ctx context.Context, rowBuilder *gorm.DB, filterMap map[string]string) (*FsOrderRel, error) {
var resp FsOrderRel
if filterMap != nil {
rowBuilder = rowBuilder.Scopes(handlers.FilterData(filterMap))
}
result := rowBuilder.WithContext(ctx).Limit(1).Find(&resp)
if result.Error != nil {
return nil, result.Error
} else {
return &resp, nil
}
}
// 事务
func (m *FsOrderModel) 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
}
func (m *FsOrderModel) TableName() string {
return m.name
}