<?php

namespace app\extra\service\blue;

use app\extra\basic\Service;
use app\model\blue\BlueTask;

class TaskService extends Service
{

    /**
     * 列表
     * @param array $param
     */
    public function getList(array $param = [])
    {
        $this->mode = new BlueTask();
        return $this->searchVal($param,$this->searchFilter($param))->append(['progress'])->withAttr(['progress' => function ($data,$resp) {
            if ($resp['sign_number'] == 0) return $resp['sign_number'];
            return ceil($resp['sign_number']/$resp['need_number'] * 100);
        }])->with(['categoryJoin' => function ($query) {
            $query->field("sign,name,icon");
        }])->paginate([
            "list_rows" => $param['pageSize'],
            "page"      => $param['page']
        ]);
    }

    /**
     *
     * @param array $param
     * @return array
     */
    public function searchFilter(array $param = []): array
    {
        $filter = [];
        !empty($param['type']) && $filter[] = ["type", '=', $param['type']];
        !empty($param['status']) && $filter[] = ["status", '=', $param['status']];
        !empty($param['name']) && $filter[] = ["name", 'like', "%{$param['name']}%"];
        return $filter;
    }

}