package dao import ( "Cold_Logistic/internal/pkg/common/global" "Cold_Logistic/internal/server/infra/models" "context" "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/core" "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors" "time" ) // CarLogStore 车辆表 type CarLogStore interface { DbBaseStore BatchSave(ctx context.Context, values []*models.CarLog, omit ...string) error GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.CarLog, error) Page(ctx context.Context, dto CarLogPageDTO) ([]CarLogPageVO, int64, error) FindAllByCarId(ctx context.Context, carId int) ([]models.CarLog, error) } var _ CarLogStore = &carLog{} type carLog struct { dbBase } func newCarLog(ds *DataStore) *carLog { return &carLog{dbBase: dbBase{ store: ds, baseEntity: &models.CarLog{}, }} } func (ds *DataStore) CarLog() CarLogStore { return newCarLog(ds) } func (a *carLog) BatchSave(ctx context.Context, values []*models.CarLog, omit ...string) error { if len(values) == 0 { return nil } db := a.store.optionDB(ctx) tokenInfo := global.GetTokenInfoFromContext(ctx) for i := range values { if values[i].Id == 0 { values[i].CreatedBy = tokenInfo.AccountId values[i].CreatedTime.Time = time.Now() values[i].Deleted = models.DeleteNo } else { values[i].UpdatedBy = tokenInfo.AccountId values[i].UpdatedTime.Time = time.Now() } } return db.Table(a.baseEntity.TableName()).Omit(omit...).Save(&values).Error } func (a *carLog) GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.CarLog, error) { res := make(map[int]models.CarLog, len(ids)) if len(ids) == 0 { return res, nil } var list []models.CarLog db := a.store.optionDB(ctx) err := db.Table(a.baseEntity.TableName()). Where("deleted = ?", models.DeleteNo). Where("id IN (?)", ids). Omit(omit...). Find(&list).Error if err != nil { return res, err } for i := range list { res[list[i].Id] = list[i] } return res, nil } type CarLogPageDTO struct { Page core.Page Name string // 名称 } type CarLogPageVO struct { Id int `gorm:"column:id" json:"id"` // DriverAccountId int `gorm:"column:driver_account_id" json:"driverAccountId"` //司机账号id HandoverTime models.MyTime `gorm:"column:handover_time" json:"handoverTime"` //交接时间 } func (a *carLog) Page(ctx context.Context, dto CarLogPageDTO) ([]CarLogPageVO, int64, error) { db := a.store.optionDB(ctx) stmt := db.Model(&models.CarLog{}). Where("deleted = ?", models.DeleteNo) //stmt.Select("id") //if dto.Name != "" { // stmt.Where("name LIKE ?", "%"+dto.Name+"%") //} var total int64 var ret []CarLogPageVO if err := stmt.Count(&total).Error; err != nil { return nil, 0, err } if total == 0 { return ret, total, nil } stmt.Limit(dto.Page.Size).Offset(dto.Page.Offset) stmt.Order("id desc") if err := stmt.Find(&ret).Error; err != nil { return nil, 0, err } return ret, total, nil } func (a *carLog) FindAllByCarId(ctx context.Context, carId int) ([]models.CarLog, error) { ret := make([]models.CarLog, 0) err := a.store.optionDB(ctx).Model(&models.CarLog{}). Where("deleted = ?", models.DeleteNo). Where("car_id = ?", carId).Find(&ret).Error return ret, errors.WithStackOnce(err) }