<?php

class Table_UploadProductDiffer
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_UploadProductDiffer();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_UploadProductDiffer();
    }

    /**
     * @param $row
     * @return mixed
     */
    public function add($row)
    {
        return $this->_table->insert($row);
    }


    /**
     * @param $row
     * @param $value
     * @param string $field
     * @return mixed
     */
    public function update($row, $value, $field = "upd_id")
    {
        $where = $this->_table->getAdapter()->quoteInto("{$field}= ?", $value);
        return $this->_table->update($row, $where);
    }

    /**
     * @param $value
     * @param string $field
     * @return mixed
     */
    public function delete($value, $field = "upd_id")
    {
        $where = $this->_table->getAdapter()->quoteInto("{$field}= ?", $value);
        return $this->_table->delete($where);
    }

    /**
     * @param $value
     * @param string $field
     * @param string $colums
     * @return mixed
     */
    public function getByField($value, $field = 'upd_id', $colums = "*")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $colums);
        $select->where("{$field} = ?", $value);
        return $this->_table->getAdapter()->fetchRow($select);
    }

    public function getAll()
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, "*");
        return $this->_table->getAdapter()->fetchAll($select);
    }

    /**
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $orderBy
     * @return array|string
     */
    public function getByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->where("1 =?", 1);
        /*CONDITION_START*/
        if (isset($condition["upd_id"]) && $condition["upd_id"] != "") {
            $select->where("upd_id = ?", $condition["upd_id"]);
        }
        if (isset($condition["customer_code"]) && $condition["customer_code"] != "") {
            $select->where("customer_code = ?", $condition["customer_code"]);
        }
        if (isset($condition["product_sku"]) && $condition["product_sku"] != "") {
            $select->where("product_sku = ?", $condition["product_sku"]);
        }
        if (isset($condition["pu_code"]) && $condition["pu_code"] != "") {
            $select->where("pu_code = ?", $condition["pu_code"]);
        }
        if (isset($condition["country_code_of_origin"]) && $condition["country_code_of_origin"] != "") {
            $select->where("country_code_of_origin = ?", $condition["country_code_of_origin"]);
        }
        if (isset($condition["user_id"]) && $condition["user_id"] != "") {
            $select->where("user_id = ?", $condition["user_id"]);
        }
        /*CONDITION_END*/
        if ('count(*)' == $type) {
            return $this->_table->getAdapter()->fetchOne($select);
        } else {
            if (!empty($orderBy)) {
                $select->order($orderBy);
            }
            if ($pageSize > 0 and $page > 0) {
                $start = ($page - 1) * $pageSize;
                $select->limit($pageSize, $start);
            }
            $sql = $select->__toString();
            return $this->_table->getAdapter()->fetchAll($sql);
        }
    }

    /**
     * Note:
     * User: Yao
     * Date: 2019/3/15
     * Time: 9:36
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $order
     */
    public  function getLeftJoinProduct($condition = array(), $type = '*', $pageSize = 0, $page = 1, $order = ""){
        $select= $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from(array('o'=>$table),$type);
        $select->where("1 =?", 1);
        $select->joinLeft(array('p'=>'product'),'p.product_sku=o.product_sku',array(
            'p_product_id'=>'product_id',
            'p_product_sku'=>'product_sku',
            'p_customer_code'=>'customer_code',
            'p_pu_code'=>'pu_code',
            'p_country_code_of_origin'=>'country_code_of_origin',
            'p_hs_code'=>'hs_code',
        ));
        if (isset($condition["upd_id"]) && $condition["upd_id"] != "") {
            $select->where("upd_id = ?", $condition["upd_id"]);
        }
        if (isset($condition["p_customer_code"]) && $condition["p_customer_code"] != "") {
            $select->where("p.customer_code = ?", $condition["p_customer_code"]);
        }
        if (isset($condition["customer_code"]) && $condition["customer_code"] != "") {
            $select->where("o.customer_code = ?", $condition["customer_code"]);
        }
        if (isset($condition["product_sku"]) && $condition["product_sku"] != "") {
            $select->where("o.product_sku = ?", $condition["product_sku"]);
        }
        if (isset($condition["pu_code"]) && $condition["pu_code"] != "") {
            $select->where("o.pu_code = ?", $condition["pu_code"]);
        }
        if (isset($condition["country_code_of_origin"]) && $condition["country_code_of_origin"] != "") {
            $select->where("o.country_code_of_origin = ?", $condition["country_code_of_origin"]);
        }
        if (isset($condition["user_id"]) && $condition["user_id"] != "") {
            $select->where("o.user_id = ?", $condition["user_id"]);
        }
        if (isset($condition["same_customer_code"]) && $condition["same_customer_code"] != "") {
            $select->where("(o.customer_code = p.customer_code) or p.customer_code is null ");
        }
        /*CONDITION_END*/
        if ('count(*)' == $type) {
            return $this->_table->getAdapter()->fetchOne($select);
        } else {
            if (!empty($orderBy)) {
                $select->order($orderBy);
            }
            if ($pageSize > 0 and $page > 0) {
                $start = ($page - 1) * $pageSize;
                $select->limit($pageSize, $start);
            }
            $sql = $select->__toString();

            return $this->_table->getAdapter()->fetchAll($sql);
        }
    }
}