<?php
declare (strict_types=1);

namespace library\basic;
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
// | Date: 2020-08-29 16:20
// +----------------------------------------------------------------------

use Closure;
use think\facade\Db;
use think\Model;

class BaseModel extends Model
{
    private static $errorMsg;

    private static $transaction = 0;

    private static $DbInstance = [];

    const DEFAULT_ERROR_MSG = '操作失败,请稍候再试!';


    /**
     * 保存 | 添加 数据
     * @param $post
     * @return bool
     */
    public function saveModel($post)
    {
        $int = 0;
        $id  = $this->getPk();
        if (!empty($post[$id])) {
            $int = self::where($id, $post[$id])->save($post);
        } else {
            $int = self::insert($post);
        }
        return true;
    }

    /**
     * 获取列表数据
     * @param $page
     * @param $where
     * @param $pageCount
     * @param $desc
     */
    public function getList($page, $where = [], $pageCount = 20, $filed = ['*', ""], $desc = '')
    {
        $that = $this;
        if (!empty($filed[1])) {
            $that = $this->alias($filed[1]);
        }
        $that = $that->field($filed[0])
            ->when(!empty($where), function ($query) use ($where) {
                foreach ($where as $k => $v) {
                    if ($v instanceof Closure) {
                        $v($query);
                    } else {
                        if (is_array($v)) {
                            //whereLike
                            if ($v[1] == 'whereLike') {
                                if (!empty($v[0]))
                                    $query->whereLike($k, $v[0]);
                                continue;
                            }
                            if ($v[1] == 'whereBetween') {
                                if (!empty($v[0])) $query->whereBetween($k, $v[0]);
                                continue;
                            }


                            $bool = false;
                            eval('$bool = ' . $v[1] . '($v[0]);');
                            if ($bool) {
                                $query->where($k, $v[0]);
                            }
                        } else {
                            $query->where($k, $v);
                        }
                    }
                }
            });
        if ($pageCount > 0) {
            $data = $that->order($desc)
                ->paginate(['list_rows' => $pageCount, 'page' => $page])
                ->toArray();
        } else {
            $odata         = $that->order($desc)
                ->select();
            $data['total'] = count($odata);
            $data['data']  = $data['total'] > 0 ? $odata->toArray() : [];
        }
        //  echo $this->getLastSql();
        return [$data['total'], $data['data']];
    }

    /**
     * 设置错误信息
     * @param string $errorMsg
     * @return bool
     */
    protected static function setErrorInfo($errorMsg = self::DEFAULT_ERROR_MSG, $rollback = false)
    {
        if ($rollback) self::rollbackTrans();
        self::$errorMsg = $errorMsg;
        return false;
    }

    /**
     * 获取错误信息
     * @param string $defaultMsg
     * @return string
     */
    public static function getErrorInfo($defaultMsg = self::DEFAULT_ERROR_MSG)
    {
        return !empty(self::$errorMsg) ? self::$errorMsg : $defaultMsg;
    }

    /**
     * 开启事务
     */
    public static function beginTrans()
    {
        Db::startTrans();
    }

    /**
     * 提交事务
     */
    public static function commitTrans()
    {
        Db::commit();
    }

    /**
     * 关闭事务
     */
    public static function rollbackTrans()
    {
        Db::rollback();
    }

    /**
     * 根据结果提交滚回事务
     * @param $res
     */
    public static function checkTrans($res)
    {
        if ($res) {
            self::commitTrans();
        } else {
            self::rollbackTrans();
        }
    }

}