1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- package db
- import (
- "context"
- "fmt"
- "github.com/sirupsen/logrus"
- "reflect"
- "youngee_b_api/model/common_model"
- "youngee_b_api/model/gorm_model"
- )
- 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)
- conditionValue := reflect.ValueOf(condition)
- for i := 0; i < conditionValue.NumField(); i++ {
- field := conditionType.Field(i)
- tag := field.Tag.Get("condition")
- value := conditionValue.FieldByName(field.Name)
- db = db.Where(fmt.Sprintf("%s = ?", tag), value)
- }
- // 查询总数
- 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 := pageNum
- offset := pageSize * pageNum // assert pageNum start with 0
- err := db.Order("updated_at desc").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
- }
|