<?php

namespace app\api\controller\worker;

use app\common\controller\Apiw;
use think\Db;
/**
 * 咨询
 */
class News extends Apiw
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = [''];


    public function index()
    {
        //所有分类
        $typelist = Db::name('news_type')
            ->where('company_id',$this->auth->company_id)
            ->order('id' , 'asc')
            ->select();

        $this->success(1, $typelist);
    }

    //
    public function lists(){
        $type_id = input('type_id',0);
        $keyword = input('keyword','','trim');

        $where = [];
        if(!empty($type_id)){
            $where['type_id'] = $type_id;
        }
        if(!empty($keyword)){
            $where['title'] = ['LIKE','%'.$keyword.'%'];
        }

        $list = Db::name('news')->field('content',true)
            ->where('company_id',$this->auth->company_id)
            ->where($where)
            ->autopage()
            ->select();
        $list = list_domain_image($list,['image']);

        $this->success(1, $list);
    }

    //详情
    public function info(){
        $id = input('id',0);
        $info = Db::name('news')
            ->where('id',$id)
            ->find();
        $info = info_domain_image($info, ['image']);

        //收藏
        $info['is_collect'] = $this->is_collect($info['id'],$this->auth->id);

        $this->success(1, $info);
    }

    //收藏,取消收藏
    public function collect(){
        $where = [
            'worker_id'  => $this->auth->id,
            'table'    => 'news',
            'table_id' => input('id',0),
        ];
        $check = Db::name('worker_collect')->where($where)->find();
        if($check){
            Db::name('worker_collect')->where($where)->delete();
            $this->success('已取消收藏');
        }else{
            Db::name('worker_collect')->insertGetId($where);
            $this->success('收藏成功');
        }
    }

    //动态是否收藏
    private function is_collect($id,$uid){
        $where = [
            'worker_id'  => $uid,
            'table'    => 'news',
            'table_id' => $id,
        ];
        $check = Db::name('worker_collect')->where($where)->find();
        if($check){
            return 1;
        }else{
            return 0;
        }
    }



}