<?php
declare (strict_types = 1);

namespace app\model\api;

use library\basic\BaseModel;
use think\Model;

/**
 * @mixin \think\Model
 */
class Advert extends BaseModel
{
    /**
     * 获取列表数据
     * @param $page
     * @param $where
     * @param $pageCount
     * @param $desc
     */
    public function getList($page,$where = [],$pageCount = 20,$filed = '*',$desc = ''){
        $data =  $this
            ->field("p.*,ap.cate_name,ap.json")
            ->alias("p")
            ->join("adver_page ap",'ap.id=p.page_id')
            ->when(!empty($where),function ($query) use($where){
                if(!empty($where['page_id'])) {
                    $query->where('p.page_id',$where['page_id']);
                }
                if(!empty($where['title'])) {
                    $query->wherelike('p.title','%' . $where['title'] . '%');
                }

                if(isset($where['sassid'])) {
                    $query->where('p.sassid',$where['sassid']);
                }
            })
            ->order($desc)
            ->paginate(['list_rows'=>$pageCount,'page'=>$page])
            ->toArray();
        foreach ($data['data'] as $k=>$v) {
            $data['data'][$k]['time'] = date('Y-m-d',$v['time']);
            $data['data'][$k]['jsonAr'] =  json_decode($v['data'],true);
        }
        //echo $this->getLastSql();
        return [$data['total'],$data['data']];
    }
    /**
     * 保存分类
     * @param $post
     * @return bool
     */
    public function savePage($post){
        if(!empty($post['id'])) {
            $this->where('id',$post['id'])->save($post);
            return true;
        } else {
            unset($post['id']);
            $post['time'] = time();
            $bool = $this->insert($post);
            return $bool;
        }
    }
}
