package db import ( "context" "fmt" "github.com/sirupsen/logrus" "reflect" "youngee_b_api/model/common_model" "youngee_b_api/model/gorm_model" "youngee_b_api/util" ) // GetFullLocalLifeList 查看所有本地生活列表 func GetFullLocalLifeList(ctx context.Context, pageSize, pageNum int32, condition *common_model.SLocalLifeCondition) ([]*gorm_model.YounggeeLocalLifeInfo, int64, error) { db := GetReadDB(ctx) // 根据带货任务状态过滤 db = db.Debug().Model(gorm_model.YounggeeLocalLifeInfo{}).Where("task_status = 4") // 根据Project条件过滤 conditionType := reflect.TypeOf(condition).Elem() conditionValue := reflect.ValueOf(condition).Elem() for i := 0; i < conditionType.NumField(); i++ { field := conditionType.Field(i) tag := field.Tag.Get("condition") value := conditionValue.FieldByName(field.Name) if (tag == "local_id" || tag == "local_name") && !util.IsBlank(value) { db = db.Where(fmt.Sprintf("local_id like '%%%v%%' or local_name like '%%%v%%'", value.Interface(), value.Interface())) } else if tag == "updated_at" && value.Interface() != "0" { db = db.Where(fmt.Sprintf("updated_at like '%s%%'", value.Interface())) } else if !util.IsBlank(value) && tag != "updated_at" { db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface()) } } // 查询总数 var total int64 var fullLocals []*gorm_model.YounggeeLocalLifeInfo if err := db.Count(&total).Error; err != nil { logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql total, err:%+v", err) return nil, 0, err } // 查询该页数据 limit := pageSize offset := pageSize * pageNum // assert pageNum start with 0 err := db.Order("updated_at desc").Limit(int(limit)).Offset(int(offset)).Find(&fullLocals).Error if err != nil { logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql total, err:%+v", err) return nil, 0, err } return fullLocals, total, nil } // GetLocalLifeDetail 根据本地生活ID查询本地生活详情 func GetLocalLifeDetail(ctx context.Context, localLifeId string) (*gorm_model.YounggeeLocalLifeInfo, error) { db := GetReadDB(ctx) var localLifeInfo *gorm_model.YounggeeLocalLifeInfo whereCondition := gorm_model.YounggeeLocalLifeInfo{LocalId: localLifeId} err := db.Model(gorm_model.YounggeeLocalLifeInfo{}).Where(whereCondition).Find(&localLifeInfo).Error if err != nil { return nil, err } return localLifeInfo, nil } // CreateSLocalLife 创建服务商加入商单后的本地生活详情 func CreateSLocalLife(ctx context.Context, newSLocal *gorm_model.YounggeeSLocalLifeInfo) error { db := GetWriteDB(ctx) err := db.Create(&newSLocal).Error if err != nil { return err } return nil } // FindBriefByLocalId 根据localId查找Brief func FindBriefByLocalId(ctx context.Context, localId string) ([]gorm_model.LocalLifeBrief, error) { db := GetReadDB(ctx) var sProjectInfo []gorm_model.LocalLifeBrief // 1. 根据服务商种草任务id过滤 err := db.Model(gorm_model.LocalLifeBrief{}).Where("local_id = ?", localId).Find(&sProjectInfo).Error if err != nil { logrus.WithContext(ctx).Errorf("[GetSProjectDetail] error query mysql, err:%+v", err) return nil, err } return sProjectInfo, nil } // FindMaterialByLocalId 根据localId查找Material func FindMaterialByLocalId(ctx context.Context, localId string) ([]gorm_model.LocalLifeMaterial, error) { db := GetReadDB(ctx) var sProjectInfo []gorm_model.LocalLifeMaterial // 1. 根据服务商种草任务id过滤 err := db.Model(gorm_model.LocalLifeMaterial{}).Where("local_id = ?", localId).Find(&sProjectInfo).Error if err != nil { logrus.WithContext(ctx).Errorf("[GetSProjectDetail] error query mysql, err:%+v", err) return nil, err } return sProjectInfo, nil } // FindSLocalByLocalIdAndSupplierId 根据LocalId和SupplierId查找SLocal func FindSLocalByLocalIdAndSupplierId(ctx context.Context, localId string, supplierId int) (int64, error) { db := GetWriteDB(ctx) whereCondition := gorm_model.YounggeeSLocalLifeInfo{ LocalId: localId, SupplierId: supplierId, } var total int64 err := db.Debug().Model(gorm_model.YounggeeSLocalLifeInfo{}).Where(whereCondition).Count(&total).Error if err != nil { return 0, err } return total, nil } // GetFullSLocalLifeList 查看加入商单后的本地生活列表 func GetFullSLocalLifeList(ctx context.Context, pageSize, pageNum int32, condition *common_model.SLocalLifeCondition) ([]*gorm_model.YounggeeSLocalLifeInfo, int64, error) { db := GetReadDB(ctx) // 根据带货任务状态过滤 db = db.Debug().Model(gorm_model.YounggeeSLocalLifeInfo{}).Where("task_status = 4") // 根据Project条件过滤 conditionType := reflect.TypeOf(condition).Elem() conditionValue := reflect.ValueOf(condition).Elem() for i := 0; i < conditionType.NumField(); i++ { field := conditionType.Field(i) tag := field.Tag.Get("condition") value := conditionValue.FieldByName(field.Name) if (tag == "local_id" || tag == "local_name") && !util.IsBlank(value) { db = db.Where(fmt.Sprintf("local_id like '%%%v%%' or local_name like '%%%v%%'", value.Interface(), value.Interface())) } else if tag == "updated_at" && value.Interface() != "0" { db = db.Where(fmt.Sprintf("updated_at like '%s%%'", value.Interface())) } else if !util.IsBlank(value) && tag != "updated_at" { db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface()) } } // 查询总数 var total int64 var fullLocals []*gorm_model.YounggeeSLocalLifeInfo if err := db.Count(&total).Error; err != nil { logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql total, err:%+v", err) return nil, 0, err } // 查询该页数据 limit := pageSize offset := pageSize * pageNum // assert pageNum start with 0 err := db.Order("updated_at desc").Limit(int(limit)).Offset(int(offset)).Find(&fullLocals).Error if err != nil { logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql total, err:%+v", err) return nil, 0, err } return fullLocals, total, nil }