package storage import ( "context" "database/sql" "time" "github.com/shopspring/decimal" "dashboard-cdc-worker/internal/dashboard" ) func (r *MySQLRepository) ApplyDailyContribution(ctx context.Context, tx *sql.Tx, c dashboard.Contribution, storageZone string) error { zone, err := time.LoadLocation(storageZone) if err != nil { zone = time.UTC } day := dashboard.DailyDate(c.EventTime, zone) newUserRecharge := decimal.Zero if dashboard.SamePeriod(c.UserCreatedAt, c.EventTime, dashboard.PeriodWindow{Type: dashboard.PeriodDay}, zone) { newUserRecharge = c.NewUserRechargeCandidate } if err := r.upsertDailyAmounts(ctx, tx, c, day, newUserRecharge); err != nil { return err } if c.CountryNewUser > 0 { if err := r.applyDailyUserMetric(ctx, tx, c, day, dashboard.MetricCountryNewUser); err != nil { return err } } if c.LuckyGiftUser { if err := r.applyDailyUserMetric(ctx, tx, c, day, dashboard.MetricLuckyGiftUser); err != nil { return err } } if c.GameUser { if err := r.applyDailyUserMetric(ctx, tx, c, day, dashboard.MetricGameUser); err != nil { return err } } return nil } func (r *MySQLRepository) ApplyPeriodContribution(ctx context.Context, tx *sql.Tx, c dashboard.Contribution, statTimezone string) error { zone, err := time.LoadLocation(statTimezone) if err != nil { zone = time.UTC } for _, window := range dashboard.PeriodWindows(c.EventTime, zone) { newUserRecharge := decimal.Zero if dashboard.SamePeriod(c.UserCreatedAt, c.EventTime, window, zone) { newUserRecharge = c.NewUserRechargeCandidate } if err := r.upsertPeriodAmounts(ctx, tx, c, window, statTimezone, newUserRecharge); err != nil { return err } if c.CountryNewUser > 0 { if err := r.applyPeriodUserMetric(ctx, tx, c, window, statTimezone, dashboard.MetricCountryNewUser); err != nil { return err } } if c.LuckyGiftUser { if err := r.applyPeriodUserMetric(ctx, tx, c, window, statTimezone, dashboard.MetricLuckyGiftUser); err != nil { return err } } if c.GameUser { if err := r.applyPeriodUserMetric(ctx, tx, c, window, statTimezone, dashboard.MetricGameUser); err != nil { return err } } } return nil } func (r *MySQLRepository) ApplyGamePeriodContribution(ctx context.Context, tx *sql.Tx, c dashboard.Contribution, statTimezone string) error { if c.GameProvider == "" || c.GameID == "" { return nil } zone, err := time.LoadLocation(statTimezone) if err != nil { zone = time.UTC } for _, window := range dashboard.PeriodWindows(c.EventTime, zone) { if err := r.upsertGamePeriodAmounts(ctx, tx, c, window, statTimezone); err != nil { return err } if c.GameUser { if err := r.applyGamePeriodUserMetric(ctx, tx, c, window, statTimezone); err != nil { return err } } } return nil } func (r *MySQLRepository) upsertDailyAmounts(ctx context.Context, tx *sql.Tx, c dashboard.Contribution, day time.Time, newUserRecharge decimal.Decimal) error { sign := decimal.NewFromInt(int64(c.DeltaSign)) _, err := tx.ExecContext(ctx, ` INSERT INTO country_dashboard_daily_metric ( stat_date, date_number, sys_origin, country_code, country_name, new_user_recharge, official_recharge, mifapay_recharge, google_recharge, dealer_recharge, salary_exchange, lucky_gift_total_flow, lucky_gift_payout, game_total_flow, game_payout, refreshed_at ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW()) ON DUPLICATE KEY UPDATE country_name = VALUES(country_name), new_user_recharge = new_user_recharge + VALUES(new_user_recharge), official_recharge = official_recharge + VALUES(official_recharge), mifapay_recharge = mifapay_recharge + VALUES(mifapay_recharge), google_recharge = google_recharge + VALUES(google_recharge), dealer_recharge = dealer_recharge + VALUES(dealer_recharge), salary_exchange = salary_exchange + VALUES(salary_exchange), lucky_gift_total_flow = lucky_gift_total_flow + VALUES(lucky_gift_total_flow), lucky_gift_payout = lucky_gift_payout + VALUES(lucky_gift_payout), game_total_flow = game_total_flow + VALUES(game_total_flow), game_payout = game_payout + VALUES(game_payout), refreshed_at = NOW() `, day, dashboard.DateNumber(day), c.SysOrigin, c.Country.Code, c.Country.Name, newUserRecharge.Mul(sign), c.OfficialRecharge.Mul(sign), c.MifapayRecharge.Mul(sign), c.GoogleRecharge.Mul(sign), c.DealerRecharge.Mul(sign), c.SalaryExchange.Mul(sign), c.LuckyGiftTotalFlow.Mul(sign), c.LuckyGiftPayout.Mul(sign), c.GameTotalFlow.Mul(sign), c.GamePayout.Mul(sign)) return err } func (r *MySQLRepository) upsertPeriodAmounts(ctx context.Context, tx *sql.Tx, c dashboard.Contribution, window dashboard.PeriodWindow, statTimezone string, newUserRecharge decimal.Decimal) error { sign := decimal.NewFromInt(int64(c.DeltaSign)) _, err := tx.ExecContext(ctx, ` INSERT INTO country_dashboard_period_metric ( period_type, period_key, stat_timezone, period_name, period_start_date, period_end_date, sys_origin, country_code, country_name, new_user_recharge, official_recharge, mifapay_recharge, google_recharge, dealer_recharge, salary_exchange, lucky_gift_total_flow, lucky_gift_payout, game_total_flow, game_payout, refreshed_at ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW()) ON DUPLICATE KEY UPDATE period_name = VALUES(period_name), period_start_date = VALUES(period_start_date), period_end_date = VALUES(period_end_date), country_name = VALUES(country_name), new_user_recharge = new_user_recharge + VALUES(new_user_recharge), official_recharge = official_recharge + VALUES(official_recharge), mifapay_recharge = mifapay_recharge + VALUES(mifapay_recharge), google_recharge = google_recharge + VALUES(google_recharge), dealer_recharge = dealer_recharge + VALUES(dealer_recharge), salary_exchange = salary_exchange + VALUES(salary_exchange), lucky_gift_total_flow = lucky_gift_total_flow + VALUES(lucky_gift_total_flow), lucky_gift_payout = lucky_gift_payout + VALUES(lucky_gift_payout), game_total_flow = game_total_flow + VALUES(game_total_flow), game_payout = game_payout + VALUES(game_payout), refreshed_at = NOW() `, window.Type, window.Key, statTimezone, window.Name, window.StartDate, window.EndDate, c.SysOrigin, c.Country.Code, c.Country.Name, newUserRecharge.Mul(sign), c.OfficialRecharge.Mul(sign), c.MifapayRecharge.Mul(sign), c.GoogleRecharge.Mul(sign), c.DealerRecharge.Mul(sign), c.SalaryExchange.Mul(sign), c.LuckyGiftTotalFlow.Mul(sign), c.LuckyGiftPayout.Mul(sign), c.GameTotalFlow.Mul(sign), c.GamePayout.Mul(sign)) return err } func (r *MySQLRepository) upsertGamePeriodAmounts(ctx context.Context, tx *sql.Tx, c dashboard.Contribution, window dashboard.PeriodWindow, statTimezone string) error { sign := decimal.NewFromInt(int64(c.DeltaSign)) _, err := tx.ExecContext(ctx, ` INSERT INTO country_dashboard_game_period_metric ( period_type, period_key, stat_timezone, period_name, period_start_date, period_end_date, sys_origin, country_code, country_name, game_provider, game_id, game_name, consume_amount, payout_amount, profit_amount, order_count, refreshed_at ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW()) ON DUPLICATE KEY UPDATE period_name = VALUES(period_name), period_start_date = VALUES(period_start_date), period_end_date = VALUES(period_end_date), country_name = VALUES(country_name), game_name = VALUES(game_name), consume_amount = consume_amount + VALUES(consume_amount), payout_amount = payout_amount + VALUES(payout_amount), profit_amount = profit_amount + VALUES(profit_amount), order_count = order_count + VALUES(order_count), refreshed_at = NOW() `, window.Type, window.Key, statTimezone, window.Name, window.StartDate, window.EndDate, c.SysOrigin, c.Country.Code, c.Country.Name, c.GameProvider, c.GameID, c.GameName, c.GameConsume.Mul(sign), c.GamePayoutBy.Mul(sign), c.GameProfit.Mul(sign), c.GameOrder*int64(c.DeltaSign)) return err }