<?php
/**
 * User: Qinii
 * Date: 2020-06-05
 * Time: 09:41
 */
namespace app\controller\api\article;

use app\common\repositories\user\UserVisitDayRepository;
use app\common\repositories\user\UserVisitRepository;
use app\Request;
use think\App;
use app\common\repositories\article\ArticleRepository as repository;
use crmeb\basic\BaseController;
use think\facade\Db;

class Article extends BaseController
{
    /**
     * @var repository
     */
    protected $repository;
    protected $merId;
    protected $source;
    /**
     * StoreBrand constructor.
     * @param App $app
     * @param repository $repository
     */
    public function __construct(App $app, repository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
        $this->source = $this->request->header('source');
        $this->merId =  $this->request->header('merId');
    }

    /**
     * @return mixed
     * @author Qinii
     */
    public function lst($cid)
    {
//        [$page, $limit] = $this->getPage();
//        $where['status']=1;
//        if($cid > 0){
//            unset($where['is_hot']);
//            $where['cid'] = $cid;
//        }
//        $mer_id=$this->request->params(['mer_id']);
//        $mer_id=(int)$mer_id['mer_id']??0;
//        if($mer_id>0){
//            $where['mer_id'] = $mer_id;
//        }
//
//        return app('json')->success($this->repository->search($mer_id,$where, $page, $limit));
        [$page, $limit] = $this->getPage();
        $where['status']=1;
        if($cid > 0){
            unset($where['is_hot']);
            $where['cid'] = $cid;
        }

        $mer_id=$this->request->params(['mer_id']);
        $mer_id=(int)$mer_id['mer_id']??0;
        if($this->source=='yhc' && $mer_id==0){
            $mer_id=$this->merId;
        }
        if($where['cid'] == 23){
            $list=Db::name('article')->where('mer_id',$mer_id)->whereIn('cid',[23,24])->page($page,$limit)->hidden(['update_time'])->where('wechat_news_id',0)->order('is_top,is_hot DESC')->order('create_time desc')->select()->toArray();
            $count=Db::name('article')->where('mer_id',$mer_id)->whereIn('cid',[23,24])->hidden(['update_time'])->where('wechat_news_id',0)->order('create_time','desc')->count('article_id');
        }else{
            $list=Db::name('article')->where('mer_id',$mer_id)->where('cid',$where['cid'])->page($page,$limit)->hidden(['update_time'])->where('wechat_news_id',0)->order('is_top,is_hot DESC')->order('create_time desc')->select()->toArray();
            $count=Db::name('article')->where('mer_id',$mer_id)->where('cid',$where['cid'])->hidden(['update_time'])->where('wechat_news_id',0)->order('create_time','desc')->count('article_id');
        }
//        echo Db::name('article')->getLastSql();
        foreach($list as  $k=>$v){
            $v['articleCategory']=Db::name('article_category')->where('article_category_id',$v['cid'])->find();
            $cate = Db::name("article_category")
                ->where('article_category_id',$v['articleCategory']['pid'])
                ->field("article_category_id,title")
                ->find();
            $list[$k]['articleCategory']['title'] = $cate['title'].'-'.$v['articleCategory']['title'];
            $v['content']=Db::name('article_content')->where('article_content_id',$v['article_id'])->find();
        }
        return app('json')->success(compact('count','list'));
    }

    /*文章详情*/
    public function detail($id)
    {
        if (!$this->repository->merApiExists($id))
            return app('json')->fail('文章不存在');
        $article=Db::name('article')->where('article_id',$id)->find();
        $article['content']=Db::name('article_content')->where('article_content_id',$id)->find();
        $article['articleCategory']=Db::name('article_category')->where('article_category_id',$article['cid'])->find();

        return app('json')->success($article);
    }



    /*系统公告*/
    public function noticeLst()
    {
       
        [$page, $limit] = $this->getPage();
        $where['status']=1;
        $where['is_del'] = 0;

        $ip = app('request')->header('x-forwarded-for');;
        $userVisitRepository = app()->make(UserVisitRepository::class);
        if (empty($userVisitRepository->getWhere(['ip' => $ip]))) {
            $userVisitRepository->addVisit(0, 'count', 0, '', $ip);
        }
        $where['mer_id'] = $this->request->header('merId',0) ;
        return app('json')->success($this->repository->notice($where, $page, $limit));
    }

    /*系统公告详情*/
    public function noticeDetail($id)
    {
        if (!$this->repository->noticeExists($id))
            return app('json')->fail('公告不存在');
        return app('json')->success($this->repository->noticeExists($id));
    }

    public function zjky_artcle()
    {
        [$page, $limit] = $this->getPage();
        $where['status']=1;
        $where['mer_id']='290';

        return app('json')->success($this->repository->search(0,$where, $page, $limit));
    }


}
