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-07-20 06:43:59 +00:00
|
|
|
"fusenapi/utils/handler"
|
|
|
|
|
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 {
|
|
|
|
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
|
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
|
|
|
|
FsOrderDetailTemplateInfo FsOrderDetailTemplate `gorm:"foreignKey:id;references:order_detail_template_id"`
|
2023-07-20 09:49:09 +00:00
|
|
|
FsProductInfo FsProduct `gorm:"foreignKey:id;references:product_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 {
|
|
|
|
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
|
|
|
|
}
|