<?php
namespace app\app\model;

use think\Model;

/**
 * 管理员用户
 *
 * @author wj
 * @date 2022-07-25
 */
class webusermodel extends Model
{
    protected $table = "t_webuser";
    public function insertData($data)
    {
        if (!isset($data['oprdate']) || empty($data['oprdate']) || !is_string($data['oprdate'])) {
            $data['oprdate'] = date("Y-m-d H:i:s");
        }
        if (isset($data['id'])) {
            unset($data['id']);
        }
        $data = $this->formatData($data);
        if (empty($data)) {
            return false;
        }
        $id = $this->insertGetId($data);
        return $id ? $id : false;
    }

    /**
     * 校验入库数据
     * 20220119
     */
    private function formatData($data)
    {
        $useData = [];
        $fields = $this->getTableFields();
        foreach ($data as $key => $value) {
            if (in_array($key, $fields)) {
                $useData[$key] = $value;
            }
        }
        return $useData;
    }
    public function getInfo($where, $field = "*", $row = true)
    {
        $info = $this->field($field)->where($where);
        if ($row) {
            $info = $info->find();
        } else {
            $info = $info->select();
        }
        return empty($info) ? false : $info;
    }

    public function updateinfo($where, $updateData)
    {
        $row = $this->where($where)->update($updateData);
        return empty($row) ? false : $row;
    }

    public function deleteinfo($where)
    {
        $row = $this->where($where)->delete();
        return empty($row) ? false : $row;
    }

    public function getList($where = [], $field = "*", $page = 1, $size = 10, $order = "id desc", $group = "", $row = false)
    {
        $sqlObj = $this->where($where);
        if ("count" != $field) {
            $sqlObj = $sqlObj->field($field)->order($order)->group($group)->page($page, $size);
            if ($row) {
                $data = $sqlObj->find();
            } else {
                $data = $sqlObj->select();
            }
        } else {
            $data = $sqlObj = $sqlObj->count();
        }
        return $data;
    }

    /**
     * 根据id获取信息
     * wj
     * 20220118
     */
    public function getinfobyid($id, $field = "*")
    {
        $where = ['id' => $id];
        $info = $this->where($where)->field($field)->find();
        return $info;
    }
    /**
     * 根据telno获取信息
     * wj
     * 20220118
     */
    public function getinfobytelno($telno)
    {
        $where = ['telno' => $telno, 'type' => 0];
        $info = $this->where($where)->find();
        return $info ? $info : false;
    }
    /**
     * 根据telno获取信息
     * wj
     * 20220118
     */
    public function getinfobytoken($token)
    {
        $where = ['token' => $token];
        $info = $this->where($where)->find();
        return $info ? $info : false;
    }
    /**
     * 重置用户token
     *
     * @param  [type] $wid
     * @param  [type] $telno
     * @return void
     * @author wj
     * @date 2022-07-25
     */
    public function settoken($wid, $telno)
    {
        $token = md5(md5($telno) . time());
        $where = ['id' => $wid];
        $updateData = ['token' => $token];
        $row = $this->where($where)->update($updateData);
        return $row ? $row : false;
    }
}
