car_log.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package dao
  2. import (
  3. "Cold_Logistic/internal/pkg/common/global"
  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. "time"
  9. )
  10. // CarLogStore 车辆表
  11. type CarLogStore interface {
  12. DbBaseStore
  13. BatchSave(ctx context.Context, values []*models.CarLog, omit ...string) error
  14. GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.CarLog, error)
  15. Page(ctx context.Context, dto CarLogPageDTO) ([]CarLogPageVO, int64, error)
  16. FindAllByCarId(ctx context.Context, carId int) ([]models.CarLog, error)
  17. }
  18. var _ CarLogStore = &carLog{}
  19. type carLog struct {
  20. dbBase
  21. }
  22. func newCarLog(ds *DataStore) *carLog {
  23. return &carLog{dbBase: dbBase{
  24. store: ds,
  25. baseEntity: &models.CarLog{},
  26. }}
  27. }
  28. func (ds *DataStore) CarLog() CarLogStore {
  29. return newCarLog(ds)
  30. }
  31. func (a *carLog) BatchSave(ctx context.Context, values []*models.CarLog, omit ...string) error {
  32. if len(values) == 0 {
  33. return nil
  34. }
  35. db := a.store.optionDB(ctx)
  36. tokenInfo := global.GetTokenInfoFromContext(ctx)
  37. for i := range values {
  38. if values[i].Id == 0 {
  39. values[i].CreatedBy = tokenInfo.AccountId
  40. values[i].CreatedTime.Time = time.Now()
  41. values[i].Deleted = models.DeleteNo
  42. } else {
  43. values[i].UpdatedBy = tokenInfo.AccountId
  44. values[i].UpdatedTime.Time = time.Now()
  45. }
  46. }
  47. return db.Table(a.baseEntity.TableName()).Omit(omit...).Save(&values).Error
  48. }
  49. func (a *carLog) GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.CarLog, error) {
  50. res := make(map[int]models.CarLog, len(ids))
  51. if len(ids) == 0 {
  52. return res, nil
  53. }
  54. var list []models.CarLog
  55. db := a.store.optionDB(ctx)
  56. err := db.Table(a.baseEntity.TableName()).
  57. Where("deleted = ?", models.DeleteNo).
  58. Where("id IN (?)", ids).
  59. Omit(omit...).
  60. Find(&list).Error
  61. if err != nil {
  62. return res, err
  63. }
  64. for i := range list {
  65. res[list[i].Id] = list[i]
  66. }
  67. return res, nil
  68. }
  69. type CarLogPageDTO struct {
  70. Page core.Page
  71. Name string // 名称
  72. }
  73. type CarLogPageVO struct {
  74. Id int `gorm:"column:id" json:"id"` //
  75. DriverAccountId int `gorm:"column:driver_account_id" json:"driverAccountId"` //司机账号id
  76. HandoverTime models.MyTime `gorm:"column:handover_time" json:"handoverTime"` //交接时间
  77. }
  78. func (a *carLog) Page(ctx context.Context, dto CarLogPageDTO) ([]CarLogPageVO, int64, error) {
  79. db := a.store.optionDB(ctx)
  80. stmt := db.Model(&models.CarLog{}).
  81. Where("deleted = ?", models.DeleteNo)
  82. //stmt.Select("id")
  83. //if dto.Name != "" {
  84. // stmt.Where("name LIKE ?", "%"+dto.Name+"%")
  85. //}
  86. var total int64
  87. var ret []CarLogPageVO
  88. if err := stmt.Count(&total).Error; err != nil {
  89. return nil, 0, err
  90. }
  91. if total == 0 {
  92. return ret, total, nil
  93. }
  94. stmt.Limit(dto.Page.Size).Offset(dto.Page.Offset)
  95. stmt.Order("id desc")
  96. if err := stmt.Find(&ret).Error; err != nil {
  97. return nil, 0, err
  98. }
  99. return ret, total, nil
  100. }
  101. func (a *carLog) FindAllByCarId(ctx context.Context, carId int) ([]models.CarLog, error) {
  102. ret := make([]models.CarLog, 0)
  103. err := a.store.optionDB(ctx).Model(&models.CarLog{}).
  104. Where("deleted = ?", models.DeleteNo).
  105. Where("car_id = ?", carId).Find(&ret).Error
  106. return ret, errors.WithStackOnce(err)
  107. }