12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- package db
- import (
- "context"
- "github.com/caixw/lib.go/conv"
- "gorm.io/gorm"
- "strconv"
- "youngee_m_api/model/gorm_model"
- )
- func CreateProduct(ctx context.Context, product gorm_model.YounggeeProduct) (*int64, error) {
- db := GetReadDB(ctx)
- err := db.Create(&product).Error
- if err != nil {
- return nil, err
- }
- return &product.ProductID, nil
- }
- func GetProductByID(ctx context.Context, productID int64) (*gorm_model.YounggeeProduct, error) {
- db := GetReadDB(ctx)
- product := &gorm_model.YounggeeProduct{}
- err := db.First(&product, productID).Error
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return nil, nil
- } else {
- return nil, err
- }
- }
- return product, nil
- }
- func GetEnterpriseIDByUserID(ctx context.Context, UserId string) string {
- db := GetReadDB(ctx)
- enterpriseInfo := gorm_model.Enterprise{}
- userId := conv.MustInt64(UserId, 0)
- err := db.Where("user_id = ?", userId).Find(&enterpriseInfo).Error
- if err != nil {
- return ""
- }
- enterpriseID := enterpriseInfo.EnterpriseID
- return enterpriseID
- }
- func GetUserIDByEnterpriseID(ctx context.Context, enterpriseId string) string {
- db := GetReadDB(ctx)
- enterpriseInfo := gorm_model.Enterprise{}
- err := db.Where("enterprise_id = ?", enterpriseId).Find(&enterpriseInfo).Error
- if err != nil {
- return ""
- }
- enterpriseID := enterpriseInfo.UserID
- return strconv.FormatInt(enterpriseID, 10)
- }
- func GetProductByEnterpriseID(ctx context.Context, enterpriseID string) ([]gorm_model.YounggeeProduct, error) {
- db := GetReadDB(ctx)
- var products []gorm_model.YounggeeProduct
- err := db.Where("enterprise_id = ?", enterpriseID).Find(&products).Error
- if err != nil {
- return nil, err
- }
- return products, nil
- }
- func GetEnterpriseIDByProductID(ctx context.Context, ProductID int64) string {
- db := GetReadDB(ctx)
- ProjectInfo := gorm_model.ProjectInfo{}
- err := db.Where("product_id = ?", ProductID).Find(&ProjectInfo).Error
- if err != nil {
- return ""
- }
- enterpriseID := ProjectInfo.EnterpriseID
- return enterpriseID
- }
- func UpdateProduct(ctx context.Context, product gorm_model.YounggeeProduct) (*int64, error) {
- db := GetReadDB(ctx)
- err := db.Model(&product).Updates(product).Error
- if err != nil {
- return nil, err
- }
- return &product.ProductID, nil
- }
|