<?php

namespace app\controller\api\business;

use crmeb\basic\BaseController;
use think\App;
use think\facade\Db;

class BusinessSchool  extends BaseController
{
    protected $merId;
    protected $source;
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->source = $this->request->header('source');
        $this->merId =  $this->request->header('merId');
    }
    public function classify_list(){
        // $list = Db::name("business_school_classify")->field('id,name') ->where('mer_id',$this->merId)->where('state',1)->where('is_del',0)->order("sort desc,id desc")-> select() -> toArray();
        $list = Db::name("business_school_classify")->field('id,name')->where('state',1)->where('is_del',0)->order("sort desc,id desc")-> select() -> toArray();
        return app("json") -> success($list);
    }

    public function lst(){
        [$page, $limit] = $this->getPage();
        $where = $this->request->params(['classify']);
        $query=Db::name('business_school')
            -> alias("sp")
            -> join("business_school_classify spc","sp.classify = spc.id","left")
            // ->where('spc.mer_id',$this->merId)
            ->where('sp.state',1)
            ->where('sp.is_del',0)
            -> when(!empty($where['classify']),function ($query) use ($where){
                $query -> where("sp.classify",$where['classify']);
            })->field("sp.id,sp.name,sp.image,sp.type,DATE(sp.create_time) as create_time,sp.reading,spc.name as classify_name");
        $count = $query -> count();
        $list = $query ->order("sp.sort desc,sp.id desc")->page($page,$limit)->select()->toArray();
        return app('json')->success(compact("list","count"));
    }
    public function detail(){
        $id = $this->request->param('id');
        $data=Db::name('business_school')
            -> alias("sp")
            -> join("business_school_classify spc","sp.classify = spc.id","left")
            // ->where('spc.mer_id',$this->merId)
            ->where('sp.state',1)
            ->where('sp.id',$id)
            ->field("sp.id,sp.name,sp.image,sp.cover,sp.type,DATE(sp.create_time) as create_time,sp.mp4,sp.content,sp.reading,spc.name as classify_name")
            ->find();
        if($data){
//            $uid = $this->request->uid();
            $uid = 0;
            $data['relation']=false;
            if($uid){
                $relation_count=Db::name('user_relation')->where('type',11)->where('type_id',$id)->where('uid',$uid)->count();
                if($relation_count>0){
                    $data['relation']=true;
                }

            }
            Db::name('business_school')->where('id',$id)->inc('reading')->update();

            return app('json')->success($data);
        }else{
            return app('json')->fail('课程不存在');
        }

    }
}
