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


    public function getHomeList(array $param = [])
    {
        $this->mode = new BlueTask();
        return $this->searchVal($param,$this->searchFilter($param))->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);
        }])->with(['categoryJoin' => function ($query) {
            $query->field("sign,name,icon,small_icon");
        },'levelJoin' => function($query){
            $query->field("id,name,level");
        }])->field("id,title,task_sn,product_cover_url,total_budget,sign_number,need_number,category,level,sys_level,product_price")->paginate([
            "list_rows" => $param['pageSize'],
            "page"      => $param['page']
        ]);
    }

    /**
     * 任务详情
     * @param array $param
     */
    public function getTaskDetail(array $param = [])
    {
        $this->mode = new BlueTask();
        return $this->searchVal($param,$this->searchFilter($param))->append(['progress','total_budget_format','price_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);
        },'price_format' => function($data,$resp){
            return formatMoneyKw($resp['product_price']/100);
        }])->with(['categoryJoin' => function ($query) {
            $query->field("sign,name,icon,small_icon");
        },'levelJoin' => function($query){
            $query->field("id,name,level");
        }])->find();
    }

    /**
     *
     * @param array $param
     * @return array
     */
    public function searchFilter(array $param = []): array
    {
        $filter = [];
        !empty($param['id']) && $filter[] = ["id", '=', $param['id']];
        !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']];
        !empty($param['name']) && $filter[] = ["name", 'like', "%{$param['name']}%"];
        return $filter;
    }

}