|
@@ -0,0 +1,149 @@
|
|
|
+package db
|
|
|
+
|
|
|
+import (
|
|
|
+ "context"
|
|
|
+ "fmt"
|
|
|
+ "github.com/sirupsen/logrus"
|
|
|
+ "reflect"
|
|
|
+ "strings"
|
|
|
+ "youngee_b_api/model/common_model"
|
|
|
+ "youngee_b_api/model/gorm_model"
|
|
|
+ "youngee_b_api/util"
|
|
|
+)
|
|
|
+
|
|
|
+func DeleteSelection(ctx context.Context, SelectionId string) error {
|
|
|
+ db := GetReadDB(ctx)
|
|
|
+ err := db.Where("selection_id = ?", SelectionId).Delete(&gorm_model.YounggeeSelectionInfo{}).Error
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func GetSelectionList(ctx context.Context, enterpriseID string, pageSize, pageNum int64, conditions *common_model.SelectionConditions) ([]*gorm_model.YounggeeSelectionInfo, int64, error) {
|
|
|
+ db := GetReadDB(ctx)
|
|
|
+ db = db.Debug().Model(gorm_model.YounggeeSelectionInfo{}).Where("enterprise_id = ?", enterpriseID)
|
|
|
+ conditionType := reflect.TypeOf(conditions).Elem()
|
|
|
+ conditionValue := reflect.ValueOf(conditions).Elem()
|
|
|
+ selectionStatus := ""
|
|
|
+ searchValue := ""
|
|
|
+ for i := 0; i < conditionType.NumField(); i++ {
|
|
|
+ field := conditionType.Field(i)
|
|
|
+ tag := field.Tag.Get("condition")
|
|
|
+ value := conditionValue.FieldByName(field.Name)
|
|
|
+ if tag == "selection_status" {
|
|
|
+ selectionStatus = fmt.Sprintf("%v", value.Interface())
|
|
|
+ } else if tag == "search_value" {
|
|
|
+ searchValue = fmt.Sprintf("%v", value.Interface())
|
|
|
+ } else if tag == "submit_at" && value.Interface() != "" {
|
|
|
+ db = db.Where(fmt.Sprintf("submit_at like '%s%%'", value.Interface()))
|
|
|
+ } else if tag == "task_ddl" && value.Interface() != "" {
|
|
|
+ db = db.Where(fmt.Sprintf("task_ddl like '%s%%'", value.Interface()))
|
|
|
+ } else if !util.IsBlank(value) && tag != "task_ddl" && tag != "submit_at" && tag != "search_value" {
|
|
|
+ db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 查询总数
|
|
|
+ var total int64
|
|
|
+ var selectionInfos []*gorm_model.YounggeeSelectionInfo
|
|
|
+ if err := db.Count(&total).Error; err != nil {
|
|
|
+ logrus.WithContext(ctx).Errorf("[GetSelectionList] error query mysql total, err:%+v", err)
|
|
|
+ return nil, 0, err
|
|
|
+ }
|
|
|
+ // 查询该页数据
|
|
|
+ limit := pageSize
|
|
|
+ offset := pageSize * pageNum // assert pageNum start with 0
|
|
|
+ if selectionStatus == "1" {
|
|
|
+ err := db.Order("submit_at desc").Limit(int(limit)).Offset(int(offset)).Find(&selectionInfos).Error
|
|
|
+ if err != nil {
|
|
|
+ logrus.WithContext(ctx).Errorf("[GetSelectionList] error query mysql total, err:%+v", err)
|
|
|
+ return nil, 0, err
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ err := db.Order("task_ddl desc").Limit(int(limit)).Offset(int(offset)).Find(&selectionInfos).Error
|
|
|
+ if err != nil {
|
|
|
+ logrus.WithContext(ctx).Errorf("[GetSelectionList] error query mysql total, err:%+v", err)
|
|
|
+ return nil, 0, err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ var newSelectionInfos []*gorm_model.YounggeeSelectionInfo
|
|
|
+ for _, v := range selectionInfos {
|
|
|
+ if searchValue == "" {
|
|
|
+ newSelectionInfos = append(newSelectionInfos, v)
|
|
|
+ } else if strings.Contains(v.SelectionID, searchValue) {
|
|
|
+ newSelectionInfos = append(newSelectionInfos, v)
|
|
|
+ } else if strings.Contains(v.SelectionName, searchValue) {
|
|
|
+ newSelectionInfos = append(newSelectionInfos, v)
|
|
|
+ } else {
|
|
|
+ total--
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return newSelectionInfos, total, nil
|
|
|
+}
|
|
|
+
|
|
|
+func GetSelectionInfo(ctx context.Context, selectionId string) (*gorm_model.YounggeeSelectionInfo, error) {
|
|
|
+ db := GetReadDB(ctx)
|
|
|
+ selectionInfo := gorm_model.YounggeeSelectionInfo{}
|
|
|
+ err := db.Model(gorm_model.YounggeeSelectionInfo{}).Where("selection_id = ?", selectionId).Find(&selectionInfo).Error
|
|
|
+ if err != nil {
|
|
|
+ logrus.WithContext(ctx).Errorf("[GetSelectionList] error query mysql total, err:%+v", err)
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ return &selectionInfo, nil
|
|
|
+}
|
|
|
+
|
|
|
+func GetSelectionBriefInfo(ctx context.Context, selectionId string) ([]*gorm_model.YounggeeSecBrief, error) {
|
|
|
+ db := GetReadDB(ctx)
|
|
|
+ var selectionBriefInfos []*gorm_model.YounggeeSecBrief
|
|
|
+ err := db.Model(gorm_model.YounggeeSecBrief{}).Where("selection_id = ?", selectionId).Find(&selectionBriefInfos).Error
|
|
|
+ if err != nil {
|
|
|
+ logrus.WithContext(ctx).Errorf("[GetSelectionBriefInfo] error query mysql, err:%+v", err)
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ return selectionBriefInfos, nil
|
|
|
+}
|
|
|
+
|
|
|
+func GetSelectionExampleInfo(ctx context.Context, selectionId string) ([]*gorm_model.YounggeeSecExample, error) {
|
|
|
+ db := GetReadDB(ctx)
|
|
|
+ var selectionExampleInfos []*gorm_model.YounggeeSecExample
|
|
|
+ err := db.Model(gorm_model.YounggeeSecExample{}).Where("selection_id = ?", selectionId).Find(&selectionExampleInfos).Error
|
|
|
+ if err != nil {
|
|
|
+ logrus.WithContext(ctx).Errorf("[GetSelectionExampleInfo] error query, err:%+v", err)
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ return selectionExampleInfos, nil
|
|
|
+}
|
|
|
+
|
|
|
+func GetProductInfo(ctx context.Context, selectionId string) (*gorm_model.YounggeeProduct, error) {
|
|
|
+ db := GetReadDB(ctx)
|
|
|
+ productId := 0
|
|
|
+ err := db.Model(gorm_model.YounggeeSelectionInfo{}).Select("product_id").Where("selection_id = ?", selectionId).Find(&productId).Error
|
|
|
+ if err != nil {
|
|
|
+ logrus.WithContext(ctx).Errorf("[GetProductInfo] error query mysql, err:%+v", err)
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ productInfo := gorm_model.YounggeeProduct{}
|
|
|
+ err = db.Model(gorm_model.YounggeeProduct{}).Where("product_id = ?", productId).Find(&productInfo).Error
|
|
|
+ if err != nil {
|
|
|
+ logrus.WithContext(ctx).Errorf("[GetProductInfo] error query mysql, err:%+v", err)
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ return &productInfo, nil
|
|
|
+}
|
|
|
+
|
|
|
+func GetProductPhotoInfo(ctx context.Context, selectionId string) ([]*gorm_model.YounggeeProductPhoto, error) {
|
|
|
+ db := GetReadDB(ctx)
|
|
|
+ productId := 0
|
|
|
+ err := db.Model(gorm_model.YounggeeSelectionInfo{}).Select("product_id").Where("selection_id = ?", selectionId).Find(&productId).Error
|
|
|
+ if err != nil {
|
|
|
+ logrus.WithContext(ctx).Errorf("[GetProductInfo] error query mysql, err:%+v", err)
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ var productPhotoInfo []*gorm_model.YounggeeProductPhoto
|
|
|
+ err = db.Model(gorm_model.YounggeeProductPhoto{}).Where("product_id = ?", productId).Find(&productPhotoInfo).Error
|
|
|
+ if err != nil {
|
|
|
+ logrus.WithContext(ctx).Errorf("[GetProductInfo] error query mysql, err:%+v", err)
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ return productPhotoInfo, nil
|
|
|
+}
|