<?php

class Table_ShippingCount
{
    const ACCOUNT_FETCH_ALL = 0;
    const ACCOUNT_FETCH_ROW = 1;
    const ACCOUNT_FETCH_ONE = 2;

    protected $_table = NULL;

    public function __construct() {
        $this->_table = new DbTable_ShippingCount();
    }

    public function insert($data) {
        return $this->_table->insert($data);
    }

    public function getByField($value, $field = 'customer_code', $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);
    }

    /**
     * 根据指定条件获取相关数据
     *
     * @param array $condition 需要检索的条件
     * @param boolean $count 只获取统计数量，默认为FALSE
     * @return mixed
     */
     public function getByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "", $groupBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);

        $select->where("1 =?", 1);
        /*CONDITION_START*/

        if (isset($condition["dateFrom"]) && $condition["dateFrom"] != "") {
            $select->where("date >= ?", $condition["dateFrom"]);
        }
        if (isset($condition["dateTo"]) && $condition["dateTo"] != "") {
            $select->where("date <= ?", $condition["dateTo"]);
        }
        if (isset($condition["customer_code"]) && $condition["customer_code"] != "") {
            $select->where("shipping_count.customer_code = ?", $condition["customer_code"]);
        }
        if (isset($condition["date"]) && $condition["date"] != "") {
            $select->where("date = ?", $condition["date"]);
        }

        /*CONDITION_END*/
        if ('count(*)' == $type) {
            return $this->_table->getAdapter()->fetchOne($select);
        } else {
            if (!empty($orderBy)) {
                $select->order($orderBy);
            }
            if (!empty($groupBy)) {
                $select->group($groupBy);
            }
            if ($pageSize > 0 and $page > 0) {
                $start = ($page - 1) * $pageSize;
                $select->limit($pageSize, $start);
            }
            $sql = $select->__toString();
            return $this->_table->getAdapter()->fetchAll($sql);
        }
    }

    /**
     * 根据指定条件获取相关数据
     *
     * @param array $condition 需要检索的条件
     * @param boolean $count 只获取统计数量，默认为FALSE
     * @return mixed
     */
     public function getByAllCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "", $groupBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->joinLeft('customer', "customer.customer_code=shipping_count.customer_code", array('customer_company_name'));
        $select->where("1 =?", 1);
        /*CONDITION_START*/

        if (isset($condition["dateFrom"]) && $condition["dateFrom"] != "") {
            $select->where("date >= ?", $condition["dateFrom"]);
        }
        if (isset($condition["dateTo"]) && $condition["dateTo"] != "") {
            $select->where("date <= ?", $condition["dateTo"]);
        }
        if (isset($condition["customer_code"]) && $condition["customer_code"] != "") {
            $select->where("shipping_count.customer_code = ?", $condition["customer_code"]);
        }
        if (isset($condition["date"]) && $condition["date"] != "") {
            $select->where("date = ?", $condition["date"]);
        }

        /*CONDITION_END*/
        if ('count(*)' == $type) {
            return $this->_table->getAdapter()->fetchOne($select);
        } else {
            if (!empty($orderBy)) {
                $select->order($orderBy);
            }
            if (!empty($groupBy)) {
                $select->group($groupBy);
            }
            if ($pageSize > 0 and $page > 0) {
                $start = ($page - 1) * $pageSize;
                $select->limit($pageSize, $start);
            }
            $sql = $select->__toString();
            return $this->_table->getAdapter()->fetchAll($sql);
        }
    }

    /**
     * 根据指定条件获取相关数据
     *
     * @param array $condition 需要检索的条件
     * @param boolean $count 只获取统计数量，默认为FALSE
     * @return mixed
     */
     public function getWithProductInfoByCondition($condition = array(), $type = '*', $orderBy = "", $groupBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->joinLeft('product', "product.product_id = shipping_count.product_id", array('product_sku', 'product_title'));
        $select->joinLeft('hs_element_map', "hs_element_map.product_id = shipping_count.product_id", array('group_concat(hs_element_map.hem_detail separator "|") as hem_detail'));

        $select->where("1 =?", 1);
        /*CONDITION_START*/

        if (isset($condition["customer_code"]) && $condition["customer_code"] != "") {
            $select->where("shipping_count.customer_code = ?", $condition["customer_code"]);
        } else {
            $select->joinLeft('customer', "customer.customer_code=shipping_count.customer_code", array('customer_company_name', 'customer_email'));
        }
        if (isset($condition["date"]) && $condition["date"] != "") {
            $select->where("date = ?", $condition["date"]);
        }

        /*CONDITION_END*/
        if ('count(*)' == $type) {
            return $this->_table->getAdapter()->fetchOne($select);
        } else {
            if (!empty($orderBy)) {
                $select->order($orderBy);
            }
            if (!empty($groupBy)) {
                $select->group($groupBy);
            }
            if ($pageSize > 0 and $page > 0) {
                $start = ($page - 1) * $pageSize;
                $select->limit($pageSize, $start);
            }
            $sql = $select->__toString();
            return $this->_table->getAdapter()->fetchAll($sql);
        }
    }
}
