123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- package db
- import (
- "context"
- "fmt"
- "github.com/issue9/conv"
- "reflect"
- "strconv"
- "youngee_b_api/model/common_model"
- "youngee_b_api/model/gorm_model"
- "youngee_b_api/model/http_model"
- "youngee_b_api/pack"
- "youngee_b_api/util"
- "github.com/sirupsen/logrus"
- "gorm.io/gorm"
- )
- 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 UpdateProject(ctx context.Context, project gorm_model.ProjectInfo) (*int64, error) {
- db := GetReadDB(ctx)
- err := db.Model(&project).Updates(project).Error
- if err != nil {
- return nil, err
- }
- return &project.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 tag == "project_status" && util.IsBlank(value) {
- db = db.Where(fmt.Sprintf("project_status != 1"))
- }
- if !util.IsBlank(value) && tag != "updated_at" && tag != "project_name" {
- db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
- }
- if tag == "updated_at" && value.Interface() != "0" {
- 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
- }
- func GetProjectTaskList(ctx context.Context, projectID string, pageSize, pageNum int64, conditions *common_model.TaskConditions) ([]*http_model.ProjectTaskInfo, int64, error) {
- db := GetReadDB(ctx)
- // 查询task表信息
- db = db.Debug().Model(gorm_model.YoungeeTaskInfo{})
- // 根据Project条件过滤
- conditionType := reflect.TypeOf(conditions).Elem()
- conditionValue := reflect.ValueOf(conditions).Elem()
- for i := 0; i < conditionType.NumField(); i++ {
- field := conditionType.Field(i)
- tag := field.Tag.Get("condition")
- value := conditionValue.FieldByName(field.Name)
- if !util.IsBlank(value) && tag != "platform_nickname" {
- db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
- } else if tag == "platform_nickname" {
- continue
- }
- }
- var taskInfos []gorm_model.YoungeeTaskInfo
- db = db.Model(gorm_model.YoungeeTaskInfo{})
- // 查询总数
- var totalTask int64
- if err := db.Count(&totalTask).Error; err != nil {
- logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
- return nil, 0, err
- }
- db.Order("task_id").Find(&taskInfos)
- // 查询账号id
- var accountIds []int
- taskMap := make(map[int]gorm_model.YoungeeTaskInfo)
- for _, taskInfo := range taskInfos {
- accountIds = append(accountIds, taskInfo.AccountID)
- taskMap[taskInfo.AccountID] = taskInfo
- }
- db1 := GetReadDB(ctx)
- db1 = db1.Debug().Model(gorm_model.YoungeePlatformAccountInfo{})
- // 根据Project条件过滤
- conditionType2 := reflect.TypeOf(conditions).Elem()
- conditionValue2 := reflect.ValueOf(conditions).Elem()
- for i := 0; i < conditionType2.NumField(); i++ {
- field := conditionType2.Field(i)
- tag := field.Tag.Get("condition")
- value := conditionValue2.FieldByName(field.Name)
- if !util.IsBlank(value) && tag == "platform_nickname" { // input:1 database:1,2,3 string
- db1 = db1.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
- }
- }
- var accountInfos []gorm_model.YoungeePlatformAccountInfo
- db1 = db1.Model(gorm_model.YoungeePlatformAccountInfo{}).Where("account_id IN ?", accountIds).Find(&accountInfos)
- // 查询总数
- var totalAccount int64
- if err := db1.Count(&totalAccount).Error; err != nil {
- logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
- return nil, 0, err
- }
- var misNum int64
- if totalAccount > totalTask {
- misNum = totalAccount - totalTask
- } else {
- misNum = totalTask - totalAccount
- }
- logrus.Println("totalAccount,totalTask,misNum:", totalAccount, totalTask, misNum)
- // 查询该页数据
- limit := pageSize + misNum
- offset := pageSize * pageNum // assert pageNum start with 0
- err := db.Order("task_id").Limit(int(limit)).Offset(int(offset)).Error
- if err != nil {
- logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
- return nil, 0, err
- }
- accountMap := make(map[int]gorm_model.YoungeePlatformAccountInfo)
- for _, accountInfo := range accountInfos {
- accountMap[accountInfo.AccountID] = accountInfo
- }
- var taskAccounts []*http_model.TaskAccount
- var projectTasks []*http_model.ProjectTaskInfo
- for _, accountId := range accountIds {
- taskAccount := new(http_model.TaskAccount)
- _, ok := taskMap[accountId]
- _, ok2 := accountMap[accountId]
- if ok && ok2 {
- taskAccount.Task = taskMap[accountId]
- taskAccount.Account = accountMap[accountId]
- }
- taskAccounts = append(taskAccounts, taskAccount)
- }
- projectTasks = pack.TaskAccountToTaskInfo(taskAccounts)
- var fullProjectTasks []*http_model.ProjectTaskInfo
- // 删除只存在于一个表中的元素
- for i := 0; i < len(projectTasks); i++ {
- if projectTasks[i].TaskID != 0 {
- fullProjectTasks = append(fullProjectTasks, projectTasks[i])
- }
- }
- var total int64
- if totalTask > totalAccount {
- total = totalAccount
- } else {
- total = totalTask
- }
- return fullProjectTasks, total, nil
- }
- func GetProjectDetail(ctx context.Context, projectID int64) (*gorm_model.ProjectInfo, error) {
- db := GetReadDB(ctx)
- var ProjectDetail *gorm_model.ProjectInfo
- err := db.Where("project_id = ?", projectID).First(&ProjectDetail).Error
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return nil, nil
- } else {
- return nil, err
- }
- }
- return ProjectDetail, nil
- }
- func GetProjectPhoto(ctx context.Context, ProjectID int64) ([]gorm_model.ProjectPhoto, error) {
- db := GetReadDB(ctx)
- ProjectPhoto := []gorm_model.ProjectPhoto{}
- err := db.Where("project_id=?", ProjectID).Find(&ProjectPhoto).Error
- if err != nil {
- return nil, err
- }
- return ProjectPhoto, nil
- }
- func GetRecruitStrategys(ctx context.Context, ProjectID int64) ([]gorm_model.RecruitStrategy, error) {
- db := GetReadDB(ctx)
- RecruitStrategys := []gorm_model.RecruitStrategy{}
- err := db.Where("project_id=?", ProjectID).Find(&RecruitStrategys).Error
- if err != nil {
- return nil, err
- }
- return RecruitStrategys, nil
- }
- func ChangeTaskStatus(ctx context.Context, data []string, taskStatus string) error {
- db := GetReadDB(ctx)
- taskInfo := gorm_model.YoungeeTaskInfo{}
- taskSta, err := strconv.Atoi(taskStatus)
- if err != nil {
- return err
- }
- if err := db.Debug().Model(&taskInfo).Where("task_id IN ?", data).
- Updates(gorm_model.YoungeeTaskInfo{TaskStatus: taskSta}).Error; err != nil {
- logrus.WithContext(ctx).Errorf("[ChangeTaskStatus] error query mysql total, err:%+v", err)
- return err
- }
- return nil
- }
- func GetProjectTalentList(ctx context.Context, projectID string, pageSize, pageNum int64, conditions *common_model.TaskConditions) ([]*http_model.ProjectTalentInfo, int64, error) {
- db := GetReadDB(ctx)
- // 查询Task表信息
- db = db.Debug().Model(gorm_model.YoungeeTaskInfo{})
- // 根据Project条件过滤
- conditionType := reflect.TypeOf(conditions).Elem()
- conditionValue := reflect.ValueOf(conditions).Elem()
- for i := 0; i < conditionType.NumField(); i++ {
- field := conditionType.Field(i)
- tag := field.Tag.Get("condition")
- value := conditionValue.FieldByName(field.Name)
- if !util.IsBlank(value) && tag != "platform_nickname" {
- db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
- } else if tag == "platform_nickname" {
- continue
- }
- }
- var taskInfos []gorm_model.YoungeeTaskInfo
- db = db.Model(gorm_model.YoungeeTaskInfo{})
- // 查询总数
- var totalTask int64
- if err := db.Count(&totalTask).Error; err != nil {
- logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
- return nil, 0, err
- }
- db.Order("task_id").Find(&taskInfos)
- // 查询账号id
- var taskIds []int
- taskMap := make(map[int]gorm_model.YoungeeTaskInfo)
- for _, taskInfo := range taskInfos {
- taskIds = append(taskIds, taskInfo.TaskID)
- taskMap[taskInfo.TaskID] = taskInfo
- }
- db1 := GetReadDB(ctx)
- db1 = db1.Debug().Model(gorm_model.YoungeeTaskLogistics{})
- // 根据Project条件过滤
- conditionType2 := reflect.TypeOf(conditions).Elem()
- conditionValue2 := reflect.ValueOf(conditions).Elem()
- for i := 0; i < conditionType2.NumField(); i++ {
- field := conditionType2.Field(i)
- tag := field.Tag.Get("condition")
- value := conditionValue2.FieldByName(field.Name)
- if !util.IsBlank(value) && tag == "platform_nickname" { // input:1 database:1,2,3 string
- db1 = db1.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
- }
- }
- var logisticsInfos []gorm_model.YoungeeTaskLogistics
- db1 = db1.Model(gorm_model.YoungeeTaskLogistics{}).Where("task_id IN ?", taskIds).Find(&logisticsInfos)
- logisticsMap := make(map[int]gorm_model.YoungeeTaskLogistics)
- for _, logisticsInfo := range logisticsInfos {
- logisticsMap[conv.MustInt(logisticsInfo.TaskID)] = logisticsInfo
- }
- /*
- db1 := GetReadDB(ctx)
- db1 = db1.Debug().Model(gorm_model.YoungeePlatformAccountInfo{})
- // 根据Project条件过滤
- conditionType2 := reflect.TypeOf(conditions).Elem()
- conditionValue2 := reflect.ValueOf(conditions).Elem()
- for i := 0; i < conditionType2.NumField(); i++ {
- field := conditionType2.Field(i)
- tag := field.Tag.Get("condition")
- value := conditionValue2.FieldByName(field.Name)
- if !util.IsBlank(value) && tag == "platform_nickname" { // input:1 database:1,2,3 string
- db1 = db1.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
- }
- }
- var accountInfos []gorm_model.YoungeePlatformAccountInfo
- db1 = db1.Model(gorm_model.YoungeePlatformAccountInfo{}).Where("account_id IN ?", accountIds).Find(&accountInfos)
- */
- // 查询总数
- var totalAccount int64
- if err := db1.Count(&totalAccount).Error; err != nil {
- logrus.WithContext(ctx).Errorf("[GetProjectTalentList] error query mysql total, err:%+v", err)
- return nil, 0, err
- }
- var misNum int64
- if totalAccount > totalTask {
- misNum = totalAccount - totalTask
- } else {
- misNum = totalTask - totalAccount
- }
- logrus.Println("totalAccount,totalTalent,misNum:", totalAccount, totalTask, misNum)
- // 查询该页数据
- limit := pageSize + misNum
- offset := pageSize * pageNum // assert pageNum start with 0
- err := db.Order("task_id").Limit(int(limit)).Offset(int(offset)).Error
- if err != nil {
- logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
- return nil, 0, err
- }
- /*
- accountMap := make(map[int]gorm_model.YoungeePlatformAccountInfo)
- for _, accountInfo := range accountInfos {
- accountMap[accountInfo.AccountID] = accountInfo
- }
- */
- var talentAccounts []*http_model.TalentAccount
- var projectTalents []*http_model.ProjectTalentInfo
- for _, taskId := range taskIds {
- talentAccount := new(http_model.TalentAccount)
- _, ok := taskMap[taskId]
- _, ok2 := logisticsMap[taskId]
- if ok && ok2 {
- talentAccount.Talent = taskMap[taskId]
- talentAccount.Logistics = logisticsMap[taskId]
- }
- talentAccounts = append(talentAccounts, talentAccount)
- }
- projectTalents = pack.TalentAccountToTaskInfo(talentAccounts)
- var fullProjectTalents []*http_model.ProjectTalentInfo
- // 删除只存在于一个表中的元素
- for i := 0; i < len(projectTalents); i++ {
- if projectTalents[i].TaskID != 0 {
- fullProjectTalents = append(fullProjectTalents, projectTalents[i])
- }
- }
- var total int64
- if totalTask > totalAccount {
- total = totalAccount
- } else {
- total = totalTask
- }
- return fullProjectTalents, total, nil
- }
|