order_transport_task.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package dao
  2. import (
  3. "Cold_Logistic/internal/pkg/common/constant"
  4. "Cold_Logistic/internal/pkg/common/global"
  5. "Cold_Logistic/internal/pkg/utils/sqlutil"
  6. "Cold_Logistic/internal/server/infra/models"
  7. "context"
  8. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/core"
  9. "time"
  10. )
  11. // OrderTransportTaskStore 运单任务表
  12. type OrderTransportTaskStore interface {
  13. DbBaseStore
  14. BatchSave(ctx context.Context, values []*models.OrderTransportTask, omit ...string) error
  15. GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.OrderTransportTask, error)
  16. Page(ctx context.Context, dto OrderTransportTaskPageDTO) ([]OrderTransportTaskPageVO, int64, error)
  17. FinishedTask(ctx context.Context, id int) error
  18. }
  19. var _ OrderTransportTaskStore = &orderTransportTask{}
  20. type orderTransportTask struct {
  21. dbBase
  22. }
  23. func newOrderTransportTask(ds *DataStore) *orderTransportTask {
  24. return &orderTransportTask{dbBase: dbBase{
  25. store: ds,
  26. baseEntity: &models.OrderTransportTask{},
  27. }}
  28. }
  29. func (ds *DataStore) OrderTransportTask() OrderTransportTaskStore {
  30. return newOrderTransportTask(ds)
  31. }
  32. func (a *orderTransportTask) BatchSave(ctx context.Context, values []*models.OrderTransportTask, omit ...string) error {
  33. if len(values) == 0 {
  34. return nil
  35. }
  36. db := a.store.optionDB(ctx)
  37. tokenInfo := global.GetTokenInfoFromContext(ctx)
  38. for i := range values {
  39. if values[i].Id == 0 {
  40. values[i].CreatedBy = tokenInfo.AccountId
  41. values[i].CreatedTime.Time = time.Now()
  42. values[i].Deleted = models.DeleteNo
  43. } else {
  44. values[i].UpdatedBy = tokenInfo.AccountId
  45. values[i].UpdatedTime.Time = time.Now()
  46. }
  47. }
  48. return db.Table(a.baseEntity.TableName()).Omit(omit...).Save(&values).Error
  49. }
  50. func (a *orderTransportTask) GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.OrderTransportTask, error) {
  51. res := make(map[int]models.OrderTransportTask, len(ids))
  52. if len(ids) == 0 {
  53. return res, nil
  54. }
  55. var list []models.OrderTransportTask
  56. db := a.store.optionDB(ctx)
  57. err := db.Table(a.baseEntity.TableName()).
  58. Where("deleted = ?", models.DeleteNo).
  59. Where("id IN (?)", ids).
  60. Omit(omit...).
  61. Find(&list).Error
  62. if err != nil {
  63. return res, err
  64. }
  65. for i := range list {
  66. res[list[i].Id] = list[i]
  67. }
  68. return res, nil
  69. }
  70. type OrderTransportTaskPageDTO struct {
  71. Page core.Page
  72. OrderNo string
  73. Status int
  74. }
  75. type OrderTransportTaskPageVO struct {
  76. OrderId int `gorm:"column:id" json:"orderId"` //
  77. OrderNo string `gorm:"column:order_no" json:"orderNo"` // 单号
  78. Status int `gorm:"column:status" json:"status"` // 状态
  79. SenderName string `gorm:"column:sender_name" json:"senderName"` // 寄件人姓名
  80. SenderPhone string `gorm:"column:sender_phone" json:"senderPhone"` // 寄件人电话
  81. ProvinceName string `gorm:"column:province_name" json:"provinceName"` // 省中文名
  82. CityName string `gorm:"column:city_name" json:"cityName"` // 市中文名
  83. RegionName string `gorm:"column:region_name" json:"regionName"` // 区中文名
  84. Address string `gorm:"column:address" json:"address"` // 详细地址
  85. }
  86. func (a *orderTransportTask) Page(ctx context.Context, dto OrderTransportTaskPageDTO) ([]OrderTransportTaskPageVO, int64, error) {
  87. db := a.store.optionDB(ctx,
  88. a.store.withAccountDataPermis(ctx, "dro.driver_account_id"),
  89. a.store.withCompanyDataPermis(ctx, "eo.pid"))
  90. stmt := db.Table(a.baseEntity.TableName()+" AS dro").
  91. Joins("LEFT JOIN express_order AS eo ON eo.id = dro.order_id").
  92. Joins("LEFT JOIN address_book AS book ON book.id = eo.sender_address_id").
  93. Where("dro.deleted = ?", models.DeleteNo).
  94. Where("eo.deleted = ?", models.DeleteNo).
  95. Where("status = ?", dto.Status)
  96. stmt.Select("eo.id, eo.order_no, dro.status, book.name AS sender_name, " +
  97. "book.phone AS sender_phone, book.province_name, book.city_name, book.region_name, book.address ")
  98. if dto.OrderNo != "" {
  99. stmt.Where("eo.name LIKE ?", sqlutil.LikeFormat(dto.OrderNo))
  100. }
  101. var total int64
  102. var ret []OrderTransportTaskPageVO
  103. if err := stmt.Count(&total).Error; err != nil {
  104. return nil, 0, err
  105. }
  106. if total == 0 {
  107. return ret, total, nil
  108. }
  109. stmt.Limit(dto.Page.Size).Offset(dto.Page.Offset)
  110. stmt.Order("eo.id ASC")
  111. if err := stmt.Find(&ret).Error; err != nil {
  112. return nil, 0, err
  113. }
  114. return ret, total, nil
  115. }
  116. func (a *orderTransportTask) FinishedTask(ctx context.Context, id int) error {
  117. up := models.OrderTransportTask{
  118. Status: constant.TaskStatusFinished,
  119. FinishedTime: models.NewMyTime(time.Now()),
  120. }
  121. err := a.store.optionDB(ctx).Model(a.baseEntity).
  122. Where("deleted = ?", models.DeleteNo).
  123. Where("id = ?", id).
  124. Updates(up).Error
  125. return err
  126. }