package dao

import (
	"youngee_b_api/app/entity"
)

type SLocalLifeDao struct{}

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

// 根据本地生活id、邀约状态返回数据列表
func (d SLocalLifeDao) GetSLocalLifeByStatus(localId string, status int64, page int, pageSize int) ([]*entity.SLocalLifeInfo, int64, error) {
	var sLocalLifeInfos []*entity.SLocalLifeInfo
	var total int64
	offset := (page - 1) * pageSize
	query := Db.Debug().Model(&entity.SLocalLifeInfo{}).Where("local_id = ? AND s_local_status = ?", localId, 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(&sLocalLifeInfos).Error
	if err != nil {
		return nil, 0, err
	}

	return sLocalLifeInfos, total, nil
}

// 获取某个 local_id 任务涉及到的服务商
func (d SLocalLifeDao) GetSLocalLifeByLocalId(localId string) ([]*entity.SLocalLifeInfo, error) {
	var sLocalLifeInfos []*entity.SLocalLifeInfo
	err := Db.Debug().Model(&entity.SLocalLifeInfo{}).Where("local_id = ?", localId).Select("s_local_id, local_id, supplier_id, service_charge_settle, supplier_type").Find(&sLocalLifeInfos).Error
	return sLocalLifeInfos, err
}

// 更新任务
func (d SLocalLifeDao) UpdateSLocalLife(sLocalLifeInfo entity.SLocalLifeInfo) error {
	err := Db.Model(&entity.SLocalLifeInfo{}).Where("s_local_id = ?", sLocalLifeInfo.SLocalID).Updates(sLocalLifeInfo).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
//}
