package dao import ( "Cold_Logistic/internal/pkg/utils/sqlutil" "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" ) // CarStore 车辆表 type CarStore interface { DbBaseStore GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.Car, error) Page(ctx context.Context, dto CarPageDTO) ([]CarPageVO, int64, error) FindByUUId(ctx context.Context, uuid string) (models.Car, error) CountByCarNumberOrSnCode(ctx context.Context, carNumber, snCode string, exclude ...int) (int64, error) FindSnCodeById(ctx context.Context, id int) (string, error) } var _ CarStore = &car{} type car struct { dbBase } func newCar(ds *DataStore) *car { return &car{dbBase: dbBase{ store: ds, baseEntity: &models.Car{}, }} } func (ds *DataStore) Car() CarStore { return newCar(ds) } func (a *car) GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.Car, error) { res := make(map[int]models.Car, len(ids)) if len(ids) == 0 { return res, nil } var list []models.Car 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 CarPageDTO struct { Page core.Page CarNumber string SnCode string DriverAccountUuid string } type CarPageVO struct { CarId int `gorm:"column:id" json:"carId"` // CarNumber string `gorm:"column:car_number" json:"carNumber"` //车牌号 SnCode string `gorm:"column:sn_code" json:"snCode"` //sn编码 DriverUuid string `gorm:"column:driver_uuid" json:"driverUuid"` //司机uuid DriverName string `gorm:"column:name" json:"driverName"` //司机 CarType int `gorm:"column:car_type" json:"carType"` //车辆类型 } func (a *car) Page(ctx context.Context, dto CarPageDTO) ([]CarPageVO, int64, error) { db := a.store.optionDB(ctx, a.store.withCompanyDataPermis(ctx, "car.pid")) stmt := db.Table(a.baseEntity.TableName()). Joins("LEFT JOIN account ON account.id = car.driver_account_id"). Where("car.deleted = ?", models.DeleteNo) stmt.Select("car.id, car.car_number, car.sn_code, car.driver_uuid, account.name, car.car_type") if dto.CarNumber != "" { stmt.Where("car.car_number LIKE ?", sqlutil.LikeFormat(dto.CarNumber)) } if dto.SnCode != "" { stmt.Where("car.sn_code LIKE ?", sqlutil.LikeFormat(dto.SnCode)) } if dto.DriverAccountUuid != "" { stmt.Where("car.driver_uuid LIKE ?", sqlutil.LikeFormat(dto.DriverAccountUuid)) } var total int64 var ret []CarPageVO 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("car.id desc") if err := stmt.Find(&ret).Error; err != nil { return nil, 0, err } return ret, total, nil } func (a *car) FindByUUId(ctx context.Context, uuid string) (models.Car, error) { ret := models.Car{} err := a.store.optionDB(ctx).Model(&models.Car{}). Where("deleted = ?", models.DeleteNo). Where("driver_uuid = ?", uuid). Find(&ret).Error return ret, errors.WithStackOnce(err) } func (a *car) CountByCarNumberOrSnCode(ctx context.Context, carNumber, snCode string, exclude ...int) (int64, error) { var ret int64 stmt := a.store.optionDB(ctx).Model(&models.Car{}). Where("deleted = ?", models.DeleteNo). Where("(car_number = ? OR sn_code = ?)", carNumber, snCode) if len(exclude) > 0 { stmt.Where("id NOT IN (?)", exclude) } err := stmt.Count(&ret).Error return ret, errors.WithStackOnce(err) } func (a *car) FindSnCodeById(ctx context.Context, id int) (string, error) { var snCode string stmt := a.store.optionDB(ctx).Model(&models.Car{}). Where("deleted = ?", models.DeleteNo). Where("id = ?", id) err := stmt.Pluck("sn_code", &snCode).Error return snCode, errors.WithStackOnce(err) }