<?php
namespace app\common\listener\classesorder;
use app\common\model\school\classes\Evaluate;
use app\common\model\school\Message;
use app\common\model\school\MessageConfig;

class OrderHook
{
    // 订单创建成功前（订单计算）

    public function classesOrderCreateBefore(&$params)
    {
           ['user_id'=>$user_id, 'classes_lib_id'=>$classes_lib_id,'param'=>$param, 'order_no'=>$order_no, 'price_info'=>$price_info] = $params;
    }


    // 订单创建成功后
    public function classesOrderCreateAfter(&$params)
    {
        ["order"=>$order] = $params;
        $detail = $order->detail;
        $user = $order->user;
        //记录订单日志
        $mini_type = "order_notice";
        $to_id = $order["user_id"];
        $status ="classes";
        $params=[
            "event"=>"classes_order_create_after",
            "order_id"=>$order["id"],
            "order_no"=>$order["order_no"],
            "classes_lib_id"=>$order["classes_lib_id"],
        ];
        $param = [
            "title"=>$detail["title"],
            "order_no" => $order["order_no"],
            "nickname" => $user["nickname"],
            "realname" => $user["realname"],
            "mobile" => $user["mobile"],
            "price" => $order["totalprice"],
        ];

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

    }



    // 订单支付成功后
    public function classesOrderPayedAfter(&$params)
    {
        ["order"=>$order] = $params;
        $detail = $order->detail;
        //记录订单日志
        $user = $order->user;
        $mini_type = "order_notice";
        $to_id = $order["user_id"];
        $status ="classes";
        $params=[
            "event"=>"classes_order_payed_after",
            "order_id"=>$order["id"],
            "order_no"=>$order["order_no"],
            "classes_lib_id"=>$order["classes_lib_id"],
        ];

        $param = [
            "title"=>$detail["title"],
            "order_no" => $order["order_no"],
            "nickname" => $user["nickname"],
            "realname" => $user["realname"],
            "mobile" => $user["mobile"],
            "price" => $order["payprice"],
        ];

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


    }

    // 订单取消成功后
    public function classesOrderCancelAfter(&$params)
    {
        ['order' => $order,"user_id"=>$user_id,"oper_type"=>$oper_type,"oper_id"=>$oper_id] = $params;

        $detail = $order->detail;
        $user = $order->user;
        if(!$user_id ||$order["user_id"] !=$user_id ){
            $desc = "[系统操作]";
        }else{
            $desc = "";
        }


        $mini_type = "order_notice";

        $to_id = $order["user_id"];
        $status ="classes";

        $params=[
            "event"=>"classes_order_cancel_after",
            "order_id"=>$order["id"],
            "order_no"=>$order["order_no"],
        ];

        $param = [
            "system"=> $desc,
            "title"=> $detail["title"],
            "order_no" => $order["order_no"],
            "nickname" => $user["nickname"],
            "realname" => $user["realname"],
            "mobile" => $user["mobile"],
            "price" => $order["payprice"],
        ];

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


    }

    // 订单完成后
    public function classesOrderFinishAfter(&$params)
    {
        ["order"=>$order] = $params;
        $detail = $order->detail;
        $user = $order->user;

        //记录订单日志

        $desc = "";

        $title = "课程完结撒花!";
        $mini_type = "order_notice";
        $to_type="user";
        $to_id = $order["user_id"];
        $status ="classes";
        $platform="user";
        $oper_id=0;
        $oper_type="system";
        $params=[
            "event"=>"classes_order_finish_after",
            "order_id"=>$order["id"],
            "order_no"=>$order["order_no"],
            "classes_lib_id"=>$order["classes_lib_id"],
        ];


        $param = [
            "system"=> $desc,
            "title"=> $detail["title"],
            "order_no" => $order["order_no"],
            "nickname" => $user["nickname"],
            "realname" => $user["realname"],
            "mobile" => $user["mobile"],
            "price" => $order["payprice"],
        ];

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

    }



    // 订单评价成功后
    public function classesOrderEvaluateAfter(&$params)
    {
        ['order' => $order,"classes_evaluate"=>$classes_evaluate,"user_id"=>$user_id,"oper_type"=>$oper_type,"oper_id"=>$oper_id] = $params;
        $user = $order->user;
        $detail = $order->detail;
        if(!$user_id ||$order["user_id"] !=$user_id ){
            $desc = "[系统操作]";
        }else{
            $desc = "";
        }

        $title = "课程订单已反馈";
        $mini_type = "order_notice";
        $to_type="user";
        $to_id = $order["user_id"];
        $status ="classes";
        $platform="user";
        $oper_id=0;
        $oper_type="system";
        $params=[
            "event"=>"classes_order_evaluate_after",
            "order_id"=>$order["id"],
            "order_no"=>$order["order_no"],
        ];


        $param = [
            "system"=> $desc,
            "title"=> $detail["title"],
            "order_no" => $order["order_no"],
            "nickname" => $user["nickname"],
            "realname" => $user["realname"],
            "mobile" => $user["mobile"],
            "price" => $order["payprice"],
        ];
        //发给用户
        (new MessageConfig)
            ->setTemplate($params["event"]."_user")
            ->setTemplateData($param)
            ->setToUid($to_id)
            ->setMessageStatus($status)
            ->setMessageMiniType($mini_type)
            ->setMessageParams($params)
            ->sendMessage();

        //发给老师
        (new MessageConfig)
            ->setTemplate($params["event"]."_teacher")
            ->setTemplateData($param)
            ->setToUid($detail["user_id"])
            ->setMessageStatus($status)
            ->setMessageMiniType($mini_type)
            ->setMessageParams($params)
            ->sendMessage();

    }




    // 订单评价更新后
    public function classesOrderEvaluateUpdateAfter(&$params)
    {
        ['order' => $order,"classes_evaluate"=>$classes_evaluate,"user_id"=>$user_id,"oper_type"=>$oper_type,"oper_id"=>$oper_id] = $params;
        $user = $order->user;
        $detail = $order->detail;
        if(!$user_id ||$order["user_id"] !=$user_id ){
            $desc = "[系统操作]";
        }else{
            $desc = "";
        }

        $title = "课程订单已修改反馈";
        $mini_type = "order_notice";
        $to_type="user";
        $to_id = $order["user_id"];
        $status ="classes";
        $platform="user";
        $oper_id=0;
        $oper_type="system";
        $params=[
            "event"=>"classes_order_evaluate_update_after",
            "order_id"=>$order["id"],
            "order_no"=>$order["order_no"],
        ];



        $param = [
            "system"=> $desc,
            "title"=> $detail["title"],
            "order_no" => $order["order_no"],
            "nickname" => $user["nickname"],
            "realname" => $user["realname"],
            "mobile" => $user["mobile"],
            "price" => $order["payprice"],
        ];
        //发给用户
        (new MessageConfig)
            ->setTemplate($params["event"]."_user")
            ->setTemplateData($param)
            ->setToUid($to_id)
            ->setMessageStatus($status)
            ->setMessageMiniType($mini_type)
            ->setMessageParams($params)
            ->sendMessage();

        //发给老师
        (new MessageConfig)
            ->setTemplate($params["event"]."_teacher")
            ->setTemplateData($param)
            ->setToUid($detail["user_id"])
            ->setMessageStatus($status)
            ->setMessageMiniType($mini_type)
            ->setMessageParams($params)
            ->sendMessage();


    }






}