waybill_logistics.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package service
  2. import (
  3. "cold-logistics/app/admin/model"
  4. "cold-logistics/app/admin/service/dto"
  5. "cold-logistics/common/actions"
  6. cDto "cold-logistics/common/dto"
  7. "cold-logistics/common/global"
  8. "errors"
  9. "fmt"
  10. "gogs.baozhida.cn/zoie/OAuth-core/service"
  11. "gorm.io/gorm"
  12. )
  13. type WaybillLogistics struct {
  14. service.Service
  15. }
  16. // GetPage 获取WaybillLogistics列表
  17. func (e *WaybillLogistics) GetPage(c *dto.WaybillLogisticsGetPageReq, list *[]model.WaybillLogistics, count *int64) error {
  18. var err error
  19. var data model.WaybillLogistics
  20. status := []int{
  21. model.WaybillStatusTruck,
  22. model.WaybillStatusStorage,
  23. model.WaybillStatusTruckOut,
  24. model.WaybillStatusStorageOut,
  25. model.WaybillStatusVanning,
  26. model.WaybillStatusVanningOut,
  27. model.WaybillStatusReceipt,
  28. }
  29. err = e.Orm.Model(&data).
  30. Scopes(
  31. cDto.MakeCondition(c.GetNeedSearch()),
  32. ).
  33. Where("status in (?)", status).
  34. Preload("User").
  35. Preload("Warehouse.User").Preload("Car.User").Preload("CoolerBox").
  36. Find(list).
  37. Count(count).Error
  38. if err != nil {
  39. e.Log.Errorf("db error: %s", err)
  40. return global.GetFailedErr
  41. }
  42. return nil
  43. }
  44. // Update 修改Waybill对象
  45. func (e *WaybillLogistics) Update(c *dto.WaybillLogisticsUpdateReq, p *actions.DataPermission) error {
  46. var err error
  47. tx := e.Orm.Begin()
  48. defer func() {
  49. if err != nil {
  50. tx.Rollback()
  51. } else {
  52. tx.Commit()
  53. }
  54. }()
  55. for _, logistics := range c.WaybillLogisticsList {
  56. var waybillLogisticsModel = model.WaybillLogistics{}
  57. // 查询运单是否存在
  58. err = e.Orm.Scopes(actions.Permission(waybillLogisticsModel.TableName(), p)).
  59. First(&waybillLogisticsModel, logistics.Id).Error
  60. if err != nil {
  61. e.Log.Errorf("db error: %s", err)
  62. if errors.Is(err, gorm.ErrRecordNotFound) {
  63. return global.UpdateNotFoundOrNoPermissionErr
  64. }
  65. return global.UpdateFailedErr
  66. }
  67. waybillLogisticsModel.CreatedAt = logistics.Time
  68. waybillLogisticsModel.UpdateBy = c.UpdateBy
  69. err = tx.Save(&waybillLogisticsModel).Error
  70. if err != nil {
  71. e.Log.Errorf("db error: %s", err)
  72. return global.UpdateFailedErr
  73. }
  74. if waybillLogisticsModel.Status == model.WaybillStatusReceipt {
  75. // 修改运单签收时间
  76. var waybillModel = model.Waybill{}
  77. // 查询运单是否存在
  78. err = tx.Scopes(actions.Permission(waybillModel.TableName(), p)).
  79. Where("waybill_no = ?", waybillLogisticsModel.WaybillNo).
  80. First(&waybillModel).Error
  81. if err != nil {
  82. e.Log.Errorf("db error: %s", err)
  83. if errors.Is(err, gorm.ErrRecordNotFound) {
  84. return errors.New(fmt.Sprintf("运单号%s不存在", waybillLogisticsModel.WaybillNo))
  85. }
  86. return errors.New(fmt.Sprintf("运单号%s查询失败", waybillLogisticsModel.WaybillNo))
  87. }
  88. waybillModel.ReceiptTime = logistics.Time
  89. err = tx.Save(&waybillModel).Error
  90. if err != nil {
  91. e.Log.Errorf("db error: %s", err)
  92. return errors.New(fmt.Sprintf("保存运单信息失败:%s", err))
  93. }
  94. }
  95. }
  96. return nil
  97. }