fusenapi/model/gmodel/fs_cloud_pick_up_logic.go
2023-08-28 14:21:06 +08:00

81 lines
2.3 KiB
Go

package gmodel
import (
"context"
"fusenapi/constants"
"time"
"gorm.io/gorm"
)
// TODO: 使用model的属性做你想做的
func (p *FsCloudPickUpModel) SavePickUpWithTransaction(ctx context.Context, pickUpData *FsCloudPickUp, stockList []FsUserStock, pickUpDetailAddList []FsCloudPickUpDetail) error {
return p.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
//保存总提单信息
if err := tx.Model(&FsCloudPickUp{}).Create(&pickUpData).Error; err != nil {
return err
}
//更新云仓库存
for _, v := range stockList {
if err := tx.Model(&FsUserStock{}).Where("`id` = ?", v.Id).Updates(&v).Error; err != nil {
return err
}
}
//添加提单详情
for _, v := range pickUpDetailAddList {
v.PickId = &pickUpData.Id //外面没赋值在这需要赋值
if err := tx.Model(&FsCloudPickUpDetail{}).Create(&v).Error; err != nil {
return err
}
}
return nil
})
}
func (p *FsCloudPickUpModel) GetCloudPickUpByIDAndUserID(ctx context.Context, userId int64, RelationID int64, cs *FsContactService) (cloudOrder *FsCloudPickUp, err error) {
err = p.db.WithContext(ctx).Model(cloudOrder).Transaction(func(tx *gorm.DB) error {
err = tx.Model(cloudOrder).Select("id").Limit(1).Where("`user_id` = ? and `id` = ?", userId, RelationID).Take(&cloudOrder).Error
if err != nil {
return err
}
ctime := time.Now().UTC().Unix()
cs.Ctime = &ctime
if constants.ConcactService(*cs.Type) == constants.TYPE_DEFAULT {
*cs.RelationId = 0
}
return tx.Model(cs).Create(cs).Error
})
return cloudOrder, err
}
type GetPickupListByParamReq struct {
UserId *int64
Status *int64
Ids []int64
Page int
Limit int
}
func (p *FsCloudPickUpModel) GetPickupListByParam(ctx context.Context, req GetPickupListByParamReq) (resp []FsCloudPickUp, total int64, err error) {
db := p.db.WithContext(ctx).Model(&FsCloudPickUp{})
if req.UserId != nil {
db = db.Where("`user_id` = ?", *req.UserId)
}
if req.Status != nil {
db = db.Where("`status` = ?", *req.Status)
}
if len(req.Ids) > 0 {
db = db.Where("`id` in (?)", req.Ids)
}
if err = db.Limit(1).Count(&total).Error; err != nil {
return nil, 0, err
}
offset := (req.Page - 1) * req.Limit
if err = db.Offset(offset).Limit(req.Limit).Order("id desc").Find(&resp).Error; err != nil {
return nil, 0, err
}
return
}