<?php

namespace App\Http\Controllers\Welfare;

use App\Http\Controllers\Miniprogram\Auth;
use App\Models\User\UserModel;
use App\Models\Welfare\JigsawFlowModel;
use App\Models\Welfare\SigninModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class SigninController extends Controller
{


    public function getSignin(Request $request)
    {
        $uid = Auth::auth();
        $signin = SigninModel::where('uid', $uid)->orderBy('created_at', 'desc')->first();
        return response()->json([
            'code' => 200,
            'message' => 'OK',
            'data' => $signin
        ]);
    }


    public function signin(Request $request)
    {
        $uid = Auth::auth();
        $signin = SigninModel::where('uid', $uid)->orderBy('created_at', 'desc')->first();

        \DB::beginTransaction();
        // 首次签到
        if (!$signin) {
            $signin = SigninModel::create([
                'uid' => $uid,
                'continue_time' => 1
            ]);
            $this->add($signin);
            \DB::commit();
        }

        //上次签到日期
        switch ($signin->created_at->toDateString()) {
            case date('Y-m-d'):
                return response()->json([
                    'code' => 200,
                    'message' => 'OK',
                    'data' => $signin
                ]);
                break;
            case date('Y-m-d', strtotime("-1day")):
                if ($signin->continue_time == 7) {
                    $signin = SigninModel::create([
                        'uid' => $uid,
                        'continue_time' => 1
                    ]);
                } else {
                    $signin = SigninModel::create([
                        'uid' => $uid,
                        'continue_time' => $signin->continue_time + 1
                    ]);
                }
                break;
            default:
                $signin = SigninModel::create([
                    'uid' => $uid,
                    'continue_time' => 1
                ]);
                break;
        }
        $this->add($signin);
        \DB::commit();
        return response()->json([
            'code' => 200,
            'message' => 'OK',
            'data' => $signin
        ]);
    }

    private function add($signin)
    {
        switch ($signin->continue_time) {
            case "1":
                $amount = 5;
                break;
            case "2":
                $amount = 6;
                break;
            case "3":
                $amount = 7;
                break;
            case "4":
                $amount = 8;
                break;
            case "5":
                $amount = 9;
                break;
            case "6":
                $amount = 10;
                break;
            case "7":
                $amount = 15;
                break;
        }

        JigsawFlowModel::create([
            'uid' => $signin->uid,
            'source' => '签到获取拼图',
            'amount' => $amount,
            'remains_amount' => $amount,
        ]);
        UserModel::where('uid', $signin->uid)->increment('jigsaw_amount', $amount);
    }
}
