package db import ( "context" "fmt" "github.com/caixw/lib.go/conv" "github.com/sirupsen/logrus" "reflect" "strings" "time" "youngee_m_api/model/common_model" "youngee_m_api/model/gorm_model" "youngee_m_api/model/http_model" "youngee_m_api/pack" "youngee_m_api/util" ) //新增 func CreateLogistics(ctx context.Context, logistics gorm_model.YoungeeTaskLogistics, RecruitStrategyID int64) (*int64, error) { db := GetReadDB(ctx) err := db.Create(&logistics).Error if err != nil { logrus.WithContext(ctx).Errorf("[logistics db] call CreateLogistics error,err:%+v", err) return nil, err } return &logistics.LogisticsID, nil } //修改接口 func UpdateLogistics(ctx context.Context, logistics gorm_model.YoungeeTaskLogistics) (*int64, error) { db := GetReadDB(ctx) err := db.Model(&logistics).Where("task_id = ?", logistics.TaskID).Updates(logistics).Error if err != nil { logrus.WithContext(ctx).Errorf("[logistics db] call UpdateLogistics error,err:%+v", err) return nil, err } return &logistics.LogisticsID, nil } // 更改签收时间 func SignForReceipt(ctx context.Context, taskID int64) error { db := GetReadDB(ctx) err := db.Model(gorm_model.YoungeeTaskLogistics{}).Where("task_id = ?", taskID).Update("signed_time", time.Now()).Error if err != nil { logrus.WithContext(ctx).Errorf("[logistics db] call UpdateLogistics error,err:%+v", err) return err } return nil } // 查询包含物流信息的task list func GetTaskLogisticsList(ctx context.Context, projectID string, pageSize, pageNum int64, conditions *common_model.TalentConditions) ([]*http_model.TaskLogisticsInfo, int64, error) { db := GetReadDB(ctx) // 查询Task表信息 db = db.Debug().Model(gorm_model.YoungeeTaskInfo{}) // 根据Project条件过滤 conditionType := reflect.TypeOf(conditions).Elem() conditionValue := reflect.ValueOf(conditions).Elem() var platform_nickname string = "" 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) { //logrus.Println("tag: ", tag) if tag == "platform_nickname" { platform_nickname = fmt.Sprintf("%v", value.Interface()) continue } else if tag == "project_id" { db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface()) } else if tag == "strategy_ids" { strategyIds := strings.Split(fmt.Sprintf("%v", value.Interface()), ",") var strategyIdList []int for _, strategyId := range strategyIds { strategyIdList = append(strategyIdList, conv.MustInt(strategyId, 0)) } db = db.Where("strategy_id in ?", strategyIdList) } else { db = db.Where(fmt.Sprintf("%s like '%%%v%%'", tag, value.Interface())) } } } 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) //fmt.Printf("过滤展示 %+v %+v \n", value, tag) if !util.IsBlank(value) && tag == "platform_nickname" { // input:1 taskIdsbase: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, 0)] = logisticsInfo } // 查询总数 var totalLogistics int64 if err := db1.Count(&totalLogistics).Error; err != nil { logrus.WithContext(ctx).Errorf("[GetProjectTalentList] error query mysql total, err:%+v", err) return nil, 0, err } // 查询该页数据 limit := pageSize 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 } var TaskLogisticss []*http_model.TaskLogistics var taskLogisticss []*http_model.TaskLogisticsInfo var newTaskLinks []*http_model.TaskLogisticsInfo for _, taskId := range taskIds { TaskLogistics := new(http_model.TaskLogistics) TaskLogistics.Talent = taskMap[taskId] TaskLogistics.Logistics = logisticsMap[taskId] TaskLogisticss = append(TaskLogisticss, TaskLogistics) } taskLogisticss = pack.TaskLogisticsToTaskInfo(TaskLogisticss) for _, v := range taskLogisticss { if platform_nickname == "" { newTaskLinks = append(newTaskLinks, v) } else if strings.Contains(v.PlatformNickname, platform_nickname) { newTaskLinks = append(newTaskLinks, v) } else { totalTask-- } } // return fulltaskLogisticss, total, nil return newTaskLinks, totalTask, nil } // 修改任务表的物流状态 func ChangeLogisticsStatus(ctx context.Context, taskIds []string) error { db := GetReadDB(ctx) err := db.Debug().Model(gorm_model.YoungeeTaskInfo{}).Where("task_id IN ?", taskIds).Update("logistics_status", 3).Error if err != nil { logrus.WithContext(ctx).Errorf("[ChangeTaskStatus] error query mysql total, err:%+v", err) return err } return nil }