<?php

namespace app\common\model;

use think\Model;

class Member extends Model
{
    public $page_info;

    /**
     * 会员列表
     * @access public
     * @author csdeshang
     * @param array $condition 条件
     * @param string $field 字段
     * @param number $page 分页
     * @param string $order 排序
     * @return array
     */
    public function getMemberList($condition = array(), $field = '*', $page = 0, $order = 'member_id desc')
    {
        if ($page) {
            $member_list = db('member')->where($condition)->order($order)->paginate($page, false, ['query' => request()->param()]);
            $this->page_info = $member_list;
            return $member_list->items();
        } else {
            return db('member')->where($condition)->order($order)->select();
        }
    }

    /**
     * 新增用户
     */
    public function addMember($data)
    {
        return db('member')->insertGetId($data);
    }

    /**
     * 编辑用户
     */
    public function editMember($condition, $data)
    {
        return db('member')->where($condition)->update($data);
    }

    /**
     * 删除用户
     */
    public function delMember($condition)
    {
        return db('member')->where($condition)->delete();
    }

    /**
     * 取单个用户
     */
    public function getMemberInfo($condition, $field = '*')
    {
        return db('member')->field($field)->where($condition)->find();
    }

    /**
     * 取得会员详细信息（优先查询缓存）
     * 如果未找到，则缓存所有字段
     * @param int $member_id
     * @param string $field 需要取得的缓存键值, 例如：'*','member_name,member_sex'
     * @return array
     */
    public function getMemberInfoByID($member_id, $fields = '*')
    {
        $member_info = rcache($member_id, 'member', $fields);
        if (empty($member_info)) {
            $member_info = $this->getMemberInfo(array('member_id' => $member_id), '*', true);
            wcache($member_id, $member_info, 'member');
        }
        return $member_info;
    }

    /**
     * 注册
     */
    public function register($register_info)
    {
        // 验证用户名是否重复
        $check_member_name = $this->getMemberInfo(array('member_name' => $register_info['member_name']));
        if (is_array($check_member_name) and count($check_member_name) > 0) {
            return array('error' => '用户名已存在');
        }

        // 会员添加
        $member_info = array();
        $member_info['member_name'] = $register_info['member_name'];
        $member_info['member_password'] = md5($register_info['member_password']);

        if (isset($register_info['member_mobilebind'])) {
            $member_info['member_mobile_bind'] = $register_info['member_mobilebind'];
            $member_info['member_mobile'] = $register_info['member_mobile'];
        }
        $member_id = $this->addMember($member_info);
        $member_res = $this->getMemberInfo(['member_id' => $member_id]);
        if ($member_id) {
            return $member_res;
        } else {
            return false;
        }
    }

    /**
     * 7天内自动登录 v3-b12
     */
    public function auto_login()
    {
        // 自动登录标记 保存7天
        cookie('auto_login', encrypt(session('member_id'), MD5_KEY), 7 * 24 * 60 * 60);
    }

    public function set_avatar_cookie()
    {
        cookie('member_avatar', session('avatar'), 365 * 24 * 60 * 60);
    }

    /**
     * 登录时创建会话SESSION
     *
     * @param array $member_info 会员信息
     */
    public function createSession($member_info = array(), $reg = false)
    {
        if (empty($member_info) || !is_array($member_info)) {
            return;
        }
        session('is_login', '1');
        session('member_id', $member_info['member_id']);
        session('member_name', $member_info['member_name']);
        session('member_email', $member_info['member_email']);

        if (!empty($member_info['member_logintime'])) {
            $update_info = array(
                'member_loginnum' => ($member_info['member_loginnum'] + 1),
                'member_logintime' => TIMESTAMP,
                'member_old_logintime' => $member_info['member_logintime'],
                'member_login_ip' => request()->ip(),
                'member_old_login_ip' => $member_info['member_login_ip']
            );
            $this->editMember(array('member_id' => $member_info['member_id']), $update_info);
        }
    }

    /**
     * 会员登录检查
     *
     */
    public function checkloginMember()
    {
        if (session('is_login') == '1') {
            @header("Location: " . url('Home/Member/index'));
            exit();
        }
    }

    //有效期选择
    public function validity($usetime){
        if($usetime == "一个月"){
            $validitytime = strtotime('+1 month');
        }elseif($usetime == "一季度"){
            $validitytime = strtotime('+3 month');
        }elseif($usetime == "半年"){
            $validitytime = strtotime('+6 month');
        }else{
            $validitytime = strtotime('+12 month');
        }
        return $validitytime;
    }

    //查询所有公司或者未参与的公司
    public function allcompany($gsid){
        $where['member_status'] = 1;
        $where['member_id'] = array('neq',$gsid);
        return db('member')->field('member_name,member_id')->where($where)->select();
    }

    //查询所有客服
    public function allservice(){
        $where['member_group'] = 2;
        $where['member_status'] = 1;
        return db('member')->field('member_id,member_name')->where($where)->select();
    }

    //所属公司 uid--用户id
    public function suoscompany($uid){
        $where['member_id'] = $uid;
        $suosid = db('member')->field('member_id')->where($where)->find();
        $suosgsid = $suosid['member_id'];
        if($suosid['member_id']){
            $he['member_id'] = $suosid['member_id'];
            $gssuos = db('member')->field('member_id')->where($he)->find();
            $suosgsid = $gssuos['member_id'];
        }
        return $suosgsid;
    }

    //所属公司下的所有人员id
    public function allsuoscompany($uid){
        $companyid = $this->suoscompany($uid);
        $where['member_id'] = $uid;
        $allglylist = db('member')->where($where)->column("member_id");
        $allglyid = implode(",",$allglylist);
        return $allglyid.','.$uid;
    }

}