<?php
namespace app\common\listener\manystore;
use app\common\model\dyqc\ManystoreShop;
use app\common\model\school\classes\ClassesLib;
use app\common\model\school\Message;
use app\common\model\school\MessageConfig;
use app\common\model\User;
use app\manystore\model\Manystore;

class ShopHook
{


    // 机构账号创建成功后（审核之前）
    public function shopCreateAfter(&$params)
    {
        ["shop"=>$shop] = $params;


        //机构初始化数据
        ManystoreShop::initData($shop->id);


    }


        // 机构账号更新成功后（审核之前）
    public function shopUpdateAfter(&$params)
    {
        ["shop"=>$shop] = $params;

        //同步所有机构地址
       $classesLibs = \app\common\model\school\classes\ClassesLib::where("shop_id",$shop["id"])->where("address_type","1")->select();
       foreach ($classesLibs as $classesLib){
            $classesLib["address_city"] = $shop["address_city"];
            $classesLib["province"] = $shop["province"];
            $classesLib["city"] = $shop["city"];
            $classesLib["district"] = $shop["district"];
            $classesLib["longitude"] = $shop["longitude"];
            $classesLib["latitude"] = $shop["latitude"];
            $classesLib["address"] = $shop["address"];
            $classesLib["address_detail"] = $shop["address_detail"];
            $classesLib->save();
  \app\common\model\school\classes\ClassesLib::update_classes($classesLib["id"]);
       }


        $activitys = \app\common\model\school\classes\activity\Activity::where("shop_id",$shop["id"])->where("address_type","1")->select();
        foreach ($activitys as $activity){
            $activity["address_city"] = $shop["address_city"];
            $activity["province"] = $shop["province"];
            $activity["city"] = $shop["city"];
            $activity["district"] = $shop["district"];
            $activity["longitude"] = $shop["longitude"];
            $activity["latitude"] = $shop["latitude"];
            $activity["address"] = $shop["address"];
            $activity["address_detail"] = $shop["address_detail"];
            $activity->save();
            \app\common\model\school\classes\activity\Activity::update_classes($activity["id"]);
        }

    }




    // 机构账号提交审核申请后
    public function shopApplyAfter(&$params)
    {
        ["shop"=>$shop] = $params;

        $user = User::where("id" ,$shop["user_id"])->find();
        //记录订单日志

        $desc = "您申请的认证{$shop['name']}已提交审核，审核时间为1-3日内,请耐心等待审核结果";

        $title = "入驻申请提交";
        $mini_type = "shop_apply";
        $to_type="user";
        $to_id = $shop["user_id"];
        $status ="system";
        $platform="user";
        $oper_id=0;
        $oper_type="system";
        $params=[
            "event"=>"shop_create_after",
            "shop_id"=>$shop["id"],
            "desc"=>$shop["desc"],
        ];




        $param = [
            "name" => $shop['name'],
            "nickname" => $user["nickname"],
            "realname" => $user["realname"],
            "mobile" => $user["mobile"],
            "desc"=>$shop['desc'],
            "address"=>$shop["address"]."(".$shop["address_detail"].")",
        ];

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

        //机构初始化数据
        ManystoreShop::initData($shop->id);


    }



    // 机构账号审核成功后
    public function shopAuthSuccessAfter(&$params)
    {
        ['shop' => $shop,"password"=>$password] = $params;
        $user = $shop->user;
        $manystore = Manystore::where("shop_id",$shop["id"])->find();
        $mobile = $user['mobile'] ?? "";
        $shop_backend_url = config("site.shop_backend_url");
        $shop_api_backend_url = config("site.shop_api_backend_url");

        $title = "入驻申请通过";
        $mini_type = "shop_apply";
        $to_type="user";
        $to_id = $shop["user_id"];
        $status ="system";
        $platform="user";
        $oper_id=0;
        $oper_type="system";
        $params=[
             "event"=>"shop_auth_success_after",
             "shop_id"=>$shop["id"],
             "desc"=>$shop["desc"],
             "username"=>$manystore["username"],
             "mobile"=>$mobile,
             "password"=>$password,
        ];





        $param = [
            "name" => $shop['name'],
            "nickname" => $user["nickname"],
            "realname" => $user["realname"],
            "mobile" => $user["mobile"],
            "desc"=>$shop['desc'],
            "address"=>$shop["address"]."(".$shop["address_detail"].")",
            "shop_backend_url" => $shop_backend_url,
            "shop_api_backend_url" => $shop_api_backend_url,
            "username"=>$manystore["username"],
            "password"=>$password,
        ];

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


    }

    // 机构账号审核失败后
    public function shopAuthFailAfter(&$params)
    {
        ["shop"=>$shop] = $params;
        $user = User::where("id" ,$shop["user_id"])->find();

        //记录订单日志

        $title = "入驻申请失败";
        $mini_type = "shop_apply";
        $to_type="user";
        $to_id = $shop["user_id"];
        $status ="system";
        $platform="user";
        $oper_id=0;
        $oper_type="system";
        $params=[
            "event"=>"shop_auth_fail_after",
            "shop_id"=>$shop["id"],
            "desc"=>$shop["desc"],
        ];


        $param = [
            "name" => $shop['name'],
            "nickname" => $user["nickname"],
            "realname" => $user["realname"],
            "mobile" => $user["mobile"],
            "desc"=>$shop['desc'],
            "address"=>$shop["address"]."(".$shop["address_detail"].")",
            "reason" => $shop['reason'],
        ];

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



    }


}