base_distributor.go 3.9 KB

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