<?php

namespace app\controller\jiandao;

use app\extra\basic\Base;
use app\model\blue\BlueExpertPlatform;
use LinFly\Annotation\Route\Controller;
use LinFly\Annotation\Route\Route;
use support\Request;
use support\Response;
use yzh52521\EasyHttp\Http;


#[Controller(prefix: "/v2")]
class StarJob extends Base
{


    #[Route(path: "task/push",methods: "get")]
    public function pushJobList(Request $request): Response
    {
        try {
            $resp = Http::asJson()->post("https://api.jiandaoyun.com/api/v1/automation/tenant/69dc899f71c9656940c3db33/hooks/69e07cb9d9016a508ad1dfad0bb32ba47135e0453631e9e6",[
                "time" => time()
            ])->array();
            return success("ok",$resp);
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }

    /**
     * 获取已添加的任务
     * @param Request $request
     * @return Response
     */
    #[Route(path: "task/param",methods: "post")]
    public function getJobList(Request $request): Response
    {
        try {
            $param = $request->all();
            print_r($param);
            return success("ok");
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }

    /**
     * 检查达人数据是否存在
     * @param Request $request
     * @return Response
     */
    #[Route(path: "task/star",methods: "post")]
    public function checkStarId(Request $request): Response
    {
        try {
            $param = $this->_valid([
                "total.require"     => trans("empty.require"),
                "uid.require"       => trans("empty.require"),
                "platform.require"  => trans("empty.require"),
            ],$request->method());
            if (!is_array($param)) return error($param);
            $star = (new BlueExpertPlatform)->where(['unique_id' => $param['uid'],'source' => $param['platform']])->findOrEmpty();
            if ($star->isEmpty()) return error("数据不存在");
            if ($param['total'] == 0) return success("ok");
            $star->jd_exist = 1;
            $state = $star->save();
            if (!$state) return errorTrans("error.data");
            return success("ok");
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }

}