package dao

import (
	"youngee_b_api/app/entity"
)

type SProjectDao struct{}

// 插入数据
func (d SProjectDao) Insert(record *entity.SProjectInfo) error {
	result := Db.Debug().Model(&entity.SProjectInfo{}).Create(&record)
	return result.Error
}

// 根据种草id、邀约状态返回数据列表
func (d SProjectDao) GetSProjectByStatus(projectId string, status int64, page int, pageSize int) ([]*entity.SProjectInfo, int64, error) {
	var sProjectInfos []*entity.SProjectInfo
	var total int64
	offset := (page - 1) * pageSize
	query := Db.Debug().Model(&entity.SProjectInfo{}).Where("project_id = ? AND s_project_status = ?", projectId, status)
	query.Count(&total)
	query = query.Select("supplier_id,  b_operator, b_operator_type")
	err := query.Order("create_time desc").Offset(offset).Limit(pageSize).Find(&sProjectInfos).Error
	if err != nil {
		return nil, 0, err
	}

	return sProjectInfos, total, nil
}

// 获取某个 project_id 任务涉及到的服务商
func (d SProjectDao) GetSProjectByProjectId(projectId string) ([]*entity.SProjectInfo, error) {
	var sProjectInfos []*entity.SProjectInfo
	err := Db.Debug().Model(&entity.SProjectInfo{}).Where("project_id = ?", projectId).Select("s_project_id, project_id, supplier_id, service_charge_settle, supplier_type").Find(&sProjectInfos).Error
	return sProjectInfos, err
}

// 更新任务
func (d SProjectDao) UpdateSProject(sProject entity.SProjectInfo) error {
	err := Db.Model(&entity.SProjectInfo{}).Where("s_project_id = ?", sProject.SProjectID).Updates(sProject).Error
	if err != nil {
		return err
	}
	return nil
}

//// 检查给定的服务商是否在该商家库中
//func (d SupplierDao) EnterpriseDatabaseCheck(enterpriseId string, supplierId int64) (bool, error) {
//	var count int64
//	err := Db.Debug().Model(&entity.EnterpriseSupplierCooperate{}).Where("enterprise_id = ? AND supplier_id = ? AND cooperate_status != 2", enterpriseId, supplierId).Count(&count).Error
//	if err != nil {
//		return false, err
//	}
//	return count > 0, nil
//}
//
//// 获取指定商家的服务商库
//func (d SupplierDao) GetSupplierByEnterprise(enterpriseId string, page int, pageSize int) ([]*entity.EnterpriseSupplierCooperate, int64, error) {
//	var enterpriseSupplierCooperates []*entity.EnterpriseSupplierCooperate
//	var total int64
//	offset := (page - 1) * pageSize
//	query := Db.Debug().Model(&entity.EnterpriseSupplierCooperate{}).Where("enterprise_id = ? AND cooperate_status = 2", enterpriseId)
//	query.Count(&total)
//	query = query.Select("supplier_id, cooperate_num, upload_talent_num, cooperate_talent_num, b_operator, b_operator_type, agree_time")
//	err := query.Order("agree_time desc").Offset(offset).Limit(pageSize).Find(&enterpriseSupplierCooperates).Error
//	if err != nil {
//		return nil, 0, err
//	}
//
//	return enterpriseSupplierCooperates, total, nil
//}
//
//// 获取邀请待确认的服务商
//func (d SupplierDao) GetSupplierConfirmingList(enterpriseId string, page int, pageSize int) ([]*entity.EnterpriseSupplierCooperate, int64, error) {
//	var enterpriseSupplierCooperates []*entity.EnterpriseSupplierCooperate
//	var total int64
//	offset := (page - 1) * pageSize
//	query := Db.Debug().Model(&entity.EnterpriseSupplierCooperate{}).Where("enterprise_id = ? AND cooperate_status = 1", enterpriseId)
//	query.Count(&total)
//	query = query.Select("supplier_id,  b_operator, b_operator_type, cooperate_status, create_time")
//	err := query.Order("create_time desc").Offset(offset).Limit(pageSize).Find(&enterpriseSupplierCooperates).Error
//	if err != nil {
//		return nil, 0, err
//	}
//
//	return enterpriseSupplierCooperates, total, nil
//}
