package gmodel import ( "context" "fusenapi/constants" "reflect" "time" "fusenapi/utils/handler" "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(handler.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(handler.OrderCheck(orderBy, fieldsMap)) } // 分页 rowBuilder = rowBuilder.Scopes(handler.Paginate(page, pageSize)) // 结果 result := rowBuilder.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 FsOrderDetailTemplate `gorm:"foreignKey:id;references:order_detail_template_id"` FsProductInfo FsProduct `gorm:"foreignKey:id;references:product_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(handler.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) TableName() string { return m.name }