product.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package db
  2. import (
  3. "context"
  4. "github.com/caixw/lib.go/conv"
  5. "gorm.io/gorm"
  6. "youngee_m_api/model/gorm_model"
  7. )
  8. func CreateProduct(ctx context.Context, product gorm_model.YounggeeProduct) (*int64, error) {
  9. db := GetReadDB(ctx)
  10. err := db.Create(&product).Error
  11. if err != nil {
  12. return nil, err
  13. }
  14. return &product.ProductID, nil
  15. }
  16. func GetProductByID(ctx context.Context, productID int64) (*gorm_model.YounggeeProduct, error) {
  17. db := GetReadDB(ctx)
  18. product := &gorm_model.YounggeeProduct{}
  19. err := db.First(&product, productID).Error
  20. if err != nil {
  21. if err == gorm.ErrRecordNotFound {
  22. return nil, nil
  23. } else {
  24. return nil, err
  25. }
  26. }
  27. return product, nil
  28. }
  29. func GetEnterpriseIDByUserID(ctx context.Context, UserId string) int64 {
  30. db := GetReadDB(ctx)
  31. enterpriseInfo := gorm_model.Enterprise{}
  32. userId := conv.MustInt64(UserId, 0)
  33. err := db.Where("user_id = ?", userId).Find(&enterpriseInfo).Error
  34. if err != nil {
  35. return 0
  36. }
  37. enterpriseID := enterpriseInfo.EnterpriseID
  38. return enterpriseID
  39. }
  40. func GetProductByEnterpriseID(ctx context.Context, enterpriseID int64) ([]gorm_model.YounggeeProduct, error) {
  41. db := GetReadDB(ctx)
  42. var products []gorm_model.YounggeeProduct
  43. err := db.Where("enterprise_id = ?", enterpriseID).Find(&products).Error
  44. if err != nil {
  45. return nil, err
  46. }
  47. return products, nil
  48. }
  49. func GetEnterpriseIDByProductID(ctx context.Context, ProductID int64) int64 {
  50. db := GetReadDB(ctx)
  51. ProjectInfo := gorm_model.ProjectInfo{}
  52. err := db.Where("product_id = ?", ProductID).Find(&ProjectInfo).Error
  53. if err != nil {
  54. return 0
  55. }
  56. enterpriseID := ProjectInfo.EnterpriseID
  57. return enterpriseID
  58. }
  59. func UpdateProduct(ctx context.Context, product gorm_model.YounggeeProduct) (*int64, error) {
  60. db := GetReadDB(ctx)
  61. err := db.Model(&product).Updates(product).Error
  62. if err != nil {
  63. return nil, err
  64. }
  65. return &product.ProductID, nil
  66. }