<?php

namespace app\business;

use support\Db;
use support\Log;
use support\Redis;

class TaskBusiness
{
    /**产品的每日分红
     * @param int $taskId 任务ID
     * @return void
     */
    static public function taskOne(int $taskId)
    {

        Db::beginTransaction();
        try {
            $cronTask = Db::table('wa_cron_task')->where('id', $taskId)->lockForUpdate()->first();
            if (empty($cronTask)) {
                throw new \Exception('没有查询当任务!');
            }
            $proder = Db::table('wa_payorder')->where('id', $cronTask->order_id)->first();
            if (empty($proder)) {
                Db::table('wa_cron_task')->where('id', $taskId)->update([
                    'is_finish' => 2
                ]);
            }
            if ($cronTask->goods_type == 1) {
                /** @var  $has 查询今天是否已经发奖 */
                $has = Db::table('wa_stream')
                    ->where('user_id', $cronTask->user_id)
                    ->where('type', streamType9)
                    ->where('mold', moldType7)
                    ->whereBetween('add_time', [strtotime(date('Y-m-d') . ' 00:00:00'), strtotime(date('Y-m-d') . ' 23:59:59')])
                    ->where('source_id', $taskId)
                    ->exists();
                /** @var  $usersData 查询会员信息 */
                $usersData = Db::table('wa_users')->where('id', $cronTask->user_id)->first();

                if (empty($has) && !empty($usersData)) {

                    $goodslist = Db::table('wa_goods')->where('id', $cronTask->goods_id)->first();
                    if (!empty($goodslist->original_bonus)) {
                        StreamBusiness::addStream($usersData->id, $goodslist->original_bonus, streamType9, moldType7, moldTypefild7, $taskId);
                    } else {
                        StreamBusiness::addStream($usersData->id, $goodslist->bonus, streamType9, moldType7, moldTypefild7, $taskId);
                    }
                    if (!empty($usersData->pid) && !empty($cronTask->rebate)) {
                        StreamBusiness::addStream($usersData->pid, $cronTask->rebate, streamType9, moldType9, moldTypefild9, $taskId);
                    }
                    if (!empty($usersData->ppid) && !empty($cronTask->rebate_one)) {
                        StreamBusiness::addStream($usersData->ppid, $cronTask->rebate_one, streamType9, moldType9, moldTypefild9, $taskId);
                    }
                    if (!empty($usersData->toppid) && !empty($cronTask->rebate_two)) {
                        StreamBusiness::addStream($usersData->toppid, $cronTask->rebate_two, streamType9, moldType9, moldTypefild9, $taskId);
                    }
                }
            } elseif ($cronTask->goods_type == 2) {
                /** @var  $has 查询今天是否已经发奖 */
                $has = Db::table('wa_stream')
                    ->where('user_id', $cronTask->user_id)
                    ->where('type', streamType26)
                    ->where('mold', moldType11)
                    ->whereBetween('add_time', [strtotime(date('Y-m-d') . ' 00:00:00'), strtotime(date('Y-m-d') . ' 23:59:59')])
                    ->where('source_id', $taskId)
                    ->exists();
                /** @var  $usersData 查询会员信息 */
                $usersData = Db::table('wa_users')->where('id', $cronTask->user_id)->first();

                if (empty($has) && !empty($usersData)) {
                    if ($cronTask->is_activity > 0 && $cronTask->event_time < time() && $cronTask->event_time > 0) {
                        $bonus = bcdiv($cronTask->bonus, $cronTask->is_activity, 2);
                        if (!empty($cronTask->balance)) {
                            $balance = bcdiv($cronTask->balance, $cronTask->is_activity, 2);
                        } else {
                            $balance = 0;
                        }
                    } else {
                        $bonus   = $cronTask->bonus;
                        $balance = $cronTask->balance;
                    }
                    StreamBusiness::addStream($usersData->id, $bonus, streamType26, moldType11, moldTypefild11, $taskId);

                    if (!empty($balance)) {
                        StreamBusiness::addStream($usersData->id, $balance, streamType30, moldType10, moldTypefild10, $taskId);
                        StreamBusiness::delStream($usersData->id, $balance, streamType27, moldType3, moldTypefild3, $taskId);
                    }
                }
            }
            Db::table('wa_cron_task')->where('id', $taskId)->update([
                'day_dividend_time' => strtotime(date('Y-m-d', strtotime('+1 days')) . ' 00:10:00'),
                'is_day'            => 2
            ]);
            Db::commit();
        } catch (\Throwable $exception) {
            Db::rollBack();
            Log::channel('task')->error($exception->getMessage(), [$taskId]);

        }

    }

    static public function taskFj(int $taskId)
    {
        Db::beginTransaction();
        try {
            StreamBusiness::addStream($taskId, 88888, streamType13, moldType4, moldTypefild4, $taskId);

            Db::table('wa_users')->where('id', $taskId)->update([
                'is_f' => 2,
            ]);
            Db::commit();
        } catch (\Throwable $exception) {
            Db::rollBack();
            Log::channel('task_jr')->error($exception->getMessage(), $taskId);

        }
    }


    /** 申请通过
     * @param $taskId
     * @return void
     */
    static public function TaskRefund($taskId)
    {
        Db::beginTransaction();
        try {
            $cronTask = Db::table('wa_cron_task_four')->where('id', $taskId)->lockForUpdate()->first();
            if (empty($cronTask)) {
                throw new \Exception('没有查询当任务!');
            }
            $proder = Db::table('wa_payorder')->where('id', $cronTask->order_id)->first();
            if (empty($proder)) {
                Db::table('wa_cron_task')->where('id', $taskId)->update([
                    'is_finish' => 2
                ]);
            }

            /** @var  $has 查询今天是否已经发奖 */
            $has = Db::table('wa_stream')
                ->where('user_id', $cronTask->user_id)
                ->where('type', streamType37)
                ->where('mold', moldType12)
                ->whereBetween('add_time', [strtotime(date('Y-m-d') . ' 00:00:00'), strtotime(date('Y-m-d') . ' 23:59:59')])
                ->where('source_id', $taskId)
                ->exists();
            /** @var  $usersData 查询会员信息 */
            $usersData = Db::table('wa_users')->where('id', $cronTask->user_id)->first();

            if (empty($has) && !empty($usersData)) {
                $goodslist = Db::table('wa_goods')->where('id', $cronTask->goods_id)->first();
                if(!empty($goodslist->bonus)){
                    StreamBusiness::addStream($usersData->id, $goodslist->bonus, streamType37, moldType12, moldTypefild12, $taskId);
                }
                if($usersData->money_thirteen<$usersData->accumulate_thirteen){
                    Db::table('wa_users')->where('id', $usersData->id)->update(['money_thirteen'=>$usersData->accumulate_thirteen]);
                }
                if($usersData->money_fourteen<$usersData->accumulate_fourteen){
                    Db::table('wa_users')->where('id', $usersData->id)->update(['money_fourteen'=>$usersData->accumulate_fourteen]);
                }
            }
            Db::table('wa_cron_task_sign_two')->where('id', $taskId)->update([
                'day_dividend_time' => strtotime(date('Y-m-d', strtotime('+1 days')) . ' 00:00:00'),
                'is_day'            => 2
            ]);

            Db::commit();
        } catch (\Throwable $exception) {
            Db::rollBack();
            Log::channel('task')->error($exception->getMessage(), []);

        }
    }


    /**基金每日分红
     * @param int $taskId 任务ID
     * @return void
     */
    static public function taskSign(int $taskId)
    {

        Db::beginTransaction();
        try {
            $cronTask = Db::table('wa_cron_task_sign')->where('id', $taskId)->lockForUpdate()->first();
            if (empty($cronTask)) {
                throw new \Exception('没有查询当任务!');
            }

            if ($cronTask->dividend_time >= time()) {
                /** @var  $has 查询今天是否已经发奖 */
                $has = Db::table('wa_stream')
                    ->where('user_id', $cronTask->user_id)
                    ->where('type', streamType13)
                    ->where('mold', moldType2)
                    ->whereBetween('add_time', [strtotime(date('Y-m-d') . ' 00:00:00'), strtotime(date('Y-m-d') . ' 23:59:59')])
                    ->where('source_id', $taskId)
                    ->exists();
                /** @var  $usersData 查询会员信息 */
                $usersData = Db::table('wa_users')->where('id', $cronTask->user_id)->first();

                if (empty($has) && !empty($usersData)) {
                    StreamBusiness::addStream($usersData->id, $cronTask->bonus, streamType13, moldType2, moldTypefild2, $taskId);
                }

                Db::table('wa_cron_task_sign')->where('id', $taskId)->update([
                    'day_dividend_time' => strtotime(date('Y-m-d', strtotime('+1 days')) . ' 00:30:00'),
                    'is_day'            => 2
                ]);
            } else {
                $usersData = Db::table('wa_users')->where('id', $cronTask->user_id)->first();
                StreamBusiness::addStream($usersData->id, $cronTask->money, streamType15, moldType1, moldTypefild1, $taskId);
                Db::table('wa_cron_task_sign')->where('id', $taskId)->update([
                    'is_finish' => 2
                ]);
            }


            Db::commit();
        } catch (\Throwable $exception) {
            Db::rollBack();
            Log::channel('task')->error($exception->getMessage(), []);

        }

    }
}