base_product.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package base
  2. import (
  3. "context"
  4. "dashoo.cn/opms_libary/myerrors"
  5. "fmt"
  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. total, err = Dao.Count()
  34. if err != nil {
  35. g.Log().Error(err)
  36. err = myerrors.New("获取总行数失败", err)
  37. return
  38. }
  39. if req.PageNum == 0 {
  40. req.PageNum = 1
  41. }
  42. err = Dao.Page(req.PageNum, req.PageSize).Order("id asc").Scan(&productList)
  43. return
  44. }
  45. //GetEntityById 根据ID查询详细信息
  46. func (s *productService) GetEntityById(id int64) (product *model.BaseProduct, err error) {
  47. product, err = s.Dao.FindOne(id)
  48. if err != nil {
  49. g.Log().Error(err)
  50. return nil, err
  51. }
  52. return
  53. }
  54. //Create 添加信息
  55. func (s *productService) Create(req *model.AddProductReq) (lastInsertId int64, err error) {
  56. product := new(model.BaseProduct)
  57. if err = gconv.Struct(req, product); err != nil {
  58. return
  59. }
  60. service.SetCreatedInfo(product, s.GetCxtUserId(), s.GetCxtUserName())
  61. lastInsertId, err = s.Dao.InsertAndGetId(product)
  62. return
  63. }
  64. //UpdateById 修改数据
  65. func (s *productService) UpdateById(req *model.UpdateProductReq) (err error) {
  66. product, err := s.Dao.FindOne(req.Id)
  67. if err != nil {
  68. return err
  69. }
  70. if product == nil {
  71. return myerrors.NewMsgError(nil, "产品信息不存在或已被删除")
  72. }
  73. if err = gconv.Struct(req, product); err != nil {
  74. return
  75. }
  76. service.SetUpdatedInfo(product, s.GetCxtUserId(), s.GetCxtUserName())
  77. _, err = s.Dao.WherePri(req.Id).Update()
  78. if err != nil {
  79. g.Log().Error(err)
  80. return
  81. }
  82. return
  83. }
  84. //DeleteByIds 删掉数据
  85. func (s *productService) DeleteByIds(ids []int64) (err error) {
  86. if len(ids) == 1 {
  87. count, err := s.Dao.WherePri(ids[0]).Count()
  88. if err != nil {
  89. g.Log().Error(err)
  90. return
  91. }
  92. if count == 0 {
  93. return myerrors.NewMsgError(nil, "数据不存在或已被删除,请刷新页面")
  94. }
  95. } else {
  96. _, err := s.Dao.WhereIn(s.Dao.Columns.Id, ids).LockShared().Count()
  97. if err != nil {
  98. g.Log().Error(err)
  99. return err
  100. }
  101. result, err := s.Dao.WhereIn(s.Dao.Columns.Id, ids).Delete()
  102. if err != nil {
  103. g.Log().Error(err)
  104. return err
  105. }
  106. rows, err := result.RowsAffected()
  107. if err == nil {
  108. if len(ids) != int(rows) {
  109. return myerrors.NewMsgError(nil, fmt.Sprintf("应更新%s行,实际更新%s行", len(ids), int(rows)))
  110. }
  111. }
  112. }
  113. return nil
  114. }