package activity

import (
	"bindbox-game/internal/repository/mysql/dao"
	"context"
)

// DeleteActivity 删除活动
// 参数: id 活动ID
// 返回: 错误信息
func (s *service) DeleteActivity(ctx context.Context, id int64) error {
	return s.writeDB.Transaction(func(tx *dao.Query) error {
		issues, err := tx.ActivityIssues.WithContext(ctx).Where(tx.ActivityIssues.ActivityID.Eq(id)).Find()
		if err != nil {
			return err
		}
		var issueIDs []int64
		for _, is := range issues {
			issueIDs = append(issueIDs, is.ID)
		}

		var drawLogIDs []int64
		if len(issueIDs) > 0 {
			logs, err := tx.ActivityDrawLogs.WithContext(ctx).Where(tx.ActivityDrawLogs.IssueID.In(issueIDs...)).Find()
			if err != nil {
				return err
			}
			for _, lg := range logs {
				drawLogIDs = append(drawLogIDs, lg.ID)
			}
		}

		if len(issueIDs) > 0 {
			if _, err = tx.ActivityRewardSettings.WithContext(ctx).Where(tx.ActivityRewardSettings.IssueID.In(issueIDs...)).Delete(); err != nil {
				return err
			}
			if _, err = tx.ActivityDrawEffects.WithContext(ctx).Where(tx.ActivityDrawEffects.IssueID.In(issueIDs...)).Delete(); err != nil {
				return err
			}
		}

		if len(drawLogIDs) > 0 {
			if _, err = tx.ActivityDrawEffects.WithContext(ctx).Where(tx.ActivityDrawEffects.DrawLogID.In(drawLogIDs...)).Delete(); err != nil {
				return err
			}
			if _, err = tx.ActivityDrawReceipts.WithContext(ctx).Where(tx.ActivityDrawReceipts.DrawLogID.In(drawLogIDs...)).Delete(); err != nil {
				return err
			}
			if _, err = tx.UserItemCards.WithContext(ctx).Where(tx.UserItemCards.UsedDrawLogID.In(drawLogIDs...)).Delete(); err != nil {
				return err
			}
			if _, err = tx.ActivityDrawLogs.WithContext(ctx).Where(tx.ActivityDrawLogs.ID.In(drawLogIDs...)).Delete(); err != nil {
				return err
			}
		}

		if _, err = tx.ActivityDrawEffects.WithContext(ctx).Where(tx.ActivityDrawEffects.ActivityID.Eq(id)).Delete(); err != nil {
			return err
		}
		if _, err = tx.UserItemCards.WithContext(ctx).Where(tx.UserItemCards.UsedActivityID.Eq(id)).Delete(); err != nil {
			return err
		}
		if len(issueIDs) > 0 {
			if _, err = tx.UserItemCards.WithContext(ctx).Where(tx.UserItemCards.UsedIssueID.In(issueIDs...)).Delete(); err != nil {
				return err
			}
		}

		if _, err = tx.UserInventory.WithContext(ctx).Where(tx.UserInventory.ActivityID.Eq(id)).Delete(); err != nil {
			return err
		}
		invs, err := tx.UserInventory.WithContext(ctx).Where(tx.UserInventory.ActivityID.Eq(id)).Find()
		if err != nil {
			return err
		}
		var invIDs []int64
		for _, iv := range invs {
			invIDs = append(invIDs, iv.ID)
		}
		if len(invIDs) > 0 {
			if _, err = tx.ShippingRecords.WithContext(ctx).Where(tx.ShippingRecords.InventoryID.In(invIDs...)).Delete(); err != nil {
				return err
			}
			if _, err = tx.UserInventoryTransfers.WithContext(ctx).Where(tx.UserInventoryTransfers.InventoryID.In(invIDs...)).Delete(); err != nil {
				return err
			}
		}

		if _, err = tx.SystemItemCards.WithContext(ctx).Where(tx.SystemItemCards.ActivityID.Eq(id)).Delete(); err != nil {
			return err
		}
		if len(issueIDs) > 0 {
			if _, err = tx.SystemItemCards.WithContext(ctx).Where(tx.SystemItemCards.IssueID.In(issueIDs...)).Delete(); err != nil {
				return err
			}
		}
		if _, err = tx.SystemCoupons.WithContext(ctx).Where(tx.SystemCoupons.ActivityID.Eq(id)).Delete(); err != nil {
			return err
		}

		if len(issueIDs) > 0 {
			if _, err = tx.ActivityIssues.WithContext(ctx).Where(tx.ActivityIssues.ActivityID.Eq(id)).Delete(); err != nil {
				return err
			}
		}

		if _, err = tx.Activities.WithContext(ctx).Where(tx.Activities.ID.Eq(id)).Delete(); err != nil {
			return err
		}
		return nil
	})
}
