<?php
namespace app\mobile\controller;
use think\facade\Session;
use think\facade\Cache;
use app\common\variable\MsgCode;
use app\common\controller\Resume as ResumeController;
use app\common\controller\Company as CompanyController;
use app\common\controller\CompanyAttestation as CompanyAttestationController;
use app\common\controller\JobsCollection as JobsCollectionController;
use app\common\controller\CompanyCollection as CompanyCollectionController;
use app\common\controller\User as UserController;
use app\common\controller\UserScore as UserScoreController;
use app\common\controller\Score as ScoreController;
use app\common\model\User as UserModel;
use app\common\model\UserInfo as UserInfoModel;
use app\common\model\Company as CompanyModel;
use app\common\model\CompanyDemeanor as CompanyDemeanorModel;
use app\common\model\StatusAlias as StatusAliasModel;
use app\common\model\CompanyAttestation as CompanyAttestationModel;
class User extends Base
{
    /**
     * index 个人中心
     */
    public function index()
    {
        $User=Session::get('User');
        $UserInfo=UserModel::getAppUserInfo(['user.id'=>$User['id']]);
        return $this->ajaxReturn('',MsgCode::$success,$UserInfo);
    }
    public function saveUser()
    {
        $User=Session::get('User');
        $D=request()->post();
        $D['uid']=$User['id'];
        return UserController::saveUserInfo($D);
    }
    public function editMobile()
    {
        $User=Session::get('User');
        $D=request()->post();
        $D['uid']=$User['id'];
        return UserController::saveMobile($D);
    }
    public function editEmail()
    {
        $User=Session::get('User');
        $D=request()->post();
        $D['uid']=$User['id'];
        return UserController::saveEmail($D);
    }
    public function statistics()
    {
        $User=Session::get('User');
        $D=[
            'role_type' =>  $User['role_type'],
            'company_id'=>  $User['company_id'],
            'resume_id' =>  $User['resume_id'],
            'uid' 		=>  $User['id']
        ];
        /*
        *  调用公共用户中心统计方法
        *  app\common\controller\Resume
        */
        return UserController::statistics($D);
    }
    /**
     * score 我的积分
     */
    public function score()
    {
        $G=request()->get();
        $User=Session::get('User');
        $G['uid']=$User['id'];
        if ($G['type']=='score') {
            return UserScoreController::getList($G);
        }elseif ($G['type']=='score_task') {
            return ScoreController::getScoreTaskList($User['id'],$User['role_type']);
        }
    }
    public function goToPersonal()
    {
        $User=Session::get('User');
        $UserInfoModel=UserInfoModel::where(['uid'=>$User['id']])->find();
        $UserInfoModel->role_type=0;
        if ($UserInfoModel->save()) {
            Cache::set('updateUser:'.$UserInfoModel->uid,true);
            return $this->ajaxReturn('',MsgCode::$success);
        }else{
            return $this->ajaxReturn('切换失败，请刷新重试',MsgCode::$error);
        }
    }
    public function goToCompany()
    {
        $User=Session::get('User');
        $UserInfoModel=UserInfoModel::where(['uid'=>$User['id']])->find();
        $UserInfoModel->role_type=1;
        if ($UserInfoModel->save()) {
            Cache::set('updateUser:'.$UserInfoModel->uid,true);
            return $this->ajaxReturn('',MsgCode::$success);
        }else{
            return $this->ajaxReturn('切换失败，请刷新重试',MsgCode::$error);
        }
    }
    public function follow()
    {
        $User=Session::get('User');
        $G=request()->get();
        if ($G['status']=='follow_jobs') {
            $JobsCollection=JobsCollectionController::getPersonalJobsCollection($User['resume_id'],$User['id']);
            if($JobsCollection->items()){
                return $this->ajaxReturn('',MsgCode::$success,$JobsCollection);
            }else{
                return $this->ajaxReturn('',MsgCode::$error);
            }
        }elseif($G['status']=='follow_company'){
            $CompanyCollection=CompanyCollectionController::getPersonalCompanyCollection($User['resume_id'],$User['id']);
            if($CompanyCollection->items()){
                return $this->ajaxReturn('',MsgCode::$success,$CompanyCollection);
            }else{
                return $this->ajaxReturn('',MsgCode::$error);
            }
        }
    }
    /**
     * attestation 用户认证
     */
    public function attestation()
    {
        $User=Session::get('User');
        if ($User['role_type']){
            if (request()->isPost()) {
                $D=request()->post();
                $D['uid']=$User['id'];
                $D['id_type']=1;
                return UserController::saveRealNameApply($D);
            }
            $Find=UserController::getRealNameApply(['uid'=>$User['id'],'id_type'=>1]);
            $Data=[
                'attestation_ms'    =>  config('xy_company.attestation_ms')
            ];
            if($Find){
                $Data['Find']=$Find;
                return $this->ajaxReturn('',MsgCode::$success,$Data);
            }else{
                return $this->ajaxReturn('',MsgCode::$error,$Data);
            }
        }else{
            if (request()->isPost()) {
                $D=request()->post();
                $D['uid']=$User['id'];
                $D['id_type']=0;
                return UserController::saveRealNameApply($D);
            }
            $Find=UserController::getRealNameApply(['uid'=>$User['id'],'id_type'=>0]);
            if($Find){
                return $this->ajaxReturn('',MsgCode::$success,$Find);
            }else{
                return $this->ajaxReturn('',MsgCode::$error);
            }
        }
    }
    public function whoSawMe()
    {
        $User=Session::get('User');
        $CompanyController=CompanyController::getCompanyBrowseList($User['company_id']);
        return $this->ajaxReturn('',MsgCode::$success,$CompanyController);
    }
    public function getBlacklist()
    {
        $User=Session::get('User');
        if($User['role_type']){
            return UserController::getCompanyBlacklist($User['id']);
        }else{
            return UserController::getResumeBlacklist($User['id']);
        }
    }
    public function setBlacklist()
    {
        $User=Session::get('User');
        $D=request()->post();
        $Data=[
            'uid'   =>  $User['id'],
            'list'  =>  $D['list']
        ];
        if($User['role_type']){
            return UserController::setCompanyBlacklist($Data);
        }else{
            return UserController::setResumeBlacklist($Data);
        }
    }
    public function cancelBlacklist($id=0)
    {
        $User=Session::get('User');
        if(request()->isPost()){
            $D=[
                'uid'       =>  $User['id'],
                'to_uid'    =>  request()->param('touid')
            ];
            if($User['role_type']){
                return UserController::cancelCompanyBlacklist($D);
            }else{
                return UserController::cancelResumeBlacklist($D);
            }
        }else{
            $D=[
                'uid'   =>  $User['id'],
                'id'    =>  $id
            ];
            if($User['role_type']){
                return UserController::cancelCompanyBlacklist($D);
            }else{
                return UserController::cancelResumeBlacklist($D);
            }
        }
    }




    /**
     * account 账户管理
     */
    public function account()
    {
        $User=Session::get('User');
        if ($User['role_type']){
            $CompanyModel=CompanyModel::where(['uid'=>$User['id']])->find();
            if ($CompanyModel) {
                $Find=CompanyModel::getEditFind($CompanyModel->id);
                View::assign('Find',$Find);
            }
            return View::fetch('company_account');
        }else{
            return redirect('/user/security');
        }
    }
    /**
     * security 账户安全
     */
    public function security()
    {
        $User=Session::get('User');
        if (request()->isPost()) {
            $D=request()->post();
            $D['uid']=$User['id'];
            return UserController::saveUserInfo($D);
        }
        if ($User['role_type']){
            return View::fetch('company_security');
        }else{
            return View::fetch('personal_security');
        }
    }
    public function getRecommendResume()
    {
        $CompanyModel=CompanyModel::where(['id'=>Session::get('User.company_id')])->find();
        $D=[
            'length'        =>  6
        ];
        if ($CompanyModel){
            $D['industry']   =   $CompanyModel->industry_id;
        }else{
            $D['recommend']  =   1;
        }
        /*
        *  调用公共获取推荐人才方法
        *  app\common\controller\Resume
        */
        return $this->ajaxReturn('',MsgCode::$success,ResumeController::getResumeList($D));
    }
}
