123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- 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"
- )
- func CreateProject(ctx context.Context, projectInfo gorm_model.ProjectInfo) (*int64, error) {
- db := GetWriteDB(ctx)
- err := db.Create(&projectInfo).Error
- if err != nil {
- return nil, err
- }
- return &projectInfo.ProjectID, nil
- }
- func GetFullProjectList(ctx context.Context, enterpriseID int64, pageSize, pageNum int32, condition *common_model.ProjectCondition) ([]*gorm_model.ProjectInfo, int64, error) {
- db := GetReadDB(ctx)
- // 根据企业id过滤
- db = db.Debug().Model(gorm_model.ProjectInfo{}).Where("enterprise_id = ?", enterpriseID)
- // 根据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 ! isBlank(value) {
- // db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
- //}
- if ! util.IsBlank(value) && tag != "updated_at"&& tag != "project_name"{
- db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
- }
- if tag == "updated_at"{
- fmt.Println(value.Interface())
- db = db.Where(fmt.Sprintf("%s > ?", tag), value.Interface())
- }
- if tag == "project_name" && ! util.IsBlank(value){
- db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
- }
- }
- // 查询总数
- var total int64
- var fullProjects []*gorm_model.ProjectInfo
- 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("project_id").Limit(int(limit)).Offset(int(offset)).Find(&fullProjects).Error
- if err != nil {
- logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql total, err:%+v", err)
- return nil, 0, err
- }
- return fullProjects, total, nil
- }
|