<?php


namespace App\Web\Models;



use App\Models\BaseModel;
use Illuminate\Support\Facades\DB;


class WebStaticPageModel extends BaseModel
{



    /**
     * 状态字段
     */
    const DELETED_AT = 'status';
    /**
     * @var string
     */
    protected $table = 'web_static_page';


    /**
     * 检查路由地址是否唯一
     * */
    public function checkRoutePathUnique($routePath,$id=0){
        $where=[];
        if(!empty($id)){
            $where[]=['id', '<>', $id];
        }
        $where[]=['status', '<', 2];
        $where[]=['route_path', '=', trim($routePath)];
        return $this->checkFieldUnique('route_path',$where);
    }


    /**
     * 检查页面名称是否唯一
     * */
    public function checkPageNameUnique($pageName,$id=0){
        $where=[];
        if(!empty($id)){
            $where[]=['id', '<>', $id];
        }
        $where[]=['status', '<', 2];
        $where[]=['page_name', '=', trim($pageName)];
        return $this->checkFieldUnique('page_name',$where);
    }

    /**
     * 静态页面保存
     * @param array $data
     * */
    public function saveStaticPageData($data){
        if (!empty($data['id'])) {
            $id = $data['id'];
            $this->newInstance()->where('id', $data['id'])->update($data);
        } else {
            $id = $this->newInstance()->insertGetId($data);
        }
        return $id;
    }


    /**
     * 获取静态页面列表
     * */
    public function getStaticPageList($params,$fields='a.id,a.create_time,a.page_name,a.route_path,a.seo_id,a.sort,a.status,a.type
,a.update_time,b.seo_title,b.seo_describe,b.seo_keyword,b.urla,b.trans_status,b.track_code_head,b.track_code_body'){
        list($pageSize, $page, $skip) = $this->getPaginatorParams($params);
        $where=[];
        if(isset($params['status'])){
            $where[]=['a.status','=',$params['status']];
        }else{
            $where[]=['a.status','<',2];
        }
        $query= $this->newInstance()->alias('a')->leftJoin('web_seo as b', 'a.seo_id', '=', 'b.id')->where($where);
        if (!empty($params['keyword'])) {
            $keyword = $params['keyword'];
            $query->where('a.page_name', 'like', "%" . $keyword . "%");
        }
        $totalCount = $query->count();
        $list= $query->skip($skip)
            ->limit($pageSize)
            ->selectRaw($fields)
            ->orderBy('sort')
            ->get();
        if(!empty($list)){
            $list=$list->toArray();
        }else{
            $list=[];
        }
        $result = $this->buildPaginator($list, $skip, $page, $pageSize, $totalCount);
        return $result;
    }
    /**
     * 获取网页菜单
     * @param array $params
     * */
    public function getPageMenuInfo($params=[]) {
        if(!empty($params['route_path'])) {
            $where=[];
            $where[]=['status','<',2];
            $where['route_path']=$params['route_path'];
            $info= $this->newInstance()->where($where)->first();
            if(!empty($info)) {
                $info = $info->toArray();
                if(!empty($info['page_content'])) {
                    $info['page_content'] = json_decode($info['page_content'],true);;
                }
            }
            return $info;
        }
    }

    /**
     * 获取静态页面详情
     * @param array $params
     * */
    public function getStaticPageInfo($params=[]){
        if(empty($params)){
            return [];
        }

        $where=[];
        $where[]=['status','<',2];
        if(!empty($params['id'])){
//            $where['id']=$params['id'];
            $where[] = ['id','=',$params['id']];
        }
        if(!empty($params['route_path'])){
            $where[] = ['route_path','=',$params['route_path']];
//            $where['route_path']=$params['route_path'];
        }
        if(isset($params['type'])){
            $where[] = ['type','=',empty($params['type'])?0:$params['type']];
//            $where['type']=empty($params['type'])?0:$params['type'];
        }

        $info= $this->newInstance()->where($where)->first();
        if(!empty($info)) {
            $info=$info->toArray();
            $info['page_content'] = json_decode($info['page_content'],true);
            //dd($info['page_content']);
            $page_content = [];
            if (!empty($info['page_content'])) {
                foreach ($info['page_content'] as $module_key => $module) {
                    if (!isset($module['module']) && !is_int($module_key) && !empty($module['module_key'])) {
                        $module_content = [$module_key=>[]];
                        $module_content[$module_key] = ['module'=>[]];
                        $module_content[$module_key]['module'] = $module;
                    } elseif (!isset($module['module']) && is_int($module_key)) {
                        $module_content = [$module['key']=>$module];
                    } else {
                        $module_content = [$module_key=>$module];
                    }
                    $page_content = array_merge($page_content,$module_content);
                }
            }
            $info['page_content'] = $page_content;
            //dd($page_content);
            //dd($page_content);
            /*if(!empty($info['page_content'])) {
                $moduleContent = json_decode($info['page_content'],true);
                $info['page_content'] = [];
                if (!empty($params['module_key_list'])) {
                    $module_key_list = $params['module_key_list'];
                    foreach ($module_key_list as $module_key) {
                        if (isset($moduleContent[$module_key])) {
                            $pageContent = mapByKey($moduleContent[$module_key],'key');
                            $info['page_content'][$module_key] = $pageContent;
                        }
                    }
                } else {
                    foreach ($moduleContent as $moduleKey => $moduleItem) {
                        $moduleContent[$moduleKey] = mapByKey($moduleItem,'key');
                    }
                    $info['page_content'] = $moduleContent;
                }
            }*/
            return $info;
        }else{
            return ['page_content'=>[]];
        }
    }

    public function getStaticPageRenderData($params=[]){
        if(empty($params)){
            return [];
        }
        $where=[];
        $where['a.status']=0;
        $where['b.relation_table']=$this->table;
        if(!empty($params['page_name'])){
            $where['a.page_name']=$params['page_name'];
        }
        if(!empty($params['route_path'])){
            $where['a.route_path']=$params['route_path'];
        }
        $fields='a.*,b.urla,b.relation_table,b.relation_id,b.seo_title,b.seo_keyword,b.seo_describe';
        $info= $this->newInstance()->alias('a')
            ->leftJoin('web_seo as b', 'a.seo_id', '=', 'b.id')
            ->where($where)
            ->selectRaw($fields)
            ->first();
        if(!empty($info)){
            $info=$info->toArray();
            if(!empty($info['page_content'])){
                $pageContent=json_decode($info['page_content'],true);
                $pageContent=mapByKey($pageContent,'key');
                $info['page_content']=$pageContent;
            }
            return $info;
        }else{
            return [];
        }
    }

    public function getWebMenu(){
        $where=[];
        $where['a.status']=0;
        $where['b.relation_table']=$this->table;
        if(!empty($params['page_name'])){
            $where['a.page_name']=$params['page_name'];
        }
        if(!empty($params['route_path'])){
            $where['a.route_path']=$params['route_path'];
        }
        $fields='a.*,b.urla,b.relation_table,b.relation_id,b.seo_title,b.seo_keyword,b.seo_describe';
        $info= $this->newInstance()->alias('a')
            ->leftJoin('web_seo as b', 'a.seo_id', '=', 'b.id')
            ->where($where)
            ->selectRaw($fields)
            ->get();
        if(!empty($info)){
            return $info->toArray();
        }else{
            return [];
        }
    }

    public function downloadPageSEOQuery()
    {
        $query1 = DB::table('web_seo as a')
            ->select([
                'b.id',
                'b.page_name AS page_name',
                'a.urla',
                'a.seo_title',
                'a.seo_keyword',
                'a.seo_describe',
                DB::raw("'静态页' as relation_table")
            ])
            ->leftJoin('web_static_page as b', 'a.relation_id', '=', 'b.id')
            ->where('relation_table', 'web_static_page')
            ->where('b.status', 0)
            ->where('a.status', 0);
        $query2 = DB::table('web_seo as a')
            ->select([
                'a.id',
                'b.title AS page_name',
                'a.urla',
                'a.seo_title',
                'a.seo_keyword',
                'a.seo_describe',
                DB::raw("'新闻详情页' as relation_table")
            ])
            ->leftJoin('blog as b', 'a.relation_id', '=', 'b.id')
            ->where('relation_table', 'blog')
            ->where('b.status', 0)
            ->where('a.status', 0);
        $query3 = DB::table('web_seo as a')
            ->select([
                'b.id',
                'b.tag_name AS page_name',
                'a.urla',
                'a.seo_title',
                'a.seo_keyword',
                'a.seo_describe',
                DB::raw("'新闻标签页' as relation_table")
            ])
            ->leftJoin('blog_tag as b', 'a.relation_id', '=', 'b.id')
            ->where('relation_table', 'blog_tag')
            ->where('b.status', 0)
            ->where('a.status', 0);
        $query4 = DB::table('web_seo as a')
            ->select([
                'b.id',
                'b.type_name AS page_name',
                'a.urla',
                'a.seo_title',
                'a.seo_keyword',
                'a.seo_describe',
                DB::raw("'新闻分类页' as relation_table")
            ])
            ->leftJoin('blog_type as b', 'a.relation_id', '=', 'b.id')
            ->where('relation_table', 'blog_type')
            ->where('b.status', 0)
            ->where('a.status', 0);
        $query5 = DB::table('web_seo as a')
            ->select([
                'b.id',
                'b.title AS page_name',
                'a.urla',
                'a.seo_title',
                'a.seo_keyword',
                'a.seo_describe',
                DB::raw("'会议详情页' as relation_table")
            ])
            ->leftJoin('meeting as b', 'a.relation_id', '=', 'b.id')
            ->where('relation_table', 'meeting')
            ->where('b.status', 0)
            ->where('a.status', 0);
        $query6 = DB::table('web_seo as a')
            ->select([
                'b.id',
                'b.type_name AS page_name',
                'a.urla',
                'a.seo_title',
                'a.seo_keyword',
                'a.seo_describe',
                DB::raw("'会议分类页' as relation_table")
            ])
            ->leftJoin('meeting_type as b', 'a.relation_id', '=', 'b.id')
            ->where('relation_table', 'meeting_type')
            ->where('b.status', 0)
            ->where('a.status', 0);
        $query16 = DB::table('web_seo as a')
            ->select([
                'b.id',
                'b.tag_name AS page_name',
                'a.urla',
                'a.seo_title',
                'a.seo_keyword',
                'a.seo_describe',
                DB::raw("'会议标签页' as relation_table")
            ])
            ->leftJoin('meeting_tag as b', 'a.relation_id', '=', 'b.id')
            ->where('relation_table', 'meeting_tag')
            ->where('b.status', 0)
            ->where('a.status', 0);
        $query7 = DB::table('web_seo as a')
            ->select([
                'b.id',
                'b.title AS page_name',
                'a.urla',
                'a.seo_title',
                'a.seo_keyword',
                'a.seo_describe',
                DB::raw("'产品详情页' as relation_table")
            ])
            ->leftJoin('product as b', 'a.relation_id', '=', 'b.id')
            ->where('relation_table', 'product')
            ->where('b.status', 0)
            ->where('a.status', 0);
        $query8 = DB::table('web_seo as a')
            ->select([
                'b.id',
                'b.type_name AS page_name',
                'a.urla',
                'a.seo_title',
                'a.seo_keyword',
                'a.seo_describe',
                DB::raw("'产品分类页' as relation_table")
            ])
            ->leftJoin('product_type as b', 'a.relation_id', '=', 'b.id')
            ->where('relation_table', 'product_type')
            ->where('b.status', 0)
            ->where('a.status', 0);
        $query9 = DB::table('web_seo as a')
            ->select([
                'b.id',
                'b.tag_name AS page_name',
                'a.urla',
                'a.seo_title',
                'a.seo_keyword',
                'a.seo_describe',
                DB::raw("'产品标签页' as relation_table")
            ])
            ->leftJoin('product_tag as b', 'a.relation_id', '=', 'b.id')
            ->where('relation_table', 'product_tag')
            ->where('b.status', 0)
            ->where('a.status', 0);
        $result = $query1
            ->union($query2)
            ->union($query3)
            ->union($query4)
            ->union($query5)
            ->union($query6)
            ->union($query16)
            ->union($query7)
            ->union($query8)
            ->union($query9)
            ->limit(10000)
            ->get();

        return $result;
    }



}
