2023-06-16 07:11:37 +00:00
|
|
|
package gmodel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-06-29 10:04:59 +00:00
|
|
|
"fusenapi/constants"
|
|
|
|
"time"
|
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) {
|
|
|
|
err = o.db.WithContext(ctx).Model(&FsOrder{}).Where("`user_id` = ? and `status` > ?", userId, statusGt).Order("id DESC").Find(&order).Error
|
|
|
|
return order, err
|
|
|
|
}
|