<?php

namespace App\Ad\Models;

use App\Models\BaseModel;

class SysAdModel extends BaseModel
{
    protected $table = 'sys_ad';

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

    /**
     * 获取广告列表
     * */
    public function getAdList($params){
        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('sys_ad_position as b', 'a.position_id', '=', 'b.id')
            ->where($where);
        $totalCount = $query->count();
        $list= $query->skip($skip)
            ->limit($pageSize)
            ->selectRaw('a.*,b.number,b.route_path_key,b.ad_key')
            ->orderBy('id')
            ->get();
        if(!empty($list)){
            $list=$list->toArray();
        }else{
            $list=[];
        }
        $result = $this->buildPaginator($list, $skip, $page, $pageSize, $totalCount);
        return $result;
    }
}
