<?php

namespace app\admin\model;

use think\Cache;
use think\Model;
use think\Url;

class Page extends Model {
    protected $autoWriteTimestamp = true;

    protected static function init()
    {
        //编辑后可用
        Page::event('after_update', function () {
            Cache::tag('page_data')->clear();
        });
        //新增后可用
        Page::event('after_insert', function () {
            Cache::tag('page_data')->clear();
        });
        //写入后  貌似没用到
        Page::event('after_write', function () {
            Cache::tag('page_data')->clear();
        });
        //删除后
        Page::event('after_delete', function () {
            Cache::tag('page_data')->clear();
        });
    }

    //获取单页数据库所有数据并分页
    public function adminGetPageAllDataToPage($param){
        $where['p.delete_time']=0;
        $where['c.delete_time']=0;
        $page_size=10;
        if(!empty($param)){
            //添加条件
            //栏目id
            if(isset($param['category_id']) && $param['category_id'] != 0){
                $where['p.category_id']=['eq',$param['category_id']];
            }
            //搜索条件
            if(!empty($param['search'])){
                $where['p.title']=['like',"%".$param['search']."%"];
            }
            isset($param['page_size']) && $param['page_size'] != 0 ? $page_size=(int)$param['page_size'] : '';

        }
        $field=('p.*,c.name as category_name');
        $data=$this
            ->alias('p')
            ->join('category c','p.category_id=c.id')
            ->where($where)
            ->order('id desc')
            ->field($field)
            ->paginate($page_size);
        return $data;
    }

    //通过id获取单条信息
    public function adminFindDataToId($id){
        $data=$this->where(['delete_time'=>0])->find($id);
        if(empty($data)){
            return false;
        }
        return $data->toArray();
    }

    //通过category_id获取单条信息
    public function adminFindDataToCategoryId($category_id){
        $data=$this->where(['delete_time'=>0,'category_id'=>$category_id])->find();
        if(empty($data)){
            return false;
        }
        return $data->toArray();
    }

    //添加
    public function add($params){
        $res=$this->isUpdate(false)->allowField(true)->save($params);
        $id=$this->id;
        Url::root('/');
        $url=url('index/page/index',['id'=>$id]);
        $this->edit(['url'=>$url,'id'=>$id]);
        return  $res;
    }

    //编辑或修改
    public function edit($params){
        Url::root('/');
        $params['url']=url('index/page/index',['id'=>$params['id']]);
        return $this->isUpdate(true)->allowField(true)->save($params);
    }

    //删除单个或多个
    public function del($id){
        if(is_array($id)){
            $ids=implode(',',$id);
        }else{
            $ids=$id;
        }
        $del=$this->destroy(['id'=>['in',$ids]]);
        return $del;

    }

    //批量移动
    public function remove_category($ids,$to_category_id){
        if(is_array($ids)){
            $up_arr=[];
            foreach ($ids as $k=>$v){
                $up_arr[$k]['id']=$v;
                $up_arr[$k]['category_id']=$to_category_id;
            }
            return $this->isUpdate(true)->allowField(true)->saveAll($up_arr);
        }
        return false;
    }
}