package dao

import (
	"time"
	"youngee_b_api/app/entity"
	"youngee_b_api/app/vo"
)

type RechargeRecordDao struct{}

func (d RechargeRecordDao) Insert(rechargeRecord *entity.RechargeRecord) error {
	err := Db.Debug().Model(entity.RechargeRecord{}).Omit("confirm_at", "refuse_at").Create(rechargeRecord).Error
	if err != nil {
		return err
	}
	return nil
}

// 获取指定企业id的累计充值金额、充值确认中金额
func (d RechargeRecordDao) GetRechargeAmount(enterpriseId string, status int64) (float64, error) {
	var totalAmount float64
	query := Db.Debug().Model(&entity.RechargeRecord{})
	err := query.Where("enterprise_id = ? AND status = ?", enterpriseId, status).Select("COALESCE(SUM(recharge_amount), 0)").Scan(&totalAmount).Error
	if err != nil {
		return 0, err
	}
	return totalAmount, nil
}

// 获取指定企业id的充值记录
func (d RechargeRecordDao) RechargeInfoList(param *vo.RechargeParam) ([]entity.RechargeRecord, int64, error) {
	rechargeRecords := []entity.RechargeRecord{}
	var total int64
	query := Db.Debug().Model(&entity.RechargeRecord{}).Where("enterprise_id = ? AND status = ?", param.EnterpriseId, param.RechargeState)
	query.Count(&total)
	query = query.Select("recharge_id, recharge_amount, transfer_voucher_url, recharge_method, commit_at, confirm_at, refuse_at, fail_reason, enterprise_id, sub_account_id")
	offset := (param.Page - 1) * param.PageSize
	var err error
	if param.RechargeState == 1 {
		err = query.Order("commit_at desc").Offset(offset).Limit(param.PageSize).Find(&rechargeRecords).Error
	} else if param.RechargeState == 2 {
		err = query.Order("confirm_at desc").Offset(offset).Limit(param.PageSize).Find(&rechargeRecords).Error
	} else if param.RechargeState == 3 {
		err = query.Order("refuse_at desc").Offset(offset).Limit(param.PageSize).Find(&rechargeRecords).Error
	}
	if err != nil {
		return nil, 0, err
	}

	return rechargeRecords, total, nil
}

// 获取指定企业id的指定充值状态记录数
func (d RechargeRecordDao) RechargeStatusCount(enterpriseId string, rechargeState int64) (int64, error) {
	var total int64
	query := Db.Debug().Model(&entity.RechargeRecord{}).Where("enterprise_id = ? AND status = ?", enterpriseId, rechargeState)
	err := query.Count(&total).Error
	if err != nil {
		return 0, err
	}
	return total, nil
}

// 更新充值状态
func (d RechargeRecordDao) UpdateRechargeStatus(rechargeId string, status int64, t time.Time) error {
	rechargeRecord := entity.RechargeRecord{
		Status: status,
	}
	if status == 2 {
		rechargeRecord.InvoiceStatus = 1
		rechargeRecord.ConfirmAt = t
	}
	if status == 3 {
		rechargeRecord.FailReason = "微信支付失败"
		rechargeRecord.RefuseAt = t
	}
	err = Db.Debug().Model(&entity.RechargeRecord{}).Where("recharge_id = ?", rechargeId).Updates(rechargeRecord).Error
	if err != nil {
		return err
	}
	return nil
}

// 查找微信充值中超时且状态为充值未确认的记录
func (d RechargeRecordDao) GetWXRechargeByStatusList(status int64) ([]*entity.RechargeRecord, error) {
	now := time.Now()
	var rechargeRecords []*entity.RechargeRecord
	err := Db.Debug().Model(&entity.RechargeRecord{}).Where("recharge_method = ? AND status = ? AND refuse_at < ?", 2, status, now).Find(&rechargeRecords).Error
	if err != nil {
		return nil, err
	}
	return rechargeRecords, nil
}

// 批量更新指定id的充值记录状态
func (d RechargeRecordDao) UpdateRechargeFailedList(noPayIds []int64) error {
	err := Db.Debug().Model(&entity.RechargeRecord{}).Where("id IN ?", noPayIds).Updates(entity.RechargeRecord{Status: 3, FailReason: "微信支付失败"}).Error
	if err != nil {
		return err
	}
	return nil
}

//// 更新所有充值记录中微信支付超时的记录
//func (d RechargeRecordDao) UpdateRechargeFailedList() error {
//	now := time.Now()
//	err := Db.Debug().Model(&entity.RechargeRecord{}).Where("recharge_method = ? AND status = ? AND refuse_at < ?", 2, 1, now).
//		Updates(map[string]interface{}{"status": 3, "fail_reason": "微信支付失败"}).Error
//	if err != nil {
//		return err
//	}
//	return nil
//}
