<?php
class Table_OrderProduct
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_OrderProduct();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_OrderProduct();
    }

    /**
     * @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 = "op_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 = "op_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 = 'op_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 = "",$lock='')
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->where("1 =?", 1);
        /*CONDITION_START*/
        
        if(isset($condition["order_id"]) && $condition["order_id"] != ""){
            $select->where("order_id = ?",$condition["order_id"]);
        }
        if(isset($condition["order_code"]) && $condition["order_code"] != ""){
            $select->where("order_code = ?",$condition["order_code"]);
        }
        if(isset($condition["product_id"]) && $condition["product_id"] != ""){
            $select->where("product_id = ?",$condition["product_id"]);
        }
        if(isset($condition["product_barcode"]) && $condition["product_barcode"] != ""){
            $select->where("product_barcode = ?",$condition["product_barcode"]);
        }
        if(isset($condition["op_quantity"]) && $condition["op_quantity"] != ""){
            $select->where("op_quantity = ?",$condition["op_quantity"]);
        }
        if(isset($condition["op_category"]) && $condition["op_category"] != ""){
            $select->where("op_category = ?",$condition["op_category"]);
        }
        if(isset($condition["op_ref_tnx"]) && $condition["op_ref_tnx"] != ""){
            $select->where("op_ref_tnx = ?",$condition["op_ref_tnx"]);
        }
        if(isset($condition["op_ref_item_id"]) && $condition["op_ref_item_id"] != ""){
            $select->where("op_ref_item_id = ?",$condition["op_ref_item_id"]);
        }
        if(isset($condition["op_ref_buyer_id"]) && $condition["op_ref_buyer_id"] != ""){
            $select->where("op_ref_buyer_id = ?",$condition["op_ref_buyer_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();
            if(!empty($lock)){
                $sql.=" $lock";
            }
            return $this->_table->getAdapter()->fetchAll($sql);
        }
    }

    public function getGoodsId($receiving_code){
        $select = "SELECT DISTINCT p.goods_id FROM order_product op JOIN receiving_order_detail rod on rod.order_code=op.order_code JOIN product p on op.product_id=p.product_id WHERE rod.receiving_code='".$receiving_code."'";
        return $this->_table->getAdapter()->fetchCol($select);
    }

    public function getJoinProductByCondition($condition){
        $sql = "select op.*,p.hs_code,p.hs_goods_name,p.currency_code,p.product_declared_value,p.product_weight,p.goods_id from order_product op join receiving_order_detail rod on  rod.order_code=op.order_code join product p on p.product_id=op.product_id where 1";
        if(isset($condition['goods_id'])&&$condition['goods_id']!=""){
            $sql.= " and p.goods_id= '".$condition['goods_id']."'";
        }
        if(isset($condition['receiving_code'])&&$condition['receiving_code']!=""){
            $sql.= " and rod.receiving_code = '".$condition['receiving_code']."'";
        }
        return $this->_table->getAdapter()->fetchAll($sql);
    }

    public function calNetWeightByCondition($condition){
        $sql = " select SUM(op.op_quantity * p.product_weight) from order_product op join receiving_order_detail rod on  rod.order_code=op.order_code join product p on p.product_id=op.product_id where 1 ";
        if(isset($condition['goods_id'])&&$condition['goods_id']!=""){
            $sql.= " and p.goods_id= '".$condition['goods_id']."'";
        }
        if(isset($condition['receiving_code'])&&$condition['receiving_code']!=""){
            $sql.= " and rod.receiving_code = '".$condition['receiving_code']."'";
        }
        return $this->_table->getAdapter()->fetchOne($sql);
    }
}