<?php

use App\Base\Base;
use App\Exception\custom\LifeAccountException;
use App\Models\auth\mysql\LifeAccount;
use App\Models\auth\mysql\Merchant;
use App\Services\lifeaccount\LifeAccountService;
use Validate\RecruitValidate;
use App\Models\auth\mysql\RecruitAccount;
use App\Exception\custom\DataException;
use App\Models\auth\mysql\EnterpriseAuthRecord;
use Api\PhpServices\JwUser\JwUser;

class RecruitController extends Base
{
    /**
     * 创建招聘个人生活号
     */
    public function create_recruitAction(){
        // 验证参数
        (new RecruitValidate())->validate();

        $params = $this->params;
        $uid    = $params['ownerId'];
        $name = $params['name'];
        if(!isset($uid) || !isset($name)){
            //添加招聘表数据
            $params['lifeAccountId'] = -1; //uid、name为空 不具备创建生活号的条件
            $recruit_id = RecruitAccount::insert($params);
            if (!$recruit_id) {
                throw new DataException(['cus' => 1]);
            }
            $this->success(['result'=>['life_account_id' => -1]]);
        }

        // 查询是否有创建资格
        if(!LifeAccountService::permission($uid)['personal']){
            throw new LifeAccountException(['cus'=>3]);
        }

        try {
            RecruitAccount::beginTransaction();
            //创建未认证的生活号
            $life_account_id = LifeAccountService::unauth_account($uid, $name, 0, 'merchant-c');
            if (!$life_account_id) {
                throw new DataException(['cus' => 1]);
            }
            //添加招聘表数据
            $recruit_id = RecruitAccount::insert($params);
            if (!$recruit_id) {
                throw new DataException(['cus' => 1]);
            }
            RecruitAccount::commit();
        } catch (\Exception $exception) {
            RecruitAccount::rollback();
            throw $exception;
        }
        $this->success(['result'=>['life_account_id' => $life_account_id]]);
    }

    /**
     * 更新招聘个人生活号
     */
    public function update_recruitAction(){
        // 验证参数
        (new RecruitValidate())->scene('ent')->validate();
        $params = $this->params;
        $uid    = $params['ownerId'];

        $res = RecruitAccount::update($params, ['ownerId' => $uid]);
        if ($res === false) {
            throw new DataException(['cus' => 1]);
        }

        $this->success();
    }

    /**
     * 查看招聘个人生活号
     */
    public function get_recruitAction(){
        // 验证参数
        (new RecruitValidate())->scene('ent')->validate();

        $params = $this->params;
        $uid    = $params['ownerId'];

        $recruit_ret = RecruitAccount::get('*', ['ownerId' => $uid]);
        if (empty($recruit_ret)) {
            throw new DataException(['cus' => 0]);
        }
        foreach($recruit_ret as $key => $val){
            if($val === 'NULL'){
                unset($recruit_ret[$key]);
            }
        }
        $this->success(['result'=>$recruit_ret]);
    }

    /**
     * 检测未绑定招聘个人生活号,并创建个人未认证生活号 并绑定
     */
    public function check_recruitAction(){
        $recruit_ret = RecruitAccount::select('*', ['lifeAccountId ' => 0]);
        if (empty($recruit_ret)) {
            throw new DataException(['cus' => 0]);
        }
        foreach($recruit_ret as $key => $val){
            //获取简网用户信息
            $jwUser = new JwUser();
            $user_info =  $jwUser->getUserByUserId(['user_id' => $val['ownerId']]);
            $avatar = $user_info['data']['avatar'] ?? 'http://qn.jwshq.cn/profile_unknown_2019_1';
            // 查询是否有创建资格
            if(!LifeAccountService::permission($val['ownerId'])['personal']){
                throw new LifeAccountException(['cus'=>3]);
            }
            try {
                RecruitAccount::beginTransaction();
                //查询是否有重复的生活号名称，有重复的加一个随机数
                $lifeInfo = LifeAccount::getRecord(['life_account_name' => $val['name']]);
                if(!empty($lifeInfo)){
                    $rand_num = mt_rand(0,99);
                    $val['name'] = $val['name'].$rand_num;
                }
                //创建未认证的生活号
                $life_account_id = LifeAccountService::unauth_account($val['ownerId'], $val['name'], 0,'batch', $avatar);
                if (!$life_account_id) {
                    throw new DataException(['cus' => 1]);
                }
                //更新招聘表数据生活号ID
                $res = RecruitAccount::update(['lifeAccountId' => $life_account_id], ['ownerId' => $val['ownerId']]);
                if ($res === false) {
                    throw new DataException(['cus' => 1]);
                }
                RecruitAccount::commit();
            } catch (\Exception $exception) {
                RecruitAccount::rollback();
                throw $exception;
            }
            sleep(1);
        }


        $this->success(['result'=>['life_account_id' => $life_account_id]]);
    }

}