<?php
class Table_Product
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_Product();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_Product();
    }

    /**
     * @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 = "product_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 = "product_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 = 'product_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 listByWhere($where) {
    	$select = $this->_table->getAdapter()->select();
    	$table = $this->_table->info('name');
    	$select->from($table, "*");
    	foreach($where as $field=>$value) {
    		$select->where("$field=?", $value);
    	}
    	return $this->_table->getAdapter()->fetchAll($select);
    }

    public function getByWhere($where, $colums = "*") {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $colums);
        foreach($where as $field=>$value) {
            $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["product_sku"]) && $condition["product_sku"] != ""){
            $select->where("product_sku = ?",$condition["product_sku"]);
        }
        if(isset($condition["product_id_gt"]) && $condition["product_id_gt"] != ""){
            $select->where("product_id > ?",$condition["product_id_gt"]);
        }
        if(isset($condition["product_barcode"]) && $condition["product_barcode"] != ""){
            $select->where("product_barcode = ?",$condition["product_barcode"]);
        }
        if(isset($condition["product_id_array"]) && $condition["product_id_array"] != "" && is_array($condition["product_id_array"])){
            $select->where("product_id IN (?)",$condition["product_id_array"]);
        }
        if(isset($condition["customer_code"]) && $condition["customer_code"] != ""){
            $select->where("customer_code = ?",$condition["customer_code"]);
        }
        if(isset($condition["customer_id"]) && $condition["customer_id"] != ""){
            $select->where("customer_id = ?",$condition["customer_id"]);
        }

        if(isset($condition["customeridArr"]) && !empty($condition["customeridArr"]) && is_array($condition["customeridArr"])){
            $select->where("customer_id IN (?)",$condition["customeridArr"]);
        }

        if(isset($condition["product_title_en"]) && $condition["product_title_en"] != ""){
            $select->where("product_title_en = ?",$condition["product_title_en"]);
        }
        if(isset($condition["product_title"]) && $condition["product_title"] != ""){
            $select->where("product_title = ?",$condition["product_title"]);
        }
          if(isset($condition["product_title_like"]) && $condition["product_title_like"] != ""){
            $select->where("product_title like ?",'%'.$condition["product_title_like"].'%');
        }
        if(isset($condition["product_status"]) && $condition["product_status"] != ""){
            $select->where("product_status = ?",$condition["product_status"]);
        }
        if(isset($condition["product_receive_status"]) && $condition["product_receive_status"] != ""){
            $select->where("product_receive_status = ?",$condition["product_receive_status"]);
        }
        if(isset($condition["pu_code"]) && $condition["pu_code"] != ""){
            $select->where("pu_code = ?",$condition["pu_code"]);
        }
        if(isset($condition["product_length"]) && $condition["product_length"] != ""){
            $select->where("product_length = ?",$condition["product_length"]);
        }
        if(isset($condition["product_width"]) && $condition["product_width"] != ""){
            $select->where("product_width = ?",$condition["product_width"]);
        }
        if(isset($condition["product_height"]) && $condition["product_height"] != ""){
            $select->where("product_height = ?",$condition["product_height"]);
        }
        if(isset($condition["product_net_weight"]) && $condition["product_net_weight"] != ""){
            $select->where("product_net_weight = ?",$condition["product_net_weight"]);
        }
        if(isset($condition["product_weight"]) && $condition["product_weight"] != ""){
            $select->where("product_weight = ?",$condition["product_weight"]);
        }
        if(isset($condition["product_sales_value"]) && $condition["product_sales_value"] != ""){
            $select->where("product_sales_value = ?",$condition["product_sales_value"]);
        }
        if(isset($condition["product_purchase_value"]) && $condition["product_purchase_value"] != ""){
            $select->where("product_purchase_value = ?",$condition["product_purchase_value"]);
        }
        if(isset($condition["product_declared_value"]) && $condition["product_declared_value"] != ""){
            $select->where("product_declared_value = ?",$condition["product_declared_value"]);
        }
        if(isset($condition["product_is_qc"]) && $condition["product_is_qc"] != ""){
            $select->where("product_is_qc = ?",$condition["product_is_qc"]);
        }
        if(isset($condition["product_barcode_type"]) && $condition["product_barcode_type"] != ""){
            $select->where("product_barcode_type = ?",$condition["product_barcode_type"]);
        }
        if(isset($condition["product_type"]) && $condition["product_type"] != ""){
            $select->where("product_type = ?",$condition["product_type"]);
        }
        if(isset($condition["pc_id"]) && $condition["pc_id"] != ""){
            $select->where("pc_id = ?",$condition["pc_id"]);
        }
        if(isset($condition["pce_id"]) && $condition["pce_id"] != ""){
            $select->where("pce_id = ?",$condition["pce_id"]);
        }
        if(isset($condition["product_add_time"]) && $condition["product_add_time"] != ""){
            $select->where("product_add_time = ?",$condition["product_add_time"]);
        }

        if(isset($condition["product_sku_keyword"]) && $condition["product_sku_keyword"] != ""){
            $select->where("product_sku like ?",'%'.$condition["product_sku_keyword"].'%');
        }		
        if(isset($condition["goods_id"]) && $condition["goods_id"] != ""){
            $select->where("goods_id = ?",$condition["goods_id"]);
        }
        if(isset($condition['has_invoice'])&&$condition['has_invoice']!=""){
            $select->where(" has_invoice = ? ",$condition['has_invoice']);

        }
        if(isset($condition["dateFor"]) && $condition["dateFor"] != ""){
            $select->where("product_add_time >= ?",$condition["dateFor"]);
        }
        if(isset($condition["dateTo"]) && $condition["dateTo"] != ""){
            $select->where("product_add_time < ?",$condition["dateTo"]);
        }
        if(isset($condition["hs_code"]) && $condition["hs_code"] != ""){
            $select->where("hs_code = ?",$condition["hs_code"]);
        }
        if(isset($condition['customerCodeArr'])&&!empty($condition['customerCodeArr'])){
            $select->where("customer_code IN (?)", $condition["customerCodeArr"]);
        }
        if(isset($condition["hs_goods_name"]) && $condition["hs_goods_name"] != ""){
            $select->where("hs_goods_name like ?",'%'.$condition["hs_goods_name"].'%');
        }
        /*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);
        }
    }

    /**
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $orderBy
     * @return array|string
     */
    public function getAllByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type)
            ->joinLeft('product_category_ebay', $table.'.pce_id = product_category_ebay.pce_id',null);
       // $select->from($table, $type);
        //$select->where("1 =?", 1);

        /*CONDITION_START*/

        if (isset($condition['pi_sellable']) && $condition['pi_sellable']=="largeZero" && isset($condition['warehouse_id'])){
            $select->joinLeft("product_inventory",$table.".product_id=product_inventory.product_id",null);
            $select->where("product_inventory.pi_sellable > ?","0");
            $select->where("product_inventory.warehouse_id = ?", $condition['warehouse_id']);
        }
        else {
          $select->where("1 =?", 1);
        }

        if(isset($condition["product_sku"]) && $condition["product_sku"] != ""){
            $select->where("product_sku = ?",$condition["product_sku"]);
        }
        if(isset($condition["product_barcode"]) && $condition["product_barcode"] != ""){
            $select->where("product_barcode = ?",$condition["product_barcode"]);
        }
        if(isset($condition["customer_code"]) && $condition["customer_code"] != ""){
            $select->where("customer_code = ?",$condition["customer_code"]);
        }
        if(isset($condition["customer_id"]) && $condition["customer_id"] != ""){
            $select->where("customer_id = ?",$condition["customer_id"]);
        }
        if(isset($condition["product_title_en"]) && $condition["product_title_en"] != ""){
            $select->where("product_title_en = ?",$condition["product_title_en"]);
        }
        if(isset($condition["product_title"]) && $condition["product_title"] != ""){
            $select->where("product_title = ?",$condition["product_title"]);
        }
        if(isset($condition["product_status"]) && $condition["product_status"] != ""){
            $select->where("product_status = ?",$condition["product_status"]);
        }
        if(isset($condition["product_receive_status"]) && $condition["product_receive_status"] != ""){
            $select->where("product_receive_status = ?",$condition["product_receive_status"]);
        }
        if(isset($condition["pu_code"]) && $condition["pu_code"] != ""){
            $select->where("pu_code = ?",$condition["pu_code"]);
        }
        if(isset($condition["product_length"]) && $condition["product_length"] != ""){
            $select->where("product_length = ?",$condition["product_length"]);
        }
        if(isset($condition["product_width"]) && $condition["product_width"] != ""){
            $select->where("product_width = ?",$condition["product_width"]);
        }
        if(isset($condition["product_height"]) && $condition["product_height"] != ""){
            $select->where("product_height = ?",$condition["product_height"]);
        }
        if(isset($condition["product_net_weight"]) && $condition["product_net_weight"] != ""){
            $select->where("product_net_weight = ?",$condition["product_net_weight"]);
        }
        if(isset($condition["product_weight"]) && $condition["product_weight"] != ""){
            $select->where("product_weight = ?",$condition["product_weight"]);
        }
        if(isset($condition["product_sales_value"]) && $condition["product_sales_value"] != ""){
            $select->where("product_sales_value = ?",$condition["product_sales_value"]);
        }
        if(isset($condition["product_purchase_value"]) && $condition["product_purchase_value"] != ""){
            $select->where("product_purchase_value = ?",$condition["product_purchase_value"]);
        }
        if(isset($condition["product_declared_value"]) && $condition["product_declared_value"] != ""){
            $select->where("product_declared_value = ?",$condition["product_declared_value"]);
        }
        if(isset($condition["product_is_qc"]) && $condition["product_is_qc"] != ""){
            $select->where("product_is_qc = ?",$condition["product_is_qc"]);
        }
        if(isset($condition["product_barcode_type"]) && $condition["product_barcode_type"] != ""){
            $select->where("product_barcode_type = ?",$condition["product_barcode_type"]);
        }
        if(isset($condition["product_type"]) && $condition["product_type"] != ""){
            $select->where("product_type = ?",$condition["product_type"]);
        }
        if(isset($condition["pc_id"]) && $condition["pc_id"] != ""){
            $select->where("pc_id = ?",$condition["pc_id"]);
        }
        if(isset($condition["pce_id"]) && $condition["pce_id"] != ""){
            $select->where("pce_id = ?",$condition["pce_id"]);
        }
        if(isset($condition["dateFor"]) && $condition["dateFor"] != ""){
            $select->where("product_add_time >= ?",$condition["dateFor"]);
        }
        if(isset($condition["dateTo"]) && $condition["dateTo"] != ""){
            $select->where("product_add_time < ?",$condition["dateTo"]);
        }

        if(isset($condition["product_sku_keyword"]) && $condition["product_sku_keyword"] != ""){
            $select->where("product_sku like ?",'%'.$condition["product_sku_keyword"].'%');
        }
		
        if(isset($condition["goods_id"]) && $condition["goods_id"] != ""){
            $select->where("goods_id = ?",$condition["goods_id"]);
        }
        if(isset($condition['has_invoice'])&&$condition['has_invoice']!=""){
            $select->where(" has_invoice = ? ",$condition['has_invoice']);
        }
        if(isset($condition["hs_code"]) && $condition["hs_code"] != ""){
            $select->where("hs_code = ?",$condition["hs_code"]);
        }
        if(isset($condition['customerCodeArr'])&&!empty($condition['customerCodeArr'])){
            $select->where("customer_code IN (?)", $condition["customerCodeArr"]);
        }
        if(isset($condition["hs_goods_name"]) && $condition["hs_goods_name"] != ""){
            $select->where("hs_goods_name like ?",'%'.$condition["hs_goods_name"].'%');
        }


        /*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();
             //die($select->__toString());
            //die("okkkkkkkkkkkkkkkkkkk");
            return $this->_table->getAdapter()->fetchAll($sql);
        }
    }


    /**
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $orderBy
     * @return array|string
     */
    public function getProductSearchByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        // echo '<pre>';
        // print_r($condition);
        // print_r($type);exit;
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $types = ($type) == 'count(*)' ? "COUNT(DISTINCT product.product_sku,customer.customer_company_name)" : $type;
        if($condition['dateFor'] || $condition['dateTo']){
            $condition['product_search_id'] = 5;//时间shipment_time唯一检索查询
        }
        switch ($condition['product_search_id']) {
            case 1://待入库指产品关联的备货ASN处于待审核状态的产品；
                $select->from($table,$types)
                    ->join('customer', $table.'.customer_id = customer.customer_id','customer.customer_company_name as E28')
                    ->join('receiving_detail', $table.'.product_barcode = receiving_detail.product_barcode','receiving_detail.receiving_code')
                    ->join('receiving', 'receiving.receiving_code = receiving_detail.receiving_code','receiving.receiving_status');
                $select->where("receiving.receiving_status = ?", 3);
                $select->where("receiving.receive_model_type = ?", 0);
                break;
            case 2://库内指目前在系统中有库存的产品；
                $select->from($table,$types)
                ->join('customer', $table.'.customer_id = customer.customer_id','customer.customer_company_name as E28')
                ->join('product_inventory', $table.'.product_id = product_inventory.product_id','product_inventory.pi_sellable');
                $select->orWhere("product_inventory.pi_sellable >0 or product_inventory.pi_reserved >0",$condition["product_id"]);
                break;
            case 3://待出库指产品关联的订单处于已提交到已加挂状态的产品；
                $select->from($table,$types)
                    ->join('customer', $table.'.customer_id = customer.customer_id','customer.customer_company_name as E28')
                    ->join('order_product', $table.'.product_id = order_product.product_id','order_product.order_code')
                    ->join('orders', 'orders.order_code = order_product.order_code','orders.order_status');
                $select->where("orders.order_status in(?)", array(4,5,6,7,8,9,10));
                $select->where("orders.order_mode_type = ?", 0);
                break;
            case 4||5://已出库只产品关联的订单处于物流完成到已签收状态的产品
                $showFields=array(
                    'product_id as E0',
                    'product_barcode as E2',
                    'product_sku as E1',
                    'product_title as E6',
                    'goods_id as E25',
                    'hs_code as E27',
                    'customer_company_name as E28',
                    'shipment_time',
                    );
                $types = ($type) == 'count(*)' ? "count(*)" : $showFields;
               // echo $types;exit;
                return Service_ProductSearch::getLikeByCondition($condition, $types, $pageSize, $page);
                break;
            default://产品状态为全部时，指所有已备案成功的产品。
                $select->from($table,$type)
                ->joinLeft('customer', $table.'.customer_id = customer.customer_id','customer.customer_company_name as E28');
                $select->where($table.".product_status = ?",1);
                break;
        }
        
       // $select->from($table, $type);
        $select->where("1 =?", 1);
        if(isset($condition["product_title"]) && $condition["product_title"] != ""){
            $select->where("product_title like ?",'%'.$condition["product_title"].'%');
        }
        if(isset($condition["hs_code"]) && $condition["hs_code"] != ""){
            $select->where("hs_code like ?",'%'.$condition["hs_code"].'%');
        }
        if(isset($condition["customer.customer_company_name_like"]) && $condition["customer.customer_company_name_like"] != ""){
            $select->where("customer_company_name like ?",'%'.$condition["customer.customer_company_name_like"].'%');
        }
        if(isset($condition["product_sku"]) && $condition["product_sku"] != ""){
            $select->where("product_sku like ?",'%'.$condition["product_sku"].'%');
        }
        if(isset($condition["goods_id"]) && $condition["goods_id"] != ""){
            $select->where("goods_id like ?",'%'.$condition["goods_id"].'%');
        }
        if(isset($condition["dateFor"]) && $condition["dateFor"] != ""){
            $select->where("order_operation_time.shipment_time >= ?",$condition["dateFor"]);
        }
        if(isset($condition["dateTo"]) && $condition["dateTo"] != ""){
            $select->where("order_operation_time.shipment_time <= ?",$condition["dateTo"]);
        }

        /*CONDITION_END*/
        if ('count(*)' == $type) {
            return $this->_table->getAdapter()->fetchOne($select);
        } else {
            //全部不需要去重复
            if(in_array($condition['product_search_id'], array(1,2,3))){
                $select->group(array("product.product_sku","customer.customer_company_name"));
            }
            if (!empty($orderBy)) {
                $select->order($orderBy);
            }
            if ($pageSize > 0 and $page > 0) {
                $start = ($page - 1) * $pageSize;
                $select->limit($pageSize, $start);
            }
            $sql = $select->__toString();
            // die($select->__toString());
            return $this->_table->getAdapter()->fetchAll($sql);
        }
    }
}