base_distributor.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package base
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "dashoo.cn/opms_libary/myerrors"
  7. "github.com/gogf/gf/errors/gerror"
  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.M
  31. if req.DistCode != "" {
  32. distributorModel = distributorModel.Where(s.Dao.Columns.DistCode+" like ?", "%"+req.DistCode+"%")
  33. }
  34. if req.DistName != "" {
  35. distributorModel = distributorModel.Where(s.Dao.Columns.DistName+" like ?", "%"+req.DistName+"%")
  36. }
  37. if req.BelongSale != "" {
  38. distributorModel = distributorModel.Where(s.Dao.Columns.BelongSale+" like ?", "%"+req.BelongSale+"%")
  39. }
  40. if req.ProvinceId > 0 {
  41. distributorModel = distributorModel.Where(s.Dao.Columns.ProvinceId+" in (?)", req.ProvinceId)
  42. }
  43. g.Log().Info("搜索条件", req.ProvinceId)
  44. total, err = distributorModel.Count()
  45. if err != nil {
  46. g.Log().Error(err)
  47. err = gerror.New("获取总行数失败")
  48. return
  49. }
  50. err = distributorModel.Page(req.GetPage()).Order("id desc").Scan(&distributorList)
  51. g.Log().Info("返回列表", distributorList)
  52. return
  53. }
  54. //Create 经销商创建
  55. func (s *distributorService) Create(req *model.AddDistributor) (lastId int64, err error) {
  56. DistributorData := new(model.BaseDistributor)
  57. if err = gconv.Struct(req, DistributorData); err != nil {
  58. return
  59. }
  60. service.SetCreatedInfo(DistributorData, s.GetCxtUserId(), s.GetCxtUserName())
  61. DistributorData.DistCode = gstr.SubStr(strconv.Itoa(int(gtime.Now().UnixNano()/1e6))+"Code", 0, -5)
  62. lastId, err = s.Dao.InsertAndGetId(DistributorData)
  63. if err != nil {
  64. g.Log().Error(err)
  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.Columns.Id, id).Scan(&distributorInfo)
  72. if err != nil {
  73. g.Log().Error(err)
  74. return
  75. }
  76. return
  77. }
  78. // UpdateById 修改数据
  79. func (s *distributorService) UpdateById(req *model.UpdateDistributorReq) (err error) {
  80. count, err := s.Dao.Where("id = ", req.Id).Count()
  81. if err != nil {
  82. g.Log().Error(err)
  83. return
  84. }
  85. if count == 0 {
  86. err = myerrors.NewMsgError(nil, "无修改数据")
  87. return
  88. }
  89. distData := new(model.BaseDistributor)
  90. if err = gconv.Struct(req, distData); err != nil {
  91. return
  92. }
  93. service.SetUpdatedInfo(distData, s.GetCxtUserId(), s.GetCxtUserName())
  94. _, err = s.Dao.FieldsEx(s.Dao.Columns.DistCode, s.Dao.Columns.Id,
  95. s.Dao.Columns.CreatedName, s.Dao.Columns.CreatedBy, s.Dao.Columns.CreatedTime).WherePri(s.Dao.Columns.Id, req.Id).Update(distData)
  96. if err != nil {
  97. g.Log().Error(err)
  98. return
  99. }
  100. return
  101. }
  102. //DeleteByIds 删除
  103. func (s *distributorService) DeleteByIds(ids []int64) (err error) {
  104. if len(ids) == 1 {
  105. count, err := s.Dao.WherePri(ids[0]).Count()
  106. if err != nil {
  107. g.Log().Error(err)
  108. return err
  109. }
  110. if count == 0 {
  111. return myerrors.NewMsgError(nil, "数据不存在或已被删除,请刷新页面")
  112. }
  113. } else {
  114. _, err := s.Dao.WhereIn(s.Dao.Columns.Id, ids).LockShared().Count()
  115. if err != nil {
  116. g.Log().Error(err)
  117. return err
  118. }
  119. result, err := s.Dao.WhereIn(s.Dao.Columns.Id, ids).Delete()
  120. if err != nil {
  121. g.Log().Error(err)
  122. return err
  123. }
  124. rows, err := result.RowsAffected()
  125. if err == nil {
  126. if len(ids) != int(rows) {
  127. return myerrors.NewMsgError(nil, fmt.Sprintf("应更新%s行,实际更新%s行", len(ids), int(rows)))
  128. }
  129. }
  130. }
  131. return
  132. }