<?php
/**
 * Created by PhpStorm.
 * User: yckj_lzj
 * Date: 2020-05-21
 * Time: 11:28
 */

namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete;

/**
 * 演示模型
 * Class Demo
 * @package app\common\model
 */
class Demo extends Model
{
    use SoftDelete;
//    protected $deleteTime = 'delete_time';
//    protected $defaultSoftDelete = 1;

    /**
     * 获取列表，自带分页功能
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function getList()
    {
        $where=[];
        $keyword=input("title");
        if($keyword){
            $where[]=array("title","like","%".$keyword."%");
        }else{
//            return returnFormat(9999,"请输入关键词");//模拟报错，上线时要注释
        }
        $orderby=input("orderby",$this->getPk()." desc");
        $page_size=input("page_size",10);//分页大小
        $list=self::where($where)->order($orderby)->paginate($page_size);
        return returnFormat(0,'查询成功',$list);
    }

    public function add()
    {

        $param = input('param.');
        $param['update_time'] = $param['create_time'] = getNow();
        $res=self::insert($param);
        if($res===false){
            return returnFormat(9999,'服务器错误，新增失败');
        }
        return returnFormat(0,'添加成功');
    }

    public function detail(){
        $id=input("id");
        if(!$id){
            return returnFormat(9999,'缺少参数');
        }
        $where['id']=$id;
        $model=self::where($where)->find();
        if(!$model){
            return returnFormat(9999,'未找到记录');
        }
        return returnFormat(0,'',$model->toArray());
    }

    public function del(){
        $id=input("id");
        $where["id"]=$id;
        $res=self::where($where)->delete();
        if($res===false){
            return returnFormat(9999,'服务器错误');
        }
        return returnFormat(0,'删除成功');
    }

    public function edit(){
        $id=input("id");
        if(!$id){
            return returnFormat(9999,'缺少参数');
        }
        $where["id"]=$id;
        $model=self::where($where)->find();
        if(!$model){
            return returnFormat(9999,'未找到记录');
        }
        $param=input("param.");
        $param["id"]=$id;
        $res=self::update($param);
        if($res===false){
            return returnFormat(9999,'服务器错误');
        }
        return returnFormat(0,'修改成功');
    }


}