<?php

namespace App\Model;

use App\Exceptions\DatabaseException;
use http\Env\Request;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class MediaCategoryModel extends Eloquent
{
    protected $collection = "media_category";

    protected $connection = 'mongodb';  //使用mongodb

    protected $primaryKey = '_id';

    public function medias()
    {
        return $this->hasMany('MediaModel');
    }

    /**
     * 查询所有媒资列表(不分页)
     * @param $request
     * @return mixed
     * @throws DatabaseException
     */
    public static function catgList($request){

        try{
            $catgList = MediaCategoryModel::get()->toArray();

            $ids = array_column($catgList,"media_category_id");

            /**
             * 获取用户业务线及对应业务线的权限
             */


            $url = getRbac($request,'api/open/getPlatform','ec8747abc5dbe8288e64f327a76ac517');
            $result = http_request_code($url,
                array(
                    "Content-Type: application/json"
                ),
                'POST',
                json_encode(array())
            );

            LogModel::addlog(['获取商店列表'=>json_decode($result)]);

            $catgs = json_decode($result,true);

            if (!isset($catgs['data'])){
                LogModel::addlog(['获取商店列表失败'=>json_decode($result)]);
                throw new \Exception("获取商店列表失败");
            }


            $catgs = $catgs['data'];

            $tmp = [];
            foreach ($catgs as $key => $catg){
                if (in_array($catg['appid'],$ids)){
                    array_push($tmp,$catg);
                }
            }


        }catch (\Exception $exception){
            throw new DatabaseException($exception->getMessage());
        }

        return $tmp;
    }

    /**
     * 查询所有媒资列表(分页)
     * @param $request
     * @return mixed
     * @throws DatabaseException
     */
    public static function catgListPage($request){
        try{

            $where = [];
            if ($request->name){
                $where['name'] = $request->name;
            }

            $catgList = MediaCategoryModel::where($where)->paginate(10);

        }catch (\Exception $exception){
            throw new DatabaseException("类别不存在");
        }
        return $catgList;

    }

    /**
     * 添加媒资类别
     * @param $request
     * @return mixed
     * @throws DatabaseException
     */
    public static function addMediaCatg($request){

        try{
            $data = [
                'name'          => $request->name,
                'status'        => 0,
                'media_category_id' => $request->media_category_id??'',
                'description'   => $request->description??''
            ];

            $mediaCatg = MediaCategoryModel::where('name',$request->name)->first();

            if (!empty($mediaCatg)){
                throw new \Exception('媒资类别已经存在');
            }
            $id = MediaCategoryModel::insertGetId($data);
        }catch (\Exception $exception){
            throw new DatabaseException($exception->getMessage());
        }
        return $id;
    }
}
