<?php

namespace app\extra\service\blue;

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

class BlueTaskUserService extends Service
{

    public function getHomeList(array $param = [])
    {
        $this->mode = new BlueTaskUser();
        return $this->searchVal($param,$this->searchFilter($param))->with(['task' => function ($query) {
            $query->field("id,title,task_sn,product_cover_url,total_budget,sign_number,need_number,category,level,sys_level,product_price")->append(['progress','total_budget_format'])->withAttr(['progress' => function ($data,$resp) {
                if ($resp['sign_number'] == 0) return $resp['sign_number'];
                return ceil($resp['sign_number']/$resp['need_number'] * 100);
            },'total_budget_format' => function($data,$resp) {
                return formatMoneyKw($resp['total_budget']/100);
            }]);
        }])->paginate([
            "list_rows" => $param['pageSize'],
            "page"      => $param['page']
        ]);
    }

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

}