logistic_service.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package myselfsrv
  2. import (
  3. "Cold_Logistic/internal/pkg/common/codex"
  4. "Cold_Logistic/internal/pkg/common/constant"
  5. "Cold_Logistic/internal/server/infra/dao"
  6. "Cold_Logistic/internal/server/infra/models"
  7. "context"
  8. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/core"
  9. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
  10. )
  11. // LogisticPage 物流公司列表
  12. func (srv *MyselfService) LogisticPage(ctx context.Context, req LogisticPageReqVO) (res core.PageListResponse, err error) {
  13. dto := dao.LogisticCompanyPageDTO{
  14. Page: req.Page,
  15. }
  16. list, total, err := srv.store.LogisticCompany().Page(ctx, dto)
  17. if err != nil {
  18. return res, errors.WithStackOnce(err)
  19. }
  20. res.Total = total
  21. res.TotalPage = total / int64(req.Page.Size)
  22. res.Count = len(list)
  23. res.List = list
  24. return res, nil
  25. }
  26. // AddLogistic 添加物流公司
  27. func (srv *MyselfService) AddLogistic(ctx context.Context, req AddLogisticReqVO) (res LogisticIdRespVO, err error) {
  28. count, err := srv.store.LogisticCompany().CountMyLogistic(ctx, req.Name, req.Pid)
  29. if err != nil {
  30. return res, errors.WithStackOnce(err)
  31. }
  32. if count > 0 {
  33. return res, errors.WithCode(codex.ErrParamValidate, "该物流公司已添加过")
  34. }
  35. logistic := models.LogisticCompany{
  36. Name: req.Name,
  37. IsDefault: req.IsDefault,
  38. Pid: req.Pid,
  39. }
  40. err = srv.store.InTx(ctx, func(ctx context.Context) error {
  41. if err = srv.store.LogisticCompany().Create(ctx, &logistic); err != nil {
  42. return errors.WithStackOnce(err)
  43. }
  44. // 只能有一个默认
  45. if logistic.IsDefault == constant.YES {
  46. err = srv.store.LogisticCompany().UpdateDefaultByCreateBy(ctx, constant.NO, logistic.Id)
  47. if err != nil {
  48. return errors.WithStackOnce(err)
  49. }
  50. }
  51. return nil
  52. })
  53. if err != nil {
  54. return res, errors.WithStackOnce(err)
  55. }
  56. res.LogisticId = logistic.Id
  57. return res, nil
  58. }
  59. // DeleteLogistic 删除物流公司
  60. func (srv *MyselfService) DeleteLogistic(ctx context.Context, req DeleteLogisticReqVO) error {
  61. return srv.store.LogisticCompany().DeleteByIds(ctx, req.LogisticId)
  62. }