<?php

namespace app\controller\api;

use app\extra\basic\Base;
use app\middleware\WxMiddleware;
use app\model\blue\BlueCategory;
use app\model\blue\BlueExpertPlatform;
use app\model\blue\BlueUserOpen;
use DI\Attribute\Inject;
use LinFly\Annotation\Route\Controller;
use LinFly\Annotation\Route\Route;
use LinFly\Annotation\Route\Middleware;
use support\Request;
use support\Response;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;


#[Controller(prefix: "/wx_api/user"),Middleware(WxMiddleware::class)]
class User extends Base
{

    #[Inject]
    protected BlueUserOpen $model;

    #[Route(path: "data",methods: "get")]
    public function getUserData(Request $request): Response
    {
        try {
            $user = $request->user;
            if (empty($user)) return errorTrans("empty.data");
            $platform = $this->checkPlatform($user['openid']);
            $member = $this->model->where("openid",$user['openid'])->field("openid,headimg,nickname")
                ->append(["withdraw",'day','mon','bonus','task','sign','level','dy_level','platform'])
                ->withAttr(['withdraw' => function(){
                        return 0; // 可提现金额
                    },'day' => function(){
                        return 0; // 今日佣金
                    },'mon' => function(){
                        return 0; // 本月佣金
                    },'bonus' => function(){
                        return 0; // 已获得奖金
                    },'task' => function(){
                        return 0; // 已接任务
                    },'sign' => function(){
                        return 0; // 0未签约，1已签约
                    },'level' => function(){
                        return 0; // 平台等级
                    },'dy_level' => function(){
                        return 0; // 抖音等级
                    },'platform' => function() use($platform) {
                        return $platform;
                    }
                ])
                ->findOrEmpty();
            return success("ok",$member->toArray());
        } catch (\Throwable $th) {
            return error($th->getMessage());
        }
    }



    /**
     * 绑定平台信息
     * @param string $openId
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    protected function checkPlatform(string $openId = ""): array
    {
        $category = (new BlueCategory)->where("status",1)->where("type",1)->field("sign,name,icon")->select();
        if ($category->isEmpty()) return [];
        $platform = [];
        $platformModel = new BlueExpertPlatform();
        foreach ($category as $key=>$val)
        {
            $bind = $platformModel->where("source",$val['sign'])->where("openid",$openId)->findOrEmpty();
            if ($bind->isEmpty()) {
                $bindState = 0;
            } else {
                $bindState = $bind['status'] + 1;
            }
            $platform[$val['sign']]['status'] = $bindState;
        }
        return $platform;
    }

}