<?php
class DModelMarketplaceMarketplace extends Model
{

	public function getMarketId($data = array()){

		$sql = "SELECT mp.*,
		COALESCE(p.product_id,pgd.product_id) product_id
		FROM oc_marketplace_product  mp
		LEFT JOIN oc_product p ON p.product_id = mp.product_key_id AND mp.tipo_produto = " . Type_ProdutoMkt::Tp_MktProd_Normal.
		" LEFT JOIN oc_product_grid_detail pgd ON pgd.product_grid_id = mp.product_key_id AND mp.tipo_produto = " . Type_ProdutoMkt::Tp_MktProd_Grade.
		" WHERE mp.marketId = '".$data['marketid']."' and mp.tipo_integracao = '".$data['tipo_integracao']."' LIMIT 1";

		$query = $this->db->query($sql);

		return $query->row;

	}

	public function getMktPlaceOrderId($marketplace, $data = array()){
	  $sql =
	   	"SELECT mo.mktOrderId, mo.order_id
		   FROM `oc_marketplace_order` mo
		   inner join oc_order o on (o.order_id = mo.order_id)
		   WHERE mo.tipo_integracao =" . $marketplace;

	  if(isset($data['order_id'])){
      $sql .= " AND mo.order_id =" .(int)$data['order_id'];
    }

    if(isset($data['mkt_order_id'])){
      $sql .= " AND mo.mktOrderId ='" .$data['mkt_order_id']."'";
    }

    $query = $this->db->query($sql);
    if($query->row){
      return $query->row;
    }else{
      return false;
    }
  }

  public function addAPICall($tipoIntegracao, $callId, $callType)
  {

    try {
      $query = $this->db->query("select * from oc_marketplace_api_call mac where mac.tipo_integracao = " . $tipoIntegracao . " AND mac.callId = '" . $callId . "' and mac.callType = '" . $callType . "' and done = 0");
      if (!$query->row) {
        $this->db->startTransaction();
        $dateTime = date('Y-m-d H:i:s');
        $this->db->query("INSERT INTO " . DB_PREFIX . "marketplace_api_call SET `tipo_integracao` = " . $tipoIntegracao . ", `callId` = '" . $this->db->escape($callId) . "', `callType` = '" . $this->db->escape($callType) . "', `dateTime` = '" . $this->db->escape($dateTime) . "', done = 0");
        $this->db->commit();
      }
    } catch (Exception $e) {
      $this->db->rollback();
      return false;
    }

    return true;
  }

    public function hasMarketplaceProductImageFkImage($tipoIntegracao, $imageId)
    {
        $sql = "select id from oc_marketplace_product_image where tipo_marketplace = $tipoIntegracao and product_image_id = $imageId";
        $query = $this->db->query($sql);
        return ($query->num_rows ? $query->row['id'] : false);
    }

    public function hasMarketplaceProductImageFkProduct($tipoIntegracao, $productId)
    {
        $sql = "select id from oc_marketplace_product_image where tipo_marketplace = $tipoIntegracao and product_id = $productId";
        $query = $this->db->query($sql);
        return ($query->num_rows ? $query->row['id'] : false);
    }

    public function saveMarketplaceProductImageFkImage($tipoIntegracao, $imageId, $mktImageId, $idFutura)
    {
        // não deixar duplicar nessa tabela
        $id = $this->hasMarketplaceProductImageFkImage($tipoIntegracao, $imageId);

        if ($id){
            $this->db->query("UPDATE " . DB_PREFIX . "marketplace_product_image
                              SET `id_futura` = " . (integer)($idFutura) . ",
                                  `marketplace_image_id` = '" . $this->db->escape($mktImageId) . "'
                              WHERE id = " . $id);
        } else {
            $this->db->query("INSERT INTO " . DB_PREFIX . "marketplace_product_image
                              SET `tipo_marketplace` = " . (integer)$tipoIntegracao . ",
                                  `product_image_id` = " . (integer)($imageId) . ",
                                  `id_futura` = " . (integer)($idFutura) . ",
                                  `marketplace_image_id` = '" . $this->db->escape($mktImageId) . "'");
        }

        return $this->db->getLastId();
    }

    public function saveMarketplaceProductImageFkProduct($tipoIntegracao, $productId, $mktImageId, $idFutura)
    {
        // não deixar duplicar nessa tabela
        $id = $this->hasMarketplaceProductImageFkProduct($tipoIntegracao, $productId);

        if ($id){
            $this->db->query("UPDATE " . DB_PREFIX . "marketplace_product_image
                              SET `id_futura` = " . (integer)($idFutura) . ",
                                  `marketplace_image_id` = '" . $this->db->escape($mktImageId) . "'
                              WHERE id = " . $id);
        } else {
            $this->db->query("INSERT INTO " . DB_PREFIX . "marketplace_product_image
                              SET `tipo_marketplace` = " . (integer)$tipoIntegracao . ",
                                  `product_id` = " . (integer)($productId) . ",
                                  `id_futura` = " . (integer)($idFutura) . ",
                                  `marketplace_image_id` = '" . $this->db->escape($mktImageId) . "'");
        }

        return $this->db->getLastId();
    }
}
