| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- <?php
- use App\Support\PromotionRules;
- use Illuminate\Database\Migrations\Migration;
- use Illuminate\Support\Facades\DB;
- return new class extends Migration {
- /**
- * Loss invite now repeats. Members who already claimed move to the next round, keeping their application on the reward;
- * coupon rewards record only the inviter's own coupons as the amount (budget_used already includes invitee coupons).
- */
- public function up(): void
- {
- $activities = DB::table('promotion_activities')->where('type', PromotionRules::LOSS)->pluck('id');
- if ($activities->isEmpty()) return;
- DB::transaction(function () use ($activities) {
- $now = date('Y-m-d H:i:s');
- $rewards = DB::table('promotion_rewards')->whereIn('activity_id', $activities)->lockForUpdate()->get();
- foreach ($rewards as $reward) {
- $rules = json_decode((string)$reward->rule_snapshot, true) ?: [];
- $update = [];
- $account = DB::table('promotion_accounts')->where('id', $reward->account_id)->lockForUpdate()->first();
- if ($account && (int)$account->round_no === (int)$reward->round_no) {
- $application = json_decode((string)$account->application_snapshot, true) ?: [];
- $rules['application'] ??= ['applied_at' => $account->applied_at] + array_diff_key($application, ['rules' => true]);
- DB::table('promotion_accounts')->where('id', $account->id)->update(['round_no' => (int)$account->round_no + 1,
- 'applied_at' => null, 'application_snapshot' => null, 'updated_at' => $now]);
- }
- if (($rules['reward_mode'] ?? 'cash') === 'coupon' && !isset($rules['coupon_face_value'])) {
- $rules['coupon_face_value'] = bcadd((string)$reward->amount, '0', 2);
- $update['amount'] = PromotionRules::faceValue($rules['inviter_coupons'] ?? []);
- }
- $update['rule_snapshot'] = json_encode($rules);
- DB::table('promotion_rewards')->where('id', $reward->id)->update($update + ['updated_at' => $now]);
- }
- });
- }
- public function down(): void
- {
- }
- };
|