sale_apply.go 3.1 KB

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