order_logistic_log.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package dao
  2. import (
  3. "Cold_Logistic/internal/server/infra/models"
  4. "context"
  5. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/core"
  6. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
  7. "time"
  8. )
  9. // OrderLogisticLogStore 订单物流日志表
  10. type OrderLogisticLogStore interface {
  11. DbBaseStore
  12. BatchSave(ctx context.Context, values []*models.OrderLogisticLog, omit ...string) error
  13. GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.OrderLogisticLog, error)
  14. Page(ctx context.Context, dto OrderLogisticLogPageDTO) ([]OrderLogisticLogPageVO, int64, error)
  15. FindAllByOrderId(ctx context.Context, orderId int) ([]models.OrderLogisticLog, error)
  16. }
  17. var _ OrderLogisticLogStore = &orderLogisticLog{}
  18. type orderLogisticLog struct {
  19. dbBase
  20. }
  21. func newOrderLogisticLog(ds *DataStore) *orderLogisticLog {
  22. return &orderLogisticLog{dbBase: dbBase{
  23. store: ds,
  24. baseEntity: &models.OrderLogisticLog{},
  25. }}
  26. }
  27. func (ds *DataStore) OrderLogisticLog() OrderLogisticLogStore {
  28. return newOrderLogisticLog(ds)
  29. }
  30. func (a *orderLogisticLog) BatchSave(ctx context.Context, values []*models.OrderLogisticLog, omit ...string) error {
  31. if len(values) == 0 {
  32. return nil
  33. }
  34. db := a.store.optionDB(ctx)
  35. //tokenInfo := ctxtoken.GetTokenInfoFromContext(ctx)
  36. for i := range values {
  37. if values[i].Id == 0 {
  38. //values[i].CreatedBy = tokenInfo.AccountId
  39. values[i].CreatedTime.Time = time.Now()
  40. values[i].Deleted = models.DeleteNo
  41. } else {
  42. //values[i].UpdatedBy = tokenInfo.AccountId
  43. values[i].UpdatedTime.Time = time.Now()
  44. }
  45. }
  46. return db.Table(a.baseEntity.TableName()).Omit(omit...).Save(&values).Error
  47. }
  48. func (a *orderLogisticLog) GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.OrderLogisticLog, error) {
  49. res := make(map[int]models.OrderLogisticLog, len(ids))
  50. if len(ids) == 0 {
  51. return res, nil
  52. }
  53. var list []models.OrderLogisticLog
  54. db := a.store.optionDB(ctx)
  55. err := db.Table(a.baseEntity.TableName()).
  56. Where("deleted = ?", models.DeleteNo).
  57. Where("id IN (?)", ids).
  58. Omit(omit...).
  59. Find(&list).Error
  60. if err != nil {
  61. return res, err
  62. }
  63. for i := range list {
  64. res[list[i].Id] = list[i]
  65. }
  66. return res, nil
  67. }
  68. type OrderLogisticLogPageDTO struct {
  69. Page core.Page
  70. Name string // 名称
  71. }
  72. type OrderLogisticLogPageVO struct {
  73. Id int `gorm:"column:id" json:"id"` //
  74. OrderId int `gorm:"column:order_id" json:"orderId"` //订单id
  75. OrderNo string `gorm:"column:order_no" json:"orderNo"` //订单号
  76. Location string `gorm:"column:location" json:"location"` //当前所在地
  77. Phone string `gorm:"column:phone" json:"phone"` //联系方式
  78. Transporter string `gorm:"column:transporter" json:"transporter"` //运输员
  79. }
  80. func (a *orderLogisticLog) Page(ctx context.Context, dto OrderLogisticLogPageDTO) ([]OrderLogisticLogPageVO, int64, error) {
  81. db := a.store.optionDB(ctx)
  82. stmt := db.Model(&models.OrderLogisticLog{}).
  83. Where("deleted = ?", models.DeleteNo)
  84. //stmt.Select("id")
  85. //if dto.Name != "" {
  86. // stmt.Where("name LIKE ?", "%"+dto.Name+"%")
  87. //}
  88. var total int64
  89. var ret []OrderLogisticLogPageVO
  90. if err := stmt.Count(&total).Error; err != nil {
  91. return nil, 0, err
  92. }
  93. if total == 0 {
  94. return ret, total, nil
  95. }
  96. stmt.Limit(dto.Page.Size).Offset(dto.Page.Offset)
  97. stmt.Order("id desc")
  98. if err := stmt.Find(&ret).Error; err != nil {
  99. return nil, 0, err
  100. }
  101. return ret, total, nil
  102. }
  103. func (a *orderLogisticLog) FindAllByOrderId(ctx context.Context, orderId int) ([]models.OrderLogisticLog, error) {
  104. ret := make([]models.OrderLogisticLog, 0, 3)
  105. err := a.store.optionDB(ctx).Model(&models.OrderLogisticLog{}).
  106. Where("deleted = ?", models.DeleteNo).
  107. Where("order_id = ?", orderId).
  108. Order("id DESC").
  109. Find(&ret).Error
  110. return ret, errors.WithStackOnce(err)
  111. }