<?php

namespace App\Models;

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

class Base extends Model
{
    protected $table = '';
    public $allCount = 0;

    /**
     * 日期序列化
     * @param \DateTimeInterface $date
     * @return string
     * @author zbj
     * @date 2023/4/13
     */
    protected function serializeDate(\DateTimeInterface $date): string
    {
        return $date->format('Y-m-d H:i:s');
    }

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

    /**
     * @param array:$condition
     * @name :获取单条数据详情
     * @return mixed
     * @author :liyuhang
     * @method get
     */
    public function read($condition,$files = ['*'])
    {

        $info = DB::table($this->table)->select($files)->where($condition)->first();
        return (array)$info;
    }

    /**
     * @name :新增
     * @return void
     * @author :liyuhang
     * @method post
     */
    public function add($data){
        return DB::table($this->table)->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 DB::table($this->table)->where($condition)->update($data);
    }

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