base_product.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package service
  2. import (
  3. "context"
  4. "dashoo.cn/micro/app/dao"
  5. "dashoo.cn/micro/app/model"
  6. "fmt"
  7. "github.com/gogf/gf/errors/gerror"
  8. "github.com/gogf/gf/frame/g"
  9. "github.com/gogf/gf/util/gconv"
  10. )
  11. type baseProductService struct {
  12. *contextService
  13. Dao *dao.BaseProductDao
  14. }
  15. func NewBaseProductService(ctx context.Context) (svc *baseProductService, err error) {
  16. svc = new(baseProductService)
  17. if svc.contextService, err = svc.Init(ctx); err != nil {
  18. return nil, err
  19. }
  20. svc.Dao = dao.NewBaseProductDao(svc.Tenant)
  21. return svc, nil
  22. }
  23. //产品信息列表
  24. func (p *baseProductService)GetList (req *model.BaseProductSearchReq)(total int, productList []*model.BaseProduct, err error){
  25. productModel := dao.BaseProduct.M
  26. if req.KeyWords != "" {
  27. productModel = productModel.Where("prod_code =", req.KeyWords)
  28. }
  29. total, err = productModel.Count()
  30. if err != nil {
  31. g.Log().Error(err)
  32. err = gerror.New("获取总行数失败")
  33. return
  34. }
  35. if req.PageNum == 0 {
  36. req.PageNum = 1
  37. }
  38. err = productModel.Page(req.PageNum, req.PageSize).Order("id asc").Scan(&productList)
  39. return
  40. }
  41. //添加信息
  42. func (p *baseProductService)Create (req *model.AddBaseProductReq) (err error){
  43. productData := new(model.BaseProduct)
  44. if err = gconv.Struct(req, productData); err != nil {
  45. return
  46. }
  47. Model := dao.BaseProduct.M
  48. res, err := Model.Insert(productData)
  49. if err != nil {
  50. return
  51. }
  52. InsertId, _ := res.LastInsertId()
  53. fmt.Println(InsertId)
  54. return
  55. }
  56. //删掉数据
  57. func (p *baseProductService)Delete(id int64)(err error){
  58. db := dao.BaseProduct.M
  59. record, err := db.FindOne("Id", id)
  60. if err != nil {
  61. return err
  62. }
  63. if record.IsEmpty() {
  64. return gerror.New("该数据不存在")
  65. }
  66. _, err = db.Delete("Id", id)
  67. return err
  68. }
  69. //修改数据
  70. func(p *baseProductService)Update(req *model.UpdateBaseProductReq)(err error){
  71. db := dao.BaseProduct.M
  72. if i, _ := db.Where("id!=?", req.Id).Count(); i == 0 {
  73. err = gerror.New("该数据不存在")
  74. return
  75. }
  76. productData := new(model.BaseProduct)
  77. if err = gconv.Struct(req, productData); err != nil {
  78. return
  79. }
  80. _,err=db.Where(req.Id).Update(productData)
  81. if err!=nil{
  82. g.Log().Error(err)
  83. err = gerror.New("修改用户信息失败")
  84. return
  85. }
  86. return
  87. }
  88. //编辑查询单条查询
  89. func (p *baseProductService)GetProductInfo(id int64)(product *model.BaseProduct,err error){
  90. err = dao.BaseProduct.Where(dao.BaseProduct.Columns.Id, id).Scan(&product)
  91. if err != nil {
  92. g.Log().Error(err)
  93. return nil, gerror.New("获取用户数据失败")
  94. }
  95. return
  96. }