EventSpy.php 5.47 KB
<?php

namespace MyMailer;

class EventSpy extends EventOnline{

    public function saveSpyOrder($spyID,$status,$post){

        if(isset($post['item_id'])){



            $data['item_name']    = isset($post['item_name']) ? $post['item_name']:null;
            $data['item_id']      = $post['item_id'];
            $data['item_url']     = isset($post['item_url']) ? $post['item_url']:null;
            $data['item_image']   = isset($post['item_image']) ? $post['item_image']:null;
            $data['quantity']     = isset($post['quantity']) ? $post['quantity']:null;
            $data['price']        = isset($post['price']) ? $post['price']:null;
            $data['spy_event_id'] = $spyID;
            $data['status']       = $status;
            if($status == 'added'){
                $model = \spyStore::findFirst("item_id='{$data['item_id']}' AND spy_event_id = {$spyID}");
                if($model instanceof \spyStore){
                    $model->save( $data );
                } else {
                    $model = new \spyStore();
                    $model->save( $data );
                }
            } else if($status == 'deleted'){
                $model = \spyStore::findFirst("item_id='{$data['item_id']}' AND spy_event_id = {$spyID}");
                if($model instanceof \spyStore){
                    $model->delete();
                } else {
                    throw new \Exception("there is no item with id {$data['item_id']}");
                }
            }





        }else{
            throw new \Exception("missing item id");
        }

    }

    public function saveSpyData($post,$customer){

        if(isset($post['action'])){
            $data['project_id']  = $post['project_id'];
            $data['customer_id'] = $customer->id;
            $data['action']      = $post['action'];
            $data['date']        = date("Y-m-d H:i:s");
            $data['target_id']   = isset($post['item_id']) ? $post['item_id']:null;
            if($data['target_id']){

                $spy_model = \spyEvent::findFirst("customer_id = '{$customer->id}' AND project_id = {$post['project_id']} AND target_id = {$data['target_id']}");
                if( $spy_model  instanceof \spyEvent){
                    $spy_model->save($data);
                }else{
                    $spy_model = new \spyEvent();
                    $spy_model->save( $data );
                }
            }else{
                $spy_model = new \spyEvent();
                $spy_model->save( $data );
            }

            return $spy_model->id;
        }else{
            throw new \Exception("action missing");
        }

    }

    public function getSpyData($post,$customer){
        if(isset($post['action'])){
            $data['project_id']  = $post['project_id'];
            $data['customer_id'] = $customer->id;
            $data['target_id']   = isset($post['item_id']) ? $post['item_id']:null;

            $spy_model = \spyEvent::findFirst("customer_id = '{$customer->id}' AND project_id = {$post['project_id']} AND target_id = {$data['target_id']}");

            if( $spy_model  instanceof \spyEvent){
                $id = $spy_model->id;
                $spy_model->delete();
                return $id;
            }else{
                throw new \Exception("spy event not found");
            }



        }else{
            throw new \Exception("action missing");
        }

    }

    public function finishSpyOrder($post, $customer){
        if(isset($post['action'])){
            $data['project_id']  = $post['project_id'];
            $data['customer_id'] = $customer->id;
            $data['action']      = $post['action'];
            $data['date']        = date("Y-m-d H:i:s");
            $data['target_id']   = isset($post['item_id']) ? $post['item_id']:null;
            $spy_model = \spyEvent::query()
                ->where("project_id = :project_id:")
                ->andWhere("action ='order_add'")
                ->andWhere("customer_id =:customer_id:")
                ->bind(array("project_id" => $post['project_id'], "customer_id"=>$customer->id))
                ->execute();

            foreach($spy_model as $model){

                $model->update(["action" =>'finish']);
            }

            $events_info = \eventInfo::query()
                ->where("project_id = :project_id:")
                ->andWhere("event_trigger ='order_time_after'")
                ->andWhere("customer_id =:customer_id:")
                ->bind(array("project_id" => $post['project_id'], "customer_id"=>$customer->id))
                ->execute();

            foreach($events_info as $event_info ){
                $event_info->delete();
            }


        }else{
            throw new \Exception("action missing");
        }

    }


    public function getCustomer($post){
        $customers_model = new \customersEmailList();
        $user = $customers_model->findFirst("email = '{$post['email']}' AND project_id = {$post['project_id']} ");
        if($user instanceof \customersEmailList){
            return  $user;
        } else {
            $user['email']       = $post['email'];
            $user['name']        = isset($post['name']) ? $post['name']:'';
            $user['project_id']  = $post['project_id'];
            $user['customer_id'] = isset($post['customer_id']) ? $post['customer_id']:'';
            $user['gender']      = isset($post['gender']) ? $post['gender']:'';
            $user['birthday']    = isset($post['birthday']) ? $post['birthday']:'';
            return $customers_model->save($user);
        }
    }

}