<?php
namespace app\index\model;

use think\Model;

class wxusermodel extends Model
{
    protected $table = "t_wxuser";

    public function insertData($data)
    {
        if (!isset($data['create_date']) || empty($data['create_date']) || !is_string($data['create_date'])) {
            $data['create_date'] = 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 = false)
    {
        $defaultfield = ['id', 'open_id', 'union_id', 'nick_name', 'avatar_url', 'gender', 'province', 'city', 'country', 'tel', 'address', 'create_date', 'real_name'];
        $field = $field ? $field : $defaultfield;
        $where = ['id' => $id];
        $info = $this->where($where)->$field()->find();
        return $info;
    }
    /**
     * 根据id修改数据
     * wj
     * 20220118
     */
    public function updatebyid($id, $updateData)
    {
        $where = ['id' => $id];
        $updateData = $this->formatData($updateData);
        if (empty($updateData)) {
            return false;
        }
        $row = $this->where($where)->update($updateData);
        return $row;
    }
    /**
     * 根据openid修改unionid
     *
     * @param  [type] $openid
     * @return void
     * @author wj
     * @date 2022-07-22
     */
    public function updateunionidbyopenid($openid, $unionid)
    {
        $where = ['openid' => $openid];
        $info = $this->where($where)->find();
        if (!empty($info)) {
            $id = $info['id'];
            if ($info['unionid'] != $unionid) {
                $updateData = ['unionid' => $unionid];
                $row = $this->where(['id' => $id])->update($updateData);
                if (empty($row)) {
                    return backarr(0, '用户unionid修改失败');
                }
            }
        }
        return backarr(1, '操作成功');
    }
    /**
     * 根据openid获取数据
     *
     * @param  [type] $openid
     * @return void
     * @author wj
     * @date 2022-07-22
     */
    public function getinfobyopenid($openid, $field = false)
    {
        $defaultfield = ['id', 'open_id', 'union_id', 'nick_name', 'avatar_url', 'gender', 'province', 'city', 'country', 'tel', 'address', 'create_date', 'real_name'];
        $field = $field ? $field : $defaultfield;
        $where = ['openid' => $openid];
        $info = $this->where($where)->field($field)->find();
        return $info ? $info : false;
    }
}
