logistic_company.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. // LogisticCompanyStore 物流公司表
  11. type LogisticCompanyStore interface {
  12. DbBaseStore
  13. Page(ctx context.Context, dto LogisticCompanyPageDTO) ([]LogisticCompanyPageVO, int64, error)
  14. CountMyLogistic(ctx context.Context, name string, pid int) (int64, error)
  15. UpdateDefaultByCreateBy(ctx context.Context, isDefault int, excludeId ...int) error
  16. FindNameById(ctx context.Context, id int) (string, error)
  17. FindMyByPid(ctx context.Context, pid int) (models.LogisticCompany, error)
  18. }
  19. var _ LogisticCompanyStore = &logisticCompany{}
  20. type logisticCompany struct {
  21. dbBase
  22. }
  23. func newLogisticCompany(ds *DataStore) *logisticCompany {
  24. return &logisticCompany{dbBase: dbBase{
  25. store: ds,
  26. baseEntity: &models.LogisticCompany{},
  27. }}
  28. }
  29. func (ds *DataStore) LogisticCompany() LogisticCompanyStore {
  30. return newLogisticCompany(ds)
  31. }
  32. type LogisticCompanyPageDTO struct {
  33. Page core.Page
  34. }
  35. type LogisticCompanyPageVO struct {
  36. LogisticId int `gorm:"column:id" json:"logisticId"` //
  37. Name string `gorm:"column:name" json:"name"` //名称
  38. IsDefault int `gorm:"column:is_default" json:"isDefault"` //是否默认:1-是 - 2-否
  39. Pid int `gorm:"column:pid" json:"pid"` //冷链系统公司Id
  40. }
  41. func (a *logisticCompany) Page(ctx context.Context, dto LogisticCompanyPageDTO) ([]LogisticCompanyPageVO, int64, error) {
  42. db := a.store.optionDB(ctx, a.store.withAccountDataPermis(ctx, "created_by"))
  43. stmt := db.Model(&models.LogisticCompany{}).
  44. Where("deleted = ?", models.DeleteNo)
  45. stmt.Select("id, name, is_default, pid")
  46. var total int64
  47. var ret []LogisticCompanyPageVO
  48. if err := stmt.Count(&total).Error; err != nil {
  49. return nil, 0, err
  50. }
  51. if total == 0 {
  52. return ret, total, nil
  53. }
  54. stmt.Limit(dto.Page.Size).Offset(dto.Page.Offset)
  55. stmt.Order("id desc")
  56. if err := stmt.Find(&ret).Error; err != nil {
  57. return nil, 0, err
  58. }
  59. return ret, total, nil
  60. }
  61. // CountMyLogistic 统计我的物流
  62. func (a *logisticCompany) CountMyLogistic(ctx context.Context, name string, pid int) (int64, error) {
  63. db := a.store.optionDB(ctx, a.store.withAccountDataPermis(ctx, "created_by"))
  64. stmt := db.Model(&models.LogisticCompany{}).
  65. Where("deleted = ?", models.DeleteNo)
  66. if name != "" {
  67. stmt.Where("name = ?", name)
  68. }
  69. if pid > 0 {
  70. stmt.Where("pid = ?", pid)
  71. }
  72. var total int64
  73. err := stmt.Count(&total).Error
  74. return total, errors.WithStackOnce(err)
  75. }
  76. // UpdateDefaultByCreateBy 修改我的默认选中
  77. func (a *logisticCompany) UpdateDefaultByCreateBy(ctx context.Context, isDefault int, excludeId ...int) error {
  78. accId := global.GetTokenInfoFromContext(ctx).AccountId
  79. ent := models.LogisticCompany{
  80. UpdatedTime: models.NewMyTime(time.Now()),
  81. UpdatedBy: accId,
  82. IsDefault: isDefault,
  83. }
  84. stmt := a.store.optionDB(ctx).Table(a.baseEntity.TableName()).
  85. Where("deleted = ?", models.DeleteNo).
  86. Where("created_by = ?", accId)
  87. if len(excludeId) > 0 {
  88. stmt.Where("id NOT IN(?)", excludeId)
  89. }
  90. err := stmt.Updates(&ent).Error
  91. return errors.WithStackOnce(err)
  92. }
  93. // FindNameById 查名字
  94. func (a *logisticCompany) FindNameById(ctx context.Context, id int) (string, error) {
  95. var name string
  96. err := a.store.optionDB(ctx).Table(a.baseEntity.TableName()).
  97. Where("deleted = ?", models.DeleteNo).
  98. Where("id = ?", id).Pluck("name", &name).Error
  99. return name, errors.WithStackOnce(err)
  100. }
  101. func (a *logisticCompany) FindMyByPid(ctx context.Context, pid int) (models.LogisticCompany, error) {
  102. var ret models.LogisticCompany
  103. err := a.store.optionDB(ctx, a.store.withAccountDataPermis(ctx, "created_by")).
  104. Model(a.baseEntity).
  105. Where("deleted = ?", models.DeleteNo).
  106. Where("pid = ?", pid).Find(&ret).Error
  107. return ret, errors.WithStackOnce(err)
  108. }