<?php

    namespace app\controller;

    use app\api\repositories\UsersRepositories;
    use app\business\BankCardBusiness;
    use app\business\LoginBusiness;
    use app\business\UserIdentityBusiness;
    use Illuminate\Support\Arr;
    use Respect\Validation\Validator;
    use support\Db;
    use support\Redis;
    use support\Request;
    use hg\apidoc\annotation as Apidoc;
    use Webman\Captcha\CaptchaBuilder;
    use Webman\Captcha\PhraseBuilder;

    #[Apidoc\Title("实名认证+修改实名信息")]
    #[Apidoc\Group("UserIdentity")]
    #[Apidoc\Sort(5)]
    class UserIdentityController
    {

        #[Apidoc\Title("实名认证+修改实名信息")]
        #[Apidoc\Url("api/user/identity.html")]
        #[Apidoc\Method("POST")]
        #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
        #[Apidoc\Param("name", type: "string", require: true, desc: "姓名", mock: "张三")]
        #[Apidoc\Param("number", type: "string", require: true, desc: "身份证号码 16-18位", mock: 500221233312836451)]
        public function Updata(Request $request)
        {
            $param              = $request->param_data;
            $param              = Arr::only($param, ['name', 'number']);
            $param['user_data'] = $request->user_data;
            Db::beginTransaction();
            try {
                Validator::input($param, [
                    'name'   => Validator::notEmpty()->stringType()->setName('姓名'),
                    'number' => Validator::notEmpty()->stringType()->Length(16, 18)->setName('身份证号码'),
                ]);
                // 示例身份证号，请替换为实际要验证的号码
                if (!isValidChineseIDCard18($param['number'])) {
                    throw new \Exception('身份证号码无效');
                }
                $identity =  Db::table('wa_user_identity')->where('number',$param['number'])->count();
                if($identity>=100){
                    throw new \Exception('当前身份证已实名');
                }
                //判断如果该上级邀请的人数有200没实名，就不允许邀请
                $not_autonym_total = Db::table('wa_users')
                    ->where('pid',$param['user_data']['pid'])
                    ->where('is_autonym',0)->count();
                if ($not_autonym_total >= 200) {
                    throw new \Exception('邀请错误');
                }
                UserIdentityBusiness::UpData($param);
            } catch (\Throwable $exception) {
                Db::rollBack();
                return error($exception->getMessage());
            }
            Db::commit();
            return success();


        }


    }
