<?php

/*
 * $Author ：PHPYUN开发团队
 *
 * 官网: http://www.phpyun.com
 *
 * 版权所有 2009-2019 宿迁鑫潮信息技术有限公司，并保留所有权利。
 *
 * 软件声明：未经授权前提下，不得用于商业运营、二次开发以及任何形式的再次发布。
 */
class transfer_model extends model
{
    /**
     * 账号分离，目前只有个人用户可以账号分离
     */
    public function setTransfer($uid, $data = array())
    {
        $uid = intval($uid);
        
        if ($uid && $data['oldpass'] && $data['username'] && $data['password']) {
            
            $data['username']   =   str_replace('！', '!', $data['username']);
            
            $msg        =   regUserNameComplex($data['username']);

            $pmsg       =   regPassWordComplex($data['password']);

            if ($msg!='') {
                
                $return['msg'] = $msg;
            } elseif (CheckRegUser($data['username']) == false && CheckRegEmail($data['username']) == false) {
                
                $return['msg'] = '新用户名不得包含特殊字符！';
            } else {
                
                $usernameNum = $this->select_num('member', array("username" => $data['username']));
                
                if ($usernameNum > 0) {
                    
                    $return['msg'] = '用户名已存在，请重新输入！';
                } elseif ($pmsg!='') {
                    $return['msg'] = $pmsg;
                }
            }
            
            if (! $return['msg']) {
                
                // 开始验证密码
                $mfiled = "`uid`,`username`,`password`,`salt`,`email`,`moblie`,`email_status`,`moblie_status`,`wxid`,`wxopenid`,`unionid`,`qqid`,`unionid`,`sinaid`";
                
                $memberInfo = $this -> select_once('member',array('uid' => $uid), $mfiled);
                
                if (!empty($memberInfo)){
                    
                    $res = passCheck($data['oldpass'], $memberInfo['salt'], $memberInfo['password']);
                    
                    if ($res) {
                        
                        // 判断是否需要解绑数据
                        if ($data['bdtype']) {
                            
                            $jbType = $data['bdtype'];
                            // 如果选择了解绑数据 那么之前数据迁移到对应的新账户名下 邮箱 QQ 微博 微信同理
                            foreach ($jbType as $key => $value) {
                                
                                if ($value == 'moblie') {
                                    
                                    $companyData['moblie_status'] = '0';
                                    $companyData['linktel'] = '';
                                    
                                    $lxData['moblie_status'] = '0';
                                    $lxData['moblie'] = '';
                                    
                                    $memberData['moblie'] = '';
                                    $memberData['moblie_status'] = '0';
                                    
                                    $adata['moblie'] = $memberInfo['moblie'];
                                    $adata['moblie_status'] = $memberInfo['moblie_status'];
                                } elseif ($value == 'email') {
                                    
                                    $companyData['email_status'] = '0';
                                    $companyData['linkmail'] = '';
                                    
                                    $lxData['email_status'] = '0';
                                    $lxData['email'] = '';
                                    
                                    $memberData['email'] = '';
                                    $memberData['email_status'] = '0';
                                    
                                    $adata['email'] = $memberInfo['email'];
                                    $adata['email_status'] = $memberInfo['email_status'];
                                } elseif ($value == 'qqid') {
                                    
                                    $memberData['qqid'] = '';
                                    $memberData['qqunionid'] = '';
                                    
                                    $adata['qqid'] = $memberInfo['qqid'];
                                    $adata['qqunionid'] = $memberInfo['qqunionid'];
                                } elseif ($value == 'wxid') {
                                    
                                    $memberData['wxid'] = '';
                                    $memberData['wxopenid'] = '';
                                    $memberData['unionid'] = '';
                                    
                                    $adata['wxid'] = $memberInfo['wxid'];
                                    $adata['wxopenid'] = $memberInfo['wxopenid'];
                                    $adata['unionid'] = $memberInfo['unionid'];
                                } elseif ($value == 'sinaid') {
                                    
                                    $memberData['sinaid'] = '';
                                    
                                    $adata['sinaid'] = $memberInfo['sinaid'];
                                }
                            }
                            $this->update_once('company', $companyData, array('uid' => $uid));
                        }
                        // 账号分离后，将原账号身份转换成其他身份，优先企业身份（不转换，原账号登录后还是个人身份，会造成误解）
                        $oldcom  =  $this->select_once('company', array('uid'=>$uid), '`uid`');
                        if (!empty($oldcom)){
                            $memberData['usertype']  =  2;
                        }
                        if (!empty($memberData)){
                            $this->update_once('member', $memberData, array('uid' => $uid));
                        }
                        // 如果未解绑邮箱 手机数据 则需要清空新账户个人信息表中对应数据
                        if (! $jbType) {
                            $newResume['email'] = '';
                            $newResume['email_status'] = '0';
                            $newResume['telphone'] = '';
                            $newResume['moblie_status'] = '0';
                        } else {
                            if (! in_array('email', $jbType)) {
                                $newResume['email'] = '';
                                $newResume['email_status'] = '0';
                            }
                            if (! in_array('moblie', $jbType)) {
                                $newResume['telphone'] = '';
                                $newResume['moblie_status'] = '0';
                            }
                        }
                        if ($newResume) {
                            $this->update_once('resume', $newResume, array('uid' => $uid));
                        }
                        
                        // 创建新账户
                        $salt = substr(uniqid(rand()), - 6);
                        $pass = passCheck($data['password'], $salt);
                        
                        $adata['username'] = $data['username'];
                        $adata['password'] = $pass;
                        
                        $adata['did'] = 0;
                        $adata['status'] = 1;
                        $adata['salt'] = $salt;
                        $adata['source'] = 21;
                        $adata['reg_date'] = time();
                        $adata['reg_ip'] = $memberInfo['reg_ip'];
                        $adata['usertype'] = 1;
                        
                        $userid = $this->insert_into('member', $adata);

                        // 容错机制 防止因为某些情况下 未返回新增ID
                        if (! $userid) {
                            $user_id = $this -> select_once('member',array("username" => $data['username']), 'uid');
                            $userid  = $user_id['uid'];
                        }
                        
                        // 将新账户UID 同步更新老数据 达成迁移效果
                        if ($userid) {

                            if(!empty($jbType) && in_array('moblie', $jbType)){
                                $user_moblie = 1;
                            }else{
                                $user_moblie = 2;
                            }

                            $nid = $this->filterTableUid($userid, $uid,$user_moblie);
                            // 增加账号分离会员日志
                            require_once ('log.model.php');
                            $LogM = new log_model($this->db, $this->def);
                            
                            $LogM -> addMemberLog($uid, 1, '账号分离成功，新账号用户名：'.$data['username']);
                            $LogM -> addMemberLog($userid, 1, '账号分离成功，原账号用户名：'.$memberInfo['username']);

                            $return['msg'] = '账户分离成功！';
                            $return['errcode'] = 1;
                        } else {
                            $return['msg'] = '账户分离失败，请重试！';
                        }
                    } else {
                        
                        $return['msg'] = '原账户密码错误！';
                    }
                } else {
                    
                    $return['msg'] = '未找到原账号！';
                }
            }
        } else {
            $return['msg'] = '请正确填写相关信息！';
        }
        
        $return['errcode'] = $return['errcode'] ? $return['errcode'] : 0;
        
        return $return;
    }

    // 迁移数据UID
    private function filterTableUid($uid, $olduid,$user_moblie = 2)
    {

        // 只需要替换UID
        $tableUidList = array(
            'resume',
            'resume_cert',
            'resume_cityclass',
            'resume_doc',
            'resume_edu',
            'resume_expect',
            'resume_jobclass',
            'resume_other',
            'resume_project',
            'resume_show',
            'resume_skill',
            'resume_training',
            'resume_work',
            'resumeout',
            'user_resume',
            'userid_job',
            'userid_msg',
            'company_msg',
            'down_resume',
            'entrust',
            'fav_job',
            'look_job',
            'look_resume',
            'member_statis',
            'msg',
            'part_apply',
            'part_collect',
            'talent_pool',
            'user_entrust',
            'user_entrust_record'
        );
        
        foreach ($tableUidList as $value) {
            //分离时如果没有选择手机号分离，分离出的个人账号下简历，简历状态改成未审核
            if($value == 'resume_expect' && $user_moblie == 2){
                $this->update_once($value, array('uid' => $uid,'state'=>0), array('uid' => $olduid));
            }else{
                $this->update_once($value, array('uid' => $uid), array('uid' => $olduid));
            }

        }
        // 需要根据usertype进行识别身份
        
        $tableUsertypeList = array(
            'atn',
            'change',
            'company_order',
            'evaluate_leave_message',
            'finder',
            'login_log',
            'member_log',
            'member_reg',
            'member_withdraw',
            'user_log',
            'company_job_rewardlist'
        );
        
        foreach ($tableUsertypeList as $value) {
            
            $this->update_once($value, array('uid' => $uid), array('uid' => $olduid,'usertype' => 1));
        }
        
        // 其他字段特殊的数据表
        $this->update_once('company_job_rewardlog', array('uid' => $uid), array('uid' => $olduid,'utype' => 1));
        
        $this->update_once('chat_log', array('uid' => $uid,'from' => $uid), array('from' => $olduid,'fusertype' => 1));
        
        $this->update_once('blacklist', array('c_uid' => $uid), array('c_uid' => $olduid,'usertype' => 1));
        
        $this->update_once('chat_log', array('to' => $uid), array('to' => $olduid,'tusertype' => 1));
        
        $this->update_once('company_pay', array('com_id' => $uid), array('com_id' => $olduid,'usertype' => 1));
        
        $this->update_once('report', array('p_uid' => $uid), array('p_uid' => $olduid,'usertype' => 1));
        
        $this->update_once('sysmsg', array('f_uid' => $uid), array('f_uid' => $olduid,'usertype' => 1));
        
        $this->update_once('recommend', array('uid' => $uid), array('uid' => $olduid,'rec_type' => 2));
        
        $nid = $this->update_once('subscribe', array('uid' => $uid), array('uid' => $olduid,'type' => 1));
        
        return $nid;
    }
}
?>