sale_apply.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package train
  2. import (
  3. "context"
  4. "dashoo.cn/opms_libary/myerrors"
  5. dao "da
  6. dao "dashoo.cn/opms_parent/app/dao/train"
  7. model "dashoo.cn/opms_parent/app/model/train"
  8. "dashoo.cn/opms_libary/myerrors"
  9. "github.com/gogf/gf/database/gdb"
  10. "github.com/gogf/gf/frame/g"
  11. "github.com/gogf/gf/util/gconv"
  12. )
  13. type SaleApplyService struct {
  14. *service.ContextService
  15. Dao *dao.TrainSaleApplyDao
  16. }
  17. func NewSaleApplyService(ctx context.Context) (svc *SaleApplyService, err error) {
  18. svc = new(SaleApplyService)
  19. if svc.ContextService, err = svc.Init(ctx); err != nil {
  20. return nil, err
  21. }
  22. svc.Dao = dao.NewTrainSaleApplyDao(svc.Tenant)
  23. return svc, nil
  24. }
  25. // GetList 销售申请信息列表
  26. func (s *SaleApplyService) GetList(ctx context.Context, req *model.TrainSaleApplySearchReq) (total int, distributorList []*model.TrainSaleApplyRes, err error) {
  27. distributorModel := s.Dao.Ctx(ctx).FieldsEx(s.Dao.C.DeletedTime)
  28. if req.CreatedBy != 0 {
  29. distributorModel = distributorModel.Where(s.Dao.C.CreatedBy, req.CreatedBy)
  30. }
  31. if req.DistributorId != 0 {
  32. distributorModel = distributorModel.Where(s.Dao.C.DistributorId, req.DistributorId)
  33. }
  34. if req.MainProduct != "" {
  35. distributorModel = distributorModel.WhereLike(s.Dao.C.MainProduct, "%"+req.MainProduct+"%")
  36. }
  37. total, _ = distributorModel.Count()
  38. err = distributorModel.Page(req.GetPage()).Order("id desc").Scan(&distributorList)
  39. if err != nil {
  40. return 0, nil, err
  41. }
  42. return
  43. }
  44. // GetEntityById 详情
  45. func (s *SaleApplyService) GetEntityById(ctx context.Context, id int64) (distributorInfo *model.TrainSaleApplyRes, err error) {
  46. err = s.Dao.Ctx(ctx).Where(s.Dao.C.Id, id).Scan(&distributorInfo)
  47. if err != nil {
  48. return nil, err
  49. }
  50. return
  51. }
  52. // Create 销售申请创建
  53. func (s *SaleApplyService) Create(ctx context.Context, req *model.TrainSaleApplyCreateReq) (lastId int64, err error) {
  54. DistributorData := new(model.TrainSaleApply)
  55. if err = gconv.Struct(req, DistributorData); err != nil {
  56. return 0, err
  57. }
  58. service.SetCreatedInfo(DistributorData, s.GetCxtUserId(), s.GetCxtUserName())
  59. txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  60. DistributorData.ApplyType = "10"
  61. lastId, err = tx.InsertAndGetId("train_sale_apply", DistributorData)
  62. if err != nil {
  63. return err
  64. }
  65. return err
  66. })
  67. if txerr != nil {
  68. return 0, txerr
  69. }
  70. return lastId, nil
  71. }
  72. // UpdateById 修改数据
  73. func (s *SaleApplyService) UpdateById(ctx context.Context, req *model.TrainSaleApplyUpdateReq) (err error) {
  74. ent, err := s.Dao.Ctx(ctx).Where("id = ", req.Id).One()
  75. if err != nil {
  76. g.Log().Error(err)
  77. return
  78. }
  79. if ent == nil {
  80. err = myerrors.TipsError("无修改数据")
  81. return
  82. }
  83. distData := new(model.TrainSaleApply)
  84. if err = gconv.Struct(req, distData); err != nil {
  85. return
  86. }
  87. service.SetUpdatedInfo(distData, s.GetCxtUserId(), s.GetCxtUserName())
  88. _, err = s.Dao.Ctx(ctx).FieldsEx(s.Dao.C.Id, s.Dao.C.CreatedName, s.Dao.C.CreatedBy, s.Dao.C.CreatedTime).
  89. WherePri(s.Dao.C.Id, req.Id).Update(distData)
  90. if err != nil {
  91. g.Log().Error(err)
  92. return
  93. }
  94. return
  95. }
  96. // DeleteByIds 删除
  97. func (s *SaleApplyService) DeleteByIds(ctx context.Context, ids []int64) (err error) {
  98. _, err = s.Dao.Ctx(ctx).WhereIn(s.Dao.C.Id, ids).Delete()
  99. if err != nil {
  100. return err
  101. }
  102. return
  103. }