<?php

namespace Bspb\Pay;

trait DBTrait
{
    //use OverallTrait;

    private $db;
    private $tableFullName;

    private function getOrderByRecordId(int $recordId)
    {
        $sql = $this->db->prepare("SELECT * FROM `{$this->tableFullName}` WHERE `id` = %d;",$recordId);
        return $this->db->get_row($sql,'ARRAY_A');
    }

    private function getOrderByWPOrderId(int $wpOrderId)
    {
        $sql = $this->db->prepare("SELECT * FROM `{$this->tableFullName}` WHERE `wp_order_id` = %d;",$wpOrderId);
        return $this->db->get_row($sql,'ARRAY_A');
    }

    private function getOrderByBankOrderId(string $bankOrderId)
    {
        $sql = $this->db->prepare("SELECT * FROM `{$this->tableFullName}` WHERE `bank_order_id` = %s;",$bankOrderId);
        return $this->db->get_row($sql,'ARRAY_A');
    }

    private function getOrders(int $pageNum = 1, int $limit = 0)
    {
        $query = "SELECT * FROM `{$this->tableFullName}` ";
        $sqlParams = [1];

        $pageNum--;
        if ( $limit > 0 ) {
            $query.="ORDER BY `id` DESC LIMIT %d,%d;";
            $sqlParams = [( $pageNum * $limit ),$limit];
        } else {
            $query.="WHERE %d ORDER BY `id` DESC;";
        }

        $sql = $this->db->prepare($query,$sqlParams);
        return $this->db->get_results($sql,'ARRAY_A');
    }

    private function getCountOrders(): int
    {
        return intval($this->db->get_row("SELECT COUNT(*) as cnt from `{$this->tableFullName}`;",'ARRAY_A')['cnt']);
    }

    private function createOrder(array $orderData)
    {
        $orderData=array_merge($orderData,[
            'created'=> (new \DateTime())->format('Y-m-d H:i:s'),
            'updated'=> (new \DateTime())->format('Y-m-d H:i:s')
        ]);
        $coltypes=[];
        foreach ($orderData as $key=>$vl) {
            $type = '%d';
            switch ($key) {
                case 'created':
                case 'updated':
                case 'password':
                case 'hppUrl':
                case 'bank_order_id':
                case 'status':
                case 'basketdata':
                    $type = '%s';
            }
            $coltypes[] = $type;
        }
        return $this->db->insert($this->tableFullName,$orderData,$coltypes);
    }

    private function updateOrder(array $orderData, int $recordId)
    {
        $orderData=array_merge($orderData,['updated'=> (new \DateTime())->format('Y-m-d H:i:s')]);
        $coltypes=[];
        foreach ($orderData as $key=>$vl) {
            $type = '%d';
            switch ($key) {
                case 'created':
                case 'updated':
                case 'password':
                case 'hppUrl':
                case 'bank_order_id':
                case 'status':
                case 'basketdata':
                    $type = '%s';
            }
            $coltypes[] = $type;
        }
        return $this->db->update($this->tableFullName,$orderData,['id'=>$recordId],$coltypes,['%d']);
    }

}
