<?php
namespace App\JsonRpc;
use App\Model\Ad;
use App\Model\AdPlace;
use Hyperf\RpcServer\Annotation\RpcService;
use App\Tools\Result;
#[RpcService(name: "AdService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
class AdService implements AdServiceInterface
{
    /**
     * @param array $data
     * @return string
     */
    public function createAd(array $data): array
    {
        $insertData = [
            'name'=>$data['name']??'',
            'pid'=>$data['pid']??0,
            'areaid'=>$data['areaid']??0,
            'amount'=>$data['amount']??0,//浏览数量
            'introduce'=>$data['introduce']??'',
            'hits'=>$data['hits']??0, //点击数量
            'fromtime'=>$data['f_t_date'][0]??0,
            'totime'=>$data['f_t_date'][1]??0,
            'text_name'=>$data['text_name']??'',
            'text_url'=>$data['text_url']??'',
            'text_title'=>$data['text_title']??'',
            'image_src'=>$data['image_src']??'',
            'image_url'=>$data['image_url']??'',
            'image_alt'=>$data['image_alt']??'',
            'video_src'=>$data['video_src']??'',//视频
            'video_url'=>$data['video_url']??'',
            'video_auto'=>$data['video_auto'] ??0,
            'video_loop'=>$data['video_loop'] ??0,
            'status'=>$data['status']??2,
            'remark'=>$data['remark']??'',
        ];
        $result = Ad::query()->insertGetId($insertData);
        if($result){
            return Result::success();
        }else{
            return Result::error("创建广告失败",500);
        }
    }
    /**
     * @param int $id
     * @return array
     */
    public function getAdInfo(int $id): array
    {
        $adInfo = Ad::query()->find($id);
        if (empty($adInfo)) {
            return Result::error("没有数据",0);
        }
        return Result::success($adInfo->toArray());
    }

    public function getAdList(array $data): array
    {
        $where = [
            ['ad.name','like','%'.$data['keyWord'].'%'],
        ];
        if(isset($data['pid'])){
            $where[]=   ['ad.pid','=',$data['pid']];
        }
        $rep = Ad::where($where)
            ->leftJoin("ad_place","ad.pid","ad_place.id")
            ->select("ad.*","ad_place.name as place_name","ad_place.typeid")
            ->limit($data['pageSize'])->orderBy("id","asc")->offset(($data['page']-1)*$data['pageSize'])->get();
        $count =  Ad::where($where)->count();
        $data = [
            'rows'=>$rep->toArray(),
            'count'=>$count
        ];
        if(empty($rep->toArray())){
            return Result::error("没有数据");
        }
        return Result::success($data);
    }

    public function updateAd(array $data): array
    {
        $where = [
            'id'=>$data['id']
        ];
        $insertData = [
            'name'=>$data['name']??'',
            'pid'=>$data['pid']??0,
            'areaid'=>$data['areaid']??0,
            'amount'=>$data['amount']??0,//浏览数量
            'introduce'=>$data['introduce']??'',
            'hits'=>$data['hits']??0, //点击数量
            'fromtime'=>$data['f_t_date'][0]??0,
            'totime'=>$data['f_t_date'][1]??0,
            'text_name'=>$data['text_name']??'',
            'text_url'=>$data['text_url']??'',
            'text_title'=>$data['text_title']??'',
            'image_src'=>$data['image_src']??'',
            'image_url'=>$data['image_url']??'',
            'image_alt'=>$data['image_alt']??'',
            'video_src'=>$data['video_src']??'',//视频
            'video_url'=>$data['video_url']??'',
            'video_auto'=>isset($data['video_auto']) && empty($data['video_auto'])?0:$data['video_auto'],
            'video_loop'=>isset($data['video_loop']) && empty($data['video_loop'])?0:$data['video_loop'],
            'status'=>$data['status']??2,
            'remark'=>$data['remark']??'',
        ];
        $result = Ad::where($where)->update($insertData);
        if($result){
            return Result::success($result);
        }else{
            return Result::error("更新失败");
        }
    }

    public function delAd(array $data): array
    {
        $result = Ad::where($data)->delete();
        if(!$result){
            return Result::error("删除失败");
        }
        return Result::success($result);
    }

    public function getAdPlaceList(array $data): array
    {
        $where = [
            ['name','like','%'.$data['keyWord'].'%'],
        ];
        if(isset($data['website_id'])){
            $where[]=   ['website_id','=',$data['website_id']];
        }
        $rep = AdPlace::where($where)->limit($data['pageSize'])->orderBy("id","desc")->offset(($data['page']-1)*$data['pageSize'])->get();
        $count =  AdPlace::where($where)->count();
        $data = [
            'rows'=>$rep->toArray(),
            'count'=>$count
        ];
        if(empty($rep->toArray())){
            return Result::error("没有数据");
        }
        return Result::success($data);
    }

    public function createAdPlace(array $data): array
    {
        unset($data['size']);
        $result = AdPlace::query()->insertGetId($data);
        if($result){
            return Result::success();
        }else{
            return Result::error("创建广告位失败",500);
        }
    }

    public function updateAdPlace(array $data): array
    {
        $where = [
            'id'=>$data['id']
        ];
        $Insdata = [
            'website_id'=>$data['website_id']??'',
            'typeid'=>$data['typeid']??1,
            'status'=>$data['status']??2,
            'name'=>$data['name']??'',
            'thumb'=>$data['thumb']??'',
            'introduce'=>$data['introduce']??'',
            'code'=>$data['code']??'',
            'width'=>empty($data['width'])?0:$data['width'],
            'height'=>empty($data['height'])?0:$data['height'],
            'price'=>$data['price']??0,
        ];
        var_dump($Insdata);
        $result = AdPlace::where($where)->update($Insdata);
        if($result){
            return Result::success($result);
        }else{
            return Result::error("更新失败");
        }
    }

    public function delAdPlace(array $data): array
    {
        $adList = Ad::where(['pid'=>$data['id']])->get();
        if($adList->toArray()){
            return Result::error("广告位里面还有广告,不能删除广告位");
        }
        $result = AdPlace::where($data)->delete();
        if(!$result){
            return Result::error("删除失败");
        }
        return Result::success($result);
    }

}