<?php

namespace App\Models\Fpdx;

use App\Models\Model;

/**
 * App\Models\Fpdx\CpTaskModel
 *
 * @property int $id
 * @property string $uid 用户
 * @property int $task_id 任务id
 * @property int $trem 期数
 * @property string $data 数据
 * @property int $create_at 时间
 * @property int $is_show 是否展示
 * @property int $thumb 点赞数
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\CpTaskModel newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\CpTaskModel newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\CpTaskModel query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\CpTaskModel whereCreateAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\CpTaskModel whereData($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\CpTaskModel whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\CpTaskModel whereIsShow($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\CpTaskModel whereTaskId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\CpTaskModel whereThumb($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\CpTaskModel whereTrem($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\CpTaskModel whereUid($value)
 * @mixin \Eloquent
 */
class CpTaskModel extends Model
{
    protected $table = 'kdgx_partner_charge_cptask';

    public $timestamps = false;

    /**
     * 某人某期任务完成情况
     * @param  term 期数
     * @param  uid 用户id
     * @return array
     */
    public static function complateData($term, $uid)
    {
        // 每天任务的数量
        $taskModel = new TaskModel();
        $datas = array();
        for ($i = 1; $i <= 3; $i++) {
            $where = [
                ['term', '=', $term],
                ['day', '=', $i]
            ];
            $datas[$i]['count'] = $taskModel->where($where)->count();
            $tmp = array();
            array_map(function ($task) use (&$tmp) {
                array_push($tmp, $task['id']);
            }, $taskModel->where($where)->get()->toArray());
            $datas[$i]['tasks'] = $tmp;
            if (self::where(['uid' => $uid])->whereIn('task_id', $tmp)->count() < $datas[$i]['count']) {
                $datas[$i]['complated'] = 0;
            } else {
                $datas[$i]['complated'] = 1;
            }
        }
        return $datas;
    }

    /**
     * 获取某人某期任务完成比例
     * @param $term 期数
     * @param $uid 用户
     * @return array
     */
    public function complate($term, $uid)
    {
        // 每天任务的数量
        $taskModel = new TaskModel();
        $data = $taskModel->where('term', $term)->get()->toArray();
        $tmp = array();
        array_map(function ($task) use (&$tmp) {
            array_push($tmp, $task['id']);
        }, $data);
        $count = self::where('uid', $uid)->whereIn('task_id', $tmp)->count();
        return array(
            'total' => count($data),
            'complated' => $count
        );
    }
}
