<?php

namespace App\Services\User;

use App\Exceptions\AlertException;
use App\Models\FlowerLogModel;
use App\Models\User\UserModel;
use App\Services\Service;
use Exception;
use Illuminate\Support\Facades\DB;

/**
 * Class TaskService
 * @package App\Services\User
 * @deprecated
 */
class TaskService extends Service
{
    /**
     * @param  int  $uid
     * @return int|string
     * @throws AlertException
     * @deprecated vip领取每日小花任务
     */
    public function getVipFlowerEveryDay(int $uid)
    {
        $user = UserModel::findOrFail($uid);

        if ($user->be_vip_at == 0) {
            throw new AlertException("你还不是会员", 102);
        }

        if (FlowerLogModel::where([['uid', $uid], ['type', 15], ['create_at', '>', mktime(0, 0, 0)]])->exists()) {
            throw new AlertException("今天已经领取过了哦，明天再来吧。点此继续赚小fa", 101);
        }

        try {
            DB::beginTransaction();
            $flower = sprintf("%.2f", 0.5 + mt_rand() / mt_getrandmax() * (1.5 - 0.5));
            UserModel::where('uid', $uid)->increment('red_flower', $flower);
            $data = array(
                'uid' => $uid,
                'create_at' => time(),
                'type' => 15,
                'gold_flower' => 0,
                'red_flower' => $flower,
                'jsk' => 0,
                'remark' => "会员每日小花礼包",
            );
            DB::table('kdgx_partner_charge_pay_logs')->insert($data);
            DB::commit();

            return $flower;
        } catch (Exception $exception) {
            DB::rollBack();

            return 0;
        }
    }

    /**
     * 领取新年任务
     * @param  int  $uid
     * @return int|string
     * @throws AlertException
     * @deprecated
     */
    public function getYearFlower(int $uid)
    {
        if (FlowerLogModel::where([['uid', $uid], ['type', 14], ['create_at', '>', mktime(0, 0, 0)]])->exists()) {
            throw new AlertException("今天已经领取过了哦，明天再来吧。点此继续赚小fa", 101);
        }

        try {
            DB::beginTransaction();
            $flower = sprintf("%.2f", 0.5 + mt_rand() / mt_getrandmax() * (1.5 - 0.5));
            UserModel::where('uid', $uid)->increment('red_flower', $flower);
            $data = array(
                'uid' => $uid,
                'create_at' => time(),
                'type' => 14,
                'gold_flower' => 0,
                'red_flower' => $flower,
                'jsk' => 0,
                'remark' => "新年小花礼包",
            );
            DB::table('kdgx_partner_charge_pay_logs')->insert($data);
            DB::commit();

            return $flower;
        } catch (\Exception $exception) {
            DB::rollBack();

            return 0;
        }
    }


    /**
     * 完成某人的任务
     * @param  int  $uid
     * @param $task_type
     * @deprecated
     */
    public function task(int $uid, $task_type)
    {
        $Dtask = array(
            1 => [
                'red_flower' => 3,
                'jsk' => 0,
                'remark' => '完成任务[创建交友卡片]',
            ],
            2 => [
                'red_flower' => 2,
                'jsk' => 0,
                'remark' => '完成任务[首次卖室友]',
            ],
            3 => [
                'red_flower' => 3,
                'jsk' => 0,
                'remark' => '完成任务[首次上架自己]',
            ],
            4 => [
                'red_flower' => 3,
                'jsk' => 0,
                'remark' => '完成任务[首次上传语音]',
            ],
            5 => [
                'red_flower' => 1,
                'jsk' => 0,
                'remark' => '完成任务[首次表白]',
            ],
            6 => [
                'red_flower' => 5,
                'jsk' => 0,
                'remark' => '完成任务[关注官方微信]',
            ],
            7 => [
                'red_flower' => 5,
                'jsk' => 0,
                'remark' => '完成任务[添加到我到小程序]',
            ],
        );

        // 判断是否已完成
        $user = UserModel::find($uid);
        switch ($task_type) {
            case 1: # 设置个人信息
                if (substr($user->task, -1, 1) == 1) {
                    return true;
                } else {
                    $user->task = substr($user->task, 0, -1) . '1';
                }
                break;
            case 2: # 首次卖室友
                if (substr($user->task, -2, 1) == 1) {
                    return true;
                } else {
                    $user->task = substr($user->task, 0, -2) . '1' . substr($user->task, -1);
                }
                break;
            case 3: # 首次上架自己
                if (substr($user->task, -3, 1) == 1) {
                    return true;
                } else {
                    $user->task = substr($user->task, 0, -3) . '1' . substr($user->task, -2);
                }
                break;
            case 4: # 首次上传语音
                if (substr($user->task, -4, 1) == 1) {
                    return true;
                } else {
                    $user->task = substr($user->task, 0, -4) . '1' . substr($user->task, -3);
                }
                break;
            case 5: # 首次表白
                if (substr($user->task, -5, 1) == 1) {
                    return true;
                } else {
                    $user->task = substr($user->task, 0, -5) . '1' . substr($user->task, -4);
                }
                break;
            case 6: # 关注公众号
                if (substr($user->task, -6, 1) == 1) {
                    return true;
                } else {
                    $user->task = substr($user->task, 0, -6) . '1' . substr($user->task, -5);
                }
                break;
            case 7: # 添加到我到小程序
                if (substr($user->task, -7, 1) == 1) {
                    return true;
                } else {
                    $user->task = substr($user->task, 0, -7) . '1' . substr($user->task, -6);
                }
                break;
            default:
                return true;
        }
        // 记录任务
        $user->save();
        // 记录小fa明细
        DB::table('kdgx_partner_charge_pay_logs')->insert([
            'uid' => $uid,
            'create_at' => time(),
            'type' => 2,
            'red_flower' => $Dtask[$task_type]['red_flower'],
            'jsk' => $Dtask[$task_type]['jsk'],
            'remark' => $Dtask[$task_type]['remark'],
        ]);
        // 添加小fa数量
        $user->increment('red_flower', $Dtask[$task_type]['red_flower']);

        return true;
    }
}
