<?php

class Table_ImportDataDetailFormat
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_ImportDataDetailFormat();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_ImportDataDetailFormat();
    }

    /**
     * @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 = "iddf_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 = "iddf_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 = 'iddf_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 getTotal($id = '', $code = '')
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $colums = array(
            'sum(iddf_value) as amount'
        );
        $select->from($table, $colums);
        $select->where("id_id = ?", $id);
        $select->where("iddf_code = ?", $code);
        return $this->_table->getAdapter()->fetchOne($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["id_id"]) && $condition["id_id"] !== "") {
            $select->where("id_id = ?", $condition["id_id"]);
        }
        if (isset($condition["iddf_rowno"]) && $condition["iddf_rowno"] != "") {
            $select->where("iddf_rowno = ?", $condition["iddf_rowno"]);
        }
        if (isset($condition["iddf_title"]) && $condition["iddf_title"] != "") {
            $select->where("iddf_title = ?", $condition["iddf_title"]);
        }
        if (isset($condition["iddf_code"]) && $condition["iddf_code"] != "") {
            $select->where("iddf_code = ?", $condition["iddf_code"]);
        }
        if (isset($condition["iddf_value"]) && $condition["iddf_value"] != "") {
            $select->where("iddf_value = ?", $condition["iddf_value"]);
        }
        /*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);
        }
    }


    public function getGroupByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $types = ($type) == 'count(*)' ? "COUNT(DISTINCT(import_data_detail_format.iddf_rowno))" : $type;
        $select->from($table, $types);
        $select->where("1 =?", 1);
        /*CONDITION_START*/

        if (isset($condition["id_id"]) && $condition["id_id"] !== "") {
            $select->where("id_id = ?", $condition["id_id"]);
        }

        /*CONDITION_END*/
        if ('count(*)' == $type) {
            return $this->_table->getAdapter()->fetchOne($select);
        } else {
            $select->group("import_data_detail_format.iddf_rowno");
            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);
        }
    }
}