package db

import (
	"context"
	"youngee_b_api/model/gorm_model"
)

// CreateJob 新建岗位
func CreateJob(ctx context.Context, job gorm_model.YounggeeJob) error {
	db := GetWriteDB(ctx)
	err := db.Create(&job).Error
	if err != nil {
		return err
	}
	return nil
}

// UpdateJob 更新岗位
func UpdateJob(ctx context.Context, job gorm_model.YounggeeJob) error {
	db := GetWriteDB(ctx)
	whereCondition := gorm_model.YounggeeJob{JobId: job.JobId}
	err := db.Model(&gorm_model.YounggeeJob{}).Where(whereCondition).Updates(job).Error
	if err != nil {
		return err
	}
	return nil
}

// DeleteJob 删除岗位
func DeleteJob(ctx context.Context, job gorm_model.YounggeeJob) error {
	db := GetWriteDB(ctx)
	whereCondition := gorm_model.YounggeeJob{JobId: job.JobId}
	err := db.Where(whereCondition).Delete(&gorm_model.YounggeeJob{}).Error
	if err != nil {
		return err
	}
	return nil
}

// FindJobByEnterpriseId 按服务商ID查找岗位信息
func FindJobByEnterpriseId(ctx context.Context, job gorm_model.YounggeeJob) ([]*gorm_model.YounggeeJob, error) {
	db := GetReadDB(ctx)
	var Jobs []*gorm_model.YounggeeJob
	whereCondition := gorm_model.YounggeeJob{SupplierId: job.SupplierId}
	err := db.Model(gorm_model.YounggeeJob{}).Where(whereCondition).Find(&Jobs).Error
	if err != nil {
		return nil, err
	}
	return Jobs, nil
}

// FindJobByJobId 按照岗位Id查找岗位信息
func FindJobByJobId(ctx context.Context, jobId int) (*gorm_model.YounggeeJob, error) {
	db := GetReadDB(ctx)
	var Job *gorm_model.YounggeeJob
	whereCondition := gorm_model.YounggeeJob{JobId: jobId}
	err := db.Model(gorm_model.YounggeeJob{}).Where(whereCondition).Find(&Job).Error
	if err != nil {
		return nil, err
	}
	return Job, nil
}

/*
func GetRewardStrategyBySelectionId(ctx context.Context, SelectionId string) ([]*gorm_model.RewardStrategy, error) {
	db := GetReadDB(ctx)
	var RewardStrategys []*gorm_model.RewardStrategy
	err := db.Model(gorm_model.RewardStrategy{}).Where("selection_id = ?", SelectionId).Find(&RewardStrategys).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetRewardStrategyBySelectionId] error query, err:%+v", err)
		return nil, err
	}
	return RewardStrategys, nil
}
*/
