package db

import (
	"context"
	"github.com/sirupsen/logrus"
	"time"
	"youngee_b_api/model/gorm_model"
)

// CreateCooperateInfo 创建商家-服务商合作关系
func CreateCooperateInfo(ctx context.Context, cooperateInfo *gorm_model.EnterpriseSupplierCooperate) error {
	db := GetWriteDB(ctx)
	err := db.Create(&cooperateInfo).Error
	if err != nil {
		return err
	}
	return nil
}

// GetCooperateInfoByIds 根据服务商ID和邀约状态在合作库中查找合作数据
func GetCooperateInfoByIds(ctx context.Context, supplierId int, cooperateStatus int, pageSize, pageNum int64) ([]*gorm_model.EnterpriseSupplierCooperate, int64, error) {
	db := GetReadDB(ctx)
	whereCondition := gorm_model.EnterpriseSupplierCooperate{
		SupplierId:      supplierId,
		CooperateStatus: cooperateStatus,
	}

	// 1. 按条件过滤
	db = db.Debug().Model(gorm_model.EnterpriseSupplierCooperate{}).Where(whereCondition)

	// 2. 返回当前页数据并统计总数
	var total int64
	var cooperateInfp []*gorm_model.EnterpriseSupplierCooperate
	if err := db.Count(&total).Error; err != nil {
		logrus.WithContext(ctx).Errorf("[GetCooperateInfoByIds] error query mysql total, err:%+v", err)
		return nil, 0, err
	}
	limit := pageSize
	offset := pageSize * pageNum // assert pageNum start with 0
	err := db.Order("enterprise_id desc").Limit(int(limit)).Offset(int(offset)).Find(&cooperateInfp).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetCooperateInfoByIds] error query mysql total, err:%+v", err)
		return nil, 0, err
	}
	return cooperateInfp, total, nil
}

// UpdateCooperateInfo 更新合作关系状态
func UpdateCooperateInfo(ctx context.Context, cooperateId int, cooperateStatus int) error {
	db := GetWriteDB(ctx)
	whereCondition := gorm_model.EnterpriseSupplierCooperate{CooperateId: cooperateId}

	if cooperateStatus == 2 {
		var cooperateInfo *gorm_model.EnterpriseSupplierCooperate
		cooperateInfo = &gorm_model.EnterpriseSupplierCooperate{}
		cooperateInfo.CooperateStatus = cooperateStatus
		var currentTime time.Time
		currentTime = time.Now()
		cooperateInfo.AgreeTime = &currentTime
		err := db.Model(&gorm_model.YounggeeJob{}).Where(whereCondition).Updates(cooperateInfo).Error
		if err != nil {
			return err
		}
	} else if cooperateStatus == 3 {
		var cooperateInfo *gorm_model.EnterpriseSupplierCooperate
		cooperateInfo = &gorm_model.EnterpriseSupplierCooperate{}
		cooperateInfo.CooperateStatus = cooperateStatus
		var currentTime time.Time
		currentTime = time.Now()
		cooperateInfo.AgreeTime = &currentTime
		err := db.Model(&gorm_model.YounggeeJob{}).Where(whereCondition).Updates(cooperateInfo).Error
		if err != nil {
			return err
		}
	}
	return nil
}

// FindCooperateInfoBySupplierAndEnterprise 查找服务商-商家是否建立合作关系
func FindCooperateInfoBySupplierAndEnterprise(ctx context.Context, supplierId int, enterpriseId string) (int64, error) {
	db := GetReadDB(ctx)
	whereCondition := gorm_model.EnterpriseSupplierCooperate{
		EnterpriseId: enterpriseId,
		SupplierId:   supplierId,
	}
	db = db.Debug().Model(gorm_model.EnterpriseSupplierCooperate{}).Where(whereCondition)
	var total int64
	if err := db.Count(&total).Error; err != nil {
		logrus.WithContext(ctx).Errorf("[GetCooperateInfoByIds] error query mysql total, err:%+v", err)
		return 0, err
	}
	return total, nil
}
