<?php

namespace app\api\controller;

use app\api\controller\base\Base;
use time\DateHelper;

class Announcement extends Base
{
    private function getModel()
    {
        return new \app\common\model\Announcement();
    }

    public function index()
    {
        $post = $this->request->param();
        $validate = new \think\Validate([
            ['id', 'number'],
            ['page', 'number'],
            ['pagenum', 'number|<=:1000']
        ]);
        if (!$validate->check($post)) {
            $this->json_error('提交失败：' . $validate->getError());
        }

        $where = [];
        if (isset($post['id'])) {
            $where['id'] = $post['id'];
        }

        if (isset($post['id'])) {
            $datalist = ($this->getModel())->where($where)->find();
        } else {
            $pagenum = $this->request->param('pagenum', 20, 'intval');
            $datalist = ($this->getModel())->where($where)->order('id desc')->paginate($pagenum, true);
            foreach ($datalist as $key => $item) {
                $item['create_day'] = DateHelper::dateFormat($item->getData('create_time'), 'Y-m-d');
                $datalist[$key] = $item;
            }
        }
        if (empty($datalist)) {
            $this->json_error("没有数据");
        }
        $this->json_success("查询成功", $datalist);
    }

}