<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Base extends Model
{
    protected $table = '';
    public $allCount = 0;
    /**
     * @name 列表数据
     * @return void
     * @author :liyuhang
     * @method
     */
    public function lists($map, $p, $row, $order = 'id', $fields = ['*']){
        //TODO::where(['id'=>'','name'=>''])
        $lists = $this->select($fields)->where($map)->forPage($p,$row)->orderBy($order)->get();
        if ($lists->isEmpty() !== false) {
            return false;
        }
        $lists = $lists->toArray();
        $this->allCount = $this->where($map)->count();
        return $lists;
    }


    /**
     * @param $map
     * @param $order
     * @param $fields
     * @name  :无分页列表
     * @return mixed
     * @author :liyuhang
     * @method
     */
    public function list($map,$order = 'id',$fields = ['*']){
        $lists = $this->select($fields)->where($map)->orderBy($order)->get();
        if ($lists->isEmpty() !== false) {
            return false;
        }
        $lists = $lists->toArray();
        return $lists;
    }
    /**
     * @param array:$condition
     * @name :获取单条数据详情
     * @return mixed
     * @author :liyuhang
     * @method get
     */
    public function read($condition,$files = ['*'])
    {
        $info = $this->select($files)->where($condition)->first();
        if ($info->isEmpty() !== false) {
            return false;
        }
        $info = $info->toArray();
        return $info;
    }


    /**
     * @name :新增
     * @return void
     * @author :liyuhang
     * @method post
     */
    public function add($data){
        return $this->insert($data);
    }

    /**
     * @name :编辑
     * @return void
     * @author :liyuhang
     * @method post
     */
    public function edit($data,$condition){
        if(isset($data['id']) && !empty($data['id'])){
            unset($data['id']);
        }
        return $this->where($condition)->update($data);
    }

    /**
     * @name : 删除数据
     * @return void
     * @author :liyuhang
     * @method
     */
    public function del($condition){
        return $this->where($condition)->delete();
    }
}