base_distributor.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. total, err = distributorModel.Count()
  43. if err != nil {
  44. err = myerrors.DbError("获取总行数失败。")
  45. return
  46. }
  47. err = distributorModel.Page(req.GetPage()).Order("id desc").Scan(&distributorList)
  48. return
  49. }
  50. //Create 经销商创建
  51. func (s *distributorService) Create(req *model.AddDistributor) (lastId int64, err error) {
  52. DistributorData := new(model.BaseDistributor)
  53. if err = gconv.Struct(req, DistributorData); err != nil {
  54. return
  55. }
  56. service.SetCreatedInfo(DistributorData, s.GetCxtUserId(), s.GetCxtUserName())
  57. DistributorData.DistCode = gstr.SubStr(strconv.Itoa(int(gtime.Now().UnixNano()/1e6))+"Code", 0, -5)
  58. lastId, err = s.Dao.InsertAndGetId(DistributorData)
  59. if err != nil {
  60. return 0, err
  61. }
  62. return
  63. }
  64. //GetEntityById 详情
  65. func (s *distributorService) GetEntityById(id int64) (distributorInfo *model.DistributorRonp, err error) {
  66. err = s.Dao.Where(base.BaseProduct.Columns.Id, id).Scan(&distributorInfo)
  67. if err != nil {
  68. return
  69. }
  70. return
  71. }
  72. // UpdateById 修改数据
  73. func (s *distributorService) UpdateById(req *model.UpdateDistributorReq) (err error) {
  74. count, err := s.Dao.Where("id = ", req.Id).Count()
  75. if err != nil {
  76. g.Log().Error(err)
  77. return
  78. }
  79. if count == 0 {
  80. err = myerrors.TipsError("无修改数据")
  81. return
  82. }
  83. distData := new(model.BaseDistributor)
  84. if err = gconv.Struct(req, distData); err != nil {
  85. return
  86. }
  87. service.SetUpdatedInfo(distData, s.GetCxtUserId(), s.GetCxtUserName())
  88. _, err = s.Dao.FieldsEx(s.Dao.Columns.DistCode, s.Dao.Columns.Id,
  89. s.Dao.Columns.CreatedName, s.Dao.Columns.CreatedBy, s.Dao.Columns.CreatedTime).WherePri(s.Dao.Columns.Id, req.Id).Update(distData)
  90. if err != nil {
  91. g.Log().Error(err)
  92. return
  93. }
  94. return
  95. }
  96. //DeleteByIds 删除
  97. func (s *distributorService) DeleteByIds(ids []int64) (err error) {
  98. if len(ids) == 1 {
  99. count, err := s.Dao.WherePri(ids[0]).Count()
  100. if err != nil {
  101. g.Log().Error(err)
  102. return err
  103. }
  104. if count == 0 {
  105. return myerrors.TipsError("数据不存在或已被删除,请刷新页面")
  106. }
  107. } else {
  108. _, err := s.Dao.WhereIn(s.Dao.Columns.Id, ids).LockShared().Count()
  109. if err != nil {
  110. g.Log().Error(err)
  111. return err
  112. }
  113. result, err := s.Dao.WhereIn(s.Dao.Columns.Id, ids).Delete()
  114. if err != nil {
  115. g.Log().Error(err)
  116. return err
  117. }
  118. rows, err := result.RowsAffected()
  119. if err == nil {
  120. if len(ids) != int(rows) {
  121. return myerrors.TipsError(fmt.Sprintf("应更新%s行,实际更新%s行", len(ids), int(rows)))
  122. }
  123. }
  124. }
  125. return
  126. }