<?php
namespace app\common\listener\user;
use app\common\model\school\classes\Verification;
use app\common\model\school\Message;
use app\common\model\school\MessageConfig;
use app\common\model\User;
use app\manystore\model\Manystore;

class UserHook
{


    //发起用户授权
        public function userAuthNeedAfter(&$params)
    {
        ['user_auth' => $user_auth,"oper_type"=>$oper_type,"oper_id"=>$oper_id] = $params;
          $shop = $user_auth->shop;
          $user = User::where("id",$user_auth["user_id"])->find();
//        //课程推送给老师
        $desc = "机构{$shop['name']}已发起授权申请，请确实是否同意！";

        $title = "机构授权申请待确认";
        $mini_type = "user_auth";
        $to_type="user";
        $to_id = $user_auth["user_id"];
        $status ="system";
        $platform="user";
        $oper_id=0;
        $oper_type="system";
        $params=[
            "event"=>"user_auth_need_after",
            "shop_id"=>$user_auth["shop_id"],
        ];

            $param = [
                "name" => $shop['name'],
                "realname"=> $user["realname"] ,
                "nickname"=> $user["nickname"] ,
                "mobile"=> $user["mobile"] ,
            ];

            //发给用户
            (new MessageConfig)
                ->setTemplate($params["event"])
                ->setTemplateData($param)
                ->setToUid($to_id)
                ->setMessageStatus($status)
                ->setMessageMiniType($mini_type)
                ->setMessageParams($params)
                ->sendMessage();

    }


    // 用户授权机构通过
    public function userAuthSuccessAfter(&$params)
    {
        ['user_auth' => $user_auth,"oper_type"=>$oper_type,"oper_id"=>$oper_id] = $params;
        $shop = $user_auth->shop;
        $user = User::where("id",$user_auth["user_id"])->find();
//        //课程推送给老师
        $desc = "您已同意机构{$shop['name']}发起的授权申请。";

        $title = "机构授权已确认";
        $mini_type = "user_auth";
        $to_type="user";
        $to_id = $user_auth["user_id"];
        $status ="system";
        $platform="user";
        $oper_id=0;
        $oper_type="system";
        $params=[
            "event"=>"user_auth_success_after",
            "shop_id"=>$user_auth["shop_id"],
        ];
        $param = [
            "name" => $shop['name'],
            "realname"=> $user["realname"] ,
            "nickname"=> $user["nickname"] ,
            "mobile"=> $user["mobile"] ,
        ];

        //发给用户
        (new MessageConfig)
            ->setTemplate($params["event"])
            ->setTemplateData($param)
            ->setToUid($to_id)
            ->setMessageStatus($status)
            ->setMessageMiniType($mini_type)
            ->setMessageParams($params)
            ->sendMessage();

        $manystore = Manystore::where("shop_id",$user_auth["shop_id"])->find();
        if($manystore){
//            //加入核销员
//          $verification  = Verification::where("user_id",$user_auth["user_id"])
//            ->where("shop_id",$user_auth["shop_id"])
//            ->where("manystore_id",$manystore["id"])->find();
//          if(!$verification){
//              Verification::create([
//                  "user_id"=>$user_auth["user_id"],
//                  "shop_id"=>$user_auth["shop_id"],
//                  "manystore_id"=>$manystore["id"],
//                  "status"=>'2',
//                  "classes_lib_ids"=>"",
//              ]);
//          }
        }

    }

    // 用户授权机构不通过
    public function userAuthFailAfter(&$params)
    {
        ['user_auth' => $user_auth,"oper_type"=>$oper_type,"oper_id"=>$oper_id] = $params;

        $shop = $user_auth->shop;
        $user = User::where("id",$user_auth["user_id"])->find();
//        //课程推送给老师
        $desc = "您已拒绝机构{$shop['name']}发起的授权申请。";

        $title = "机构授权已拒绝";
        $mini_type = "user_auth";
        $to_type="user";
        $to_id = $user_auth["user_id"];
        $status ="system";
        $platform="user";
        $oper_id=0;
        $oper_type="system";
        $params=[
            "event"=>"user_auth_fail_after",
            "shop_id"=>$user_auth["shop_id"],
        ];

        $param = [
            "name" => $shop['name'],
            "realname"=> $user["realname"] ,
            "nickname"=> $user["nickname"] ,
            "mobile"=> $user["mobile"] ,
        ];

        //发给用户
        (new MessageConfig)
            ->setTemplate($params["event"])
            ->setTemplateData($param)
            ->setToUid($to_id)
            ->setMessageStatus($status)
            ->setMessageMiniType($mini_type)
            ->setMessageParams($params)
            ->sendMessage();
    }

    public function userUpdateAfter(&$params)
    {
        //设置用户更新信息
        ['user' => $user] = $params;
        //如果存在机构端，则同时同步机构端账号信息
        $manystore = Manystore::where("user_id",$user["id"])->find();
        if($manystore){
            $manystore["nickname"] =  $user["realname"] ?:$user["nickname"];
            if($user["avatar"])$manystore["avatar"] = $user["avatar"];
            $manystore->save();
        }
    }

}