rcube_platform_users.php 2.1 KB
<?php


/**
 * 接入平台的用户
 * @author:dc
 * @time 2022/7/22 14:10
 * Class rcube_platform_users
 */
class rcube_platform_users{

    private $db;

    /**
     * 表
     * @var string
     */
    public $table;

    /**
     * rcube_platform constructor.
     * @param null $db
     */
    public function __construct($db = null)
    {

        $this->db   =   $db ? $db : rcube::get_instance()->get_dbh();

        $this->table    =   $this->db->table_name('platform_users',true);
    }


    public function firstByPlatformUserId($platform_user_id)
    {
        // 查询数据
        $result   =   $this->db->query("select * from ". $this->table ." where `platform_user_id` = ?",$platform_user_id);

        return $this->db->fetch_assoc($result);
    }


    public function firstById($id){
        // 查询数据
        $result   =   $this->db->query("select * from ". $this->table ." where `id` = ?",$id);

        return $this->db->fetch_assoc($result);
    }


    /**
     * 创建用户,平台用户
     * @param $platform_id
     * @param $user_id
     * @param $platform_user_id
     * @author:dc
     * @time 2022/7/23 14:21
     */
    public function create($platform_id,$user_id,$platform_user_id){
        // 存在,就直接返回
        $result   =   $this->db->query("select `id`,`platform_id`,`user_id`,`platform_user_id` from {$this->table} where `platform_id` = ? and `user_id` = ? and `platform_user_id` = ? limit 1",$platform_id,$user_id,$platform_user_id);
        $row = $this->db->fetch_assoc($result);
        if($row){
            return $row;
        }
        // 使用参数绑定一样要按照?所排列的字段来,不然会混乱
        // 不存在,则新增
        $insert =   $this->db->query("insert into {$this->table} set `platform_id` = ? , `user_id` = ? , `platform_user_id` = ? , `created_at` = ?",$platform_id,$user_id,$platform_user_id,date('Y-m-d H:i:s'));

        if ($insert && $this->db->affected_rows($insert) && ($id = $this->db->insert_id('platform_users'))) {
            return $this->firstById($id);
        }else{
            return false;
        }

    }







}