1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- package db
- import (
- "context"
- "github.com/sirupsen/logrus"
- "gorm.io/gorm"
- "youngee_b_api/model/gorm_model"
- )
- // CreateSupplier 新建服务商
- func CreateSupplier(ctx context.Context, newSupplier gorm_model.YoungeeSupplier) (*int, error) {
- db := GetWriteDB(ctx)
- err := db.Create(&newSupplier).Error
- if err != nil {
- return nil, err
- }
- return &newSupplier.SupplierId, nil
- }
- // GetSupplierByUserID 用户ID查找服务商信息
- func GetSupplierByUserID(ctx context.Context, userID int64) (*gorm_model.YoungeeSupplier, error) {
- db := GetReadDB(ctx)
- var supplierInfo *gorm_model.YoungeeSupplier
- err := db.Where("user_id = ?", userID).First(&supplierInfo).Error
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return nil, nil
- } else {
- return nil, err
- }
- }
- return supplierInfo, nil
- }
- // GetSupplierById 服务商ID查找服务商信息
- func GetSupplierById(ctx context.Context, supplierId int) (*gorm_model.YoungeeSupplier, error) {
- db := GetReadDB(ctx)
- var supplierInfo *gorm_model.YoungeeSupplier
- err := db.Where("supplier_id = ?", supplierId).First(&supplierInfo).Error
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return nil, nil
- } else {
- return nil, err
- }
- }
- return supplierInfo, nil
- }
- // UpdateSupplier 服务商信息更新
- func UpdateSupplier(ctx context.Context, supplierInfo *gorm_model.YoungeeSupplier) error {
- db := GetWriteDB(ctx)
- whereCondition := gorm_model.YoungeeSupplier{SupplierId: supplierInfo.SupplierId}
- err := db.Model(&gorm_model.YoungeeSupplier{}).Where(whereCondition).Updates(supplierInfo).Error
- if err != nil {
- return err
- }
- return nil
- }
- // CountSupplierUserByPhone 按照手机号码统计服务商数量
- func CountSupplierUserByPhone(ctx context.Context, phone string) (int64, error) {
- db := GetReadDB(ctx)
- var taskNum int64
- err := db.Model(gorm_model.YoungeeSupplier{}).Where("phone_number = ?", phone).Count(&taskNum).Error
- if err != nil {
- logrus.WithContext(ctx).Errorf("[CountSupplierUserByPhone] error read mysql, err:%+v", err)
- return 0, err
- }
- return taskNum, nil
- }
|