<?php
namespace App\JsonRpc;

use App\Model\OldModel\Article as OldArticle;
use App\Model\OldModel\ArticleData as OldArticleData;
use App\Model\OldModel\Category;
use App\Model\Article;
use App\Model\Web;
use App\Model\Rule;
use App\Model\ArticleData;
use Hyperf\DbConnection\Db;
use Hyperf\RpcServer\Annotation\RpcService;
use App\Tools\Result;

use function Hyperf\Support\retry;

#[RpcService(name: "CollectorService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
class CollectorService implements CollectorServiceInterface
{
    /**
     * 添加网站
     * @param array $data
     * @return array|mixed
     */
    public function addWeb(array $data): array
    {
        $where = [
            'name' => $data['name']
        ];
        $isweb = Web::where($where)->first();
        if(empty($isweb)){
            date_default_timezone_set('Asia/Shanghai');
            $time = time();
            $catetime = date('Y-m-d H:i:s', $time);
            $data['created_at'] = $catetime;
            $web = Web::insert($data);
            
        }else{
            return Result::error('此网站已存在，不可重复添加！');
        }
        if(empty($web)){
            return Result::error('添加失败');
        }
        return Result::success('添加成功');
    }
    /**
     * 获取并搜索网站
     * @param array $data
     * @return array|mixed
     */
    public function getWeb(array $data): array
    {
        
        if(isset($data['keyWord'])){
            $where = [
                ['name','like','%'.$data['keyWord'].'%']
            ];
            $rep = Web::where($where)->limit($data['pageSize'])->orderBy("created_at","desc")->offset(($data['page']-1)*$data['pageSize'])->get();
            $count =  Web::where($where)->count();
            if($count==0){
                return Result::error('未查找到相关网站！');
            }
        }else{
            $rep = Web::limit($data['pageSize'])->orderBy("created_at","desc")->offset(($data['page']-1)*$data['pageSize'])->get();
            $count =  Web::count();
            
        }
        $data = [
            'rep' => $rep->toArray(),
            'count' => $count
        ];
        if(empty($rep)){
            return Result::error('您还未添加网站，请先去添加！');
            
        }
        
        return Result::success($data);
    }
    /**
     * 修改网站
     * @param array $data
     * @return array|mixed
     */
    public function upWeb(array $data): array
    {
        $web = Web::where('id',$data['id'])->first();
        if(empty($web->toArray())){
            return Result::error('请输入正确的网站id！');
            
        }else{
            $id = Web::where('id',$data['id'])->update($data);
            if(empty($id)){
                return Result::error('无法修改！');
            }
        }
        return Result::success($id);
    }
    /**
     * 删除网站
     * @param array $data
     * @return array|mixed
     */
    public function delWeb(array $data): array
    {
        $web = Web::where('id',$data['id'])->first();
        if(empty($web->toArray())){
            return Result::error('请输入正确的网站id！');
            
        }else{
            $id = Web::where('id',$data['id'])->delete();
            if(empty($id)){
                return Result::error('无法删除！');
            }
        }
        return Result::success($id);
    }
    /**
     * 添加规则任务
     * @param array $data
     * @return array|mixed
     */
    public function addRule(array $data): array
    {
        $web = Web::where('id',$data['web_id'])->get();
        if(empty($web->toArray())){
            return Result::error('请输入正确的网站id！');
            
        }else{
            $rulename = Rule::where('name',$data['name'])->get();
            if(empty($rulename->toArray())){
                date_default_timezone_set('Asia/Shanghai');
                $time = time();
                $catetime = date('Y-m-d H:i:s', $time);
                $data['created_at'] = $catetime;
                $id = Rule::insertGetId($data);
                $result = ['data' => $id];
            }else{
                return Result::error('此任务已存在！');
            }
        }
       
        return Result::success($result['data']);
    }
    /**
     * 获取并搜索规则任务
     * @param array $data
     * @return array|mixed
     */
    public function getRule(array $data): array
    {
        $web = Web::where('id',$data['web_id'])->get();
        if(empty($web->toArray())){
            return Result::error('请输入正确的网站id!');
            
        }else{
            if(isset($data['keyWord'])){
                $where = [
                    ['name','like','%'.$data['keyWord'].'%'],
                    ['web_id','=', $data['web_id']] 
                ];
                $rep = Rule::withCount(relations:'arts')->where($where)->limit($data['pageSize'])->orderBy("created_at","desc")->offset(($data['page']-1)*$data['pageSize'])->get();
                $count = Rule::where($where)->count();
                if($count==0){
                    return Result::error('未查找到相关网站！');
                }
            }else{
                $rep = Rule::withCount(relations:'arts')->where('web_id',$data['web_id'])->limit($data['pageSize'])->orderBy("created_at","desc")->offset(($data['page']-1)*$data['pageSize'])->get();
                $count = Rule::where('web_id',$data['web_id'])->count();
                if($count==0){
                    return Result::error('此网站下暂无规则任务！');
                }
            }       
        }
        $data = [
            'rep' => $rep->toArray(),
            'count' => $count
        ];
        return Result::success($data);
    }
     /**
     * 获取某个任务规则
     * @param array $data
     * @return array|mixed
     */
    public function getOneRule(array $data): array
    {
        $result = Rule::where('id',$data['id'])->first();
        if(empty($result)){
            return Result::error('请输入正确的规则任务id！');
            
        }else{
            return Result::success($result);
        } 
    }
    /**
     * 修改规则任务
     * @param array $data
     * @return array|mixed
     */
    public function upRule(array $data): array
    {
        $rule = Rule::where('id',$data['id'])->first();
        if(empty($rule)){
            return Result::error('请输入正确的规则任务id！');
            
        }else{
            $rulename = Rule::where('name',$data['name'])->first();
            if(empty($rulename)){
                $result = Rule::where('id',$data['id'])->update($data);
            }else{
                return Result::error('此任务名称已存在！');
            }
        }
       
        return Result::success($result);
    }
    /**
     * 删除规则任务
     * @param array $data
     * @return array
     */
    public function delRule(array $data): array
    {
        $where = ['id' => $data['rule_id']];
        $rule = Rule::where($where)->first();
        if(empty($rule)){
            return Result::error('请输入正确的规则任务id！');
            
        }else{
            $delrule = Rule::where($where)->delete();
            if(empty($delrule)){
                return Result::error('此规则任务删除失败！');
            }
        }
        return Result::success($delrule);
    }

    /**
     * 开始采集
     * @param array $data
     * @return array
     */
    public function sendCrawler(array $data): array
    {
        $result =  Article::get();
        $b = OldArticle::get();
        $a = [
            'old'=>$b,
            'new'=>$result
        ];
        return  Result::success($a);
    }
    /**
     * 获取并搜索资讯
     * @param array $data
     * @return array
     */
    public function getInfo(array $data): array
    {
        $where = [
            ['rule_id','=',$data['rule_id']]
        ];
        //若存在条件参数都存到where数组
        if(isset($data['title'])){
            $where[] = ['title','like','%'.$data['title'].'%'];
        }
        if(isset($data['source'])){
            $art_source = Article::where($where)->get();
            if(!empty($art_source->toArray())){
                $where[] = ['source','=',$data['source']];
            } 
        }
        if(isset($data['state'])){
            $where[] = ['state','=',$data['state']];
        }
        //跨库查询栏目导航及采集的新闻
        $info = Article::query()
        ->where($where)
        ->with(['category' => function ($query) {
            $query->select('name');
        }])
        ->orderBy("article.id","desc")
        ->limit($data['pageSize'])
        ->offset(($data['page']-1)*$data['pageSize'])->get();
        $count = Article::where($where)->count();
        if($count == 0){
            return Result::error('暂无资讯');
        }
        $data = [
            'rep' => $info->toArray(),
            'count' => $count
        ];
        return  Result::success($data);
    }
    /**
     * 获取某个资讯
     * @param array $data
     * @return array
     */
    public function getOneInfo(array $data): array
    {
        $where = ['id' => $data['art_id']];
        $inf = Article::where($where)->first();
        $info =  Article::where($where)
        ->leftJoin('article_data','article_id','id')
        ->select('article.*','article_data.content')
        ->first();
        if($inf['catid']!=null){
            $category = Category::where(['id'=>$info['catid']])->select('name')->first();
            $info['category'] = $category['name'];
        }
        if(empty($info)){
            return Result::error('请输入正确的资讯id！');
        }
        return  Result::success($info);
    }
    /**
     * 修改资讯
     * @param array $data
     * @return array
     */
    public function upInfo(array $data): array
    {
        $id = $data['art_id'];
        $content = $data['content'];
        unset($data['art_id']);
        //去掉此元素
        unset($data['content']);
        //去掉此元素
        $info =  Article::where('id',$id)->first();  
        if($info['state']==1){
            return Result::error('此文章已导入 ，不可编辑！');
        }else{
            Db::beginTransaction();
            try{
                $info =  Article::where('id',$id)->update($data);  
                $art_data = ArticleData::where('article_id',$id)->update(['content'=>$content]);

                Db::commit();
            } catch(\Throwable $ex){
                Db::rollBack();
                var_dump($ex->getMessage());
                return Result::error("修改失败",0);
            }
            $data = [
                'info' => $info,
                'art_data' => $art_data
            ];
            return  Result::success($data);
        }
        
    }
    /**
    * 删除资讯
    * @param array $data
    * @return array
    */
    public function delInfo(array $data): array
    {
        $id = $data['art_id'];
        $info = Article::where('id',$id)->first();
        if($info['state']==1){
            return Result::error('此文章已导入，不可删除！'); 
        }else{
            Db::beginTransaction();
            try{
                $delinfo = Article::where('id',$id)->delete();
                $deldata = ArticleData::where('article_id',$id)->delete();
                Db::commit();
            } catch(\Throwable $ex){
                Db::rollBack();
                var_dump($ex->getMessage());
                return Result::error("删除失败",0);
            }
        }
        $data = [
            'delinfo' => $delinfo,
            'deldata' => $deldata
        ];
        return  Result::success($data);
    }
    /**
    * 关联导航池
    * @param array $data
    * @return array
    */
    public function addCatid(array $data): array
    {
        $id = $data['rule_id'];
        //查找此规则任务下的文章是否都已经导入
        $info = Article::where('rule_id',$id)->where('state',0)->select('id')->get();
        if(empty($info->toArray())){
            return Result::error('所有文章都已导入，不可修改关联的导航池！'); 
        }else{
            //查找此规则任务下的文章是否已经有导入的文章
            $article = Article::where('rule_id',$id)->where('state',1)->select('id')->get();
            if(!empty($article->toArray())){
                //若有已导入的文章则直接复制之前已导入的导航池
                $catid = Article::whereIn('id',$article)->select('catid')->first();
                //若未导入的文章已经复制之前的导航，则无需修改
                $art_catid = Article::whereIn('id',$info)->whereNull('catid')->count();
                if($art_catid>0){
                    $result = Article::whereIn('id',$info)->update(['catid'=>$catid['catid']]);
                }else{
                    $result = ['已全部关联导航，无需导入！'];
                }
            }else{
                //若不存在已导入的文章则判断是否存在导航id
                if(isset($data['catid'])){
                    //若存在直接使用此导航id
                    $result = Article::whereIn('id',$info)->update(['catid'=>$data['catid']]);
                    
                }else{
                    //若不存在则返回所有导航栏目
                    $result = Category::select('id','name')->get();
                }
            }
              
        }
        return  Result::success($result);
    }
    /**
    * 导入文章
    * @param array $data
    * @return array
    */
    public function addArt(array $data): array
    {
        // var_dump("======@@@====");
        $where = [
            'rule_id' => $data['rule_id'],
            'state' => 0
        ];
        $arts_id = Article::where($where)->wherenotNull('catid')->orderBy('id')->select('id')->get();
        $arts = Article::where($where)->wherenotNull('catid')->select('title','catid','level','introduce','keyword','author','copyfrom','fromurl','hits','islink','imgurl','admin_user_id','is_original')->orderBy('id')->get()->toArray();       
        $arts_data = ArticleData::whereIn('article_id',$arts_id)->select('content')->orderBy('article_id')->get()->toArray();
        // var_dump($article_data);
        $data = [
            'articles' => $arts,
            'art_content' => $arts_data
        ];
        Db::beginTransaction();
        try{
           
            $oldart = OldArticle::insert($arts);
            $oldart_data = OldArticleData::insert($arts_data);
            $upstate_art = Article::where($where)->wherenotNull('catid')->update(['state' => 1]);
            Db::commit();
            
        } catch(\Throwable $ex){
            Db::rollBack();
            var_dump($ex->getMessage());
            return Result::error($ex->getMessage(),0);
        }
        return Result::success($data);
    }
}