base_product.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package base
  2. import (
  3. "context"
  4. "fmt"
  5. "dashoo.cn/opms_libary/myerrors"
  6. "github.com/gogf/gf/frame/g"
  7. "github.com/gogf/gf/util/gconv"
  8. "dashoo.cn/micro/app/dao/base"
  9. model "dashoo.cn/micro/app/model/base"
  10. "dashoo.cn/micro/app/service"
  11. )
  12. type productService struct {
  13. *service.ContextService
  14. Dao *base.BaseProductDao
  15. }
  16. func NewProductService(ctx context.Context) (svc *productService, err error) {
  17. svc = new(productService)
  18. if svc.ContextService, err = svc.Init(ctx); err != nil {
  19. return nil, err
  20. }
  21. svc.Dao = base.NewBaseProductDao(svc.Tenant)
  22. return svc, nil
  23. }
  24. //GetList 产品信息列表
  25. func (s *productService) GetList(req *model.ProductSearchReq) (total int, productList []*model.BaseProduct, err error) {
  26. Dao := &s.Dao.BaseProductDao
  27. if req.ProdCode != "" {
  28. Dao = Dao.Where("prod_code = ?", req.ProdCode)
  29. }
  30. if req.ProdName != "" {
  31. Dao = Dao.Where("prod_name like ?", "%"+req.ProdName+"%")
  32. }
  33. if req.ProdClass != "" {
  34. Dao = Dao.Where("prod_class = ?", req.ProdClass)
  35. }
  36. total, err = Dao.Count()
  37. if err != nil {
  38. g.Log().Error(err)
  39. return
  40. }
  41. if req.PageNum == 0 {
  42. req.PageNum = 1
  43. }
  44. err = Dao.Page(req.PageNum, req.PageSize).Order("id asc").Scan(&productList)
  45. return
  46. }
  47. //GetEntityById 根据ID查询详细信息
  48. func (s *productService) GetEntityById(id int64) (product *model.BaseProduct, err error) {
  49. product, err = s.Dao.FindOne(id)
  50. if err != nil {
  51. g.Log().Error(err)
  52. return
  53. }
  54. return
  55. }
  56. //Create 添加信息
  57. func (s *productService) Create(req *model.AddProductReq) (lastInsertId int64, err error) {
  58. product := new(model.BaseProduct)
  59. if err = gconv.Struct(req, product); err != nil {
  60. g.Log().Error(err)
  61. return
  62. }
  63. service.SetCreatedInfo(product, s.GetCxtUserId(), s.GetCxtUserName())
  64. lastInsertId, err = s.Dao.InsertAndGetId(product)
  65. if err != nil {
  66. g.Log().Error(err)
  67. return
  68. }
  69. return
  70. }
  71. //UpdateById 修改数据
  72. func (s *productService) UpdateById(req *model.UpdateProductReq) (err error) {
  73. product, err := s.Dao.FindOne(req.Id)
  74. if err != nil {
  75. g.Log().Error(err)
  76. return err
  77. }
  78. if product == nil {
  79. return myerrors.NewMsgError(nil, "产品信息不存在或已被删除")
  80. }
  81. if err = gconv.Struct(req, product); err != nil {
  82. return
  83. }
  84. service.SetUpdatedInfo(product, s.GetCxtUserId(), s.GetCxtUserName())
  85. _, err = s.Dao.WherePri(req.Id).Update(product)
  86. if err != nil {
  87. g.Log().Error(err)
  88. return
  89. }
  90. return
  91. }
  92. //DeleteByIds 删掉数据
  93. func (s *productService) DeleteByIds(ids []int64) (err error) {
  94. if len(ids) == 1 {
  95. count, err := s.Dao.WherePri(ids[0]).Count()
  96. if err != nil {
  97. g.Log().Error(err)
  98. return err
  99. }
  100. if count == 0 {
  101. return myerrors.NewMsgError(nil, "数据不存在或已被删除,请刷新页面")
  102. }
  103. _, err = s.Dao.WherePri(s.Dao.Columns.Id, ids[0]).Delete()
  104. if err != nil {
  105. g.Log().Error(err)
  106. return err
  107. }
  108. return err
  109. } else {
  110. _, err := s.Dao.WhereIn(s.Dao.Columns.Id, ids).LockShared().Count()
  111. if err != nil {
  112. g.Log().Error(err)
  113. return err
  114. }
  115. result, err := s.Dao.WhereIn(s.Dao.Columns.Id, ids).Delete()
  116. if err != nil {
  117. g.Log().Error(err)
  118. return err
  119. }
  120. rows, err := result.RowsAffected()
  121. if err == nil {
  122. if len(ids) != int(rows) {
  123. return myerrors.NewMsgError(nil, fmt.Sprintf("应更新%s行,实际更新%s行", len(ids), int(rows)))
  124. }
  125. }
  126. }
  127. return nil
  128. }