package dao

import (
	"errors"
	"gorm.io/gorm"
	"youngee_b_api/app/entity"
)

type EnterpriseDao struct{}

func (d EnterpriseDao) GetEnterpriseInfo(enterpriseId string) (*entity.Enterprise, error) {
	var enterprise entity.Enterprise
	err := Db.Debug().Model(&entity.Enterprise{}).Where("enterprise_id = ?", enterpriseId).Find(&enterprise).Error
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, nil
		} else {
			return nil, err
		}
	}
	return &enterprise, nil
}

func (d EnterpriseDao) GetEnterprise(enterpriseId string) (*entity.Enterprise, error) {
	var enterprise entity.Enterprise
	err := Db.Model(&entity.Enterprise{}).Where("enterprise_id = ?", enterpriseId).Select("business_name, user_id").Find(&enterprise).Error
	if err != nil {
		return nil, err
	}
	return &enterprise, nil
}

func (d EnterpriseDao) GetEnterprisePhone(enterpriseId string) (string, error) {
	var phone string
	err := Db.Debug().Model(&entity.Enterprise{}).Where("enterprise_id = ?", enterpriseId).Select("phone").Find(&phone).Error
	if err != nil {
		return "", err
	}
	return phone, nil
}

// 充值-更新账户余额
func (d EnterpriseDao) UpdateEnterpriseBalance(enterpriseId string, amount float64) (*string, error) {
	var enterprise entity.Enterprise
	var err error
	err = Db.Debug().Model(&entity.Enterprise{}).Where("enterprise_id = ?", enterpriseId).Find(&enterprise).Error
	if err != nil {
		return nil, err
	}
	newBalance := enterprise.Balance + amount
	newAvailableBalance := enterprise.AvailableBalance + amount
	err = Db.Debug().Model(&entity.Enterprise{}).Where("enterprise_id = ?", enterpriseId).Updates(entity.Enterprise{
		Balance:          newBalance,
		AvailableBalance: newAvailableBalance}).Error
	if err != nil {
		return nil, err
	}
	return &enterpriseId, nil
}

// 支付-更新账户余额、冻结金额
func (d EnterpriseDao) UpdateEnterpriseBalanceAndFrozen(enterpriseId string, needPay float64) (*string, error) {
	var enterprise entity.Enterprise
	var err error
	err = Db.Debug().Model(&entity.Enterprise{}).Where("enterprise_id = ?", enterpriseId).Find(&enterprise).Error
	if err != nil {
		return nil, err
	}
	if enterprise.AvailableBalance < needPay {
		return nil, errors.New("可用余额不足")
	}
	newAvailableBalance := enterprise.AvailableBalance - needPay
	newFrozenBalance := enterprise.FrozenBalance + needPay
	err = Db.Model(&entity.Enterprise{}).Where("enterprise_id = ?", enterpriseId).Updates(entity.Enterprise{
		AvailableBalance: newAvailableBalance,
		FrozenBalance:    newFrozenBalance}).Error
	if err != nil {
		return nil, err
	}
	return &enterpriseId, nil
}

// 解冻-更新账户余额、冻结金额
func (d EnterpriseDao) UpdateEnterpriseBalanceAndFrozen2(enterpriseId string, needPay float64, realPayments float64) (*string, error) {
	var enterprise entity.Enterprise
	var err error
	err = Db.Model(&entity.Enterprise{}).Where("enterprise_id = ?", enterpriseId).Find(&enterprise).Error
	if err != nil {
		return nil, err
	}
	newBalance := enterprise.Balance - realPayments
	newAvailableBalance := enterprise.AvailableBalance + (needPay - realPayments)
	newFrozenBalance := enterprise.FrozenBalance - needPay
	err = Db.Debug().Model(&entity.Enterprise{}).Where("enterprise_id = ?", enterpriseId).Updates(entity.Enterprise{
		Balance:          newBalance,
		AvailableBalance: newAvailableBalance,
		FrozenBalance:    newFrozenBalance}).Error
	if err != nil {
		return nil, err
	}
	return &enterpriseId, nil
}

// 更新商家信息
func (d EnterpriseDao) UpdateEnterprise(enterprise entity.Enterprise) error {
	err := Db.Model(&entity.Enterprise{}).Where("enterprise_id = ?", enterprise.EnterpriseID).Updates(enterprise).Error
	if err != nil {
		return err
	}
	return nil
}
