<?php
declare (strict_types = 1);
namespace app\system\controller\v1;

use app\BaseController;
use app\Request;
use library\services\UtilService;
use app\model\api\News as NewsModel;

// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | 展示信息管理
// +----------------------------------------------------------------------
// | Date: 2020-09-06 21:53
// +----------------------------------------------------------------------

class News extends  BaseController{
    /**
     * 公告列表
     * @param Request $request
     */
    public function list(Request $request){
        $post = UtilService::getMore([
            ['status', ''],
        ],$request);
        $where=[];
        if(is_numeric($post["status"]) && in_array((string)$post["status"],["0","1"])){
            $where["status"] = $post["status"];
        }
        $data = (new NewsModel)->where($where)->order("time","desc")->select()->toArray();
        foreach($data as $k=>$v){
            $data[$k]["time"] = date("Y-m-d H:i:s",$v["time"]);
        }
        return app("json")->success(["list"=>$data]);
    }
    /**
     * 公告添加
     * @param Request $request
     */
    public function add(Request $request){
        $post = UtilService::getMore([
            ['id', ''],
            ['title', '','empty','请输入标题'],
            ['content', '','empty','请输入内容'],
            ['status', '','is_numeric','参数错误'],
        ],$request);
        $save = [];
        $save["title"]   = $post["title"];
        $save["content"] = $post["content"];
        $save["status"]  = empty($post["status"]) ? 0 : 1;
        $save["time"]    = time();
        if(empty($post["id"])){
            (new NewsModel)->insert($save);
        }else{
            (new NewsModel)->where("id",$post["id"])->update($save);
        }
        return app("json")->success("操作成功");
    }
    /**
     * 公告删除
     * @param Request $request
     */
    public function del(Request $request){
        $post = UtilService::getMore([
            ['id', '','empty','参数错误'],
        ],$request);
        (new NewsModel)->where("id",$post["id"])->delete();
        return app("json")->success("删除成功");
    }
}