<?php
namespace app\common\listener\activityorder;
use app\common\model\manystore\Shop;
use app\common\model\school\classes\activity\order\Order;
use app\common\model\school\classes\activity\order\OrderItem;
use app\common\model\school\classes\Evaluate;
use app\common\model\school\classes\Verification;
use app\common\model\school\Message;
use app\common\model\school\MessageConfig;

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

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


    // 订单创建成功后
    public function classesActivityOrderCreateAfter(&$params)
    {
        ["order"=>$order] = $params;

        $order = Order::where("id" , $order["id"])->find();

        $detail = $order->detail;
        $user = $order->user;
        $order_item = OrderItem::where("classes_activity_order_id",$order["id"])->find();
        $shop = Shop::where("id",$order['shop_id'])->find();
        //记录订单日志
        $mini_type = "activity_order";
        $to_id = $order["user_id"];
        $status ="activity";
        $params=[
            "event"=>"classes_activity_order_create_after",
            "order_id"=>$order["id"],
            "order_no"=>$order["order_no"],
            "classes_activity_id"=>$order["classes_activity_id"],
        ];
        $param = [
            "title"=>$detail["title"],
            "name" => $order_item["name"],
            "shop_name" => $shop['name'],
            "order_no" => $order["order_no"],
            "nickname" => $user["nickname"],
            "realname" => $user["realname"],
            "mobile" => $user["mobile"],
            "price" => $order["totalprice"],
            "payprice" => $order["payprice"],
            "real_refundprice" => $order["real_refundprice"],
            "sub_refundprice" => $order["sub_refundprice"],
            "reason" => $order["reason"],
            "address"=>$detail["address"]."(".$detail["address_detail"].")",
            "start_time" => date("Y-m-d H:i",$detail["start_time"]), //格式化日期格式 $order["start_time"], //格式化日期格式
            "end_time" =>   date("Y-m-d H:i",$detail["end_time"]),
        ];

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


    }



    // 订单支付成功后
    public function classesActivityOrderPayedAfter(&$params)
    {
        ["order"=>$order] = $params;

        $order = Order::where("id" , $order["id"])->find();
        $detail = $order->detail;
        $user = $order->user;
        $order_item = OrderItem::where("classes_activity_order_id",$order["id"])->find();
        $shop = Shop::where("id",$order['shop_id'])->find();
        //记录订单日志
        $mini_type = "activity_order";
        $to_id = $order["user_id"];
        $status ="activity";
        $params=[
            "event"=>"classes_activity_order_payed_after",
            "order_id"=>$order["id"],
            "order_no"=>$order["order_no"],
            "classes_activity_id"=>$order["classes_activity_id"],
        ];
        $param = [
            "title"=>$detail["title"],
            "name" => $order_item["name"],
            "shop_name" => $shop['name'],
            "order_no" => $order["order_no"],
            "nickname" => $user["nickname"],
            "realname" => $user["realname"],
            "mobile" => $user["mobile"],
            "price" => $order["totalprice"],
            "payprice" => $order["payprice"],
            "real_refundprice" => $order["real_refundprice"],
            "sub_refundprice" => $order["sub_refundprice"],
            "reason" => $order["reason"],
            "address"=>$detail["address"]."(".$detail["address_detail"].")",
            "start_time" => date("Y-m-d H:i",$detail["start_time"]), //格式化日期格式 $order["start_time"], //格式化日期格式
            "end_time" =>   date("Y-m-d H:i",$detail["end_time"]),
        ];

        //发用户
        (new MessageConfig)
            ->setTemplate($params["event"]."_user")
            ->setTemplateData($param)
            ->setToUid($to_id)
            ->setMessageStatus($status)
            ->setMessageMiniType($mini_type)
            ->setMessageParams($params)
            ->sendMessage();
        //发所有核销员
       $verifications = Verification::where("shop_id",$order['shop_id'])->select();
        foreach ($verifications as $verification){
            (new MessageConfig)
                ->setTemplate($params["event"]."_verification")
                ->setTemplateData($param)
                ->setToUid($verification["user_id"])
                ->setMessageStatus($status)
                ->setMessageMiniType($mini_type)
                ->setMessageParams($params)
                ->sendMessage();
        }


    }

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


        $order = Order::where("id" , $order["id"])->find();

        $detail = $order->detail;
        $user = $order->user;
        $order_item = OrderItem::where("classes_activity_order_id",$order["id"])->find();
        $shop = Shop::where("id",$order['shop_id'])->find();
        //记录订单日志
        $mini_type = "activity_order";
        $to_id = $order["user_id"];
        $status ="activity";
        $params=[
            "event"=>"classes_activity_order_cancel_after",
            "order_id"=>$order["id"],
            "order_no"=>$order["order_no"],
            "classes_activity_id"=>$order["classes_activity_id"],
        ];
        $param = [
            "title"=>$detail["title"],
            "name" => $order_item["name"],
            "shop_name" => $shop['name'],
            "order_no" => $order["order_no"],
            "nickname" => $user["nickname"],
            "realname" => $user["realname"],
            "mobile" => $user["mobile"],
            "price" => $order["totalprice"],
            "payprice" => $order["payprice"],
            "real_refundprice" => $order["real_refundprice"],
            "sub_refundprice" => $order["sub_refundprice"],
            "reason" => $order["reason"],
            "address"=>$detail["address"]."(".$detail["address_detail"].")",
            "start_time" => date("Y-m-d H:i",$detail["start_time"]), //格式化日期格式 $order["start_time"], //格式化日期格式
            "end_time" =>   date("Y-m-d H:i",$detail["end_time"]),
        ];

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


    }





    // 订单评价成功后
    public function classesActivityOrderEvaluateAfter(&$params)
    {
        ['order' => $order,"classes_evaluate"=>$classes_evaluate,"user_id"=>$user_id,"oper_type"=>$oper_type,"oper_id"=>$oper_id] = $params;


    }




    // 订单评价更新后
    public function classesOrderEvaluateUpdateAfter(&$params)
    {
        ['order' => $order,"classes_evaluate"=>$classes_evaluate,"user_id"=>$user_id,"oper_type"=>$oper_type,"oper_id"=>$oper_id] = $params;


    }





    // 订单审核通过后
    public function classesActivityOrderAuthSuccessAfter(&$params)
    {
        ['order' => $order,"user_id"=>$user_id,"oper_type"=>$oper_type,"oper_id"=>$oper_id] = $params;

        $order = Order::where("id" , $order["id"])->find();
        $detail = $order->detail;
        $user = $order->user;
        $order_item = OrderItem::where("classes_activity_order_id",$order["id"])->find();
        $shop = Shop::where("id",$order['shop_id'])->find();
        //记录订单日志
        $mini_type = "activity_order";
        $to_id = $order["user_id"];
        $status ="activity";
        $params=[
            "event"=>"classes_activity_order_auth_success_after",
            "order_id"=>$order["id"],
            "order_no"=>$order["order_no"],
            "classes_activity_id"=>$order["classes_activity_id"],
        ];
        $param = [
            "title"=>$detail["title"],
            "name" => $order_item["name"],
            "shop_name" => $shop['name'],
            "order_no" => $order["order_no"],
            "nickname" => $user["nickname"],
            "realname" => $user["realname"],
            "mobile" => $user["mobile"],
            "price" => $order["totalprice"],
            "payprice" => $order["payprice"],
            "real_refundprice" => $order["real_refundprice"],
            "sub_refundprice" => $order["sub_refundprice"],
            "reason" => $order["reason"],
            "address"=>$detail["address"]."(".$detail["address_detail"].")",
            "start_time" => date("Y-m-d H:i",$detail["start_time"]), //格式化日期格式 $order["start_time"], //格式化日期格式
            "end_time" =>   date("Y-m-d H:i",$detail["end_time"]),
        ];

        //发用户
        (new MessageConfig)
            ->setTemplate($params["event"]."_user")
            ->setTemplateData($param)
            ->setToUid($to_id)
            ->setMessageStatus($status)
            ->setMessageMiniType($mini_type)
            ->setMessageParams($params)
            ->sendMessage();
        //发所有核销员
        $verifications = Verification::where("shop_id",$order['shop_id'])->select();
        foreach ($verifications as $verification){
            (new MessageConfig)
                ->setTemplate($params["event"]."_verification")
                ->setTemplateData($param)
                ->setToUid($verification["user_id"])
                ->setMessageStatus($status)
                ->setMessageMiniType($mini_type)
                ->setMessageParams($params)
                ->sendMessage();
        }


    }

    // 订单审核失败后
    public function classesActivityOrderAuthFailAfter(&$params)
    {
        ['order' => $order,"user_id"=>$user_id,"oper_type"=>$oper_type,"oper_id"=>$oper_id] = $params;
//        $detail = $order->detail;
        $order = Order::where("id" , $order["id"])->find();
        $detail = $order->detail;
        $user = $order->user;
        $order_item = OrderItem::where("classes_activity_order_id",$order["id"])->find();
        $shop = Shop::where("id",$order['shop_id'])->find();
        //记录订单日志
        $mini_type = "activity_order";
        $to_id = $order["user_id"];
        $status ="activity";
        $params=[
            "event"=>"classes_activity_order_auth_fail_after",
            "order_id"=>$order["id"],
            "order_no"=>$order["order_no"],
            "classes_activity_id"=>$order["classes_activity_id"],
        ];
        $param = [
            "title"=>$detail["title"],
            "name" => $order_item["name"],
            "shop_name" => $shop['name'],
            "order_no" => $order["order_no"],
            "nickname" => $user["nickname"],
            "realname" => $user["realname"],
            "mobile" => $user["mobile"],
            "price" => $order["totalprice"],
            "payprice" => $order["payprice"],
            "real_refundprice" => $order["real_refundprice"],
            "sub_refundprice" => $order["sub_refundprice"],
            "reason" => $order["reason"],
            "address"=>$detail["address"]."(".$detail["address_detail"].")",
            "start_time" => date("Y-m-d H:i",$detail["start_time"]), //格式化日期格式 $order["start_time"], //格式化日期格式
            "end_time" =>   date("Y-m-d H:i",$detail["end_time"]),
        ];

        //发用户
        (new MessageConfig)
            ->setTemplate($params["event"]."_user")
            ->setTemplateData($param)
            ->setToUid($to_id)
            ->setMessageStatus($status)
            ->setMessageMiniType($mini_type)
            ->setMessageParams($params)
            ->sendMessage();
        //发所有核销员
        $verifications = Verification::where("shop_id",$order['shop_id'])->select();
        foreach ($verifications as $verification){
            (new MessageConfig)
                ->setTemplate($params["event"]."_verification")
                ->setTemplateData($param)
                ->setToUid($verification["user_id"])
                ->setMessageStatus($status)
                ->setMessageMiniType($mini_type)
                ->setMessageParams($params)
                ->sendMessage();
        }

    }


    // 订单审核失败退款后
    public function classesActivityOrderRefundSuccessAfter(&$params)
    {
        ['order' => $order,"user_id"=>$user_id,"oper_type"=>$oper_type,"oper_id"=>$oper_id] = $params;
        $order = Order::where("id" , $order["id"])->find();
        $detail = $order->detail;
        $user = $order->user;
        $order_item = OrderItem::where("classes_activity_order_id",$order["id"])->find();
        $shop = Shop::where("id",$order['shop_id'])->find();
        //记录订单日志
        $mini_type = "activity_order";
        $to_id = $order["user_id"];
        $status ="activity";
        $params=[
            "event"=>"classes_activity_order_refund_success_after",
            "order_id"=>$order["id"],
            "order_no"=>$order["order_no"],
            "classes_activity_id"=>$order["classes_activity_id"],
        ];
        $param = [
            "title"=>$detail["title"],
            "name" => $order_item["name"],
            "shop_name" => $shop['name'],
            "order_no" => $order["order_no"],
            "nickname" => $user["nickname"],
            "realname" => $user["realname"],
            "mobile" => $user["mobile"],
            "price" => $order["totalprice"],
            "payprice" => $order["payprice"],
            "real_refundprice" => $order["real_refundprice"],
            "sub_refundprice" => $order["sub_refundprice"],
            "reason" => $order["reason"],
            "address"=>$detail["address"]."(".$detail["address_detail"].")",
            "start_time" => date("Y-m-d H:i",$detail["start_time"]), //格式化日期格式 $order["start_time"], //格式化日期格式
            "end_time" =>   date("Y-m-d H:i",$detail["end_time"]),
        ];

        //发用户
        (new MessageConfig)
            ->setTemplate($params["event"]."_user")
            ->setTemplateData($param)
            ->setToUid($to_id)
            ->setMessageStatus($status)
            ->setMessageMiniType($mini_type)
            ->setMessageParams($params)
            ->sendMessage();
        //发所有核销员
        $verifications = Verification::where("shop_id",$order['shop_id'])->select();
        foreach ($verifications as $verification){
            (new MessageConfig)
                ->setTemplate($params["event"]."_verification")
                ->setTemplateData($param)
                ->setToUid($verification["user_id"])
                ->setMessageStatus($status)
                ->setMessageMiniType($mini_type)
                ->setMessageParams($params)
                ->sendMessage();
        }

    }

    // 订单审核失败退款失败后
    public function classesActivityOrderRefundFailAfter(&$params)
    {
        ['order' => $order,"user_id"=>$user_id,"oper_type"=>$oper_type,"oper_id"=>$oper_id] = $params;
//        $detail = $order->detail;

        $order = Order::where("id" , $order["id"])->find();
        $detail = $order->detail;
        $user = $order->user;
        $order_item = OrderItem::where("classes_activity_order_id",$order["id"])->find();
        $shop = Shop::where("id",$order['shop_id'])->find();
        //记录订单日志
        $mini_type = "activity_order";
        $to_id = $order["user_id"];
        $status ="activity";
        $params=[
            "event"=>"classes_activity_order_refund_fail_after",
            "order_id"=>$order["id"],
            "order_no"=>$order["order_no"],
            "classes_activity_id"=>$order["classes_activity_id"],
        ];
        $param = [
            "title"=>$detail["title"],
            "name" => $order_item["name"],
            "shop_name" => $shop['name'],
            "order_no" => $order["order_no"],
            "nickname" => $user["nickname"],
            "realname" => $user["realname"],
            "mobile" => $user["mobile"],
            "price" => $order["totalprice"],
            "payprice" => $order["payprice"],
            "real_refundprice" => $order["real_refundprice"],
            "sub_refundprice" => $order["sub_refundprice"],
            "reason" => $order["reason"],
            "address"=>$detail["address"]."(".$detail["address_detail"].")",
            "start_time" => date("Y-m-d H:i",$detail["start_time"]), //格式化日期格式 $order["start_time"], //格式化日期格式
            "end_time" =>   date("Y-m-d H:i",$detail["end_time"]),
        ];

        //发用户
        (new MessageConfig)
            ->setTemplate($params["event"]."_user")
            ->setTemplateData($param)
            ->setToUid($to_id)
            ->setMessageStatus($status)
            ->setMessageMiniType($mini_type)
            ->setMessageParams($params)
            ->sendMessage();
        //发所有核销员
        $verifications = Verification::where("shop_id",$order['shop_id'])->select();
        foreach ($verifications as $verification){
            (new MessageConfig)
                ->setTemplate($params["event"]."_verification")
                ->setTemplateData($param)
                ->setToUid($verification["user_id"])
                ->setMessageStatus($status)
                ->setMessageMiniType($mini_type)
                ->setMessageParams($params)
                ->sendMessage();
        }

    }




    // 订单核销完成后
    public function classesActivityOrderFinishAfter(&$params)
    {
        ['order' => $order,"user_id"=>$user_id,"oper_type"=>$oper_type,"oper_id"=>$oper_id] = $params;
//        $detail = $order->detail;



        $order = Order::where("id" , $order["id"])->find();
        $detail = $order->detail;
        $user = $order->user;
        $order_item = OrderItem::where("classes_activity_order_id",$order["id"])->find();
        $shop = Shop::where("id",$order['shop_id'])->find();
        //记录订单日志
        $mini_type = "activity_order";
        $to_id = $order["user_id"];
        $status ="activity";
        $params=[
            "event"=>"classes_activity_order_finish_after",
            "order_id"=>$order["id"],
            "order_no"=>$order["order_no"],
            "classes_activity_id"=>$order["classes_activity_id"],
        ];
        $param = [
            "title"=>$detail["title"],
            "name" => $order_item["name"],
            "shop_name" => $shop['name'],
            "order_no" => $order["order_no"],
            "nickname" => $user["nickname"],
            "realname" => $user["realname"],
            "mobile" => $user["mobile"],
            "price" => $order["totalprice"],
            "payprice" => $order["payprice"],
            "real_refundprice" => $order["real_refundprice"],
            "sub_refundprice" => $order["sub_refundprice"],
            "reason" => $order["reason"],
            "address"=>$detail["address"]."(".$detail["address_detail"].")",
            "start_time" => date("Y-m-d H:i",$detail["start_time"]), //格式化日期格式 $order["start_time"], //格式化日期格式
            "end_time" =>   date("Y-m-d H:i",$detail["end_time"]),
        ];

        //发用户
        (new MessageConfig)
            ->setTemplate($params["event"]."_user")
            ->setTemplateData($param)
            ->setToUid($to_id)
            ->setMessageStatus($status)
            ->setMessageMiniType($mini_type)
            ->setMessageParams($params)
            ->sendMessage();
        //发所有核销员
        $verifications = Verification::where("shop_id",$order['shop_id'])->select();
        foreach ($verifications as $verification){
            (new MessageConfig)
                ->setTemplate($params["event"]."_verification")
                ->setTemplateData($param)
                ->setToUid($verification["user_id"])
                ->setMessageStatus($status)
                ->setMessageMiniType($mini_type)
                ->setMessageParams($params)
                ->sendMessage();
        }

    }




}