car.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package dao
  2. import (
  3. "Cold_Logistic/internal/pkg/utils/sqlutil"
  4. "Cold_Logistic/internal/server/infra/models"
  5. "context"
  6. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/core"
  7. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
  8. )
  9. // CarStore 车辆表
  10. type CarStore interface {
  11. DbBaseStore
  12. GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.Car, error)
  13. Page(ctx context.Context, dto CarPageDTO) ([]CarPageVO, int64, error)
  14. FindByUUId(ctx context.Context, uuid string) (models.Car, error)
  15. CountByCarNumberOrSnCode(ctx context.Context, carNumber, snCode string, exclude ...int) (int64, error)
  16. FindSnCodeById(ctx context.Context, id int) (string, error)
  17. }
  18. var _ CarStore = &car{}
  19. type car struct {
  20. dbBase
  21. }
  22. func newCar(ds *DataStore) *car {
  23. return &car{dbBase: dbBase{
  24. store: ds,
  25. baseEntity: &models.Car{},
  26. }}
  27. }
  28. func (ds *DataStore) Car() CarStore {
  29. return newCar(ds)
  30. }
  31. func (a *car) GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.Car, error) {
  32. res := make(map[int]models.Car, len(ids))
  33. if len(ids) == 0 {
  34. return res, nil
  35. }
  36. var list []models.Car
  37. db := a.store.optionDB(ctx)
  38. err := db.Table(a.baseEntity.TableName()).
  39. Where("deleted = ?", models.DeleteNo).
  40. Where("id IN (?)", ids).
  41. Omit(omit...).
  42. Find(&list).Error
  43. if err != nil {
  44. return res, err
  45. }
  46. for i := range list {
  47. res[list[i].Id] = list[i]
  48. }
  49. return res, nil
  50. }
  51. type CarPageDTO struct {
  52. Page core.Page
  53. CarNumber string
  54. SnCode string
  55. DriverAccountUuid string
  56. }
  57. type CarPageVO struct {
  58. CarId int `gorm:"column:id" json:"carId"` //
  59. CarNumber string `gorm:"column:car_number" json:"carNumber"` //车牌号
  60. SnCode string `gorm:"column:sn_code" json:"snCode"` //sn编码
  61. DriverUuid string `gorm:"column:driver_uuid" json:"driverUuid"` //司机uuid
  62. DriverName string `gorm:"column:name" json:"driverName"` //司机
  63. CarType int `gorm:"column:car_type" json:"carType"` //车辆类型
  64. }
  65. func (a *car) Page(ctx context.Context, dto CarPageDTO) ([]CarPageVO, int64, error) {
  66. db := a.store.optionDB(ctx, a.store.withCompanyDataPermis(ctx, "car.pid"))
  67. stmt := db.Table(a.baseEntity.TableName()).
  68. Joins("LEFT JOIN account ON account.id = car.driver_account_id").
  69. Where("car.deleted = ?", models.DeleteNo)
  70. stmt.Select("car.id, car.car_number, car.sn_code, car.driver_uuid, account.name, car.car_type")
  71. if dto.CarNumber != "" {
  72. stmt.Where("car.car_number LIKE ?", sqlutil.LikeFormat(dto.CarNumber))
  73. }
  74. if dto.SnCode != "" {
  75. stmt.Where("car.sn_code LIKE ?", sqlutil.LikeFormat(dto.SnCode))
  76. }
  77. if dto.DriverAccountUuid != "" {
  78. stmt.Where("car.driver_uuid LIKE ?", sqlutil.LikeFormat(dto.DriverAccountUuid))
  79. }
  80. var total int64
  81. var ret []CarPageVO
  82. if err := stmt.Count(&total).Error; err != nil {
  83. return nil, 0, err
  84. }
  85. if total == 0 {
  86. return ret, total, nil
  87. }
  88. stmt.Limit(dto.Page.Size).Offset(dto.Page.Offset)
  89. stmt.Order("car.id desc")
  90. if err := stmt.Find(&ret).Error; err != nil {
  91. return nil, 0, err
  92. }
  93. return ret, total, nil
  94. }
  95. func (a *car) FindByUUId(ctx context.Context, uuid string) (models.Car, error) {
  96. ret := models.Car{}
  97. err := a.store.optionDB(ctx).Model(&models.Car{}).
  98. Where("deleted = ?", models.DeleteNo).
  99. Where("driver_uuid = ?", uuid).
  100. Find(&ret).Error
  101. return ret, errors.WithStackOnce(err)
  102. }
  103. func (a *car) CountByCarNumberOrSnCode(ctx context.Context, carNumber, snCode string, exclude ...int) (int64, error) {
  104. var ret int64
  105. stmt := a.store.optionDB(ctx).Model(&models.Car{}).
  106. Where("deleted = ?", models.DeleteNo).
  107. Where("(car_number = ? OR sn_code = ?)", carNumber, snCode)
  108. if len(exclude) > 0 {
  109. stmt.Where("id NOT IN (?)", exclude)
  110. }
  111. err := stmt.Count(&ret).Error
  112. return ret, errors.WithStackOnce(err)
  113. }
  114. func (a *car) FindSnCodeById(ctx context.Context, id int) (string, error) {
  115. var snCode string
  116. stmt := a.store.optionDB(ctx).Model(&models.Car{}).
  117. Where("deleted = ?", models.DeleteNo).
  118. Where("id = ?", id)
  119. err := stmt.Pluck("sn_code", &snCode).Error
  120. return snCode, errors.WithStackOnce(err)
  121. }