<?php
/**
 * @Statement: [ An Ordinary Person ]
 * @Author: 闻子 < QQ：270988107 >
 * @Copyright: 闻子 2016-2019 All rights reserved.
 *
 *    .--,       .--,
 *   ( (  \.---./  ) )
 *    '.__/o   o\__.'
 *       {=  ^  =}
 *        >  -  <
 *       /       \
 *      //       \\
 *     //|   .   |\\
 *     "'\       /'"_.-~^`'-.
 *        \  _  /--'         `
 *      ___)( )(___
 *     (((__) (__)))    高山仰止,景行行止.虽不能至,心向往之.
 *
 */

namespace app\common\model;

use think\Model;

use think\Db;

use think\facade\Cache;

/**
 * 模型数据基类
 * @Author 闻子 <270988107@qq.com>
 */
class BaseModel extends Model
{

    // 查询对象
    private static $ob_query = null;

    public function updateCacheVersion()
    {

    }

    /**
     * 设置数据
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function setInfo($data = [], $where = [], $sequence = null)
    {

        $pk = $this->getPk();

        $return_data = null;

        if (empty($data[$pk])) {

            $return_data = $this->allowField(true)->save($data, $where, $sequence);

            $return_data && $this->updateCacheVersion();

        } else {

            if (empty($where)) {

                $where[$pk] = $data[$pk];

            }

            $return_data = $this->updateInfo($where, $data);

        }

        return $return_data;

    }

    /**
     * 新增数据
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function addInfo($data = [], $is_return_pk = true)
    {

        $data[TIME_CT_NAME] = TIME_NOW;

        $return_data = $this->insert($data, false, $is_return_pk);

        $return_data && $this->updateCacheVersion();

        return $return_data;

    }

    /**
     * 更新数据
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function updateInfo($where = [], $data = [])
    {

        $return_data = $this->allowField(true)->save($data, $where);

        $return_data && $this->updateCacheVersion();

        return $return_data;

    }

    /**
     * 更新数据一
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function updatenosaveInfo($where = [], $data = [])
    {

        $data[TIME_UT_NAME] = TIME_NOW;

        $return_data = $this->allowField(true)->where($where)->update($data);

        $return_data && $this->updateCacheVersion();

        return $return_data;

    }

    /**
     * 更新数据二
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function updateDan($where = [], $data = [])
    {

        $return_data = $this->update($data, $where);

        return $return_data;

    }

    /**
     * 统计数据
     * @Author 闻子 <270988107@qq.com>
     */
    public function stat($where = [], $stat_type = 'count', $field = 'id')
    {

        return $this->where($where)->$stat_type($field);

    }

    /**
     * 设置数据列表
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function setList($data_list = [], $replace = false)
    {

        $return_data = $this->saveAll($data_list, $replace);

        $return_data && $this->updateCacheVersion();

        return $return_data;

    }

    /**
     * 设置某个字段值
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function setFieldValue($where = [], $field = '', $value = '')
    {

        return $this->updateInfo($where, [$field => $value]);

    }

    /**
     * 批量删除数据
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function deleteAllInfo($where = [], $is_true = false)
    {

        if ($is_true) {

            $return_data = $this->where($where)->delete();

            $return_data && $this->updateCacheVersion();
        } else {

            $return_data = $this->setFieldValue($where, DATA_COMMON_STATUS, DATA_DELETE);
        }

        return $return_data;

    }

    /**
     * 删除数据
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function deleteInfo($where = [], $is_true = false)
    {

        if ($is_true) {

            $return_data = $this->where($where)->delete();

            $return_data && $this->updateCacheVersion();

        } else {

            $return_data = $this->setFieldValue($where, DATA_COMMON_STATUS, DATA_DELETE);
        }

        return $return_data;

    }

    /**
     * 删除数据一
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function deleteyi($data = '')
    {

        $return_data = Db::name($this->name)->delete($data);

        return $return_data;

    }

    /**
     * 获取某个列的数组
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function getColumn($where = [], $field = '', $key = '')
    {

        return Db::name($this->name)->where($where)->column($field, $key);

    }

    /**
     * 获取limit列表
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function getLimitData($where = '', $limit = 0)
    {

        return Db::name($this->name)->where($where)->limit($limit)->select();

    }

    /**
     * 获取列表
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function getlistRes($data = '')
    {

        return Db::name($this->name)->select();

    }

    /**
     * 获取排序列表
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function getOrder($data = '')
    {

        return Db::name($this->name)->order($data)->select();

    }

    /**
     * 获取查询排序列表
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function getWhereOrder($where = [], $data = '')
    {

        return Db::name($this->name)->where($where)->order($data)->select();

    }

    /**
     * 获取权限组列表
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function getGroupList($where = [], $field = true, $order = '', $paginate = 0)
    {

        return Db::name($this->name)->where($where)->field($field)->order($order)->paginate($paginate);

    }

    /**
     * 获取判断分页列表
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function getJuegList($where = [], $wheres = [], $paginate = 0)
    {

        return Db::name($this->name)->where($where)->whereOr($wheres)->paginate($paginate);

    }

    /**
     * 获取排序列表一
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function getOrderLimit($where = '', $data = '', $limit = 0)
    {

        return Db::name($this->name)->where($where)->order($data)->limit($limit)->select();

    }

    /**
     * 获取排序分页列表
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function getOrderPaginate($data = '', $paginate = 0)
    {

        return Db::name($this->name)->order($data)->paginate($paginate);

    }

    /**
     * 获取asc排序列表一
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function getAscOrder($where = [], $whereOr = [], $data = '')
    {

        return Db::name($this->name)->where($where)->whereOr($whereOr)->order($data)->select();

    }

    /**
     * 获取某个字段的值
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function getValue($where = [], $field = '', $default = null, $force = false)
    {

        return Db::name($this->name)->where($where)->value($field, $default, $force);

    }

    /**
     * 获取单条数据
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function getInfo($where = [], $field = true, $join = null, $data = null, $alias = 'm')
    {

        self::$ob_query = $this->where($where)->field($field);

        if (!empty($alias) && !empty($join)) {

            self::$ob_query = self::$ob_query->alias($alias);

        }

        !empty($join) && self::$ob_query = self::$ob_query->join($join);

        return $this->getResultData(DATA_DISABLE, $data);

    }

    /**
     * 获取单条排序数据
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function getOrderInfo($where = [], $field = true, $order = '')
    {

        return Db::name($this->name)->where($where)->field($field)->order($order)->find();

    }

    /**
     * 获取单条双查询数据
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function getSInfo($where = [], $whereOr = [], $field = true, $join = null, $data = null, $alias = 'm')
    {

        self::$ob_query = $this->where($where)->whereOr($whereOr)->field($field);

        if (!empty($alias) && !empty($join)) {

            self::$ob_query = self::$ob_query->alias($alias);

        }

        !empty($join) && self::$ob_query = self::$ob_query->join($join);

        return $this->getResultData(DATA_DISABLE, $data);

    }

    /**
     * 获取列表数据
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function getJoin($where = [], $join = null, $data = null, $alias = 'm')
    {

        self::$ob_query = $this->where($where)->select();

        if (!empty($alias) && !empty($join)) {

            self::$ob_query = self::$ob_query->alias($alias);

        }

        !empty($join) && self::$ob_query = self::$ob_query->join($join);

        return $this->getResultData(DATA_DISABLE, $data);

    }

    /**
     * 获取分页数据
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function getPaginate($field = true, $paginate = 0)
    {

        return Db::name($this->name)->field($field)->paginate($paginate);

    }

    /**
     * 随机获取单条数据
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function getrandomInfo($field = true, $limit = 0, $order = '')
    {

        return Db::name($this->name)->field($field)->limit($limit)->orderRaw($order)->find();

    }

    /**
     * 获取判断分页数据
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function getWherePaginate($where = [], $field = true, $paginate = 0)
    {

        return Db::name($this->name)->where($where)->field($field)->paginate($paginate);

    }

    /**
     * 获取判断分页数据一
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function getPaginateRes($where = '', $whereOr = '', $field = true, $paginate = 0)
    {

        return Db::name($this->name)->where($where)->whereOr($whereOr)->field($field)->paginate($paginate);

    }

    /**
     * 获取判断分页数据二
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function getFieldRes($where = '', $field = '', $paginate = 0)
    {

        return Db::name($this->name)->where($where)->field($field)->paginate($paginate);

    }

    /**
     * 获取列表数据
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function getList($where = [], $whereOr = [], $field = true, $order = '', $paginate = 0, $join = [], $group = '', $limit = null, $data = null, $alias = 'm')
    {

        empty($join) && !isset($where[DATA_COMMON_STATUS]) && $where[DATA_COMMON_STATUS] = ['neq', DATA_DELETE];

        self::$ob_query = $this->where($where)->whereOr($whereOr)->order($order);

        self::$ob_query = self::$ob_query->field($field);

        if (!empty($alias) && !empty($join)) {

            self::$ob_query = self::$ob_query->alias($alias);

        }

        !empty($join) && self::$ob_query = self::$ob_query->join($join);

        !empty($group) && self::$ob_query = self::$ob_query->group($group);

        !empty($limit) && self::$ob_query = self::$ob_query->limit($limit);

        if (DATA_DISABLE === $paginate) {

            if (empty(config('list_rows'))) {

                $paginate = DB_LIST_ROWS;

            } else {

                $paginate = config('list_rows');

            }
        }

        return $this->getResultData($paginate, $data);

    }

    /**
     * 获取结果数据
     * @Author 闻子 <270988107@qq.com>
     */
    final protected function getResultData($paginate = 0, $data = null)
    {

        $result_data = null;

        $backtrace = debug_backtrace(false, 2);

        array_shift($backtrace);

        $function = $backtrace[0]['function'];

        if ($function == 'getList') {

            $result_data = false !== $paginate ? self::$ob_query->paginate($paginate) : self::$ob_query->select($data);

        } else {

            $result_data = self::$ob_query->find($data);
        }

        self::$ob_query->removeOption();

        return $result_data;

    }
}