<?php

namespace app\controller\admin;

use app\extra\basic\Base;
use app\extra\service\blue\ExpertService;
use app\middleware\AuthMiddleware;
use app\model\blue\BlueExpert;
use app\model\blue\BlueExpertPlatform;
use DI\Attribute\Inject;
use LinFly\Annotation\Route\Controller;
use LinFly\Annotation\Route\Middleware;
use LinFly\Annotation\Route\Route;
use support\Request;
use support\Response;

/**
 * 达人
 */
#[Controller(prefix: "/api/expert"),Middleware(AuthMiddleware::class)]
class Expert extends Base
{

    #[Inject]
    protected BlueExpert $model;

    #[Inject]
    protected ExpertService $service;

    /**
     *
     * @param Request $request
     * @return Response
     */
    #[Route(path: "list",methods: "get")]
    public function getDataList(Request $request): Response
    {
        try {
            $param = $request->all();
            $list = $this->service->getList($param);
            return successTrans("success.data",pageFormat($list),200);
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }

    #[Route(path: "platform",methods: "get")]
    public function getPlatform(): Response
    {
        try {
            $param = $this->_valid([
                "id.require"    => trans("empty.require")
            ]);
            if (!is_array($param)) return error($param);
            $user = $this->model->where("id",$param['id'])->findOrEmpty();
            if ($user->isEmpty()) return errorTrans("empty.data");
            $platform = (new BlueExpertPlatform)->where("openid",$user['openid'])->append(['source_name','aweme_count_format','follower_count_format','total_favorited_format'])->withAttr(['source_name' => function($data,$resp){
                return $this->service->source[$resp['source']];
            },'aweme_count_format' => function($data,$resp){
//                return formatMoneyKw($resp['aweme_count'],1);
                return $resp['aweme_count'];
            },'follower_count_format' => function($data,$resp){
                return formatMoneyKw($resp['follower_count'],1);
            },'total_favorited_format' => function($data,$resp){
                return formatMoneyKw($resp['total_favorited'],1);
            }])->select()->toArray();
            return success("ok",$platform);
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }


}