product.go 2.2 KB

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