<?php
namespace app\common\listener\classeshourorder;
use app\common\model\school\classes\hourorder\OrderLog;
use app\common\model\school\classes\Teacher;
use app\common\model\school\Message;
use app\common\model\school\MessageConfig;

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

    public function classeshourOrderCreateBefore(&$params)
    {

        ['user_id'=>$user_id, 'classes_order_id'=>$classes_order_id,'param'=>$param, 'order_no'=>$order_no, 'price_info'=>$price_info,'classes_lib_spec_id'=>$classes_lib_spec_id] = $params;


    }


    // 订单创建成功后
    public function classeshourOrderCreateAfter(&$params)
    {
        ["order"=>$order] = $params;
        $detail = $order->detail;
        $user = $order->user;
        //查询老师信息和老师用户信息
        $teacher = Teacher::where("id", $detail["teacher_id"])->find();
        $teacher_user = null;
        if($teacher){
            $teacher_user = $teacher->user;
        }


        //记录订单日志
        if($order["type"] == "1"){
            $desc = "";
        }else{
            $desc = "[机构老师代操作]";
        }
        $title = "创建课时预约成功";
        $mini_type = "classes_auth";
        $to_type="user";
        $to_id = $order["user_id"];
        $status ="order";
        $platform="user";
        $oper_id=0;
        $oper_type="system";
        $params=[
            "event"=>"classeshour_order_create_after",
            "order_id"=>$order["id"],
            "order_no"=>$order["order_no"],
        ];


        $param = [
            "title"=>$detail["title"],
            "name" => $order["name"],
            "order_no" => $order["order_no"],
            "nickname" => $user["nickname"],
            "realname" => $user["realname"],
            "mobile" => $user["mobile"],
//            "price" => $order["totalprice"],
            "system"=>$desc,
            "teacher_name"=> $teacher["name"] ?? "课程负责人",
            "teacher_realname"=> $teacher_user["realname"] ?? "课程负责人真实姓名",
            "teacher_nickname"=> $teacher_user["nickname"] ?? "课程负责人昵称",
            "teacher_mobile"=> $teacher_user["mobile"] ?? "课程负责人电话",
            "start_time" => date("Y-m-d H:i",$order["start_time"]), //格式化日期格式 $order["start_time"], //格式化日期格式
            "end_time" =>   date("Y-m-d H:i",$order["end_time"]),
            "address"=>$detail["address"]."(".$detail["address_detail"].")",
        ];

        //发给用户
        (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 classeshourOrderUpdateAfter(&$params)
    {
        ['order' => $order,"user_id"=>$user_id,"oper_type"=>$oper_type,"oper_id"=>$oper_id] = $params;
        $detail = $order->detail;
        $user = $order->user;
        //查询老师信息和老师用户信息
        $teacher = Teacher::where("id", $detail["teacher_id"])->find();
        $teacher_user = null;
        if($teacher){
            $teacher_user = $teacher->user;
        }
        //插入订单取消日志
        if(!$user_id ||$order["user_id"] !=$user_id ){
            $desc = "[机构老师代操作]";
        }else{
            $desc = "";
        }

        $title = "课时预约单更换课时";
        $mini_type = "classes_order_notice";
        $to_type="user";
        $to_id = $order["user_id"];
        $status ="order";
        $platform="user";
        $oper_id=0;
        $oper_type="system";
        $params=[
            "event"=>"classeshour_order_update_after",
            "order_id"=>$order["id"],
            "order_no"=>$order["order_no"],
        ];


        $param = [
            "title"=>$detail["title"],
            "name" => $order["name"],
            "order_no" => $order["order_no"],
            "nickname" => $user["nickname"],
            "realname" => $user["realname"],
            "mobile" => $user["mobile"],
//            "price" => $order["totalprice"],
            "system"=>$desc,
            "teacher_name"=> $teacher["name"] ?? "课程负责人",
            "teacher_realname"=> $teacher_user["realname"] ?? "课程负责人真实姓名",
            "teacher_nickname"=> $teacher_user["nickname"] ?? "课程负责人昵称",
            "teacher_mobile"=> $teacher_user["mobile"] ?? "课程负责人电话",
            "start_time" => date("Y-m-d H:i",$order["start_time"]), //格式化日期格式 $order["start_time"], //格式化日期格式
            "end_time" =>   date("Y-m-d H:i",$order["end_time"]),
            "address"=>$detail["address"]."(".$detail["address_detail"].")",
        ];

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


    }




    // 订单审核通过后
    public function classeshourOrderAuthSuccessAfter(&$params)
    {
        ['order' => $order,"user_id"=>$user_id,"oper_type"=>$oper_type,"oper_id"=>$oper_id] = $params;
        $detail = $order->detail;
        $user = $order->user;
        //查询老师信息和老师用户信息
        $teacher = Teacher::where("id", $detail["teacher_id"])->find();
        $teacher_user = null;
        if($teacher){
            $teacher_user = $teacher->user;
        }
        //插入订单取消日志
        if(!$user_id ||$order["user_id"] !=$user_id ){
            $pron = "[机构老师代操作]";
        }else{
            $pron = "";
        }
        $desc = $pron;

        $title = "课时预约单审核通过";
        $mini_type = "classes_auth";
        $to_type="user";
        $to_id = $order["user_id"];
        $status ="order";
        $platform="user";
        $oper_id=0;
        $oper_type="system";
        $params=[
            "event"=>"classeshour_order_auth_success_after",
            "order_id"=>$order["id"],
            "order_no"=>$order["order_no"],
        ];



        $param = [
            "title"=>$detail["title"],
            "name" => $order["name"],
            "order_no" => $order["order_no"],
            "nickname" => $user["nickname"],
            "realname" => $user["realname"],
            "mobile" => $user["mobile"],
//            "price" => $order["totalprice"],
            "system"=>$desc,
            "teacher_name"=> $teacher["name"] ?? "课程负责人",
            "teacher_realname"=> $teacher_user["realname"] ?? "课程负责人真实姓名",
            "teacher_nickname"=> $teacher_user["nickname"] ?? "课程负责人昵称",
            "teacher_mobile"=> $teacher_user["mobile"] ?? "课程负责人电话",
            "start_time" => date("Y-m-d H:i",$order["start_time"]), //格式化日期格式 $order["start_time"], //格式化日期格式
            "end_time" =>   date("Y-m-d H:i",$order["end_time"]),
            "address"=>$detail["address"]."(".$detail["address_detail"].")",
        ];

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

    }

    // 订单审核失败后
    public function classeshourOrderAuthFailAfter(&$params)
    {
        ['order' => $order,"user_id"=>$user_id,"oper_type"=>$oper_type,"oper_id"=>$oper_id] = $params;
        $detail = $order->detail;
        $user = $order->user;
        //查询老师信息和老师用户信息
        $teacher = Teacher::where("id", $detail["teacher_id"])->find();
        $teacher_user = null;
        if($teacher){
            $teacher_user = $teacher->user;
        }
        //插入订单取消日志
        if(!$user_id ||$order["user_id"] !=$user_id ){
            $pron = "[机构老师代操作]";
        }else{
            $pron = "";
        }
        $desc = $pron;

        $title = "课时预约单审核不通过";
        $mini_type = "classes_auth";
        $to_type="user";
        $to_id = $order["user_id"];
        $status ="order";
        $platform="user";
        $oper_id=0;
        $oper_type="system";
        $params=[
            "event"=>"classeshour_order_auth_fail_after",
            "order_id"=>$order["id"],
            "order_no"=>$order["order_no"],
            "reason"=>$order['reason']
        ];



        $param = [
            "title"=>$detail["title"],
            "name" => $order["name"],
            "order_no" => $order["order_no"],
            "nickname" => $user["nickname"],
            "realname" => $user["realname"],
            "mobile" => $user["mobile"],
            "reason" => $order["reason"],
            "system"=>$desc,
            "teacher_name"=> $teacher["name"] ?? "课程负责人",
            "teacher_realname"=> $teacher_user["realname"] ?? "课程负责人真实姓名",
            "teacher_nickname"=> $teacher_user["nickname"] ?? "课程负责人昵称",
            "teacher_mobile"=> $teacher_user["mobile"] ?? "课程负责人电话",
            "start_time" => date("Y-m-d H:i",$order["start_time"]), //格式化日期格式 $order["start_time"], //格式化日期格式
            "end_time" =>   date("Y-m-d H:i",$order["end_time"]),
            "address"=>$detail["address"]."(".$detail["address_detail"].")",
        ];

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

    }

    // 订单核销完成后
    public function classeshourOrderFinishAfter(&$params)
    {
        ['order' => $order,"user_id"=>$user_id,"oper_type"=>$oper_type,"oper_id"=>$oper_id] = $params;
        $detail = $order->detail;
        $user = $order->user;
        //查询老师信息和老师用户信息
        $teacher = Teacher::where("id", $detail["teacher_id"])->find();
        $teacher_user = null;
        if($teacher){
            $teacher_user = $teacher->user;
        }

        if(!$user_id ||$order["user_id"] !=$user_id ){
            $desc = "[机构老师代操作]";
        }else{
            $desc = "";
        }

        $title = "课时预约单已核销";
        $mini_type = "classes_order_notice";
        $to_type="user";
        $to_id = $order["user_id"];
        $status ="order";
        $platform="user";
        $oper_id=0;
        $oper_type="system";
        $params=[
            "event"=>"classeshour_order_finish_after",
            "order_id"=>$order["id"],
            "order_no"=>$order["order_no"],
        ];




        $param = [
            "title"=>$detail["title"],
            "name" => $order["name"],
            "order_no" => $order["order_no"],
            "nickname" => $user["nickname"],
            "realname" => $user["realname"],
            "mobile" => $user["mobile"],
//            "reason" => $order["reason"],
            "system"=>$desc,
            "teacher_name"=> $teacher["name"] ?? "课程负责人",
            "teacher_realname"=> $teacher_user["realname"] ?? "课程负责人真实姓名",
            "teacher_nickname"=> $teacher_user["nickname"] ?? "课程负责人昵称",
            "teacher_mobile"=> $teacher_user["mobile"] ?? "课程负责人电话",
            "start_time" => date("Y-m-d H:i",$order["start_time"]), //格式化日期格式 $order["start_time"], //格式化日期格式
            "end_time" =>   date("Y-m-d H:i",$order["end_time"]),
            "address"=>$detail["address"]."(".$detail["address_detail"].")",
        ];

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

    }




    // 订单取消成功后
    public function classeshourOrderCancelAfter(&$params)
    {
        ['order' => $order,"user_id"=>$user_id,"oper_type"=>$oper_type,"oper_id"=>$oper_id] = $params;
        $detail = $order->detail;
        $user = $order->user;
        //查询老师信息和老师用户信息
        $teacher = Teacher::where("id", $detail["teacher_id"])->find();
        $teacher_user = null;
        if($teacher){
            $teacher_user = $teacher->user;
        }
        if(!$user_id ||$order["user_id"] !=$user_id ){
            $desc = "[机构老师代操作]";
        }else{
            $desc = "";
        }

        $title = "课时预约单已取消";
        $mini_type = "classes_order_notice";
        $to_type="user";
        $to_id = $order["user_id"];
        $status ="order";
        $platform="user";
        $oper_id=0;
        $oper_type="system";
        $params=[
            "event"=>"classeshour_order_cancel_after",
            "order_id"=>$order["id"],
            "order_no"=>$order["order_no"],
        ];


        $param = [
            "title"=>$detail["title"],
            "name" => $order["name"],
            "order_no" => $order["order_no"],
            "nickname" => $user["nickname"],
            "realname" => $user["realname"],
            "mobile" => $user["mobile"],
//            "reason" => $order["reason"],
            "system"=>$desc,
            "teacher_name"=> $teacher["name"] ?? "课程负责人",
            "teacher_realname"=> $teacher_user["realname"] ?? "课程负责人真实姓名",
            "teacher_nickname"=> $teacher_user["nickname"] ?? "课程负责人昵称",
            "teacher_mobile"=> $teacher_user["mobile"] ?? "课程负责人电话",
            "start_time" => date("Y-m-d H:i",$order["start_time"]), //格式化日期格式 $order["start_time"], //格式化日期格式
            "end_time" =>   date("Y-m-d H:i",$order["end_time"]),
            "address"=>$detail["address"]."(".$detail["address_detail"].")",
        ];

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

    }


}