base_distributor.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package base
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/gogf/gf/container/garray"
  6. "strconv"
  7. "dashoo.cn/opms_libary/myerrors"
  8. "github.com/gogf/gf/frame/g"
  9. "github.com/gogf/gf/os/gtime"
  10. "github.com/gogf/gf/text/gstr"
  11. "github.com/gogf/gf/util/gconv"
  12. "dashoo.cn/micro/app/dao/base"
  13. model "dashoo.cn/micro/app/model/base"
  14. "dashoo.cn/micro/app/service"
  15. )
  16. type distributorService struct {
  17. *service.ContextService
  18. Dao *base.BaseDistributorDao
  19. }
  20. func NewDistributorService(ctx context.Context) (svc *distributorService, err error) {
  21. svc = new(distributorService)
  22. if svc.ContextService, err = svc.Init(ctx); err != nil {
  23. return nil, err
  24. }
  25. svc.Dao = base.NewBaseDistributorDao(svc.Tenant)
  26. return svc, nil
  27. }
  28. // GetList 经销商信息列表
  29. func (s *distributorService) GetList(req *model.BaseDistributorSearchReq) (total int, distributorList []*model.DistributorRonp, err error) {
  30. distributorModel := s.Dao.FieldsEx(s.Dao.C.DeletedTime)
  31. // 用户仅有销售工程师角色展示自己的数据,其他人可以看到所有数据
  32. if len(s.CxtUser.Roles) == 1 && garray.NewStrArrayFrom(s.CxtUser.Roles, true).Contains("SalesEngineer") {
  33. distributorModel = distributorModel.DataScope(s.Ctx, "belong_sale_id")
  34. }
  35. if req.DistCode != "" {
  36. distributorModel = distributorModel.WhereLike(s.Dao.C.DistCode, "%"+req.DistCode+"%")
  37. }
  38. if req.DistName != "" {
  39. distributorModel = distributorModel.WhereLike(s.Dao.C.DistName, "%"+req.DistName+"%")
  40. }
  41. if req.BelongSale != "" {
  42. distributorModel = distributorModel.WhereLike(s.Dao.C.BelongSale, "%"+req.BelongSale+"%")
  43. }
  44. if len(req.ProvinceId) > 0 {
  45. distributorModel = distributorModel.WhereIn(s.Dao.C.ProvinceId, req.ProvinceId)
  46. }
  47. total, err = distributorModel.Count()
  48. if err != nil {
  49. err = myerrors.DbError("获取总行数失败。")
  50. return
  51. }
  52. err = distributorModel.Page(req.GetPage()).Order("id desc").Scan(&distributorList)
  53. return
  54. }
  55. // Create 经销商创建
  56. func (s *distributorService) Create(req *model.AddDistributor) (lastId int64, err error) {
  57. DistributorData := new(model.BaseDistributor)
  58. if err = gconv.Struct(req, DistributorData); err != nil {
  59. return
  60. }
  61. service.SetCreatedInfo(DistributorData, s.GetCxtUserId(), s.GetCxtUserName())
  62. DistributorData.DistCode = gstr.SubStr(strconv.Itoa(int(gtime.Now().UnixNano()/1e6))+"Code", 0, -5)
  63. lastId, err = s.Dao.InsertAndGetId(DistributorData)
  64. if err != nil {
  65. return 0, err
  66. }
  67. return
  68. }
  69. // GetEntityById 详情
  70. func (s *distributorService) GetEntityById(id int64) (distributorInfo *model.DistributorRonp, err error) {
  71. err = s.Dao.Where(base.BaseProduct.C.Id, id).Scan(&distributorInfo)
  72. if err != nil {
  73. return
  74. }
  75. return
  76. }
  77. // UpdateById 修改数据
  78. func (s *distributorService) UpdateById(req *model.UpdateDistributorReq) (err error) {
  79. count, err := s.Dao.Where("id = ", req.Id).Count()
  80. if err != nil {
  81. g.Log().Error(err)
  82. return
  83. }
  84. if count == 0 {
  85. err = myerrors.TipsError("无修改数据")
  86. return
  87. }
  88. distData := new(model.BaseDistributor)
  89. if err = gconv.Struct(req, distData); err != nil {
  90. return
  91. }
  92. service.SetUpdatedInfo(distData, s.GetCxtUserId(), s.GetCxtUserName())
  93. _, err = s.Dao.FieldsEx(s.Dao.C.DistCode, s.Dao.C.Id,
  94. s.Dao.C.CreatedName, s.Dao.C.CreatedBy, s.Dao.C.CreatedTime).WherePri(s.Dao.C.Id, req.Id).Update(distData)
  95. if err != nil {
  96. g.Log().Error(err)
  97. return
  98. }
  99. return
  100. }
  101. // DeleteByIds 删除
  102. func (s *distributorService) DeleteByIds(ids []int64) (err error) {
  103. if len(ids) == 1 {
  104. count, err := s.Dao.WherePri(ids[0]).Count()
  105. if err != nil {
  106. g.Log().Error(err)
  107. return err
  108. }
  109. if count == 0 {
  110. return myerrors.TipsError("数据不存在或已被删除,请刷新页面")
  111. }
  112. } else {
  113. _, err := s.Dao.WhereIn(s.Dao.C.Id, ids).LockShared().Count()
  114. if err != nil {
  115. g.Log().Error(err)
  116. return err
  117. }
  118. result, err := s.Dao.WhereIn(s.Dao.C.Id, ids).Delete()
  119. if err != nil {
  120. g.Log().Error(err)
  121. return err
  122. }
  123. rows, err := result.RowsAffected()
  124. if err == nil {
  125. if len(ids) != int(rows) {
  126. return myerrors.TipsError(fmt.Sprintf("应更新%s行,实际更新%s行", len(ids), int(rows)))
  127. }
  128. }
  129. }
  130. return
  131. }