<?php
namespace app\common\listener\classes;
use app\common\model\manystore\Shop;
use app\common\model\school\classes\Visit;
use app\common\model\school\Message;
use app\common\model\school\MessageConfig;
use app\common\model\User;

class ActivityHook
{




    // 课程审核提交
    public function activityAuthNeedAfter(&$params)
    {
        ['activity' => $activityAuth] = $params;
        $shop = Shop::where("id",$activityAuth['shop_id'])->find();
        if($shop && $shop['user_id']){
            $user = User::where("id",$shop["user_id"])->find();
            //课程推送给老师
            $desc = "您的新课程活动{$activityAuth['title']}已提交审核，审核时间为1-3日内,请耐心等待审核结果";

            $title = "新课程活动审核";
            $mini_type = "activity_apply";
            $to_type="user";
            $to_id = $shop["user_id"];
            $status ="activity";
            $platform="user";
            $oper_id=0;
            $oper_type="system";
            $params=[
                "event"=>"activity_auth_need_after",
                "classes_lib_id"=>$activityAuth["id"],
            ];


            $param = [
                "name" => $shop['name'],
                "shop_realname"=> $user["realname"],
                "shop_nickname"=> $user["nickname"],
                "shop_mobile"=> $user["mobile"],
                "title" => $activityAuth['title'],
                "address"=>$activityAuth["address"]."(".$activityAuth["address_detail"].")",
                "price" => $activityAuth["price"],
                "start_time" => date("Y-m-d H:i",$activityAuth["start_time"]), //格式化日期格式 $order["start_time"], //格式化日期格式
                "end_time" =>   date("Y-m-d H:i",$activityAuth["end_time"]),
                "sign_start_time" => date("Y-m-d H:i",$activityAuth["sign_start_time"]), //格式化日期格式 $order["start_time"], //格式化日期格式
                "sign_end_time" =>   date("Y-m-d H:i",$activityAuth["sign_end_time"]),
                "reason" => $activityAuth['reason'],
            ];

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

        }



    }



    // 课程审核通过后
    public function activityAuthSuccessAfter(&$params)
    {
        ['activity' => $activityAuth] = $params;
        $shop = Shop::where("id",$activityAuth['shop_id'])->find();
        if($shop && $shop['user_id']){
            $user = User::where("id",$shop["user_id"])->find();
            //课程推送给老师
            $desc = "您的新课程活动{$activityAuth['title']}已审核通过，可以在后台操作课程活动上架！";

            $title = "新课程活动审核成功";
            $mini_type = "activity_apply";
            $to_type="user";
            $to_id = $shop["user_id"];
            $status ="activity";
            $platform="user";
            $oper_id=0;
            $oper_type="system";
            $params=[
                "event"=>"activity_auth_success_after",
                "classes_activity_id"=>$activityAuth["id"],
            ];



            $param = [
                "name" => $shop['name'],
                "shop_realname"=> $user["realname"],
                "shop_nickname"=> $user["nickname"],
                "shop_mobile"=> $user["mobile"],
                "title" => $activityAuth['title'],
                "address"=>$activityAuth["address"]."(".$activityAuth["address_detail"].")",
                "price" => $activityAuth["price"],
                "start_time" => date("Y-m-d H:i",$activityAuth["start_time"]), //格式化日期格式 $order["start_time"], //格式化日期格式
                "end_time" =>   date("Y-m-d H:i",$activityAuth["end_time"]),
                "sign_start_time" => date("Y-m-d H:i",$activityAuth["sign_start_time"]), //格式化日期格式 $order["start_time"], //格式化日期格式
                "sign_end_time" =>   date("Y-m-d H:i",$activityAuth["sign_end_time"]),
                "reason" => $activityAuth['reason'],
            ];

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


        }


    }

    // 课程审核失败后
    public function activityAuthFailAfter(&$params)
    {
        ['activity' => $activityAuth] = $params;
        $shop = Shop::where("id",$activityAuth['shop_id'])->find();
        if($shop && $shop['user_id']){
            $user = User::where("id",$shop["user_id"])->find();
//课程推送给老师
            $desc = "您的新课程活动{$activityAuth['title']}审核未通过，未通过原因为:{$activityAuth['reason']},整改后,可以在后台重新提交！";

            $title = "新课程活动审核不通过";
            $mini_type = "activity_apply";
            $to_type="user";
            $to_id = $shop["user_id"];
            $status ="activity";
            $platform="user";
            $oper_id=0;
            $oper_type="system";
            $params=[
                "event"=>"activity_auth_fail_after",
                "classes_activity_id"=>$activityAuth["id"],
            ];




            $param = [
                "name" => $shop['name'],
                "shop_realname"=> $user["realname"],
                "shop_nickname"=> $user["nickname"],
                "shop_mobile"=> $user["mobile"],
                "title" => $activityAuth['title'],
                "address"=>$activityAuth["address"]."(".$activityAuth["address_detail"].")",
                "price" => $activityAuth["price"],
                "start_time" => date("Y-m-d H:i",$activityAuth["start_time"]), //格式化日期格式 $order["start_time"], //格式化日期格式
                "end_time" =>   date("Y-m-d H:i",$activityAuth["end_time"]),
                "sign_start_time" => date("Y-m-d H:i",$activityAuth["sign_start_time"]), //格式化日期格式 $order["start_time"], //格式化日期格式
                "sign_end_time" =>   date("Y-m-d H:i",$activityAuth["sign_end_time"]),
                "reason" => $activityAuth['reason'],
            ];

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


        }



    }


        public function activityViewAfter(&$params)
        {
            ['activity' => $activity,"user_id"=>$user_id,"oper_type"=>$oper_type,"oper_id"=>$oper_id] = $params;
//访问记录
            Visit::create([
                "classes_lib_id"=>0,
                "classes_activity_id"=>$activity["id"],
                "shop_id"=>$activity["shop_id"],
                "status"=>'2',
                "user_id"=>$user_id,
            ]);
        }


    public function activityCollectSuccessAfter(&$params)
    {
        ['activity' => $activity,"user_id"=>$user_id,"oper_type"=>$oper_type,"oper_id"=>$oper_id] = $params;

    }

    public function activityCollectCancelAfter(&$params)
    {
        ['activity' => $activity,"user_id"=>$user_id,"oper_type"=>$oper_type,"oper_id"=>$oper_id] = $params;

    }




}