<?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
                ]);
            }

            /** @var  $has 查询今天是否已经发奖 */
            $has = Db::table('wa_stream')
                ->where('user_id', $cronTask->user_id)
                ->where('type', streamType14)
                ->where('mold', moldType1)
                ->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, streamType14, moldType1, moldTypefild1, $taskId);
            }

            Db::table('wa_cron_task')->where('id', $taskId)->update([
                'day_dividend_time' => strtotime("+1 days"),
                '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 {

            $has = Db::table('wa_apply_record')
                ->where('id', $taskId)
                ->first();
            if (!$has) {
                throw new \Exception('没有查询当申请!');
            }
            Db::table('wa_apply_record')
                ->where('id', $taskId)->update(['status'=>2]);
            StreamBusiness::addStream($has->uid,$has->money, streamType12, moldType2, moldTypefild2);

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