sale_apply.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.CreatedBy != 0 {
  28. distributorModel = distributorModel.Where(s.Dao.C.CreatedBy, req.CreatedBy)
  29. }
  30. if req.DistributorId != 0 {
  31. distributorModel = distributorModel.Where(s.Dao.C.DistributorId, req.DistributorId)
  32. }
  33. if req.MainProduct != "" {
  34. distributorModel = distributorModel.WhereLike(s.Dao.C.MainProduct, "%"+req.MainProduct+"%")
  35. }
  36. total, _ = distributorModel.Count()
  37. err = distributorModel.Page(req.GetPage()).Order("id desc").Scan(&distributorList)
  38. if err != nil {
  39. return 0, nil, err
  40. }
  41. return
  42. }
  43. // GetEntityById 详情
  44. func (s *SaleApplyService) GetEntityById(ctx context.Context, id int64) (distributorInfo *model.TrainSaleApplyRes, err error) {
  45. err = s.Dao.Ctx(ctx).Where(s.Dao.C.Id, id).Scan(&distributorInfo)
  46. if err != nil {
  47. return nil, err
  48. }
  49. return
  50. }
  51. // Create 销售申请创建
  52. func (s *SaleApplyService) Create(ctx context.Context, req *model.TrainSaleApplyCreateReq) (lastId int64, err error) {
  53. DistributorData := new(model.TrainSaleApply)
  54. if err = gconv.Struct(req, DistributorData); err != nil {
  55. return 0, err
  56. }
  57. service.SetCreatedInfo(DistributorData, s.GetCxtUserId(), s.GetCxtUserName())
  58. txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  59. DistributorData.ApplyType = "10"
  60. lastId, err = tx.InsertAndGetId("train_sale_apply", DistributorData)
  61. if err != nil {
  62. return err
  63. }
  64. return err
  65. })
  66. if txerr != nil {
  67. return 0, txerr
  68. }
  69. return lastId, nil
  70. }
  71. // UpdateById 修改数据
  72. func (s *SaleApplyService) UpdateById(ctx context.Context, req *model.TrainSaleApplyUpdateReq) (err error) {
  73. ent, err := s.Dao.Ctx(ctx).Where("id = ", req.Id).One()
  74. if err != nil {
  75. g.Log().Error(err)
  76. return
  77. }
  78. if ent == nil {
  79. err = myerrors.TipsError("无修改数据")
  80. return
  81. }
  82. distData := new(model.TrainSaleApply)
  83. if err = gconv.Struct(req, distData); err != nil {
  84. return
  85. }
  86. service.SetUpdatedInfo(distData, s.GetCxtUserId(), s.GetCxtUserName())
  87. _, err = s.Dao.Ctx(ctx).FieldsEx(s.Dao.C.Id, s.Dao.C.CreatedName, s.Dao.C.CreatedBy, s.Dao.C.CreatedTime).
  88. WherePri(s.Dao.C.Id, req.Id).Update(distData)
  89. if err != nil {
  90. g.Log().Error(err)
  91. return
  92. }
  93. return
  94. }
  95. // DeleteByIds 删除
  96. func (s *SaleApplyService) DeleteByIds(ctx context.Context, ids []int64) (err error) {
  97. _, err = s.Dao.Ctx(ctx).WhereIn(s.Dao.C.Id, ids).Delete()
  98. if err != nil {
  99. return err
  100. }
  101. return
  102. }