<?php


namespace App\Web\Models;



use App\Models\BaseModel;
use App\Web\Services\WebService;
use Illuminate\Support\Facades\DB;


class WebSeoModel extends BaseModel
{



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


    /**
     * 检查Urla是否唯一
     * */
    public function checkUrlaUnique($urla,$id=0){
        if(empty($urla)){
            return true;
        }else{
            $where=[];
            if(!empty($id)){
                $where[]=['id', '<>', $id];
            }
            $where[]=['status', '<', 2];
            $where[]=['urla', '=', trim($urla)];
            return $this->checkFieldUnique('urla',$where);
        }

    }

    /**
     * Seo优化保存
     * @param array $data
     * */
    public function saveSeoData($data){
        if (!empty($data['id'])) {
            $id = $data['id'];
            $this->newInstance()->where('id', $data['id'])->update($data);
        } else {
            $id = $this->newInstance()->insertGetId($data);
        }
        return $id;
    }


    /**
     * 根据关联关系删除Seo
     * */
    public function delSeoByRelation($params){
        $ret=false;
        if(!empty($params['relation_id'])&&!empty($params['relation_table'])){
            $ret= $this->newInstance()
                ->where('relation_id', $params['relation_id'])
                ->where('relation_table', $params['relation_table'])
                ->update(['status'=>2,'update_time'=>nowTime()]);
        }
       return $ret;
    }

    /**
     * 根际id列表获取数据列表
     * */
    public function getSeoListByIds($ids){
        $list=$this->newInstance()
            ->where('status','=',0)
            ->whereIn('id',$ids)->get();
        if(!empty($list)){
            $list=$list->toArray();
        }else{
            $list=[];
        }
        return $list;
    }

    /**
     * 获取Seo详情
     * @param array $params
     * */
    public function getSeoInfo($params=[]){
        if(empty($params)){
            return [];
        }
        $where=[];
        $where[]=['status','<',2];
        if(!empty($params['id'])){
            $where['id']=$params['id'];
        }
        if(!empty($params['urla'])){
            $where['urla']=$params['urla'];
        }
        $info= $this->newInstance()->where($where)->first();
        if(!empty($info)){
            return $info->toArray();
        }else{
            return [];
        }
    }



    public function getSeoList($params=[],$fields='a.*'){
        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')->where($where);
        if (!empty($params['keyword'])) {
            $keyword = $params['keyword'];
            $query->where('a.seo_title', 'like', "%" . $keyword . "%");
        }
        $totalCount = $query->count();
        $list= $query->skip($skip)
            ->limit($pageSize)
            ->selectRaw($fields)
            ->get();
        if(!empty($list)){
            $list=$list->toArray();
        }else{
            $list=[];
        }
        $result = $this->buildPaginator($list, $skip, $page, $pageSize, $totalCount);
        return $result;
    }

    public function checkUrlaIsExits($urlas)
    {
        $where=[];
        if(!empty($id)){
            $where[]=['id', '<>', $id];
        }
        $where[]=['status', '<', 2];
        $where['urla'] = [['in', $urlas]];
        return $this->newInstance()->buildQuery($where)->selectRaw('id,urla')->get()->toArray();
    }

    public function updateSeoInfo($urla, $newUrla, $seoTitle, $seoKeyword, $seoDes, $relationTable = 'web_static_page')
    {
        $update = [];
        if (!empty($newUrla)) {
            $update['urla'] = $newUrla;
        }
        if (!empty($seoTitle)) {
            $update['seo_title'] = $seoTitle;
        }
        if (!empty($seoKeyword)) {
            $update['seo_keyword'] = $seoKeyword;
        }
        if (!empty($seoDes)) {
            $update['seo_describe'] = $seoDes;
        }
        if(!empty($update)) {
            return $this->newInstance()
                ->where('urla', $urla)
                //->where('relation_table', $relationTable)
                ->update($update);
        }
    }


    public function updateTransStatus($id,$transStatus) {
        $update = [];
        if (!empty($transStatus)) {
            $update['trans_status'] = $transStatus;
        }
        if(!empty($update)) {
            return $this->newInstance()
                ->where('id', $id)
                ->update($update);
        }
        return $id;
    }
}
