2023-06-16 07:11:37 +00:00
|
|
|
package gmodel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-06-29 10:04:59 +00:00
|
|
|
"fusenapi/constants"
|
2023-07-20 06:43:59 +00:00
|
|
|
"reflect"
|
2023-06-29 10:04:59 +00:00
|
|
|
"time"
|
2023-06-28 11:32:41 +00:00
|
|
|
|
2023-08-03 09:37:25 +00:00
|
|
|
"fusenapi/utils/handlers"
|
2023-07-20 06:43:59 +00:00
|
|
|
|
2023-06-28 11:32:41 +00:00
|
|
|
"gorm.io/gorm"
|
2023-06-16 07:11:37 +00:00
|
|
|
)
|
|
|
|
|
2023-06-20 09:28:28 +00:00
|
|
|
func (o *FsOrderModel) FindOneBySn(ctx context.Context, userId int64, sn string) (resp *FsOrder, err error) {
|
2023-06-21 04:34:57 +00:00
|
|
|
err = o.db.WithContext(ctx).Model(&FsOrder{}).Where(" `user_id` = ? and `sn` = ? ", userId, sn).Take(&resp).Error
|
2023-06-20 09:28:28 +00:00
|
|
|
return resp, err
|
2023-06-16 07:11:37 +00:00
|
|
|
}
|
2023-06-20 08:46:56 +00:00
|
|
|
|
2023-06-20 09:28:28 +00:00
|
|
|
func (o *FsOrderModel) FindOne(ctx context.Context, userId int64, OrderId int64) (order *FsOrder, err error) {
|
2023-06-28 09:05:31 +00:00
|
|
|
err = o.db.WithContext(ctx).Model(&FsOrder{}).Where("`user_id` = ? and `id` = ?", userId, OrderId).Take(&order).Error
|
2023-06-20 09:28:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return
|
2023-06-20 08:46:56 +00:00
|
|
|
}
|
|
|
|
|
2023-06-20 09:29:02 +00:00
|
|
|
func (o *FsOrderModel) Update(ctx context.Context, data *FsOrder) error {
|
2023-06-28 09:05:31 +00:00
|
|
|
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
|
2023-06-16 07:11:37 +00:00
|
|
|
}
|
2023-06-28 11:32:41 +00:00
|
|
|
|
2023-06-29 10:04:59 +00:00
|
|
|
func (o *FsOrderModel) FindOneAndCreateServiceContact(ctx context.Context, userId int64, OrderId int64, cs *FsContactService) (order *FsOrder, err error) {
|
2023-06-28 11:32:41 +00:00
|
|
|
err = o.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
2023-06-29 10:04:59 +00:00
|
|
|
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
|
2023-06-28 11:32:41 +00:00
|
|
|
})
|
|
|
|
return order, err
|
|
|
|
}
|
2023-07-06 09:43:07 +00:00
|
|
|
|
|
|
|
// 获取用户最近下单成功的订单
|
|
|
|
func (o *FsOrderModel) FindLastSuccessOneOrder(ctx context.Context, userId int64, statusGt int64) (order *FsOrder, err error) {
|
2023-07-17 09:08:45 +00:00
|
|
|
err = o.db.WithContext(ctx).Model(&FsOrder{}).Where("`user_id` = ? and `status` > ?", userId, statusGt).Order("id DESC").Take(&order).Error
|
2023-07-06 09:43:07 +00:00
|
|
|
return order, err
|
|
|
|
}
|
2023-07-20 06:43:59 +00:00
|
|
|
|
|
|
|
// 分页查询的订单
|
|
|
|
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 {
|
2023-08-03 09:37:25 +00:00
|
|
|
rowBuilder = rowBuilder.Scopes(handlers.FilterData(filterMap))
|
2023-07-20 06:43:59 +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-20 06:43:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 分页
|
2023-08-03 09:37:25 +00:00
|
|
|
rowBuilder = rowBuilder.Scopes(handlers.Paginate(page, pageSize))
|
2023-07-20 06:43:59 +00:00
|
|
|
|
|
|
|
// 结果
|
2023-07-24 10:04:24 +00:00
|
|
|
result := rowBuilder.Debug().WithContext(ctx).Find(&resp)
|
2023-07-20 06:43:59 +00:00
|
|
|
if result.Error != nil {
|
|
|
|
return nil, result.Error
|
|
|
|
} else {
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type FsOrderRel struct {
|
|
|
|
FsOrder
|
2023-07-20 09:49:09 +00:00
|
|
|
FsOrderDetails []FsOrderDetails `gorm:"foreignKey:order_id;references:id"`
|
|
|
|
FsOrderAffiliateInfo FsOrderAffiliate `gorm:"foreignKey:order_id;references:id"`
|
2023-07-20 06:43:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type FsOrderDetails struct {
|
|
|
|
FsOrderDetail
|
2023-07-24 10:04:24 +00:00
|
|
|
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"` //获取模板信息
|
2023-07-20 06:43:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-08-03 09:37:25 +00:00
|
|
|
countBuilder = countBuilder.Scopes(handlers.FilterData(filterMap))
|
2023-07-20 06:43:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result := countBuilder.WithContext(ctx).Limit(1).Count(&count)
|
|
|
|
if result.Error != nil {
|
|
|
|
return 0, result.Error
|
|
|
|
} else {
|
|
|
|
return count, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-26 11:23:16 +00:00
|
|
|
func (m *FsOrderModel) FindOneByQuery(ctx context.Context, rowBuilder *gorm.DB, filterMap map[string]string) (*FsOrderRel, error) {
|
|
|
|
var resp FsOrderRel
|
|
|
|
|
|
|
|
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-26 03:06:05 +00:00
|
|
|
// 事务
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-20 06:43:59 +00:00
|
|
|
func (m *FsOrderModel) TableName() string {
|
|
|
|
return m.name
|
|
|
|
}
|