<?php

namespace app\controller\jiandao;

use app\extra\basic\Base;
use app\extra\tools\XcJob;
use app\model\bluev\BluevOceanAccount;
use HuaweiCloud\SDK\Apig\V2\Model\Signature;
use LinFly\Annotation\Route\Controller;
use LinFly\Annotation\Route\Route;
use support\Request;
use support\Response;
use support\think\Db;
use yzh52521\EasyHttp\Http;


#[Controller(prefix: "/jd/star")]
class JDStar extends Base
{


    #[Route(path: "push",methods: "post")]
    public function getJDYPush(Request $request): Response
    {
        try {
            // 密钥： g8Fbj7NHwS5OhhVEVfP1nTN7
            $param = $request->all();
            print_r($param);
            return success("ok");
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }

    #[Route(path: "info",methods: "get")]
    public function getStarInfo(Request $request): Response
    {
        try {
            $param = $this->_valid([
                "uid.require"   => trans("empty.require"),
                "name.require"  => trans("empty.require")
            ],$request->method());
            if (!is_array($param)) return error($param);
            [$state,$xcData] = (new XcJob)->token()->getStarData($param['uid']);
            if (!$state) return error($xcData);
            if (empty($xcData['list'])) return error("获取数据失败");
            $return = [];
            foreach ($xcData['list'] as $key=>$val) {
                if (md5($val['attribute_datas']['nick_name']) == md5($param['name'])) {
                    $return['level'] = $val['attribute_datas']['author_ecom_level']; // 带货等级
                    $return['avatar_uri'] = $val['attribute_datas']['avatar_uri']; // 用户头像
                    $return['nick_name'] = $val['attribute_datas']['nick_name']; // 用户昵称
                    $return['gender'] = $val['attribute_datas']['gender']; // 性别 1 男 2女
                    $return['follower'] = (int)$val['attribute_datas']['follower']; // 粉丝数
                    $return['star_id'] = (string)$val['star_id']; // 星图ID
                    $return['city'] = $val['attribute_datas']['city']; // 所在城市
                }
            }
            if (empty($return)) return error("获取数据失败");
            [$state,$starDetail] = (new XcJob)->token()->getStarDetail($return['star_id']);
            if (!$state) return error($xcData);
            $return['link'] = "https://www.douyin.com/user/{$starDetail['sec_uid']}";
            return success("ok",$return);
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }

    #[Route(path: "express",methods: "get")]
    public function getExpress(Request $request)
    {
        try {
            $param = $this->_valid([
                "number.require"   => trans("empty.require"),
            ],$request->method());
            if (!is_array($param)) return error($param);
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }

    /**
     * 方舟消耗统计
     * @param Request $request
     * @return Response
     */
    #[Route(path: "stat",methods: "get")]
    public function getAdStatData(Request $request): Response
    {
        try {
            $param = $this->_valid([
                "day.default"   => date("Y-m-d",strtotime("-1 day")),
                "type.default"  => 'day',
                "page.default"  => 1,
                "size.default"  => 10,
            ],$request->method());
            if (!is_array($param)) return error($param);
            switch ($param['type'])
            {
                case "year":
                    $data = (new BluevOceanAccount)
                        ->where("day", "like","%{$param['day']}%")
                        ->field("YEAR(day) as year, advertiser_id, ROUND(SUM(noGrantCost)/100000,2) AS total_cost")
                        ->group("YEAR(day), advertiser_id")
                        ->order('year', 'desc')
                        ->paginate([
                            "list_rows" => $param['size'],
                            "page" => $param['page']
                        ]);
                    break;
                case "mon":
                    $data = (new BluevOceanAccount)
                        ->where("day", "like","%{$param['day']}%")
                        ->field("DATE_FORMAT(day, '%Y-%m') AS month, advertiser_id, ROUND(SUM(noGrantCost)/100000,2) AS total_cost")
                        ->group("DATE_FORMAT(day, '%Y-%m'), advertiser_id")
                        ->order('month', 'desc')
                        ->paginate([
                            "list_rows" => $param['size'],
                            "page" => $param['page']
                        ]);
                    break;
                case "day":
                default:
                    $data = (new BluevOceanAccount)->where("day", $param['day'])->paginate([
                                "list_rows" => $param['size'],
                                "page" => $param['page']
                            ]);
                    break;
            }
            return success("ok",pageFormat($data));
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }

    #[Route(path: "cost",methods: "get")]
    public function getJobCostData(Request $request)
    {
        try {

        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }

}